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