2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifdef SUPPORT_NOTIFICATION
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"
32 #include <bundle_internal.h>
33 #include <notification.h>
35 #include "notification_text_domain.h"
36 #include "notification_internal.h"
37 #include "notification_error.h"
45 #include <tzplatform_config.h>
47 #define __(s) dgettext(PKG_NAME, s)
49 #define DP_DOMAIN PKG_NAME
50 #define DP_LOCALEDIR LOCALE_DIR
51 #define DP_NOTIFICATION_NO_SUBJECT "Preparing to download..."
53 #define DP_NOTIFICATION_DRM_ICON_PATH IMAGE_DIR"/download_manager_icon_drm.png"
54 #define DP_NOTIFICATION_UNKNOWN_ICON_PATH IMAGE_DIR"/download_manager_icon_unknown.png"
55 #define DP_NOTIFICATION_EXCEL_ICON_PATH IMAGE_DIR"/download_manager_icon_xls.png"
56 #define DP_NOTIFICATION_HTML_ICON_PATH IMAGE_DIR"/download_manager_icon_html.png"
57 #define DP_NOTIFICATION_MUSIC_ICON_PATH IMAGE_DIR"/download_manager_icon_music.png"
58 #define DP_NOTIFICATION_PDF_ICON_PATH IMAGE_DIR"/download_manager_icon_pdf.png"
59 #define DP_NOTIFICATION_PPT_ICON_PATH IMAGE_DIR"/download_manager_icon_ppt.png"
60 #define DP_NOTIFICATION_TEXT_ICON_PATH IMAGE_DIR"/download_manager_icon_text.png"
61 #define DP_NOTIFICATION_WORD_ICON_PATH IMAGE_DIR"/download_manager_icon_word.png"
62 #define DP_NOTIFICATION_VIDEO_ICON_PATH IMAGE_DIR"/download_manager_icon_movie.png"
63 #define DP_NOTIFICATION_IMAGE_ICON_PATH IMAGE_DIR"/download_manager_icon_img.png"
64 #define DP_NOTIFICATION_FALSH_ICON_PATH IMAGE_DIR"/download_manager_icon_swf.png"
65 #define DP_NOTIFICATION_TPK_ICON_PATH IMAGE_DIR"/download_manager_icon_tpk.png"
66 #define DP_NOTIFICATION_VCAL_ICON_PATH IMAGE_DIR"/download_manager_icon_date.png"
69 #define QP_PRELOAD_NOTI_ICON_PATH tzplatform_mkpath(TZ_SYS_RO_APP, "com.samsung.quickpanel/shared/res/noti_icons")
71 // each application path
72 #define QP_PRELOAD_COMMON_PATH tzplatform_mkpath(TZ_SYS_RO_APP, "com.samsung.quickpanel/shared/res/noti_icons/Common")
73 #define DP_NOTIFICATION_COMPLETED_ICON_PATH tzplatform_mkpath(TZ_SYS_RO_APP, "com.samsung.quickpanel/shared/res/noti_icons/Common/noti_download_complete.png")
74 #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 #define DP_NOTIFICATION_ONGOING_ICON_PATH "reserved://quickpanel/ani/downloading"
77 #define DP_NOTIFICATION_DOWNLOADING_ICON_PATH "reserved://indicator/ani/downloading"
78 #define DP_NOTIFICATION_FAILED_INDICATOR_ICON_PATH IMAGE_DIR"/B03_Processing_download_failed.png"
79 #define DP_NOTIFICATION_COMPLETED_INDICATOR_ICON_PATH IMAGE_DIR"/B03_Processing_download_complete.png"
81 #define DP_MAX_ICONS_TABLE_COUNT 15
83 static char *file_icons_table[DP_MAX_ICONS_TABLE_COUNT] = {
85 DP_NOTIFICATION_UNKNOWN_ICON_PATH,
87 DP_NOTIFICATION_IMAGE_ICON_PATH,
89 DP_NOTIFICATION_VIDEO_ICON_PATH,
91 DP_NOTIFICATION_MUSIC_ICON_PATH,
93 DP_NOTIFICATION_PDF_ICON_PATH,
95 DP_NOTIFICATION_WORD_ICON_PATH,
97 DP_NOTIFICATION_PPT_ICON_PATH,
99 DP_NOTIFICATION_EXCEL_ICON_PATH,
101 DP_NOTIFICATION_HTML_ICON_PATH,
103 DP_NOTIFICATION_TEXT_ICON_PATH,
105 DP_NOTIFICATION_DRM_ICON_PATH,
106 DP_NOTIFICATION_DRM_ICON_PATH,
107 DP_NOTIFICATION_FALSH_ICON_PATH,
108 DP_NOTIFICATION_TPK_ICON_PATH,
109 DP_NOTIFICATION_VCAL_ICON_PATH,
112 static const char *__dp_noti_error_str(int err)
115 case NOTIFICATION_ERROR_INVALID_PARAMETER:
116 return "NOTIFICATION_ERROR_INVALID_PARAMETER";
117 case NOTIFICATION_ERROR_OUT_OF_MEMORY:
118 return "NOTIFICATION_ERROR_OUT_OF_MEMORY";
119 case NOTIFICATION_ERROR_FROM_DB:
120 return "NOTIFICATION_ERROR_FROM_DB";
121 case NOTIFICATION_ERROR_ALREADY_EXIST_ID:
122 return "NOTIFICATION_ERROR_ALREADY_EXIST_ID";
123 case NOTIFICATION_ERROR_FROM_DBUS:
124 return "NOTIFICATION_ERROR_FROM_DBUS";
125 case NOTIFICATION_ERROR_NOT_EXIST_ID:
126 return "NOTIFICATION_ERROR_NOT_EXIST_ID";
130 return "Unknown error";
133 char *__dp_noti_get_sender(char *url)
139 char *credential_sender = NULL;
144 found = strstr(url, "://");
150 found = strchr(temp, '/');
154 sender = calloc(len + 1, sizeof(char));
157 snprintf(sender, len + 1, "%s", temp);
159 sender = dp_strdup(temp);
164 // For credential URL
165 found = strchr(sender, '@');
166 found1 = strchr(sender, ':');
167 if (found != NULL && found1 != NULL && found1 < found) {
171 credential_sender = calloc(len + 1, sizeof(char));
172 if (credential_sender == NULL) {
176 snprintf(credential_sender, len + 1, "%s", found);
178 return credential_sender;
184 static char *__dp_noti_status(dp_state_type state)
186 char *message = NULL;
188 case DP_STATE_COMPLETED:
189 message = "IDS_DM_HEADER_DOWNLOAD_COMPLETE";
191 case DP_STATE_CANCELED:
192 case DP_STATE_FAILED:
193 message = "IDS_DM_BODY_DOWNLOAD_FAILED_M_STATUS_ABB";
201 void __lang_changed_cb(keynode_t *key, void* data)
204 str = vconf_get_str(VCONFKEY_LANGSET);
206 setlocale(LC_ALL, str);
207 bindtextdomain(PKG_NAME, LOCALE_DIR);
208 textdomain(PKG_NAME);
213 void dp_notification_set_locale()
215 // move to notification.c
217 __lang_changed_cb(NULL, NULL);
218 if (vconf_notify_key_changed(VCONFKEY_LANGSET, __lang_changed_cb, NULL) != 0)
219 TRACE_ERROR("Fail to set language changed vconf callback");
222 void dp_notification_clear_locale()
224 // move to notification.c
225 if (vconf_ignore_key_changed(VCONFKEY_LANGSET, __lang_changed_cb) != 0)
226 TRACE_ERROR("Fail to unset language changed vconf callback");
229 void dp_notification_clear_ongoings()
231 int err = NOTIFICATION_ERROR_NONE;
232 err = notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_ONGOING);
233 if (err != NOTIFICATION_ERROR_NONE)
234 TRACE_DEBUG("[FAIL] clear noti [%s]", __dp_noti_error_str(err));
238 int dp_notification_ongoing_new(const char *url, const char *pkgname, const char *subject, unsigned char *raws_buffer, const int raws_length)
240 int err = NOTIFICATION_ERROR_NONE;
241 notification_h noti_handle = NULL;
242 noti_handle = notification_create(NOTIFICATION_TYPE_ONGOING);
243 if (noti_handle == NULL) {
244 TRACE_ERROR("failed to create notification handle");
248 if (subject != NULL) {
249 err = notification_set_text(noti_handle,
250 NOTIFICATION_TEXT_TYPE_TITLE, subject, NULL,
251 NOTIFICATION_VARIABLE_TYPE_NONE);
253 err = notification_set_text(noti_handle,
254 NOTIFICATION_TEXT_TYPE_TITLE, DP_NOTIFICATION_NO_SUBJECT, NULL,
255 NOTIFICATION_VARIABLE_TYPE_NONE);
257 if (err != NOTIFICATION_ERROR_NONE)
258 TRACE_ERROR("failed to set subject error:%s", __dp_noti_error_str(err));
261 err = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_CONTENT, url, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
262 if (err != NOTIFICATION_ERROR_NONE)
263 TRACE_ERROR("failed to set url error:%s", __dp_noti_error_str(err));
268 err = notification_set_image(noti_handle,
269 NOTIFICATION_IMAGE_TYPE_ICON,
270 DP_NOTIFICATION_ONGOING_ICON_PATH);
271 if (err != NOTIFICATION_ERROR_NONE)
272 TRACE_ERROR("failed to set icon error:%s", __dp_noti_error_str(err));
274 if (raws_buffer != NULL && raws_length > 0) {
276 b = bundle_decode_raw((bundle_raw *)raws_buffer, raws_length);
278 err = notification_set_execute_option(noti_handle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
281 if (b != NULL && pkgname != NULL) {
282 if (appsvc_set_pkgname(b, pkgname) != APPSVC_RET_OK)
283 TRACE_ERROR("failed to set set pkgname");
285 err = notification_set_execute_option(noti_handle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
287 TRACE_ERROR("failed to create bundle");
290 if (err != NOTIFICATION_ERROR_NONE)
291 TRACE_ERROR("failed to set service error:%s", __dp_noti_error_str(err));
297 err = notification_set_property(noti_handle,
298 NOTIFICATION_PROP_DISABLE_TICKERNOTI);
299 if (err != NOTIFICATION_ERROR_NONE) {
300 TRACE_ERROR("failed to set property error:%s", __dp_noti_error_str(err));
301 notification_free(noti_handle);
304 err = notification_set_image(noti_handle,
305 NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR,
306 DP_NOTIFICATION_DOWNLOADING_ICON_PATH);
307 if (err != NOTIFICATION_ERROR_NONE) {
308 TRACE_ERROR("failed to set icon indicator error:%s", __dp_noti_error_str(err));
309 notification_free(noti_handle);
312 err = notification_set_display_applist(noti_handle,
313 NOTIFICATION_DISPLAY_APP_ALL);
314 if (err != NOTIFICATION_ERROR_NONE) {
315 TRACE_ERROR("failed to set display app all error:%s", __dp_noti_error_str(err));
316 notification_free(noti_handle);
321 err = notification_insert(noti_handle, &priv_id);
322 if (err != NOTIFICATION_ERROR_NONE) {
323 TRACE_ERROR("failed to set priv_id error:%s", __dp_noti_error_str(err));
324 notification_free(noti_handle);
328 //TRACE_DEBUG("m_noti_id [%d]", priv_id);
329 notification_free(noti_handle);
331 return priv_id; // store on memory for reuse it
336 int dp_notification_ongoing_update(const int noti_priv_id, const double received_size, const double file_size, const char *subject)
338 if (noti_priv_id > 0) {
339 int err = NOTIFICATION_ERROR_NONE;
341 err = notification_update_progress(NULL, noti_priv_id, (received_size / file_size));
343 err = notification_update_size(NULL, noti_priv_id, received_size);
344 if (err != NOTIFICATION_ERROR_NONE) {
345 TRACE_ERROR("failed to update error:%s", __dp_noti_error_str(err));
346 // return 0 because progress is called frequently
348 if (subject != NULL) {
349 notification_h noti_handle = NULL;
350 noti_handle = notification_load(NULL, noti_priv_id);
351 if (noti_handle != NULL) {
352 err = notification_set_text(noti_handle,
353 NOTIFICATION_TEXT_TYPE_TITLE, subject, NULL,
354 NOTIFICATION_VARIABLE_TYPE_NONE);
355 err = notification_update(noti_handle);
356 if (err != NOTIFICATION_ERROR_NONE)
357 TRACE_ERROR("failed to update by priv_id:%d", noti_priv_id);
358 notification_free(noti_handle);
360 TRACE_ERROR("failed to load handle by priv_id:%d", noti_priv_id);
369 int dp_notification_delete_ongoing(const int noti_priv_id)
371 int err = NOTIFICATION_ERROR_NONE;
372 err = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_ONGOING, noti_priv_id);
373 if (err != NOTIFICATION_ERROR_NONE)
374 TRACE_ERROR("failed to delete notification by priv_id:%d error:%s", noti_priv_id, __dp_noti_error_str(err));
379 int dp_notification_delete(const int noti_priv_id)
381 int err = NOTIFICATION_ERROR_NONE;
382 err = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_NOTI, noti_priv_id);
383 if (err != NOTIFICATION_ERROR_NONE)
384 TRACE_ERROR("failed to delete notification by priv_id:%d error:%s", noti_priv_id, __dp_noti_error_str(err));
390 int dp_notification_new(void *dbhandle, const int download_id, const int state, int content_type, const char *pkgname)
392 int errorcode = DP_ERROR_NONE;
393 int err = NOTIFICATION_ERROR_NONE;
395 if (state != DP_STATE_COMPLETED &&
396 state != DP_STATE_CANCELED &&
397 state != DP_STATE_FAILED) {
398 TRACE_ERROR("deny by invalid state:%d id:%d", state, download_id);
403 notification_h noti_handle = NULL;
404 noti_handle = notification_create(NOTIFICATION_TYPE_NOTI);
405 if (noti_handle == NULL) {
406 TRACE_ERROR("failed to create notification handle");
410 err = notification_set_layout(noti_handle, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
411 if (err != NOTIFICATION_ERROR_NONE) {
412 TRACE_ERROR("Fail to set notification layout [%d]", err);
413 notification_free(noti_handle);
417 err = notification_set_text_domain(noti_handle, DP_DOMAIN, DP_LOCALEDIR);
418 if (err != NOTIFICATION_ERROR_NONE) {
419 TRACE_ERROR("Fail to set text domain [%d]", err);
420 notification_free(noti_handle);
426 if (dp_db_get_property_string(dbhandle, download_id, DP_TABLE_NOTIFICATION, DP_DB_COL_NOTI_DESCRIPTION, (unsigned char **)&string, &length, &errorcode) < 0)
427 TRACE_ERROR("failed to get description id:%d error:%s", download_id, dp_print_errorcode(errorcode));
429 if (string != NULL) {
430 err = notification_set_text(noti_handle,
431 NOTIFICATION_TEXT_TYPE_CONTENT, string, NULL,
432 NOTIFICATION_VARIABLE_TYPE_NONE);
434 if (err != NOTIFICATION_ERROR_NONE)
435 TRACE_ERROR("failed to set state id:%d error:%s", download_id, __dp_noti_error_str(err));
437 string = __dp_noti_status(state);
438 err = notification_set_text(noti_handle,
439 NOTIFICATION_TEXT_TYPE_CONTENT, __(string), string,
440 NOTIFICATION_VARIABLE_TYPE_NONE);
441 if (err != NOTIFICATION_ERROR_NONE)
442 TRACE_ERROR("failed to set state id:%d error:%s", download_id, __dp_noti_error_str(err));
446 if (dp_db_get_property_string(dbhandle, download_id, DP_TABLE_NOTIFICATION, DP_DB_COL_NOTI_SUBJECT, (unsigned char **)&string, &length, &errorcode) < 0)
447 TRACE_ERROR("failed to get subject id:%d error:%s", download_id, dp_print_errorcode(errorcode));
448 err = NOTIFICATION_ERROR_NONE;
449 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)
450 TRACE_ERROR("failed to get content_name id:%d error:%s", download_id, dp_print_errorcode(errorcode));
452 string = dp_strdup(DP_NOTIFICATION_NO_SUBJECT);
453 if (string != NULL) {
454 err = notification_set_text(noti_handle,
455 NOTIFICATION_TEXT_TYPE_TITLE, string, NULL,
456 NOTIFICATION_VARIABLE_TYPE_NONE);
459 if (err != NOTIFICATION_ERROR_NONE)
460 TRACE_ERROR("failed to set state id:%d error:%s", download_id, __dp_noti_error_str(err));
462 err = notification_set_time(noti_handle, time(NULL));
463 if (err != NOTIFICATION_ERROR_NONE)
464 TRACE_ERROR("failed to set time id:%d error:%s", download_id, __dp_noti_error_str(err));
467 bundle_raw *raws_buffer = NULL;
468 if (state == DP_STATE_COMPLETED) {
469 if (dp_db_get_property_string(dbhandle, download_id, DP_TABLE_NOTIFICATION, DP_DB_COL_NOTI_RAW_COMPLETE, &raws_buffer, &length, &errorcode) < 0)
470 TRACE_ERROR("failed to get bundle raws id:%d", download_id);
472 if (raws_buffer != NULL) {
473 b = bundle_decode_raw(raws_buffer, length);
474 bundle_free_encoded_rawdata(&raws_buffer);
477 err = notification_set_execute_option(noti_handle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
480 if (dp_db_get_property_string(dbhandle, download_id, DP_TABLE_DOWNLOAD, DP_DB_COL_SAVED_PATH, (unsigned char **)&string, &length, &errorcode) < 0)
481 TRACE_ERROR("failed to get saved_path id:%d error:%s", download_id, dp_print_errorcode(errorcode));
483 if (b != NULL && string != NULL) {
484 if (appsvc_set_operation(b, APPSVC_OPERATION_VIEW) != APPSVC_RET_OK) {
485 TRACE_ERROR("failed to set service operation id:%d", download_id);
487 if (appsvc_set_uri(b, string) != APPSVC_RET_OK) {
488 TRACE_ERROR("failed to set service uri id:%d", download_id);
490 err = notification_set_execute_option(noti_handle,
491 NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
495 TRACE_SECURE_ERROR("failed to create bundle id:%d path:%s", download_id, string);
500 if (err != NOTIFICATION_ERROR_NONE)
501 TRACE_ERROR("failed to set service id:%d error:%s", download_id, __dp_noti_error_str(err));
503 char *file_type_icon = DP_NOTIFICATION_UNKNOWN_ICON_PATH;
504 if (content_type > 0 && content_type < DP_MAX_ICONS_TABLE_COUNT)
505 file_type_icon = file_icons_table[content_type];
507 err = notification_set_image(noti_handle, NOTIFICATION_IMAGE_TYPE_ICON, file_type_icon);
508 if (err != NOTIFICATION_ERROR_NONE)
509 TRACE_ERROR("failed to set icon id:%d error:%s", download_id, __dp_noti_error_str(err));
511 err = notification_set_image(noti_handle, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR,
512 DP_NOTIFICATION_COMPLETED_INDICATOR_ICON_PATH);
513 if (err != NOTIFICATION_ERROR_NONE)
514 TRACE_ERROR("failed to set icon id:%d error:%s", download_id, __dp_noti_error_str(err));
515 } else if (state == DP_STATE_CANCELED || state == DP_STATE_FAILED) {
517 if (dp_db_get_property_string(dbhandle, download_id, DP_TABLE_NOTIFICATION, DP_DB_COL_NOTI_RAW_FAIL, &raws_buffer, &length, &errorcode) < 0)
518 TRACE_ERROR("failed to get bundle raws id:%d", download_id);
520 if (raws_buffer != NULL) {
521 b = bundle_decode_raw(raws_buffer, length);
522 bundle_free_encoded_rawdata(&raws_buffer);
525 err = notification_set_execute_option(noti_handle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
528 if (b != NULL && pkgname != NULL) {
529 if (appsvc_set_pkgname(b, pkgname) != APPSVC_RET_OK) {
530 TRACE_ERROR("failed to set set pkgname id:%d", download_id);
532 err = notification_set_execute_option(noti_handle,
533 NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
536 TRACE_ERROR("failed to create bundle id:%d", download_id);
539 if (err != NOTIFICATION_ERROR_NONE)
540 TRACE_ERROR("failed to set service id:%d error:%s", download_id, __dp_noti_error_str(err));
542 err = notification_set_image(noti_handle, NOTIFICATION_IMAGE_TYPE_ICON,
543 DP_NOTIFICATION_FAILED_ICON_PATH);
544 if (err != NOTIFICATION_ERROR_NONE)
545 TRACE_ERROR("failed to set icon id:%d error:%s", download_id, __dp_noti_error_str(err));
547 err = notification_set_image(noti_handle, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR,
548 DP_NOTIFICATION_FAILED_INDICATOR_ICON_PATH);
549 if (err != NOTIFICATION_ERROR_NONE)
550 TRACE_ERROR("failed to set icon id:%d error:%s", download_id, __dp_noti_error_str(err));
556 err = notification_set_property(noti_handle,
557 NOTIFICATION_PROP_DISABLE_TICKERNOTI);
558 if (err != NOTIFICATION_ERROR_NONE)
559 TRACE_ERROR("failed to set property id:%d error:%s", download_id, __dp_noti_error_str(err));
561 err = notification_set_display_applist(noti_handle,
562 NOTIFICATION_DISPLAY_APP_ALL ^ NOTIFICATION_DISPLAY_APP_INDICATOR);
563 if (err != NOTIFICATION_ERROR_NONE)
564 TRACE_ERROR("failed to set display app all id:%d error:%s", download_id, __dp_noti_error_str(err));
567 err = notification_insert(noti_handle, &priv_id);
568 if (err != NOTIFICATION_ERROR_NONE)
569 TRACE_ERROR("failed to set priv_id id:%d error:%s", download_id, __dp_noti_error_str(err));
571 //TRACE_DEBUG("m_noti_id [%d]", priv_id);
572 notification_free(noti_handle);
577 void dp_notification_clear_ongoings()