cf35bbb0df02bc421c3a75f6e6d27c634335d6ac
[platform/framework/web/download-provider.git] / provider / download-provider-notification.c
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifdef SUPPORT_NOTIFICATION
18 #include <time.h>
19 #include <sys/time.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22
23 #include "download-provider.h"
24 #include "download-provider-notification.h"
25 #include "download-provider-db-defs.h"
26 #include "download-provider-db.h"
27 #include "download-provider-log.h"
28 #include "download-provider-client.h"
29 #include "download-provider-utils.h"
30
31 #include <bundle.h>
32 #include <bundle_internal.h>
33 #include <notification.h>
34
35 #include "notification_text_domain.h"
36 #include "notification_internal.h"
37 #include "notification_error.h"
38
39 #include <appsvc.h>
40
41 #include <vconf.h>
42 #include <locale.h>
43 #include <libintl.h>
44
45 #include <tzplatform_config.h>
46
47 #define __(s) dgettext(PKG_NAME, s)
48
49 #define DP_DOMAIN PKG_NAME
50 #define DP_LOCALEDIR LOCALE_DIR
51 #define DP_NOTIFICATION_NO_SUBJECT "No Name"
52
53 #ifdef _TIZEN_2_3_UX
54 #define DP_NOTIFICATION_DRM_ICON_PATH IMAGE_DIR"/download_manager_icon_drm.png"
55 #define DP_NOTIFICATION_UNKNOWN_ICON_PATH IMAGE_DIR"/download_manager_icon_unknown.png"
56 #define DP_NOTIFICATION_EXCEL_ICON_PATH IMAGE_DIR"/download_manager_icon_xls.png"
57 #define DP_NOTIFICATION_HTML_ICON_PATH IMAGE_DIR"/download_manager_icon_html.png"
58 #define DP_NOTIFICATION_MUSIC_ICON_PATH IMAGE_DIR"/download_manager_icon_music.png"
59 #define DP_NOTIFICATION_PDF_ICON_PATH IMAGE_DIR"/download_manager_icon_pdf.png"
60 #define DP_NOTIFICATION_PPT_ICON_PATH IMAGE_DIR"/download_manager_icon_ppt.png"
61 #define DP_NOTIFICATION_TEXT_ICON_PATH IMAGE_DIR"/download_manager_icon_text.png"
62 #define DP_NOTIFICATION_WORD_ICON_PATH IMAGE_DIR"/download_manager_icon_word.png"
63 #define DP_NOTIFICATION_VIDEO_ICON_PATH IMAGE_DIR"/download_manager_icon_movie.png"
64 #define DP_NOTIFICATION_IMAGE_ICON_PATH IMAGE_DIR"/download_manager_icon_img.png"
65 #define DP_NOTIFICATION_FALSH_ICON_PATH IMAGE_DIR"/download_manager_icon_swf.png"
66 #define DP_NOTIFICATION_TPK_ICON_PATH IMAGE_DIR"/download_manager_icon_tpk.png"
67 #define DP_NOTIFICATION_VCAL_ICON_PATH IMAGE_DIR"/download_manager_icon_date.png"
68
69 // base path
70 #define QP_PRELOAD_NOTI_ICON_PATH tzplatform_mkpath(TZ_SYS_RO_APP, "com.samsung.quickpanel/shared/res/noti_icons")
71
72 // each application path
73 #define QP_PRELOAD_COMMON_PATH tzplatform_mkpath(TZ_SYS_RO_APP, "com.samsung.quickpanel/shared/res/noti_icons/Common")
74 #define DP_NOTIFICATION_COMPLETED_ICON_PATH tzplatform_mkpath(TZ_SYS_RO_APP, "com.samsung.quickpanel/shared/res/noti_icons/Common/noti_download_complete.png")
75 #define DP_NOTIFICATION_FAILED_ICON_PATH tzplatform_mkpath(TZ_SYS_RO_APP, "com.samsung.quickpanel/shared/res/noti_icons/Common/noti_download_failed.png")
76
77 #define DP_NOTIFICATION_ONGOING_ICON_PATH "reserved://quickpanel/ani/downloading"
78 #define DP_NOTIFICATION_DOWNLOADING_ICON_PATH "reserved://indicator/ani/downloading"
79 #define DP_NOTIFICATION_FAILED_INDICATOR_ICON_PATH IMAGE_DIR"/B03_Processing_download_failed.png"
80 #define DP_NOTIFICATION_COMPLETED_INDICATOR_ICON_PATH IMAGE_DIR"/B03_Processing_download_complete.png"
81 #else
82 #define DP_NOTIFICATION_DRM_ICON_PATH IMAGE_DIR"/U01_icon_drm.png"
83 #define DP_NOTIFICATION_UNKNOWN_ICON_PATH IMAGE_DIR"/U01_icon_unkown.png"
84 #define DP_NOTIFICATION_EXCEL_ICON_PATH IMAGE_DIR"/U01_icon_excel.png"
85 #define DP_NOTIFICATION_HTML_ICON_PATH IMAGE_DIR"/U01_icon_html.png"
86 #define DP_NOTIFICATION_MUSIC_ICON_PATH IMAGE_DIR"/U01_list_icon_mp3.png"
87 #define DP_NOTIFICATION_PDF_ICON_PATH IMAGE_DIR"/U01_icon_pdf.png"
88 #define DP_NOTIFICATION_PPT_ICON_PATH IMAGE_DIR"/U01_icon_ppt.png"
89 #define DP_NOTIFICATION_TEXT_ICON_PATH IMAGE_DIR"/U01_icon_text.png"
90 #define DP_NOTIFICATION_WORD_ICON_PATH IMAGE_DIR"/U01_icon_word.png"
91 #define DP_NOTIFICATION_VIDEO_ICON_PATH IMAGE_DIR"/U01_list_icon_mp4.png"
92 #define DP_NOTIFICATION_IMAGE_ICON_PATH IMAGE_DIR"/U01_list_icon_image.png"
93 #define DP_NOTIFICATION_FALSH_ICON_PATH IMAGE_DIR"/U01_icon_swf.png"
94 #define DP_NOTIFICATION_TPK_ICON_PATH IMAGE_DIR"/U01_icon_tpk.png"
95 #define DP_NOTIFICATION_VCAL_ICON_PATH IMAGE_DIR"/U01_icon_vcs.png"
96
97 #define DP_NOTIFICATION_FAILED_ICON_PATH IMAGE_DIR"/noti_download_failed.png"
98 #define DP_NOTIFICATION_COMPLETED_ICON_PATH IMAGE_DIR"/noti_download_complete.png"
99
100 #define DP_NOTIFICATION_ONGOING_ICON_PATH "reserved://quickpanel/ani/downloading"
101 #define DP_NOTIFICATION_DOWNLOADING_ICON_PATH "reserved://indicator/ani/downloading"
102 #define DP_NOTIFICATION_FAILED_INDICATOR_ICON_PATH IMAGE_DIR"/B03_processing_download_fail.png"
103 #define DP_NOTIFICATION_COMPLETED_INDICATOR_ICON_PATH IMAGE_DIR"/B03_processing_download_complete.png"
104 #endif
105
106 #define DP_MAX_ICONS_TABLE_COUNT 15
107
108 char *file_icons_table[DP_MAX_ICONS_TABLE_COUNT]={
109                 //unknown file type
110                 DP_NOTIFICATION_UNKNOWN_ICON_PATH,
111                 //image
112                 DP_NOTIFICATION_IMAGE_ICON_PATH,
113                 //video
114                 DP_NOTIFICATION_VIDEO_ICON_PATH,
115                 // audio /music
116                 DP_NOTIFICATION_MUSIC_ICON_PATH,
117                 // PDF
118                 DP_NOTIFICATION_PDF_ICON_PATH,
119                 // word
120                 DP_NOTIFICATION_WORD_ICON_PATH,
121                 // ppt
122                 DP_NOTIFICATION_PPT_ICON_PATH,
123                 // excel
124                 DP_NOTIFICATION_EXCEL_ICON_PATH,
125                 // html
126                 DP_NOTIFICATION_HTML_ICON_PATH,
127                 // txt
128                 DP_NOTIFICATION_TEXT_ICON_PATH,
129                 // DRM
130                 DP_NOTIFICATION_DRM_ICON_PATH,
131                 DP_NOTIFICATION_DRM_ICON_PATH,
132                 DP_NOTIFICATION_FALSH_ICON_PATH,
133                 DP_NOTIFICATION_TPK_ICON_PATH,
134                 DP_NOTIFICATION_VCAL_ICON_PATH,
135 };
136
137 static const char *__dp_noti_error_str(int err)
138 {
139         switch (err) {
140         case NOTIFICATION_ERROR_INVALID_PARAMETER:
141                 return "NOTIFICATION_ERROR_INVALID_PARAMETER";
142         case NOTIFICATION_ERROR_OUT_OF_MEMORY:
143                 return "NOTIFICATION_ERROR_OUT_OF_MEMORY";
144         case NOTIFICATION_ERROR_FROM_DB:
145                 return "NOTIFICATION_ERROR_FROM_DB";
146         case NOTIFICATION_ERROR_ALREADY_EXIST_ID:
147                 return "NOTIFICATION_ERROR_ALREADY_EXIST_ID";
148         case NOTIFICATION_ERROR_FROM_DBUS:
149                 return "NOTIFICATION_ERROR_FROM_DBUS";
150         case NOTIFICATION_ERROR_NOT_EXIST_ID:
151                 return "NOTIFICATION_ERROR_NOT_EXIST_ID";
152         default:
153                 break;
154         }
155         return "Unknown error";
156 }
157
158 char *__dp_noti_get_sender(char *url)
159 {
160         char *temp = NULL;
161         char *found = NULL;
162         char *found1 = NULL;
163         char *sender = NULL;
164         char *credential_sender = NULL;
165
166         if (url == NULL)
167                 return NULL;
168
169         found = strstr(url, "://");
170         if (found != NULL) {
171                 temp = found + 3;
172         } else {
173                 temp = url;
174         }
175         found = strchr(temp, '/');
176         if (found != NULL) {
177                 int len = 0;
178                 len = found - temp;
179                 sender = calloc(len + 1, sizeof(char));
180                 if (sender == NULL)
181                         return NULL;
182                 snprintf(sender, len + 1, "%s", temp);
183         } else {
184                 sender = dp_strdup(temp);
185         if (sender == NULL)
186             return NULL;
187         }
188
189         // For credential URL
190         found = strchr(sender, '@');
191         found1 = strchr(sender, ':');
192         if (found != NULL && found1 != NULL && found1 < found) {
193                 int len = 0;
194                 found = found + 1;
195                 len = strlen(found);
196                 credential_sender = calloc(len + 1, sizeof(char));
197                 if (credential_sender == NULL) {
198                         free(sender);
199                         return NULL;
200                 }
201                 snprintf(credential_sender, len + 1, "%s", found);
202                 free(sender);
203                 return credential_sender;
204         } else {
205                 return sender;
206         }
207 }
208
209 static char *__dp_noti_status(dp_state_type state)
210 {
211         char *message = NULL;
212         switch (state) {
213         case DP_STATE_COMPLETED:
214                 message = "IDS_DM_HEADER_DOWNLOAD_COMPLETE";
215                 break;
216         case DP_STATE_CANCELED:
217         case DP_STATE_FAILED:
218                 message = "IDS_DM_BODY_DOWNLOAD_FAILED_M_STATUS_ABB";
219                 break;
220         default:
221                 break;
222         }
223         return message;
224 }
225
226 void __lang_changed_cb(keynode_t *key, void* data)
227 {
228         char *str = NULL;
229         str = vconf_get_str(VCONFKEY_LANGSET);
230         if (str != NULL) {
231                 setlocale(LC_ALL, str);
232                 bindtextdomain(PKG_NAME, LOCALE_DIR);
233                 textdomain(PKG_NAME);
234         }
235         free(str);
236 }
237
238 void dp_notification_set_locale()
239 {
240         // move to notification.c
241         // locale
242         __lang_changed_cb(NULL, NULL);
243         if (vconf_notify_key_changed(VCONFKEY_LANGSET, __lang_changed_cb, NULL) != 0)
244                 TRACE_ERROR("Fail to set language changed vconf callback");
245 }
246
247 void dp_notification_clear_locale()
248 {
249         // move to notification.c
250         if (vconf_ignore_key_changed(VCONFKEY_LANGSET, __lang_changed_cb) != 0)
251                 TRACE_ERROR("Fail to unset language changed vconf callback");
252 }
253
254 void dp_notification_clear_ongoings()
255 {
256         int err = NOTIFICATION_ERROR_NONE;
257         err = notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_ONGOING);
258         if (err != NOTIFICATION_ERROR_NONE) {
259                 TRACE_DEBUG("[FAIL] clear noti [%s]", __dp_noti_error_str(err));
260         }
261         return;
262 }
263
264 int dp_notification_ongoing_new(const char *pkgname, const char *subject, unsigned char *raws_buffer, const int raws_length)
265 {
266         int err = NOTIFICATION_ERROR_NONE;
267         notification_h noti_handle = NULL;
268         noti_handle = notification_create(NOTIFICATION_TYPE_ONGOING);
269         if (noti_handle == NULL) {
270                 TRACE_ERROR("failed to create notification handle");
271                 return -1;
272         }
273
274         if (subject != NULL) {
275                 err = notification_set_text(noti_handle,
276                                 NOTIFICATION_TEXT_TYPE_TITLE, subject, NULL,
277                                 NOTIFICATION_VARIABLE_TYPE_NONE);
278         } else {
279                 err = notification_set_text(noti_handle,
280                                 NOTIFICATION_TEXT_TYPE_TITLE, DP_NOTIFICATION_NO_SUBJECT, NULL,
281                                 NOTIFICATION_VARIABLE_TYPE_NONE);
282         }
283         if (err != NOTIFICATION_ERROR_NONE) {
284                 TRACE_ERROR("failed to set subject error:%s", __dp_noti_error_str(err));
285         }
286
287         err = notification_set_image(noti_handle,
288                         NOTIFICATION_IMAGE_TYPE_ICON,
289                         DP_NOTIFICATION_ONGOING_ICON_PATH);
290         if (err != NOTIFICATION_ERROR_NONE) {
291                 TRACE_ERROR("failed to set icon error:%s", __dp_noti_error_str(err));
292         }
293
294         if (raws_buffer != NULL && raws_length > 0) {
295                 bundle *b = NULL;
296                 b = bundle_decode_raw((bundle_raw *)raws_buffer, raws_length);
297                 if (b != NULL) {
298                         err = notification_set_execute_option(noti_handle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
299                 } else {
300                         b = bundle_create();
301                         if (b != NULL && pkgname != NULL) {
302                                 if (appsvc_set_pkgname(b, pkgname) != APPSVC_RET_OK) {
303                                         TRACE_ERROR("failed to set set pkgname");
304                                 } else {
305                                         err = notification_set_execute_option(noti_handle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
306                                 }
307                         } else {
308                                 TRACE_ERROR("failed to create bundle");
309                         }
310                 }
311                 if (err != NOTIFICATION_ERROR_NONE) {
312                         TRACE_ERROR("failed to set service error:%s", __dp_noti_error_str(err));
313                 }
314                 if (b != NULL)
315                         bundle_free(b);
316         }
317
318         err = notification_set_property(noti_handle,
319                         NOTIFICATION_PROP_DISABLE_TICKERNOTI);
320         if (err != NOTIFICATION_ERROR_NONE) {
321                 TRACE_ERROR("failed to set property error:%s", __dp_noti_error_str(err));
322                 notification_free(noti_handle);
323                 return -1;
324         }
325         err = notification_set_image(noti_handle,
326                         NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR,
327                         DP_NOTIFICATION_DOWNLOADING_ICON_PATH);
328         if (err != NOTIFICATION_ERROR_NONE) {
329                 TRACE_ERROR("failed to set icon indicator error:%s", __dp_noti_error_str(err));
330                 notification_free(noti_handle);
331                 return -1;
332         }
333         err = notification_set_display_applist(noti_handle,
334                         NOTIFICATION_DISPLAY_APP_ALL);
335         if (err != NOTIFICATION_ERROR_NONE) {
336                 TRACE_ERROR("failed to set display app all error:%s", __dp_noti_error_str(err));
337                 notification_free(noti_handle);
338                 return -1;
339         }
340
341         int priv_id = 0;
342         err = notification_insert(noti_handle, &priv_id);
343         if (err != NOTIFICATION_ERROR_NONE) {
344                 TRACE_ERROR("failed to set priv_id error:%s", __dp_noti_error_str(err));
345                 notification_free(noti_handle);
346                 return -1;
347         }
348
349         //TRACE_DEBUG("m_noti_id [%d]", priv_id);
350         notification_free(noti_handle);
351
352         return priv_id; // store on memory for reuse it
353 }
354
355
356
357 int dp_notification_ongoing_update(const int noti_priv_id, const double received_size, const double file_size, const char *subject)
358 {
359         if (noti_priv_id > 0) {
360                 int err = NOTIFICATION_ERROR_NONE;
361                 if (file_size > 0)
362                         err = notification_update_progress(NULL, noti_priv_id, (received_size / file_size));
363                 else
364                         err = notification_update_size(NULL, noti_priv_id, received_size);
365                 if (err != NOTIFICATION_ERROR_NONE) {
366                         TRACE_ERROR("failed to update error:%s", __dp_noti_error_str(err));
367                         // return 0 because progress is called frequently
368                 }
369                 if (subject != NULL) {
370                         notification_h noti_handle = NULL;
371                         noti_handle = notification_load(NULL, noti_priv_id);
372                         if (noti_handle != NULL) {
373                                 err = notification_set_text(noti_handle,
374                                         NOTIFICATION_TEXT_TYPE_TITLE, subject, NULL,
375                                         NOTIFICATION_VARIABLE_TYPE_NONE);
376                                 err = notification_update(noti_handle);
377                                 if (err != NOTIFICATION_ERROR_NONE) {
378                                         TRACE_ERROR("failed to update by priv_id:%d", noti_priv_id);
379                                 }
380                                 notification_free(noti_handle);
381                         } else {
382                                 TRACE_ERROR("failed to load handle by priv_id:%d", noti_priv_id);
383                                 return -1;
384                         }
385                 }
386         }
387         return 0;
388 }
389
390
391 int dp_notification_delete_ongoing(const int noti_priv_id)
392 {
393         int err = NOTIFICATION_ERROR_NONE;
394         err = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_ONGOING, noti_priv_id);
395         if (err != NOTIFICATION_ERROR_NONE) {
396                 TRACE_ERROR("failed to delete notification by priv_id:%d error:%s", noti_priv_id, __dp_noti_error_str(err));
397         }
398         return 0;
399 }
400
401 int dp_notification_delete(const int noti_priv_id)
402 {
403         int err = NOTIFICATION_ERROR_NONE;
404         err = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_NOTI, noti_priv_id);
405         if (err != NOTIFICATION_ERROR_NONE) {
406                 TRACE_ERROR("failed to delete notification by priv_id:%d error:%s", noti_priv_id, __dp_noti_error_str(err));
407         }
408         return 0;
409 }
410
411
412 int dp_notification_new(void *dbhandle, const int download_id, const int state, int content_type, const char *pkgname)
413 {
414         int errorcode = DP_ERROR_NONE;
415         int err = NOTIFICATION_ERROR_NONE;
416
417         if (state != DP_STATE_COMPLETED &&
418                         state != DP_STATE_CANCELED &&
419                         state != DP_STATE_FAILED) {
420                 TRACE_ERROR("deny by invalid state:%d id:%d", state, download_id);
421                 return -1;
422         }
423
424
425         notification_h noti_handle = NULL;
426         noti_handle = notification_create(NOTIFICATION_TYPE_NOTI);
427         if (noti_handle == NULL) {
428                 TRACE_ERROR("failed to create notification handle");
429                 return -1;
430         }
431
432         err = notification_set_layout(noti_handle, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
433         if (err != NOTIFICATION_ERROR_NONE) {
434                 TRACE_ERROR("Fail to set notification layout [%d]", err);
435                 notification_free(noti_handle);
436                 return -1;
437         }
438
439         err = notification_set_text_domain(noti_handle, DP_DOMAIN, DP_LOCALEDIR);
440         if (err != NOTIFICATION_ERROR_NONE) {
441                 TRACE_ERROR("Fail to set text domain [%d]", err);
442                 notification_free(noti_handle);
443                 return -1;
444         }
445
446         char *string = NULL;
447         unsigned length = 0;
448         if (dp_db_get_property_string(dbhandle, download_id, DP_TABLE_NOTIFICATION, DP_DB_COL_NOTI_DESCRIPTION, (unsigned char **)&string, &length, &errorcode) < 0) {
449                 TRACE_ERROR("failed to get description id:%d error:%s", download_id, dp_print_errorcode(errorcode));
450         }
451         if (string != NULL) {
452 #ifdef _TIZEN_2_3_UX
453                 err = notification_set_text(noti_handle,
454                                 NOTIFICATION_TEXT_TYPE_CONTENT, string, NULL,
455                                 NOTIFICATION_VARIABLE_TYPE_NONE);
456 #else
457                 err = notification_set_text(noti_handle,
458                                 NOTIFICATION_TEXT_TYPE_TITLE, string, NULL,
459                                 NOTIFICATION_VARIABLE_TYPE_NONE);
460 #endif
461                 free(string);
462                 if (err != NOTIFICATION_ERROR_NONE) {
463                         TRACE_ERROR("failed to set state id:%d error:%s", download_id, __dp_noti_error_str(err));
464                 }
465         } else {
466                 string = __dp_noti_status(state);
467 #ifdef _TIZEN_2_3_UX
468                 err = notification_set_text(noti_handle,
469                                 NOTIFICATION_TEXT_TYPE_CONTENT, __(string), string,
470                                         NOTIFICATION_VARIABLE_TYPE_NONE);
471 #else
472                 err = notification_set_text(noti_handle,
473                                 NOTIFICATION_TEXT_TYPE_TITLE, __(string), string,
474                                         NOTIFICATION_VARIABLE_TYPE_NONE);
475 #endif
476                 if (err != NOTIFICATION_ERROR_NONE) {
477                         TRACE_ERROR("failed to set state id:%d error:%s", download_id, __dp_noti_error_str(err));
478                 }
479         }
480
481         string = NULL;
482         if (dp_db_get_property_string(dbhandle, download_id, DP_TABLE_NOTIFICATION, DP_DB_COL_NOTI_SUBJECT, (unsigned char **)&string, &length, &errorcode) < 0) {
483                 TRACE_ERROR("failed to get subject id:%d error:%s", download_id, dp_print_errorcode(errorcode));
484         }
485         err = NOTIFICATION_ERROR_NONE;
486         if (string == NULL && dp_db_get_property_string(dbhandle, download_id, DP_TABLE_DOWNLOAD, DP_DB_COL_CONTENT_NAME, (unsigned char **)&string, &length, &errorcode) < 0) {
487                 TRACE_ERROR("failed to get content_name id:%d error:%s", download_id, dp_print_errorcode(errorcode));
488         }
489         if (string == NULL)
490                 string = dp_strdup(DP_NOTIFICATION_NO_SUBJECT);
491         if (string != NULL) {
492 #ifdef _TIZEN_2_3_UX
493                 err = notification_set_text(noti_handle,
494                                 NOTIFICATION_TEXT_TYPE_TITLE, string, NULL,
495                                 NOTIFICATION_VARIABLE_TYPE_NONE);
496 #else
497                 err = notification_set_text(noti_handle,
498                                 NOTIFICATION_TEXT_TYPE_CONTENT, string, NULL,
499                                 NOTIFICATION_VARIABLE_TYPE_NONE);
500 #endif
501                 free(string);
502                 string = NULL;
503                 if (err != NOTIFICATION_ERROR_NONE) {
504                         TRACE_ERROR("failed to set state id:%d error:%s", download_id, __dp_noti_error_str(err));
505                 }
506         }
507         err = notification_set_time(noti_handle, time(NULL));
508         if (err != NOTIFICATION_ERROR_NONE) {
509                 TRACE_ERROR("failed to set time id:%d error:%s", download_id, __dp_noti_error_str(err));
510         }
511
512         bundle *b = NULL;
513         bundle_raw *raws_buffer = NULL;
514         if (state == DP_STATE_COMPLETED) {
515
516                 if (dp_db_get_property_string(dbhandle, download_id, DP_TABLE_NOTIFICATION, DP_DB_COL_NOTI_RAW_COMPLETE, &raws_buffer, &length, &errorcode) < 0) {
517                         TRACE_ERROR("failed to get bundle raws id:%d", download_id);
518                 }
519                 if (raws_buffer != NULL) {
520                         b = bundle_decode_raw(raws_buffer, length);
521                         bundle_free_encoded_rawdata(&raws_buffer);
522                 }
523                 if (b != NULL) {
524                         err = notification_set_execute_option(noti_handle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
525                 } else {
526                         b = bundle_create();
527                         if (dp_db_get_property_string(dbhandle, download_id, DP_TABLE_DOWNLOAD, DP_DB_COL_SAVED_PATH, (unsigned char **)&string, &length, &errorcode) < 0) {
528                                 TRACE_ERROR("failed to get saved_path id:%d error:%s", download_id, dp_print_errorcode(errorcode));
529                         }
530                         if (b != NULL && string != NULL) {
531                                 if (appsvc_set_operation(b, APPSVC_OPERATION_VIEW) != APPSVC_RET_OK) {
532                                         TRACE_ERROR("failed to set service operation id:%d", download_id);
533                                 } else {
534                                         if (appsvc_set_uri(b, string) != APPSVC_RET_OK) {
535                                                 TRACE_ERROR("failed to set service uri id:%d", download_id);
536                                         } else {
537                                                 err = notification_set_execute_option(noti_handle,
538                                                         NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
539                                         }
540                                 }
541                         } else {
542                                 TRACE_SECURE_ERROR("failed to create bundle id:%d path:%s", download_id, string);
543                         }
544                         free(string);
545                         string = NULL;
546                 }
547                 if (err != NOTIFICATION_ERROR_NONE) {
548                         TRACE_ERROR("failed to set service id:%d error:%s", download_id, __dp_noti_error_str(err));
549                 }
550
551                 char *file_type_icon = DP_NOTIFICATION_UNKNOWN_ICON_PATH;
552                 if (content_type > 0 && content_type < DP_MAX_ICONS_TABLE_COUNT)
553                         file_type_icon = file_icons_table[content_type];
554
555                 err = notification_set_image(noti_handle, NOTIFICATION_IMAGE_TYPE_ICON, file_type_icon);
556                 if (err != NOTIFICATION_ERROR_NONE) {
557                         TRACE_ERROR("failed to set icon id:%d error:%s", download_id, __dp_noti_error_str(err));
558                 }
559                 err = notification_set_image(noti_handle, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR,
560                                 DP_NOTIFICATION_COMPLETED_INDICATOR_ICON_PATH);
561                 if (err != NOTIFICATION_ERROR_NONE) {
562                         TRACE_ERROR("failed to set icon id:%d error:%s", download_id, __dp_noti_error_str(err));
563                 }
564
565         } else if (state == DP_STATE_CANCELED || state == DP_STATE_FAILED) {
566
567                 if (dp_db_get_property_string(dbhandle, download_id, DP_TABLE_NOTIFICATION, DP_DB_COL_NOTI_RAW_FAIL, &raws_buffer, &length, &errorcode) < 0) {
568                         TRACE_ERROR("failed to get bundle raws id:%d", download_id);
569                 }
570                 if (raws_buffer != NULL) {
571                         b = bundle_decode_raw(raws_buffer, length);
572                         bundle_free_encoded_rawdata(&raws_buffer);
573                 }
574                 if (b != NULL) {
575                         err = notification_set_execute_option(noti_handle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
576                 } else {
577                         b = bundle_create();
578                         if (b != NULL && pkgname != NULL) {
579                                 if (appsvc_set_pkgname(b, pkgname) != APPSVC_RET_OK) {
580                                         TRACE_ERROR("failed to set set pkgname id:%d", download_id);
581                                 } else {
582                                         err = notification_set_execute_option(noti_handle,
583                                                         NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
584                                 }
585                         } else {
586                                 TRACE_ERROR("failed to create bundle id:%d", download_id);
587                         }
588                 }
589                 if (err != NOTIFICATION_ERROR_NONE) {
590                         TRACE_ERROR("failed to set service id:%d error:%s", download_id, __dp_noti_error_str(err));
591                 }
592
593                 err = notification_set_image(noti_handle, NOTIFICATION_IMAGE_TYPE_ICON,
594                                 DP_NOTIFICATION_FAILED_ICON_PATH);
595                 if (err != NOTIFICATION_ERROR_NONE) {
596                         TRACE_ERROR("failed to set icon id:%d error:%s", download_id, __dp_noti_error_str(err));
597                 }
598                 err = notification_set_image(noti_handle, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR,
599                                 DP_NOTIFICATION_FAILED_INDICATOR_ICON_PATH);
600                 if (err != NOTIFICATION_ERROR_NONE) {
601                         TRACE_ERROR("failed to set icon id:%d error:%s", download_id, __dp_noti_error_str(err));
602                 }
603
604         }
605
606         if (b != NULL)
607                 bundle_free(b);
608
609         err = notification_set_property(noti_handle,
610                         NOTIFICATION_PROP_DISABLE_TICKERNOTI);
611         if (err != NOTIFICATION_ERROR_NONE) {
612                 TRACE_ERROR("failed to set property id:%d error:%s", download_id, __dp_noti_error_str(err));
613         }
614         err = notification_set_display_applist(noti_handle,
615                         NOTIFICATION_DISPLAY_APP_ALL ^ NOTIFICATION_DISPLAY_APP_INDICATOR);
616         if (err != NOTIFICATION_ERROR_NONE) {
617                 TRACE_ERROR("failed to set display app all id:%d error:%s", download_id, __dp_noti_error_str(err));
618         }
619
620         int priv_id = 0;
621         err = notification_insert(noti_handle, &priv_id);
622         if (err != NOTIFICATION_ERROR_NONE) {
623                 TRACE_ERROR("failed to set priv_id id:%d error:%s", download_id, __dp_noti_error_str(err));
624         }
625
626         //TRACE_DEBUG("m_noti_id [%d]", priv_id);
627         notification_free(noti_handle);
628
629         return priv_id;
630 }
631 #else
632 void dp_notification_clear_ongoings()
633 {
634         return;
635 }
636 #endif