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