aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ecore/src/examples/ecore_file_download_example.c
blob: c03940b7fcfc69788d9bf43533eee1d33beb3cfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <Eina.h>
#include <Ecore.h>
#include <Ecore_File.h>

/* 
 * ecore_file_download() example
 *
 * compile with:
 * gcc ecore_file_download_example.c `pkg-config --libs --cflags ecore-file` \
 *     -o ecore_file_download_example
 *
 */

#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
completion_cb(void *data, const char *file, int status)
{
   printf("Done (status: %d)\n", status);
   ecore_main_loop_quit();
}

int
progress_cb(void *data, const char *file,
            long int dltotal, long int dlnow,
            long int ultotal, long int ulnow)
{
   printf("Progress: %ld/%ld\n", dlnow, dltotal);
   return ECORE_FILE_PROGRESS_CONTINUE; //  continue the download
}


int main(void)
{
   double start;
   Eina_Hash *headers;

   eina_init();
   ecore_init();
   ecore_file_init();

   if (ecore_file_exists(DST))
     ecore_file_unlink(DST);

   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));
     }
   else
     {
        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();
   return 0;
}