2 * Copyright (c) 2012 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.
22 #include "notification.h"
25 #include "download-provider-notification.h"
26 #include "download-provider-request.h"
27 #include "download-provider-db.h"
28 #include "download-provider-log.h"
31 #define S_(s) dgettext("sys_string", s)
33 #define DP_NOTIFICATION_ICON_PATH IMAGE_DIR"/Q02_Notification_Download_failed.png"
35 static const char *__noti_error_str(
36 notification_error_e err)
39 case NOTIFICATION_ERROR_INVALID_DATA:
40 return "NOTIFICATION_ERROR_INVALID_DATA";
41 case NOTIFICATION_ERROR_NO_MEMORY:
42 return "NOTIFICATION_ERROR_NO_MEMORY";
43 case NOTIFICATION_ERROR_FROM_DB:
44 return "NOTIFICATION_ERROR_FROM_DB";
45 case NOTIFICATION_ERROR_ALREADY_EXIST_ID:
46 return "NOTIFICATION_ERROR_ALREADY_EXIST_ID";
47 case NOTIFICATION_ERROR_FROM_DBUS:
48 return "NOTIFICATION_ERROR_FROM_DBUS";
49 case NOTIFICATION_ERROR_NOT_EXIST_ID:
50 return "NOTIFICATION_ERROR_NOT_EXIST_ID";
54 return "Unknown error";
57 static char *__get_string_status(dp_state_type state)
61 case DP_STATE_COMPLETED:
62 //message = S_("IDS_COM_POP_SUCCESS");
63 message = "Completed";
65 case DP_STATE_CANCELED:
66 //message = S_("IDS_COM_POP_CANCELLED");
70 //message = S_("IDS_COM_POP_FAILED");
78 static void __free_char_pointer_array(char **array, int count)
83 for (i = 0; i < count; i++) {
90 static int __set_extra_data(bundle *b, int id)
92 db_conds_list_fmt conds_p;
95 // get count of key list.
97 memset(&conds_p, 0x00, sizeof(db_conds_list_fmt));
98 conds_p.column = DP_DB_COL_ID;
99 conds_p.type = DP_DB_COL_TYPE_INT;
101 count = dp_db_get_conds_rows_count(DP_DB_TABLE_NOTIFICATION,
102 DP_DB_COL_DISTINCT_EXTRA_KEY, "AND", 1, &conds_p);
104 char **keys_array = NULL;
106 keys_array = (char **)calloc(count, sizeof(char *));
107 if (keys_array == NULL) {
108 TRACE_ERROR("[FAIL] calloc failed");
113 dp_db_get_conds_list(DP_DB_TABLE_NOTIFICATION,
114 DP_DB_COL_DISTINCT_EXTRA_KEY , DP_DB_COL_TYPE_TEXT,
115 (void **)keys_array, count, -1, NULL, NULL,
117 if (keys_count <= 0) {
118 TRACE_ERROR("[FAIL] Wrong db data");
119 __free_char_pointer_array(keys_array, 0);
122 for (i = 0; i < keys_count; i++) {
123 db_conds_list_fmt conds_p2[2];
124 int conds_count2 = 2;
125 char **values_array = NULL;
127 int values_count = 0;
129 memset(conds_p2, 0x00, conds_count2 * sizeof(db_conds_list_fmt));
130 conds_p2[0].column = DP_DB_COL_ID;
131 conds_p2[0].type = DP_DB_COL_TYPE_INT;
132 conds_p2[0].value = &id;
133 conds_p2[1].column = DP_DB_COL_EXTRA_KEY;
134 conds_p2[1].type = DP_DB_COL_TYPE_TEXT;
135 conds_p2[1].value = (void *)(keys_array[i]);
137 check_rows = dp_db_get_conds_rows_count(
138 DP_DB_TABLE_NOTIFICATION, DP_DB_COL_EXTRA_VALUE, "AND",
139 conds_count2, conds_p2);
140 if (check_rows <= 0) {
141 TRACE_ERROR("[FAIL] No values about key");
142 __free_char_pointer_array(keys_array, keys_count);
145 values_array = (char **)calloc(check_rows, sizeof(char *));
146 if (values_array == NULL) {
147 TRACE_ERROR("[FAIL] calloc failed");
148 __free_char_pointer_array(keys_array, keys_count);
152 dp_db_get_conds_list(DP_DB_TABLE_NOTIFICATION,
153 DP_DB_COL_EXTRA_VALUE, DP_DB_COL_TYPE_TEXT,
154 (void **)values_array, check_rows, -1, NULL, NULL,
155 "AND", conds_count2, conds_p2);
156 if (values_count <= 0) {
157 TRACE_ERROR("[FAIL] No values about key");
158 __free_char_pointer_array(keys_array, keys_count);
159 __free_char_pointer_array(values_array, 0);
162 if (values_count == 1) {
163 char *key = keys_array[i];
164 char *value = values_array[values_count-1];
165 if (appsvc_add_data(b, key, value) !=
167 TRACE_ERROR("[FAIL] set add data");
168 __free_char_pointer_array(keys_array, keys_count);
169 __free_char_pointer_array(values_array, values_count);
173 char *key = keys_array[i];
174 if (appsvc_add_data_array(b, key, (const char **)values_array,
175 values_count) != APPSVC_RET_OK) {
176 TRACE_ERROR("[FAIL] set add data");
177 __free_char_pointer_array(keys_array, keys_count);
178 __free_char_pointer_array(values_array, values_count);
182 __free_char_pointer_array(values_array, values_count);
184 __free_char_pointer_array(keys_array, keys_count);
189 int dp_set_downloadinginfo_notification(int id, char *packagename)
191 notification_h noti_handle = NULL;
192 notification_error_e err = NOTIFICATION_ERROR_NONE;
196 #ifdef NOTI_NEW_VERSION_API
197 noti_handle = notification_create(NOTIFICATION_TYPE_ONGOING);
199 noti_handle = notification_new(NOTIFICATION_TYPE_ONGOING,
200 NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
204 TRACE_ERROR("[FAIL] create notification handle");
209 dp_db_get_text_column
210 (id, DP_DB_TABLE_DOWNLOAD_INFO, DP_DB_COL_CONTENT_NAME);
212 if (content_name == NULL)
213 content_name = strdup("No Name");
215 err = notification_set_text(noti_handle,
216 NOTIFICATION_TEXT_TYPE_TITLE, content_name, NULL,
217 NOTIFICATION_VARIABLE_TYPE_NONE);
221 if (err != NOTIFICATION_ERROR_NONE) {
222 TRACE_ERROR("[FAIL] set title [%s]", __noti_error_str(err));
223 notification_free(noti_handle);
227 err = notification_set_image(noti_handle,
228 NOTIFICATION_IMAGE_TYPE_ICON, DP_NOTIFICATION_ICON_PATH);
229 if (err != NOTIFICATION_ERROR_NONE) {
230 TRACE_ERROR("[FAIL] set icon [%s]", __noti_error_str(err));
231 notification_free(noti_handle);
237 TRACE_ERROR("[FAIL] create bundle");
238 notification_free(noti_handle);
243 appsvc_set_pkgname(b, packagename) != APPSVC_RET_OK) {
244 TRACE_ERROR("[FAIL] set pkg name");
246 notification_free(noti_handle);
250 if (__set_extra_data(b, id) < 0) {
252 notification_free(noti_handle);
256 err = notification_set_execute_option(noti_handle,
257 NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
259 if (err != NOTIFICATION_ERROR_NONE) {
260 TRACE_ERROR("[FAIL] set execute option [%s]", __noti_error_str(err));
262 notification_free(noti_handle);
267 err = notification_set_property(noti_handle,
268 NOTIFICATION_PROP_DISABLE_TICKERNOTI);
269 if (err != NOTIFICATION_ERROR_NONE) {
270 TRACE_ERROR("[FAIL] set property [%s]", __noti_error_str(err));
271 notification_free(noti_handle);
275 err = notification_insert(noti_handle, &privId);
276 if (err != NOTIFICATION_ERROR_NONE) {
277 TRACE_ERROR("[FAIL] set insert [%s]", __noti_error_str(err));
278 notification_free(noti_handle);
282 TRACE_INFO("m_noti_id [%d]", privId);
283 notification_free(noti_handle);
287 int dp_set_downloadedinfo_notification(int priv_id, int id, char *packagename, dp_state_type state)
289 notification_h noti_handle = NULL;
290 notification_error_e err = NOTIFICATION_ERROR_NONE;
295 err = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_ONGOING,
297 if (err != NOTIFICATION_ERROR_NONE) {
298 TRACE_ERROR("[FAIL] delete notification handle [%s]",
299 __noti_error_str(err));
303 #ifdef NOTI_NEW_VERSION_API
304 noti_handle = notification_create(NOTIFICATION_TYPE_NOTI);
306 noti_handle = notification_new(NOTIFICATION_TYPE_NOTI,
307 NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
311 TRACE_ERROR("[FAIL] create notification handle");
316 dp_db_get_text_column
317 (id, DP_DB_TABLE_DOWNLOAD_INFO, DP_DB_COL_CONTENT_NAME);
319 if (content_name == NULL)
320 content_name = strdup("No Name");
322 err = notification_set_text(noti_handle,
323 NOTIFICATION_TEXT_TYPE_TITLE, content_name,
324 NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
328 if (err != NOTIFICATION_ERROR_NONE) {
329 TRACE_ERROR("[FAIL] set title [%s]", __noti_error_str(err));
330 notification_free(noti_handle);
334 err = notification_set_text(noti_handle,
335 NOTIFICATION_TEXT_TYPE_CONTENT,
336 __get_string_status(state), NULL,
337 NOTIFICATION_VARIABLE_TYPE_NONE);
338 if (err != NOTIFICATION_ERROR_NONE) {
339 TRACE_ERROR("[FAIL] set text [%s]", __noti_error_str(err));
340 notification_free(noti_handle);
343 time_t tt = time(NULL);
345 err = notification_set_time(noti_handle, tt);
346 if (err != NOTIFICATION_ERROR_NONE) {
347 TRACE_ERROR("[FAIL] set time [%s]", __noti_error_str(err));
348 notification_free(noti_handle);
354 TRACE_ERROR("[FAIL] create bundle");
355 notification_free(noti_handle);
359 if (state == DP_STATE_COMPLETED) {
360 if (appsvc_set_operation(b, APPSVC_OPERATION_VIEW) != APPSVC_RET_OK) {
361 TRACE_ERROR("[FAIL] appsvc set operation");
363 notification_free(noti_handle);
368 dp_db_get_text_column
369 (id, DP_DB_TABLE_DOWNLOAD_INFO, DP_DB_COL_SAVED_PATH);
370 if (savedpath && appsvc_set_uri(b, savedpath) !=
372 TRACE_ERROR("[FAIL] appsvc set uri");
375 notification_free(noti_handle);
380 err = notification_set_execute_option(noti_handle,
381 NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
382 if (err != NOTIFICATION_ERROR_NONE) {
383 TRACE_ERROR("[FAIL] set execute option[%s]", __noti_error_str(err));
385 notification_free(noti_handle);
389 } else if (state == DP_STATE_CANCELED || state == DP_STATE_FAILED) {
390 if (__set_extra_data(b, id) < 0) {
392 notification_free(noti_handle);
397 appsvc_set_pkgname(b, packagename) !=
399 TRACE_ERROR("[FAIL] set pkg name");
401 notification_free(noti_handle);
404 err = notification_set_execute_option(noti_handle,
405 NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
406 if (err != NOTIFICATION_ERROR_NONE) {
407 TRACE_ERROR("[FAIL] set execute option[%s]", __noti_error_str(err));
409 notification_free(noti_handle);
413 TRACE_ERROR("[CRITICAL] invalid state");
415 notification_free(noti_handle);
419 if (err != NOTIFICATION_ERROR_NONE) {
420 TRACE_ERROR("[FAIL] set time [%s]", __noti_error_str(err));
421 notification_free(noti_handle);
428 err = notification_set_image(noti_handle, NOTIFICATION_IMAGE_TYPE_ICON,
429 DP_NOTIFICATION_ICON_PATH);
430 if (err != NOTIFICATION_ERROR_NONE) {
431 TRACE_ERROR("[FAIL] set icon [%s]", __noti_error_str(err));
432 notification_free(noti_handle);
436 err = notification_set_property(noti_handle,
437 NOTIFICATION_PROP_DISABLE_TICKERNOTI);
438 if (err != NOTIFICATION_ERROR_NONE) {
439 TRACE_ERROR("[FAIL] set property [%s]", __noti_error_str(err));
440 notification_free(noti_handle);
444 err = notification_insert(noti_handle, &privId);
445 if (err != NOTIFICATION_ERROR_NONE) {
446 TRACE_ERROR("[FAIL] set insert [%s]", __noti_error_str(err));
447 notification_free(noti_handle);
451 TRACE_INFO("m_noti_id [%d]", privId);
452 notification_free(noti_handle);
456 void dp_update_downloadinginfo_notification(int priv_id, double received_size, double file_size)
458 notification_error_e err = NOTIFICATION_ERROR_NONE;
460 TRACE_ERROR("[FAIL] Invalid priv_id[%d]", priv_id);
466 progress = received_size / file_size;
467 err = notification_update_progress(NULL, priv_id, progress);
468 if (err != NOTIFICATION_ERROR_NONE)
469 TRACE_ERROR("[FAIL] update noti progress[%s]",
470 __noti_error_str(err));
472 err = notification_update_size(NULL, priv_id, received_size);
473 if (err != NOTIFICATION_ERROR_NONE)
474 TRACE_ERROR("[FAIL] update noti progress[%s]",
475 __noti_error_str(err));
479 void dp_clear_downloadinginfo_notification()
481 notification_error_e err = NOTIFICATION_ERROR_NONE;
482 err = notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_ONGOING);
483 if (err != NOTIFICATION_ERROR_NONE) {
484 TRACE_ERROR("[FAIL] clear noti [%s]", __noti_error_str(err));