ecore: Add ecore_file_download_full patch by Sangho Park <gouache95@gmail.com>
authorcedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Wed, 16 Feb 2011 12:44:34 +0000 (12:44 +0000)
committercedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Wed, 16 Feb 2011 12:44:34 +0000 (12:44 +0000)
git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/ecore@57087 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

AUTHORS
ChangeLog
examples/ecore_file_download_example.c
src/lib/ecore_file/Ecore_File.h
src/lib/ecore_file/ecore_file_download.c

diff --git a/AUTHORS b/AUTHORS
index c86d644..cef6952 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -36,3 +36,4 @@ Brett Nash <nash@nash.id.au>
 Mike Blumenkrantz <mike@zentific.com>
 Leif Middelschulte <leif.middelschulte@gmail.com>
 Mike McCormack <mj.mccormack@samsung.com>
+Sangho Park <gouache95@gmail.com>
index e9b5e9b..a445e7a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -34,3 +34,8 @@
 2011-02-11 Mike Blumenkrantz
 
         * Added ecore_con_client_port_get
+
+2011-02-16 Sangho Park
+
+       * Added ecore_file_download_full API.
+
index 089595e..c03940b 100644 (file)
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <Eina.h>
 #include <Ecore.h>
 #include <Ecore_File.h>
 
@@ -13,6 +14,7 @@
 
 #define URL "http://www.kernel.org/pub/linux/kernel/v1.0/linux-1.0.tar.gz"
 #define DST "linux-1.0.tar.gz"
+#define DST_MIME "[x-gzip]linux-1.0.tar.gz"
 
 
 void
@@ -35,6 +37,7 @@ progress_cb(void *data, const char *file,
 int main(void)
 {
    double start;
+   Eina_Hash *headers;
 
    eina_init();
    ecore_init();
@@ -46,18 +49,36 @@ int main(void)
    start = ecore_time_get();
 
    if (ecore_file_download(URL, DST, completion_cb, progress_cb, NULL, NULL))
-   {
-      printf("Download started successfully:\n  URL: %s\n  DEST: %s\n", URL, DST);
-      ecore_main_loop_begin();
-      printf("\nTime elapsed: %f seconds\n", ecore_time_get() - start);
-      printf("Downloaded %lld bytes\n", ecore_file_size(DST));
-   }
+     {
+        printf("Download started successfully:\n  URL: %s\n  DEST: %s\n", URL, DST);
+        ecore_main_loop_begin();
+        printf("\nTime elapsed: %f seconds\n", ecore_time_get() - start);
+        printf("Downloaded %lld bytes\n", ecore_file_size(DST));
+     }
    else
-   {
-       printf("Error, can't start download\n");
-       return 1;
-   }
+     {
+        printf("Error, can't start download\n");
+        goto done;
+     }
 
+   headers = eina_hash_string_small_new(NULL);
+   eina_hash_add(headers, "Content-type", "application/x-gzip");
+
+   if (ecore_file_download_full(URL, DST_MIME, completion_cb, progress_cb, NULL, NULL, headers))
+     {
+        printf("Download started successfully:\n  URL: %s\n  DEST: %s\n", URL, DST_MIME);
+        ecore_main_loop_begin();
+        printf("\nTime elapsed: %f seconds\n", ecore_time_get() - start);
+        printf("Downloaded %lld bytes\n", ecore_file_size(DST));
+     }
+   else
+     {
+        printf("Error, can't start download\n");
+        goto done; 
+     }
+
+done:
+   if (headers) eina_hash_free(headers);
    ecore_file_shutdown();
    ecore_shutdown();
    eina_shutdown();
index ac4270c..e734d5e 100644 (file)
@@ -166,6 +166,14 @@ EAPI Eina_Bool ecore_file_download(const char *url,
                                    Ecore_File_Download_Progress_Cb progress_cb,
                                    void *data,
                                    Ecore_File_Download_Job **job_ret);
+EAPI Eina_Bool ecore_file_download_full(const char *url,
+                                        const char *dst,
+                                        Ecore_File_Download_Completion_Cb completion_cb,
+                                        Ecore_File_Download_Progress_Cb progress_cb,
+                                        void *data,
+                                        Ecore_File_Download_Job **job_ret,
+                                        Eina_Hash *headers);
+
 EAPI void      ecore_file_download_abort_all(void);
 EAPI void      ecore_file_download_abort(Ecore_File_Download_Job *job);
 EAPI Eina_Bool ecore_file_download_protocol_available(const char *protocol);
index 6b744ad..7c58320 100644 (file)
@@ -32,7 +32,8 @@ struct _Ecore_File_Download_Job
 Ecore_File_Download_Job *_ecore_file_download_curl(const char *url, const char *dst,
                                                    Ecore_File_Download_Completion_Cb completion_cb,
                                                    Ecore_File_Download_Progress_Cb progress_cb,
-                                                   void *data);
+                                                   void *data,
+                                                   Eina_Hash *headers);
 
 static Eina_Bool _ecore_file_download_url_complete_cb(void *data, int type, void *event);
 static Eina_Bool _ecore_file_download_url_progress_cb(void *data, int type, void *event);
@@ -77,48 +78,23 @@ ecore_file_download_shutdown(void)
 #endif /* BUILD_ECORE_CON */
 }
 
