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