4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <notification.h>
22 #include <notification_list.h>
23 #include <notification_text_domain.h>
24 #include <notification_internal.h>
28 #include "bt-share-common.h"
29 #include "bt-share-notification.h"
30 #include "bt-share-main.h"
31 #include "bt-share-ipc.h"
32 #include "obex-event-handler.h"
33 #include "bluetooth-share-api.h"
34 #include "bt-share-resource.h"
36 #define BT_PRIV_ID_STR_LEN 12
38 extern GSList *bt_rcv_noti_info_list;
39 extern GSList *bt_snd_noti_info_list;
41 void _bt_insert_instant_notification(bt_notification_type_e type)
44 notification_h noti = NULL;
45 notification_error_e ret = NOTIFICATION_ERROR_NONE;
47 char *icon_path = NULL;
49 DBG("Create notification type : %d", type);
52 case BT_SENDING_NOTI: {
53 title = BT_STR_SENDING_FILES;
54 icon_path = BT_ICON_NOTIFICATION_SENT_INDICATOR;
58 case BT_RECEIVING_NOTI: {
59 title = BT_STR_RECEIVING_FILES;
60 icon_path = BT_ICON_NOTIFICATION_RECEIVED_INDICATOR;
65 title = BT_STR_FILES_SENT;
66 icon_path = BT_ICON_NOTIFICATION_SENT_INDICATOR;
70 case BT_SENT_FAILED_NOTI: {
71 title = BT_STR_SENDING_FILES_FAILED;
72 icon_path = BT_ICON_NOTIFICATION_SENT_INDICATOR;
76 case BT_RECEIVED_NOTI: {
77 title = BT_STR_FILES_RECEIVED;
78 icon_path = BT_ICON_NOTIFICATION_RECEIVED_INDICATOR;
82 case BT_RECEIVED_FAILED_NOTI: {
83 title = BT_STR_RECEIVING_FILES_FAILED;
84 icon_path = BT_ICON_NOTIFICATION_RECEIVED_INDICATOR;
92 noti = notification_create(NOTIFICATION_TYPE_NOTI);
93 retm_if(!noti, "Fail to notification_create");
96 ret = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE,
98 NOTIFICATION_VARIABLE_TYPE_NONE);
99 warn_if(ret != NOTIFICATION_ERROR_NONE,
100 "Fail to notification_set_text [%d]", ret);
104 ret = notification_set_image(noti,
105 NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, icon_path);
106 warn_if(ret != NOTIFICATION_ERROR_NONE,
107 "Fail to notification_set_image [%d]", ret);
110 ret = notification_set_display_applist(noti,
111 NOTIFICATION_DISPLAY_APP_TICKER);
112 warn_if(ret != NOTIFICATION_ERROR_NONE,
113 "Fail to notification_set_display_applist [%d]", ret);
115 ret = notification_post(noti);
116 warn_if(ret != NOTIFICATION_ERROR_NONE,
117 "Fail to notification_insert [%d]", ret);
120 notification_free(noti);
124 notification_h _bt_insert_notification(bt_appdata_t *ad,
125 bt_notification_type_e type, char *dev_name,
126 char *address, char *db_sid)
128 notification_h noti = NULL;
129 notification_error_e ret = NOTIFICATION_ERROR_NONE;
130 notification_type_e noti_type = NOTIFICATION_TYPE_NONE;
132 char *content = NULL;
133 char *icon_path = NULL;
134 char *indicator_icon_path = NULL;
140 bool include_name_param = false;
142 DBG("Create notification type : %d", type);
144 if (type == BT_SENT_NOTI) {
145 success = ad->send_data.tr_success;
146 fail = ad->send_data.tr_fail;
149 title = "IDS_BT_MBODY_SENDING_FAILED";
151 title = "IDS_WIFI_BODY_FILES_SENT_TO_PS_M_STATUS";
152 include_name_param = true;
155 if (success == 0 && fail > 0)
156 content = "IDS_BT_SBODY_PD_FAILED_ABB";
157 else if (fail == 0 && success > 0)
158 content = "IDS_BT_SBODY_PD_SUCCEEDED_ABB";
160 content = "IDS_BT_SBODY_P1SD_SUCCEEDED_P2SD_FAILED_ABB2";
162 icon_path = BT_ICON_NOTIFICATION_SENT;
163 indicator_icon_path = BT_ICON_NOTIFICATION_SENT_INDICATOR;
164 flag = NOTIFICATION_PROP_DISABLE_AUTO_DELETE;
165 app_id = NOTI_OPC_APP_ID;
166 noti_type = NOTIFICATION_TYPE_NOTI;
167 } else if (type == BT_RECEIVED_NOTI) {
168 success = ad->recv_data.tr_success;
169 fail = ad->recv_data.tr_fail;
171 if (success == 0 && fail > 0) {
172 content = "IDS_BT_SBODY_PD_FAILED_ABB";
173 title = "IDS_BT_MBODY_RECEIVING_FAILED";
174 } else if (fail == 0 && success > 0) {
175 title = "IDS_WIFI_MBODY_FILES_RECEIVED_FROM_PS";
176 content = "IDS_BT_SBODY_PD_SUCCEEDED_ABB";
177 include_name_param = true;
179 title = "IDS_WIFI_MBODY_FILES_RECEIVED_FROM_PS";
180 content = "IDS_BT_SBODY_P1SD_SUCCEEDED_P2SD_FAILED_ABB2";
181 include_name_param = true;
183 icon_path = BT_ICON_NOTIFICATION_RECEIVED;
184 indicator_icon_path = BT_ICON_NOTIFICATION_RECEIVED_INDICATOR;
185 flag = NOTIFICATION_PROP_DISABLE_AUTO_DELETE;
186 app_id = NOTI_OPS_APP_ID;
187 noti_type = NOTIFICATION_TYPE_NOTI;
188 } else if (type == BT_SENDING_NOTI) {
189 title = "IDS_BT_SBODY_PREPARING_TO_SEND_FILES_ING_ABB";
190 icon_path = BT_ICON_NOTIFICATION_SENDING;
191 indicator_icon_path = BT_ICON_NOTIFICATION_SENDING_INDICATOR;
192 flag = NOTIFICATION_PROP_DISABLE_TICKERNOTI;
193 noti_type = NOTIFICATION_TYPE_ONGOING;
194 } else if (type == BT_RECEIVING_NOTI) {
195 title = "IDS_BT_SBODY_PREPARING_TO_RECEIVE_FILES_ING_ABB";
196 icon_path = BT_ICON_NOTIFICATION_RECEIVING;
197 indicator_icon_path = BT_ICON_NOTIFICATION_RECEIVING_INDICATOR;
198 flag = NOTIFICATION_PROP_DISABLE_TICKERNOTI;
199 noti_type = NOTIFICATION_TYPE_ONGOING;
202 noti = notification_create(noti_type);
204 ERR("Fail to notification_create");
208 notification_set_text_domain(noti, BT_COMMON_PKG, BT_COMMON_RES);
211 if (include_name_param == true) {
212 ret = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE,
214 NOTIFICATION_VARIABLE_TYPE_STRING, dev_name,
215 NOTIFICATION_VARIABLE_TYPE_NONE);
217 ret = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE,
219 NOTIFICATION_VARIABLE_TYPE_NONE);
221 warn_if(ret != NOTIFICATION_ERROR_NONE,
222 "Fail to notification_set_text [%d]", ret);
226 DBG("success : %d, fail : %d", success, fail);
227 if (success > 0 && fail > 0) {
228 ret = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT,
230 NOTIFICATION_VARIABLE_TYPE_INT, success,
231 NOTIFICATION_VARIABLE_TYPE_INT, fail,
232 NOTIFICATION_VARIABLE_TYPE_NONE);
234 ret = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT,
236 NOTIFICATION_VARIABLE_TYPE_INT, (success > 0) ? success : fail,
237 NOTIFICATION_VARIABLE_TYPE_NONE);
240 warn_if(ret != NOTIFICATION_ERROR_NONE,
241 "Fail to notification_set_text [%d]", ret);
245 ret = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, icon_path);
246 warn_if(ret != NOTIFICATION_ERROR_NONE,
247 "Fail to notification_set_image [%d]", ret);
250 if (indicator_icon_path) {
251 ret = notification_set_image(noti,
252 NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, indicator_icon_path);
253 warn_if(ret != NOTIFICATION_ERROR_NONE,
254 "Fail to notification_set_image [%d]", ret);
258 ret = notification_set_property(noti, flag);
259 warn_if(ret != NOTIFICATION_ERROR_NONE,
260 "Fail to notification_set_property [%d]", ret);
262 if (type == BT_SENDING_NOTI || type == BT_RECEIVING_NOTI) {
263 ret = notification_set_property(noti,
264 NOTIFICATION_PROP_DISABLE_APP_LAUNCH);
265 warn_if(ret != NOTIFICATION_ERROR_NONE,
266 "Fail to set property for DISABLE_APP_LAUNCH [%d]", ret);
271 ret = notification_set_pkgname(noti, app_id);
272 warn_if(ret != NOTIFICATION_ERROR_NONE,
273 "Fail to notification_set_pkgname [%d]", ret);
276 ret = notification_set_display_applist(noti,
277 NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY | NOTIFICATION_DISPLAY_APP_INDICATOR);
278 warn_if(ret != NOTIFICATION_ERROR_NONE,
279 "Fail to notification_set_display_applist [%d]", ret);
281 if (type == BT_SENT_NOTI) {
282 _bt_set_notification_app_launch(noti, dev_name, address, db_sid,
283 CREATE_TR_LIST, NOTI_TR_TYPE_OUT, NULL, 0, NULL, 0);
284 } else if (type == BT_RECEIVED_NOTI) {
285 _bt_set_notification_app_launch(noti, dev_name, address, db_sid,
286 CREATE_TR_LIST, NOTI_TR_TYPE_IN, NULL, 0, NULL, 0);
289 ret = notification_insert(noti, ¬i_id);
290 warn_if(ret != NOTIFICATION_ERROR_NONE,
291 "Fail to notification_insert [%d]", ret);
293 if (type == BT_SENT_NOTI)
294 ad->send_noti_id = noti_id;
295 else if (type == BT_RECEIVED_NOTI)
296 ad->receive_noti_id = noti_id;
298 INFO("Insert %s type: %d ", (type == BT_SENT_NOTI ||
299 type == BT_RECEIVED_NOTI) ? "Notification" : "Ongoing", type);
304 int _bt_update_notification(bt_appdata_t *ad,
305 notification_h noti, char *title, char *content, char *dev_name,
306 char *icon_path, char *opc_count)
308 retvm_if(!noti, BT_SHARE_FAIL, "noti is NULL");
310 INFO("Update noti : %p", noti);
311 notification_error_e ret = NOTIFICATION_ERROR_NONE;
316 if (ad->send_noti == noti) {
317 success = ad->send_data.tr_success;
318 fail = ad->send_data.tr_fail;
319 } else if (ad->receive_noti == noti) {
320 success = ad->recv_data.tr_success;
321 fail = ad->recv_data.tr_fail;
324 if (ad->send_noti == noti || ad->receive_noti == noti) {
325 if (success == 0 && fail > 0)
326 content = "IDS_BT_SBODY_PD_FAILED_ABB";
327 else if (fail == 0 && success > 0)
328 content = "IDS_BT_SBODY_PD_SUCCEEDED_ABB";
330 content = "IDS_BT_SBODY_P1SD_SUCCEEDED_P2SD_FAILED_ABB2";
334 ret = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE,
336 NOTIFICATION_VARIABLE_TYPE_NONE);
337 warn_if(ret != NOTIFICATION_ERROR_NONE,
338 "Fail to notification_set_text [%d]", ret);
342 if (noti == ad->send_noti || noti == ad->receive_noti) {
343 if (success > 0 && fail > 0) {
344 ret = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT,
346 NOTIFICATION_VARIABLE_TYPE_INT, success,
347 NOTIFICATION_VARIABLE_TYPE_INT, fail,
348 NOTIFICATION_VARIABLE_TYPE_NONE);
350 ret = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT,
352 NOTIFICATION_VARIABLE_TYPE_INT, (success > 0) ? success : fail,
353 NOTIFICATION_VARIABLE_TYPE_NONE);
357 ret = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT,
359 NOTIFICATION_VARIABLE_TYPE_STRING, dev_name,
360 NOTIFICATION_VARIABLE_TYPE_NONE);
362 ret = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT,
363 NULL, content, NOTIFICATION_VARIABLE_TYPE_NONE);
367 warn_if(ret != NOTIFICATION_ERROR_NONE,
368 "Fail to notification_set_text [%d]", ret);
372 ret = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_EVENT_COUNT,
373 NULL, opc_count, NOTIFICATION_VARIABLE_TYPE_NONE);
374 warn_if(ret != NOTIFICATION_ERROR_NONE,
375 "Fail to notification_set_text [%d]", ret);
379 ret = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON,
381 warn_if(ret != NOTIFICATION_ERROR_NONE,
382 "Fail to notification_set_image [%d]", ret);
385 ret = notification_get_property(noti, &flags);
386 if (ret == NOTIFICATION_ERROR_NONE) {
387 if (flags & NOTIFICATION_PROP_DISABLE_APP_LAUNCH) {
388 ret = notification_set_property(noti,
389 flags ^ NOTIFICATION_PROP_DISABLE_APP_LAUNCH);
390 warn_if(ret != NOTIFICATION_ERROR_NONE,
391 "Failed to set app launch property");
394 ERR("failed to get the property: [%d]", ret);
397 ret = notification_update(noti);
398 warn_if(ret != NOTIFICATION_ERROR_NONE,
399 "Fail to notification_update [%d]", ret);
404 int _bt_update_notification_progress(notification_h not,
407 notification_error_e ret = NOTIFICATION_ERROR_NONE;
409 ret = notification_update_progress(not, id, (double)val / 100);
410 warn_if(ret != NOTIFICATION_ERROR_NONE,
411 "Fail to notification_update_progress [%d]\n", ret);
416 int _bt_get_notification_priv_id(notification_h noti)
420 notification_error_e ret = NOTIFICATION_ERROR_NONE;
422 ret = notification_get_id(noti, &group_id, &priv_id);
423 warn_if(ret != NOTIFICATION_ERROR_NONE,
424 "Fail to notification_get_id [%d]", ret);
429 int _bt_delete_notification(notification_h noti)
431 retv_if(!noti, BT_SHARE_FAIL);
432 notification_error_e ret = NOTIFICATION_ERROR_NONE;
434 INFO("Delete noti : %p", noti);
436 /* In case daemon, give full path */
437 ret = notification_delete(noti);
438 warn_if(ret != NOTIFICATION_ERROR_NONE,
439 "Fail to notification_delete [%d]", ret);
444 int _bt_set_notification_app_launch(notification_h noti,
448 bt_notification_launch_type_e launch_type,
449 const char *transfer_type,
450 const char *filename,
452 const char *progress_cnt,
456 retv_if(!noti, BT_SHARE_FAIL);
457 retv_if(!transfer_type, BT_SHARE_FAIL);
458 retv_if(launch_type != CREATE_PROGRESS && launch_type != CREATE_TR_LIST,
461 notification_error_e ret = NOTIFICATION_ERROR_NONE;
464 retv_if(!b, BT_SHARE_FAIL);
466 appsvc_set_pkgname(b, UI_PACKAGE);
467 appsvc_add_data(b, "device_name", device_name);
468 appsvc_add_data(b, "device_addr", device_addr);
469 appsvc_add_data(b, "transfer_type", transfer_type);
470 appsvc_add_data(b, "db_sid", db_sid);
472 if (launch_type == CREATE_PROGRESS) {
475 return BT_SHARE_FAIL;
478 char priv_id_str[BT_PRIV_ID_STR_LEN] = {0, };
479 char size_str[BT_PRIV_ID_STR_LEN] = {0, };
480 snprintf(priv_id_str, BT_PRIV_ID_STR_LEN, "%d", transfer_id);
481 snprintf(size_str, BT_PRIV_ID_STR_LEN, "%lu", size);
483 appsvc_add_data(b, "launch-type", "ongoing");
484 appsvc_add_data(b, "filename", filename);
485 appsvc_add_data(b, "size", size_str);
486 appsvc_add_data(b, "transfer_id", priv_id_str);
487 if (g_strcmp0(transfer_type, NOTI_TR_TYPE_OUT) == 0) {
490 return BT_SHARE_FAIL;
492 appsvc_add_data(b, "progress_cnt", progress_cnt);
495 char priv_id_str[BT_PRIV_ID_STR_LEN] = {0, };
496 snprintf(priv_id_str, BT_PRIV_ID_STR_LEN, "%d", transfer_id);
498 appsvc_add_data(b, "launch-type", "transfer_list");
499 appsvc_add_data(b, "transfer_id", priv_id_str);
500 appsvc_add_data(b, "filename", filename);
503 ret = notification_set_execute_option(noti,
504 NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH,
506 warn_if(ret != NOTIFICATION_ERROR_NONE,
507 "Fail to notification_set_execute_option [%d]\n", ret);
515 gboolean _bt_update_notification_status(bt_appdata_t *ad)
517 retv_if(ad == NULL, FALSE);
519 notification_h noti = NULL;
520 notification_list_h list_head = NULL;
521 notification_list_h list_traverse = NULL;
522 gboolean is_noti_existed = FALSE;
523 gboolean send_noti = FALSE;
524 gboolean receive_noti = FALSE;
531 /* When bt-share is launched, need to update notification status */
532 notification_get_list(NOTIFICATION_TYPE_NOTI, -1, &list_head);
533 list_traverse = list_head;
535 while (list_traverse != NULL) {
536 noti = notification_list_get_data(list_traverse);
537 notification_get_pkgname(noti, &app_id);
539 if (g_strcmp0(app_id, NOTI_OPS_APP_ID) == 0) {
540 ret = notification_clone(noti, &ad->receive_noti);
541 warn_if(ret != NOTIFICATION_ERROR_NONE,
542 "Fail to clone notificatoin");
544 ret = notification_get_id(noti, &group_id, &priv_id);
545 warn_if(ret != NOTIFICATION_ERROR_NONE,
546 "Fail to get notification id");
548 ad->receive_noti_id = priv_id;
550 } else if (g_strcmp0(app_id, NOTI_OPC_APP_ID) == 0) {
551 ret = notification_clone(noti, &ad->send_noti);
552 warn_if(ret != NOTIFICATION_ERROR_NONE,
553 "Unable to clone notificatoin");
555 ret = notification_get_id(noti, &group_id, &priv_id);
556 warn_if(ret != NOTIFICATION_ERROR_NONE,
557 "Fail to get notification id");
559 ad->send_noti_id = priv_id;
563 if (receive_noti || send_noti) {
565 /* Set notication insert_time */
566 notification_get_insert_time(noti, &ret_time);
567 notification_set_time(noti, ret_time);
568 notification_update(noti);
569 is_noti_existed = TRUE;
571 receive_noti = FALSE;
574 list_traverse = notification_list_get_next(list_traverse);
577 if (list_head != NULL) {
578 notification_free_list(list_head);
582 /* If notification item is removed durning BT off, need to remove DB */
583 db = bt_share_open_db();
584 retv_if(!db, is_noti_existed);
587 bt_share_remove_all_tr_data(db, BT_DB_INBOUND);
589 bt_share_remove_all_tr_data(db, BT_DB_OUTBOUND);
590 bt_share_close_db(db);
592 return is_noti_existed;
596 /* LCOV_EXCL_START */
597 static void __bt_notification_changed_cb(void *data, notification_type_e type,
598 notification_op *op_list, int num_op)
601 retm_if(data == NULL, "Invalid data");
602 retm_if(op_list == NULL, "Invalid op_list");
604 bt_appdata_t *ad = (bt_appdata_t *)data;
608 ret_if(type != NOTIFICATION_TYPE_NOTI ||
609 (op_list->type != NOTIFICATION_OP_DELETE &&
610 op_list->type != NOTIFICATION_OP_DELETE_ALL));
612 DBG("Notifications Changed: %d", num_op);
613 DBG("Currently Received NotiID: %d", ad->receive_noti_id);
614 db = bt_share_open_db();
615 for (i = 0; i < num_op; i++, op_list++) {
616 DBG("TYPE:%d PRIV_ID:%d", op_list->type, op_list->priv_id);
618 if (op_list->priv_id == ad->receive_noti_id) {
619 ad->receive_noti = NULL;
620 ad->receive_noti_id = 0;
623 if (bt_share_check_noti_id(db, BT_DB_OUTBOUND, op_list->priv_id)
624 == BT_SHARE_ERR_NONE) {
625 DBG("Delete Entries from OUTBOUND for NotiID %d", op_list->priv_id);
626 ad->send_noti = NULL;
627 ad->send_noti_id = 0;
628 ad->send_data.tr_fail = 0;
629 ad->send_data.tr_success = 0;
630 _bt_remove_temporary_files_by_noti_id(db, op_list->priv_id);
631 bt_share_remove_tr_data_by_notification(db,
632 BT_DB_OUTBOUND, op_list->priv_id);
634 if (bt_share_check_noti_id(db, BT_DB_INBOUND, op_list->priv_id)
635 == BT_SHARE_ERR_NONE) {
636 DBG("Delete Entries from INBOUND for NotiID %d", op_list->priv_id);
637 ad->receive_noti = NULL;
638 ad->receive_noti_id = 0;
639 ad->recv_data.tr_fail = 0;
640 ad->recv_data.tr_success = 0;
641 bt_share_remove_tr_data_by_notification(db,
642 BT_DB_INBOUND, op_list->priv_id);
645 bt_share_close_db(db);
647 DBG("SEND: Success[%d] Fail[%d] ||||| RECEIVE: Success[%d] Fail[%d]",
648 ad->send_data.tr_success, ad->send_data.tr_fail,
649 ad->recv_data.tr_success, ad->recv_data.tr_fail);
655 void _bt_register_notification_cb(bt_appdata_t *ad)
657 notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
659 noti_err = notification_register_detailed_changed_cb(
660 __bt_notification_changed_cb, ad);
661 warn_if(noti_err != NOTIFICATION_ERROR_NONE,
662 "notification_resister_changed_cb failed [%d]\n", noti_err);
665 void _bt_unregister_notification_cb(bt_appdata_t *ad)
667 notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
669 noti_err = notification_unregister_detailed_changed_cb(
670 __bt_notification_changed_cb, NULL);
671 warn_if(noti_err != NOTIFICATION_ERROR_NONE,
672 "notification_unresister_changed_cb failed [%d]\n", noti_err);