-/**
- * @addtogroup Ecore_File_Group Ecore_File - Files and directories convenience functions
- *
- * @{
- */
+static Eina_Bool
+_ecore_file_download_headers_foreach_cb(const Eina_Hash *hash __UNUSED__, const void *key, void *data, void *fdata)
+{
+   Ecore_File_Download_Job *job = fdata;
+   ecore_con_url_additional_header_add(job->url_con, key, data);
 
-/**
- * @brief Download the given url to the given destination.
- *
- * @param  url The complete url to download.
- * @param  dst The local file to save the downloaded to.
- * @param  completion_cb A callback called on download complete.
- * @param  progress_cb A callback called during the download operation.
- * @param  data User data passed to both callbacks.
- * @param  job_ret Job used to abort the download.
- * @return EINA_TRUE if the download start or EINA_FALSE on failure
- *
- * This function starts the download of the URL @p url and saves it to
- * @p dst. @p url must provide the protocol, including 'http://',
- * 'ftp://' or 'file://'. Ecore_File must be compiled with CURL to
- * download using http and ftp protocols. If @p dst is ill-formed, or
- * if it already exists, the function returns EINA_FALSE. When the
- * download is complete, the callback @p completion_cb is called and
- * @p data is passed to it. The @p status parameter of @p completion_cb 
- * will be filled with the status of the download (200, 404,...). The
- * @p progress_cb is called during the download operation, each time a
- * packet is received or when CURL wants. It can be used to display the
- * percentage of the downloaded file. Return 0 from this callback, if provided,
- * to continue the operation or anything else to abort the download. The only
- * operations that can be aborted are those with protocol 'http' or 'ftp'. In 
- * that case @p job_ret can be filled. It can be used with
- * ecore_file_download_abort() or ecore_file_download_abort_all() to
- * respectively abort one or all download operations. This function returns
- * EINA_TRUE if the download starts, EINA_FALSE otherwise.
- */
-EAPI Eina_Bool
-ecore_file_download(const char *url,
-                    const char *dst,
-                    Ecore_File_Download_Completion_Cb completion_cb,
-                    Ecore_File_Download_Progress_Cb progress_cb,
-                    void *data,
-                    Ecore_File_Download_Job **job_ret)
+   return EINA_TRUE;
+}
+
+static Eina_Bool
+_ecore_file_download(const char *url,
+                     const char *dst,
+                     Ecore_File_Download_Completion_Cb completion_cb,
+                     Ecore_File_Download_Progress_Cb progress_cb,
+                     void *data,
+                     Ecore_File_Download_Job **job_ret,
+                     Eina_Hash *headers)
 {
 #ifdef BUILD_ECORE_CON
    char *dir = ecore_file_dir_get(dst);
@@ -154,7 +130,7 @@ ecore_file_download(const char *url,
         /* download */
         Ecore_File_Download_Job *job;
 
-        job = _ecore_file_download_curl(url, dst, completion_cb, progress_cb, data);
+       job = _ecore_file_download_curl(url, dst, completion_cb, progress_cb, data, headers);
         if(job_ret) *job_ret = job;
         if(job)
           return EINA_TRUE;
@@ -189,6 +165,76 @@ ecore_file_download(const char *url,
 }
 
 /**
+ * @addtogroup Ecore_File_Group Ecore_File - Files and directories convenience functions
+ *
+ * @{
+ */
+
+/**
+ * @brief Download the given url to the given destination.
+ *
+ * @param  url The complete url to download.
+ * @param  dst The local file to save the downloaded to.
+ * @param  completion_cb A callback called on download complete.
+ * @param  progress_cb A callback called during the download operation.
+ * @param  data User data passed to both callbacks.
+ * @param  job_ret Job used to abort the download.
+ * @return EINA_TRUE if the download start or EINA_FALSE on failure
+ *
+ * This function starts the download of the URL @p url and saves it to
+ * @p dst. @p url must provide the protocol, including 'http://',
+ * 'ftp://' or 'file://'. Ecore_File must be compiled with CURL to
+ * download using http and ftp protocols. If @p dst is ill-formed, or
+ * if it already exists, the function returns EINA_FALSE. When the
+ * download is complete, the callback @p completion_cb is called and
+ * @p data is passed to it. The @p status parameter of @p completion_cb 
+ * will be filled with the status of the download (200, 404,...). The
+ * @p progress_cb is called during the download operation, each time a
+ * packet is received or when CURL wants. It can be used to display the
+ * percentage of the downloaded file. Return 0 from this callback, if provided,
+ * to continue the operation or anything else to abort the download. The only
+ * operations that can be aborted are those with protocol 'http' or 'ftp'. In 
+ * that case @p job_ret can be filled. It can be used with
+ * ecore_file_download_abort() or ecore_file_download_abort_all() to
+ * respectively abort one or all download operations. This function returns
+ * EINA_TRUE if the download starts, EINA_FALSE otherwise.
+ */
+EAPI Eina_Bool
+ecore_file_download(const char *url,
+                    const char *dst,
+                    Ecore_File_Download_Completion_Cb completion_cb,
+                    Ecore_File_Download_Progress_Cb progress_cb,
+                    void *data,
+                    Ecore_File_Download_Job **job_ret)
+{
+   _ecore_file_download(url, dst, completion_cb, progress_cb, data, job_ret, NULL);
+}
+
+/**
+ * @brief Download the given url to the given destination with additional headers.
+ *
+ * @param  url The complete url to download.
+ * @param  dst The local file to save the downloaded to.
+ * @param  completion_cb A callback called on download complete.
+ * @param  progress_cb A callback called during the download operation.
+ * @param  data User data passed to both callbacks.
+ * @param  job_ret Job used to abort the download.
+ * @param  headers pointer of header lists.
+ * @return EINA_TRUE if the download start or EINA_FALSE on failure
+ */
+EAPI Eina_Bool
+ecore_file_download_full(const char *url,
+                         const char *dst,
+                         Ecore_File_Download_Completion_Cb completion_cb,
+                         Ecore_File_Download_Progress_Cb progress_cb,
+                         void *data,
+                         Ecore_File_Download_Job **job_ret,
+                         Eina_Hash *headers)
+{
+   _ecore_file_download(url, dst, completion_cb, progress_cb, data, job_ret, headers);
+}
+
+/**
  * @brief Check if the given protocol is available.
  *
  * @param  protocol The protocol to check.
@@ -278,7 +324,8 @@ Ecore_File_Download_Job *
 _ecore_file_download_curl(const char *url, const char *dst,
                           Ecore_File_Download_Completion_Cb completion_cb,
                           Ecore_File_Download_Progress_Cb progress_cb,
-                          void *data)
+                          void *data,
+                          Eina_Hash *headers)
 {
    Ecore_File_Download_Job *job;
 
@@ -301,6 +348,7 @@ _ecore_file_download_curl(const char *url, const char *dst,
         return NULL;
      }
 
+   if (headers) eina_hash_foreach(headers, _ecore_file_download_headers_foreach_cb, job);
    ecore_con_url_fd_set(job->url_con, fileno(job->file));
    ecore_con_url_data_set(job->url_con, data);