3a7fff9ce6f845a7ed7acada2ac08893bd0d9022
[platform/core/api/notification.git] / notification / src / notification_internal.c
1 /*
2  * Copyright (c) 2000 - 2017 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 <gio/gio.h>
25
26 #include <aul.h>
27 #include <tizen.h>
28 #include <bundle.h>
29 #include <bundle_internal.h>
30 #include <app_control.h>
31 #include <app_control_internal.h>
32 #include <pkgmgr-info.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 #include <notification_shared_file.h>
44
45 #define REGULAR_UID_MIN 5000
46
47 typedef struct _notification_cb_info notification_cb_info_s;
48 typedef struct _notification_event_cb_info notification_event_cb_info_s;
49
50 typedef enum __notification_cb_type {
51         NOTIFICATION_CB_NORMAL = 1,
52         NOTIFICATION_CB_DETAILED,
53 } _notification_cb_type_e;
54
55 struct _notification_cb_info {
56         _notification_cb_type_e cb_type;
57         void (*changed_cb)(void *data, notification_type_e type);
58         void (*detailed_changed_cb)(void *data, notification_type_e type, notification_op *op_list, int num_op);
59         void *data;
60 };
61
62 struct _notification_event_cb_info {
63         int priv_id;
64         event_handler_cb cb;
65         void *userdata;
66 };
67
68 static GHashTable *_noti_cb_hash = NULL;
69 static GList *__noti_event_cb_list = NULL;
70
71 /* LCOV_EXCL_START */
72 void notification_reset_event_handler_list(void)
73 {
74         GList *iter;
75         notification_event_cb_info_s *info;
76
77         if (!__noti_event_cb_list)
78                 return;
79
80         iter = g_list_first(__noti_event_cb_list);
81         while (iter) {
82                 info = g_list_nth_data(iter, 0);
83                 if (info)
84                         notification_ipc_reset_event_handler(info->priv_id);
85                 iter = g_list_next(iter);
86         }
87 }
88
89 static void __free_changed_cb_info(gpointer data)
90 {
91         notification_cb_info_s *noti_cb_info = (notification_cb_info_s *)data;
92
93         if (noti_cb_info)
94                 free(noti_cb_info);
95 }
96 /* LCOV_EXCL_STOP */
97
98 /* LCOV_EXCL_START */
99 void notification_call_changed_cb_for_uid(notification_op *op_list, int op_num, uid_t uid)
100 {
101         notification_type_e type = NOTIFICATION_TYPE_NOTI;
102         GList *noti_cb_list;
103         notification_cb_info_s *noti_cb_info;
104
105         if (_noti_cb_hash == NULL)
106                 return;
107
108         noti_cb_list = (GList *)g_hash_table_lookup(_noti_cb_hash, GUINT_TO_POINTER(uid));
109         if (noti_cb_list == NULL)
110                 return;
111
112         if (op_list == NULL) {
113                 ERR("Invalid parameter");
114                 return;
115         }
116
117         noti_cb_list = g_list_first(noti_cb_list);
118         notification_get_type(op_list->noti, &type);
119
120         for (; noti_cb_list != NULL; noti_cb_list = noti_cb_list->next) {
121                 noti_cb_info = noti_cb_list->data;
122
123                 if (noti_cb_info->cb_type == NOTIFICATION_CB_NORMAL && noti_cb_info->changed_cb)
124                         noti_cb_info->changed_cb(noti_cb_info->data, type);
125
126                 if (noti_cb_info->cb_type == NOTIFICATION_CB_DETAILED && noti_cb_info->detailed_changed_cb) {
127                         noti_cb_info->detailed_changed_cb(noti_cb_info->data,
128                                         type, op_list, op_num);
129                 }
130         }
131 }
132
133 static gint __priv_id_compare(gconstpointer a, gconstpointer b)
134 {
135         const notification_event_cb_info_s *info = NULL;
136
137         if (!a)
138                 return -1;
139
140         info = (notification_event_cb_info_s *)a;
141
142         if (info->priv_id == GPOINTER_TO_INT(b))
143                 return 0;
144
145         return 1;
146 }
147
148 void notification_call_event_handler_cb(notification_h noti, int event_type)
149 {
150         int ret;
151         int priv_id;
152         GList *find_list;
153         notification_event_cb_info_s *info;
154
155         if (__noti_event_cb_list == NULL)
156                 return;
157
158         ret = notification_get_id(noti, NULL, &priv_id);
159         if (ret != NOTIFICATION_ERROR_NONE)
160                 return;
161
162         __noti_event_cb_list = g_list_first(__noti_event_cb_list);
163         find_list = g_list_find_custom(__noti_event_cb_list, GINT_TO_POINTER(priv_id),
164                                        (GCompareFunc)__priv_id_compare);
165         if (find_list == NULL)
166                 return;
167
168         info = g_list_nth_data(find_list, 0);
169         info->cb(noti, event_type, info->userdata);
170 }
171 /* LCOV_EXCL_STOP */
172
173 /* LCOV_EXCL_START */
174 void notification_delete_event_handler_cb(int priv_id)
175 {
176         GList *delete_list;
177         notification_event_cb_info_s *info;
178
179         if (__noti_event_cb_list == NULL)
180                 return;
181
182         __noti_event_cb_list = g_list_first(__noti_event_cb_list);
183         delete_list = g_list_find_custom(__noti_event_cb_list, GINT_TO_POINTER(priv_id),
184                                          (GCompareFunc)__priv_id_compare);
185
186         if (delete_list == NULL)
187                 return;
188
189         info = g_list_nth_data(delete_list, 0);
190         __noti_event_cb_list = g_list_remove(g_list_first(__noti_event_cb_list), info);
191
192         if (info)
193                 free(info);
194
195         if (__noti_event_cb_list == NULL)
196                 notification_ipc_event_monitor_fini();
197 }
198 /* LCOV_EXCL_STOP */
199
200 /* LCOV_EXCL_START */
201 EXPORT_API int notification_add_deferred_task(
202                 void (*deferred_task_cb)(void *data), void *user_data)
203 {
204         if (deferred_task_cb == NULL)
205                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
206
207         return notification_ipc_add_deffered_task(deferred_task_cb, user_data);
208 }
209
210 EXPORT_API int notification_del_deferred_task(
211                 void (*deferred_task_cb)(void *data))
212 {
213         if (deferred_task_cb == NULL)
214                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
215
216         return notification_ipc_del_deffered_task(deferred_task_cb);
217 }
218 /* LCOV_EXCL_STOP */
219
220 /* LCOV_EXCL_START */
221 EXPORT_API int notification_resister_changed_cb_for_uid(
222                 void (*changed_cb)(void *data, notification_type_e type),
223                 void *user_data, uid_t uid)
224 {
225         GList *noti_cb_list;
226         notification_cb_info_s *noti_cb_info_new;
227
228         if (changed_cb == NULL)
229                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
230
231         if (_noti_cb_hash == NULL)
232                 _noti_cb_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
233
234         noti_cb_info_new = (notification_cb_info_s *)malloc(sizeof(notification_cb_info_s));
235         if (noti_cb_info_new == NULL) {
236                 ERR("Failed to alloc memory");
237                 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
238         }
239
240         noti_cb_info_new->cb_type = NOTIFICATION_CB_NORMAL;
241         noti_cb_info_new->changed_cb = changed_cb;
242         noti_cb_info_new->detailed_changed_cb = NULL;
243         noti_cb_info_new->data = user_data;
244
245         noti_cb_list = g_hash_table_lookup(_noti_cb_hash, GUINT_TO_POINTER(uid));
246
247         if (noti_cb_list == NULL) {
248                 noti_cb_list = g_list_append(noti_cb_list, noti_cb_info_new);
249                 g_hash_table_insert(_noti_cb_hash, GUINT_TO_POINTER(uid), noti_cb_list);
250         } else {
251                 noti_cb_list = g_list_append(noti_cb_list, noti_cb_info_new);
252         }
253
254         if (notification_ipc_monitor_init(uid) != NOTIFICATION_ERROR_NONE) {
255                 notification_unresister_changed_cb_for_uid(changed_cb, uid);
256                 return NOTIFICATION_ERROR_IO_ERROR;
257         }
258
259         return NOTIFICATION_ERROR_NONE;
260 }
261 /* LCOV_EXCL_STOP */
262
263 /* LCOV_EXCL_START */
264 EXPORT_API int notification_resister_changed_cb(
265                 void (*changed_cb)(void *data, notification_type_e type),
266                 void *user_data)
267 {
268         return notification_resister_changed_cb_for_uid(changed_cb, user_data, getuid());
269 }
270
271 static gint _noti_changed_compare(gconstpointer a, gconstpointer b)
272 {
273         const struct _notification_cb_info *info = NULL;
274
275         if (!a)
276                 return -1;
277         info = (notification_cb_info_s *)a;
278
279         if (info->changed_cb == b)
280                 return 0;
281
282         return 1;
283 }
284
285 EXPORT_API int notification_unresister_changed_cb_for_uid(
286                 void (*changed_cb)(void *data, notification_type_e type), uid_t uid)
287 {
288         notification_cb_info_s *noti_cb_info;
289         GList *noti_cb_list;
290         GList *delete_cb_list;
291
292         if (changed_cb == NULL)
293                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
294
295         if (_noti_cb_hash == NULL)
296                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
297
298         noti_cb_list = (GList *)g_hash_table_lookup(_noti_cb_hash, GUINT_TO_POINTER(uid));
299         if (noti_cb_list == NULL)
300                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
301
302         noti_cb_list = g_list_first(noti_cb_list);
303         delete_cb_list = g_list_find_custom(noti_cb_list, (gconstpointer)changed_cb,
304                                             (GCompareFunc)_noti_changed_compare);
305         if (delete_cb_list) {
306                 noti_cb_info = g_list_nth_data(delete_cb_list, 0);
307                 noti_cb_list = g_list_delete_link(noti_cb_list, delete_cb_list);
308                 __free_changed_cb_info(noti_cb_info);
309
310                 if (noti_cb_list == NULL) {
311                         g_hash_table_steal(_noti_cb_hash, GUINT_TO_POINTER(uid));
312                 } else {
313                         noti_cb_list = g_list_first(noti_cb_list);
314                         g_hash_table_replace(_noti_cb_hash, GUINT_TO_POINTER(uid), noti_cb_list);
315                 }
316
317         } else {
318                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
319         }
320
321         if (g_hash_table_size(_noti_cb_hash) == 0)
322                 notification_ipc_monitor_fini();
323
324         return NOTIFICATION_ERROR_NONE;
325 }
326
327 EXPORT_API int notification_unresister_changed_cb(
328                 void (*changed_cb)(void *data, notification_type_e type))
329 {
330         return notification_unresister_changed_cb_for_uid(changed_cb, getuid());
331 }
332
333 EXPORT_API int notification_update_progress(notification_h noti,
334                 int priv_id,
335                 double progress)
336 {
337         int ret;
338         int input_priv_id;
339         char *caller_app_id;
340         double input_progress;
341
342         if (priv_id <= NOTIFICATION_PRIV_ID_NONE) {
343                 if (noti == NULL)
344                         return NOTIFICATION_ERROR_INVALID_PARAMETER;
345
346                 input_priv_id = noti->priv_id;
347         } else {
348                 input_priv_id = priv_id;
349         }
350
351         if (noti == NULL)
352                 caller_app_id = notification_get_app_id_by_pid(getpid());
353         else
354                 caller_app_id = strdup(noti->caller_app_id);
355
356         if (progress < 0.0)
357                 input_progress = 0.0;
358         else if (progress > 1.0)
359                 input_progress = 1.0;
360         else
361                 input_progress = progress;
362
363         ret = notification_ongoing_update_progress(caller_app_id, input_priv_id,
364                         input_progress);
365
366         if (caller_app_id)
367                 free(caller_app_id);
368
369         return ret;
370 }
371
372 EXPORT_API int notification_update_size(notification_h noti,
373                 int priv_id,
374                 double size)
375 {
376         int ret;
377         int input_priv_id;
378         char *caller_app_id;
379         double input_size;
380
381         if (priv_id <= NOTIFICATION_PRIV_ID_NONE) {
382                 if (noti == NULL)
383                         return NOTIFICATION_ERROR_INVALID_PARAMETER;
384
385                 input_priv_id = noti->priv_id;
386         } else {
387                 input_priv_id = priv_id;
388         }
389
390         if (noti == NULL)
391                 caller_app_id = notification_get_app_id_by_pid(getpid());
392         else
393                 caller_app_id = strdup(noti->caller_app_id);
394
395         if (size < 0.0)
396                 input_size = 0.0;
397         else
398                 input_size = size;
399
400         ret = notification_ongoing_update_size(caller_app_id, input_priv_id,
401                         input_size);
402
403         if (caller_app_id)
404                 free(caller_app_id);
405
406         return ret;
407 }
408
409 EXPORT_API int notification_update_content(notification_h noti,
410                 int priv_id,
411                 const char *content)
412 {
413         char *caller_app_id;
414         int input_priv_id;
415         int ret;
416
417         if (priv_id <= NOTIFICATION_PRIV_ID_NONE) {
418                 if (noti == NULL)
419                         return NOTIFICATION_ERROR_INVALID_PARAMETER;
420
421                 input_priv_id = noti->priv_id;
422         } else {
423                 input_priv_id = priv_id;
424         }
425
426         if (noti == NULL)
427                 caller_app_id = notification_get_app_id_by_pid(getpid());
428         else
429                 caller_app_id = strdup(noti->caller_app_id);
430
431         ret = notification_ongoing_update_content(caller_app_id, input_priv_id,
432                         content);
433
434         if (caller_app_id)
435                 free(caller_app_id);
436
437         return ret;
438 }
439 /* LCOV_EXCL_STOP */
440
441 /* notification_set_icon will be removed */
442 /* LCOV_EXCL_START */
443 EXPORT_API int notification_set_icon(notification_h noti,
444                 const char *icon_path)
445 {
446         return notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, icon_path);
447 }
448 /* LCOV_EXCL_STOP */
449
450 /* notification_get_icon will be removed */
451 /* LCOV_EXCL_START */
452 EXPORT_API int notification_get_icon(notification_h noti,
453                 char **icon_path)
454 {
455         int ret = NOTIFICATION_ERROR_NONE;
456         char *ret_image_path = NULL;
457
458         ret = notification_get_image(noti, NOTIFICATION_IMAGE_TYPE_ICON,
459                                 &ret_image_path);
460
461         if (ret == NOTIFICATION_ERROR_NONE && icon_path != NULL)
462                 *icon_path = ret_image_path;
463
464         return ret;
465 }
466 /* LCOV_EXCL_STOP */
467
468 /* LCOV_EXCL_START */
469 EXPORT_API int notification_translate_localized_text(notification_h noti)
470 {
471         int ret = NOTIFICATION_ERROR_NONE;
472         char *ret_text = NULL;
473         char buf_key[32];
474         char *bundle_val = NULL;
475         char *new_text;
476         bundle *b;
477         notification_text_type_e type = NOTIFICATION_TEXT_TYPE_TITLE;
478
479         noti->is_translation = false;
480
481         for (; type <= NOTIFICATION_TEXT_TYPE_MAX; type++) {
482                 ret = notification_get_text(noti, type, &ret_text);
483                 if (ret == NOTIFICATION_ERROR_NONE && ret_text) {
484                         if (noti->b_text == NULL) {
485                                 noti->b_text = bundle_create();
486                                 if (noti->b_text == NULL)
487                                         return NOTIFICATION_ERROR_OUT_OF_MEMORY;
488                         }
489
490                         b = noti->b_text;
491
492                         new_text = strdup(ret_text);
493
494                         snprintf(buf_key, sizeof(buf_key), "%d", type);
495                         bundle_get_str(b, buf_key, &bundle_val);
496                         if (bundle_val != NULL)
497                                 bundle_del(b, buf_key);
498
499                         bundle_add_str(b, buf_key, new_text);
500                         free(new_text);
501                         new_text = NULL;
502
503                         noti->num_format_args = 0;
504                         bundle_val = NULL;
505                 }
506         }
507
508         noti->is_translation = true;
509
510         return ret;
511 }
512 /* LCOV_EXCL_STOP */
513
514 /* LCOV_EXCL_START */
515 EXPORT_API int notification_set_title(notification_h noti,
516                 const char *title,
517                 const char *loc_title)
518 {
519         return notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE,
520                                 title, loc_title,
521                                 NOTIFICATION_VARIABLE_TYPE_NONE);
522 }
523 /* LCOV_EXCL_STOP */
524
525 /* LCOV_EXCL_START */
526 EXPORT_API int notification_get_title(notification_h noti,
527                 char **title,
528                 char **loc_title)
529 {
530         int ret;
531         char *ret_text = NULL;
532
533         ret = notification_get_text(noti, NOTIFICATION_TEXT_TYPE_TITLE,
534                                 &ret_text);
535
536         if (title != NULL)
537                 *title = ret_text;
538
539         if (loc_title != NULL)
540                 *loc_title = NULL;
541
542         return ret;
543 }
544 /* LCOV_EXCL_STOP */
545
546 /* LCOV_EXCL_START */
547 EXPORT_API int notification_set_content(notification_h noti,
548                 const char *content,
549                 const char *loc_content)
550 {
551         return notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT,
552                                 content, loc_content,
553                                 NOTIFICATION_VARIABLE_TYPE_NONE);
554 }
555 /* LCOV_EXCL_STOP */
556
557 /* LCOV_EXCL_START */
558 EXPORT_API int notification_get_content(notification_h noti,
559                 char **content,
560                 char **loc_content)
561 {
562         int ret;
563         char *ret_text = NULL;
564
565         ret = notification_get_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT,
566                                 &ret_text);
567
568         if (content != NULL)
569                 *content = ret_text;
570
571         if (loc_content != NULL)
572                 *loc_content = NULL;
573
574         return ret;
575 }
576 /* LCOV_EXCL_STOP */
577
578 /* LCOV_EXCL_START */
579 EXPORT_API int notification_set_application(notification_h noti,
580                 const char *app_id)
581 {
582         if (noti == NULL || app_id == NULL)
583                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
584
585         if (noti->launch_app_id)
586                 free(noti->launch_app_id);
587
588         noti->launch_app_id = strdup(app_id);
589
590         return NOTIFICATION_ERROR_NONE;
591 }
592 /* LCOV_EXCL_STOP */
593
594 /* LCOV_EXCL_START */
595 EXPORT_API int notification_get_application(notification_h noti,
596                 char **app_id)
597 {
598         if (noti == NULL || app_id == NULL)
599                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
600
601         if (noti->launch_app_id)
602                 *app_id = noti->launch_app_id;
603         else
604                 *app_id = noti->caller_app_id;
605
606         return NOTIFICATION_ERROR_NONE;
607 }
608 /* LCOV_EXCL_STOP */
609
610 /* LCOV_EXCL_START */
611 EXPORT_API int notification_set_args(notification_h noti, bundle *args,
612                 bundle *group_args)
613 {
614         if (noti == NULL || args == NULL)
615                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
616
617         if (noti->args)
618                 bundle_free(noti->args);
619
620         noti->args = bundle_dup(args);
621
622         if (noti->group_args) {
623                 bundle_free(noti->group_args);
624                 noti->group_args = NULL;
625         }
626
627         if (group_args != NULL)
628                 noti->group_args = bundle_dup(group_args);
629
630         return NOTIFICATION_ERROR_NONE;
631 }
632 /* LCOV_EXCL_STOP */
633
634 /* LCOV_EXCL_START */
635 EXPORT_API int notification_get_args(notification_h noti,
636                 bundle **args,
637                 bundle **group_args)
638 {
639         if (noti == NULL || args == NULL)
640                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
641
642         if (noti->args)
643                 *args = noti->args;
644         else
645                 *args = NULL;
646
647         if (group_args != NULL && noti->group_args)
648                 *group_args = noti->group_args;
649
650         return NOTIFICATION_ERROR_NONE;
651 }
652 /* LCOV_EXCL_STOP */
653
654 /* LCOV_EXCL_START */
655 int notification_get_grouping_list_for_uid(notification_type_e type, int count,
656                 notification_list_h *list, uid_t uid)
657 {
658         notification_list_h get_list = NULL;
659         int ret = 0;
660
661         if (list == NULL)
662                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
663
664         ret = notification_noti_get_grouping_list(type, 1, count, &get_list, NULL, uid);
665         if (ret != NOTIFICATION_ERROR_NONE)
666                 return ret;
667
668         *list = get_list;
669
670         return NOTIFICATION_ERROR_NONE;
671 }
672
673 EXPORT_API int notification_get_grouping_list(notification_type_e type, int count,
674                 notification_list_h *list)
675 {
676         return notification_get_grouping_list_for_uid(type, count, list, getuid());
677 }
678 /* LCOV_EXCL_STOP */
679
680 /* LCOV_EXCL_START */
681 EXPORT_API int notification_delete_group_by_group_id(const char *app_id,
682                 notification_type_e type,
683                 int group_id)
684 {
685         int ret;
686         char *caller_app_id;
687
688         if (app_id == NULL)
689                 caller_app_id = notification_get_app_id_by_pid(getpid());
690         else
691                 caller_app_id = strdup(app_id);
692
693         ret = notification_ipc_request_delete_multiple(type, caller_app_id, getuid());
694
695         if (caller_app_id)
696                 free(caller_app_id);
697
698         return ret;
699 }
700 /* LCOV_EXCL_STOP */
701
702 /* LCOV_EXCL_START */
703 int notification_delete_group_by_priv_id_for_uid(const char *app_id,
704                 notification_type_e type,
705                 int priv_id, uid_t uid)
706 {
707         int ret;
708         char *caller_app_id;
709
710         if (app_id == NULL)
711                 caller_app_id = notification_get_app_id_by_pid(getpid());
712         else
713                 caller_app_id = strdup(app_id);
714
715         ret = notification_ipc_request_delete_single(type, caller_app_id, priv_id, uid);
716
717         if (caller_app_id)
718                 free(caller_app_id);
719
720         return ret;
721 }
722 /* LCOV_EXCL_STOP */
723
724 /* LCOV_EXCL_START */
725 EXPORT_API int notification_delete_group_by_priv_id(const char *app_id,
726                 notification_type_e type,
727                 int priv_id)
728 {
729         return notification_delete_group_by_priv_id_for_uid(app_id, type, priv_id, getuid());
730 }
731
732 int notification_get_count_for_uid(notification_type_e type,
733                 const char *app_id,
734                 int group_id,
735                 int priv_id, int *count,
736                 uid_t uid)
737 {
738         int ret;
739         char *caller_app_id;
740
741         if (count == NULL)
742                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
743
744         if (app_id == NULL)
745                 caller_app_id = notification_get_app_id_by_pid(getpid());
746         else
747                 caller_app_id = strdup(app_id);
748
749         ret = notification_ipc_request_get_count(
750                         type,
751                         caller_app_id,
752                         group_id,
753                         priv_id,
754                         count,
755                         uid);
756
757         if (caller_app_id)
758                 free(caller_app_id);
759
760         return ret;
761 }
762 /* LCOV_EXCL_STOP */
763
764 /* LCOV_EXCL_START */
765 EXPORT_API int notification_get_count(notification_type_e type,
766                 const char *app_id,
767                 int group_id,
768                 int priv_id, int *count)
769 {
770         return notification_get_count_for_uid(type, app_id, group_id, priv_id, count, getuid());
771 }
772
773 int notification_clear_for_uid(notification_type_e type, uid_t uid)
774 {
775         if (type <= NOTIFICATION_TYPE_NONE || type > NOTIFICATION_TYPE_MAX)
776                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
777
778         return notification_ipc_request_delete_multiple(type, NULL, uid);
779 }
780 /* LCOV_EXCL_STOP */
781
782 /* LCOV_EXCL_START */
783 EXPORT_API int notification_clear(notification_type_e type)
784 {
785         return notification_clear_for_uid(type, getuid());
786 }
787
788 EXPORT_API int notification_op_get_data(notification_op *noti_op, notification_op_data_type_e type,
789                 void *data)
790 {
791         if (noti_op == NULL || data == NULL)
792                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
793
794         switch (type) {
795         case NOTIFICATION_OP_DATA_TYPE:
796                 *((int *)data) = noti_op->type;
797                 break;
798         case NOTIFICATION_OP_DATA_PRIV_ID:
799                 *((int *)data) = noti_op->priv_id;
800                 break;
801         case NOTIFICATION_OP_DATA_NOTI:
802                 *((notification_h *)data) = noti_op->noti;
803                 break;
804         case NOTIFICATION_OP_DATA_EXTRA_INFO_1:
805                 *((int *)data) = noti_op->extra_info_1;
806                 break;
807         case NOTIFICATION_OP_DATA_EXTRA_INFO_2:
808                 *((int *)data) = noti_op->extra_info_2;
809                 break;
810         default:
811                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
812         }
813
814         return NOTIFICATION_ERROR_NONE;
815 }
816 /* LCOV_EXCL_STOP */
817
818 /* LCOV_EXCL_START */
819 EXPORT_API int notification_set_pkgname(notification_h noti,
820                 const char *pkgname)
821 {
822         return notification_set_app_id(noti, pkgname);
823 }
824 /* LCOV_EXCL_STOP */
825
826 /* LCOV_EXCL_START */
827 EXPORT_API int notification_set_app_id(notification_h noti,
828                 const char *app_id)
829 {
830         if (noti == NULL || app_id == NULL)
831                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
832
833         /* Remove previous caller app_id */
834         if (noti->caller_app_id) {
835                 free(noti->caller_app_id);
836                 noti->caller_app_id = NULL;
837         }
838
839         noti->caller_app_id = strdup(app_id);
840
841         return NOTIFICATION_ERROR_NONE;
842 }
843 /* LCOV_EXCL_STOP */
844
845 /* LCOV_EXCL_START */
846 int notification_delete_all_by_type_for_uid(const char *app_id,
847                 notification_type_e type, uid_t uid)
848 {
849         int ret;
850         char *caller_app_id;
851
852         if (type <= NOTIFICATION_TYPE_NONE || type > NOTIFICATION_TYPE_MAX)
853                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
854
855         if (app_id == NULL)
856                 caller_app_id = notification_get_app_id_by_pid(getpid());
857         else
858                 caller_app_id = strdup(app_id);
859
860         ret = notification_ipc_request_delete_multiple(type, caller_app_id, uid);
861
862         if (caller_app_id)
863                 free(caller_app_id);
864
865         return ret;
866 }
867 /* LCOV_EXCL_STOP */
868
869 /* LCOV_EXCL_START */
870 EXPORT_API int notification_delete_all_by_type(const char *app_id,
871                 notification_type_e type)
872 {
873         return notification_delete_all_by_type_for_uid(app_id, type, getuid());
874 }
875
876 int notification_delete_by_priv_id_for_uid(const char *app_id,
877                 notification_type_e type,
878                 int priv_id,
879                 uid_t uid)
880 {
881         int ret;
882         char *caller_app_id;
883
884         if (priv_id <= NOTIFICATION_PRIV_ID_NONE)
885                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
886
887         if (app_id == NULL)
888                 caller_app_id = notification_get_app_id_by_pid(getpid());
889         else
890                 caller_app_id = strdup(app_id);
891
892         ret = notification_ipc_request_delete_single(type, caller_app_id, priv_id, uid);
893
894         if (caller_app_id)
895                 free(caller_app_id);
896
897         return ret;
898 }
899 /* LCOV_EXCL_STOP */
900
901 /* LCOV_EXCL_START */
902 EXPORT_API int notification_delete_by_priv_id(const char *app_id,
903                 notification_type_e type,
904                 int priv_id)
905 {
906         return notification_delete_by_priv_id_for_uid(app_id, type, priv_id, getuid());
907 }
908
909 EXPORT_API int notification_set_execute_option(notification_h noti,
910                 notification_execute_type_e type,
911                 const char *text,
912                 const char *key,
913                 bundle *service_handle)
914 {
915         char buf_key[32] = { 0, };
916         char *ret_val = NULL;
917         bundle *b = NULL;
918
919         if (noti == NULL)
920                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
921
922         if (type <= NOTIFICATION_EXECUTE_TYPE_NONE
923                         || type > NOTIFICATION_EXECUTE_TYPE_MAX)
924                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
925
926         if (noti->b_execute_option == NULL)
927                 noti->b_execute_option = bundle_create();
928
929         b = noti->b_execute_option;
930
931         if (text != NULL) {
932                 snprintf(buf_key, sizeof(buf_key), "text%d", type);
933
934                 bundle_get_str(b, buf_key, &ret_val);
935                 if (ret_val != NULL)
936                         bundle_del(b, buf_key);
937
938                 bundle_add_str(b, buf_key, text);
939         }
940
941         if (key != NULL) {
942                 snprintf(buf_key, sizeof(buf_key), "key%d", type);
943
944                 bundle_get_str(b, buf_key, &ret_val);
945                 if (ret_val != NULL)
946                         bundle_del(b, buf_key);
947
948                 bundle_add_str(b, buf_key, key);
949         }
950
951         switch ((int)type) {
952         case NOTIFICATION_EXECUTE_TYPE_RESPONDING:
953                 if (noti->b_service_responding != NULL) {
954                         bundle_free(noti->b_service_responding);
955                         noti->b_service_responding = NULL;
956                 }
957
958                 if (service_handle != NULL)
959                         noti->b_service_responding = bundle_dup(service_handle);
960
961                 break;
962         case NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH:
963                 if (noti->b_service_single_launch != NULL) {
964                         bundle_free(noti->b_service_single_launch);
965                         noti->b_service_single_launch = NULL;
966                 }
967
968                 if (service_handle != NULL)
969                         noti->b_service_single_launch =
970                                 bundle_dup(service_handle);
971
972                 break;
973         case NOTIFICATION_EXECUTE_TYPE_MULTI_LAUNCH:
974                 if (noti->b_service_multi_launch != NULL) {
975                         bundle_free(noti->b_service_multi_launch);
976                         noti->b_service_multi_launch = NULL;
977                 }
978
979                 if (service_handle != NULL)
980                         noti->b_service_multi_launch =
981                                 bundle_dup(service_handle);
982
983                 break;
984         }
985
986         return NOTIFICATION_ERROR_NONE;
987 }
988 /* LCOV_EXCL_STOP */
989
990 /* LCOV_EXCL_START */
991 EXPORT_API int notification_get_id(notification_h noti,
992                 int *group_id, int *priv_id)
993 {
994         if (noti == NULL)
995                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
996
997         if (group_id) {
998                 if (noti->group_id < NOTIFICATION_GROUP_ID_NONE)
999                         *group_id = NOTIFICATION_GROUP_ID_NONE;
1000                 else
1001                         *group_id = noti->group_id;
1002         }
1003
1004         if (priv_id)
1005                 *priv_id = noti->priv_id;
1006
1007         return NOTIFICATION_ERROR_NONE;
1008 }
1009 /* LCOV_EXCL_STOP */
1010
1011 /* LCOV_EXCL_START */
1012 EXPORT_API int notification_set_priv_id(notification_h noti, int priv_id)
1013 {
1014         if (noti == NULL || priv_id <= 0)
1015                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1016
1017         noti->priv_id = priv_id;
1018
1019         return NOTIFICATION_ERROR_NONE;
1020 }
1021 /* LCOV_EXCL_STOP */
1022
1023 /* LCOV_EXCL_START */
1024 notification_h notification_load_for_uid(char *app_id,
1025                 int priv_id, uid_t uid)
1026 {
1027         int ret;
1028         notification_h noti;
1029
1030         noti = (notification_h)calloc(1, sizeof(struct _notification));
1031         if (noti == NULL) {
1032                 ERR("Failed to alloc memory");
1033                 set_last_result(NOTIFICATION_ERROR_OUT_OF_MEMORY);
1034                 return NULL;
1035         }
1036
1037         ret = notification_ipc_request_load_noti_by_priv_id(noti, app_id, priv_id, uid);
1038         if (ret != NOTIFICATION_ERROR_NONE) {
1039                 notification_free(noti);
1040                 set_last_result(ret);
1041                 return NULL;
1042         }
1043
1044         set_last_result(NOTIFICATION_ERROR_NONE);
1045
1046         return noti;
1047 }
1048 /* LCOV_EXCL_STOP */
1049
1050 /* LCOV_EXCL_START */
1051 EXPORT_API notification_h notification_load(char *app_id,
1052                 int priv_id)
1053 {
1054         return notification_load_for_uid(app_id, priv_id, getuid());
1055 }
1056 /* LCOV_EXCL_STOP */
1057
1058 /* LCOV_EXCL_START */
1059 EXPORT_API notification_h notification_new(notification_type_e type,
1060                 int group_id, int priv_id)
1061 {
1062         return notification_create(type);
1063 }
1064
1065 static void _notification_get_text_domain(notification_h noti)
1066 {
1067 }
1068 /* LCOV_EXCL_STOP */
1069
1070 /* LCOV_EXCL_START */
1071 EXPORT_API int notification_get_execute_option(notification_h noti,
1072                 notification_execute_type_e type,
1073                 const char **text,
1074                 bundle **service_handle)
1075 {
1076         char buf_key[32] = { 0, };
1077         char *ret_val = NULL;
1078         char *get_str = NULL;
1079         bundle *b = NULL;
1080
1081         if (noti == NULL)
1082                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1083
1084         if (type <= NOTIFICATION_EXECUTE_TYPE_NONE
1085                         || type > NOTIFICATION_EXECUTE_TYPE_MAX)
1086                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1087
1088         switch (type) {
1089         case NOTIFICATION_EXECUTE_TYPE_RESPONDING:
1090                 b = noti->b_service_responding;
1091                 break;
1092         case NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH:
1093                 b = noti->b_service_single_launch;
1094                 break;
1095         case NOTIFICATION_EXECUTE_TYPE_MULTI_LAUNCH:
1096                 b = noti->b_service_multi_launch;
1097                 break;
1098         default:
1099                 break;
1100         }
1101
1102         if (b != NULL) {
1103                 if (text != NULL) {
1104                         if (noti->domain == NULL || noti->dir == NULL)
1105                                 _notification_get_text_domain(noti);
1106
1107                         snprintf(buf_key, sizeof(buf_key), "key%d", type);
1108
1109                         bundle_get_str(b, buf_key, &ret_val);
1110                         if (ret_val != NULL && noti->domain != NULL
1111                                         && noti->dir != NULL) {
1112                                 bindtextdomain(noti->domain, noti->dir);
1113
1114                                 get_str = dgettext(noti->domain, ret_val);
1115
1116                                 *text = get_str;
1117                         } else if (ret_val != NULL) {
1118                                 get_str = dgettext("sys_string", ret_val);
1119
1120                                 *text = get_str;
1121                         } else {
1122                                 snprintf(buf_key, sizeof(buf_key), "text%d",
1123                                                 type);
1124
1125                                 bundle_get_str(b, buf_key, &ret_val);
1126
1127                                 *text = ret_val;
1128                         }
1129                 }
1130         }
1131
1132         if (service_handle != NULL)
1133                 *service_handle = b;
1134
1135         return NOTIFICATION_ERROR_NONE;
1136 }
1137 /* LCOV_EXCL_STOP */
1138
1139 /* LCOV_EXCL_START */
1140 EXPORT_API int notification_insert_for_uid(notification_h noti,
1141                 int *priv_id, uid_t uid)
1142 {
1143         int ret;
1144         int id;
1145
1146         if (noti == NULL)
1147                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1148
1149         if (noti->type <= NOTIFICATION_TYPE_NONE
1150                         || noti->type > NOTIFICATION_TYPE_MAX)
1151                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1152
1153         noti->uid = uid;
1154         noti->insert_time = time(NULL);
1155
1156         ret = notification_ipc_request_insert(noti, &id);
1157         if (ret != NOTIFICATION_ERROR_NONE)
1158                 return ret;
1159
1160         noti->priv_id = id;
1161
1162         /* If priv_id is valid data, set priv_id */
1163         if (priv_id != NULL)
1164                 *priv_id = noti->priv_id;
1165
1166         return NOTIFICATION_ERROR_NONE;
1167 }
1168
1169 EXPORT_API int notification_insert(notification_h noti,
1170                 int *priv_id)
1171 {
1172         return notification_insert_for_uid(noti, priv_id, getuid());
1173 }
1174
1175 EXPORT_API int notification_update_async_for_uid(notification_h noti,
1176                 void (*result_cb)(int priv_id, int result, void *data), void *user_data, uid_t uid)
1177 {
1178         if (noti == NULL)
1179                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1180
1181         noti->uid = uid;
1182         /* Update insert time ? */
1183         noti->insert_time = time(NULL);
1184
1185         return notification_ipc_request_update_async(noti, result_cb, user_data);
1186 }
1187
1188 EXPORT_API int notification_update_async(notification_h noti,
1189                 void (*result_cb)(int priv_id, int result, void *data), void *user_data)
1190 {
1191         return notification_update_async_for_uid(noti, result_cb, user_data, getuid());
1192 }
1193 /* LCOV_EXCL_STOP */
1194
1195 /* LCOV_EXCL_START */
1196 EXPORT_API int notification_register_detailed_changed_cb_for_uid(
1197                 void (*detailed_changed_cb)(void *data, notification_type_e type, notification_op *op_list, int num_op),
1198                 void *user_data, uid_t uid)
1199 {
1200         GList *noti_cb_list = NULL;
1201         notification_cb_info_s *noti_cb_info_new = NULL;
1202
1203         if (detailed_changed_cb == NULL)
1204                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1205
1206         if (_noti_cb_hash == NULL)
1207                 _noti_cb_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
1208
1209         noti_cb_info_new = (notification_cb_info_s *)malloc(sizeof(notification_cb_info_s));
1210         if (noti_cb_info_new == NULL) {
1211                 ERR("Failed to alloc memory");
1212                 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
1213         }
1214
1215         noti_cb_info_new->cb_type = NOTIFICATION_CB_DETAILED;
1216         noti_cb_info_new->changed_cb = NULL;
1217         noti_cb_info_new->detailed_changed_cb = detailed_changed_cb;
1218         noti_cb_info_new->data = user_data;
1219
1220         noti_cb_list = g_hash_table_lookup(_noti_cb_hash, GUINT_TO_POINTER(uid));
1221
1222         if (noti_cb_list == NULL) {
1223                 noti_cb_list = g_list_append(noti_cb_list, noti_cb_info_new);
1224                 g_hash_table_insert(_noti_cb_hash, GUINT_TO_POINTER(uid), noti_cb_list);
1225         } else {
1226                 noti_cb_list = g_list_append(noti_cb_list, noti_cb_info_new);
1227         }
1228
1229         if (notification_ipc_monitor_init(uid) != NOTIFICATION_ERROR_NONE) {
1230                 notification_unregister_detailed_changed_cb_for_uid(detailed_changed_cb, user_data, uid);
1231                 return NOTIFICATION_ERROR_IO_ERROR;
1232         }
1233
1234         return NOTIFICATION_ERROR_NONE;
1235 }
1236 /* LCOV_EXCL_STOP */
1237
1238 /* LCOV_EXCL_START */
1239 EXPORT_API int notification_register_detailed_changed_cb(
1240                 void (*detailed_changed_cb)(void *data, notification_type_e type, notification_op *op_list, int num_op),
1241                 void *user_data)
1242 {
1243         return notification_register_detailed_changed_cb_for_uid(detailed_changed_cb, user_data, getuid());
1244 }
1245
1246 static gint _noti_detailed_changed_compare(gconstpointer a, gconstpointer b)
1247 {
1248         const struct _notification_cb_info *info = NULL;
1249
1250         if (!a)
1251                 return -1;
1252         info = (notification_cb_info_s *)a;
1253
1254         if (info->detailed_changed_cb == b)
1255                 return 0;
1256
1257         return 1;
1258 }
1259
1260 EXPORT_API int notification_unregister_detailed_changed_cb_for_uid(
1261                 void (*detailed_changed_cb)(void *data, notification_type_e type, notification_op *op_list, int num_op),
1262                 void *user_data, uid_t uid)
1263 {
1264         notification_cb_info_s *noti_cb_info;
1265         GList *noti_cb_list;
1266         GList *delete_cb_list;
1267
1268         if (detailed_changed_cb == NULL)
1269                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1270
1271         if (_noti_cb_hash == NULL)
1272                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1273
1274         noti_cb_list = (GList *)g_hash_table_lookup(_noti_cb_hash, GUINT_TO_POINTER(uid));
1275
1276         if (noti_cb_list == NULL)
1277                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1278
1279         noti_cb_list = g_list_first(noti_cb_list);
1280         delete_cb_list = g_list_find_custom(noti_cb_list, (gconstpointer)detailed_changed_cb,
1281                                             (GCompareFunc)_noti_detailed_changed_compare);
1282
1283         if (delete_cb_list) {
1284                 noti_cb_info = (notification_cb_info_s *)g_list_nth_data(delete_cb_list, 0);
1285                 noti_cb_list = g_list_delete_link(noti_cb_list, delete_cb_list);
1286                 __free_changed_cb_info(noti_cb_info);
1287                 if (noti_cb_list == NULL) {
1288                         g_hash_table_steal(_noti_cb_hash, GUINT_TO_POINTER(uid));
1289                 } else {
1290                         noti_cb_list = g_list_first(noti_cb_list);
1291                         g_hash_table_replace(_noti_cb_hash, GUINT_TO_POINTER(uid), noti_cb_list);
1292                 }
1293
1294         } else {
1295                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1296         }
1297
1298         if (g_hash_table_size(_noti_cb_hash) == 0)
1299                 notification_ipc_monitor_fini();
1300
1301         return NOTIFICATION_ERROR_NONE;
1302
1303 }
1304
1305 EXPORT_API int notification_unregister_detailed_changed_cb(
1306                 void (*detailed_changed_cb)(void *data, notification_type_e type, notification_op *op_list, int num_op),
1307                 void *user_data)
1308 {
1309         return notification_unregister_detailed_changed_cb_for_uid(detailed_changed_cb, user_data, getuid());
1310 }
1311 /* LCOV_EXCL_STOP */
1312
1313 /* LCOV_EXCL_START */
1314 EXPORT_API int notification_is_service_ready(void)
1315 {
1316         return notification_ipc_is_master_ready();
1317 }
1318 /* LCOV_EXCL_STOP */
1319
1320 /* LCOV_EXCL_START */
1321 EXPORT_API int notification_set_uid(notification_h noti,
1322                 uid_t uid)
1323 {
1324         if (noti == NULL)
1325                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1326
1327         noti->uid = uid;
1328         return NOTIFICATION_ERROR_NONE;
1329 }
1330
1331 EXPORT_API int notification_get_uid(notification_h noti,
1332                 uid_t *uid)
1333 {
1334         if (noti == NULL || uid == NULL)
1335                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1336
1337         *uid = noti->uid;
1338         return NOTIFICATION_ERROR_NONE;
1339 }
1340
1341 static GList *__copy_private_file(notification_h noti)
1342 {
1343         bundle *dst_b;
1344         bundle *src_b;
1345         char buf_key[32];
1346         char *src_path;
1347         char *dst_path;
1348         int i;
1349         GList *file_list = NULL;
1350         int ret;
1351
1352         if (noti->b_priv_image_path && noti->b_image_path) {
1353                 dst_b = noti->b_priv_image_path;
1354                 src_b = noti->b_image_path;
1355                 for (i = NOTIFICATION_IMAGE_TYPE_ICON; i <= NOTIFICATION_IMAGE_TYPE_MAX; i++) {
1356                         src_path = NULL;
1357                         dst_path = NULL;
1358
1359                         snprintf(buf_key, sizeof(buf_key), "%d", i);
1360
1361                         bundle_get_str(dst_b, buf_key, &dst_path);
1362                         if (dst_path == NULL)
1363                                 continue;
1364
1365                         bundle_get_str(src_b, buf_key, &src_path);
1366                         if (src_path == NULL)
1367                                 continue;
1368
1369                         ret = notification_copy_private_file(src_path, dst_path);
1370                         if (ret == NOTIFICATION_ERROR_NONE)
1371                                 file_list = g_list_append(file_list, strdup(dst_path));
1372                 }
1373         }
1374         if (noti->sound_path && noti->priv_sound_path) {
1375                 ret = notification_copy_private_file(noti->sound_path,
1376                                                      noti->priv_sound_path);
1377                 if (ret == NOTIFICATION_ERROR_NONE)
1378                         file_list = g_list_append(file_list,
1379                                                   strdup(noti->priv_sound_path));
1380         }
1381
1382         if (noti->vibration_path && noti->priv_vibration_path) {
1383                 ret = notification_copy_private_file(noti->vibration_path,
1384                                                      noti->priv_vibration_path);
1385                 if (ret == NOTIFICATION_ERROR_NONE)
1386                         file_list = g_list_append(file_list,
1387                                                   strdup(noti->priv_vibration_path));
1388         }
1389
1390         return file_list;
1391 }
1392 /* LCOV_EXCL_STOP */
1393
1394 /* LCOV_EXCL_START */
1395 static void __remove_private_file(gpointer data, gpointer user_data)
1396 {
1397         GFile *src = NULL;
1398         char *path = (char *)data;
1399
1400         src = g_file_new_for_path(path);
1401         if (src) {
1402                 g_file_delete(src, NULL, NULL);
1403                 g_object_unref(src);
1404         }
1405 }
1406 /* LCOV_EXCL_STOP */
1407
1408 /* LCOV_EXCL_START */
1409 EXPORT_API int notification_post_for_uid(notification_h noti, uid_t uid)
1410 {
1411         int ret = 0;
1412         int id = 0;
1413         GList *file_list;
1414
1415         if (noti == NULL)
1416                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1417
1418         if (noti->type <= NOTIFICATION_TYPE_NONE
1419                         || noti->type > NOTIFICATION_TYPE_MAX)
1420                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1421
1422         /* Save insert time */
1423         noti->insert_time = time(NULL);
1424         noti->uid = uid;
1425
1426         file_list = __copy_private_file(noti);
1427         ret = notification_ipc_request_insert(noti, &id);
1428         if (ret == NOTIFICATION_ERROR_NONE) {
1429                 noti->priv_id = id;
1430                 INFO("Posted notification id[%d]", id);
1431         } else {
1432                 g_list_foreach(file_list, __remove_private_file, NULL);
1433         }
1434
1435         if (file_list)
1436                 g_list_free_full(file_list, free);
1437
1438         return ret;
1439 }
1440
1441 EXPORT_API int notification_update_for_uid(notification_h noti, uid_t uid)
1442 {
1443         if (noti == NULL) {
1444                 notification_ipc_request_refresh(uid);
1445                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1446         }
1447
1448         noti->uid = uid;
1449         /* Update insert time ? */
1450         noti->insert_time = time(NULL);
1451
1452         return notification_ipc_request_update(noti);
1453 }
1454
1455 EXPORT_API int notification_delete_for_uid(notification_h noti, uid_t uid)
1456 {
1457         if (noti == NULL)
1458                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1459
1460         return notification_ipc_request_delete_single(NOTIFICATION_TYPE_NONE,
1461                                 noti->caller_app_id, noti->priv_id, uid);
1462 }
1463
1464 EXPORT_API int notification_delete_all_for_uid(notification_type_e type, uid_t uid)
1465 {
1466         int ret = 0;
1467         char *caller_app_id = NULL;
1468
1469         if (type <= NOTIFICATION_TYPE_NONE || type > NOTIFICATION_TYPE_MAX)
1470                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1471
1472         caller_app_id = notification_get_app_id_by_pid(getpid());
1473
1474         ret = notification_ipc_request_delete_multiple(type, caller_app_id, uid);
1475
1476         if (caller_app_id)
1477                 free(caller_app_id);
1478
1479         return ret;
1480 }
1481 /* LCOV_EXCL_STOP */
1482
1483 /* LCOV_EXCL_START */
1484 EXPORT_API notification_h notification_load_by_tag_for_uid(const char *tag, uid_t uid)
1485 {
1486         int ret;
1487         notification_h noti;
1488         char *caller_app_id;
1489
1490         if (tag == NULL) {
1491                 ERR("Invalid tag");
1492                 set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
1493                 return NULL;
1494         }
1495
1496         caller_app_id = notification_get_app_id_by_pid(getpid());
1497         if (!caller_app_id) {
1498                 ERR("Failed to get a package name");
1499                 set_last_result(NOTIFICATION_ERROR_OUT_OF_MEMORY);
1500                 return NULL;
1501         }
1502
1503         noti = (notification_h)calloc(1, sizeof(struct _notification));
1504         if (noti == NULL) {
1505                 ERR("Failed to alloc a new notification");
1506                 set_last_result(NOTIFICATION_ERROR_OUT_OF_MEMORY);
1507                 free(caller_app_id);
1508
1509                 return NULL;
1510         }
1511
1512         ret = notification_ipc_request_load_noti_by_tag(noti, caller_app_id, (char *)tag, uid);
1513         free(caller_app_id);
1514
1515         set_last_result(ret);
1516         if (ret != NOTIFICATION_ERROR_NONE) {
1517                 notification_free(noti);
1518                 return NULL;
1519         }
1520
1521         return noti;
1522 }
1523 /* LCOV_EXCL_STOP */
1524
1525 /* LCOV_EXCL_START */
1526 EXPORT_API notification_h notification_create_from_package_template(const char *app_id, const char *template_name)
1527 {
1528         int ret;
1529         notification_h noti;
1530
1531         if (app_id == NULL || template_name == NULL) {
1532                 ERR("Invalid parameter");
1533                 set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
1534                 return NULL;
1535         }
1536
1537         noti = (notification_h)calloc(1, sizeof(struct _notification));
1538         if (noti == NULL) {
1539                 ERR("Failed to alloc memory");
1540                 set_last_result(NOTIFICATION_ERROR_OUT_OF_MEMORY);
1541                 return NULL;
1542         }
1543
1544         ret = notification_ipc_request_create_from_package_template(noti, app_id, template_name);
1545         set_last_result(ret);
1546         if (ret != NOTIFICATION_ERROR_NONE) {
1547                 notification_free(noti);
1548                 return NULL;
1549         }
1550
1551         return noti;
1552 }
1553 /* LCOV_EXCL_STOP */
1554
1555 /* LCOV_EXCL_START */
1556 EXPORT_API int notification_set_default_button(notification_h noti, notification_button_index_e index)
1557 {
1558         if (noti == NULL)
1559                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1560
1561         if (index < 0 || index > NOTIFICATION_BUTTON_6)
1562                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1563
1564         noti->default_button_index = index;
1565
1566         return NOTIFICATION_ERROR_NONE;
1567 }
1568
1569 EXPORT_API int notification_get_default_button(notification_h noti, notification_button_index_e *index)
1570 {
1571         if (noti == NULL || index == NULL)
1572                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1573
1574         *index = noti->default_button_index;
1575
1576         return NOTIFICATION_ERROR_NONE;
1577 }
1578
1579 EXPORT_API int notification_get_ongoing_value_type(notification_h noti, notification_ongoing_value_type_e *type)
1580 {
1581         if (noti == NULL || type == NULL)
1582                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1583
1584         *type = noti->ongoing_value_type;
1585
1586         return NOTIFICATION_ERROR_NONE;
1587 }
1588
1589 EXPORT_API int notification_set_ongoing_value_type(notification_h noti, notification_ongoing_value_type_e type)
1590 {
1591         if (noti == NULL)
1592                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1593
1594         if (type < NOTIFICATION_ONGOING_VALUE_TYPE_PERCENT || type > NOTIFICATION_ONGOING_VALUE_TYPE_TIME)
1595                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1596
1597         noti->ongoing_value_type = type;
1598
1599         return NOTIFICATION_ERROR_NONE;
1600 }
1601
1602 EXPORT_API int notification_get_ongoing_time(notification_h noti, int *current, int *duration)
1603 {
1604         if (noti == NULL || current == NULL || duration == NULL)
1605                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1606
1607         *current = noti->ongoing_current;
1608         *duration = noti->ongoing_duration;
1609
1610         return NOTIFICATION_ERROR_NONE;
1611 }
1612
1613 EXPORT_API int notification_set_ongoing_time(notification_h noti, int current, int duration)
1614 {
1615         if (noti == NULL)
1616                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1617
1618         if (current < 0 || duration < 0 || current > duration)
1619                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1620
1621         noti->ongoing_current = current;
1622         noti->ongoing_duration = duration;
1623
1624         return NOTIFICATION_ERROR_NONE;
1625 }
1626
1627 EXPORT_API int notification_get_hide_timeout(notification_h noti, int *timeout)
1628 {
1629         if (noti == NULL || timeout == NULL)
1630                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1631
1632         *timeout = noti->hide_timeout;
1633
1634         return NOTIFICATION_ERROR_NONE;
1635 }
1636
1637 EXPORT_API int notification_set_hide_timeout(notification_h noti, int timeout)
1638 {
1639         if (noti == NULL || timeout < 0)
1640                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1641
1642         noti->hide_timeout = timeout;
1643
1644         return NOTIFICATION_ERROR_NONE;
1645 }
1646
1647 EXPORT_API int notification_get_delete_timeout(notification_h noti, int *timeout)
1648 {
1649         if (noti == NULL || timeout == NULL)
1650                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1651
1652         *timeout = noti->delete_timeout;
1653
1654         return NOTIFICATION_ERROR_NONE;
1655 }
1656
1657 EXPORT_API int notification_set_delete_timeout(notification_h noti, int timeout)
1658 {
1659         if (noti == NULL || timeout < 0)
1660                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1661
1662         noti->delete_timeout = timeout;
1663
1664         return NOTIFICATION_ERROR_NONE;
1665 }
1666
1667 EXPORT_API int notification_get_text_input_max_length(notification_h noti, int *text_input_max_length)
1668 {
1669         if (noti == NULL || text_input_max_length == NULL)
1670                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1671
1672         *text_input_max_length = noti->text_input_max_length;
1673
1674         return NOTIFICATION_ERROR_NONE;
1675 }
1676 /* LCOV_EXCL_STOP */
1677
1678 /* LCOV_EXCL_START */
1679 EXPORT_API int notification_post_with_event_cb_for_uid(notification_h noti, event_handler_cb cb,
1680                                                         void *userdata, uid_t uid)
1681 {
1682         int ret;
1683         int priv_id;
1684         notification_event_cb_info_s *info = NULL;
1685         GList *find_list;
1686
1687         if (noti == NULL || cb == NULL)
1688                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1689
1690         if (noti->type <= NOTIFICATION_TYPE_NONE || noti->type > NOTIFICATION_TYPE_MAX)
1691                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1692
1693         noti->insert_time = time(NULL);
1694         noti->event_flag = true;
1695         noti->uid = uid;
1696
1697         ret = notification_ipc_request_insert(noti, &priv_id);
1698         if (ret != NOTIFICATION_ERROR_NONE)
1699                 return ret;
1700
1701         noti->priv_id = priv_id;
1702
1703         __noti_event_cb_list = g_list_first(__noti_event_cb_list);
1704         find_list = g_list_find_custom(__noti_event_cb_list, GINT_TO_POINTER(priv_id),
1705                                        (GCompareFunc)__priv_id_compare);
1706
1707         if (find_list) {
1708                 info = g_list_nth_data(find_list, 0);
1709                 info->cb = cb;
1710                 info->userdata = userdata;
1711         } else {
1712                 info = (notification_event_cb_info_s *)malloc(sizeof(notification_event_cb_info_s));
1713                 if (info == NULL) {
1714                         ERR("Failed to alloc memory");
1715                         return NOTIFICATION_ERROR_OUT_OF_MEMORY;
1716                 }
1717                 info->priv_id = priv_id;
1718                 info->cb = cb;
1719                 info->userdata = userdata;
1720                 __noti_event_cb_list = g_list_append(__noti_event_cb_list, info);
1721         }
1722
1723         return ret;
1724 }
1725 /* LCOV_EXCL_STOP */
1726
1727 /* LCOV_EXCL_START */
1728 EXPORT_API int notification_post_with_event_cb(notification_h noti, event_handler_cb cb, void *userdata)
1729 {
1730         return notification_post_with_event_cb_for_uid(noti, cb, userdata, getuid());
1731 }
1732
1733 EXPORT_API int notification_send_event(notification_h noti, int event_type)
1734 {
1735         int ret;
1736         bool event_flag;
1737
1738         if (noti == NULL)
1739                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1740
1741         if (!((event_type >= NOTIFICATION_EVENT_TYPE_CLICK_ON_BUTTON_1
1742                 && event_type <= NOTIFICATION_EVENT_TYPE_MAX) ||
1743                 (event_type >= NOTIFICATION_EVENT_TYPE_HIDDEN_BY_USER
1744                 && event_type <= NOTIFICATION_EVENT_TYPE_HIDDEN_BY_EXTERNAL) ||
1745                 (event_type >= NOTIFICATION_EVENT_TYPE_PRESSED
1746                 && event_type <= NOTIFICATION_EVENT_TYPE_DELETED) ||
1747                 (event_type == NOTIFICATION_EVENT_TYPE_CHECK_BOX)))
1748                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1749
1750         ret = notification_get_event_flag(noti, &event_flag);
1751         if (ret != NOTIFICATION_ERROR_NONE || event_flag == false)
1752                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1753
1754         ret = notification_ipc_send_event(noti, event_type, -1);
1755
1756         return ret;
1757 }
1758
1759 EXPORT_API int notification_send_event_by_priv_id(int priv_id, int event_type)
1760 {
1761         int ret;
1762
1763         if (priv_id <= 0)
1764                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1765
1766         if (!((event_type >= NOTIFICATION_EVENT_TYPE_CLICK_ON_BUTTON_1
1767                 && event_type <= NOTIFICATION_EVENT_TYPE_MAX) ||
1768                 (event_type >= NOTIFICATION_EVENT_TYPE_HIDDEN_BY_USER
1769                 && event_type <= NOTIFICATION_EVENT_TYPE_HIDDEN_BY_EXTERNAL) ||
1770                 (event_type >= NOTIFICATION_EVENT_TYPE_PRESSED
1771                 && event_type <= NOTIFICATION_EVENT_TYPE_DELETED) ||
1772                 (event_type == NOTIFICATION_EVENT_TYPE_CHECK_BOX)))
1773                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1774
1775         ret = notification_ipc_send_event(NULL, event_type, priv_id);
1776         return ret;
1777 }
1778
1779 EXPORT_API int notification_get_event_flag(notification_h noti, bool *flag)
1780 {
1781         if (noti == NULL || flag == NULL)
1782                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1783
1784         *flag = noti->event_flag;
1785
1786         return NOTIFICATION_ERROR_NONE;
1787 }
1788
1789 EXPORT_API int notification_check_event_receiver_available(notification_h noti, bool *available)
1790 {
1791         int ret;
1792         int priv_id;
1793
1794         if (noti == NULL || available == NULL)
1795                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1796
1797         ret = notification_get_id(noti, NULL, &priv_id);
1798         if (ret != NOTIFICATION_ERROR_NONE) {
1799                 ERR("Failed to get priv id");
1800                 return ret;
1801         }
1802
1803         ret = notification_ipc_check_event_receiver(priv_id, available);
1804
1805         return ret;
1806 }
1807
1808 static bundle *_create_bundle_from_bundle_raw(bundle_raw *string)
1809 {
1810         if (string == NULL || string[0] == '\0')
1811                 return NULL;
1812
1813         return bundle_decode(string, strlen((char *)string));
1814 }
1815
1816 EXPORT_API int notification_set_extention_data(notification_h noti, const char *key, bundle *value)
1817 {
1818         return notification_set_extension_data(noti, key, value);
1819 }
1820
1821 EXPORT_API int notification_set_extension_data(notification_h noti, const char *key, bundle *value)
1822 {
1823         int ret;
1824         int len = 0;
1825         char *del = NULL;
1826         bundle_raw *raw = NULL;
1827
1828         if (noti == NULL || key == NULL)
1829                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1830
1831         if (noti->args == NULL)
1832                 noti->args = bundle_create();
1833
1834         if (value == NULL) {
1835                 ret = bundle_del(noti->args, key);
1836                 if (ret == BUNDLE_ERROR_NONE)
1837                         return NOTIFICATION_ERROR_NONE;
1838                 else
1839                         return NOTIFICATION_ERROR_INVALID_PARAMETER;
1840         }
1841
1842         bundle_get_str(noti->args, key, &del);
1843         if (del != NULL) {
1844                 bundle_del(noti->args, key);
1845                 del = NULL;
1846         }
1847
1848         bundle_encode(value, &raw, &len);
1849         bundle_add_str(noti->args, key, (const char *)raw);
1850         bundle_free_encoded_rawdata(&raw);
1851
1852         return NOTIFICATION_ERROR_NONE;
1853 }
1854
1855 EXPORT_API int notification_get_extention_data(notification_h noti, const char *key, bundle **value)
1856 {
1857         return notification_get_extension_data(noti, key, value);
1858 }
1859
1860 EXPORT_API int notification_get_extension_data(notification_h noti, const char *key, bundle **value)
1861 {
1862         char *ret_str;
1863         bundle *args;
1864         int ret;
1865
1866         if (noti == NULL || key == NULL || value == NULL)
1867                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1868
1869         if (noti->args == NULL)
1870                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1871
1872         args = noti->args;
1873
1874         ret = bundle_get_str(args, key, &ret_str);
1875         if (ret != BUNDLE_ERROR_NONE)
1876                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1877
1878         *value = _create_bundle_from_bundle_raw((bundle_raw *)ret_str);
1879         if (*value == NULL)
1880                 return NOTIFICATION_ERROR_IO_ERROR;
1881
1882         return NOTIFICATION_ERROR_NONE;
1883 }
1884 /* LCOV_EXCL_STOP */
1885
1886 #define KEY_LEN 40
1887 #define EXTENSION_EVENT_KEY "_NOTIFICATION_EXTENSION_EVENT_"
1888
1889 /* LCOV_EXCL_START */
1890 EXPORT_API int notification_set_extension_event_handler(notification_h noti,
1891                                         notification_event_type_extension_e event,
1892                                         app_control_h event_handler)
1893 {
1894         int err;
1895         int ret = NOTIFICATION_ERROR_NONE;
1896         int len;
1897         bundle *app_control_bundle = NULL;
1898         bundle_raw *b_raw = NULL;
1899         char key[KEY_LEN];
1900         char *del = NULL;
1901
1902         if (noti == NULL || event_handler == NULL) {
1903                 ERR("Invalid parameter");
1904                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1905         }
1906
1907         if (event < NOTIFICATION_EVENT_TYPE_HIDDEN_BY_USER ||
1908             event > NOTIFICATION_EVENT_TYPE_HIDDEN_BY_EXTERNAL) {
1909                 ERR("Invalid event [%d]", event);
1910                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1911         }
1912
1913         if (noti->args == NULL)
1914                 noti->args = bundle_create();
1915
1916         snprintf(key, sizeof(key), "%s%d", EXTENSION_EVENT_KEY, event);
1917         bundle_get_str(noti->args, key, &del);
1918         if (del != NULL) {
1919                 bundle_del(noti->args, key);
1920                 del = NULL;
1921         }
1922
1923         err = app_control_export_as_bundle(event_handler, &app_control_bundle);
1924         if (err != APP_CONTROL_ERROR_NONE) {
1925                 ERR("Failed to export app_control to bundle [%d]", err);
1926                 ret = NOTIFICATION_ERROR_IO_ERROR;
1927                 goto out;
1928         }
1929
1930         err = bundle_encode(app_control_bundle, &b_raw, &len);
1931         if (err != BUNDLE_ERROR_NONE) {
1932                 ERR("Failed to encode bundle [%d]", err);
1933                 ret = NOTIFICATION_ERROR_IO_ERROR;
1934                 goto out;
1935         }
1936
1937         err = bundle_add_str(noti->args, key, (const char *)b_raw);
1938         if (err != BUNDLE_ERROR_NONE) {
1939                 ERR("Failed to add str to bundle [%d]", err);
1940                 ret = NOTIFICATION_ERROR_IO_ERROR;
1941                 goto out;
1942         }
1943
1944 out:
1945         if (b_raw)
1946                 bundle_free_encoded_rawdata(&b_raw);
1947         if (app_control_bundle)
1948                 bundle_free(app_control_bundle);
1949
1950         return ret;
1951 }
1952 /* LCOV_EXCL_STOP */
1953
1954 /* LCOV_EXCL_START */
1955 EXPORT_API int notification_get_extension_event_handler(notification_h noti,
1956                                         notification_event_type_extension_e event,
1957                                         app_control_h *event_handler)
1958 {
1959         int err;
1960         int ret = NOTIFICATION_ERROR_NONE;
1961         char *ret_str = NULL;
1962         char key[KEY_LEN];
1963         bundle *app_control_bundle = NULL;
1964         app_control_h ret_app_control = NULL;
1965
1966         if (noti == NULL || event_handler == NULL) {
1967                 ERR("Invalid parameter");
1968                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1969         }
1970
1971         if (event < NOTIFICATION_EVENT_TYPE_HIDDEN_BY_USER ||
1972             event > NOTIFICATION_EVENT_TYPE_HIDDEN_BY_EXTERNAL) {
1973                 ERR("Invalid event [%d]", event);
1974                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1975         }
1976
1977         snprintf(key, sizeof(key), "%s%d", EXTENSION_EVENT_KEY, event);
1978
1979         bundle_get_str(noti->args, key, &ret_str);
1980         if (ret_str == NULL) {
1981                 ERR("No handler, Invalid event[%d]", event);
1982                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1983         }
1984
1985         app_control_bundle = _create_bundle_from_bundle_raw((bundle_raw *)ret_str);
1986         if (app_control_bundle == NULL) {
1987                 ERR("Failed to create bundle");
1988                 return NOTIFICATION_ERROR_IO_ERROR;
1989         }
1990
1991         err = app_control_create(&ret_app_control);
1992         if (err != APP_CONTROL_ERROR_NONE) {
1993                 ERR("Failed to create app control [%d]", err);
1994                 ret = NOTIFICATION_ERROR_IO_ERROR;
1995                 goto out;
1996         }
1997
1998         err = app_control_import_from_bundle(ret_app_control, app_control_bundle);
1999         if (err != APP_CONTROL_ERROR_NONE) {
2000                 ERR("Failed to import app control from bundle [%d]", err);
2001                 app_control_destroy(ret_app_control);
2002                 ret = NOTIFICATION_ERROR_IO_ERROR;
2003                 goto out;
2004         }
2005
2006         *event_handler = ret_app_control;
2007
2008 out:
2009         if (app_control_bundle)
2010                 bundle_free(app_control_bundle);
2011
2012         return ret;
2013 }
2014 /* LCOV_EXCL_STOP */
2015
2016 /* LCOV_EXCL_START */
2017 EXPORT_API int notification_get_all_count_for_uid(notification_type_e type, int *count, uid_t uid)
2018 {
2019         int ret;
2020
2021         if (count == NULL) {
2022                 ERR("Invalid parameter - count is null");
2023                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
2024         }
2025
2026         if (type < NOTIFICATION_TYPE_NONE || type > NOTIFICATION_TYPE_MAX) {
2027                 ERR("Invalid parameter - wrong type");
2028                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
2029         }
2030
2031         ret = notification_ipc_request_get_all_count(type, count, uid);
2032         if (ret != NOTIFICATION_ERROR_NONE) {
2033                 ERR("Failed to get count [%d]", ret);
2034                 return ret;
2035         }
2036
2037         return ret;
2038 }
2039
2040 EXPORT_API int notification_get_all_count(notification_type_e type, int *count)
2041 {
2042         return notification_get_all_count_for_uid(type, count, getuid());
2043 }
2044
2045 EXPORT_API int notification_set_app_label(notification_h noti, char *label)
2046 {
2047         if (noti == NULL || label == NULL)
2048                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
2049
2050         if (noti->app_label)
2051                 free(noti->app_label);
2052
2053         noti->app_label = strdup(label);
2054
2055         return NOTIFICATION_ERROR_NONE;
2056 }
2057
2058 EXPORT_API int notification_get_app_label(notification_h noti, char **label)
2059 {
2060         if (noti == NULL || label == NULL)
2061                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
2062
2063         if (noti->app_label)
2064                 *label = noti->app_label;
2065
2066         return NOTIFICATION_ERROR_NONE;
2067 }
2068
2069 static void __set_caller_info(bundle *b, const char *appid, uid_t uid)
2070 {
2071         pkgmgrinfo_appinfo_h handle;
2072         char buf[12];
2073         char *pkgid = NULL;
2074         int r;
2075
2076         snprintf(buf, sizeof(buf), "%u", uid);
2077         bundle_del(b, AUL_K_ORG_CALLER_UID);
2078         bundle_add(b, AUL_K_ORG_CALLER_UID, buf);
2079
2080         bundle_del(b, AUL_K_ORG_CALLER_APPID);
2081         bundle_add(b, AUL_K_ORG_CALLER_APPID, appid);
2082
2083         r = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &handle);
2084         if (r != PMINFO_R_OK)
2085                 return;
2086
2087         pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
2088         if (pkgid) {
2089                 bundle_del(b, AUL_K_ORG_CALLER_PKGID);
2090                 bundle_add(b, AUL_K_ORG_CALLER_PKGID, pkgid);
2091         }
2092         pkgmgrinfo_appinfo_destroy_appinfo(handle);
2093 }
2094
2095 static void __set_indirect_request(bundle *b)
2096 {
2097         bundle_del(b, AUL_K_REQUEST_TYPE);
2098         bundle_add(b, AUL_K_REQUEST_TYPE, "indirect-request");
2099 }
2100
2101 EXPORT_API int notification_set_indirect_request(notification_h noti,
2102                 pid_t pid, uid_t uid)
2103 {
2104         char appid[256] = { 0, };
2105         int r;
2106         int i;
2107
2108         if (noti == NULL || pid <= 1 || uid < REGULAR_UID_MIN)
2109                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
2110
2111         r = aul_app_get_appid_bypid(pid, appid, sizeof(appid));
2112         if (r != AUL_R_OK)
2113                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
2114
2115         if (noti->b_service_responding) {
2116                 __set_caller_info(noti->b_service_responding, appid, uid);
2117                 __set_indirect_request(noti->b_service_responding);
2118         }
2119
2120         if (noti->b_service_single_launch) {
2121                 __set_caller_info(noti->b_service_single_launch, appid, uid);
2122                 __set_indirect_request(noti->b_service_single_launch);
2123         }
2124
2125         if (noti->b_service_multi_launch) {
2126                 __set_caller_info(noti->b_service_multi_launch, appid, uid);
2127                 __set_indirect_request(noti->b_service_multi_launch);
2128         }
2129
2130         for (i = 0; i <= NOTIFICATION_EVENT_TYPE_MAX; i++) {
2131                 if (noti->b_event_handler[i]) {
2132                         __set_caller_info(noti->b_event_handler[i], appid, uid);
2133                         __set_indirect_request(noti->b_event_handler[i]);
2134                 }
2135         }
2136
2137         return NOTIFICATION_ERROR_NONE;
2138 }
2139
2140
2141 int notification_delete_by_display_applist_for_uid(int display_applist, uid_t uid)
2142 {
2143         if (display_applist < NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY)
2144                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
2145
2146         return notification_ipc_request_delete_by_display_applist(display_applist, uid);
2147 }
2148
2149 EXPORT_API int notification_delete_by_display_applist(int display_applist)
2150 {
2151         return notification_delete_by_display_applist_for_uid(display_applist, getuid());
2152 }
2153
2154 EXPORT_API int notification_set_check_box(notification_h noti,
2155                 bool flag, bool checked)
2156 {
2157         if (noti == NULL) {
2158                 ERR("Invalid parameter");
2159                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
2160         }
2161
2162         noti->check_box = flag;
2163         noti->check_box_value = checked;
2164
2165         return NOTIFICATION_ERROR_NONE;
2166 }
2167
2168 EXPORT_API int notification_get_check_box(notification_h noti,
2169                 bool *flag, bool *checked)
2170 {
2171         if (noti == NULL || flag == NULL || checked == NULL) {
2172                 ERR("Invalid parameter");
2173                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
2174         }
2175
2176         *flag = noti->check_box;
2177         *checked = noti->check_box_value;
2178
2179         return NOTIFICATION_ERROR_NONE;
2180 }
2181
2182 EXPORT_API int notification_set_check_box_checked(notification_h noti,
2183                 bool checked)
2184 {
2185         if (noti == NULL) {
2186                 ERR("Invalid parameter");
2187                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
2188         }
2189
2190         noti->check_box_value = checked;
2191
2192         return NOTIFICATION_ERROR_NONE;
2193 }
2194
2195 EXPORT_API int notification_get_check_box_checked(notification_h noti,
2196                 bool *checked)
2197 {
2198         if (noti == NULL || checked == NULL) {
2199                 ERR("Invalid parameter");
2200                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
2201         }
2202
2203         if (noti->check_box)
2204                 *checked = noti->check_box_value;
2205         else
2206                 *checked = false;
2207
2208         return NOTIFICATION_ERROR_NONE;
2209 }
2210 /* LCOV_EXCL_STOP */