changed operation string
[framework/api/application.git] / src / ui_notification.c
1 /*
2  * Copyright (c) 2011 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
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdarg.h>
22 #include <sys/stat.h>
23
24 #include <dlog.h>
25 #include <notification.h>
26
27 #include <app.h>
28 #include <app_service_private.h>
29
30 #ifdef LOG_TAG
31 #undef LOG_TAG
32 #endif
33
34 #define LOG_TAG "TIZEN_N_UI_NOTIFICATION"
35
36 struct ui_notification_s {
37         notification_h raw_handle;
38         bool ongoing;
39         bool posted;
40         bool removed;
41         char *icon;
42         struct tm *time;
43         char *title;
44         char *content;
45         service_h service;
46         char *sound;
47         bool vibration;
48 };
49
50 static int ui_notification_error_handler(int error, const char *func, const char *on_error)
51 {
52         int retcode;
53         char *error_msg;
54
55         switch (error)
56         {
57         case NOTIFICATION_ERROR_NONE:
58                 retcode = UI_NOTIFICATION_ERROR_NONE;
59                 break;
60
61         case NOTIFICATION_ERROR_INVALID_DATA:
62                 retcode = UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
63                 error_msg = "INVALID_PARAMETER";
64                 break;
65
66         case NOTIFICATION_ERROR_NO_MEMORY:
67                 retcode = UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
68                 error_msg = "OUT_OF_MEMORY";
69                 break;
70
71         case NOTIFICATION_ERROR_FROM_DB:
72                 retcode = UI_NOTIFICATION_ERROR_DB_FAILED;
73                 error_msg = "DB_FAILED";
74                 break;
75
76         case NOTIFICATION_ERROR_ALREADY_EXIST_ID:
77         case NOTIFICATION_ERROR_NOT_EXIST_ID:
78                 retcode = UI_NOTIFICATION_ERROR_INVALID_STATE;
79                 error_msg = "INVALID_STATE";
80                 break;
81
82         default:
83                 retcode = UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
84                 error_msg = "INVALID_PARAMETER";
85         }
86
87         if (retcode != UI_NOTIFICATION_ERROR_NONE)
88         {
89                 LOGE("[%s] %s(0x%08x) : %s", func, error_msg, retcode, on_error);
90         }
91
92         return retcode;
93 }
94
95
96 int ui_notification_create(bool ongoing, ui_notification_h *notification)
97 {
98         ui_notification_h notification_out;
99
100         if (notification == NULL)
101         {
102                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid output param", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
103                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
104         }
105
106         notification_out = (ui_notification_h)calloc(1, sizeof(struct ui_notification_s));
107
108         if (notification_out == NULL)
109         {
110                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
111                 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
112         }
113
114         notification_out->raw_handle = NULL;
115         notification_out->ongoing = ongoing;
116         notification_out->posted = false;
117         notification_out->removed = false;
118         notification_out->icon = NULL;
119         notification_out->time = NULL;
120         notification_out->title = NULL;
121         notification_out->content = NULL;
122         notification_out->service = NULL;
123         notification_out->sound = NULL;
124         notification_out->vibration = false;
125
126         *notification = notification_out;
127
128         return UI_NOTIFICATION_ERROR_NONE;
129 }
130
131 static int ui_notification_construct(bool ongoing, notification_h raw_handle, ui_notification_h *notification)
132 {
133         int retcode;
134         ui_notification_h notification_out;
135         char *icon;
136         time_t time;
137         char *title;
138         char *content;
139         bundle *service_data;
140         const char *sound = NULL;
141         notification_sound_type_e sound_type;
142         notification_vibration_type_e vib_type;
143
144         if (notification == NULL)
145         {
146                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid output param", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
147                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
148         }
149
150         notification_out = (ui_notification_h)calloc(1, sizeof(struct ui_notification_s));
151
152         if (notification_out == NULL)
153         {
154                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
155                 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
156         }
157
158         retcode = ui_notification_error_handler(notification_clone(raw_handle, &(notification_out->raw_handle)),\
159                                  __FUNCTION__, "failed to clone the notification handle");
160
161         if (retcode != NOTIFICATION_ERROR_NONE)
162         {
163                 return retcode;
164         }
165
166         notification_out->ongoing = ongoing;
167
168         notification_out->posted = true;
169
170         notification_out->removed = false;
171
172         if (!notification_get_image(raw_handle, NOTIFICATION_IMAGE_TYPE_ICON, &icon) && icon)
173         {
174                 notification_out->icon = strdup(icon);
175         }
176
177         if (!notification_get_time(raw_handle, &time))
178         {
179                 notification_out->time = malloc(sizeof(struct tm));
180
181                 if (notification_out->time == NULL)
182                 {
183                         ui_notification_destroy(notification_out);
184                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
185                         return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
186                 }
187
188                 localtime_r(&time, notification_out->time);
189         }
190
191         if (!notification_get_text(raw_handle, NOTIFICATION_TEXT_TYPE_TITLE, &title) && title)
192         {
193                 notification_out->title = strdup(title);
194         }
195
196         if (!notification_get_text(raw_handle, NOTIFICATION_TEXT_TYPE_CONTENT, &content) && content)
197         {
198                 notification_out->content = strdup(content);
199         }
200
201         if (!notification_get_sound(raw_handle, &sound_type, &sound) && sound)
202         {
203                 notification_out->sound = strdup(sound);
204         }
205
206         if (!notification_get_vibration(raw_handle, &vib_type, NULL))
207         {
208                 if (vib_type == NOTIFICATION_VIBRATION_TYPE_DEFAULT)
209                 {
210                         notification_out->vibration = true;
211                 }
212         }
213
214         if (!notification_get_execute_option(raw_handle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, &service_data))
215         {
216                 service_h service;
217
218                 if (!service_create_request(service_data, &service))
219                 {
220                         notification_out->service = service;
221                 }
222         }
223
224         *notification = notification_out;
225
226         return UI_NOTIFICATION_ERROR_NONE;
227 }
228
229 int ui_notification_destroy(ui_notification_h notification)
230 {
231         if (notification == NULL)
232         {
233                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid handle", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
234                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
235         }
236
237         if (notification->raw_handle)
238                 notification_free(notification->raw_handle);
239
240         if (notification->icon)
241                 free(notification->icon);
242
243         if (notification->time)
244                 free(notification->time);
245
246         if (notification->title)
247                 free(notification->title);
248
249         if (notification->content)
250                 free(notification->content);
251
252         if (notification->sound)
253                 free(notification->sound);
254
255         if (notification->service)
256                 service_destroy(notification->service);
257
258         free(notification);
259
260         return UI_NOTIFICATION_ERROR_NONE;
261 }
262
263 int ui_notification_clone(ui_notification_h *clone, ui_notification_h notification)
264 {
265         ui_notification_h notification_out;
266         int retcode;
267
268         if (clone == NULL || notification == NULL)
269         {
270                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
271                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
272         }
273
274         notification_out = (ui_notification_h)calloc(1, sizeof(struct ui_notification_s));
275
276         if (notification->raw_handle != NULL)
277         {
278                 retcode = notification_clone(notification->raw_handle, &(notification_out->raw_handle));
279
280                 if (retcode)
281                 {
282                         return ui_notification_error_handler(retcode, __FUNCTION__, "failed to clone the handle");
283                 }
284         }
285
286         notification_out->ongoing = notification->ongoing;
287
288         notification_out->posted = notification->posted;
289
290         notification_out->removed = notification->removed;
291
292         if (notification->icon)
293         {
294                 notification_out->icon = strdup(notification->icon);
295         }
296
297         if (notification->time)
298         {
299                 notification_out->time = malloc(sizeof(struct tm));
300                 if (notification_out->time != NULL)
301                 {
302                         memcpy(notification_out->time, notification->time, sizeof(struct tm));
303                 }
304         }
305
306         if (notification->title)
307         {
308                 notification_out->title = strdup(notification->title);
309         }
310
311         if (notification->content)
312         {
313                 notification_out->content = strdup(notification->content);
314         }
315
316         if (notification->sound)
317         {
318                 notification_out->sound = strdup(notification->sound);
319         }
320
321         notification_out->vibration = notification->vibration;
322
323         if (notification->service)
324         {
325                 service_clone(&(notification_out->service), notification->service);
326         }
327
328         *clone = notification_out;
329
330         return UI_NOTIFICATION_ERROR_NONE;
331 }
332
333 int ui_notification_is_ongoing(ui_notification_h notification, bool *ongoing)
334 {
335         if (notification == NULL || ongoing == NULL)
336         {
337                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
338                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
339         }
340
341         *ongoing = notification->ongoing;
342
343         return UI_NOTIFICATION_ERROR_NONE;
344 }
345
346 int ui_notification_set_icon(ui_notification_h notification, const char *path)
347 {
348         char *path_dup = NULL;
349
350         if (notification == NULL)
351         {
352                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
353                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
354         }
355
356         if (path != NULL)
357         {
358                 path_dup = strdup(path);
359
360                 if (path_dup == NULL)
361                 {
362                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
363                         return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
364                 }
365         }
366
367         if (notification->icon != NULL)
368         {
369                 free(notification->icon);
370         }
371
372         notification->icon = path_dup;
373
374         return UI_NOTIFICATION_ERROR_NONE;
375 }
376
377 int ui_notification_get_icon(ui_notification_h notification, char **path)
378 {
379         char *path_dup = NULL;
380
381         if (notification == NULL || path == NULL)
382         {
383                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
384                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
385         }
386
387         if (notification->icon != NULL)
388         {
389                 path_dup = strdup(notification->icon);
390
391                 if (path_dup == NULL)
392                 {
393                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
394                         return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
395                 }
396         }
397
398         *path = path_dup;
399
400         return UI_NOTIFICATION_ERROR_NONE;
401 }
402
403 int ui_notification_set_time(ui_notification_h notification, struct tm *time)
404 {
405         struct tm *time_dup = NULL;
406
407         if (notification == NULL)
408         {
409                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
410                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
411         }
412
413         if (time != NULL)
414         {
415                 time_dup = malloc(sizeof(struct tm));
416
417                 if (time_dup == NULL)
418                 {
419                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
420                         return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
421                 }
422
423                 memcpy(time_dup, time, sizeof(struct tm));
424         }
425
426         if (notification->time != NULL)
427         {
428                 free(notification->time);
429         }
430
431         notification->time = time_dup;
432
433         return UI_NOTIFICATION_ERROR_NONE;
434 }
435
436 int ui_notification_get_time(ui_notification_h notification, struct tm **time)
437 {
438         struct tm *time_dup = NULL;
439
440         if (notification == NULL || time == NULL)
441         {
442                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
443                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
444         }
445
446         if (notification->time != NULL)
447         {
448                 time_dup = malloc(sizeof(struct tm));
449
450                 if (time_dup == NULL)
451                 {
452                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
453                         return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
454                 }
455
456                 memcpy(time_dup, notification->time, sizeof(struct tm));
457         }
458
459         *time = time_dup;
460
461         return UI_NOTIFICATION_ERROR_NONE;
462 }
463
464 int ui_notification_set_title(ui_notification_h notification, const char *title)
465 {
466         char *title_dup = NULL;
467
468         if (notification == NULL)
469         {
470                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
471                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
472         }
473
474         if (title != NULL)
475         {
476                 title_dup = strdup(title);
477
478                 if (title_dup == NULL)
479                 {
480                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
481                         return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
482                 }
483         }
484
485         if (notification->title != NULL)
486         {
487                 free(notification->title);
488         }
489
490         notification->title = title_dup;
491
492         return UI_NOTIFICATION_ERROR_NONE;
493 }
494
495 int ui_notification_get_title(ui_notification_h notification, char **title)
496 {
497         char *title_dup = NULL;
498
499         if (notification == NULL || title == NULL)
500         {
501                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
502                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
503         }
504
505         if (notification->title != NULL)
506         {
507                 title_dup = strdup(notification->title);
508
509                 if (title_dup == NULL)
510                 {
511                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
512                         return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
513                 }
514         }
515
516         *title = title_dup;
517
518         return UI_NOTIFICATION_ERROR_NONE;
519 }
520
521
522 int ui_notification_set_content(ui_notification_h notification, const char *content)
523 {
524         char *content_dup = NULL;
525
526         if (notification == NULL)
527         {
528                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
529                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
530         }
531
532         if (content != NULL)
533         {
534                 content_dup = strdup(content);
535
536                 if (content_dup == NULL)
537                 {
538                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
539                         return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
540                 }
541         }
542
543         if (notification->content != NULL)
544         {
545                 free(notification->content);
546         }
547
548         notification->content = content_dup;
549
550         return UI_NOTIFICATION_ERROR_NONE;
551 }
552
553 int ui_notification_get_content(ui_notification_h notification, char **content)
554 {
555         char *content_dup = NULL;
556
557         if (notification == NULL || content == NULL)
558         {
559                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
560                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
561         }
562
563         if (notification->content != NULL)
564         {
565                 content_dup = strdup(notification->content);
566
567                 if (content_dup == NULL)
568                 {
569                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
570                         return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
571                 }
572         }
573
574         *content = content_dup;
575
576         return UI_NOTIFICATION_ERROR_NONE;
577 }
578
579
580 int ui_notification_set_service(ui_notification_h notification, service_h service)
581 {
582         int retcode;
583         service_h service_dup = NULL;
584
585         if (notification == NULL)
586         {
587                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
588                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
589         }
590
591         if (service != NULL)
592         {
593                 retcode = service_clone(&service_dup, service);
594
595                 if (retcode != SERVICE_ERROR_NONE)
596                 {
597                         if (retcode == SERVICE_ERROR_OUT_OF_MEMORY)
598                         {
599                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
600                                 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
601                         }
602                         else
603                         {
604                                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid service handle", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
605                                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
606                         }
607                 }
608         }
609
610         if (notification->service != NULL)
611         {
612                 service_destroy(notification->service);
613         }
614
615         notification->service = service_dup;
616
617         return UI_NOTIFICATION_ERROR_NONE;
618 }
619
620 int ui_notification_get_service(ui_notification_h notification, service_h *service)
621 {
622         int retcode;
623         service_h service_dup = NULL;
624
625         if (notification == NULL || service == NULL)
626         {
627                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
628                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
629         }
630
631         if (notification->service != NULL)
632         {
633                 retcode = service_clone(&service_dup, notification->service);
634
635                 if (retcode != SERVICE_ERROR_NONE)
636                 {
637                         if (retcode == SERVICE_ERROR_OUT_OF_MEMORY)
638                         {
639                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
640                                 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
641                         }
642                         else
643                         {
644                                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid service handle", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
645                                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
646                         }
647                 }
648         }
649
650         *service = service_dup;
651
652         return UI_NOTIFICATION_ERROR_NONE;
653 }
654
655 int ui_notification_set_sound(ui_notification_h notification, const char *path)
656 {
657         char *path_dup = NULL;
658
659         if (notification == NULL)
660         {
661                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
662                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
663         }
664
665         if (path != NULL)
666         {
667                 path_dup = strdup(path);
668
669                 if (path_dup == NULL)
670                 {
671                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
672                         return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
673                 }
674         }
675
676         if (notification->sound != NULL)
677         {
678                 free(notification->sound);
679         }
680
681         notification->sound = path_dup;
682
683         return UI_NOTIFICATION_ERROR_NONE;
684 }
685
686 int ui_notification_get_sound(ui_notification_h notification, char **path)
687 {
688         char *path_dup = NULL;
689
690         if (notification == NULL || path == NULL)
691         {
692                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
693                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
694         }
695
696         if (notification->sound != NULL)
697         {
698                 path_dup = strdup(notification->sound);
699
700                 if (path_dup == NULL)
701                 {
702                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
703                         *path = NULL;
704
705                         return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
706                 }
707         }
708
709         *path = path_dup;
710
711         return UI_NOTIFICATION_ERROR_NONE;
712 }
713
714 int ui_notification_set_vibration(ui_notification_h notification, bool value)
715 {
716         if (notification == NULL)
717         {
718                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
719                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
720         }
721
722         notification->vibration = value;
723
724         return UI_NOTIFICATION_ERROR_NONE;
725 }
726
727 int ui_notification_get_vibration(ui_notification_h notification, bool *value)
728 {
729         if (notification == NULL || value == NULL)
730         {
731                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
732                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
733         }
734
735         *value = notification->vibration;
736
737         return UI_NOTIFICATION_ERROR_NONE;
738 }
739
740 static int ui_notification_build_attributes(ui_notification_h notification)
741 {
742         bundle *service_data;
743
744         if (notification == NULL)
745         {
746                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid handle", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
747                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
748         }
749
750         if (notification->icon != NULL)
751         {
752                 struct stat st;
753
754                 if (stat(notification->icon, &st) < 0)
755                 {
756                         LOGE("[%s] NO_SUCH_FILE(0x%08x) : invalid icon", __FUNCTION__, UI_NOTIFICATION_ERROR_NO_SUCH_FILE);
757                         return UI_NOTIFICATION_ERROR_NO_SUCH_FILE;
758                 }
759
760                 notification_set_image(notification->raw_handle, NOTIFICATION_IMAGE_TYPE_ICON, notification->icon);
761         }
762
763         if (notification->time != NULL)
764         {
765                 notification_set_time(notification->raw_handle, mktime(notification->time));
766         }
767
768         if (notification->title != NULL)
769         {
770                 notification_set_text(notification->raw_handle, NOTIFICATION_TEXT_TYPE_TITLE, notification->title, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
771         }
772
773         if (notification->content != NULL)
774         {
775                 notification_set_text(notification->raw_handle, NOTIFICATION_TEXT_TYPE_CONTENT, notification->content, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
776         }
777
778         if (notification->service != NULL && service_to_bundle(notification->service, &service_data) == SERVICE_ERROR_NONE)
779         {
780                 notification_set_property(notification->raw_handle, 0);
781                 notification_set_execute_option(notification->raw_handle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, service_data);
782         }
783         else
784         {
785                 notification_set_property(notification->raw_handle, NOTIFICATION_PROP_DISABLE_APP_LAUNCH);
786         }
787
788         if (notification->sound != NULL)
789         {
790                 struct stat st;
791
792                 if (stat(notification->sound, &st) < 0)
793                 {
794                         LOGE("[%s] NO_SUCH_FILE(0x%08x) : invalid sound file", __FUNCTION__, UI_NOTIFICATION_ERROR_NO_SUCH_FILE);
795                         return UI_NOTIFICATION_ERROR_NO_SUCH_FILE;
796                 }
797                 notification_set_sound(notification->raw_handle, NOTIFICATION_SOUND_TYPE_USER_DATA, notification->sound);
798         }
799
800         if (notification->vibration)
801         {
802                 notification_set_vibration(notification->raw_handle, NOTIFICATION_VIBRATION_TYPE_DEFAULT, NULL);
803         }
804
805         return UI_NOTIFICATION_ERROR_NONE;
806 }
807
808 int ui_notification_post(ui_notification_h notification)
809 {
810         int retcode;
811
812         if (notification == NULL)
813         {
814                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
815                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
816         }
817
818         if (notification->posted == true)
819         {
820                 LOGE("[%s] INVALID_STATE(0x%08x) : the notification was already posted", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_STATE);
821                 return UI_NOTIFICATION_ERROR_INVALID_STATE;
822         }
823
824         if (notification->ongoing == true)
825         {
826                 notification->raw_handle = notification_new(NOTIFICATION_TYPE_ONGOING, NOTIFICATION_GROUP_ID_DEFAULT, NOTIFICATION_PRIV_ID_NONE);
827         }
828         else
829         {
830                 notification->raw_handle = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_DEFAULT, NOTIFICATION_PRIV_ID_NONE);
831         }
832
833         if (notification->raw_handle == NULL)
834         {
835                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
836                 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
837         }
838
839         retcode = ui_notification_build_attributes(notification);
840
841         if (retcode != UI_NOTIFICATION_ERROR_NONE)
842         {
843                 return retcode;
844         }
845
846         retcode = ui_notification_error_handler(notification_insert(notification->raw_handle, NULL), __FUNCTION__, "failed to post a notification");
847
848         if (retcode == UI_NOTIFICATION_ERROR_NONE)
849         {
850                 notification->posted = true;
851         }
852
853         return retcode;
854 }
855
856 int ui_notification_update(ui_notification_h notification)
857 {
858         int retcode;
859
860         if (notification == NULL)
861         {
862                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
863                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
864         }
865
866         if (notification->posted == false)
867         {
868                 LOGE("[%s] INVALID_STATE(0x%08x) : the notification was not posted", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_STATE);
869                 return UI_NOTIFICATION_ERROR_INVALID_STATE;
870         }
871
872         if (notification->removed == true)
873         {
874                 LOGE("[%s] INVALID_STATE(0x%08x) : the notification was canceled or cleared", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_STATE);
875                 return UI_NOTIFICATION_ERROR_INVALID_STATE;
876         }
877
878         retcode = ui_notification_build_attributes(notification);
879
880         if (retcode != UI_NOTIFICATION_ERROR_NONE)
881         {
882                 return retcode;
883         }
884
885         retcode = ui_notification_error_handler(notification_update(notification->raw_handle), __FUNCTION__, "failed to post a notification");
886
887         if (retcode == UI_NOTIFICATION_ERROR_INVALID_STATE)
888         {
889                 notification->removed = true;
890         }
891
892         return retcode;
893 }
894
895 int  ui_notification_update_progress(ui_notification_h notification, ui_notification_progress_type_e type, double value)
896 {
897         int retcode;
898
899         if (notification == NULL)
900         {
901                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
902                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
903         }
904
905         if (notification->raw_handle == NULL)
906         {
907                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid handle", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
908                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
909         }
910
911         if (notification->posted == false)
912         {
913                 LOGE("[%s] INVALID_STATE(0x%08x) : the notification was not posted", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_STATE);
914                 return UI_NOTIFICATION_ERROR_INVALID_STATE;
915         }
916
917         if (notification->removed == true)
918         {
919                 LOGE("[%s] INVALID_STATE(0x%08x) : the notification was canceled or cleared", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_STATE);
920                 return UI_NOTIFICATION_ERROR_INVALID_STATE;
921         }
922
923         if (value < 0)
924         {
925                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : the value must be greater than or equal to zero.", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
926                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
927         }
928
929         switch (type)
930         {
931         case UI_NOTIFICATION_PROGRESS_TYPE_SIZE:
932                 retcode = ui_notification_error_handler(
933                         notification_update_size(notification->raw_handle, NOTIFICATION_PRIV_ID_NONE, value),
934                         __FUNCTION__, "failed to update the progress");
935                 break;
936
937         case UI_NOTIFICATION_PROGRESS_TYPE_PERCENTAGE:
938                 retcode = ui_notification_error_handler(
939                         notification_update_progress(notification->raw_handle, NOTIFICATION_PRIV_ID_NONE, value),
940                         __FUNCTION__, "failed to update the progress");
941                 break;
942
943         default:
944                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid progress type", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
945                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
946         }
947
948         if (retcode == UI_NOTIFICATION_ERROR_INVALID_STATE)
949         {
950                 notification->removed = true;
951         }
952
953         return retcode;
954 }
955
956 int ui_notification_cancel(ui_notification_h notification)
957 {
958         int retcode;
959
960         if (notification == NULL)
961         {
962                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
963                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
964         }
965
966         if (notification->raw_handle == NULL)
967         {
968                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid handle", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
969                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
970         }
971
972         if (notification->posted == false)
973         {
974                 LOGE("[%s] INVALID_STATE(0x%08x) : the notification was not posted", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_STATE);
975                 return UI_NOTIFICATION_ERROR_INVALID_STATE;
976         }
977
978         if (notification->removed == true)
979         {
980                 LOGE("[%s] INVALID_STATE(0x%08x) : the notification was canceled or cleared", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_STATE);
981                 return UI_NOTIFICATION_ERROR_INVALID_STATE;
982         }
983
984         retcode = ui_notification_error_handler(notification_delete(notification->raw_handle), __FUNCTION__, "failed to cancel the notification");
985
986         if (retcode == UI_NOTIFICATION_ERROR_NONE)
987         {
988                 notification->removed = true;
989         }
990
991         return retcode;
992 }
993
994 void ui_notification_cancel_all(void)
995 {
996         notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_NONE);
997 }
998
999 void ui_notification_cancel_all_by_type(bool ongoing)
1000 {
1001         notification_type_e type = NOTIFICATION_TYPE_NONE;
1002
1003         if (ongoing)
1004                 type = NOTIFICATION_TYPE_ONGOING;
1005         else
1006                 type = NOTIFICATION_TYPE_NOTI;
1007
1008         notification_delete_all_by_type(NULL, type);
1009 }
1010
1011 void ui_notification_cancel_all_by_package(const char *package, bool ongoing)
1012 {
1013         notification_type_e type = NOTIFICATION_TYPE_NONE;
1014
1015         if (ongoing)
1016                 type = NOTIFICATION_TYPE_ONGOING;
1017         else
1018                 type = NOTIFICATION_TYPE_NOTI;
1019
1020         notification_delete_all_by_type(package, type);
1021 }
1022
1023 int ui_notification_cancel_all_by_app_id(const char *app_id, bool ongoing)
1024 {
1025         notification_type_e type = NOTIFICATION_TYPE_NONE;
1026
1027         if (app_id == NULL)
1028         {
1029                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
1030                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
1031         }
1032
1033         if (ongoing)
1034                 type = NOTIFICATION_TYPE_ONGOING;
1035         else
1036                 type = NOTIFICATION_TYPE_NOTI;
1037
1038         notification_delete_all_by_type(app_id, type);
1039
1040         return UI_NOTIFICATION_ERROR_NONE;
1041 }
1042
1043 static bool ui_notification_package_equal(notification_h handle)
1044 {
1045         char *package = NULL;
1046         char *handle_package = NULL;
1047
1048         if (app_get_package(&package))
1049         {
1050                 return false;
1051         }
1052
1053         if (notification_get_pkgname(handle, &handle_package))
1054         {
1055                 return false;
1056         }
1057
1058         if (strlen(package) == strlen(handle_package))
1059         {
1060                 if (!strncmp(package, handle_package, strlen(package)))
1061                 {
1062                         return true;
1063                 }
1064         }
1065
1066         return false;
1067 }
1068
1069 int ui_notification_foreach_notification_posted(bool ongoing, ui_notification_cb callback, void *user_data)
1070 {
1071         notification_list_h raw_handle_list;
1072         notification_h raw_handle;
1073         notification_type_e notification_type = ongoing ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
1074         ui_notification_h notification = NULL;
1075         bool iterate_next = true;
1076
1077         if (callback == NULL)
1078         {
1079                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
1080                 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
1081         }
1082
1083         if (notification_get_grouping_list(notification_type, -1, &raw_handle_list))
1084         {
1085                 LOGE("[%s] DB_FAILED(0x%08x) : failed to get a notification list", __FUNCTION__, UI_NOTIFICATION_ERROR_DB_FAILED);
1086                 return UI_NOTIFICATION_ERROR_DB_FAILED;
1087         }
1088
1089         while (raw_handle_list != NULL)
1090         {
1091                 raw_handle = notification_list_get_data(raw_handle_list);
1092
1093                 if (raw_handle != NULL && ui_notification_package_equal(raw_handle))
1094                 {
1095                         if (!ui_notification_construct(ongoing, raw_handle, &notification))
1096                         {
1097                                 iterate_next = callback(notification, user_data);
1098
1099                                 ui_notification_destroy(notification);
1100
1101                                 if (iterate_next == false)
1102                                 {
1103                                         break;
1104                                 }
1105                         }
1106                 }
1107
1108                 raw_handle_list = notification_list_get_next(raw_handle_list);
1109         }
1110
1111         notification_free_list(raw_handle_list);
1112
1113         return UI_NOTIFICATION_ERROR_NONE;
1114 }
1115