Initial support for Bluetooth notifcations
[platform/core/appfw/notification-service.git] / notification_display_service.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <signal.h>
5 #include <sys/inotify.h>
6 #include <notification.h>
7 #ifdef HAVE_WAYLAND
8 #include <libwlmessage.h>
9 #endif
10
11
12 int fd, wd;
13
14 void sigint_handler (int s)
15 {
16         inotify_rm_watch (fd, wd);
17         close (fd);
18         exit (0);
19 }
20
21 void display_notifications ()
22 {
23         notification_h noti = NULL;
24         notification_list_h notification_list = NULL;
25         notification_list_h get_list = NULL;
26
27         char *pkgname = NULL;
28         char *title = NULL;
29         char *content = NULL;
30         char *image_path = NULL;
31         char *info1 = NULL;
32         enum { NOTIF_TYPE_INFO, NOTIF_TYPE_USERPROMPT, NOTIF_TYPE_USERCONFIRM } type = 0;
33
34
35         notification_get_list (NOTIFICATION_TYPE_NOTI, -1, &notification_list);
36         if (notification_list) {
37                 get_list = notification_list_get_head (notification_list);
38                 while (get_list) {
39                         noti = notification_list_get_data (get_list);
40                         notification_get_pkgname (noti, &pkgname);
41                         if (pkgname == NULL)
42                                 notification_get_application (noti, &pkgname);
43                         notification_get_title (noti, &title, NULL);
44                         notification_get_text (noti, NOTIFICATION_TEXT_TYPE_CONTENT, &content);
45                         notification_get_image (noti, NOTIFICATION_IMAGE_TYPE_ICON, &image_path);
46
47                          /* react specifically to the source framework and event (TODO : plugins !) */
48                         if (!strcmp(pkgname, "bt-agent")) {
49                                 notification_get_text (noti, NOTIFICATION_TEXT_TYPE_INFO_1, &info1);
50                                 if (info1) {
51                                         if ( (!strcmp(info1, "RequestPinCode")) || (!strcmp(info1, "RequestPasskey")) ) {
52                                                 type = NOTIF_TYPE_USERPROMPT;
53                                         }
54                                         else if (!strcmp(info1, "RequestConfirmation")) {
55                                                 type = NOTIF_TYPE_USERCONFIRM;
56                                                 content = "Please confirm";
57                                         }
58                                 }
59                         }
60
61 #                       ifdef HAVE_WAYLAND
62                         struct wlmessage *wlmessage = wlmessage_create ();
63                         if (title)
64                                 wlmessage_set_title (wlmessage, title);
65                         if (image_path)
66                                 wlmessage_set_icon (wlmessage, image_path);
67                         if (content)
68                                 wlmessage_set_message (wlmessage, content);
69                         else
70                                 wlmessage_set_message (wlmessage, "<Default>");
71                         if (type == NOTIF_TYPE_USERPROMPT)
72                                 wlmessage_set_textfield (wlmessage, "");
73                         if (type == NOTIF_TYPE_USERCONFIRM) {
74                                 wlmessage_add_button (wlmessage, 1, "Yes");
75                                 wlmessage_add_button (wlmessage, 0, "No");
76                         } else {
77                                 wlmessage_add_button (wlmessage, 0, "Ok");
78                         }
79
80                         if (wlmessage_show (wlmessage, NULL) < 0) {
81                                 wlmessage_destroy (wlmessage);
82                                 return;
83                         }
84                         wlmessage_destroy (wlmessage);
85
86 #                       else
87                         fprintf(stderr, "\nNew Notification : %s\n", title); 
88                         fprintf(stderr, "Icon : %s\n", image_path); 
89                         fprintf(stderr, "Message : %s\n", content); 
90 #                       endif
91
92                         get_list = notification_list_remove(get_list, noti);
93                         notification_delete(noti);
94                 }
95         }
96 }
97
98 int main (int argc, char **argv)
99 {
100         char buffer[8192];
101
102          /* display notifications once, so it stays useful without inotify  */
103         display_notifications ();
104
105         fd = inotify_init ();
106         if (fd < 0) {
107                 fprintf (stderr, "ERROR: cannot initialize inotify\n");
108                 fprintf (stderr, "Verify that your kernel integrates it\n");
109                 return -1;
110         }
111
112         signal (SIGINT, sigint_handler);
113         wd = inotify_add_watch (fd, "/usr/dbspace/.notification.db", IN_MODIFY);
114         while (1) {
115                 read (fd, buffer, sizeof(buffer));
116                 display_notifications ();
117         }
118
119         return 0;
120 }