Create a "notification-display-service" daemon
[platform/core/appfw/notification-service.git] / notification_display_service.c
1 #include <stdio.h>
2 #include <sys/stat.h>
3 #include <glib.h>
4 #include <notification.h>
5 #include <dlog.h>
6 #include <libwlmessage.h>
7
8
9 void display_notifications_cb (void *data, notification_type_e notif_type)
10 {
11         notification_h noti = NULL;
12         notification_list_h notification_list = NULL;
13         notification_list_h get_list = NULL;
14
15         char *pkgname = NULL;
16         char *title = NULL;
17         char *content = NULL;
18         char *image_path = NULL;
19
20         notification_get_list (NOTIFICATION_TYPE_NOTI, -1, &notification_list);
21         if (notification_list) {
22                 get_list = notification_list_get_head (notification_list);
23                 while (get_list) {
24                         noti = notification_list_get_data (get_list);
25                         notification_get_pkgname (noti, &pkgname);
26                         if (pkgname == NULL)
27                                 notification_get_application (noti, &pkgname);
28                         notification_get_title (noti, &title, NULL);
29                         notification_get_text (noti, NOTIFICATION_TEXT_TYPE_CONTENT, &content);
30                         notification_get_image (noti, NOTIFICATION_IMAGE_TYPE_ICON, &image_path);
31
32                         struct wlmessage *wlmessage = wlmessage_create ();
33                         wlmessage_set_title (wlmessage, title);
34                         wlmessage_set_icon (wlmessage, image_path);
35                         wlmessage_set_message (wlmessage, content);
36                         wlmessage_add_button (wlmessage, 0, "Ok");
37                         if (wlmessage_show (wlmessage, NULL) < 0) {
38                                 wlmessage_destroy (wlmessage);
39                                 return;
40                         }
41                         wlmessage_destroy (wlmessage);
42
43                         LOGD("\nNew Notification : %s\n", title);
44                         LOGD("Icon : %s\n", image_path);
45                         LOGD("Message : %s\n", content);
46
47                         get_list = notification_list_remove(get_list, noti);
48                         notification_delete(noti);
49                 }
50         }
51 }
52
53 int main (int argc, char **argv)
54 {
55         GMainLoop *mainloop = NULL;
56         notification_error_e error_n;
57         int error_s;
58         struct stat buf;
59
60 retry_socket:
61         LOGD("Checking if the notifications server socket exists...");
62         error_s = stat ("/tmp/.notification.service", &buf);
63         if (error_s == -1) {
64                 LOGD("Could not find the notifications server socket");
65                 sleep (5);
66                 goto retry_socket;
67         }
68
69 retry_service:
70         LOGD("Checking if the notifications server is available...");
71         error_n = notification_resister_changed_cb (display_notifications_cb, NULL);
72         if (error_n != NOTIFICATION_ERROR_NONE) {
73                 LOGD("Could not register with notifications server");
74                 sleep (5);
75                 goto retry_service;
76         }
77
78         mainloop = g_main_loop_new (NULL, FALSE);
79         if (!mainloop) {
80                 printf ("Failed to create the GLib main loop\n");
81                 return -1;
82         }
83
84         g_main_loop_run (mainloop);
85
86         return 0;
87 }