Check uid in notification_create func
[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
1235
1236
1237
1238 EXPORT_API int notification_set_property(notification_h noti,
1239                                                           int flags)
1240 {
1241         if (noti == NULL)
1242                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1243
1244         noti->flags_for_property = flags;
1245
1246         return NOTIFICATION_ERROR_NONE;
1247 }
1248
1249 EXPORT_API int notification_get_property(notification_h noti,
1250                                                           int *flags)
1251 {
1252         if (noti == NULL || flags == NULL)
1253                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1254
1255         *flags = noti->flags_for_property;
1256
1257         return NOTIFICATION_ERROR_NONE;
1258 }
1259
1260 EXPORT_API int notification_set_display_applist(notification_h noti,
1261                                                                  int applist)
1262 {
1263         if (noti == NULL)
1264                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1265
1266         if (applist == 0xffffffff) /* 0xffffffff means old NOTIFICATION_DISPLAY_APP_ALL */
1267                 applist = NOTIFICATION_DISPLAY_APP_ALL;
1268
1269         noti->display_applist = applist;
1270
1271         return NOTIFICATION_ERROR_NONE;
1272 }
1273
1274 EXPORT_API int notification_get_display_applist(notification_h noti,
1275                                                                  int *applist)
1276 {
1277         if (noti == NULL || applist == NULL)
1278                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1279
1280         *applist = noti->display_applist;
1281
1282         return NOTIFICATION_ERROR_NONE;
1283 }
1284
1285 EXPORT_API int notification_set_size(notification_h noti,
1286                                                       double size)
1287 {
1288         if (noti == NULL)
1289                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1290
1291         noti->progress_size = size;
1292
1293         return NOTIFICATION_ERROR_NONE;
1294 }
1295
1296 EXPORT_API int notification_get_size(notification_h noti,
1297                                                       double *size)
1298 {
1299         if (noti == NULL || size == NULL)
1300                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1301
1302         *size = noti->progress_size;
1303
1304         return NOTIFICATION_ERROR_NONE;
1305 }
1306
1307 EXPORT_API int notification_set_progress(notification_h noti,
1308                                                           double percentage)
1309 {
1310         if (noti == NULL)
1311                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1312
1313         noti->progress_percentage = percentage;
1314
1315         return NOTIFICATION_ERROR_NONE;
1316 }
1317
1318 EXPORT_API int notification_get_progress(notification_h noti,
1319                                                           double *percentage)
1320 {
1321         if (noti == NULL || percentage == NULL)
1322                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1323
1324         *percentage = noti->progress_percentage;
1325
1326         return NOTIFICATION_ERROR_NONE;
1327 }
1328
1329 EXPORT_API int notification_get_pkgname(notification_h noti,
1330                                                          char **pkgname)
1331 {
1332         if (noti == NULL || pkgname == NULL)
1333                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1334
1335         if (noti->caller_pkgname)
1336                 *pkgname = noti->caller_pkgname;
1337         else
1338                 *pkgname = NULL;
1339
1340         return NOTIFICATION_ERROR_NONE;
1341 }
1342
1343 EXPORT_API int notification_set_layout(notification_h noti,
1344                 notification_ly_type_e layout)
1345 {
1346         if (noti == NULL || (layout < NOTIFICATION_LY_NONE || layout >= NOTIFICATION_LY_MAX))
1347                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1348
1349         noti->layout = layout;
1350
1351         return NOTIFICATION_ERROR_NONE;
1352 }
1353
1354 EXPORT_API int notification_get_layout(notification_h noti,
1355                 notification_ly_type_e *layout)
1356 {
1357         if (noti == NULL || layout == NULL)
1358                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1359
1360         *layout = noti->layout;
1361
1362         return NOTIFICATION_ERROR_NONE;
1363 }
1364
1365
1366
1367 EXPORT_API int notification_get_type(notification_h noti,
1368                                                       notification_type_e *type)
1369 {
1370         if (noti == NULL || type == NULL)
1371                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1372
1373         *type = noti->type;
1374
1375         return NOTIFICATION_ERROR_NONE;
1376 }
1377
1378 EXPORT_API int notification_post(notification_h noti)
1379 {
1380         return notification_post_for_uid(noti, getuid());
1381 }
1382
1383 EXPORT_API int notification_update(notification_h noti)
1384 {
1385         return notification_update_for_uid(noti, getuid());
1386 }
1387
1388 EXPORT_API int notification_delete_all(notification_type_e type)
1389 {
1390         return notification_delete_all_for_uid(type, getuid());
1391 }
1392
1393 EXPORT_API int notification_delete(notification_h noti)
1394 {
1395         return notification_delete_for_uid(noti, getuid());
1396 }
1397
1398 static notification_h _notification_create(notification_type_e type)
1399 {
1400         notification_h noti = NULL;
1401         package_info_h package_info = NULL;
1402         char *app_id = NULL;
1403         char *domain_name = NULL;
1404         char *app_root_path = NULL;
1405         char locale_directory[PATH_MAX] = { 0, }; /* PATH_MAX 4096 */
1406         int err_app_manager = APP_MANAGER_ERROR_NONE;
1407
1408         if (type <= NOTIFICATION_TYPE_NONE || type >= NOTIFICATION_TYPE_MAX) {
1409                 NOTIFICATION_ERR("INVALID TYPE : %d", type);
1410                 set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
1411                 return NULL;
1412         }
1413
1414         noti = (notification_h) calloc(1, sizeof(struct _notification));
1415         if (noti == NULL) {
1416                 NOTIFICATION_ERR("NO MEMORY : noti == NULL");
1417                 set_last_result(NOTIFICATION_ERROR_OUT_OF_MEMORY);
1418                 return NULL;
1419         }
1420
1421         noti->type = type;
1422
1423         if (type == NOTIFICATION_TYPE_NOTI)
1424                 noti->layout = NOTIFICATION_LY_NOTI_EVENT_SINGLE;
1425         else if (type == NOTIFICATION_TYPE_ONGOING)
1426                 noti->layout = NOTIFICATION_LY_ONGOING_PROGRESS;
1427
1428         noti->group_id = NOTIFICATION_GROUP_ID_NONE;
1429         noti->sound_type = NOTIFICATION_SOUND_TYPE_NONE;
1430         noti->vibration_type = NOTIFICATION_VIBRATION_TYPE_NONE;
1431         noti->led_operation = NOTIFICATION_LED_OP_OFF;
1432         noti->display_applist = NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY | NOTIFICATION_DISPLAY_APP_TICKER | NOTIFICATION_DISPLAY_APP_INDICATOR;
1433         noti->auto_remove = true;
1434         noti->ongoing_flag = false;
1435
1436         if (getuid() >= REGULAR_UID_MIN) {
1437                 noti->caller_pkgname = notification_get_pkgname_by_pid();
1438
1439                 err_app_manager = app_manager_get_app_id(getpid(), &app_id);
1440                 if (err_app_manager != APP_MANAGER_ERROR_NONE || app_id == NULL) {
1441                         NOTIFICATION_WARN("app_manager_get_app_id failed err[%d] app_id[%p]",
1442                                         err_app_manager, app_id);
1443                         goto out;
1444                 }
1445
1446                 /* app name is used as domain name */
1447                 /* domain_name is allocated by app_get_package_app_name */
1448                 err_app_manager = app_get_package_app_name(app_id, &domain_name);
1449
1450                 if (err_app_manager != APP_ERROR_NONE || domain_name == NULL) {
1451                         NOTIFICATION_WARN("app_get_package_app_name failed err[%d] domain_name[%p]",
1452                                         err_app_manager, domain_name);
1453                         goto out;
1454                 }
1455
1456
1457                 err_app_manager = package_info_create(noti->caller_pkgname, &package_info);
1458
1459                 if (err_app_manager != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
1460                         NOTIFICATION_WARN("package_info_create failed err[%d] package_info[%p] caller_pkgname[%s]",
1461                                         err_app_manager, package_info, noti->caller_pkgname);
1462                         goto out;
1463                 }
1464
1465                 err_app_manager = package_info_get_root_path(package_info, &app_root_path);
1466
1467                 if (err_app_manager != PACKAGE_MANAGER_ERROR_NONE || app_root_path == NULL) {
1468                         NOTIFICATION_WARN("package_info_get_root_path failed err[%d] app_root_path[%p]",
1469                                         err_app_manager, app_root_path);
1470                         goto out;
1471                 }
1472
1473                 snprintf(locale_directory, PATH_MAX, "%s/res/locale", app_root_path);
1474
1475                 noti->domain = strdup(domain_name);
1476                 noti->dir    = strdup(locale_directory);
1477         }
1478
1479 out:
1480         if (domain_name)
1481                 free(domain_name);
1482
1483         if (app_id)
1484                 free(app_id);
1485
1486         if (app_root_path)
1487                 free(app_root_path);
1488
1489         if (package_info)
1490                 package_info_destroy(package_info);
1491
1492         /*!
1493          * \NOTE
1494          * Other fields are already initialized with ZERO.
1495          */
1496         set_last_result(NOTIFICATION_ERROR_NONE);
1497         return noti;
1498 }
1499
1500 EXPORT_API notification_h notification_create(notification_type_e type)
1501 {
1502         return _notification_create(type);
1503 }
1504
1505 EXPORT_API notification_h  notification_load_by_tag(const char *tag)
1506 {
1507         return notification_load_by_tag_for_uid(tag, getuid());
1508 }
1509
1510 EXPORT_API int notification_clone(notification_h noti, notification_h *clone)
1511 {
1512         int i = 0;
1513         notification_h new_noti = NULL;
1514
1515         if (noti == NULL || clone == NULL) {
1516                 NOTIFICATION_ERR("INVALID PARAMETER.");
1517                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1518         }
1519
1520         new_noti = (notification_h) calloc(1, sizeof(struct _notification));
1521         if (new_noti == NULL) {
1522                 NOTIFICATION_ERR("NO MEMORY : noti == NULL");
1523                 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
1524         }
1525
1526         new_noti->type = noti->type;
1527         new_noti->layout = noti->layout;
1528
1529         new_noti->group_id = noti->group_id;
1530         new_noti->internal_group_id = noti->internal_group_id;
1531         new_noti->priv_id = noti->priv_id;
1532
1533         if (noti->caller_pkgname != NULL)
1534                 new_noti->caller_pkgname = strdup(noti->caller_pkgname);
1535         else
1536                 new_noti->caller_pkgname = notification_get_pkgname_by_pid();
1537
1538         if (noti->launch_pkgname != NULL)
1539                 new_noti->launch_pkgname = strdup(noti->launch_pkgname);
1540         else
1541                 new_noti->launch_pkgname = NULL;
1542
1543         if (noti->args != NULL)
1544                 new_noti->args = bundle_dup(noti->args);
1545         else
1546                 new_noti->args = NULL;
1547
1548         if (noti->group_args != NULL)
1549                 new_noti->group_args = bundle_dup(noti->group_args);
1550         else
1551                 new_noti->group_args = NULL;
1552
1553         if (noti->b_execute_option != NULL)
1554                 new_noti->b_execute_option = bundle_dup(noti->b_execute_option);
1555         else
1556                 new_noti->b_execute_option = NULL;
1557
1558         if (noti->b_service_responding != NULL)
1559                 new_noti->b_service_responding = bundle_dup(noti->b_service_responding);
1560         else
1561                 new_noti->b_service_responding = NULL;
1562
1563         if (noti->b_service_single_launch != NULL)
1564                 new_noti->b_service_single_launch = bundle_dup(noti->b_service_single_launch);
1565         else
1566                 new_noti->b_service_single_launch = NULL;
1567
1568         if (noti->b_service_multi_launch != NULL)
1569                 new_noti->b_service_multi_launch = bundle_dup(noti->b_service_multi_launch);
1570         else
1571                 new_noti->b_service_multi_launch = NULL;
1572
1573         for (i = 0; i < NOTIFICATION_EVENT_TYPE_MAX; i++) {
1574                 if (noti->b_event_handler[i] != NULL)
1575                         new_noti->b_event_handler[i] = bundle_dup(noti->b_event_handler[i]);
1576                 else
1577                         new_noti->b_event_handler[i] = NULL;
1578         }
1579
1580         new_noti->sound_type = noti->sound_type;
1581         if (noti->sound_path != NULL)
1582                 new_noti->sound_path = strdup(noti->sound_path);
1583         else
1584                 new_noti->sound_path = NULL;
1585
1586         new_noti->vibration_type = noti->vibration_type;
1587         if (noti->vibration_path != NULL)
1588                 new_noti->vibration_path = strdup(noti->vibration_path);
1589         else
1590                 new_noti->vibration_path = NULL;
1591
1592         new_noti->led_operation = noti->led_operation;
1593         new_noti->led_argb = noti->led_argb;
1594         new_noti->led_on_ms = noti->led_on_ms;
1595         new_noti->led_off_ms = noti->led_off_ms;
1596
1597         if (noti->domain != NULL)
1598                 new_noti->domain = strdup(noti->domain);
1599         else
1600                 new_noti->domain = NULL;
1601
1602         if (noti->dir != NULL)
1603                 new_noti->dir = strdup(noti->dir);
1604         else
1605                 new_noti->dir = NULL;
1606
1607         if (noti->b_text != NULL)
1608                 new_noti->b_text = bundle_dup(noti->b_text);
1609         else
1610                 new_noti->b_text = NULL;
1611
1612         if (noti->b_key != NULL)
1613                 new_noti->b_key = bundle_dup(noti->b_key);
1614         else
1615                 new_noti->b_key = NULL;
1616
1617         if (noti->tag != NULL)
1618                 new_noti->tag = strdup(noti->tag);
1619         else
1620                 new_noti->tag = NULL;
1621
1622         if (noti->b_format_args != NULL)
1623                 new_noti->b_format_args = bundle_dup(noti->b_format_args);
1624         else
1625                 new_noti->b_format_args = NULL;
1626
1627         new_noti->num_format_args = noti->num_format_args;
1628
1629         if (noti->b_image_path != NULL)
1630                 new_noti->b_image_path = bundle_dup(noti->b_image_path);
1631         else
1632                 new_noti->b_image_path = NULL;
1633
1634         new_noti->time = noti->time;
1635         new_noti->insert_time = noti->insert_time;
1636
1637         new_noti->flags_for_property = noti->flags_for_property;
1638         new_noti->display_applist = noti->display_applist;
1639
1640         new_noti->progress_size = noti->progress_size;
1641         new_noti->progress_percentage = noti->progress_percentage;
1642
1643         new_noti->ongoing_flag = noti->ongoing_flag;
1644         new_noti->auto_remove = noti->auto_remove;
1645         new_noti->uid = noti->uid;
1646
1647         new_noti->app_icon_path = NULL;
1648         new_noti->app_name = NULL;
1649         new_noti->temp_title = NULL;
1650         new_noti->temp_content = NULL;
1651
1652         *clone = new_noti;
1653
1654         return NOTIFICATION_ERROR_NONE;
1655 }
1656
1657
1658 EXPORT_API int notification_free(notification_h noti)
1659 {
1660         int i = 0;
1661         if (noti == NULL)
1662                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1663
1664         if (noti->caller_pkgname)
1665                 free(noti->caller_pkgname);
1666
1667         if (noti->launch_pkgname)
1668                 free(noti->launch_pkgname);
1669
1670         if (noti->args)
1671                 bundle_free(noti->args);
1672
1673         if (noti->group_args)
1674                 bundle_free(noti->group_args);
1675
1676         if (noti->b_execute_option)
1677                 bundle_free(noti->b_execute_option);
1678
1679         if (noti->b_service_responding)
1680                 bundle_free(noti->b_service_responding);
1681
1682         if (noti->b_service_single_launch)
1683                 bundle_free(noti->b_service_single_launch);
1684
1685         if (noti->b_service_multi_launch)
1686                 bundle_free(noti->b_service_multi_launch);
1687
1688         for (i = 0; i < NOTIFICATION_EVENT_TYPE_MAX; i++) {
1689                 if (noti->b_event_handler[i] != NULL)
1690                         bundle_free(noti->b_event_handler[i]);
1691         }
1692
1693         if (noti->sound_path)
1694                 free(noti->sound_path);
1695
1696         if (noti->vibration_path)
1697                 free(noti->vibration_path);
1698
1699         if (noti->domain)
1700                 free(noti->domain);
1701
1702         if (noti->dir)
1703                 free(noti->dir);
1704
1705         if (noti->b_text)
1706                 bundle_free(noti->b_text);
1707
1708         if (noti->b_key)
1709                 bundle_free(noti->b_key);
1710
1711         if (noti->b_format_args)
1712                 bundle_free(noti->b_format_args);
1713
1714         if (noti->b_image_path)
1715                 bundle_free(noti->b_image_path);
1716
1717         if (noti->app_icon_path)
1718                 free(noti->app_icon_path);
1719
1720         if (noti->app_name)
1721                 free(noti->app_name);
1722
1723         if (noti->temp_title)
1724                 free(noti->temp_title);
1725
1726         if (noti->temp_content)
1727                 free(noti->temp_content);
1728
1729         if (noti->tag)
1730                 free(noti->tag);
1731
1732         free(noti);
1733
1734         return NOTIFICATION_ERROR_NONE;
1735 }
1736
1737 EXPORT_API int notification_set_tag(notification_h noti, const char *tag)
1738 {
1739         if (noti == NULL)
1740                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1741
1742         if (tag != NULL) {
1743                 if (noti->tag != NULL)
1744                         free(noti->tag);
1745
1746                 noti->tag = strdup(tag);
1747         }
1748
1749         return NOTIFICATION_ERROR_NONE;
1750
1751 }
1752
1753 EXPORT_API int notification_get_tag(notification_h noti, const char **tag)
1754 {
1755         if (noti == NULL)
1756                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1757
1758         *tag = noti->tag;
1759         return NOTIFICATION_ERROR_NONE;
1760 }
1761
1762 /* LCOV_EXCL_START */
1763 void notification_call_posted_toast_cb(const char *message)
1764 {
1765         if (posted_toast_message_cb != NULL)
1766                 posted_toast_message_cb((void *)message);
1767 }
1768 /* LCOV_EXCL_STOP */
1769
1770 EXPORT_API int notification_set_ongoing_flag(notification_h noti, bool ongoing_flag)
1771 {
1772         if (noti == NULL)
1773                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1774
1775         noti->ongoing_flag = ongoing_flag;
1776
1777         return NOTIFICATION_ERROR_NONE;
1778 }
1779
1780 EXPORT_API int notification_get_ongoing_flag(notification_h noti, bool *ongoing_flag)
1781 {
1782         if (noti == NULL || ongoing_flag == NULL)
1783                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1784
1785         *ongoing_flag = noti->ongoing_flag;
1786
1787         return NOTIFICATION_ERROR_NONE;
1788 }
1789
1790
1791 EXPORT_API int notification_add_button(notification_h noti, notification_button_index_e button_index)
1792 {
1793         if (noti == NULL || button_index < NOTIFICATION_BUTTON_1 || button_index > NOTIFICATION_BUTTON_6)
1794                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1795
1796         return NOTIFICATION_ERROR_NONE;
1797 }
1798
1799 EXPORT_API int notification_remove_button(notification_h noti, notification_button_index_e button_index)
1800 {
1801         if (noti == NULL || button_index < NOTIFICATION_BUTTON_1 || button_index > NOTIFICATION_BUTTON_6)
1802                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1803
1804         if (noti->b_event_handler[button_index - 1]) {
1805                 bundle_free(noti->b_event_handler[button_index - 1]);
1806                 noti->b_event_handler[button_index - 1] = NULL;
1807         }
1808
1809         return NOTIFICATION_ERROR_NONE;
1810 }
1811
1812 EXPORT_API int notification_set_auto_remove(notification_h noti, bool auto_remove)
1813 {
1814         if (noti == NULL)
1815                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1816
1817         noti->auto_remove = auto_remove;
1818
1819         return NOTIFICATION_ERROR_NONE;
1820 }
1821
1822 EXPORT_API int notification_get_auto_remove(notification_h noti, bool *auto_remove)
1823 {
1824         if (noti == NULL || auto_remove == NULL)
1825                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1826
1827         *auto_remove = noti->auto_remove;
1828
1829         return NOTIFICATION_ERROR_NONE;
1830 }
1831