From: Kim Kibum Date: Sun, 29 Apr 2012 07:58:34 +0000 (+0900) Subject: upload tizen1.0 source X-Git-Tag: 2.0_alpha~25 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cc43abce746e1ab0b51711a81371c6983d2de4b4;p=platform%2Fcore%2Fapi%2Furl-download.git upload tizen1.0 source --- diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..58b5f46 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,89 @@ + +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +SET(fw_name "capi-web-url-download") + +PROJECT(${fw_name}) + +SET(CMAKE_INSTALL_PREFIX /usr) +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) + +SET(INC_DIR include) +INCLUDE_DIRECTORIES(${INC_DIR}) + +SET(requires "dlog capi-base-common libdownload-agent bundle") +SET(pc_requires "capi-base-common") + +INCLUDE(FindPkgConfig) +pkg_check_modules(${fw_name} REQUIRED ${requires}) +FOREACH(flag ${${fw_name}_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIC -Wall -Werror") +SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") + +IF("${ARCH}" STREQUAL "arm") + ADD_DEFINITIONS("-DTARGET") +ENDIF("${ARCH}" STREQUAL "arm") + +ADD_DEFINITIONS("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"") +ADD_DEFINITIONS("-DSLP_DEBUG") + +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath=/usr/lib") + +aux_source_directory(src SOURCES) +ADD_LIBRARY(${fw_name} SHARED ${SOURCES}) + +TARGET_LINK_LIBRARIES(${fw_name} ${${fw_name}_LDFLAGS}) + +INSTALL(TARGETS ${fw_name} DESTINATION lib) +INSTALL( + DIRECTORY ${INC_DIR}/ DESTINATION include/web + FILES_MATCHING + PATTERN "*_private.h" EXCLUDE + PATTERN "${INC_DIR}/*.h" + ) + +SET(PC_NAME ${fw_name}) +SET(PC_REQUIRED ${pc_requires}) +SET(PC_LDFLAGS -l${fw_name}) +SET(PC_CFLAGS -I\${includedir}/system) + +CONFIGURE_FILE( + capi-web-url-download.pc.in + ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name}.pc + @ONLY +) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name}.pc DESTINATION lib/pkgconfig) + +IF(UNIX) + +ADD_CUSTOM_TARGET (distclean @echo cleaning for source distribution) +ADD_CUSTOM_COMMAND( + DEPENDS clean + COMMENT "distribution clean" + COMMAND find + ARGS . + -not -name config.cmake -and \( + -name tester.c -or + -name Testing -or + -name CMakeFiles -or + -name cmake.depends -or + -name cmake.check_depends -or + -name CMakeCache.txt -or + -name cmake.check_cache -or + -name *.cmake -or + -name Makefile -or + -name core -or + -name core.* -or + -name gmon.out -or + -name install_manifest.txt -or + -name *.pc -or + -name *~ \) + | grep -v TC | xargs rm -rf + TARGET distclean + VERBATIM +) + +ENDIF(UNIX) + diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..bbe9d02 --- /dev/null +++ b/LICENSE @@ -0,0 +1,206 @@ +Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + diff --git a/capi-web-url-download.pc.in b/capi-web-url-download.pc.in new file mode 100644 index 0000000..2ecfef5 --- /dev/null +++ b/capi-web-url-download.pc.in @@ -0,0 +1,15 @@ + +# Package Information for pkg-config + +prefix=@PREFIX@ +exec_prefix=/usr +libdir=/usr/lib +includedir=/usr/include/web + +Name: @PC_NAME@ +Description: @PACKAGE_DESCRIPTION@ +Version: @VERSION@ +Requires: @PC_REQUIRED@ +Libs: -L${libdir} @PC_LDFLAGS@ +Cflags: -I${includedir} + diff --git a/debian/README b/debian/README new file mode 100755 index 0000000..e69de29 diff --git a/debian/capi-web-url-download-dev.install b/debian/capi-web-url-download-dev.install new file mode 100755 index 0000000..761a28b --- /dev/null +++ b/debian/capi-web-url-download-dev.install @@ -0,0 +1,4 @@ +/usr/include/* +/usr/include/*/* +/usr/lib/pkgconfig/*.pc + diff --git a/debian/capi-web-url-download-dev.postinst b/debian/capi-web-url-download-dev.postinst new file mode 100755 index 0000000..1a24852 --- /dev/null +++ b/debian/capi-web-url-download-dev.postinst @@ -0,0 +1 @@ +#!/bin/sh diff --git a/debian/capi-web-url-download.install b/debian/capi-web-url-download.install new file mode 100755 index 0000000..4a755a4 --- /dev/null +++ b/debian/capi-web-url-download.install @@ -0,0 +1 @@ +/usr/lib/lib*.so* diff --git a/debian/capi-web-url-download.postinst b/debian/capi-web-url-download.postinst new file mode 100755 index 0000000..1a24852 --- /dev/null +++ b/debian/capi-web-url-download.postinst @@ -0,0 +1 @@ +#!/bin/sh diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..1fb8f4a --- /dev/null +++ b/debian/changelog @@ -0,0 +1,55 @@ +capi-web-url-download (0.0.3-0) unstable; urgency=low + + * Git: slp/api/url-download + * Tag: capi-web-url-download_0.0.3-0 + + * Renew TC cases for DTS + - According to guideline of DTS TC, each test cases are seperated at each tc file one by one + * Change build configuration file. + - The mischaracter and dependency is changed at debian control file + + -- Jungki Kwak Thu, 15 Mar 2012 23:25:44 +0900 + +capi-web-url-download (0.0.2-1) unstable; urgency=low + + * Git: slp/api/url-download + * Tag: capi-web-url-download_0.0.2-1 + + * Rollback the code about unsigned long int type. + * Rename the spec file for rpm build + + -- Jungki Kwak Thu, 08 Mar 2012 18:19:09 +0900 + +capi-web-url-download (0.0.2-0) unstable; urgency=low + + * Git: slp/api/url-download + * Tag: capi-web-url-download_0.0.2-0 + + * Change the type of file size. + * Create spec file for rpm build. + * Implement sample code for url download CAPI. + * Use linked list for storing downloading items. + * Available to cancel an download item regardless of the state. + * Modify configurations of Test cases. + * Add comments about g_type_init() at a header file. + * + + -- Jungki Kwak Wed, 07 Mar 2012 15:41:03 +0900 + +capi-web-url-download (0.0.1-2) unstable; urgency=low + + * Bug fixed : memory leak + * Git: slp/api/url-download + * Tag: capi-web-url-download_0.0.1-2 + + -- Junghyuk Park Tue, 21 Feb 2012 14:51:04 +0900 + +capi-web-url-download (0.0.1-1) unstable; urgency=low + + * Initial upload + * Git: slp/api/url-download + * Tag: capi-web-url-download_0.0.1-1 + + -- Junghyuk Park Thu, 16 Feb 2012 15:21:27 +0900 + + diff --git a/debian/compat b/debian/compat new file mode 100755 index 0000000..7ed6ff8 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +5 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..d535273 --- /dev/null +++ b/debian/control @@ -0,0 +1,22 @@ + +Source: capi-web-url-download +Section: libs +Priority: extra +Maintainer: Woongsuk Cho , junghyuk park , JungKi Kwak , InBum Chang +Build-Depends: debhelper (>= 5), dlog-dev, capi-base-common-dev, libdownload-agent-dev, libbundle-dev + +Package: capi-web-url-download +Architecture: any +Depends: ${shlibs:Depends}, ${misc:Depends} +Description: A URL Download library in Tizen Native API + +Package: capi-web-url-download-dev +Architecture: any +Depends: ${shlibs:Depends}, ${misc:Depends}, capi-web-url-download (= ${Source-Version}), capi-base-common-dev +Description: A URL Download library in Tizen Native API (DEV) + +Package: capi-web-url-download-dbg +Architecture: any +Depends: ${shlibs:Depends}, ${misc:Depends}, capi-web-url-download (= ${Source-Version}) +Description: A URL Download library in Tizen Native API (DBG) + diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..3fd0be9 --- /dev/null +++ b/debian/rules @@ -0,0 +1,65 @@ +#!/usr/bin/make -f + +CFLAGS = -Wall -g + +ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS))) + CFLAGS += -O0 +else + CFLAGS += -O2 +endif +CMAKE_ROOT_DIR ?= $(CURDIR) +CMAKE_BUILD_DIR ?= $(CURDIR)/cmake_build_tmp + +configure: configure-stamp +configure-stamp: + dh_testdir + mkdir -p $(CMAKE_BUILD_DIR) && cd $(CMAKE_BUILD_DIR) && cmake .. + touch configure-stamp + + +build: build-stamp +build-stamp: configure-stamp + dh_testdir + cd $(CMAKE_BUILD_DIR) && $(MAKE) + touch $@ + +clean: + cd $(CMAKE_ROOT_DIR) + dh_testdir + dh_testroot + rm -f build-stamp configure-stamp + rm -f `find . -name *.pc` + rm -rf $(CMAKE_BUILD_DIR) + dh_clean + +install: build + dh_testdir + dh_testroot + dh_clean -k + dh_installdirs + + cd $(CMAKE_BUILD_DIR) && $(MAKE) DESTDIR=$(CURDIR)/debian/tmp install + +binary-indep: build install + +binary-arch: build install + dh_testdir + dh_testroot + dh_installchangelogs + dh_installdocs + dh_installexamples + dh_install --sourcedir=debian/tmp + dh_installman + dh_link + dh_strip --dbg-package=capi-web-url-download-dbg + dh_fixperms + dh_makeshlibs + dh_installdeb + dh_shlibdeps + dh_gencontrol + dh_md5sums + dh_builddeb + +binary: binary-indep binary-arch +.PHONY: build clean binary-indep binary-arch binary install configure + diff --git a/include/url_download.h b/include/url_download.h new file mode 100644 index 0000000..cc5d4b7 --- /dev/null +++ b/include/url_download.h @@ -0,0 +1,611 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __TIZEN_WEB_URL_DOWNLOAD_H__ +#define __TIZEN_WEB_URL_DOWNLOAD_H__ + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @addtogroup CAPI_WEB_URL_DOWNLOAD_MODULE + * @{ + */ + +/** + * @brief Name for download service operation. Download Manager application is launched. + */ +#define SERVICE_OPERATION_DOWNLOAD "http://tizen.org/appsvc/operation/download" + + +/** + * @brief URL download handle. + */ +typedef struct url_download_s *url_download_h; + + +/** + * @brief Enumeration of error code for URL download + */ +typedef enum +{ + URL_DOWNLOAD_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */ + URL_DOWNLOAD_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */ + URL_DOWNLOAD_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */ + URL_DOWNLOAD_ERROR_IO_ERROR = TIZEN_ERROR_IO_ERROR , /**< Internal I/O error */ + URL_DOWNLOAD_ERROR_NETWORK_UNREACHABLE = TIZEN_ERROR_NETWORK_UNREACHABLE, /**< Network is unreachable */ + URL_DOWNLOAD_ERROR_CONNECTION_TIMED_OUT = TIZEN_ERROR_CONNECTION_TIME_OUT, /**< Connection timed out */ + URL_DOWNLOAD_ERROR_NO_SPACE = TIZEN_ERROR_FILE_NO_SPACE_ON_DEVICE, /**< No space left on device */ + URL_DOWNLOAD_ERROR_FIELD_NOT_FOUND = TIZEN_ERROR_KEY_NOT_AVAILABLE, /**< Specified field not found */ + URL_DOWNLOAD_ERROR_INVALID_STATE = TIZEN_ERROR_WEB_CLASS | 0x21, /**< Invalid state */ + URL_DOWNLOAD_ERROR_CONNECTION_FAILED = TIZEN_ERROR_WEB_CLASS | 0x22, /**< Connection failed */ + URL_DOWNLOAD_ERROR_SSL_FAILED = TIZEN_ERROR_WEB_CLASS | 0x23, /**< SSL negotiation failed */ + URL_DOWNLOAD_ERROR_INVALID_URL = TIZEN_ERROR_WEB_CLASS | 0x24, /**< Invalid URL */ + URL_DOWNLOAD_ERROR_INVALID_DESTINATION = TIZEN_ERROR_WEB_CLASS | 0x25, /**< Invalid destination */ +} url_download_error_e; + + +/** + * @brief Enumerations of state of download + */ +typedef enum +{ + URL_DOWNLOAD_STATE_READY, /**< It is ready to download */ + URL_DOWNLOAD_STATE_DOWNLOADING, /**< The download is currently running */ + URL_DOWNLOAD_STATE_PAUSED, /**< The download is waiting to resume or stop */ + URL_DOWNLOAD_STATE_COMPLETED, /**< The download is completed. */ +} url_download_state_e; + + +/** + * @brief Called when the download is started. + * + * @param [in] download The download handle + * @param [in] user_data The user data passed from url_download_set_started_cb() + * @pre url_download_start() will cause this callback if you register this callback using url_download_set_started_cb() + * @see url_download_start() + * @see url_download_set_started_cb() + * @see url_download_unset_started_cb() + */ +typedef void (*url_download_started_cb) (url_download_h download, void *user_data); + + +/** + * @brief Called when the download is paused. + * + * @param [in] download The download handle + * @param [in] user_data The user data passed from url_download_set_paused_cb() + * @pre url_download_pause() will cause this callback if you register this callback using url_download_set_paused_cb() + * @see url_download_pause() + * @see url_download_set_paused_cb() + * @see url_download_unset_paused_cb() + */ +typedef void (*url_download_paused_cb) (url_download_h download, void *user_data); + + +/** + * @brief Called when the download is completed. + * + * @param [in] download The download handle + * @param [in] path The absolute path to the downloaded file + * @param [in] user_data The user data passed from url_download_set_completed_cb() + * @pre This callback function will be invoked when the download is completed if you register this callback using url_download_set_paused_cb() + * @see url_download_set_completed_cb() + * @see url_download_unset_completed_cb() + */ +typedef void (*url_download_completed_cb) (url_download_h download, const char * path, void *user_data); + + +/** + * @brief Called when the download is stopped. + * + * @remarks This callback function is invoked when the download is explicitly stopped by url_download_stop(). \n + * And this callback function is also invoked when the download failed from either downloading or paused state. + * @param [in] download The download handle + * @param [in] error The error code \n + * If the download is stopped with url_download_stop(), the error code is #URL_DOWNLOAD_ERROR_NONE + * @param [in] user_data The user data passed from url_download_set_stopped_cb() + * @pre This callback function is invoked if you register this callback using url_download_set_stopped_cb(). + * @see url_download_stop() + * @see url_download_set_stopped_cb() + * @see url_download_unset_stopped_cb() + */ +typedef void (*url_download_stopped_cb) (url_download_h download, url_download_error_e error, void *user_data); + + +/** + * @brief Called when the progress of download changes. + * + * @remarks This callback function is only invoked in the downloading state. + * @param [in] download The download handle + * @param [in] received The size of the data received in bytes + * @param [in] total The total size of the data to receive in bytes + * @param [in] user_data The user data passed from url_download_set_progress_cb() + * @pre This callback function is invoked if you register this callback using url_download_set_progress_cb(). + * @see url_download_stop() + * @see url_download_set_progress_cb() + * @see url_download_unset_progress_cb() + */ +typedef void (*url_download_progress_cb) (url_download_h download, unsigned long long received, unsigned long long total, void *user_data); + + +/** +* @brief Called to retrieve the HTTP header field to be included with the download +* +* @remarks The @a field must not be deallocated by an application. +* @param [in] download The download handle +* @param [in] field The HTTP header field +* @param [in] user_data The user data passed from the foreach function +* @return @c true to continue with the next iteration of the loop, \n @c false to break out of the loop. +* @pre url_download_foreach_http_header_field() will invoke this callback. +* @see url_download_foreach_http_header_field() +*/ +typedef bool (*url_download_http_header_field_cb)(url_download_h download, const char *field, void *user_data); + + +/** + * @brief Creates a download handle. + * + * @remarks The @a download must be released with url_download_destroy() by you.\n + * The g_type_init() should be called when creating a main loop by user side. \n + * Because the libsoup, which is http stack library of download module, use gobject internally. + * @param [out] download A download handle to be newly created on success + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #URL_DOWNLOAD_ERROR_IO_ERROR Internal I/O error + * @post The download state will be #URL_DOWNLOAD_STATE_READY + * @see url_download_destroy() + */ +int url_download_create(url_download_h *download); + + +/** + * @brief Destroys the URL download handle. + * + * @param [in] download The download handle + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @see url_download_create() + */ +int url_download_destroy(url_download_h download); + + +/** + * @brief Sets the URL to download. + * + * @remarks This function should be called before downloading (See url_download_start()) + * @param [in] download The download handle + * @param [in] url The URL to download \n + * If the @a url is NULL, it clears the previous value. + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @pre The download state must be #URL_DOWNLOAD_STATE_READY or #URL_DOWNLOAD_STATE_COMPLETED. + * @see url_download_get_url() + */ +int url_download_set_url(url_download_h download, const char *url); + + +/** + * @brief Gets the URL to download. + * + * @remarks The @a url must be released with free() by you. + * @param [in] download The download handle + * @param [out] url The URL to download + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @see url_download_set_url() + */ +int url_download_get_url(url_download_h download, char **url); + + +/** + * @brief Sets the destination for the downloaded file. + * + * @details The file will be downloaded to the specified destination. + * The downloaded file is saved to an auto-generated file name in the destination. (See url_download_completed_cb()) + * If the destination is not specified, the file will be downloaded to default storage. (See the @ref CAPI_STORAGE_MODULE API) + * + * @remarks This function should be called before downloading (see url_download_start()) + * @param [in] download The download handle + * @param [in] path The absolute path to the downloaded file + * If the @a path is NULL, it clears the previous value. + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @pre The download state must be #URL_DOWNLOAD_STATE_READY or #URL_DOWNLOAD_STATE_COMPLETED. + * @see url_download_get_destination() + */ +int url_download_set_destination(url_download_h download, const char *path); + + +/** + * @brief Gets the destination for the downloaded file. + * + * @remarks The @a path must be released with free() by you. + * @param [in] download The download handle + * @param [out] path The absolute path to the downloaded file + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @see url_download_set_destination() + */ +int url_download_get_destination(url_download_h download, char **path); + + +/** + * @brief Adds an HTTP header field to the download request + * + * @details The given HTTP header field will be included with the HTTP request of the download request. \n + * Refer to the HTTP/1.1: HTTP Message Headers + * @remarks This function should be called before downloading (see url_download_start()) \n + * This function replaces any existing value for the given key. \n + * This function returns #URL_DOWNLOAD_ERROR_INVALID_PARAMETER if field or value is zero-length string. + * @param [in] download The download handle + * @param [in] field The name of the HTTP header field + * @param [in] value The value associated with given field + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #URL_DOWNLOAD_ERROR_IO_ERROR Internal I/O error + * @pre The download state must be #URL_DOWNLOAD_STATE_READY or #URL_DOWNLOAD_STATE_COMPLETED. + * @see url_download_get_http_header_field() + * @see url_download_remove_http_header_field() + */ +int url_download_add_http_header_field(url_download_h download, const char *field, const char *value); + + +/** + * @brief Gets the value associated with given HTTP header field from the download + * + * @remarks This function returns #URL_DOWNLOAD_ERROR_INVALID_PARAMETER if field is zero-length string. \n + * The @a value must be released with free() by you. + * @param [in] download The download handle + * @param [in] field The name of the HTTP header field + * @param [out] value The value associated with given field + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #URL_DOWNLOAD_ERROR_FIELD_NOT_FOUND Specified field not found + * @see url_download_add_http_header_field() + * @see url_download_remove_http_header_field() + */ +int url_download_get_http_header_field(url_download_h download, const char *field, char **value); + + +/** + * @brief Removes the given HTTP header field from the download + * + * @remarks This function should be called before downloading (see url_download_start()) \n + * This function returns #URL_DOWNLOAD_ERROR_INVALID_PARAMETER if field is zero-length string. + * @param [in] download The download handle + * @param [in] field The name of the HTTP header field + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #URL_DOWNLOAD_ERROR_FIELD_NOT_FOUND Specified field not found + * @retval #URL_DOWNLOAD_ERROR_IO_ERROR Internal I/O error + * @see url_download_add_http_header_field() + * @see url_download_get_http_header_field() + */ +int url_download_remove_http_header_field(url_download_h download, const char *field); + + +/** + * @brief Registers a callback function to be invoked when the download starts. + * + * @remarks This function should be called before downloading (see url_download_start()) + * @param [in] download The download handle + * @param [in] callback The callback function to register + * @param [in] user_data The user data to be passed to the callback function + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @pre The download state must be #URL_DOWNLOAD_STATE_READY or #URL_DOWNLOAD_STATE_COMPLETED. + * @post url_download_started_cb() will be invoked. + * @see url_download_unset_started_cb() + * @see url_download_started_cb() +*/ +int url_download_set_started_cb(url_download_h download, url_download_started_cb callback, void* user_data); + + +/** + * @brief Unregisters the callback function. + * + * @remarks This function should be called before downloading (see url_download_start()) + * @param [in] download The download handle + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @pre The download state must be #URL_DOWNLOAD_STATE_READY or #URL_DOWNLOAD_STATE_COMPLETED. + * @see url_download_set_started_cb() + * @see url_download_started_cb() +*/ +int url_download_unset_started_cb(url_download_h download); + + +/** + * @brief Registers a callback function to be invoked when the download is paused. + * + * @remarks This function should be called before downloading (see url_download_start()) + * @param [in] download The download handle + * @param [in] callback The callback function to register + * @param [in] user_data The user data to be passed to the callback function + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @pre The download state must be #URL_DOWNLOAD_STATE_READY or #URL_DOWNLOAD_STATE_COMPLETED. + * @post url_download_paused_cb() will be invoked. + * @see url_download_unset_paused_cb() + * @see url_download_paused_cb() +*/ +int url_download_set_paused_cb(url_download_h download, url_download_paused_cb callback, void* user_data); + + +/** + * @brief Unregisters the callback function. + * + * @remarks This function should be called before downloading (see url_download_start()) + * @param [in] download The download handle + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @pre The download state must be #URL_DOWNLOAD_STATE_READY or #URL_DOWNLOAD_STATE_COMPLETED. + * @see url_download_set_paused_cb() + * @see url_download_paused_cb() +*/ +int url_download_unset_paused_cb(url_download_h download); + + +/** + * @brief Registers a callback function to be invoked when the download completed. + * + * @remarks This function should be called before downloading (see url_download_start()) + * @param [in] download The download handle + * @param [in] callback The callback function to register + * @param [in] user_data The user data to be passed to the callback function + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @pre The download state must be #URL_DOWNLOAD_STATE_READY or #URL_DOWNLOAD_STATE_COMPLETED. + * @post url_download_completed_cb() will be invoked. + * @see url_download_unset_completed_cb() + * @see url_download_completed_cb() +*/ +int url_download_set_completed_cb(url_download_h download, url_download_completed_cb callback, void* user_data); + + +/** + * @brief Unregisters the callback function. + * + * @remarks This function should be called before downloading (see url_download_start()) + * @param [in] download The download handle + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @pre The download state must be #URL_DOWNLOAD_STATE_READY or #URL_DOWNLOAD_STATE_COMPLETED. + * @see url_download_set_completed_cb() + * @see url_download_completed_cb() +*/ +int url_download_unset_completed_cb(url_download_h download); + + +/** + * @brief Registers a callback function to be invoked when the download is stopped. + * + * @remarks This function should be called before downloading (see url_download_start()) + * @param [in] download The download handle + * @param [in] callback The callback function to register + * @param [in] user_data The user data to be passed to the callback function + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @pre The download state must be #URL_DOWNLOAD_STATE_READY or #URL_DOWNLOAD_STATE_COMPLETED. + * @post url_download_stopped_cb() will be invoked. + * @see url_download_unset_stopped_cb() + * @see url_download_stopped_cb() +*/ +int url_download_set_stopped_cb(url_download_h download, url_download_stopped_cb callback, void* user_data); + + +/** + * @brief Unregisters the callback function. + * + * @remarks This function should be called before downloading (see url_download_start()) + * @param [in] download The download handle + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @pre The download state must be #URL_DOWNLOAD_STATE_READY or #URL_DOWNLOAD_STATE_COMPLETED. + * @see url_download_set_stopped_cb() + * @see url_download_stopped_cb() +*/ +int url_download_unset_stopped_cb(url_download_h download); + + +/** + * @brief Registers a callback function to be invoked when progress of the download changes + * + * @remarks This function should be called before downloading (see url_download_start()) + * @param [in] download The download handle + * @param [in] callback The callback function to register + * @param [in] user_data The user data to be passed to the callback function + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @pre The download state must be #URL_DOWNLOAD_STATE_READY or #URL_DOWNLOAD_STATE_COMPLETED. + * @post url_download_progress_cb() will be invoked. + * @see url_download_unset_progress_cb() + * @see url_download_progress_cb() +*/ +int url_download_set_progress_cb(url_download_h download, url_download_progress_cb callback, void *user_data); + + +/** + * @brief Unregisters the callback function. + * + * @remarks This function should be called before downloading (see url_download_start()) + * @param [in] download The download handle + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @pre The download state must be #URL_DOWNLOAD_STATE_READY or #URL_DOWNLOAD_STATE_COMPLETED. + * @see url_download_set_progress_cb() + * @see url_download_progress_cb() +*/ +int url_download_unset_progress_cb(url_download_h download); + + +/** + * @brief Starts or resumes the download, asynchronously. + * + * @details This function starts to download the current URL, or resumes the download if paused. + * + * @remarks The URL is the mandatory information to start the download. + * @param [in] download The download handle + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #URL_DOWNLOAD_ERROR_IO_ERROR Internal I/O error + * @retval #URL_DOWNLOAD_ERROR_URL Invalid URL + * @retval #URL_DOWNLOAD_ERROR_DESTINATION Invalid destination + * @pre The download state must be #URL_DOWNLOAD_STATE_READY, #URL_DOWNLOAD_STATE_PAUSED or #URL_DOWNLOAD_STATE_COMPLETED. + * @post The download state will be #URL_DOWNLOAD_STATE_DOWNLOADING + * @see url_download_set_url() + * @see url_download_pause() + * @see url_download_stop() + * @see url_download_set_started_cb() + * @see url_download_unset_started_cb() + * @see url_download_started_cb() + */ +int url_download_start(url_download_h download); + + +/** + * @brief Pauses the download, asynchronously. + * + * @remarks The paused download can be restarted with url_download_start() or canceled with url_download_stop() + * @param [in] download The download handle + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #URL_DOWNLOAD_ERROR_IO_ERROR Internal I/O error + * @pre The download state must be #URL_DOWNLOAD_STATE_DOWNLOADING. + * @post The download state will be #URL_DOWNLOAD_STATE_PAUSED. + * @see url_download_start() + * @see url_download_stop() + * @see url_download_set_paused_cb() + * @see url_download_unset_paused_cb() + * @see url_download_paused_cb() + */ +int url_download_pause(url_download_h download); + + +/** + * @brief Stops the download, asynchronously. + * + * @details This function cancels the running download and its state will be #URL_DOWNLOAD_STATE_READY + * @remarks The stopped download can be restarted with url_download_start(). + * @param [in] download The download handle + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #URL_DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #URL_DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #URL_DOWNLOAD_ERROR_IO_ERROR Internal I/O error + * @pre The download state must be #URL_DOWNLOAD_STATE_DOWNLOADING. + * @post url_download_stopped_cb() will be invoked if it is registered with url_download_set_stopped_cb() + * @post The download state will be #URL_DOWNLOAD_STATE_READY. + * @see url_download_start() + * @see url_download_stop() + * @see url_download_set_stopped_cb() + * @see url_download_unset_stopped_cb() + * @see url_download_stopped_cb() + */ +int url_download_stop(url_download_h download); + +/** + * @brief Gets the download's current state. + * + * @param [in] download The download handle + * @param [out] state The current state of the download + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @see #url_download_state_e + */ +int url_download_get_state(url_download_h download, url_download_state_e *state); + + +/** + * @brief Retrieves all HTTP header fields to be included with the download + * @details This function calls url_download_http_header_field_cb() once for each HTTP header field added.\n + * If url_download_http_header_field_cb() callback function returns false, then iteration will be finished. + * + * @param [in] download The download handle + * @param [in] callback The iteration callback function + * @param [in] user_data The user data to be passed to the callback function + * @return 0 on success, otherwise a negative error value. + * @retval #URL_DOWNLOAD_ERROR_NONE Successful + * @retval #URL_DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @post This function invokes url_download_http_header_field_cb(). + * @see url_download_http_header_field_cb() + */ +int url_download_foreach_http_header_field(url_download_h download, url_download_http_header_field_cb callback, void *user_data); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __TIZEN_WEB_URL_DOWNLOAD_H__ */ diff --git a/include/url_download_private.h b/include/url_download_private.h new file mode 100755 index 0000000..0116c92 --- /dev/null +++ b/include/url_download_private.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef __TIZEN_WEB_URL_DOWNLOAD_PRIVATE_H__ +#define __TIZEN_WEB_URL_DOWNLOAD_PRIVATE_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +typedef da_client_cb_t *url_download_agent_h; + +/** + * url_download_cb_s + */ +struct url_download_cb_s { + url_download_started_cb started; + void *started_user_data; + + url_download_paused_cb paused; + void *paused_user_data; + + url_download_completed_cb completed; + void *completed_user_data; + + url_download_stopped_cb stopped; + void *stopped_user_data; + + url_download_progress_cb progress; + void *progress_user_data; +}; + +struct url_download_s { + url_download_agent_h agent; + da_handle_t id; + struct url_download_cb_s callback; + url_download_state_e state; + char *url; + char *destination; + bundle *http_header; + char *completed_path; +}; + +#ifdef __cplusplus +} +#endif + +#endif /* __TIZEN_WEB_URL_DOWNLOAD_PRIVATE_H__ */ diff --git a/packaging/capi-web-url-download.spec b/packaging/capi-web-url-download.spec new file mode 100644 index 0000000..5219ddd --- /dev/null +++ b/packaging/capi-web-url-download.spec @@ -0,0 +1,53 @@ + +Name: capi-web-url-download +Summary: CAPI for content download with web url +Version: 0.0.3 +Release: 1 +Group: TO_BE_FILLED_IN +License: TO_BE_FILLED_IN +URL: N/A +Source0: %{name}-%{version}.tar.gz +BuildRequires: pkgconfig(capi-base-common) +BuildRequires: pkgconfig(libdownload-agent) +BuildRequires: pkgconfig(bundle) +BuildRequires: pkgconfig(dlog) +BuildRequires: cmake +BuildRequires: expat-devel + +%description +CAPI for the content download + +%package devel +Summary: url download +Group: Development/Libraries +Requires: %{name} = %{version}-%{release} + +%description devel +CAPI for content downloading with web url (developement files) + +%prep +%setup -q + +%build +cmake . -DCMAKE_INSTALL_PREFIX="/usr/lib" + +make %{?jobs:-j%jobs} + +%install +rm -rf %{buildroot} +%make_install + +%post -p /sbin/ldconfig + +%postun -p /sbin/ldconfig + +%files +%defattr(-,root,root,-) +/usr/lib/libcapi-web-url-download.so + +%files devel +%defattr(-,root,root,-) +/usr/lib/libcapi-web-url-download.so +/usr/lib/pkgconfig/capi-web-url-download.pc +/usr/include/web/url_download.h + diff --git a/sample/build.sh b/sample/build.sh new file mode 100644 index 0000000..d0aafc6 --- /dev/null +++ b/sample/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +gcc -o url_download_test test.c -I./ `pkg-config --cflags --libs capi-web-url-download ecore gobject-2.0` -g + + diff --git a/sample/test.c b/sample/test.c new file mode 100644 index 0000000..e8303b3 --- /dev/null +++ b/sample/test.c @@ -0,0 +1,403 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#define LOGD(fmt, ...) \ + do { printf("[D][L:%3d] " fmt, __LINE__, ##__VA_ARGS__); \ + printf("\n"); \ + } while(0); +#define LOGE(fmt, ...) \ + do { printf("[E][L:%3d] " fmt, __LINE__, ##__VA_ARGS__); \ + printf("\n"); \ + } while(0); +#define LOGC(fmt, ...) \ + do { printf("[C][L:%3d] " fmt, __LINE__, ##__VA_ARGS__); \ + printf("\n"); \ + } while(0); + +#define STRINGFY(xx) #xx + +#define TEST_URL "http://cdn.naver.com/naver/NanumFont/setupmac/NanumFontSetup_ECO_OTF_Ver1.0.app.zip" +#define TEST_URL2 "http://builds.nightly.webkit.org/files/trunk/src/WebKit-r109693.tar.bz2" + +#define TEST_GALAXY_UA "Mozilla/5.0 (Linux; U; Android 2.3.3; ko-kr; SHW-M250S Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1" +#define TEST_UA "AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1" + +url_download_h handle = NULL; + +void print_state_str() +{ + int ret = 0; + url_download_state_e state = 0; + ret = url_download_get_state(handle, &state); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to get state"); + return; + } + + switch (state) + { + case URL_DOWNLOAD_STATE_READY: + LOGD("State : Ready"); + break; + case URL_DOWNLOAD_STATE_COMPLETED: + LOGD("State : Competed"); + break; + case URL_DOWNLOAD_STATE_PAUSED: + LOGD("State : Paused"); + break; + case URL_DOWNLOAD_STATE_DOWNLOADING: + LOGD("State : Downloading"); + break; + } +} + +void print_error_str(url_download_error_e err) +{ + switch (err) + { + case URL_DOWNLOAD_ERROR_NONE: + LOGD("%s",STRINGFY(URL_DOWNLOAD_ERROR_NONE)); + break; + case URL_DOWNLOAD_ERROR_INVALID_PARAMETER: + LOGD("Err:%s",STRINGFY(URL_DOWNLOAD_ERROR_INVALID_PARAMETER)); + break; + case URL_DOWNLOAD_ERROR_OUT_OF_MEMORY: + LOGD("Err:%s",STRINGFY(URL_DOWNLOAD_ERROR_OUT_OF_MEMORY)); + break; + case URL_DOWNLOAD_ERROR_IO_ERROR: + LOGD("Err:%s",STRINGFY(URL_DOWNLOAD_ERROR_IO_ERROR)); + break; + case URL_DOWNLOAD_ERROR_NETWORK_UNREACHABLE: + LOGD("Err:%s",STRINGFY(URL_DOWNLOAD_ERROR_NETWORK_UNREACHABLE)); + break; + case URL_DOWNLOAD_ERROR_CONNECTION_TIMED_OUT: + LOGD("Err:%s",STRINGFY(URL_DOWNLOAD_ERROR_CONNECTION_TIMED_OUT)); + break; + case URL_DOWNLOAD_ERROR_NO_SPACE: + LOGD("Err:%s", STRINGFY(URL_DOWNLOAD_ERROR_NO_SPACE)); + break; + case URL_DOWNLOAD_ERROR_FIELD_NOT_FOUND: + LOGD("Err:%s", STRINGFY(URL_DOWNLOAD_ERROR_FIELD_NOT_FOUND)); + break; + case URL_DOWNLOAD_ERROR_INVALID_STATE: + LOGD("Err:%s", STRINGFY(URL_DOWNLOAD_ERROR_INVALID_STATE)); + break; + case URL_DOWNLOAD_ERROR_CONNECTION_FAILED: + LOGD("Err:%s", STRINGFY(URL_DOWNLOAD_ERROR_CONNECTION_FAILED)); + break; + case URL_DOWNLOAD_ERROR_SSL_FAILED: + LOGD("Err:%s", STRINGFY(URL_DOWNLOAD_ERROR_SSL_FAILED)); + break; + case URL_DOWNLOAD_ERROR_INVALID_URL: + LOGD("Err:%s", STRINGFY(URL_DOWNLOAD_ERROR_INVALID_URL)); + break; + default: + LOGD("No Error"); + break; + } +} + +void completed_cb(url_download_h download, const char *path, void *user_data) +{ + LOGD("===Completed Callback==="); + print_state_str(); + if (path) + LOGD("Downloaded Path:%s", path); + ecore_main_loop_quit(); + LOGD("========================"); +} + +void progress_cb(url_download_h download, unsigned long long received, unsigned long long total, void *user_data) +{ + LOGD("===Progress Callback==="); + print_state_str(); + LOGD("received[%llu] total[%llu]",received, total); + LOGD("======================="); +} + +void stopped_cb(url_download_h download, url_download_error_e error, void *user_data) +{ + LOGD("===Stoped Callback==="); + print_state_str(); + print_error_str(error); + LOGD("====================="); +} + +void started_cb(url_download_h download, void *user_data) +{ + LOGD("===Started Callback==="); + print_state_str(); + LOGD("======================"); +} + +bool http_header_cb(url_download_h download, const char *field, void *user_data) +{ + print_state_str(); + if (field) + LOGD("HTTP header field : %s",field); +} + +void init() +{ + int ret = 0; + ret = url_download_create(&handle); + if (ret != URL_DOWNLOAD_ERROR_NONE) + { + LOGE("Fail to create download handle"); + return; + } + ret = url_download_set_url(handle, TEST_URL); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to set url"); + return; + } + + ret = url_download_set_destination (handle, "./"); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to set install directory"); + return; + } + ret = url_download_add_http_header_field(handle, "User-Agent", TEST_UA); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to add http header"); + return; + } + + ret = url_download_set_completed_cb(handle, completed_cb, NULL); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to set completed callback"); + return; + } + + ret = url_download_set_stopped_cb(handle, stopped_cb, NULL); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to set stopped callback"); + return; + } + + ret = url_download_set_progress_cb(handle, progress_cb, NULL); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to set progress callback"); + return; + } + + ret = url_download_set_started_cb(handle, started_cb, NULL); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to set started callback"); + return; + } + + ret = url_download_foreach_http_header_field(handle, http_header_cb, NULL); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to start download"); + return; + } + + ret = url_download_start(handle); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to start download"); + return; + } +} + +Eina_Bool request_start(void *data) +{ + int ret = 0; + url_download_state_e state; + + LOGD("#REQUEST Start#"); + ret = url_download_get_state(handle, &state); + if (state != URL_DOWNLOAD_STATE_PAUSED && + state != URL_DOWNLOAD_STATE_COMPLETED && + state != URL_DOWNLOAD_STATE_READY) + { + LOGE("Cannot stop at present state"); + return 1; + } + + ret = url_download_start(handle); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to start download"); + } + return 0; +} + +Eina_Bool request_stop(void *data) +{ + int ret = 0; + url_download_state_e state; + + LOGD("#REQUEST Stop#"); + ret = url_download_get_state(handle, &state); + if (state != URL_DOWNLOAD_STATE_PAUSED && + state != URL_DOWNLOAD_STATE_DOWNLOADING) + { + LOGC("Cannot stop at present state"); + return 1; + } + + ret = url_download_stop(handle); + if (ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to stop download"); + } + return 0; +} + +Eina_Bool request_pause(void *data) +{ + int ret = 0; + url_download_state_e state; + + LOGD("#REQUEST Pause#"); + ret = url_download_get_state(handle, &state); + if (state != URL_DOWNLOAD_STATE_DOWNLOADING) + { + LOGC("Cannot stop at present state"); + return 1; + } + + ret = url_download_pause(handle); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to pause download"); + } + return 0; +} + +void deinit() +{ + int ret = 0; + char **value = NULL; + char **url = NULL; + char **destination = NULL; + + ret = url_download_get_http_header_field(handle, "User-Agent", value); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to get the value of the http field"); + return; + } + + if (value) + { + int i = 0; + int num = sizeof(value)/sizeof(char *); + LOGD("num[%d] size[%d] size[%d]", num, sizeof(value), sizeof(char *)); + for (i = 0; i < num; i++) + { + LOGD("Value[%d]:%s",i+1,value[i]); + } + } + + ret = url_download_get_url(handle, url); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to get url"); + return; + } + if (url && *url) + LOGD("URL:%s", *url) + + ret = url_download_get_destination(handle, destination); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to remove the value of the http field"); + return; + } + if (destination && *destination) + LOGD("Detination Path:%s", *destination); + + + ret = url_download_remove_http_header_field(handle, "User-Agent"); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to remove the value of the http field"); + return; + } + + ret = url_download_unset_completed_cb(handle); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to unset completed callback"); + return; + } + + ret = url_download_unset_stopped_cb(handle); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to unset stopped callback"); + return; + } + + ret = url_download_unset_progress_cb(handle); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to unset progress callback"); + return; + } + + ret = url_download_unset_started_cb(handle); + if ( ret != URL_DOWNLOAD_ERROR_NONE ) + { + LOGE("Fail to unset started callback"); + return; + } + ret = url_download_destroy(handle); + if (ret != URL_DOWNLOAD_ERROR_NONE) + { + LOGE("Fail to destroy download handle"); + return; + } +} + +int main() +{ + Ecore_Timer *t1 = NULL; + Ecore_Timer *t2 = NULL; + Ecore_Timer *t3 = NULL; + Ecore_Timer *t4 = NULL; + /* Should this function because libsoup use gobejct. + * If appcore_init() is used, g_type_init() is not needed. + * This is called internally in appcore_init(). + */ + g_type_init(); + ecore_init(); + init(); + t1 = ecore_timer_add(2.0, request_stop, NULL); + t2 = ecore_timer_add(5.0, request_start, NULL); + t3 = ecore_timer_add(8.0, request_pause, NULL); + t4 = ecore_timer_add(11.0, request_start, NULL); + ecore_main_loop_begin(); + ecore_shutdown(); + LOGD("Program Exit"); +} diff --git a/src/url_download.c b/src/url_download.c new file mode 100644 index 0000000..4c67c59 --- /dev/null +++ b/src/url_download.c @@ -0,0 +1,1306 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include +#include + +#include +#include + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "TIZEN_N_URL_DOWNLOAD" + + +#define STATE_IS_RUNNING(_download_) \ + (_download_->state == URL_DOWNLOAD_STATE_DOWNLOADING \ + || _download_->state == URL_DOWNLOAD_STATE_PAUSED) + + +#define STRING_IS_INVALID(_string_) \ + (_string_ == NULL || _string_[0] == '\0') + + +static bool DBG_AGENT = true; +static bool DBG_HTTP_HEADER = true; +static bool DBG_DOWNLOAD = true; + +static void url_download_agent_state_cb(user_notify_info_t *notify_info, void* user_param); +static void url_download_agent_progress_cb(user_download_info_t *download_info,void* user_param); +static int url_download_start_resume(url_download_h download); +static int url_download_start_download(url_download_h download); + + +static const char* url_download_error_to_string(int error_code) +{ + char *error_name = NULL; + + switch (error_code) + { + case URL_DOWNLOAD_ERROR_NONE: + error_name = "ERROR_NONE"; + break; + case URL_DOWNLOAD_ERROR_INVALID_PARAMETER: + error_name = "INVALID_PARAMETER"; + break; + case URL_DOWNLOAD_ERROR_OUT_OF_MEMORY: + error_name = "OUT_OF_MEMORY"; + break; + case URL_DOWNLOAD_ERROR_IO_ERROR: + error_name = "IO_ERROR"; + break; + case URL_DOWNLOAD_ERROR_NETWORK_UNREACHABLE: + error_name = "NETWORK_UNREACHABLE"; + break; + case URL_DOWNLOAD_ERROR_CONNECTION_TIMED_OUT: + error_name = "CONNECTION_TIMED_OUT"; + break; + + case URL_DOWNLOAD_ERROR_FIELD_NOT_FOUND: + error_name = "FIELD_NOT_FOUND"; + break; + + case URL_DOWNLOAD_ERROR_NO_SPACE: + error_name = "NO_SPACE"; + break; + case URL_DOWNLOAD_ERROR_INVALID_STATE: + error_name = "INVALID_STATE"; + break; + case URL_DOWNLOAD_ERROR_CONNECTION_FAILED: + error_name = "CONNECTION_FAILED"; + break; + case URL_DOWNLOAD_ERROR_SSL_FAILED: + error_name = "SSL_FAILED"; + break; + + case URL_DOWNLOAD_ERROR_INVALID_URL: + error_name = "INVALID_URL"; + break; + + case URL_DOWNLOAD_ERROR_INVALID_DESTINATION: + error_name = "INVALID_DESTINATION"; + break; + + default: + error_name = "UNKNOWN"; + break; + } + return error_name; +} + +static int url_download_error(const char *function, int error_code, const char *description) +{ + const char *error_name = NULL; + + error_name = url_download_error_to_string(error_code); + + if (description) + { + LOGE("[%s] %s(0x%08x) : %s", function, error_name, error_code, description); + } + else + { + LOGE("[%s] %s(0x%08x)", function, error_name, error_code); + } + + return error_code; +} + +static const char* url_download_state_to_string(url_download_state_e state) +{ + switch (state) + { + case URL_DOWNLOAD_STATE_READY: + return "READY"; + + case URL_DOWNLOAD_STATE_DOWNLOADING: + return "DOWNLOADING"; + + case URL_DOWNLOAD_STATE_PAUSED: + return "PAUSED"; + + case URL_DOWNLOAD_STATE_COMPLETED: + return "COMPLETED"; + + default: + return "INVALID"; + } +} + +static int url_download_error_invalid_state(const char *function, url_download_h download) +{ + LOGE("[%s] INVALID_STATE(0x%08x) : state(%s)", + function, URL_DOWNLOAD_ERROR_INVALID_STATE, url_download_state_to_string(download->state)); + + return URL_DOWNLOAD_ERROR_INVALID_STATE; +} + +struct _download_list_t { + url_download_h data; + struct _download_list_t *next; +}; +static int _download_agent_reference = 0; +static da_client_cb_t *_download_agent = NULL; +static struct _download_list_t *_download_list = NULL; + +static int url_download_agent_create(url_download_agent_h *agent) +{ + int retcode; + + if (_download_agent == NULL) + { + _download_agent = calloc(1, sizeof(da_client_cb_t)); + + if (!_download_agent) + { + return URL_DOWNLOAD_ERROR_OUT_OF_MEMORY; + } + + _download_agent->user_noti_cb = url_download_agent_state_cb; + _download_agent->update_dl_info_cb = url_download_agent_progress_cb; + _download_agent->send_dd_info_cb = NULL; + _download_agent->go_to_next_url_cb = NULL; + + retcode = da_init(_download_agent, DA_DOWNLOAD_MANAGING_METHOD_AUTO); + + if (retcode) + { + free(_download_agent); + _download_agent = NULL; + return URL_DOWNLOAD_ERROR_IO_ERROR; + } + + LOGI_IF(DBG_AGENT, "[%s] download-agent is created", __FUNCTION__); + + } + + _download_agent_reference++; + + *agent = _download_agent; + + LOGI_IF(DBG_AGENT, "[%s] download-agent reference(%d)", __FUNCTION__, _download_agent_reference); + + return URL_DOWNLOAD_ERROR_NONE; +} + +static void url_download_agent_destroy(url_download_agent_h agent) +{ + if (agent == NULL || agent != _download_agent) + { + url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + return; + } + + _download_agent_reference--; + + if (_download_agent_reference <= 0) + { + da_deinit(); + + free(_download_agent); + _download_agent = NULL; + + _download_agent_reference = 0; + + LOGI_IF(DBG_AGENT, "[%s] download-agent is destroyed", __FUNCTION__); + } + + LOGI_IF(DBG_AGENT, "[%s] download-agent reference(%d)", __FUNCTION__, _download_agent_reference); + +} + +static url_download_error_e url_download_agent_error(int error) +{ + switch (error) + { + case DA_RESULT_OK: + return URL_DOWNLOAD_ERROR_NONE; + + case DA_ERR_NETWORK_FAIL: + return URL_DOWNLOAD_ERROR_CONNECTION_FAILED; + + case DA_ERR_UNREACHABLE_SERVER: + return URL_DOWNLOAD_ERROR_NETWORK_UNREACHABLE; + + case DA_ERR_HTTP_TIMEOUT: + return URL_DOWNLOAD_ERROR_CONNECTION_TIMED_OUT; + + case DA_ERR_SSL_FAIL: + return URL_DOWNLOAD_ERROR_SSL_FAILED; + + case DA_ERR_FAIL_TO_ACCESS_FILE: + case DA_ERR_FAIL_TO_ACCESS_STORAGE: + case DA_ERR_FAIL_TO_INSTALL_FILE: + return URL_DOWNLOAD_ERROR_INVALID_DESTINATION; + + case DA_ERR_DISK_FULL: + return URL_DOWNLOAD_ERROR_NO_SPACE; + + case DA_ERR_INVALID_URL: + case DA_ERR_UNSUPPORTED_PROTOCAL: + return URL_DOWNLOAD_ERROR_INVALID_URL; + + case DA_ERR_INVALID_INSTALL_PATH: + return URL_DOWNLOAD_ERROR_INVALID_DESTINATION; + + default: + return URL_DOWNLOAD_ERROR_IO_ERROR; + } +} + + +static int url_download_agent_dispatch_state_change(int da_state, url_download_state_e prev_state, url_download_state_e *next_state) +{ + switch (da_state) + { + case DA_STATE_DOWNLOAD_STARTED: + case DA_STATE_RESUMED: + *next_state = URL_DOWNLOAD_STATE_DOWNLOADING; + return URL_DOWNLOAD_ERROR_NONE; + + case DA_STATE_FINISHED: + *next_state = URL_DOWNLOAD_STATE_COMPLETED; + return URL_DOWNLOAD_ERROR_NONE; + + case DA_STATE_CANCELED: + case DA_STATE_FAILED: + *next_state = URL_DOWNLOAD_STATE_READY; + return URL_DOWNLOAD_ERROR_NONE; + + case DA_STATE_SUSPENDED: + *next_state = URL_DOWNLOAD_STATE_PAUSED; + return URL_DOWNLOAD_ERROR_NONE; + + case DA_STATE_DOWNLOADING: + if (prev_state == URL_DOWNLOAD_STATE_PAUSED) + { + *next_state = URL_DOWNLOAD_STATE_DOWNLOADING; + return URL_DOWNLOAD_ERROR_NONE; + } + return URL_DOWNLOAD_ERROR_INVALID_STATE; + + default: + return URL_DOWNLOAD_ERROR_INVALID_STATE; + } +} + + +static bool is_available_download_data(url_download_h download) +{ + struct _download_list_t *head = NULL; + bool ret = false; + head = _download_list; + while(head) + { + if (head->data == download) + { + ret = true; + break; + } + head = head->next; + } + return ret; +} + +static void url_download_agent_state_cb(user_notify_info_t *notify_info, void* user_data) +{ + url_download_h download = NULL; + url_download_state_e state = -1; + url_download_error_e stopped_code = URL_DOWNLOAD_ERROR_NONE; + + if (user_data == NULL) + { + url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_IO_ERROR, "failed to get the download handle"); + return; + } + + download = (url_download_h)user_data; + if (!is_available_download_data(download)) + { + url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, "download item is already destroyed"); + return; + } +/* + if (download->agent == NULL) + { + url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_IO_ERROR, "failed to get the download agent handle"); + return; + } +*/ + if (url_download_agent_dispatch_state_change(notify_info->state, download->state, &state)) + { + LOGI_IF(DBG_AGENT, "[%s] id[%d]no need to dispatch state change : da-state(%d)", __FUNCTION__, download->id, notify_info->state); + return; + } + + LOGI_IF(DBG_AGENT, "[%s] id(%d), state(%s)", __FUNCTION__, download->id, url_download_state_to_string(state)); + + switch (state) + { + case URL_DOWNLOAD_STATE_READY: + download->state = URL_DOWNLOAD_STATE_READY; + if (download->callback.stopped) + { + stopped_code = url_download_agent_error(notify_info->err); + LOGI_IF(DBG_AGENT, "[%s] id(%d), stopped by [%s]", + __FUNCTION__, download->id, url_download_error_to_string(stopped_code)); + download->callback.stopped(download,stopped_code, download->callback.stopped_user_data); + } + return; + + case URL_DOWNLOAD_STATE_DOWNLOADING: + download->state = URL_DOWNLOAD_STATE_DOWNLOADING; + if (download->callback.started) + { + download->callback.started(download, download->callback.started_user_data); + } + return; + + case URL_DOWNLOAD_STATE_PAUSED: + download->state = URL_DOWNLOAD_STATE_PAUSED; + if (download->callback.paused) + { + download->callback.paused(download, download->callback.paused_user_data); + } + return; + + case URL_DOWNLOAD_STATE_COMPLETED: + download->state = URL_DOWNLOAD_STATE_COMPLETED; + if (download->callback.completed) + { + download->callback.completed(download, download->completed_path, download->callback.completed_user_data); + } + return; + + default: + url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_IO_ERROR, "invalid state change event"); + return; + } + +} + + +static void url_download_agent_progress_cb(user_download_info_t *download_info,void* user_data) +{ + url_download_h download; + + if (user_data == NULL) + { + url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_IO_ERROR, "failed to get the download handle"); + return; + } + + download = (url_download_h)user_data; + + if (!is_available_download_data(download)) + { + url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, "download item is already destroyed"); + return; + } + + if (download_info->saved_path != NULL) + { + download->completed_path = strdup(download_info->saved_path); + } + + if (download->callback.progress) + { + download->callback.progress( + download, + download_info->total_received_size, download_info->file_size, + download->callback.progress_user_data); + } +} + + +int url_download_create(url_download_h *download) +{ + url_download_agent_h agent; + url_download_h download_new; + struct _download_list_t *head = NULL; + struct _download_list_t *temp_item = NULL; + + if (download == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + download_new = (url_download_h)calloc(1, sizeof(struct url_download_s)); + + if (download_new == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_OUT_OF_MEMORY, NULL); + } + + if (url_download_agent_create(&agent)) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_IO_ERROR, "failed to initialize an agent"); + } + + download_new->agent = agent; + + download_new->http_header = bundle_create(); + + if (!download_new->http_header) + { + url_download_destroy(download_new); + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_IO_ERROR, "failed to initialize a bundle"); + } + + download_new->state = URL_DOWNLOAD_STATE_READY; + *download = download_new; + if (_download_list == NULL) + { + _download_list = calloc(1, sizeof(struct _download_list_t)); + if (_download_list == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_OUT_OF_MEMORY, NULL); + } + _download_list->data = download_new; + } else { + head = _download_list; + while (head->next) + { + head = head->next; + } + temp_item = calloc(1, sizeof(struct _download_list_t)); + if (temp_item == NULL) { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_OUT_OF_MEMORY, NULL); + } + temp_item->data = download_new; + head->next = temp_item; + } + + return URL_DOWNLOAD_ERROR_NONE; +} + +int url_download_destroy(url_download_h download) +{ + struct _download_list_t *head = NULL; + struct _download_list_t *prev = NULL; + if (download == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (STATE_IS_RUNNING(download)) + { + url_download_stop(download); + } + + if (download->url) + { + free(download->url); + download->url = NULL; + } + + if (download->destination) + { + free(download->destination); + download->destination = NULL; + } + + if (download->http_header) + { + bundle_free(download->http_header); + download->http_header = NULL; + } + + if (download->completed_path) + { + free(download->completed_path); + download->completed_path = NULL; + } + + url_download_agent_destroy(download->agent); + + head = _download_list; + while (head) + { + if (head->data && head->data == download) + { + head->data = NULL; + if (prev) + { + if (head->next) + prev->next = head->next; + else + prev->next = NULL; + } + else + { + if (head->next) + _download_list = head->next; + else + _download_list = NULL; + } + free(head); + head = NULL; + break; + } + prev = head; + head = head->next; + } + + free(download); + download = NULL; + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_set_url(url_download_h download, const char *url) +{ + char *url_dup = NULL; + + if (download == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (STATE_IS_RUNNING(download)) + { + return url_download_error_invalid_state(__FUNCTION__, download); + } + + if (url != NULL) + { + url_dup = strdup(url); + + if (url_dup == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_OUT_OF_MEMORY, NULL); + } + } + + if (download->url != NULL) + { + free(download->url); + } + + download->url = url_dup; + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_get_url(url_download_h download, char **url) +{ + char *url_dup = NULL; + + if (download == NULL || url == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (download->url != NULL) + { + url_dup = strdup(download->url); + + if (url_dup == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_OUT_OF_MEMORY, NULL); + } + } + + *url = url_dup; + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_set_destination(url_download_h download, const char *path) +{ + char *path_dup = NULL; + + if (download == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (STATE_IS_RUNNING(download)) + { + return url_download_error_invalid_state(__FUNCTION__, download); + } + + if (path != NULL) + { + path_dup = strdup(path); + + if (path_dup == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_OUT_OF_MEMORY, NULL); + } + } + + if (download->destination != NULL) + { + free(download->destination); + } + + download->destination = path_dup; + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_get_destination(url_download_h download, char **path) +{ + char *path_dup = NULL; + + if (download == NULL || path == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (download->destination != NULL) + { + path_dup = strdup(download->destination); + + if (path_dup == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_OUT_OF_MEMORY, NULL); + } + } + + *path = path_dup; + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_add_http_header_field(url_download_h download, const char *field, const char *value) +{ + if (download == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (STRING_IS_INVALID(field) || STRING_IS_INVALID(value)) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (STATE_IS_RUNNING(download)) + { + return url_download_error_invalid_state(__FUNCTION__, download); + } + + if (bundle_get_val(download->http_header, field)) + { + bundle_del(download->http_header, field); + } + + if (bundle_add(download->http_header, field, value)) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_IO_ERROR, NULL); + } + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_get_http_header_field(url_download_h download, const char *field, char **value) +{ + const char *bundle_value; + char *field_value_dup; + + if (download == NULL || value == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (STRING_IS_INVALID(field)) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + bundle_value = bundle_get_val(download->http_header, field); + + if (bundle_value == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_FIELD_NOT_FOUND, NULL); + } + + field_value_dup = strdup(bundle_value); + + if (field_value_dup == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_OUT_OF_MEMORY, NULL); + } + + *value = field_value_dup; + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_remove_http_header_field(url_download_h download, const char *field) +{ + if (download == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (STRING_IS_INVALID(field)) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, "invalid field"); + } + + if (STATE_IS_RUNNING(download)) + { + return url_download_error_invalid_state(__FUNCTION__, download); + } + + if (!bundle_get_val(download->http_header, field)) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_FIELD_NOT_FOUND, NULL); + } + + if (bundle_del(download->http_header, field)) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_IO_ERROR, NULL); + } + + return URL_DOWNLOAD_ERROR_NONE; +} + +typedef struct http_field_array_s{ + char **array; + int array_length; + int position; +} http_field_array_t; + +static void url_download_get_all_http_header_fields_iterator(const char *field_name, const char *field_value, void *user_data) +{ + http_field_array_t *http_field_array; + char *field_buffer; + int field_buffer_length; + const char *field_delimiters = ": "; + + http_field_array = user_data; + + if (http_field_array == NULL) + { + url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + return; + } + + // REF : http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 + field_buffer_length = strlen(field_name) + strlen(field_delimiters) + strlen(field_value) + 1; + + field_buffer = calloc(field_buffer_length, sizeof(char)); + + if (field_buffer == NULL) + { + url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_OUT_OF_MEMORY, NULL); + return; + } + + snprintf(field_buffer, field_buffer_length, "%s%s%s", field_name, field_delimiters, field_value); + + http_field_array->array[http_field_array->position] = field_buffer; + http_field_array->position++; + +} + +static int url_download_get_all_http_header_fields(url_download_h download, char ***fields, int *fields_length) +{ + http_field_array_t http_field_array; + + if (download == NULL || fields == NULL || fields_length == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + http_field_array.position = 0; + http_field_array.array_length = bundle_get_count(download->http_header); + http_field_array.array = calloc(http_field_array.array_length, sizeof(char*)); + + if (http_field_array.array == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_OUT_OF_MEMORY, NULL); + } + + bundle_iterate(download->http_header, url_download_get_all_http_header_fields_iterator, &http_field_array); + + if (DBG_HTTP_HEADER) + { + int i=0; + + for (i=0; icallback.started = callback; + download->callback.started_user_data = user_data; + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_unset_started_cb(url_download_h download) +{ + if (download == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (STATE_IS_RUNNING(download)) + { + return url_download_error_invalid_state(__FUNCTION__, download); + } + + download->callback.started = NULL; + download->callback.started_user_data = NULL; + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_set_paused_cb(url_download_h download, url_download_paused_cb callback, void* user_data) +{ + if (download == NULL || callback == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (STATE_IS_RUNNING(download)) + { + return url_download_error_invalid_state(__FUNCTION__, download); + } + + download->callback.paused = callback; + download->callback.paused_user_data = user_data; + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_unset_paused_cb(url_download_h download) +{ + if (download == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (STATE_IS_RUNNING(download)) + { + return url_download_error_invalid_state(__FUNCTION__, download); + } + + download->callback.paused = NULL; + download->callback.paused_user_data = NULL; + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_set_completed_cb(url_download_h download, url_download_completed_cb callback, void* user_data) +{ + if (download == NULL || callback == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (STATE_IS_RUNNING(download)) + { + return url_download_error_invalid_state(__FUNCTION__, download); + } + + download->callback.completed = callback; + download->callback.completed_user_data = user_data; + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_unset_completed_cb(url_download_h download) +{ + if (download == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (STATE_IS_RUNNING(download)) + { + return url_download_error_invalid_state(__FUNCTION__, download); + } + + download->callback.completed = NULL; + download->callback.completed_user_data = NULL; + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_set_stopped_cb(url_download_h download, url_download_stopped_cb callback, void* user_data) +{ + if (download == NULL || callback == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (STATE_IS_RUNNING(download)) + { + return url_download_error_invalid_state(__FUNCTION__, download); + } + + download->callback.stopped = callback; + download->callback.stopped_user_data = user_data; + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_unset_stopped_cb(url_download_h download) +{ + if (download == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (STATE_IS_RUNNING(download)) + { + return url_download_error_invalid_state(__FUNCTION__, download); + } + + download->callback.stopped = NULL; + download->callback.stopped_user_data = NULL; + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_set_progress_cb(url_download_h download, url_download_progress_cb callback, void *user_data) +{ + if (download == NULL || callback == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (STATE_IS_RUNNING(download)) + { + return url_download_error_invalid_state(__FUNCTION__, download); + } + + download->callback.progress = callback; + download->callback.progress_user_data = user_data; + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_unset_progress_cb(url_download_h download) +{ + if (download == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (STATE_IS_RUNNING(download)) + { + return url_download_error_invalid_state(__FUNCTION__, download); + } + + download->callback.progress = NULL; + download->callback.progress_user_data = NULL; + + return URL_DOWNLOAD_ERROR_NONE; +} + +static int url_download_start_download(url_download_h download) +{ + enum { + FEATURE_DEFAULT = 0, + FEATURE_DESTINATION = 1, + FEATURE_HTTP = 2, + FEATURE_DESTINATION_HTTP = 3, + }; + + int retcode; + int feature_set = 0; + int feature_oma_off = DA_FEATURE_OFF; + + char **http_headers = NULL; + int http_headers_length = 0; + + if (download == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + download->id = -1; + download->completed_path = NULL; + + if (STRING_IS_INVALID(download->url)) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, "URL is invalid"); + } + + if (download->destination != NULL) + { + feature_set += FEATURE_DESTINATION; + } + + if (bundle_get_count(download->http_header) > 0) + { + feature_set += FEATURE_HTTP; + + if (url_download_get_all_http_header_fields(download, &http_headers, &http_headers_length)) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_IO_ERROR, "Failed to set HTTP headers"); + } + } + + LOGI_IF(DBG_DOWNLOAD, "[%s] try to start download : URL(%s) feature(%d)", __FUNCTION__, download->url, feature_set); + + switch (feature_set) + { + case FEATURE_DEFAULT: + retcode = da_start_download_with_extension( + download->url, + &(download->id), + DA_FEATURE_OMA_AUTO_DOWNLOAD, &feature_oma_off, + DA_FEATURE_USER_DATA, download, + NULL + ); + break; + + case FEATURE_DESTINATION: + retcode = da_start_download_with_extension( + download->url, + &(download->id), + DA_FEATURE_OMA_AUTO_DOWNLOAD, &feature_oma_off, + DA_FEATURE_USER_DATA, download, + DA_FEATURE_INSTALL_PATH, download->destination, + NULL + ); + break; + + case FEATURE_HTTP: + retcode = da_start_download_with_extension( + download->url, + &(download->id), + DA_FEATURE_OMA_AUTO_DOWNLOAD, &feature_oma_off, + DA_FEATURE_USER_DATA, download, + DA_FEATURE_REQUEST_HEADER, http_headers, &http_headers_length, + NULL + ); + break; + + case FEATURE_DESTINATION_HTTP: + retcode = da_start_download_with_extension( + download->url, + &(download->id), + DA_FEATURE_OMA_AUTO_DOWNLOAD, &feature_oma_off, + DA_FEATURE_USER_DATA, download, + DA_FEATURE_INSTALL_PATH, download->destination, + DA_FEATURE_REQUEST_HEADER, http_headers, &http_headers_length, + NULL + ); + break; + + default: + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_IO_ERROR, "Invalid feature reqeust"); + } + + LOGI_IF(DBG_DOWNLOAD, "[%s] id(%d) feature(%d) da-retcode(%d)", __FUNCTION__, download->id, feature_set, retcode); + + if (retcode) + { + return url_download_error(__FUNCTION__, url_download_agent_error(retcode), "failed to start the download"); + } + else + { + return URL_DOWNLOAD_ERROR_NONE; + } +} + + +static int url_download_start_resume(url_download_h download) +{ + if (download == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (da_resume_download(download->id)) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_IO_ERROR, "failed to resume the download"); + } + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_start(url_download_h download) +{ + if (download == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + switch (download->state) + { + case URL_DOWNLOAD_STATE_COMPLETED: + case URL_DOWNLOAD_STATE_READY: + return url_download_start_download(download); + + case URL_DOWNLOAD_STATE_DOWNLOADING: + return url_download_error_invalid_state(__FUNCTION__, download); + + case URL_DOWNLOAD_STATE_PAUSED: + return url_download_start_resume(download); + + default: + return url_download_error_invalid_state(__FUNCTION__, download); + } +} + +int url_download_pause(url_download_h download) +{ + if (download == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + if (download->state != URL_DOWNLOAD_STATE_DOWNLOADING) + { + return url_download_error_invalid_state(__FUNCTION__, download); + } + + if (da_suspend_download(download->id)) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_IO_ERROR, "failed to pause the download"); + } + + return URL_DOWNLOAD_ERROR_NONE; +} + + +int url_download_stop(url_download_h download) +{ + if (download == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } +/* + if (download->state != URL_DOWNLOAD_STATE_DOWNLOADING) + { + return url_download_error_invalid_state(__FUNCTION__, download); + } +*/ + if (da_cancel_download(download->id)) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_IO_ERROR, "failed to stop the download"); + } + + return URL_DOWNLOAD_ERROR_NONE; +} + +int url_download_get_state(url_download_h download, url_download_state_e *state) +{ + if (download == NULL || state == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + *state = download->state; + + return URL_DOWNLOAD_ERROR_NONE; +} + + +typedef struct { + url_download_h download; + url_download_http_header_field_cb callback; + void* user_data; + bool foreach_break; +} foreach_context_http_header_field_t; + +static void url_download_foreach_http_header_field_iterator(const char *field_name, const char *field_value, void *user_data) +{ + foreach_context_http_header_field_t *foreach_context; + + foreach_context = user_data; + + if (foreach_context == NULL) + { + url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + return; + } + + + if (foreach_context->foreach_break == true) + { + return; + } + + if (foreach_context->callback != NULL) + { + foreach_context->foreach_break = !foreach_context->callback(foreach_context->download, + field_name, foreach_context->user_data); + } + +} + +int url_download_foreach_http_header_field(url_download_h download, url_download_http_header_field_cb callback, void *user_data) +{ + foreach_context_http_header_field_t foreach_context = { + .download = download, + .callback = callback, + .user_data = user_data, + .foreach_break = false + }; + + if (download == NULL || callback == NULL) + { + return url_download_error(__FUNCTION__, URL_DOWNLOAD_ERROR_INVALID_PARAMETER, NULL); + } + + bundle_iterate(download->http_header, url_download_foreach_http_header_field_iterator, &foreach_context); + + return URL_DOWNLOAD_ERROR_NONE; +} +