delete a space & use a tab
[platform/core/api/url-download.git] / src / download-wrapping.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <sys/socket.h>
6 #include <sys/un.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <unistd.h>
10 #include <system_info.h>
11
12 #include <dlog.h>
13 #include <download.h>
14 #include <download-provider-interface.h>
15
16 #define DEBUG_MSG
17 #ifdef DEBUG_MSG
18 #include <dlog.h>
19 #ifdef LOG_TAG
20 #undef LOG_TAG
21 #endif
22 #define LOG_TAG "TIZEN_N_URL_DOWNLOAD"
23 #define TRACE_ERROR(format, ARG...)  \
24 { \
25 LOGE(format, ##ARG); \
26 }
27 #define TRACE_STRERROR(format, ARG...)  \
28 { \
29 LOGE(format" [%s]", ##ARG, strerror(errno)); \
30 }
31 #define TRACE_INFO(format, ARG...)  \
32 { \
33 LOGI(format, ##ARG); \
34 }
35 #else
36 #define TRACE_DEBUG_MSG(format, ARG...) ;
37 #endif
38
39 #define TELEPHONY_FEATURE       "tizen.org/feature/network.telephony"
40 #define WIFI_FEATURE            "tizen.org/feature/network.wifi"
41 #define WIFI_DIRECT_FEATURE     "tizen.org/feature/network.wifi.direct"
42
43 /////////////////////// APIs /////////////////////////////////
44
45 int download_create(int *download_id)
46 {
47         TRACE_INFO("");
48         if (download_id == NULL)
49                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
50         return dp_interface_create(download_id);
51 }
52
53 int download_destroy(int download_id)
54 {
55         TRACE_INFO("");
56         return dp_interface_destroy(download_id);
57 }
58
59 int download_start(int download_id)
60 {
61         TRACE_INFO("");
62         return dp_interface_start(download_id);
63 }
64
65 int download_pause(int download_id)
66 {
67         TRACE_INFO("");
68         return dp_interface_pause(download_id);
69 }
70
71 int download_cancel(int download_id)
72 {
73         TRACE_INFO("");
74         return dp_interface_cancel(download_id);
75 }
76
77
78 int download_set_url(int download_id, const char *url)
79 {
80         TRACE_INFO("");
81         if (url == NULL)
82                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
83         return dp_interface_set_url(download_id, url);
84 }
85
86
87 int download_get_url(int download_id, char **url)
88 {
89         TRACE_INFO("");
90         if (url == NULL)
91                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
92         return dp_interface_get_url(download_id, url);
93 }
94
95 int download_set_network_type(int download_id,
96                                                 download_network_type_e net_type)
97 {
98         TRACE_INFO("");
99
100         int bIsTelephonyFeatureSupported = 0;
101         int bIsWifiFeatureSupported = 0;
102         int bIsWifiDirectFeatureSupported = 0;
103
104         system_info_get_platform_bool (TELEPHONY_FEATURE, &bIsTelephonyFeatureSupported);
105         system_info_get_platform_bool (WIFI_FEATURE, &bIsWifiFeatureSupported);
106         system_info_get_platform_bool (WIFI_DIRECT_FEATURE, &bIsWifiDirectFeatureSupported);
107
108         switch (net_type) {
109         case DOWNLOAD_NETWORK_DATA_NETWORK:
110                 if (!bIsTelephonyFeatureSupported) {
111                         return DOWNLOAD_ERROR_NOT_SUPPORTED;
112                 }
113                 break;
114         case DOWNLOAD_NETWORK_WIFI:
115                 if (!bIsWifiFeatureSupported) {
116                         return DOWNLOAD_ERROR_NOT_SUPPORTED;
117                 }
118                 break;
119         case DOWNLOAD_NETWORK_WIFI_DIRECT:
120                 if (!bIsWifiDirectFeatureSupported) {
121                         return DOWNLOAD_ERROR_NOT_SUPPORTED;
122                 }
123                 break;
124         case DOWNLOAD_NETWORK_ALL:
125                 if (!bIsTelephonyFeatureSupported && !bIsWifiFeatureSupported && !bIsWifiDirectFeatureSupported) {
126                         return DOWNLOAD_ERROR_NOT_SUPPORTED;
127                 }
128                 break;
129         }
130
131         return dp_interface_set_network_type(download_id, (int)net_type);
132 }
133
134 int download_get_network_type(int download_id,
135                                                         download_network_type_e *net_type)
136 {
137         TRACE_INFO("");
138
139         if (net_type == NULL) {
140                 TRACE_ERROR("Parameter NULL Check");
141                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
142         }
143         int network_type = DOWNLOAD_ADAPTOR_NETWORK_ALL;
144         int ret = dp_interface_get_network_type(download_id, &network_type);
145         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
146                 *net_type = network_type;
147         return ret;
148 }
149
150 int download_set_network_bonding(int download_id, bool enable)
151 {
152         TRACE_INFO("");
153         return dp_interface_set_network_bonding(download_id, (int)enable);
154 }
155
156 int download_get_network_bonding(int download_id, bool *enable)
157 {
158         int is_set = 0;
159         TRACE_INFO("");
160         if (enable == NULL) {
161                 TRACE_ERROR("Parameter NULL Check");
162                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
163         }
164         int ret = dp_interface_get_network_bonding(download_id, &is_set);
165         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
166                 *enable = (bool)is_set;
167         return ret;
168 }
169
170 int download_set_destination(int download_id, const char *path)
171 {
172         TRACE_INFO("");
173         if (path == NULL)
174                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
175         return dp_interface_set_destination(download_id, path);
176 }
177
178
179 int download_get_destination(int download_id, char **path)
180 {
181         TRACE_INFO("");
182         if (path == NULL)
183                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
184         return dp_interface_get_destination(download_id, path);
185 }
186
187 int download_set_file_name(int download_id, const char *file_name)
188 {
189         TRACE_INFO("");
190         if (file_name == NULL)
191                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
192         return dp_interface_set_file_name(download_id, file_name);
193 }
194
195 int download_get_file_name(int download_id, char **file_name)
196 {
197         TRACE_INFO("");
198         if (file_name == NULL)
199                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
200         return dp_interface_get_file_name(download_id, file_name);
201 }
202
203 int download_get_downloaded_file_path(int download_id, char **path)
204 {
205         TRACE_INFO("");
206         if (path == NULL)
207                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
208         return dp_interface_get_downloaded_file_path(download_id, path);
209 }
210
211 int download_add_http_header_field(int download_id, const char *field,
212         const char *value)
213 {
214         TRACE_INFO("");
215         if (field == NULL || value == NULL)
216                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
217         return
218                 dp_interface_add_http_header_field(download_id, field, value);
219 }
220
221 int download_get_http_header_field(int download_id,
222         const char *field, char **value)
223 {
224         TRACE_INFO("");
225         if (field == NULL || value == NULL)
226                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
227         return
228                 dp_interface_get_http_header_field(download_id, field, value);
229 }
230
231 int download_get_http_header_field_list(int download_id, char ***fields,
232         int *length)
233 {
234         TRACE_INFO("");
235         if (fields == NULL || length == NULL)
236                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
237         return dp_interface_get_http_header_field_list(download_id, fields,
238                 length);
239 }
240
241 int download_remove_http_header_field(int download_id,
242         const char *field)
243 {
244         TRACE_INFO("");
245         if (field == NULL)
246                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
247         return dp_interface_remove_http_header_field(download_id, field);
248 }
249
250 // download_state_changed_cb is different with dp_interface_state_changed_cb.
251 int download_set_state_changed_cb(int download_id,
252         download_state_changed_cb callback, void* user_data)
253 {
254         TRACE_INFO("");
255         return dp_interface_set_state_changed_cb(download_id,
256                 (dp_interface_state_changed_cb)callback, user_data);
257 }
258
259 int download_unset_state_changed_cb(int download_id)
260 {
261         TRACE_INFO("");
262         return dp_interface_unset_state_changed_cb(download_id);
263 }
264
265 // download_progress_cb is same with dp_interface_progress_cb.
266 int download_set_progress_cb(int download_id,
267         download_progress_cb callback, void *user_data)
268 {
269         TRACE_INFO("");
270         return dp_interface_set_progress_cb(download_id,
271                         (dp_interface_progress_cb)callback, user_data);
272 }
273
274 int download_unset_progress_cb(int download_id)
275 {
276         TRACE_INFO("");
277         return dp_interface_unset_progress_cb(download_id);
278 }
279
280 int download_get_state(int download_id, download_state_e *state)
281 {
282         int statecode = 0;
283         TRACE_INFO("");
284         if (state == NULL) {
285                 TRACE_ERROR("Parameter NULL Check");
286                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
287         }
288         int ret = dp_interface_get_state(download_id, &statecode);
289         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
290                 *state = statecode;
291         return ret;
292 }
293
294 int download_get_temp_path(int download_id, char **temp_path)
295 {
296         TRACE_INFO("");
297         if (temp_path == NULL)
298                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
299         return dp_interface_get_temp_path(download_id, temp_path);
300 }
301
302 int download_get_content_name(int download_id, char **content_name)
303 {
304         TRACE_INFO("");
305         if (content_name == NULL)
306                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
307         return dp_interface_get_content_name(download_id, content_name);
308 }
309
310 int download_get_content_size(int download_id,
311         unsigned long long *content_size)
312 {
313         TRACE_INFO("");
314         if (content_size == NULL)
315                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
316         return dp_interface_get_content_size(download_id, content_size);
317 }
318
319 int download_get_mime_type(int download_id, char **mime_type)
320 {
321         TRACE_INFO("");
322         if (mime_type == NULL)
323                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
324         return dp_interface_get_mime_type(download_id, mime_type);
325 }
326
327 int download_set_auto_download(int download_id, bool enable)
328 {
329         TRACE_INFO("");
330         return dp_interface_set_auto_download(download_id, (int)enable);
331 }
332
333 int download_get_auto_download(int download_id, bool *enable)
334 {
335         int is_set = 0;
336         TRACE_INFO("");
337         if (enable == NULL) {
338                 TRACE_ERROR("Parameter NULL Check");
339                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
340         }
341         int ret = dp_interface_get_auto_download(download_id, &is_set);
342         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
343                 *enable = (bool)is_set;
344         return ret;
345 }
346
347 int download_get_error(int download_id, download_error_e *error)
348 {
349         int errorcode = 0;
350         TRACE_INFO("");
351         if (error == NULL) {
352                 TRACE_ERROR("Parameter NULL Check");
353                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
354         }
355         int ret = dp_interface_get_error(download_id, &errorcode);
356         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
357                 *error = errorcode;
358         return ret;
359 }
360
361 int download_get_http_status(int download_id, int *http_status)
362 {
363         TRACE_INFO("");
364         if (http_status == NULL)
365                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
366         return dp_interface_get_http_status(download_id, http_status);
367 }
368
369 int download_set_notification_app_control(int download_id, download_notification_app_control_type_e type, app_control_h handle)
370 {
371         TRACE_INFO("");
372         if (handle == NULL)
373                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
374         return dp_interface_set_notification_service_handle(download_id, (int)type, handle);
375 }
376
377 int download_get_notification_app_control(int download_id, download_notification_app_control_type_e type, app_control_h *handle)
378 {
379         TRACE_INFO("");
380         if (handle == NULL)
381                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
382         return dp_interface_get_notification_service_handle(download_id, (int)type, handle);
383 }
384
385 int download_set_notification_title(int download_id, const char *title)
386 {
387         TRACE_INFO("");
388         if (title == NULL)
389                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
390         return dp_interface_set_notification_title(download_id, title);
391 }
392
393 int download_get_notification_title(int download_id, char **title)
394 {
395         TRACE_INFO("");
396         if (title == NULL)
397                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
398         return dp_interface_get_notification_title(download_id, title);
399 }
400
401 int download_set_notification_description(int download_id, const char *description)
402 {
403         TRACE_INFO("");
404         if (description == NULL)
405                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
406         return dp_interface_set_notification_description(download_id, description);
407 }
408
409 int download_get_notification_description(int download_id, char **description)
410 {
411         TRACE_INFO("");
412         if (description == NULL)
413                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
414         return dp_interface_get_notification_description(download_id, description);
415 }
416
417 int download_set_notification_type(int download_id, download_notification_type_e type)
418 {
419         TRACE_INFO("");
420         return dp_interface_set_notification_type(download_id, (int)type);
421 }
422
423 int download_get_notification_type(int download_id, download_notification_type_e *type)
424 {
425         int noti_type = 0;
426         TRACE_INFO("");
427         if (type == NULL) {
428                 TRACE_ERROR("Parameter NULL Check");
429                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
430         }
431         int ret = dp_interface_get_notification_type(download_id, &noti_type);
432         if (ret == DOWNLOAD_ADAPTOR_ERROR_NONE)
433                 *type = (download_notification_type_e)noti_type;
434         return ret;
435 }
436
437 int download_get_etag(int download_id, char **etag)
438 {
439         TRACE_INFO("");
440         if (etag == NULL)
441                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
442         return dp_interface_get_etag(download_id, etag);
443 }
444
445 int download_set_temp_file_path(int download_id, char *path)
446 {
447         TRACE_INFO("");
448         if (path == NULL)
449                 return DOWNLOAD_ERROR_INVALID_PARAMETER;
450         return dp_interface_set_temp_file_path(download_id, path);
451 }