Tizen 2.1 base
[framework/uifw/ecore.git] / src / examples / ecore_file_download_example.c
1 #include <stdio.h>
2 #include <Eina.h>
3 #include <Ecore.h>
4 #include <Ecore_File.h>
5
6 /*
7  * ecore_file_download() example
8  *
9  * compile with:
10  * gcc ecore_file_download_example.c `pkg-config --libs --cflags ecore-file` \
11  *     -o ecore_file_download_example
12  *
13  */
14
15 #define URL      "http://www.kernel.org/pub/linux/kernel/v1.0/linux-1.0.tar.gz"
16 #define DST      "linux-1.0.tar.gz"
17 #define DST_MIME "[x-gzip]linux-1.0.tar.gz"
18
19 void
20 completion_cb(void *data, const char *file, int status)
21 {
22    printf("Done (status: %d)\n", status);
23    ecore_main_loop_quit();
24 }
25
26 int
27 progress_cb(void *data, const char *file,
28             long int dltotal, long int dlnow,
29             long int ultotal, long int ulnow)
30 {
31    printf("Progress: %ld/%ld\n", dlnow, dltotal);
32    return ECORE_FILE_PROGRESS_CONTINUE; //  continue the download
33 }
34
35 int
36 main(void)
37 {
38    double start;
39    Eina_Hash *headers;
40
41    eina_init();
42    ecore_init();
43    ecore_file_init();
44
45    if (ecore_file_exists(DST))
46      ecore_file_unlink(DST);
47
48    start = ecore_time_get();
49
50    if (ecore_file_download(URL, DST, completion_cb, progress_cb, NULL, NULL))
51      {
52         printf("Download started successfully:\n  URL: %s\n  DEST: %s\n", URL, DST);
53         ecore_main_loop_begin();
54         printf("\nTime elapsed: %f seconds\n", ecore_time_get() - start);
55         printf("Downloaded %lld bytes\n", ecore_file_size(DST));
56      }
57    else
58      {
59         printf("Error, can't start download\n");
60         goto done;
61      }
62
63    headers = eina_hash_string_small_new(NULL);
64    eina_hash_add(headers, "Content-type", "application/x-gzip");
65
66    if (ecore_file_download_full(URL, DST_MIME, completion_cb, progress_cb, NULL, NULL, headers))
67      {
68         printf("Download started successfully:\n  URL: %s\n  DEST: %s\n", URL, DST_MIME);
69         ecore_main_loop_begin();
70         printf("\nTime elapsed: %f seconds\n", ecore_time_get() - start);
71         printf("Downloaded %lld bytes\n", ecore_file_size(DST));
72      }
73    else
74      {
75         printf("Error, can't start download\n");
76         goto done;
77      }
78
79 done:
80    if (headers) eina_hash_free(headers);
81    ecore_file_shutdown();
82    ecore_shutdown();
83    eina_shutdown();
84    return 0;
85 }
86