ba38bb1b3eaf48f1266cb74646a051b08bd75b1f
[profile/ivi/download-provider.git] / src / download-provider-notification.c
1 #include <app.h>
2 #include <app_ui_notification.h>
3 #include <app_service.h>
4
5 #include <time.h>
6 #include <sys/time.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <errno.h>
10
11 #include "download-provider-notification.h"
12 #include "download-provider-utils.h"
13 #include "download-provider-log.h"
14
15 #include <libintl.h>
16
17 #define S_(s) dgettext("sys_string", s)
18
19 #define DP_NOTIFICATION_ICON_PATH IMAGE_DIR"/Q02_Notification_Download_failed.png"
20
21 void print_app_error_message(int ret)
22 {
23         switch (ret) {
24         case APP_ERROR_NONE:
25                 TRACE_DEBUG_MSG("APP_ERROR_NONE");
26                 break;
27         case APP_ERROR_INVALID_PARAMETER:
28                 TRACE_DEBUG_MSG("APP_ERROR_INVALID_PARAMETER");
29                 break;
30         case APP_ERROR_OUT_OF_MEMORY:
31                 TRACE_DEBUG_MSG("APP_ERROR_OUT_OF_MEMORY");
32                 break;
33         case APP_ERROR_INVALID_CONTEXT:
34                 TRACE_DEBUG_MSG("APP_ERROR_INVALID_CONTEXT");
35                 break;
36         case APP_ERROR_NO_SUCH_FILE:
37                 TRACE_DEBUG_MSG("APP_ERROR_NO_SUCH_FILE");
38                 break;
39         case APP_ERROR_ALREADY_RUNNING:
40                 TRACE_DEBUG_MSG("APP_ERROR_ALREADY_RUNNING");
41                 break;
42         default:
43                 TRACE_DEBUG_MSG("Unknown error");
44                 break;
45         }
46 }
47
48 void print_notification_error_message(int ret)
49 {
50         switch (ret) {
51         case UI_NOTIFICATION_ERROR_NONE:
52                 TRACE_DEBUG_MSG("UI_NOTIFICATION_ERROR_NONE");
53                 break;
54         case UI_NOTIFICATION_ERROR_INVALID_PARAMETER:
55                 TRACE_DEBUG_MSG("UI_NOTIFICATION_ERROR_INVALID_PARAMETER");
56                 break;
57         case UI_NOTIFICATION_ERROR_OUT_OF_MEMORY:
58                 TRACE_DEBUG_MSG("UI_NOTIFICATION_ERROR_OUT_OF_MEMORY");
59                 break;
60         case UI_NOTIFICATION_ERROR_DB_FAILED:
61                 TRACE_DEBUG_MSG("UI_NOTIFICATION_ERROR_DB_FAILED");
62                 break;
63         case UI_NOTIFICATION_ERROR_NO_SUCH_FILE:
64                 TRACE_DEBUG_MSG("UI_NOTIFICATION_ERROR_NO_SUCH_FILE");
65                 break;
66         case UI_NOTIFICATION_ERROR_INVALID_STATE:
67                 TRACE_DEBUG_MSG("UI_NOTIFICATION_ERROR_INVALID_STATE");
68                 break;
69         default:
70                 TRACE_DEBUG_MSG("Unknown error");
71                 break;
72         }
73 }
74
75 char *__get_string_status(int state)
76 {
77         char *message = NULL;
78         switch (state) {
79         case DOWNLOAD_STATE_INSTALLING:
80                 message = S_("IDS_COM_POP_INSTALLING_ING");
81                 break;
82         case DOWNLOAD_STATE_FINISHED:
83                 message = strdup("Completed");
84                 break;
85         case DOWNLOAD_STATE_STOPPED:
86                 message = S_("IDS_COM_POP_CANCELLED");
87                 break;
88         case DOWNLOAD_STATE_FAILED:
89                 message = S_("IDS_COM_POP_FAILED");
90                 break;
91         default:
92                 break;
93         }
94         return message;
95 }
96
97 bool download_provider_appfw_notification_cb(ui_notification_h notification,
98                                                         void *user_data)
99 {
100         if (!user_data)
101                 return false;
102         download_clientinfo *clientinfo = (download_clientinfo *) user_data;
103         if (!clientinfo->downloadinfo)
104                 return false;
105
106         bool checkInfo = false;
107         char *title = NULL;
108         char *content = NULL;
109         ui_notification_get_title(notification, &title);
110         ui_notification_get_content(notification, &content);
111
112         TRACE_DEBUG_MSG("title [%s]", title);
113         TRACE_DEBUG_MSG("content [%s]", content);
114
115         // relatively unique
116         if (title && clientinfo->downloadinfo->content_name) {
117                 int title_length = strlen(title);
118                 int content_name_length =
119                         strlen(clientinfo->downloadinfo->content_name);
120                 if (title_length == content_name_length)
121                         if (strncmp
122                                 (title, clientinfo->downloadinfo->content_name,
123                                 title_length) == 0)
124                                 checkInfo = true;
125         }
126         // Only when matched title.
127         if (checkInfo && content) {
128                 char *failed_content =
129                         __get_string_status(DOWNLOAD_STATE_FAILED);
130                 if (failed_content) {
131                         int content_length = strlen(content);
132                         int content_name_length = strlen(failed_content);
133                         if (content_length == content_name_length)
134                                 if (strncmp
135                                         (content, failed_content,
136                                         content_length) == 0)
137                                         checkInfo = true;
138                         free(failed_content);
139                 }
140                 if (!checkInfo) {
141                         char *stopped_content =
142                                 __get_string_status(DOWNLOAD_STATE_STOPPED);
143                         if (stopped_content) {
144                                 int content_length = strlen(content);
145                                 int content_name_length =
146                                         strlen(stopped_content);
147                                 if (content_length == content_name_length)
148                                         if (strncmp
149                                                 (content, stopped_content,
150                                                 content_length) == 0)
151                                                 checkInfo = true;
152                                 free(stopped_content);
153                         }
154                 }
155         }
156
157         if (title)
158                 free(title);
159         if (content)
160                 free(content);
161
162         if (checkInfo) {        // compare info with noti info.
163                 TRACE_DEBUG_MSG("ui_notification_cancel");
164                 ui_notification_cancel(notification);
165                 return false;   // do not search noti item anymore.
166         }
167         return true;
168 }
169
170 int destroy_appfw_service(download_clientinfo *clientinfo)
171 {
172         if (!clientinfo)
173                 return -1;
174
175         if (clientinfo->service_handle) {
176                 service_destroy(clientinfo->service_handle);
177         }
178         clientinfo->service_handle = NULL;
179         return 0;
180 }
181
182 int create_appfw_service(download_clientinfo *clientinfo)
183 {
184         if (!clientinfo)
185                 return -1;
186
187         if (!clientinfo->service_handle)
188                 destroy_appfw_service(clientinfo);
189         if (service_create(&clientinfo->service_handle) < 0) {
190                 TRACE_DEBUG_MSG("failed service_create (%s)", strerror(errno));
191                 return false;
192         }
193
194         if (clientinfo->requestinfo
195                 && clientinfo->requestinfo->client_packagename.str) {
196                 if (service_set_package(clientinfo->service_handle,
197                                         clientinfo->requestinfo->
198                                         client_packagename.str) < 0)
199                         TRACE_DEBUG_MSG("failed service_set_package (%s)",
200                                         strerror(errno));
201         }
202         return 0;
203 }
204
205 int destroy_appfw_notification(download_clientinfo *clientinfo)
206 {
207         if (!clientinfo)
208                 return -1;
209
210         destroy_appfw_service(clientinfo);
211         if (clientinfo->ui_notification_handle) {
212                 bool ongoing = 0;
213                 ui_notification_is_ongoing(clientinfo->ui_notification_handle,
214                                                 &ongoing);
215                 if (ongoing) {
216                         if (ui_notification_cancel
217                                 (clientinfo->ui_notification_handle) < 0)
218                                 TRACE_DEBUG_MSG("Fail ui_notification_cancel");
219                 }
220                 ui_notification_destroy(clientinfo->ui_notification_handle);
221         }
222         clientinfo->ui_notification_handle = NULL;
223         return 0;
224 }
225
226 int create_appfw_notification(download_clientinfo *clientinfo, bool ongoing)
227 {
228         if (!clientinfo)
229                 return -1;
230
231         int ret = 0;
232
233         if (ui_notification_create(ongoing, &clientinfo->ui_notification_handle)
234                 < 0) {
235                 TRACE_DEBUG_MSG("Fail to create notification handle");
236                 return -1;
237         }
238
239         if (clientinfo->downloadinfo) {
240                 if (clientinfo->downloadinfo->content_name) {
241                         if (ui_notification_set_title
242                                 (clientinfo->ui_notification_handle,
243                                 clientinfo->downloadinfo->content_name) < 0) {
244                                 TRACE_DEBUG_MSG
245                                         ("failed ui_notification_set_title (%s)",
246                                         strerror(errno));
247                                 destroy_appfw_notification(clientinfo);
248                                 return -1;
249                         }
250                 }
251         }
252
253         if (ui_notification_set_icon
254                 (clientinfo->ui_notification_handle,
255                 DP_NOTIFICATION_ICON_PATH) < 0) {
256                 TRACE_DEBUG_MSG("Fail ui_notification_set_icon (%s)",
257                                 strerror(errno));
258                 destroy_appfw_notification(clientinfo);
259                 return -1;
260         }
261
262         create_appfw_service(clientinfo);
263
264         if (clientinfo->service_handle) {
265                 if (!ongoing) {
266                         // view the special viewer by contents
267                         if (clientinfo->service_handle
268                                 && clientinfo->downloadinginfo
269                                 && clientinfo->downloadinginfo->saved_path
270                                 && clientinfo->state == DOWNLOAD_STATE_FINISHED) {
271                                 if (service_set_operation
272                                         (clientinfo->service_handle,
273                                         SERVICE_OPERATION_VIEW) < 0) {
274                                         TRACE_DEBUG_MSG
275                                                 ("Fail service_set_operation");
276                                         destroy_appfw_service(clientinfo);
277                                 }
278                                 if (service_set_uri(clientinfo->service_handle,
279                                                         clientinfo->downloadinginfo->saved_path)
280                                         < 0) {
281                                         TRACE_DEBUG_MSG("Fail service_set_uri");
282                                         destroy_appfw_service(clientinfo);
283                                 }
284                         }
285                 }
286                 if (ui_notification_set_service
287                         (clientinfo->ui_notification_handle,
288                         clientinfo->service_handle) < 0) {
289                         TRACE_DEBUG_MSG("Fail ui_notification_set_service");
290                         destroy_appfw_service(clientinfo);
291                 }
292         }
293
294         if ((ret =
295                 ui_notification_post(clientinfo->ui_notification_handle)) !=
296                 UI_NOTIFICATION_ERROR_NONE) {
297                 TRACE_DEBUG_MSG("Fail to post [%d]", ret);
298                 print_notification_error_message(ret);
299                 destroy_appfw_notification(clientinfo);
300                 return -1;
301         }
302
303         return 0;
304 }
305
306 int set_downloadinginfo_appfw_notification(download_clientinfo *clientinfo)
307 {
308         if (!clientinfo || !clientinfo->downloadinginfo)
309                 return -1;
310
311         if (!clientinfo->ui_notification_handle) {
312                 create_appfw_notification(clientinfo, true);
313         }
314
315         if (!clientinfo->ui_notification_handle)
316                 return -1;
317
318         if (clientinfo->downloadinfo && clientinfo->downloadinfo->file_size > 0) {
319                 double progress =
320                         (double)clientinfo->downloadinginfo->received_size /
321                         (double)clientinfo->downloadinfo->file_size;
322                 if (ui_notification_update_progress
323                         (clientinfo->ui_notification_handle,
324                         UI_NOTIFICATION_PROGRESS_TYPE_PERCENTAGE, progress) < 0) {
325                         TRACE_DEBUG_MSG("Fail to update progress");
326                         destroy_appfw_notification(clientinfo);
327                         return -1;
328                 }
329         } else {
330                 if (ui_notification_update_progress
331                         (clientinfo->ui_notification_handle,
332                         UI_NOTIFICATION_PROGRESS_TYPE_SIZE,
333                         (double)(clientinfo->downloadinginfo->received_size)) <
334                         0) {
335                         TRACE_DEBUG_MSG("Fail to update size");
336                         destroy_appfw_notification(clientinfo);
337                         return -1;
338                 }
339         }
340
341         return 0;
342 }
343
344 int set_downloadedinfo_appfw_notification(download_clientinfo *clientinfo)
345 {
346         if (!clientinfo)
347                 return -1;
348
349         destroy_appfw_notification(clientinfo);
350
351         if (!clientinfo->ui_notification_handle)
352                 create_appfw_notification(clientinfo, false);
353
354         // fill downloaded info to post to notification bar.
355         char *message = __get_string_status(clientinfo->state);
356         TRACE_DEBUG_MSG("message : [%s]", message);
357         if (message) {
358                 if (ui_notification_set_content
359                         (clientinfo->ui_notification_handle, message) < 0)
360                         TRACE_DEBUG_MSG("Fail to set content");
361         }
362
363         time_t tt = time(NULL);
364         struct tm *localTime = localtime(&tt);
365
366         if (ui_notification_set_time
367                 (clientinfo->ui_notification_handle, localTime) < 0)
368                 TRACE_DEBUG_MSG("Fail to set time");
369
370         if (ui_notification_update(clientinfo->ui_notification_handle) < 0)
371                 TRACE_DEBUG_MSG("Fail to ui_notification_update");
372
373         destroy_appfw_notification(clientinfo);
374         return 0;
375 }