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