diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/ecore/src/examples/ecore_con_url_download_example.c | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/libraries/ecore/src/examples/ecore_con_url_download_example.c b/libraries/ecore/src/examples/ecore_con_url_download_example.c new file mode 100644 index 0000000..0cb81e1 --- /dev/null +++ b/libraries/ecore/src/examples/ecore_con_url_download_example.c | |||
@@ -0,0 +1,110 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <sys/types.h> | ||
3 | #include <sys/stat.h> | ||
4 | #include <fcntl.h> | ||
5 | #include <Ecore.h> | ||
6 | #include <Ecore_Con.h> | ||
7 | |||
8 | struct _request { | ||
9 | long size; | ||
10 | }; | ||
11 | |||
12 | static Eina_Bool | ||
13 | _url_progress_cb(void *data, int type, void *event_info) | ||
14 | { | ||
15 | Ecore_Con_Event_Url_Progress *url_progress = event_info; | ||
16 | float percent; | ||
17 | |||
18 | if (url_progress->down.total > 0) | ||
19 | { | ||
20 | struct _request *req = ecore_con_url_data_get(url_progress->url_con); | ||
21 | req->size = url_progress->down.now; | ||
22 | |||
23 | percent = (url_progress->down.now / url_progress->down.total) * 100; | ||
24 | printf("Total of download complete: %0.1f (%0.0f)%%\n", | ||
25 | percent, url_progress->down.now); | ||
26 | } | ||
27 | |||
28 | return EINA_TRUE; | ||
29 | } | ||
30 | |||
31 | static Eina_Bool | ||
32 | _url_complete_cb(void *data, int type, void *event_info) | ||
33 | { | ||
34 | Ecore_Con_Event_Url_Complete *url_complete = event_info; | ||
35 | |||
36 | struct _request *req = ecore_con_url_data_get(url_complete->url_con); | ||
37 | int nbytes = ecore_con_url_received_bytes_get(url_complete->url_con); | ||
38 | |||
39 | printf("\n"); | ||
40 | printf("download completed with status code: %d\n", url_complete->status); | ||
41 | printf("Total size of downloaded file: %ld bytes\n", req->size); | ||
42 | printf("Total size of downloaded file: %ld bytes " | ||
43 | "(from received_bytes_get)\n", nbytes); | ||
44 | ecore_main_loop_quit(); | ||
45 | |||
46 | return EINA_TRUE; | ||
47 | } | ||
48 | |||
49 | int main(int argc, const char *argv[]) | ||
50 | { | ||
51 | Ecore_Con_Url *ec_url = NULL; | ||
52 | struct _request *req; | ||
53 | int fd; | ||
54 | const char *filename = "downloadedfile.dat"; | ||
55 | |||
56 | if (argc < 2) | ||
57 | { | ||
58 | printf("need one parameter: <url>\n"); | ||
59 | return -1; | ||
60 | } | ||
61 | |||
62 | fd = open(filename, O_CREAT|O_WRONLY|O_TRUNC, 0644); | ||
63 | |||
64 | if (fd == -1) | ||
65 | { | ||
66 | printf("error: could not open file for writing: \"%s\"\n", | ||
67 | filename); | ||
68 | return -1; | ||
69 | } | ||
70 | |||
71 | ecore_init(); | ||
72 | ecore_con_init(); | ||
73 | ecore_con_url_init(); | ||
74 | |||
75 | ec_url = ecore_con_url_new(argv[1]); | ||
76 | if (!ec_url) | ||
77 | { | ||
78 | printf("error when creating ecore con url object.\n"); | ||
79 | goto end; | ||
80 | } | ||
81 | |||
82 | req = malloc(sizeof(*req)); | ||
83 | req->size = 0; | ||
84 | ecore_con_url_data_set(ec_url, req); | ||
85 | |||
86 | ecore_con_url_fd_set(ec_url, fd); | ||
87 | |||
88 | ecore_event_handler_add(ECORE_CON_EVENT_URL_PROGRESS, _url_progress_cb, NULL); | ||
89 | ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, _url_complete_cb, NULL); | ||
90 | |||
91 | if (!ecore_con_url_get(ec_url)) | ||
92 | { | ||
93 | printf("could not realize request.\n"); | ||
94 | goto free_ec_url; | ||
95 | } | ||
96 | |||
97 | ecore_main_loop_begin(); | ||
98 | |||
99 | free_ec_url: | ||
100 | free(req); | ||
101 | ecore_con_url_free(ec_url); | ||
102 | end: | ||
103 | |||
104 | close(fd); | ||
105 | ecore_con_url_shutdown(); | ||
106 | ecore_con_shutdown(); | ||
107 | ecore_shutdown(); | ||
108 | |||
109 | return 0; | ||
110 | } | ||