2d6b69f695df7bc1f52d0951015317bca9faeb29
[platform/core/api/notification.git] / src / notification_internal.c
1 /*
2  * Copyright (c) 2000 - 2016 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 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <libintl.h>
23 #include <dbus/dbus.h>
24 #include <dbus/dbus-glib-lowlevel.h>
25
26 #include <app.h>
27 #include <app_control_internal.h>
28 #include <aul.h>
29 #include <appsvc.h>
30 #include <tizen.h>
31 #include <vconf-keys.h>
32 #include <vconf.h>
33
34 #include <notification.h>
35 #include <notification_list.h>
36 #include <notification_debug.h>
37 #include <notification_private.h>
38 #include <notification_noti.h>
39 #include <notification_ongoing.h>
40 #include <notification_group.h>
41 #include <notification_ipc.h>
42 #include <notification_internal.h>
43
44 typedef struct _notification_cb_list notification_cb_list_s;
45
46 typedef enum __notification_cb_type {
47         NOTIFICATION_CB_NORMAL = 1,
48         NOTIFICATION_CB_DETAILED,
49 } _notification_cb_type_e;
50
51 struct _notification_cb_list {
52         notification_cb_list_s *prev;
53         notification_cb_list_s *next;
54
55         _notification_cb_type_e cb_type;
56         void (*changed_cb) (void *data, notification_type_e type);
57         void (*detailed_changed_cb) (void *data, notification_type_e type, notification_op *op_list, int num_op);
58         void *data;
59 };
60
61 static notification_cb_list_s *g_notification_cb_list = NULL;
62
63 void notification_call_changed_cb(notification_op *op_list, int op_num)
64 {
65         notification_cb_list_s *noti_cb_list = NULL;
66         notification_type_e type = 0;
67
68         if (g_notification_cb_list == NULL)
69                 return;
70
71         noti_cb_list = g_notification_cb_list;
72
73         while (noti_cb_list->prev != NULL)
74                 noti_cb_list = noti_cb_list->prev;
75
76
77         if (op_list == NULL) {
78                 NOTIFICATION_ERR("invalid data");
79                 return ;
80         }
81
82         notification_get_type(op_list->noti, &type);
83
84         while (noti_cb_list != NULL) {
85                 if (noti_cb_list->cb_type == NOTIFICATION_CB_NORMAL && noti_cb_list->changed_cb) {
86                         noti_cb_list->changed_cb(noti_cb_list->data,
87                                                  type);
88                 }
89                 if (noti_cb_list->cb_type == NOTIFICATION_CB_DETAILED && noti_cb_list->detailed_changed_cb) {
90                         noti_cb_list->detailed_changed_cb(noti_cb_list->data,
91                                                 type, op_list, op_num);
92                 }
93
94                 noti_cb_list = noti_cb_list->next;
95         }
96 }
97
98 EXPORT_API int notification_add_deferred_task(
99                 void (*deferred_task_cb)(void *data), void *user_data)
100 {
101         if (deferred_task_cb == NULL)
102                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
103
104         return notification_ipc_add_deffered_task(deferred_task_cb, user_data);
105 }
106
107 EXPORT_API int notification_del_deferred_task(
108                 void (*deferred_task_cb)(void *data))
109 {
110         if (deferred_task_cb == NULL)
111                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
112
113         return notification_ipc_del_deffered_task(deferred_task_cb);
114 }
115
116 EXPORT_API int notification_resister_changed_cb(void (*changed_cb)
117                                  (void *data, notification_type_e type),
118                                  void *user_data)
119 {
120         notification_cb_list_s *noti_cb_list_new = NULL;
121         notification_cb_list_s *noti_cb_list = NULL;
122
123         if (changed_cb == NULL)
124                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
125
126         noti_cb_list_new =
127             (notification_cb_list_s *) malloc(sizeof(notification_cb_list_s));
128
129         if (noti_cb_list_new == NULL) {
130                 NOTIFICATION_ERR("malloc failed");
131                 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
132         }
133
134         noti_cb_list_new->next = NULL;
135         noti_cb_list_new->prev = NULL;
136
137         noti_cb_list_new->cb_type = NOTIFICATION_CB_NORMAL;
138         noti_cb_list_new->changed_cb = changed_cb;
139         noti_cb_list_new->detailed_changed_cb = NULL;
140         noti_cb_list_new->data = user_data;
141
142         if (g_notification_cb_list == NULL) {
143                 g_notification_cb_list = noti_cb_list_new;
144         } else {
145                 noti_cb_list = g_notification_cb_list;
146
147                 while (noti_cb_list->next != NULL)
148                         noti_cb_list = noti_cb_list->next;
149
150
151                 noti_cb_list->next = noti_cb_list_new;
152                 noti_cb_list_new->prev = noti_cb_list;
153         }
154
155         if (notification_ipc_monitor_init() != NOTIFICATION_ERROR_NONE) {
156                 notification_unresister_changed_cb(changed_cb);
157                 return NOTIFICATION_ERROR_IO_ERROR;
158         }
159
160         return NOTIFICATION_ERROR_NONE;
161 }
162
163 EXPORT_API int notification_unresister_changed_cb(void (*changed_cb)
164                                    (void *data, notification_type_e type))
165 {
166         notification_cb_list_s *noti_cb_list = NULL;
167         notification_cb_list_s *noti_cb_list_prev = NULL;
168         notification_cb_list_s *noti_cb_list_next = NULL;
169
170         noti_cb_list = g_notification_cb_list;
171
172         if (changed_cb == NULL)
173                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
174
175         if (noti_cb_list == NULL)
176                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
177
178         while (noti_cb_list->prev != NULL)
179                 noti_cb_list = noti_cb_list->prev;
180
181
182         do {
183                 if (noti_cb_list->changed_cb == changed_cb) {
184                         noti_cb_list_prev = noti_cb_list->prev;
185                         noti_cb_list_next = noti_cb_list->next;
186
187                         if (noti_cb_list_prev == NULL)
188                                 g_notification_cb_list = noti_cb_list_next;
189                         else
190                                 noti_cb_list_prev->next = noti_cb_list_next;
191
192                         if (noti_cb_list_next == NULL) {
193                                 if (noti_cb_list_prev != NULL)
194                                         noti_cb_list_prev->next = NULL;
195
196                         } else {
197                                 noti_cb_list_next->prev = noti_cb_list_prev;
198                         }
199
200                         free(noti_cb_list);
201
202                         if (g_notification_cb_list == NULL)
203                                 notification_ipc_monitor_fini();
204
205                         return NOTIFICATION_ERROR_NONE;
206                 }
207                 noti_cb_list = noti_cb_list->next;
208         } while (noti_cb_list != NULL);
209
210         return NOTIFICATION_ERROR_INVALID_PARAMETER;
211 }
212
213 EXPORT_API int notification_update_progress(notification_h noti,
214                                                              int priv_id,
215                                                              double progress)
216 {
217         char *caller_pkgname = NULL;
218         int input_priv_id = 0;
219         int ret = 0;
220         double input_progress = 0.0;
221
222         if (priv_id <= NOTIFICATION_PRIV_ID_NONE) {
223                 if (noti == NULL)
224                         return NOTIFICATION_ERROR_INVALID_PARAMETER;
225                 else
226                         input_priv_id = noti->priv_id;
227
228         } else {
229                 input_priv_id = priv_id;
230         }
231
232         if (noti == NULL)
233                 caller_pkgname = notification_get_pkgname_by_pid();
234         else
235                 caller_pkgname = strdup(noti->caller_pkgname);
236
237         if (progress < 0.0)
238                 input_progress = 0.0;
239         else if (progress > 1.0)
240                 input_progress = 1.0;
241         else
242                 input_progress = progress;
243
244         ret = notification_ongoing_update_progress(caller_pkgname, input_priv_id,
245                                              input_progress);
246
247         if (caller_pkgname)
248                 free(caller_pkgname);
249
250         return ret;
251 }
252
253 EXPORT_API int notification_update_size(notification_h noti,
254                                                          int priv_id,
255                                                          double size)
256 {
257         char *caller_pkgname = NULL;
258         int input_priv_id = 0;
259         int ret = 0;
260         double input_size = 0.0;
261
262         if (priv_id <= NOTIFICATION_PRIV_ID_NONE) {
263                 if (noti == NULL)
264                         return NOTIFICATION_ERROR_INVALID_PARAMETER;
265                 else
266                         input_priv_id = noti->priv_id;
267         } else {
268                 input_priv_id = priv_id;
269         }
270
271         if (noti == NULL)
272                 caller_pkgname = notification_get_pkgname_by_pid();
273         else
274                 caller_pkgname = strdup(noti->caller_pkgname);
275
276         if (size < 0.0)
277                 input_size = 0.0;
278         else
279                 input_size = size;
280
281         ret = notification_ongoing_update_size(caller_pkgname, input_priv_id,
282                                          input_size);
283
284         if (caller_pkgname)
285                 free(caller_pkgname);
286
287         return ret;
288 }
289
290 EXPORT_API int notification_update_content(notification_h noti,
291                                                          int priv_id,
292                                                          const char *content)
293 {
294         char *caller_pkgname = NULL;
295         int input_priv_id = 0;
296         int ret = 0;
297
298         if (priv_id <= NOTIFICATION_PRIV_ID_NONE) {
299                 if (noti == NULL)
300                         return NOTIFICATION_ERROR_INVALID_PARAMETER;
301                 else
302                         input_priv_id = noti->priv_id;
303
304         } else {
305                 input_priv_id = priv_id;
306         }
307
308         if (noti == NULL)
309                 caller_pkgname = notification_get_pkgname_by_pid();
310         else
311                 caller_pkgname = strdup(noti->caller_pkgname);
312
313         ret = notification_ongoing_update_content(caller_pkgname, input_priv_id,
314                                          content);
315
316         if (caller_pkgname)
317                 free(caller_pkgname);
318
319         return ret;
320 }
321
322 /* notification_set_icon will be removed */
323 /* LCOV_EXCL_START */
324 EXPORT_API int notification_set_icon(notification_h noti,
325                                                       const char *icon_path)
326 {
327         int ret_err = NOTIFICATION_ERROR_NONE;
328
329         ret_err =
330             notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON,
331                                    icon_path);
332
333         return ret_err;
334 }
335 /* LCOV_EXCL_STOP */
336
337 /* notification_get_icon will be removed */
338 /* LCOV_EXCL_START */
339 EXPORT_API int notification_get_icon(notification_h noti,
340                                                       char **icon_path)
341 {
342         int ret_err = NOTIFICATION_ERROR_NONE;
343         char *ret_image_path = NULL;
344
345         ret_err =
346             notification_get_image(noti, NOTIFICATION_IMAGE_TYPE_ICON,
347                                    &ret_image_path);
348
349         if (ret_err == NOTIFICATION_ERROR_NONE && icon_path != NULL)
350                 *icon_path = ret_image_path;
351
352         return ret_err;
353 }
354 /* LCOV_EXCL_STOP */
355
356 EXPORT_API int notification_translate_localized_text(notification_h noti)
357 {
358         int noti_err = NOTIFICATION_ERROR_NONE;
359         char *ret_text = NULL;
360         char buf_key[32];
361         char *bundle_val = NULL;
362         char *new_text;
363         bundle *b;
364         notification_text_type_e type = NOTIFICATION_TEXT_TYPE_TITLE;
365
366         for (; type < NOTIFICATION_TEXT_TYPE_MAX; type++) {
367                 noti_err = notification_get_text(noti, type, &ret_text);
368                 if (noti_err == NOTIFICATION_ERROR_NONE && ret_text) {
369                         if (noti->b_text == NULL) {
370                                 noti->b_text = bundle_create();
371                                 if (noti->b_text == NULL)
372                                         return NOTIFICATION_ERROR_OUT_OF_MEMORY;
373                         }
374
375                         b = noti->b_text;
376
377                         new_text = strdup(ret_text);
378
379                         snprintf(buf_key, sizeof(buf_key), "%d", type);
380                         bundle_get_str(b, buf_key, &bundle_val);
381                         if (bundle_val != NULL)
382                                 bundle_del(b, buf_key);
383
384                         bundle_add_str(b, buf_key, new_text);
385                         free(new_text);
386                         new_text = NULL;
387
388                         noti->num_format_args = 0;
389                         bundle_val = NULL;
390                 }
391         }
392
393         if (noti->b_key) {
394                 bundle_free(noti->b_key);
395                 noti->b_key = NULL;
396         }
397
398         return noti_err;
399 }
400
401 /* LCOV_EXCL_START */
402 EXPORT_API int notification_set_title(notification_h noti,
403                                                        const char *title,
404                                                        const char *loc_title)
405 {
406         int noti_err = NOTIFICATION_ERROR_NONE;
407
408         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE,
409                                          title, loc_title,
410                                          NOTIFICATION_VARIABLE_TYPE_NONE);
411
412         return noti_err;
413 }
414 /* LCOV_EXCL_STOP */
415
416 /* LCOV_EXCL_START */
417 EXPORT_API int notification_get_title(notification_h noti,
418                                                        char **title,
419                                                        char **loc_title)
420 {
421         int noti_err = NOTIFICATION_ERROR_NONE;
422         char *ret_text = NULL;
423
424         noti_err =
425             notification_get_text(noti, NOTIFICATION_TEXT_TYPE_TITLE,
426                                   &ret_text);
427
428         if (title != NULL)
429                 *title = ret_text;
430
431         if (loc_title != NULL)
432                 *loc_title = NULL;
433
434         return noti_err;
435 }
436 /* LCOV_EXCL_STOP */
437
438 /* LCOV_EXCL_START */
439 EXPORT_API int notification_set_content(notification_h noti,
440                                                          const char *content,
441                                                          const char *loc_content)
442 {
443         int noti_err = NOTIFICATION_ERROR_NONE;
444
445         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT,
446                                          content, loc_content,
447                                          NOTIFICATION_VARIABLE_TYPE_NONE);
448
449         return noti_err;
450 }
451 /* LCOV_EXCL_STOP */
452
453 /* LCOV_EXCL_START */
454 EXPORT_API int notification_get_content(notification_h noti,
455                                                          char **content,
456                                                          char **loc_content)
457 {
458         int noti_err = NOTIFICATION_ERROR_NONE;
459         char *ret_text = NULL;
460
461         noti_err =
462             notification_get_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT,
463                                   &ret_text);
464
465         if (content != NULL)
466                 *content = ret_text;
467
468         if (loc_content != NULL)
469                 *loc_content = NULL;
470
471         return noti_err;
472
473 #if 0
474         ret =
475             vconf_get_bool
476             (VCONFKEY_SETAPPL_STATE_TICKER_NOTI_DISPLAY_CONTENT_BOOL, &boolval);
477
478         if (ret == -1 || boolval == 0) {
479                 if (content != NULL && noti->default_content != NULL)
480                         *content = noti->default_content;
481
482                 if (loc_content != NULL && noti->loc_default_content != NULL)
483                         *loc_content = noti->loc_default_content;
484         }
485 #endif
486 }
487 /* LCOV_EXCL_STOP */
488
489 /* LCOV_EXCL_START */
490 EXPORT_API int notification_set_application(notification_h noti,
491                                                              const char *pkgname)
492 {
493         if (noti == NULL || pkgname == NULL)
494                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
495
496         if (noti->launch_pkgname)
497                 free(noti->launch_pkgname);
498
499         noti->launch_pkgname = strdup(pkgname);
500
501         return NOTIFICATION_ERROR_NONE;
502 }
503 /* LCOV_EXCL_STOP */
504
505 /* LCOV_EXCL_START */
506 EXPORT_API int notification_get_application(notification_h noti,
507                                                              char **pkgname)
508 {
509         if (noti == NULL || pkgname == NULL)
510                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
511
512         if (noti->launch_pkgname)
513                 *pkgname = noti->launch_pkgname;
514         else
515                 *pkgname = noti->caller_pkgname;
516
517         return NOTIFICATION_ERROR_NONE;
518 }
519 /* LCOV_EXCL_STOP */
520
521 /* LCOV_EXCL_START */
522 EXPORT_API int notification_set_args(notification_h noti, bundle *args,
523                 bundle *group_args)
524 {
525         if (noti == NULL || args == NULL)
526                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
527
528         if (noti->args)
529                 bundle_free(noti->args);
530
531         noti->args = bundle_dup(args);
532
533         if (noti->group_args) {
534                 bundle_free(noti->group_args);
535                 noti->group_args = NULL;
536         }
537
538         if (group_args != NULL)
539                 noti->group_args = bundle_dup(group_args);
540
541         return NOTIFICATION_ERROR_NONE;
542 }
543 /* LCOV_EXCL_STOP */
544
545 /* LCOV_EXCL_START */
546 EXPORT_API int notification_get_args(notification_h noti,
547                                                       bundle **args,
548                                                       bundle **group_args)
549 {
550         if (noti == NULL || args == NULL)
551                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
552
553         if (noti->args)
554                 *args = noti->args;
555         else
556                 *args = NULL;
557
558         if (group_args != NULL && noti->group_args)
559                 *group_args = noti->group_args;
560
561         return NOTIFICATION_ERROR_NONE;
562 }
563 /* LCOV_EXCL_STOP */
564
565 /* LCOV_EXCL_START */
566 EXPORT_API int notification_get_grouping_list(notification_type_e type, int count,
567                                notification_list_h *list)
568 {
569         notification_list_h get_list = NULL;
570         int ret = 0;
571
572         if (list == NULL)
573                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
574
575         ret = notification_noti_get_grouping_list(type, count, &get_list);
576         if (ret != NOTIFICATION_ERROR_NONE)
577                 return ret;
578
579         *list = get_list;
580
581         return NOTIFICATION_ERROR_NONE;
582 }
583 /* LCOV_EXCL_STOP */
584
585 /* LCOV_EXCL_START */
586 EXPORT_API int notification_delete_group_by_group_id(const char *pkgname,
587                                                                       notification_type_e type,
588                                                                       int group_id)
589 {
590         int ret = 0;
591         char *caller_pkgname = NULL;
592
593         if (pkgname == NULL)
594                 caller_pkgname = notification_get_pkgname_by_pid();
595         else
596                 caller_pkgname = strdup(pkgname);
597
598         ret = notification_ipc_request_delete_multiple(type, caller_pkgname);
599
600         if (caller_pkgname)
601                 free(caller_pkgname);
602
603         return ret;
604 }
605 /* LCOV_EXCL_STOP */
606
607 /* LCOV_EXCL_START */
608 EXPORT_API int notification_delete_group_by_priv_id(const char *pkgname,
609                                                                      notification_type_e type,
610                                                                      int priv_id)
611 {
612         int ret = 0;
613         char *caller_pkgname = NULL;
614
615         if (pkgname == NULL)
616                 caller_pkgname = notification_get_pkgname_by_pid();
617         else
618                 caller_pkgname = strdup(pkgname);
619
620         ret = notification_ipc_request_delete_single(type, caller_pkgname, priv_id);
621
622         if (caller_pkgname)
623                 free(caller_pkgname);
624
625         return ret;
626 }
627 /* LCOV_EXCL_STOP */
628
629 /* LCOV_EXCL_START */
630 EXPORT_API int notification_get_count(notification_type_e type,
631                                                        const char *pkgname,
632                                                        int group_id,
633                                                        int priv_id, int *count)
634 {
635         int ret = 0;
636         char *caller_pkgname = NULL;
637
638         if (count == NULL)
639                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
640
641         if (pkgname == NULL)
642                 caller_pkgname = notification_get_pkgname_by_pid();
643         else
644                 caller_pkgname = strdup(pkgname);
645
646         ret = notification_ipc_request_get_count(
647                         type,
648                         caller_pkgname,
649                         group_id,
650                         priv_id,
651                         count);
652
653         if (caller_pkgname)
654                 free(caller_pkgname);
655
656         return ret;
657 }
658 /* LCOV_EXCL_STOP */
659
660 /* LCOV_EXCL_START */
661 EXPORT_API int notification_clear(notification_type_e type)
662 {
663         int ret = 0;
664
665         if (type <= NOTIFICATION_TYPE_NONE || type >= NOTIFICATION_TYPE_MAX)
666                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
667
668         ret = notification_ipc_request_delete_multiple(type, NULL);
669
670         return ret;
671 }
672 /* LCOV_EXCL_STOP */
673
674 /* LCOV_EXCL_START */
675 EXPORT_API int notification_op_get_data(notification_op *noti_op, notification_op_data_type_e type,
676                                                        void *data)
677 {
678         if (noti_op == NULL || data == NULL)
679                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
680
681         switch (type) {
682         case NOTIFICATION_OP_DATA_TYPE:
683                 *((int *)data) = noti_op->type;
684                 break;
685         case NOTIFICATION_OP_DATA_PRIV_ID:
686                 *((int *)data) = noti_op->priv_id;
687                 break;
688         case NOTIFICATION_OP_DATA_NOTI:
689                 *((notification_h *)data) = noti_op->noti;
690                 break;
691         case NOTIFICATION_OP_DATA_EXTRA_INFO_1:
692                 *((int *)data) = noti_op->extra_info_1;
693                 break;
694         case NOTIFICATION_OP_DATA_EXTRA_INFO_2:
695                 *((int *)data) = noti_op->extra_info_2;
696                 break;
697         default:
698                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
699                 break;
700         }
701
702         return NOTIFICATION_ERROR_NONE;
703 }
704 /* LCOV_EXCL_STOP */
705
706 /* LCOV_EXCL_START */
707 EXPORT_API int notification_set_pkgname(notification_h noti,
708                                                          const char *pkgname)
709 {
710         /* check noti and pkgname are valid data */
711         if (noti == NULL || pkgname == NULL)
712                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
713
714         /* Remove previous caller pkgname */
715         if (noti->caller_pkgname) {
716                 free(noti->caller_pkgname);
717                 noti->caller_pkgname = NULL;
718         }
719
720         noti->caller_pkgname = strdup(pkgname);
721
722         return NOTIFICATION_ERROR_NONE;
723 }
724 /* LCOV_EXCL_STOP */
725
726 /* LCOV_EXCL_START */
727 EXPORT_API int notification_delete_all_by_type(const char *pkgname,
728                                                                 notification_type_e type)
729 {
730         int ret = 0;
731         char *caller_pkgname = NULL;
732
733         if (type <= NOTIFICATION_TYPE_NONE || type >= NOTIFICATION_TYPE_MAX)
734                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
735
736         if (pkgname == NULL)
737                 caller_pkgname = notification_get_pkgname_by_pid();
738         else
739                 caller_pkgname = strdup(pkgname);
740
741         ret = notification_ipc_request_delete_multiple(type, caller_pkgname);
742
743         if (caller_pkgname)
744                 free(caller_pkgname);
745
746         return ret;
747 }
748 /* LCOV_EXCL_STOP */
749
750 /* LCOV_EXCL_START */
751 EXPORT_API int notification_delete_by_priv_id(const char *pkgname,
752                                                                notification_type_e type,
753                                                                int priv_id)
754 {
755         int ret = 0;
756         char *caller_pkgname = NULL;
757
758         if (priv_id <= NOTIFICATION_PRIV_ID_NONE)
759                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
760
761         if (pkgname == NULL)
762                 caller_pkgname = notification_get_pkgname_by_pid();
763         else
764                 caller_pkgname = strdup(pkgname);
765
766         ret = notification_ipc_request_delete_single(type, caller_pkgname, priv_id);
767
768         if (caller_pkgname)
769                 free(caller_pkgname);
770
771         return ret;
772 }
773 /* LCOV_EXCL_STOP */
774
775 /* LCOV_EXCL_START */
776 EXPORT_API int notification_set_execute_option(notification_h noti,
777                                                                 notification_execute_type_e type,
778                                                                 const char *text,
779                                                                 const char *key,
780                                                                 bundle *service_handle)
781 {
782         char buf_key[32] = { 0, };
783         char *ret_val = NULL;
784         bundle *b = NULL;
785
786         if (noti == NULL)
787                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
788
789         if (type <= NOTIFICATION_EXECUTE_TYPE_NONE
790             || type >= NOTIFICATION_EXECUTE_TYPE_MAX)
791                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
792
793         /* Create execute option bundle if does not exist */
794         if (noti->b_execute_option == NULL)
795                 noti->b_execute_option = bundle_create();
796
797         b = noti->b_execute_option;
798
799         /* Save text */
800         if (text != NULL) {
801                 /* Make text key */
802                 snprintf(buf_key, sizeof(buf_key), "text%d", type);
803
804                 /* Check text key exist */
805                 bundle_get_str(b, buf_key, &ret_val);
806                 if (ret_val != NULL)
807                         /* Remove previous data */
808                         bundle_del(b, buf_key);
809
810                 /* Add text data */
811                 bundle_add_str(b, buf_key, text);
812         }
813
814         /* Save key */
815         if (key != NULL) {
816                 /* Make key key */
817                 snprintf(buf_key, sizeof(buf_key), "key%d", type);
818
819                 /* Check key key exist */
820                 bundle_get_str(b, buf_key, &ret_val);
821                 if (ret_val != NULL)
822                         /* Remove previous data */
823                         bundle_del(b, buf_key);
824
825                 /* Add text data */
826                 bundle_add_str(b, buf_key, key);
827         }
828
829         switch ((int)type) {
830         case NOTIFICATION_EXECUTE_TYPE_RESPONDING:
831                 /* Remove previous data if exist */
832                 if (noti->b_service_responding != NULL) {
833                         bundle_free(noti->b_service_responding);
834                         noti->b_service_responding = NULL;
835                 }
836
837                 /* Save service handle */
838                 if (service_handle != NULL)
839                         noti->b_service_responding = bundle_dup(service_handle);
840
841                 break;
842         case NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH:
843                 /* Remove previous data if exist */
844                 if (noti->b_service_single_launch != NULL) {
845                         bundle_free(noti->b_service_single_launch);
846                         noti->b_service_single_launch = NULL;
847                 }
848
849                 /* Save service handle */
850                 if (service_handle != NULL)
851                         noti->b_service_single_launch =
852                                 bundle_dup(service_handle);
853
854                 break;
855         case NOTIFICATION_EXECUTE_TYPE_MULTI_LAUNCH:
856                 /* Remove previous data if exist */
857                 if (noti->b_service_multi_launch != NULL) {
858                         bundle_free(noti->b_service_multi_launch);
859                         noti->b_service_multi_launch = NULL;
860                 }
861
862                 /* Save service handle */
863                 if (service_handle != NULL)
864                         noti->b_service_multi_launch =
865                                 bundle_dup(service_handle);
866
867                 break;
868         }
869
870         return NOTIFICATION_ERROR_NONE;
871 }
872 /* LCOV_EXCL_STOP */
873
874 /* LCOV_EXCL_START */
875 EXPORT_API int notification_get_id(notification_h noti,
876                                                     int *group_id, int *priv_id)
877 {
878         /* check noti is valid data */
879         if (noti == NULL)
880                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
881
882         /* Check group_id is valid data */
883         if (group_id) {
884                 /* Set group id */
885                 if (noti->group_id < NOTIFICATION_GROUP_ID_NONE)
886                         *group_id = NOTIFICATION_GROUP_ID_NONE;
887                 else
888                         *group_id = noti->group_id;
889         }
890
891         /* Check priv_id is valid data */
892         if (priv_id)
893                 /* Set priv_id */
894                 *priv_id = noti->priv_id;
895
896         return NOTIFICATION_ERROR_NONE;
897 }
898 /* LCOV_EXCL_STOP */
899
900 /* LCOV_EXCL_START */
901 EXPORT_API notification_h notification_load(char *pkgname,
902                                                       int priv_id)
903 {
904         int ret = 0;
905         notification_h noti = NULL;
906
907         noti = (notification_h)calloc(1, sizeof(struct _notification));
908         if (noti == NULL) {
909                 NOTIFICATION_ERR("NO MEMORY : noti == NULL");
910                 return NULL;
911         }
912
913         ret = notification_ipc_request_load_noti_by_priv_id(noti, pkgname, priv_id);
914         if (ret != NOTIFICATION_ERROR_NONE) {
915                 notification_free(noti);
916                 return NULL;
917         }
918
919         return noti;
920 }
921 /* LCOV_EXCL_STOP */
922
923 EXPORT_API notification_h notification_new(notification_type_e type,
924                                            int group_id, int priv_id)
925 {
926         return notification_create(type); //LCOV_EXCL_LINE
927 }
928
929 static void _notification_get_text_domain(notification_h noti)
930 {
931 /*
932         if (noti->domain != NULL) {
933
934         }
935
936         if (noti->dir != NULL) {
937
938         }
939 */
940 }
941
942 /* LCOV_EXCL_START */
943 EXPORT_API int notification_get_execute_option(notification_h noti,
944                                                                 notification_execute_type_e type,
945                                                                 const char **text,
946                                                                 bundle **service_handle)
947 {
948         char buf_key[32] = { 0, };
949         char *ret_val = NULL;
950         char *get_str = NULL;
951         bundle *b = NULL;
952
953         if (noti == NULL)
954                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
955
956         if (type <= NOTIFICATION_EXECUTE_TYPE_NONE
957             || type >= NOTIFICATION_EXECUTE_TYPE_MAX)
958                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
959
960
961         switch (type) {
962         case NOTIFICATION_EXECUTE_TYPE_RESPONDING:
963                 b = noti->b_service_responding;
964                 break;
965         case NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH:
966                 b = noti->b_service_single_launch;
967                 break;
968         case NOTIFICATION_EXECUTE_TYPE_MULTI_LAUNCH:
969                 b = noti->b_service_multi_launch;
970                 break;
971         default:
972                 break;
973         }
974
975         if (b != NULL) {
976         /* Return text */
977                 if (text != NULL) {
978                         /*  Get text domain and dir */
979                         if (noti->domain == NULL || noti->dir == NULL)
980                                 _notification_get_text_domain(noti);
981
982                         /* Make key */
983                         snprintf(buf_key, sizeof(buf_key), "key%d", type);
984
985                         /* Check key key exist */
986                         bundle_get_str(b, buf_key, &ret_val);
987                         if (ret_val != NULL && noti->domain != NULL
988                             && noti->dir != NULL) {
989                                 /* Get application string */
990                                 bindtextdomain(noti->domain, noti->dir);
991
992                                 get_str = dgettext(noti->domain, ret_val);
993
994                                 *text = get_str;
995                         } else if (ret_val != NULL) {
996                                 /* Get system string */
997                                 get_str = dgettext("sys_string", ret_val);
998
999                                 *text = get_str;
1000                         } else {
1001                                 /* Get basic text */
1002                                 snprintf(buf_key, sizeof(buf_key), "text%d",
1003                                          type);
1004
1005                                 bundle_get_str(b, buf_key, &ret_val);
1006
1007                                 *text = ret_val;
1008                         }
1009                 }
1010         }
1011
1012         if (service_handle != NULL)
1013                 *service_handle = b;
1014
1015         return NOTIFICATION_ERROR_NONE;
1016 }
1017 /* LCOV_EXCL_STOP */
1018
1019 EXPORT_API int notification_insert(notification_h noti,
1020                                                     int *priv_id)
1021 {
1022         int ret = 0;
1023         int id = 0;
1024
1025         /* Check noti is vaild data */
1026         if (noti == NULL)
1027                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1028
1029         /* Check noti type is valid type */
1030         if (noti->type <= NOTIFICATION_TYPE_NONE
1031             || noti->type >= NOTIFICATION_TYPE_MAX)
1032                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1033
1034         /* Save insert time */
1035         noti->insert_time = time(NULL);
1036         ret = notification_ipc_request_insert(noti, &id);
1037         if (ret != NOTIFICATION_ERROR_NONE)
1038                 return ret;
1039
1040         noti->priv_id = id;
1041         NOTIFICATION_DBG("from master:%d", id);
1042
1043         /* If priv_id is valid data, set priv_id */
1044         if (priv_id != NULL)
1045                 *priv_id = noti->priv_id;
1046
1047         return NOTIFICATION_ERROR_NONE;
1048 }
1049
1050 EXPORT_API int notification_update_async(notification_h noti,
1051                 void (*result_cb)(int priv_id, int result, void *data), void *user_data)
1052 {
1053         int ret = 0;
1054
1055         if (noti == NULL)
1056                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1057
1058         /* Update insert time ? */
1059         noti->insert_time = time(NULL);
1060         ret = notification_ipc_request_update_async(noti, result_cb, user_data);
1061
1062         return ret;
1063 }
1064
1065 EXPORT_API int notification_register_detailed_changed_cb(
1066                 void (*detailed_changed_cb)(void *data, notification_type_e type, notification_op *op_list, int num_op),
1067                 void *user_data)
1068 {
1069         notification_cb_list_s *noti_cb_list_new = NULL;
1070         notification_cb_list_s *noti_cb_list = NULL;
1071
1072         if (detailed_changed_cb == NULL)
1073                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1074
1075         if (notification_ipc_monitor_init() != NOTIFICATION_ERROR_NONE)
1076                 return NOTIFICATION_ERROR_IO_ERROR;
1077
1078         noti_cb_list_new =
1079             (notification_cb_list_s *) malloc(sizeof(notification_cb_list_s));
1080
1081         if (noti_cb_list_new == NULL) {
1082                 NOTIFICATION_ERR("malloc failed");
1083                 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
1084         }
1085
1086         noti_cb_list_new->next = NULL;
1087         noti_cb_list_new->prev = NULL;
1088
1089         noti_cb_list_new->cb_type = NOTIFICATION_CB_DETAILED;
1090         noti_cb_list_new->changed_cb = NULL;
1091         noti_cb_list_new->detailed_changed_cb = detailed_changed_cb;
1092         noti_cb_list_new->data = user_data;
1093
1094         if (g_notification_cb_list == NULL) {
1095                 g_notification_cb_list = noti_cb_list_new;
1096         } else {
1097                 noti_cb_list = g_notification_cb_list;
1098
1099                 while (noti_cb_list->next != NULL)
1100                         noti_cb_list = noti_cb_list->next;
1101
1102
1103                 noti_cb_list->next = noti_cb_list_new;
1104                 noti_cb_list_new->prev = noti_cb_list;
1105         }
1106         return NOTIFICATION_ERROR_NONE;
1107 }
1108
1109 EXPORT_API int notification_unregister_detailed_changed_cb(
1110                 void (*detailed_changed_cb)(void *data, notification_type_e type, notification_op *op_list, int num_op),
1111                 void *user_data)
1112 {
1113         notification_cb_list_s *noti_cb_list = NULL;
1114         notification_cb_list_s *noti_cb_list_prev = NULL;
1115         notification_cb_list_s *noti_cb_list_next = NULL;
1116
1117         noti_cb_list = g_notification_cb_list;
1118
1119         if (detailed_changed_cb == NULL)
1120                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1121
1122         if (noti_cb_list == NULL)
1123                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1124
1125
1126         while (noti_cb_list->prev != NULL)
1127                 noti_cb_list = noti_cb_list->prev;
1128
1129
1130         do {
1131                 if (noti_cb_list->detailed_changed_cb == detailed_changed_cb) {
1132                         noti_cb_list_prev = noti_cb_list->prev;
1133                         noti_cb_list_next = noti_cb_list->next;
1134
1135                         if (noti_cb_list_prev == NULL)
1136                                 g_notification_cb_list = noti_cb_list_next;
1137                         else
1138                                 noti_cb_list_prev->next = noti_cb_list_next;
1139
1140                         if (noti_cb_list_next == NULL) {
1141                                 if (noti_cb_list_prev != NULL)
1142                                         noti_cb_list_prev->next = NULL;
1143
1144                         } else {
1145                                 noti_cb_list_next->prev = noti_cb_list_prev;
1146                         }
1147
1148                         free(noti_cb_list);
1149
1150                         if (g_notification_cb_list == NULL)
1151                                 notification_ipc_monitor_fini();
1152
1153                         return NOTIFICATION_ERROR_NONE;
1154                 }
1155                 noti_cb_list = noti_cb_list->next;
1156         } while (noti_cb_list != NULL);
1157
1158         return NOTIFICATION_ERROR_INVALID_PARAMETER;
1159 }
1160
1161 EXPORT_API int notification_is_service_ready(void)
1162 {
1163         return notification_ipc_is_master_ready();
1164 }
1165