Add new api to get notification block state
[platform/core/api/notification.git] / src / notification.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_internal.h>
28 #include <app_manager.h>
29 #include <app_control_internal.h>
30 #include <package_manager.h>
31 #include <aul.h>
32 #include <appsvc.h>
33 #include <tizen.h>
34 #include <vconf-keys.h>
35 #include <vconf.h>
36
37 #include <notification.h>
38 #include <notification_list.h>
39 #include <notification_debug.h>
40 #include <notification_private.h>
41 #include <notification_noti.h>
42 #include <notification_ongoing.h>
43 #include <notification_group.h>
44 #include <notification_ipc.h>
45 #include <notification_internal.h>
46
47 static void (*posted_toast_message_cb) (void *data);
48
49 #define NOTI_TEXT_RESULT_LEN 2048
50 #define NOTI_PKGNAME_LEN        512
51
52 #define REGULAR_UID_MIN 5000
53
54 char *notification_get_pkgname_by_pid(void)
55 {
56         char pkgname[NOTI_PKGNAME_LEN + 1] = { 0, };
57         int pid = 0, ret = AUL_R_OK;
58         int fd;
59         char  *dup_pkgname;
60         char buf[NOTI_PKGNAME_LEN + 1] = { 0, };
61
62         pid = getpid();
63
64         ret = aul_app_get_pkgname_bypid(pid, pkgname, sizeof(pkgname));
65         if (ret != AUL_R_OK) {
66
67                 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
68
69                 fd = open(buf, O_RDONLY);
70                 if (fd < 0)
71                         return NULL;
72
73                 ret = read(fd, pkgname, sizeof(pkgname) - 1);
74                 close(fd);
75
76                 if (ret <= 0)
77                         return NULL;
78
79                 pkgname[ret] = '\0';
80                 /*!
81                  * \NOTE
82                  * "ret" is not able to be larger than "sizeof(pkgname) - 1",
83                  * if the system is not going wrong.
84                  */
85         } else {
86                 if (strlen(pkgname) <= 0)
87                         return NULL;
88         }
89
90         dup_pkgname = strdup(pkgname);
91         if (!dup_pkgname)
92                 NOTIFICATION_ERR("Heap: %d\n", errno);
93
94         return dup_pkgname;
95 }
96
97 EXPORT_API int notification_set_image(notification_h noti,
98                                                        notification_image_type_e type,
99                                                        const char *image_path)
100 {
101         bundle *b = NULL;
102         char buf_key[32] = { 0, };
103         char *ret_val = NULL;
104
105         if (noti == NULL || image_path == NULL)
106                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
107
108         if (type <= NOTIFICATION_IMAGE_TYPE_NONE
109             || type >= NOTIFICATION_IMAGE_TYPE_MAX)
110                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
111
112         if (noti->b_image_path) {
113                 b = noti->b_image_path;
114
115                 snprintf(buf_key, sizeof(buf_key), "%d", type);
116
117                 bundle_get_str(b, buf_key, &ret_val);
118                 if (ret_val != NULL)
119                         bundle_del(b, buf_key);
120
121                 bundle_add_str(b, buf_key, image_path);
122         } else {
123                 b = bundle_create();
124
125                 snprintf(buf_key, sizeof(buf_key), "%d", type);
126
127                 bundle_add_str(b, buf_key, image_path);
128
129                 noti->b_image_path = b;
130         }
131
132         return NOTIFICATION_ERROR_NONE;
133 }
134
135 EXPORT_API int notification_get_image(notification_h noti,
136                                                        notification_image_type_e type,
137                                                        char **image_path)
138 {
139         bundle *b = NULL;
140         char buf_key[32] = { 0, };
141         char *ret_val = NULL;
142
143         if (noti == NULL || image_path == NULL)
144                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
145
146         if (type <= NOTIFICATION_IMAGE_TYPE_NONE
147             || type >= NOTIFICATION_IMAGE_TYPE_MAX)
148                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
149
150         if (noti->b_image_path) {
151                 b = noti->b_image_path;
152
153                 snprintf(buf_key, sizeof(buf_key), "%d", type);
154
155                 bundle_get_str(b, buf_key, &ret_val);
156
157                 *image_path = ret_val;
158         } else {
159                 /* If image path bundle does not exist, image path is NULL */
160                 *image_path = NULL;
161         }
162
163         /* If image path is NULL and type is ICON, icon path set from AIL */
164         /* order : user icon -> launch_pkgname icon -> caller_pkgname icon -> service app icon */
165         if (*image_path == NULL && type == NOTIFICATION_IMAGE_TYPE_ICON) {
166                 if (noti->app_icon_path != NULL)
167                         *image_path = noti->app_icon_path;
168                 else
169                         *image_path = NULL;
170         }
171
172         return NOTIFICATION_ERROR_NONE;
173 }
174
175 EXPORT_API int notification_set_time(notification_h noti, time_t input_time)
176 {
177         if (noti == NULL)
178                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
179
180         if (input_time == 0)
181                 noti->time = time(NULL);
182         else
183                 noti->time = input_time;
184
185         return NOTIFICATION_ERROR_NONE;
186 }
187
188 EXPORT_API int notification_get_time(notification_h noti, time_t *ret_time)
189 {
190         if (noti == NULL || ret_time == NULL)
191                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
192
193         *ret_time = noti->time;
194
195         return NOTIFICATION_ERROR_NONE;
196 }
197
198 EXPORT_API int notification_get_insert_time(notification_h noti,
199                 time_t *ret_time)
200 {
201         if (noti == NULL || ret_time == NULL)
202                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
203
204         *ret_time = noti->insert_time;
205
206         return NOTIFICATION_ERROR_NONE;
207 }
208
209 EXPORT_API int notification_set_text(notification_h noti,
210                 notification_text_type_e type, const char *text,
211                 const char *key, int args_type, ...)
212 {
213         bundle *b = NULL;
214         char buf_key[32] = { 0, };
215         char buf_val[1024] = { 0, };
216         char *ret_val = NULL;
217         va_list var_args;
218         notification_variable_type_e var_type;
219         int num_args = 0;
220         int noti_err = NOTIFICATION_ERROR_NONE;
221         int var_value_int = 0;
222         double var_value_double = 0.0;
223         char *var_value_string = NULL;
224         notification_count_pos_type_e var_value_count =
225             NOTIFICATION_COUNT_POS_NONE;
226
227         if (noti == NULL)
228                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
229
230         if (type <= NOTIFICATION_TEXT_TYPE_NONE
231             || type >= NOTIFICATION_TEXT_TYPE_MAX)
232                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
233
234         if (text != NULL) {
235                 if (noti->b_text != NULL) {
236                         b = noti->b_text;
237
238                         snprintf(buf_key, sizeof(buf_key), "%d", type);
239
240                         bundle_get_str(b, buf_key, &ret_val);
241
242                         if (ret_val != NULL)
243                                 bundle_del(b, buf_key);
244
245                         snprintf(buf_val, sizeof(buf_val), "%s", text);
246
247                         bundle_add_str(b, buf_key, buf_val);
248                 } else {
249                         b = bundle_create();
250
251                         snprintf(buf_key, sizeof(buf_key), "%d", type);
252                         snprintf(buf_val, sizeof(buf_val), "%s", text);
253
254                         bundle_add_str(b, buf_key, buf_val);
255
256                         noti->b_text = b;
257                 }
258         } else {
259                 if (noti->b_text != NULL) {
260                         b = noti->b_text;
261
262                         snprintf(buf_key, sizeof(buf_key), "%d", type);
263
264                         bundle_get_str(b, buf_key, &ret_val);
265                         if (ret_val != NULL)
266                                 bundle_del(b, buf_key);
267                 }
268         }
269
270         if (key != NULL) {
271                 if (noti->b_key != NULL) {
272                         b = noti->b_key;
273
274                         snprintf(buf_key, sizeof(buf_key), "%d", type);
275
276                         bundle_get_str(b, buf_key, &ret_val);
277                         if (ret_val != NULL)
278                                 bundle_del(b, buf_key);
279
280                         snprintf(buf_val, sizeof(buf_val), "%s", key);
281
282                         bundle_add_str(b, buf_key, buf_val);
283                 } else {
284                         b = bundle_create();
285
286                         snprintf(buf_key, sizeof(buf_key), "%d", type);
287
288                         snprintf(buf_val, sizeof(buf_val), "%s", key);
289
290                         bundle_add_str(b, buf_key, buf_val);
291
292                         noti->b_key = b;
293                 }
294         } else {
295                 if (noti->b_key != NULL) {
296                         b = noti->b_key;
297
298                         snprintf(buf_key, sizeof(buf_key), "%d", type);
299
300                         bundle_get_str(b, buf_key, &ret_val);
301                         if (ret_val != NULL)
302                                 bundle_del(b, buf_key);
303                 }
304         }
305
306         if (noti->b_format_args != NULL)
307                 b = noti->b_format_args;
308         else
309                 b = bundle_create();
310
311         va_start(var_args, args_type);
312
313         var_type = args_type;
314         num_args = 0;
315
316         while (var_type != NOTIFICATION_VARIABLE_TYPE_NONE) {
317                 /* Type */
318                 snprintf(buf_key, sizeof(buf_key), "%dtype%d", type, num_args);
319                 snprintf(buf_val, sizeof(buf_val), "%d", var_type);
320
321                 bundle_get_str(b, buf_key, &ret_val);
322                 if (ret_val != NULL)
323                         bundle_del(b, buf_key);
324
325                 bundle_add_str(b, buf_key, buf_val);
326
327                 switch (var_type) {
328                 case NOTIFICATION_VARIABLE_TYPE_INT:
329                         var_value_int = va_arg(var_args, int);
330
331                         /* Value */
332                         snprintf(buf_key, sizeof(buf_key), "%dvalue%d", type,
333                                  num_args);
334                         snprintf(buf_val, sizeof(buf_val), "%d", var_value_int);
335
336                         bundle_get_str(b, buf_key, &ret_val);
337                         if (ret_val != NULL)
338                                 bundle_del(b, buf_key);
339
340                         bundle_add_str(b, buf_key, buf_val);
341                         break;
342
343                 case NOTIFICATION_VARIABLE_TYPE_DOUBLE:
344                         var_value_double = va_arg(var_args, double);
345
346                         /* Value */
347                         snprintf(buf_key, sizeof(buf_key), "%dvalue%d", type,
348                                  num_args);
349                         snprintf(buf_val, sizeof(buf_val), "%.2f",
350                                  var_value_double);
351
352                         bundle_get_str(b, buf_key, &ret_val);
353                         if (ret_val != NULL)
354                                 bundle_del(b, buf_key);
355
356                         bundle_add_str(b, buf_key, buf_val);
357                         break;
358
359                 case NOTIFICATION_VARIABLE_TYPE_STRING:
360                         var_value_string = va_arg(var_args, char *);
361
362                         /* Value */
363                         snprintf(buf_key, sizeof(buf_key), "%dvalue%d", type,
364                                  num_args);
365                         snprintf(buf_val, sizeof(buf_val), "%s",
366                                  var_value_string);
367
368                         bundle_get_str(b, buf_key, &ret_val);
369                         if (ret_val != NULL)
370                                 bundle_del(b, buf_key);
371
372                         bundle_add_str(b, buf_key, buf_val);
373                         break;
374
375                 case NOTIFICATION_VARIABLE_TYPE_COUNT:
376                         var_value_count =
377                             va_arg(var_args, notification_count_pos_type_e);
378
379                         /* Value */
380                         snprintf(buf_key, sizeof(buf_key), "%dvalue%d", type,
381                                  num_args);
382                         snprintf(buf_val, sizeof(buf_val), "%d",
383                                  var_value_count);
384
385                         bundle_get_str(b, buf_key, &ret_val);
386                         if (ret_val != NULL)
387                                 bundle_del(b, buf_key);
388
389                         bundle_add_str(b, buf_key, buf_val);
390                         break;
391
392                 default:
393                         NOTIFICATION_ERR("Error. invalid variable type. : %d",
394                                          var_type);
395                         noti_err = NOTIFICATION_ERROR_INVALID_PARAMETER;
396                         break;
397                 }
398
399                 num_args++;
400                 var_type = va_arg(var_args, notification_variable_type_e);
401         }
402         va_end(var_args);
403
404         if (noti_err == NOTIFICATION_ERROR_NONE)
405                 noti->num_format_args = num_args;
406         else
407                 noti->num_format_args = 0;
408
409         snprintf(buf_key, sizeof(buf_key), "num%d", type);
410         snprintf(buf_val, sizeof(buf_val), "%d", noti->num_format_args);
411
412         bundle_get_str(b, buf_key, &ret_val);
413         if (ret_val != NULL)
414                 bundle_del(b, buf_key);
415
416         bundle_add_str(b, buf_key, buf_val);
417
418         noti->b_format_args = b;
419
420         return noti_err;
421 }
422
423 EXPORT_API int notification_get_text(notification_h noti,
424                                                       notification_text_type_e type,
425                                                       char **text)
426 {
427         bundle *b = NULL;
428         char buf_key[32] = { 0, };
429         char *ret_val = NULL;
430         char *get_str = NULL;
431         notification_text_type_e check_type = NOTIFICATION_TEXT_TYPE_NONE;
432
433         char *temp_str = NULL;
434         char *translated_str = NULL;
435         char result_str[NOTI_TEXT_RESULT_LEN] = { 0, };
436         char buf_str[1024] = { 0, };
437         int num_args = 0;
438         notification_variable_type_e ret_var_type = 0;
439         int ret_variable_int = 0;
440         double ret_variable_double = 0.0;
441         int src_len = 0;
442         int max_len = 0;
443
444         if (noti == NULL || text == NULL)
445                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
446
447         if (type <= NOTIFICATION_TEXT_TYPE_NONE
448             || type >= NOTIFICATION_TEXT_TYPE_MAX)
449                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
450
451
452         if (noti->b_key != NULL) {
453                 b = noti->b_key;
454
455                 /* Get text domain and dir */
456                 /* _notification_get_text_domain(noti); */
457
458                 snprintf(buf_key, sizeof(buf_key), "%d", type);
459
460                 bundle_get_str(b, buf_key, &ret_val);
461                 if (ret_val != NULL && noti->domain != NULL
462                     && noti->dir != NULL) {
463                         /* Get application string */
464                         bindtextdomain(noti->domain, noti->dir);
465
466                         get_str = dgettext(noti->domain, ret_val);
467                         if (get_str == ret_val) /* not found */
468                                 get_str = NULL;
469                 } else if (ret_val != NULL) {
470                         /* Get system string */
471                         get_str = dgettext("sys_string", ret_val);
472                         if (get_str == ret_val) /* not found */
473                                 get_str = NULL;
474                 } else {
475                         get_str = NULL;
476                 }
477         }
478
479         if (get_str == NULL && noti->b_text != NULL) {
480                 b = noti->b_text;
481
482                 snprintf(buf_key, sizeof(buf_key), "%d", type);
483
484                 bundle_get_str(b, buf_key, &get_str);
485         }
486
487         if (get_str == NULL && ret_val != NULL)
488                 get_str = ret_val; /* fallback for printing anything */
489
490         check_type = type;
491
492         if (get_str != NULL) {
493                 /* Get number format args */
494                 b = noti->b_format_args;
495                 noti->num_format_args = 0;
496
497                 if (b != NULL) {
498                         snprintf(buf_key, sizeof(buf_key), "num%d", check_type);
499                         bundle_get_str(b, buf_key, &ret_val);
500                         if (ret_val != NULL)
501                                 noti->num_format_args = atoi(ret_val);
502                 }
503
504                 if (noti->num_format_args == 0) {
505                         *text = (char *)get_str;
506                 } else {
507                         /* Check first variable is count, LEFT pos */
508                         snprintf(buf_key, sizeof(buf_key), "%dtype%d",
509                                  check_type, num_args);
510                         bundle_get_str(b, buf_key, &ret_val);
511                         if (ret_val != NULL)
512                                 ret_var_type = atoi(ret_val);
513
514                         if (ret_var_type == NOTIFICATION_VARIABLE_TYPE_COUNT) {
515                                 /* Get var Value */
516                                 snprintf(buf_key, sizeof(buf_key), "%dvalue%d",
517                                          check_type, num_args);
518                                 bundle_get_str(b, buf_key, &ret_val);
519                                 if (ret_val != NULL)
520                                         ret_variable_int = atoi(ret_val);
521
522                                 if (ret_variable_int ==
523                                     NOTIFICATION_COUNT_POS_LEFT) {
524                                         notification_get_count(noti->type,
525                                                         noti->caller_pkgname,
526                                                         noti->group_id,
527                                                         noti->priv_id,
528                                                         &ret_variable_int);
529                                         snprintf(buf_str, sizeof(buf_str),
530                                                  "%d ", ret_variable_int);
531
532                                         src_len = strlen(result_str);
533                                         max_len = NOTI_TEXT_RESULT_LEN - src_len - 1;
534
535                                         strncat(result_str, buf_str,
536                                                         max_len);
537                                         num_args++;
538                                 }
539                         }
540
541                         /* Check variable IN pos */
542                         for (temp_str = (char *)get_str; *temp_str != '\0';
543                              temp_str++) {
544                                 if (*temp_str != '%') {
545                                         strncat(result_str, temp_str, 1);
546                                 } else {
547                                         if (*(temp_str + 1) == '%') {
548                                                 strncat(result_str, temp_str,
549                                                         1);
550                                         } else if (*(temp_str + 1) == 'd') {
551                                                 /* Get var Type */
552                                                 ret_variable_int = 0;
553
554                                                 snprintf(buf_key,
555                                                          sizeof(buf_key),
556                                                          "%dtype%d", check_type,
557                                                          num_args);
558                                                 bundle_get_str(b, buf_key, &ret_val);
559                                                 if (ret_val != NULL)
560                                                         ret_var_type = atoi(ret_val);
561
562                                                 if (ret_var_type ==
563                                                     NOTIFICATION_VARIABLE_TYPE_COUNT) {
564                                                         /* Get notification count */
565                                                         notification_get_count(noti->type,
566                                                                         noti->caller_pkgname,
567                                                                         noti->group_id,
568                                                                         noti->priv_id,
569                                                                         &ret_variable_int);
570                                                 } else {
571                                                         /* Get var Value */
572                                                         snprintf(buf_key,
573                                                                  sizeof(buf_key),
574                                                                  "%dvalue%d",
575                                                                  check_type,
576                                                                  num_args);
577                                                         bundle_get_str(b, buf_key, &ret_val);
578                                                         if (ret_val != NULL)
579                                                                 ret_variable_int = atoi(ret_val);
580                                                 }
581
582                                                 snprintf(buf_str,
583                                                          sizeof(buf_str), "%d",
584                                                          ret_variable_int);
585
586                                                 src_len = strlen(result_str);
587                                                 max_len = NOTI_TEXT_RESULT_LEN - src_len - 1;
588
589                                                 strncat(result_str, buf_str,
590                                                                 max_len);
591
592                                                 temp_str++;
593
594                                                 num_args++;
595                                         } else if (*(temp_str + 1) == 's') {
596                                                 /* Get var Value */
597                                                 snprintf(buf_key,
598                                                          sizeof(buf_key),
599                                                          "%dvalue%d",
600                                                          check_type, num_args);
601                                                 bundle_get_str(b, buf_key, &ret_val);
602
603                                                 if (ret_val != NULL && noti->domain != NULL     && noti->dir != NULL) {
604                                                         /* Get application string */
605                                                         bindtextdomain(noti->domain, noti->dir);
606                                                         translated_str = dgettext(noti->domain, ret_val);
607                                                         NOTIFICATION_INFO("translated_str[%s]", translated_str);
608                                                 } else if (ret_val != NULL) {
609                                                         /* Get system string */
610                                                         translated_str = dgettext("sys_string", ret_val);
611                                                         NOTIFICATION_INFO("translated_str[%s]", translated_str);
612                                                 } else {
613                                                         translated_str = NULL;
614                                                 }
615
616                                                 if (translated_str != NULL) {
617                                                         strncpy(buf_str, translated_str, sizeof(buf_str) - 1);
618                                                         src_len = strlen(result_str);
619                                                         max_len = NOTI_TEXT_RESULT_LEN - src_len - 1;
620                                                         strncat(result_str, buf_str, max_len);
621                                                 }
622                                                 temp_str++;
623                                                 num_args++;
624                                         } else if (*(temp_str + 1) == 'f') {
625                                                 /* Get var Value */
626                                                 snprintf(buf_key,
627                                                          sizeof(buf_key),
628                                                          "%dvalue%d",
629                                                          check_type, num_args);
630                                                 bundle_get_str(b, buf_key, &ret_val);
631                                                 if (ret_val != NULL)
632                                                         ret_variable_double = atof(ret_val);
633
634                                                 snprintf(buf_str,
635                                                          sizeof(buf_str),
636                                                          "%.2f",
637                                                          ret_variable_double);
638
639                                                 src_len = strlen(result_str);
640                                                 max_len = NOTI_TEXT_RESULT_LEN - src_len - 1;
641                                                 strncat(result_str, buf_str, max_len);
642
643                                                 temp_str++;
644                                                 num_args++;
645                                         } else if (*(temp_str + 1) >= '1' && *(temp_str + 1) <= '9') {
646                                                 if (*(temp_str + 3) == 'd') {
647                                                         /* Get var Type */
648                                                         ret_variable_int = 0;
649
650                                                         snprintf(buf_key,
651                                                                  sizeof(buf_key),
652                                                                  "%dtype%d", check_type,
653                                                                  num_args + *(temp_str + 1) - 49);
654                                                         bundle_get_str(b, buf_key, &ret_val);
655                                                         if (ret_val != NULL)
656                                                                 ret_var_type = atoi(ret_val);
657
658                                                         if (ret_var_type ==
659                                                             NOTIFICATION_VARIABLE_TYPE_COUNT) {
660                                                                 /* Get notification count */
661                                                                 notification_get_count(noti->type,
662                                                                                 noti->caller_pkgname,
663                                                                                 noti->group_id,
664                                                                                 noti->priv_id,
665                                                                                 &ret_variable_int);
666                                                         } else {
667                                                                 /* Get var Value */
668                                                                 snprintf(buf_key,
669                                                                          sizeof
670                                                                          (buf_key),
671                                                                          "%dvalue%d",
672                                                                          check_type,
673                                                                          num_args + *(temp_str + 1) - 49);
674                                                                 bundle_get_str(b, buf_key, &ret_val);
675                                                                 if (ret_val != NULL)
676                                                                         ret_variable_int = atoi(ret_val);
677
678                                                         }
679
680                                                         snprintf(buf_str,
681                                                                  sizeof(buf_str), "%d",
682                                                                  ret_variable_int);
683
684                                                         src_len = strlen(result_str);
685                                                         max_len = NOTI_TEXT_RESULT_LEN - src_len - 1;
686
687                                                         strncat(result_str, buf_str, max_len);
688
689                                                         temp_str += 3;
690                                                 } else if (*(temp_str + 3) == 's') {
691                                                         /* Get var Value */
692                                                         snprintf(buf_key,
693                                                                  sizeof(buf_key),
694                                                                  "%dvalue%d",
695                                                                  check_type, num_args + *(temp_str + 1) - 49);
696                                                         bundle_get_str(b, buf_key, &ret_val);
697
698                                                         snprintf(buf_str,
699                                                                  sizeof(buf_str), "%s",
700                                                                  ret_val);
701
702                                                         src_len = strlen(result_str);
703                                                         max_len = NOTI_TEXT_RESULT_LEN - src_len - 1;
704
705                                                         strncat(result_str, buf_str, max_len);
706
707                                                         temp_str += 3;
708                                                 } else if (*(temp_str + 3) == 'f') {
709                                                         /* Get var Value */
710                                                         snprintf(buf_key,
711                                                                  sizeof(buf_key),
712                                                                  "%dvalue%d",
713                                                                  check_type, num_args + *(temp_str + 1) - 49);
714                                                         bundle_get_str(b, buf_key, &ret_val);
715                                                         if (ret_val != NULL)
716                                                                 ret_variable_double = atof(ret_val);
717
718                                                         snprintf(buf_str,
719                                                                  sizeof(buf_str),
720                                                                  "%.2f",
721                                                                  ret_variable_double);
722
723                                                         src_len = strlen(result_str);
724                                                         max_len = NOTI_TEXT_RESULT_LEN - src_len - 1;
725
726                                                         strncat(result_str, buf_str, max_len);
727
728                                                         temp_str += 3;
729                                                 }
730                                         }
731                                 }
732
733                         }
734
735                         /* Check last variable is count, LEFT pos */
736                         if (num_args < noti->num_format_args) {
737                                 snprintf(buf_key, sizeof(buf_key), "%dtype%d",
738                                          check_type, num_args);
739                                 bundle_get_str(b, buf_key, &ret_val);
740                                 if (ret_val != NULL)
741                                         ret_var_type = atoi(ret_val);
742
743                                 if (ret_var_type ==
744                                     NOTIFICATION_VARIABLE_TYPE_COUNT) {
745                                         /* Get var Value */
746                                         snprintf(buf_key, sizeof(buf_key),
747                                                  "%dvalue%d", check_type,
748                                                  num_args);
749                                         bundle_get_str(b, buf_key, &ret_val);
750                                         if (ret_val != NULL)
751                                                 ret_variable_int = atoi(ret_val);
752
753                                         if (ret_variable_int ==
754                                             NOTIFICATION_COUNT_POS_RIGHT) {
755                                                 notification_get_count(noti->type,
756                                                                 noti->caller_pkgname,
757                                                                 noti->group_id,
758                                                                 noti->priv_id,
759                                                                 &ret_variable_int);
760                                                 snprintf(buf_str,
761                                                          sizeof(buf_str), " %d",
762                                                          ret_variable_int);
763
764                                                 src_len = strlen(result_str);
765                                                 max_len = NOTI_TEXT_RESULT_LEN - src_len - 1;
766
767                                                 strncat(result_str, buf_str, max_len);
768
769                                                 num_args++;
770                                         }
771
772                                 }
773                         }
774
775                         switch (check_type) {
776                         case NOTIFICATION_TEXT_TYPE_TITLE:
777                         case NOTIFICATION_TEXT_TYPE_GROUP_TITLE:
778                                 if (noti->temp_title != NULL)
779                                         free(noti->temp_title);
780
781                                 noti->temp_title = strdup(result_str);
782
783                                 *text = noti->temp_title;
784                                 break;
785                         case NOTIFICATION_TEXT_TYPE_CONTENT:
786                         case NOTIFICATION_TEXT_TYPE_CONTENT_FOR_DISPLAY_OPTION_IS_OFF:
787                         case NOTIFICATION_TEXT_TYPE_GROUP_CONTENT:
788                         case NOTIFICATION_TEXT_TYPE_GROUP_CONTENT_FOR_DISPLAY_OPTION_IS_OFF:
789                                 if (noti->temp_content !=
790                                     NULL)
791                                         free(noti->temp_content);
792
793                                 noti->temp_content = strdup(result_str);
794
795                                 *text = noti->temp_content;
796                                 break;
797                         default:
798                                 break;
799                         }
800
801                 }
802
803         } else {
804                 *text = NULL;
805         }
806
807         return NOTIFICATION_ERROR_NONE;
808 }
809
810 EXPORT_API int notification_set_text_domain(notification_h noti,
811                                                              const char *domain,
812                                                              const char *dir)
813 {
814         if (noti == NULL || domain == NULL || dir == NULL)
815                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
816
817         if (noti->domain)
818                 free(noti->domain);
819
820         noti->domain = strdup(domain);
821
822         if (noti->dir)
823                 free(noti->dir);
824
825         noti->dir = strdup(dir);
826
827         return NOTIFICATION_ERROR_NONE;
828 }
829
830 EXPORT_API int notification_get_text_domain(notification_h noti,
831                                                              char **domain,
832                                                              char **dir)
833 {
834         if (noti == NULL)
835                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
836
837         if (domain != NULL && noti->domain != NULL)
838                 *domain = noti->domain;
839
840         if (dir != NULL && noti->dir != NULL)
841                 *dir = noti->dir;
842
843         return NOTIFICATION_ERROR_NONE;
844 }
845
846 EXPORT_API int notification_set_time_to_text(notification_h noti, notification_text_type_e type,
847                                                                 time_t time)
848 {
849         int ret = NOTIFICATION_ERROR_NONE;
850         char buf[256] = { 0, };
851         char buf_tag[512] = { 0, };
852
853         if (noti == NULL)
854                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
855
856         if (time <= 0)
857                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
858
859         if (type <= NOTIFICATION_TEXT_TYPE_NONE
860             || type >= NOTIFICATION_TEXT_TYPE_MAX)
861                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
862
863
864         snprintf(buf, sizeof(buf), "%lu", time);
865         ret = notification_noti_set_tag(TAG_TIME, buf, buf_tag, sizeof(buf_tag));
866
867         if (ret != NOTIFICATION_ERROR_NONE)
868                 return ret;
869
870         return notification_set_text(noti, type, buf_tag, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
871 }
872
873 EXPORT_API int notification_get_time_from_text(notification_h noti, notification_text_type_e type,
874                                                                 time_t *time)
875 {
876         int ret = NOTIFICATION_ERROR_NONE;
877         char *ret_text = NULL;
878         char *tag_value;
879
880         if (noti == NULL)
881                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
882
883         if (time == NULL)
884                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
885
886         if (type <= NOTIFICATION_TEXT_TYPE_NONE
887             || type >= NOTIFICATION_TEXT_TYPE_MAX)
888                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
889
890         ret = notification_get_text(noti, type, &ret_text);
891
892         if (ret != NOTIFICATION_ERROR_NONE || ret_text == NULL)
893                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
894
895         if (notification_noti_get_tag_type(ret_text) == TAG_TYPE_INVALID)
896                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
897
898         tag_value = notification_noti_strip_tag(ret_text);
899         if (tag_value == NULL)
900                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
901
902         *time = atol(tag_value);
903         free(tag_value);
904
905         return NOTIFICATION_ERROR_NONE;
906 }
907
908 EXPORT_API int notification_set_sound(notification_h noti,
909                                                        notification_sound_type_e type,
910                                                        const char *path)
911 {
912         if (noti == NULL)
913                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
914
915         if (type < NOTIFICATION_SOUND_TYPE_NONE
916             || type >= NOTIFICATION_SOUND_TYPE_MAX)
917                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
918
919         noti->sound_type = type;
920
921         /* Save sound path if user data type */
922         if (type == NOTIFICATION_SOUND_TYPE_USER_DATA && path != NULL) {
923                 if (noti->sound_path != NULL)
924                         free(noti->sound_path);
925
926                 noti->sound_path = strdup(path);
927         } else {
928                 if (noti->sound_path != NULL) {
929                         free(noti->sound_path);
930                         noti->sound_path = NULL;
931                 }
932                 if (type == NOTIFICATION_SOUND_TYPE_USER_DATA) {
933                         noti->sound_type = NOTIFICATION_SOUND_TYPE_DEFAULT;
934                         return NOTIFICATION_ERROR_INVALID_PARAMETER;
935                 }
936         }
937
938         return NOTIFICATION_ERROR_NONE;
939 }
940
941 EXPORT_API int notification_get_sound(notification_h noti,
942                                                        notification_sound_type_e *type,
943                                                        const char **path)
944 {
945         if (noti == NULL || type == NULL)
946                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
947
948         *type = noti->sound_type;
949
950         /* Set sound path if user data type */
951         if (noti->sound_type == NOTIFICATION_SOUND_TYPE_USER_DATA
952             && path != NULL)
953                 *path = noti->sound_path;
954
955         return NOTIFICATION_ERROR_NONE;
956 }
957
958 EXPORT_API int notification_set_vibration(notification_h noti,
959                                                            notification_vibration_type_e type,
960                                                            const char *path)
961 {
962         if (noti == NULL)
963                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
964
965         if (type < NOTIFICATION_VIBRATION_TYPE_NONE
966             || type >= NOTIFICATION_VIBRATION_TYPE_MAX)
967                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
968
969         noti->vibration_type = type;
970
971         /* Save sound path if user data type */
972         if (type == NOTIFICATION_VIBRATION_TYPE_USER_DATA && path != NULL) {
973                 if (noti->vibration_path != NULL)
974                         free(noti->vibration_path);
975
976                 noti->vibration_path = strdup(path);
977         } else {
978                 if (noti->vibration_path != NULL) {
979                         free(noti->vibration_path);
980                         noti->vibration_path = NULL;
981                 }
982                 if (type == NOTIFICATION_VIBRATION_TYPE_USER_DATA) {
983                         noti->vibration_type = NOTIFICATION_VIBRATION_TYPE_DEFAULT;
984                         return NOTIFICATION_ERROR_INVALID_PARAMETER;
985                 }
986         }
987
988         return NOTIFICATION_ERROR_NONE;
989
990 }
991
992 EXPORT_API int notification_get_vibration(notification_h noti,
993                                                            notification_vibration_type_e *type,
994                                                            const char **path)
995 {
996         if (noti == NULL || type == NULL)
997                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
998
999         *type = noti->vibration_type;
1000
1001         /* Set sound path if user data type */
1002         if (noti->vibration_type == NOTIFICATION_VIBRATION_TYPE_USER_DATA
1003             && path != NULL)
1004                 *path = noti->vibration_path;
1005
1006         return NOTIFICATION_ERROR_NONE;
1007 }
1008
1009 EXPORT_API int notification_set_led(notification_h noti,
1010                                                            notification_led_op_e operation,
1011                                                            int led_argb)
1012 {
1013         if (noti == NULL)
1014                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1015
1016         if (operation < NOTIFICATION_LED_OP_OFF
1017             || operation >= NOTIFICATION_LED_OP_MAX)
1018                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1019
1020         noti->led_operation = operation;
1021
1022         /* Save led argb if operation is turning on LED with custom color */
1023         if (operation == NOTIFICATION_LED_OP_ON_CUSTOM_COLOR)
1024                 noti->led_argb = led_argb;
1025
1026         return NOTIFICATION_ERROR_NONE;
1027 }
1028
1029 EXPORT_API int notification_get_led(notification_h noti,
1030                                                            notification_led_op_e *operation,
1031                                                            int *led_argb)
1032 {
1033         if (noti == NULL || operation == NULL)
1034                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1035
1036         *operation = noti->led_operation;
1037
1038         /* Save led argb if operation is turning on LED with custom color */
1039         if (noti->led_operation == NOTIFICATION_LED_OP_ON_CUSTOM_COLOR
1040             && led_argb != NULL)
1041                 *led_argb = noti->led_argb;
1042
1043         return NOTIFICATION_ERROR_NONE;
1044 }
1045
1046 EXPORT_API int notification_set_led_time_period(notification_h noti,
1047                                                                         int on_ms, int off_ms)
1048 {
1049         if (noti == NULL || on_ms < 0 || off_ms < 0)
1050                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1051
1052         noti->led_on_ms = on_ms;
1053         noti->led_off_ms = off_ms;
1054
1055         return NOTIFICATION_ERROR_NONE;
1056 }
1057
1058 EXPORT_API int notification_get_led_time_period(notification_h noti,
1059                                                                         int *on_ms, int *off_ms)
1060 {
1061         if (noti == NULL)
1062                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1063
1064         if (on_ms)
1065                 *(on_ms) = noti->led_on_ms;
1066         if (off_ms)
1067                 *(off_ms) = noti->led_off_ms;
1068
1069         return NOTIFICATION_ERROR_NONE;
1070 }
1071
1072 EXPORT_API int notification_set_launch_option(notification_h noti,
1073                                                                 notification_launch_option_type type, void *option)
1074 {
1075         int err = NOTIFICATION_ERROR_NONE;
1076         int ret = 0;
1077         bundle *b = NULL;
1078         app_control_h app_control = option;
1079
1080         if (noti == NULL || app_control == NULL || type != NOTIFICATION_LAUNCH_OPTION_APP_CONTROL) {
1081                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
1082                 goto out;
1083         }
1084
1085         if ((ret = app_control_export_as_bundle(app_control, &b)) != APP_CONTROL_ERROR_NONE) {
1086                 NOTIFICATION_ERR("Failed to convert appcontrol to bundle:%d", ret);
1087                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
1088                 goto out;
1089         }
1090
1091         err = notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, b);
1092
1093 out:
1094         if (b)
1095                 bundle_free(b);
1096
1097         return err;
1098 }
1099
1100 EXPORT_API int notification_get_launch_option(notification_h noti,
1101                                                                 notification_launch_option_type type, void *option)
1102 {
1103         int ret = 0;
1104         bundle *b = NULL;
1105         app_control_h *app_control = (app_control_h *)option;
1106         app_control_h app_control_new = NULL;
1107
1108         if (noti == NULL)
1109                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1110
1111         if (app_control == NULL)
1112                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1113
1114         if (type != NOTIFICATION_LAUNCH_OPTION_APP_CONTROL)
1115                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1116
1117
1118         ret = notification_get_execute_option(noti,
1119                                 NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH,
1120                                 NULL,
1121                                 &b);
1122         if (ret == NOTIFICATION_ERROR_NONE && b != NULL) {
1123                 ret = app_control_create(&app_control_new);
1124                 if (ret == APP_CONTROL_ERROR_NONE && app_control_new != NULL) {
1125                         ret = app_control_import_from_bundle(app_control_new, b);
1126                         if (ret == APP_CONTROL_ERROR_NONE) {
1127                                 *app_control = app_control_new;
1128                         } else {
1129                                 app_control_destroy(app_control_new);
1130                                 NOTIFICATION_ERR("Failed to import app control from bundle:%d", ret);
1131                                 return NOTIFICATION_ERROR_IO_ERROR;
1132                         }
1133                 } else {
1134                         NOTIFICATION_ERR("Failed to create app control:%d", ret);
1135                         return NOTIFICATION_ERROR_IO_ERROR;
1136                 }
1137         } else {
1138                 NOTIFICATION_ERR("Failed to get execute option:%d", ret);
1139                 return ret;
1140         }
1141
1142         return NOTIFICATION_ERROR_NONE;
1143 }
1144
1145 EXPORT_API int notification_set_event_handler(notification_h noti, notification_event_type_e event_type, app_control_h event_handler)
1146 {
1147         int err = NOTIFICATION_ERROR_NONE;
1148         bundle *app_control_bundle = NULL;
1149
1150         if (noti == NULL) {
1151                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
1152                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
1153                 goto out;
1154         }
1155
1156         if (event_type < NOTIFICATION_EVENT_TYPE_CLICK_ON_BUTTON_1
1157                 || event_type > NOTIFICATION_EVENT_TYPE_CLICK_ON_THUMBNAIL) {
1158                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
1159                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
1160                 goto out;
1161         }
1162
1163         if ((err = app_control_export_as_bundle(event_handler, &app_control_bundle)) != APP_CONTROL_ERROR_NONE) {
1164                 NOTIFICATION_ERR("app_control_to_bundle failed [%d]", err);
1165                 goto out;
1166         }
1167
1168         if (noti->b_event_handler[event_type] != NULL)
1169                 bundle_free(noti->b_event_handler[event_type]);
1170
1171         noti->b_event_handler[event_type] = app_control_bundle;
1172
1173 out:
1174         return err;
1175 }
1176
1177 EXPORT_API int notification_get_event_handler(notification_h noti, notification_event_type_e event_type, app_control_h *event_handler)
1178 {
1179         int err = NOTIFICATION_ERROR_NONE;
1180         bundle *b = NULL;
1181         app_control_h app_control_new = NULL;
1182
1183         if (noti == NULL) {
1184                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
1185                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
1186                 goto out;
1187         }
1188
1189         if (event_handler == NULL) {
1190                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
1191                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
1192                 goto out;
1193         }
1194
1195         if (event_type < NOTIFICATION_EVENT_TYPE_CLICK_ON_BUTTON_1
1196                 || event_type > NOTIFICATION_EVENT_TYPE_CLICK_ON_THUMBNAIL) {
1197                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
1198                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
1199                 goto out;
1200         }
1201
1202         b = noti->b_event_handler[event_type];
1203         if (b == NULL) {
1204                 NOTIFICATION_DBG("No event handler\n");
1205                 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
1206                 goto out;
1207         }
1208
1209         err = app_control_create(&app_control_new);
1210         if (err != APP_CONTROL_ERROR_NONE || app_control_new == NULL) {
1211                 NOTIFICATION_ERR("app_control_create failed [%d]", err);
1212                 err = NOTIFICATION_ERROR_IO_ERROR;
1213                 goto out;
1214         }
1215
1216         err = app_control_import_from_bundle(app_control_new, b);
1217         if (err == APP_CONTROL_ERROR_NONE) {
1218                 *event_handler = app_control_new;
1219         } else {
1220                 app_control_destroy(app_control_new);
1221                 app_control_new = NULL;
1222                 NOTIFICATION_ERR("Failed to import app control from bundle [%d]", err);
1223                 err = NOTIFICATION_ERROR_IO_ERROR;
1224                 goto out;
1225         }
1226
1227 out:
1228         if (event_handler)
1229                 *event_handler = app_control_new;
1230
1231         return err;
1232 }
1233
1234 EXPORT_API int notification_set_property(notification_h noti,
1235                                                           int flags)
1236 {
1237         if (noti == NULL)
1238                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1239
1240         noti->flags_for_property = flags;
1241
1242         return NOTIFICATION_ERROR_NONE;
1243 }
1244
1245 EXPORT_API int notification_get_property(notification_h noti,
1246                                                           int *flags)
1247 {
1248         if (noti == NULL || flags == NULL)
1249                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1250
1251         *flags = noti->flags_for_property;
1252
1253         return NOTIFICATION_ERROR_NONE;
1254 }
1255
1256 EXPORT_API int notification_set_display_applist(notification_h noti,
1257                                                                  int applist)
1258 {
1259         if (noti == NULL)
1260                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1261
1262         if (applist == 0xffffffff) /* 0xffffffff means old NOTIFICATION_DISPLAY_APP_ALL */
1263                 applist = NOTIFICATION_DISPLAY_APP_ALL;
1264
1265         noti->display_applist = applist;
1266
1267         return NOTIFICATION_ERROR_NONE;
1268 }
1269
1270 EXPORT_API int notification_get_display_applist(notification_h noti,
1271                                                                  int *applist)
1272 {
1273         if (noti == NULL || applist == NULL)
1274                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1275
1276         *applist = noti->display_applist;
1277
1278         return NOTIFICATION_ERROR_NONE;
1279 }
1280
1281 EXPORT_API int notification_set_size(notification_h noti,
1282                                                       double size)
1283 {
1284         if (noti == NULL)
1285                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1286
1287         noti->progress_size = size;
1288
1289         return NOTIFICATION_ERROR_NONE;
1290 }
1291
1292 EXPORT_API int notification_get_size(notification_h noti,
1293                                                       double *size)
1294 {
1295         if (noti == NULL || size == NULL)
1296                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1297
1298         *size = noti->progress_size;
1299
1300         return NOTIFICATION_ERROR_NONE;
1301 }
1302
1303 EXPORT_API int notification_set_progress(notification_h noti,
1304                                                           double percentage)
1305 {
1306         if (noti == NULL)
1307                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1308
1309         noti->progress_percentage = percentage;
1310
1311         return NOTIFICATION_ERROR_NONE;
1312 }
1313
1314 EXPORT_API int notification_get_progress(notification_h noti,
1315                                                           double *percentage)
1316 {
1317         if (noti == NULL || percentage == NULL)
1318                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1319
1320         *percentage = noti->progress_percentage;
1321
1322         return NOTIFICATION_ERROR_NONE;
1323 }
1324
1325 EXPORT_API int notification_get_pkgname(notification_h noti,
1326                                                          char **pkgname)
1327 {
1328         if (noti == NULL || pkgname == NULL)
1329                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1330
1331         if (noti->caller_pkgname)
1332                 *pkgname = noti->caller_pkgname;
1333         else
1334                 *pkgname = NULL;
1335
1336         return NOTIFICATION_ERROR_NONE;
1337 }
1338
1339 EXPORT_API int notification_set_layout(notification_h noti,
1340                 notification_ly_type_e layout)
1341 {
1342         if (noti == NULL || (layout < NOTIFICATION_LY_NONE || layout >= NOTIFICATION_LY_MAX))
1343                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1344
1345         noti->layout = layout;
1346
1347         return NOTIFICATION_ERROR_NONE;
1348 }
1349
1350 EXPORT_API int notification_get_layout(notification_h noti,
1351                 notification_ly_type_e *layout)
1352 {
1353         if (noti == NULL || layout == NULL)
1354                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1355
1356         *layout = noti->layout;
1357
1358         return NOTIFICATION_ERROR_NONE;
1359 }
1360
1361
1362
1363 EXPORT_API int notification_get_type(notification_h noti,
1364                                                       notification_type_e *type)
1365 {
1366         if (noti == NULL || type == NULL)
1367                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1368
1369         *type = noti->type;
1370
1371         return NOTIFICATION_ERROR_NONE;
1372 }
1373
1374 EXPORT_API int notification_post(notification_h noti)
1375 {
1376         return notification_post_for_uid(noti, getuid());
1377 }
1378
1379 EXPORT_API int notification_update(notification_h noti)
1380 {
1381         return notification_update_for_uid(noti, getuid());
1382 }
1383
1384 EXPORT_API int notification_delete_all(notification_type_e type)
1385 {
1386         return notification_delete_all_for_uid(type, getuid());
1387 }
1388
1389 EXPORT_API int notification_delete(notification_h noti)
1390 {
1391         return notification_delete_for_uid(noti, getuid());
1392 }
1393
1394 static notification_h _notification_create(notification_type_e type)
1395 {
1396         notification_h noti = NULL;
1397         package_info_h package_info = NULL;
1398         char *app_id = NULL;
1399         char *domain_name = NULL;
1400         char *app_root_path = NULL;
1401         char locale_directory[PATH_MAX] = { 0, }; /* PATH_MAX 4096 */
1402         int err_app_manager = APP_MANAGER_ERROR_NONE;
1403
1404         if (type <= NOTIFICATION_TYPE_NONE || type >= NOTIFICATION_TYPE_MAX) {
1405                 NOTIFICATION_ERR("INVALID TYPE : %d", type);
1406                 set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
1407                 return NULL;
1408         }
1409
1410         noti = (notification_h) calloc(1, sizeof(struct _notification));
1411         if (noti == NULL) {
1412                 NOTIFICATION_ERR("NO MEMORY : noti == NULL");
1413                 set_last_result(NOTIFICATION_ERROR_OUT_OF_MEMORY);
1414                 return NULL;
1415         }
1416
1417         noti->type = type;
1418
1419         if (type == NOTIFICATION_TYPE_NOTI)
1420                 noti->layout = NOTIFICATION_LY_NOTI_EVENT_SINGLE;
1421         else if (type == NOTIFICATION_TYPE_ONGOING)
1422                 noti->layout = NOTIFICATION_LY_ONGOING_PROGRESS;
1423
1424         noti->group_id = NOTIFICATION_GROUP_ID_NONE;
1425         noti->sound_type = NOTIFICATION_SOUND_TYPE_NONE;
1426         noti->vibration_type = NOTIFICATION_VIBRATION_TYPE_NONE;
1427         noti->led_operation = NOTIFICATION_LED_OP_OFF;
1428         noti->display_applist = NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY | NOTIFICATION_DISPLAY_APP_TICKER | NOTIFICATION_DISPLAY_APP_INDICATOR;
1429         noti->auto_remove = true;
1430         noti->ongoing_flag = false;
1431
1432         if (getuid() >= REGULAR_UID_MIN) {
1433                 noti->caller_pkgname = notification_get_pkgname_by_pid();
1434
1435                 err_app_manager = app_manager_get_app_id(getpid(), &app_id);
1436                 if (err_app_manager != APP_MANAGER_ERROR_NONE || app_id == NULL) {
1437                         NOTIFICATION_WARN("app_manager_get_app_id failed err[%d] app_id[%p]",
1438                                         err_app_manager, app_id);
1439                         goto out;
1440                 }
1441
1442                 /* app name is used as domain name */
1443                 /* domain_name is allocated by app_get_package_app_name */
1444                 err_app_manager = app_get_package_app_name(app_id, &domain_name);
1445
1446                 if (err_app_manager != APP_ERROR_NONE || domain_name == NULL) {
1447                         NOTIFICATION_WARN("app_get_package_app_name failed err[%d] domain_name[%p]",
1448                                         err_app_manager, domain_name);
1449                         goto out;
1450                 }
1451
1452
1453                 err_app_manager = package_info_create(noti->caller_pkgname, &package_info);
1454
1455                 if (err_app_manager != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
1456                         NOTIFICATION_WARN("package_info_create failed err[%d] package_info[%p] caller_pkgname[%s]",
1457                                         err_app_manager, package_info, noti->caller_pkgname);
1458                         goto out;
1459                 }
1460
1461                 err_app_manager = package_info_get_root_path(package_info, &app_root_path);
1462
1463                 if (err_app_manager != PACKAGE_MANAGER_ERROR_NONE || app_root_path == NULL) {
1464                         NOTIFICATION_WARN("package_info_get_root_path failed err[%d] app_root_path[%p]",
1465                                         err_app_manager, app_root_path);
1466                         goto out;
1467                 }
1468
1469                 snprintf(locale_directory, PATH_MAX, "%s/res/locale", app_root_path);
1470
1471                 noti->domain = strdup(domain_name);
1472                 noti->dir    = strdup(locale_directory);
1473         }
1474
1475 out:
1476         if (domain_name)
1477                 free(domain_name);
1478
1479         if (app_id)
1480                 free(app_id);
1481
1482         if (app_root_path)
1483                 free(app_root_path);
1484
1485         if (package_info)
1486                 package_info_destroy(package_info);
1487
1488         /*!
1489          * \NOTE
1490          * Other fields are already initialized with ZERO.
1491          */
1492         set_last_result(NOTIFICATION_ERROR_NONE);
1493         return noti;
1494 }
1495
1496 EXPORT_API notification_h notification_create(notification_type_e type)
1497 {
1498         return _notification_create(type);
1499 }
1500
1501 EXPORT_API notification_h notification_load_by_tag(const char *tag)
1502 {
1503         return notification_load_by_tag_for_uid(tag, getuid());
1504 }
1505
1506 EXPORT_API int notification_clone(notification_h noti, notification_h *clone)
1507 {
1508         int i = 0;
1509         notification_h new_noti = NULL;
1510
1511         if (noti == NULL || clone == NULL) {
1512                 NOTIFICATION_ERR("INVALID PARAMETER.");
1513                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1514         }
1515
1516         new_noti = (notification_h) calloc(1, sizeof(struct _notification));
1517         if (new_noti == NULL) {
1518                 NOTIFICATION_ERR("NO MEMORY : noti == NULL");
1519                 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
1520         }
1521
1522         new_noti->type = noti->type;
1523         new_noti->layout = noti->layout;
1524
1525         new_noti->group_id = noti->group_id;
1526         new_noti->internal_group_id = noti->internal_group_id;
1527         new_noti->priv_id = noti->priv_id;
1528
1529         if (noti->caller_pkgname != NULL)
1530                 new_noti->caller_pkgname = strdup(noti->caller_pkgname);
1531         else
1532                 new_noti->caller_pkgname = notification_get_pkgname_by_pid();
1533
1534         if (noti->launch_pkgname != NULL)
1535                 new_noti->launch_pkgname = strdup(noti->launch_pkgname);
1536         else
1537                 new_noti->launch_pkgname = NULL;
1538
1539         if (noti->args != NULL)
1540                 new_noti->args = bundle_dup(noti->args);
1541         else
1542                 new_noti->args = NULL;
1543
1544         if (noti->group_args != NULL)
1545                 new_noti->group_args = bundle_dup(noti->group_args);
1546         else
1547                 new_noti->group_args = NULL;
1548
1549         if (noti->b_execute_option != NULL)
1550                 new_noti->b_execute_option = bundle_dup(noti->b_execute_option);
1551         else
1552                 new_noti->b_execute_option = NULL;
1553
1554         if (noti->b_service_responding != NULL)
1555                 new_noti->b_service_responding = bundle_dup(noti->b_service_responding);
1556         else
1557                 new_noti->b_service_responding = NULL;
1558
1559         if (noti->b_service_single_launch != NULL)
1560                 new_noti->b_service_single_launch = bundle_dup(noti->b_service_single_launch);
1561         else
1562                 new_noti->b_service_single_launch = NULL;
1563
1564         if (noti->b_service_multi_launch != NULL)
1565                 new_noti->b_service_multi_launch = bundle_dup(noti->b_service_multi_launch);
1566         else
1567                 new_noti->b_service_multi_launch = NULL;
1568
1569         for (i = 0; i < NOTIFICATION_EVENT_TYPE_MAX; i++) {
1570                 if (noti->b_event_handler[i] != NULL)
1571                         new_noti->b_event_handler[i] = bundle_dup(noti->b_event_handler[i]);
1572                 else
1573                         new_noti->b_event_handler[i] = NULL;
1574         }
1575
1576         new_noti->sound_type = noti->sound_type;
1577         if (noti->sound_path != NULL)
1578                 new_noti->sound_path = strdup(noti->sound_path);
1579         else
1580                 new_noti->sound_path = NULL;
1581
1582         new_noti->vibration_type = noti->vibration_type;
1583         if (noti->vibration_path != NULL)
1584                 new_noti->vibration_path = strdup(noti->vibration_path);
1585         else
1586                 new_noti->vibration_path = NULL;
1587
1588         new_noti->led_operation = noti->led_operation;
1589         new_noti->led_argb = noti->led_argb;
1590         new_noti->led_on_ms = noti->led_on_ms;
1591         new_noti->led_off_ms = noti->led_off_ms;
1592
1593         if (noti->domain != NULL)
1594                 new_noti->domain = strdup(noti->domain);
1595         else
1596                 new_noti->domain = NULL;
1597
1598         if (noti->dir != NULL)
1599                 new_noti->dir = strdup(noti->dir);
1600         else
1601                 new_noti->dir = NULL;
1602
1603         if (noti->b_text != NULL)
1604                 new_noti->b_text = bundle_dup(noti->b_text);
1605         else
1606                 new_noti->b_text = NULL;
1607
1608         if (noti->b_key != NULL)
1609                 new_noti->b_key = bundle_dup(noti->b_key);
1610         else
1611                 new_noti->b_key = NULL;
1612
1613         if (noti->tag != NULL)
1614                 new_noti->tag = strdup(noti->tag);
1615         else
1616                 new_noti->tag = NULL;
1617
1618         if (noti->b_format_args != NULL)
1619                 new_noti->b_format_args = bundle_dup(noti->b_format_args);
1620         else
1621                 new_noti->b_format_args = NULL;
1622
1623         new_noti->num_format_args = noti->num_format_args;
1624
1625         if (noti->b_image_path != NULL)
1626                 new_noti->b_image_path = bundle_dup(noti->b_image_path);
1627         else
1628                 new_noti->b_image_path = NULL;
1629
1630         new_noti->time = noti->time;
1631         new_noti->insert_time = noti->insert_time;
1632
1633         new_noti->flags_for_property = noti->flags_for_property;
1634         new_noti->display_applist = noti->display_applist;
1635
1636         new_noti->progress_size = noti->progress_size;
1637         new_noti->progress_percentage = noti->progress_percentage;
1638
1639         new_noti->ongoing_flag = noti->ongoing_flag;
1640         new_noti->auto_remove = noti->auto_remove;
1641         new_noti->uid = noti->uid;
1642
1643         new_noti->app_icon_path = NULL;
1644         new_noti->app_name = NULL;
1645         new_noti->temp_title = NULL;
1646         new_noti->temp_content = NULL;
1647
1648         *clone = new_noti;
1649
1650         return NOTIFICATION_ERROR_NONE;
1651 }
1652
1653
1654 EXPORT_API int notification_free(notification_h noti)
1655 {
1656         int i = 0;
1657         if (noti == NULL)
1658                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1659
1660         if (noti->caller_pkgname)
1661                 free(noti->caller_pkgname);
1662
1663         if (noti->launch_pkgname)
1664                 free(noti->launch_pkgname);
1665
1666         if (noti->args)
1667                 bundle_free(noti->args);
1668
1669         if (noti->group_args)
1670                 bundle_free(noti->group_args);
1671
1672         if (noti->b_execute_option)
1673                 bundle_free(noti->b_execute_option);
1674
1675         if (noti->b_service_responding)
1676                 bundle_free(noti->b_service_responding);
1677
1678         if (noti->b_service_single_launch)
1679                 bundle_free(noti->b_service_single_launch);
1680
1681         if (noti->b_service_multi_launch)
1682                 bundle_free(noti->b_service_multi_launch);
1683
1684         for (i = 0; i < NOTIFICATION_EVENT_TYPE_MAX; i++) {
1685                 if (noti->b_event_handler[i] != NULL)
1686                         bundle_free(noti->b_event_handler[i]);
1687         }
1688
1689         if (noti->sound_path)
1690                 free(noti->sound_path);
1691
1692         if (noti->vibration_path)
1693                 free(noti->vibration_path);
1694
1695         if (noti->domain)
1696                 free(noti->domain);
1697
1698         if (noti->dir)
1699                 free(noti->dir);
1700
1701         if (noti->b_text)
1702                 bundle_free(noti->b_text);
1703
1704         if (noti->b_key)
1705                 bundle_free(noti->b_key);
1706
1707         if (noti->b_format_args)
1708                 bundle_free(noti->b_format_args);
1709
1710         if (noti->b_image_path)
1711                 bundle_free(noti->b_image_path);
1712
1713         if (noti->app_icon_path)
1714                 free(noti->app_icon_path);
1715
1716         if (noti->app_name)
1717                 free(noti->app_name);
1718
1719         if (noti->temp_title)
1720                 free(noti->temp_title);
1721
1722         if (noti->temp_content)
1723                 free(noti->temp_content);
1724
1725         if (noti->tag)
1726                 free(noti->tag);
1727
1728         free(noti);
1729
1730         return NOTIFICATION_ERROR_NONE;
1731 }
1732
1733 EXPORT_API int notification_set_tag(notification_h noti, const char *tag)
1734 {
1735         if (noti == NULL)
1736                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1737
1738         if (tag != NULL) {
1739                 if (noti->tag != NULL)
1740                         free(noti->tag);
1741
1742                 noti->tag = strdup(tag);
1743         }
1744
1745         return NOTIFICATION_ERROR_NONE;
1746
1747 }
1748
1749 EXPORT_API int notification_get_tag(notification_h noti, const char **tag)
1750 {
1751         if (noti == NULL)
1752                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1753
1754         *tag = noti->tag;
1755         return NOTIFICATION_ERROR_NONE;
1756 }
1757
1758 /* LCOV_EXCL_START */
1759 void notification_call_posted_toast_cb(const char *message)
1760 {
1761         if (posted_toast_message_cb != NULL)
1762                 posted_toast_message_cb((void *)message);
1763 }
1764 /* LCOV_EXCL_STOP */
1765
1766 EXPORT_API int notification_set_ongoing_flag(notification_h noti, bool ongoing_flag)
1767 {
1768         if (noti == NULL)
1769                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1770
1771         noti->ongoing_flag = ongoing_flag;
1772
1773         return NOTIFICATION_ERROR_NONE;
1774 }
1775
1776 EXPORT_API int notification_get_ongoing_flag(notification_h noti, bool *ongoing_flag)
1777 {
1778         if (noti == NULL || ongoing_flag == NULL)
1779                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1780
1781         *ongoing_flag = noti->ongoing_flag;
1782
1783         return NOTIFICATION_ERROR_NONE;
1784 }
1785
1786
1787 EXPORT_API int notification_add_button(notification_h noti, notification_button_index_e button_index)
1788 {
1789         if (noti == NULL || button_index < NOTIFICATION_BUTTON_1 || button_index > NOTIFICATION_BUTTON_6)
1790                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1791
1792         return NOTIFICATION_ERROR_NONE;
1793 }
1794
1795 EXPORT_API int notification_remove_button(notification_h noti, notification_button_index_e button_index)
1796 {
1797         if (noti == NULL || button_index < NOTIFICATION_BUTTON_1 || button_index > NOTIFICATION_BUTTON_6)
1798                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1799
1800         if (noti->b_event_handler[button_index - 1]) {
1801                 bundle_free(noti->b_event_handler[button_index - 1]);
1802                 noti->b_event_handler[button_index - 1] = NULL;
1803         }
1804
1805         return NOTIFICATION_ERROR_NONE;
1806 }
1807
1808 EXPORT_API int notification_set_auto_remove(notification_h noti, bool auto_remove)
1809 {
1810         if (noti == NULL)
1811                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1812
1813         noti->auto_remove = auto_remove;
1814
1815         return NOTIFICATION_ERROR_NONE;
1816 }
1817
1818 EXPORT_API int notification_get_auto_remove(notification_h noti, bool *auto_remove)
1819 {
1820         if (noti == NULL || auto_remove == NULL)
1821                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1822
1823         *auto_remove = noti->auto_remove;
1824
1825         return NOTIFICATION_ERROR_NONE;
1826 }
1827
1828 EXPORT_API int notification_save_as_template(notification_h noti, const char *template_name)
1829 {
1830         if (noti == NULL || template_name == NULL) {
1831                 NOTIFICATION_ERR("Invalid parameter");
1832                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1833         }
1834
1835         return notification_ipc_request_save_as_template(noti, template_name);
1836 }
1837
1838 EXPORT_API notification_h notification_create_from_template(const char *template_name)
1839 {
1840         int ret = 0;
1841         notification_h noti = NULL;
1842
1843         if (template_name == NULL) {
1844                 NOTIFICATION_ERR("Invalid parameter");
1845                 set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
1846                 return NULL;
1847         }
1848
1849         noti = (notification_h)calloc(1, sizeof(struct _notification));
1850         if (noti == NULL) {
1851                 NOTIFICATION_ERR("Failed to alloc a new notification");
1852                 set_last_result(NOTIFICATION_ERROR_OUT_OF_MEMORY);
1853                 return NULL;
1854         }
1855
1856         ret = notification_ipc_request_create_from_template(noti, template_name);
1857
1858         set_last_result(ret);
1859         if (ret != NOTIFICATION_ERROR_NONE) {
1860                 notification_free(noti);
1861                 return NULL;
1862         }
1863
1864         return noti;
1865 }
1866
1867 EXPORT_API int notification_get_noti_block_state(notification_block_state_e *state)
1868 {
1869         int ret;
1870         char *pkgname;
1871         int do_not_disturb;
1872         int do_not_disturb_except;
1873         int allow_to_notify;
1874
1875         if (state == NULL)
1876                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1877
1878         pkgname = notification_get_pkgname_by_pid();
1879
1880         ret = notification_ipc_get_noti_block_state(pkgname, &do_not_disturb, &do_not_disturb_except, &allow_to_notify, getuid());
1881
1882         if (ret != NOTIFICATION_ERROR_NONE) {
1883                 if (pkgname)
1884                         free(pkgname);
1885                 return ret;
1886         }
1887
1888         if (allow_to_notify) {
1889                 *state = NOTIFICATION_BLOCK_STATE_ALLOWED;
1890                 if (do_not_disturb && !do_not_disturb_except)
1891                         *state = NOTIFICATION_BLOCK_STATE_DO_NOT_DISTURB;
1892         } else {
1893                 *state = NOTIFICATION_BLOCK_STATE_BLOCKED;
1894         }
1895
1896         if (pkgname)
1897                 free(pkgname);
1898
1899         return ret;
1900 }