e0d2fd8207551eebe0869167c72043ca01aed08f
[platform/core/appfw/notification-service.git] / notification_display_service.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <glib.h>
4 #include <dirent.h>
5 #include <notification.h>
6 #include <dlfcn.h>
7 #include <dlog.h>
8
9
10 void load_plugins (char *path, int (**fct)(notification_h))
11 {
12         DIR *dir;
13         struct dirent *plugins_dir;
14         char *plugin_path;
15         void *plugin;
16         int (*plugin_fct)(notification_h);
17
18         dir = opendir (path);
19         if (!dir)
20                 return;
21
22         while ((plugins_dir = readdir(dir)) != NULL) {
23                 if (g_str_has_suffix (plugins_dir->d_name, ".so")) {
24                         plugin_path = g_strconcat (path, G_DIR_SEPARATOR_S, plugins_dir->d_name, NULL);
25                         plugin = dlopen (plugin_path, RTLD_NOW | RTLD_LOCAL);
26                         plugin_fct = dlsym (plugin, "display_notification");
27                         g_free (plugin_path);
28                         if (!plugin) {
29                                 LOGD("\"%s\" is not a plugin, continuing", plugins_dir->d_name);
30                                 continue;
31                         } else if (!plugin_fct) {
32                                 LOGD("Plugin \"%s\" incompatible, continuing", plugins_dir->d_name);
33                                 dlclose (plugins_dir->d_name);
34                                 continue;
35                         } else {
36                                  /* use the first working plugin, if not configured otherwise */
37                                 LOGD("Plugin \"%s\" compatible, loading...", plugins_dir->d_name);
38                                 *fct = plugin_fct;
39                                 break;
40                         }
41                 }
42         }
43
44         closedir (dir);
45 }
46
47 int display_notification_text (notification_h noti)
48 {
49         char *pkgname = NULL;
50         char *title = NULL;
51         char *content = NULL;
52         char *image_path = NULL;
53
54         notification_get_pkgname (noti, &pkgname);
55         if (pkgname == NULL)
56                 notification_get_application (noti, &pkgname);
57         notification_get_title (noti, &title, NULL);
58         notification_get_text (noti, NOTIFICATION_TEXT_TYPE_CONTENT, &content);
59         notification_get_image (noti, NOTIFICATION_IMAGE_TYPE_ICON, &image_path);
60
61         LOGD("\nNew Notification : %s", title);
62         LOGD("Icon : %s", image_path);
63         LOGD("Message : %s", content);
64
65         return 1;
66 }
67
68 void display_notifications_cb (void *data, notification_type_e notif_type)
69 {
70         int (*fct)(notification_h) = data;
71         notification_h noti = NULL;
72         notification_list_h notification_list = NULL;
73         notification_list_h get_list = NULL;
74
75         notification_get_list (NOTIFICATION_TYPE_NOTI, -1, &notification_list);
76         if (notification_list) {
77                 get_list = notification_list_get_head (notification_list);
78                 while (get_list) {
79                         noti = notification_list_get_data (get_list);
80
81                          /* if the display function was successful, delete the notification */
82                         if  ( (*fct)(noti) ) {
83                                 get_list = notification_list_remove(get_list, noti);
84                                 notification_delete(noti);
85                         }
86                 }
87         }
88 }
89
90 void del_pending_notifications(void)
91 {
92         notification_h noti = NULL;
93         notification_list_h notification_list = NULL;
94         notification_list_h get_list = NULL;
95
96         notification_get_list (NOTIFICATION_TYPE_NOTI, -1, &notification_list);
97         if (notification_list) {
98                 get_list = notification_list_get_head (notification_list);
99                 while (get_list) {
100                         noti = notification_list_get_data(get_list);
101                         notification_delete(noti);
102                         LOGD("remove pending notification: %p from DB", noti);
103                         get_list = notification_list_remove(get_list, noti);
104                 }
105         }
106 }
107
108 int main (int argc, char **argv)
109 {
110         GMainLoop *mainloop = NULL;
111         gboolean error_s;
112         notification_error_e error_n;
113         int (*disp_fct)(notification_h);
114
115          /* fall back to pure text notification if no plugin works */
116         disp_fct = &display_notification_text;
117         LOGD("Checking for display plugins...");
118         if (g_file_test (PLUGINSDIR, G_FILE_TEST_IS_DIR))
119                 load_plugins (PLUGINSDIR, &disp_fct);
120
121 retry_socket:
122         LOGD("Checking if the notifications server socket exists...");
123         error_s = g_file_test ("/tmp/.notification.service", G_FILE_TEST_EXISTS);
124         if (!error_s) {
125                 LOGD("Could not find the notifications server socket");
126                 sleep (5);
127                 goto retry_socket;
128         }
129
130         /* remove all notifications stored in DB before handling new notifications */
131         del_pending_notifications();
132
133 retry_service:
134         LOGD("Checking if the notifications server is available...");
135         error_n = notification_resister_changed_cb (display_notifications_cb, (*disp_fct));
136         if (error_n != NOTIFICATION_ERROR_NONE) {
137                 LOGD("Could not register with notifications server");
138                 sleep (5);
139                 goto retry_service;
140         }
141
142         mainloop = g_main_loop_new (NULL, FALSE);
143         if (!mainloop) {
144                 printf ("Failed to create the GLib main loop\n");
145                 return -1;
146         }
147
148         g_main_loop_run (mainloop);
149
150         return 0;
151 }