ea25bdcb072738a48ff27fa1c784592d387ae7dc
[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 <sys/socket.h>
7 #include <linux/un.h>
8 #include <notification.h>
9 #ifdef HAVE_WAYLAND
10 #include <libwlmessage.h>
11 #endif
12
13
14 int fd, wd;
15
16 void sigint_handler (int s)
17 {
18         inotify_rm_watch (fd, wd);
19         close (fd);
20         exit (0);
21 }
22
23 void display_notifications ()
24 {
25         notification_h noti = NULL;
26         notification_list_h notification_list = NULL;
27         notification_list_h get_list = NULL;
28
29         char *pkgname = NULL;
30         char *title = NULL;
31         char *content = NULL;
32         char *image_path = NULL;
33         char *info1 = NULL;
34         enum { NOTIF_TYPE_INFO, NOTIF_TYPE_USERPROMPT, NOTIF_TYPE_USERCONFIRM } type = 0;
35         int clicked_button = 0;
36
37         notification_get_list (NOTIFICATION_TYPE_NOTI, -1, &notification_list);
38         if (notification_list) {
39                 get_list = notification_list_get_head (notification_list);
40                 while (get_list) {
41                         noti = notification_list_get_data (get_list);
42                         notification_get_pkgname (noti, &pkgname);
43                         if (pkgname == NULL)
44                                 notification_get_application (noti, &pkgname);
45                         notification_get_title (noti, &title, NULL);
46                         notification_get_text (noti, NOTIFICATION_TEXT_TYPE_CONTENT, &content);
47                         notification_get_image (noti, NOTIFICATION_IMAGE_TYPE_ICON, &image_path);
48
49                          /* react specifically to the source framework and event (TODO : plugins !) */
50                         if (!strcmp(pkgname, "bt-agent")) {
51                                 notification_get_text (noti, NOTIFICATION_TEXT_TYPE_INFO_1, &info1);
52                                 if (info1) {
53                                         if ( (!strcmp(info1, "RequestPinCode")) || (!strcmp(info1, "RequestPasskey")) ) {
54                                                 type = NOTIF_TYPE_USERPROMPT;
55                                         }
56                                         else if (!strcmp(info1, "RequestConfirmation")) {
57                                                 type = NOTIF_TYPE_USERCONFIRM;
58                                                 content = strdup("Please confirm");
59                                         }
60                                 }
61                         }
62
63 #                       ifdef HAVE_WAYLAND
64                         struct wlmessage *wlmessage = wlmessage_create ();
65                         if (title)
66                                 wlmessage_set_title (wlmessage, title);
67                         if (image_path)
68                                 wlmessage_set_icon (wlmessage, image_path);
69                         if (content)
70                                 wlmessage_set_message (wlmessage, content);
71                         else
72                                 wlmessage_set_message (wlmessage, "<Default>");
73                         if (type == NOTIF_TYPE_USERPROMPT)
74                                 wlmessage_set_textfield (wlmessage, "");
75                         if (type == NOTIF_TYPE_USERCONFIRM) {
76                                 wlmessage_add_button (wlmessage, 1, "Yes");
77                                 wlmessage_add_button (wlmessage, 0, "No");
78                         } else {
79                                 wlmessage_add_button (wlmessage, 0, "Ok");
80                         }
81
82                         clicked_button = wlmessage_show (wlmessage, NULL);
83
84                         if (clicked_button < 0) {
85                                 wlmessage_destroy (wlmessage);
86                                 return;
87                         } else if ((clicked_button == 1) && (type == NOTIF_TYPE_USERCONFIRM)) {
88                                 if (!strcmp(pkgname, "bt-agent")) {
89                                         struct sockaddr_un addr;
90                                         int socket_fd, nbytes;
91                                         char buffer[256];
92
93                                         socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
94
95                                         memset (&addr, 0, sizeof(struct sockaddr_un));
96                                         addr.sun_family = AF_UNIX;
97                                         snprintf (addr.sun_path, UNIX_PATH_MAX, "/tmp/.bluetooth.service");
98
99                                         if (connect (socket_fd,
100                                                      (struct sockaddr *)&addr,
101                                                      sizeof(struct sockaddr_un)) != 0) {
102                                                 fprintf(stderr, "Cannot connect to /tmp/.bluetooth.service\n");
103                                         } else {
104                                                 nbytes = snprintf(buffer, 256, "1 clicked");
105                                                 write (socket_fd, buffer, nbytes);
106                                                 close (socket_fd);
107                                         }
108                                 }
109                         }
110                         wlmessage_destroy (wlmessage);
111
112 #                       else
113                         fprintf(stderr, "\nNew Notification : %s\n", title); 
114                         fprintf(stderr, "Icon : %s\n", image_path); 
115                         fprintf(stderr, "Message : %s\n", content); 
116 #                       endif
117
118                         get_list = notification_list_remove(get_list, noti);
119                         notification_delete(noti);
120                 }
121         }
122 }
123
124 int main (int argc, char **argv)
125 {
126         char buffer[8192];
127
128          /* display notifications once, so it stays useful without inotify  */
129         display_notifications ();
130
131         fd = inotify_init ();
132         if (fd < 0) {
133                 fprintf (stderr, "ERROR: cannot initialize inotify\n");
134                 fprintf (stderr, "Verify that your kernel integrates it\n");
135                 return -1;
136         }
137
138         signal (SIGINT, sigint_handler);
139         wd = inotify_add_watch (fd, "/usr/dbspace/.notification.db", IN_MODIFY);
140         while (1) {
141                 read (fd, buffer, sizeof(buffer));
142                 display_notifications ();
143         }
144
145         return 0;
146 }