Code Sync with Private GIT
[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_FINISHED:
80                 //message = S_("IDS_COM_POP_SUCCESS");
81                 message = "Completed";
82                 break;
83         case DOWNLOAD_STATE_STOPPED:
84                 //message = S_("IDS_COM_POP_CANCELLED");
85                 message = "Canclled";
86                 break;
87         case DOWNLOAD_STATE_FAILED:
88                 //message = S_("IDS_COM_POP_FAILED");
89                 message = "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, bool ongoing)
183 {
184         if (!clientinfo)
185                 return -1;
186
187         if (ongoing) {
188                 if (!clientinfo->service_handle) {
189                         if (service_create(&clientinfo->service_handle) < 0) {
190                                 TRACE_DEBUG_MSG("failed service_create (%s)", strerror(errno));
191                                 return false;
192                         }
193                         if (clientinfo->requestinfo
194                                 && clientinfo->requestinfo->client_packagename.str) {
195                                 if (service_set_package(clientinfo->service_handle,
196                                                         clientinfo->requestinfo->
197                                                         client_packagename.str) < 0)
198                                         TRACE_DEBUG_MSG("failed service_set_package (%s)",
199                                                         strerror(errno));
200                         }
201                 }
202         } else {
203                 if (clientinfo->service_handle) {
204                         destroy_appfw_service(clientinfo);
205                 }
206                 if (service_create(&clientinfo->service_handle) < 0) {
207                         TRACE_DEBUG_MSG("failed service_create (%s)", strerror(errno));
208                         return false;
209                 }
210         }
211         return 0;
212 }
213
214 int destroy_appfw_notification(download_clientinfo *clientinfo)
215 {
216         if (!clientinfo)
217                 return -1;
218
219         destroy_appfw_service(clientinfo);
220         if (clientinfo->ui_notification_handle) {
221                 bool ongoing = 0;
222                 ui_notification_is_ongoing(clientinfo->ui_notification_handle,
223                                                 &ongoing);
224                 if (ongoing) {
225                         if (ui_notification_cancel
226                                 (clientinfo->ui_notification_handle) < 0)
227                                 TRACE_DEBUG_MSG("Fail ui_notification_cancel");
228                 }
229                 ui_notification_destroy(clientinfo->ui_notification_handle);
230         }
231         clientinfo->ui_notification_handle = NULL;
232         return 0;
233 }
234
235 int create_appfw_notification(download_clientinfo *clientinfo, bool ongoing)
236 {
237         if (!clientinfo)
238                 return -1;
239
240         int ret = 0;
241
242         if (ui_notification_create(ongoing, &clientinfo->ui_notification_handle)
243                 < 0) {
244                 TRACE_DEBUG_MSG("Fail to create notification handle");
245                 return -1;
246         }
247
248         if (clientinfo->downloadinfo) {
249                 TRACE_DEBUG_MSG("###content_name[%s]", clientinfo->downloadinfo->content_name);
250                 char *title = clientinfo->downloadinfo->content_name;
251                 if (!title)
252                         //title = S_("IDS_COM_BODY_NO_NAME");
253                         title = "No name";
254                 if (ui_notification_set_title(
255                                 clientinfo->ui_notification_handle, title) < 0) {
256                         TRACE_DEBUG_MSG
257                                 ("failed ui_notification_set_title (%s)",
258                                 strerror(errno));
259                         destroy_appfw_notification(clientinfo);
260                         return -1;
261                 }
262         }
263
264         if (ui_notification_set_icon
265                 (clientinfo->ui_notification_handle,
266                 DP_NOTIFICATION_ICON_PATH) < 0) {
267                 TRACE_DEBUG_MSG("Fail ui_notification_set_icon (%s)",
268                                 strerror(errno));
269                 destroy_appfw_notification(clientinfo);
270                 return -1;
271         }
272
273
274         create_appfw_service(clientinfo, ongoing);
275         if (!ongoing) {
276                 // view the special viewer by contents
277                 if (clientinfo->downloadinginfo
278                         && clientinfo->downloadinginfo->saved_path
279                         && clientinfo->state == DOWNLOAD_STATE_FINISHED) {
280                         if (service_set_operation
281                                 (clientinfo->service_handle,
282                                 SERVICE_OPERATION_VIEW) < 0) {
283                                 TRACE_DEBUG_MSG
284                                         ("Fail service_set_operation");
285                                 destroy_appfw_service(clientinfo);
286                         }
287                         if (service_set_uri(clientinfo->service_handle,
288                                                 clientinfo->downloadinginfo->saved_path)
289                                 < 0) {
290                                 TRACE_DEBUG_MSG("Fail service_set_uri");
291                                 destroy_appfw_service(clientinfo);
292                         }
293                 } else {
294                         if (service_set_package(clientinfo->service_handle,
295                                                 clientinfo->requestinfo->
296                                                 client_packagename.str) < 0)
297                                 TRACE_DEBUG_MSG("failed service_set_package (%s)",
298                                                 strerror(errno));
299                 }
300         }
301         if (ui_notification_set_service
302                 (clientinfo->ui_notification_handle,
303                 clientinfo->service_handle) < 0) {
304                 TRACE_DEBUG_MSG("Fail ui_notification_set_service");
305                 destroy_appfw_service(clientinfo);
306         }
307
308         if ((ret =
309                 ui_notification_post(clientinfo->ui_notification_handle)) !=
310                 UI_NOTIFICATION_ERROR_NONE) {
311                 TRACE_DEBUG_MSG("Fail to post [%d]", ret);
312                 print_notification_error_message(ret);
313                 destroy_appfw_notification(clientinfo);
314                 return -1;
315         }
316
317         return 0;
318 }
319
320 int set_downloadinginfo_appfw_notification(download_clientinfo *clientinfo)
321 {
322         if (!clientinfo || !clientinfo->downloadinginfo)
323                 return -1;
324
325         if (!clientinfo->ui_notification_handle) {
326                 create_appfw_notification(clientinfo, true);
327         }
328
329         if (!clientinfo->ui_notification_handle)
330                 return -1;
331
332         if (clientinfo->downloadinfo && clientinfo->downloadinfo->file_size > 0) {
333                 double progress =
334                         (double)clientinfo->downloadinginfo->received_size /
335                         (double)clientinfo->downloadinfo->file_size;
336                 if (ui_notification_update_progress
337                         (clientinfo->ui_notification_handle,
338                         UI_NOTIFICATION_PROGRESS_TYPE_PERCENTAGE, progress) < 0) {
339                         TRACE_DEBUG_MSG("Fail to update progress");
340                         destroy_appfw_notification(clientinfo);
341                         return -1;
342                 }
343         } else {
344                 if (ui_notification_update_progress
345                         (clientinfo->ui_notification_handle,
346                         UI_NOTIFICATION_PROGRESS_TYPE_SIZE,
347                         (double)(clientinfo->downloadinginfo->received_size)) <
348                         0) {
349                         TRACE_DEBUG_MSG("Fail to update size");
350                         destroy_appfw_notification(clientinfo);
351                         return -1;
352                 }
353         }
354
355         return 0;
356 }
357
358 int set_downloadedinfo_appfw_notification(download_clientinfo *clientinfo)
359 {
360         if (!clientinfo)
361                 return -1;
362
363         destroy_appfw_notification(clientinfo);
364
365         if (!clientinfo->ui_notification_handle)
366                 create_appfw_notification(clientinfo, false);
367
368         // fill downloaded info to post to notification bar.
369         char *message = __get_string_status(clientinfo->state);
370         TRACE_DEBUG_MSG("message : [%s]", message);
371         if (message) {
372                 if (ui_notification_set_content
373                         (clientinfo->ui_notification_handle, message) < 0)
374                         TRACE_DEBUG_MSG("Fail to set content");
375         }
376
377         time_t tt = time(NULL);
378         struct tm *localTime = localtime(&tt);
379
380         if (ui_notification_set_time
381                 (clientinfo->ui_notification_handle, localTime) < 0)
382                 TRACE_DEBUG_MSG("Fail to set time");
383
384         if (ui_notification_update(clientinfo->ui_notification_handle) < 0)
385                 TRACE_DEBUG_MSG("Fail to ui_notification_update");
386
387         destroy_appfw_notification(clientinfo);
388         return 0;
389 }
390
391 void clear_downloadinginfo_appfw_notification()
392 {
393         ui_notification_cancel_all_by_type(true);
394         return;
395 }