Initial implementation of a graphical notifications display deamon.
[platform/core/appfw/notification-service.git] / bluetooth_notification_client.c
1 #include <Ecore.h>
2 #include <notification.h>
3 #include <unistd.h>
4
5 #include <dbus/dbus.h>
6 #include <dbus/dbus-glib.h>
7 #include <dbus/dbus-glib-lowlevel.h>
8 #include <bundle.h>
9 #ifdef HAVE_WAYLAND
10 #include <libwlmessage.h>
11 #endif
12
13 typedef enum {
14            BT_AGENT_ACCEPT,
15            BT_AGENT_REJECT,
16            BT_AGENT_CANCEL,
17            BT_CORE_AGENT_TIMEOUT,
18 } bt_agent_accept_type_t;
19
20
21 typedef void (*bt_notification)(DBusGProxy *proxy);
22
23 static DBusGProxy*
24 __bluetooth_create_agent_proxy(DBusGConnection *sys_conn, const char *path)
25 {
26         return dbus_g_proxy_new_for_name (sys_conn,
27                                           "org.projectx.bt",
28                                           path,
29                                           "org.bluez.Agent1");
30 }
31
32 static DBusGProxy*
33 __bluetooth_create_obex_proxy(DBusGConnection *sys_conn)
34 {
35         return dbus_g_proxy_new_for_name(sys_conn,
36                                          "org.bluez.frwk_agent",
37                                          "/org/obex/ops_agent",
38                                          "org.openobex.Agent");
39 }
40
41 static void
42 __notify_passkey_confirm_request_accept_cb( DBusGProxy* agent_proxy)
43 {
44         dbus_g_proxy_call_no_reply( agent_proxy, "ReplyConfirmation",
45                                    G_TYPE_UINT, BT_AGENT_ACCEPT,
46                                    G_TYPE_INVALID, G_TYPE_INVALID);
47
48 }
49
50 static void
51 __notify_passkey_confirm_request_cancel_cb(DBusGProxy* agent_proxy)
52 {
53
54         dbus_g_proxy_call_no_reply( agent_proxy, "ReplyConfirmation",
55                                     G_TYPE_UINT, BT_AGENT_CANCEL,
56                                     G_TYPE_INVALID, G_TYPE_INVALID);
57
58 }
59
60 static void
61 __notify_push_authorize_request_accept_cb(DBusGProxy* obex_proxy)
62 {
63
64         dbus_g_proxy_call_no_reply( obex_proxy, "ReplyAuthorize",
65                                     G_TYPE_UINT, BT_AGENT_ACCEPT,
66                                     G_TYPE_INVALID, G_TYPE_INVALID);
67
68 }
69
70 static void
71 __notify_push_authorize_request_cancel_cb(DBusGProxy* obex_proxy)
72 {
73
74         dbus_g_proxy_call_no_reply( obex_proxy, "ReplyAuthorize",
75                                     G_TYPE_UINT, BT_AGENT_CANCEL,
76                                     G_TYPE_INVALID, G_TYPE_INVALID);
77
78 }
79
80 static void
81 __notify_authorize_request_accept_cb(DBusGProxy* agent_proxy)
82 {
83
84          dbus_g_proxy_call_no_reply( agent_proxy, "ReplyAuthorize",
85                                      G_TYPE_UINT, BT_AGENT_ACCEPT,
86                                      G_TYPE_INVALID, G_TYPE_INVALID);
87 }
88
89 static void
90 __notify_authorize_request_cancel_cb(DBusGProxy* agent_proxy)
91 {
92
93          dbus_g_proxy_call_no_reply( agent_proxy, "ReplyAuthorize",
94                                      G_TYPE_UINT, BT_AGENT_CANCEL,
95                                      G_TYPE_INVALID, G_TYPE_INVALID);
96
97 }
98
99 static int
100 __display_notification(bt_notification cb_1, bt_notification cb_2, DBusGProxy *proxy)
101 {
102
103          notification_error_e err = NOTIFICATION_ERROR_NONE;
104          int bt_yesno;
105          bt_yesno = 1;
106          char line[4];
107
108 #ifdef HAVE_WAYLAND
109          struct wlmessage *wlmessage = wlmessage_create();
110          wlmessage_set_message(wlmessage, "Do you confirm ?");
111          wlmessage_add_button(wlmessage, 1, "Yes");
112          wlmessage_add_button(wlmessage, 0, "No");
113          wlmessage_set_default_button(wlmessage, 1);
114          bt_yesno = wlmessage_show(wlmessage, NULL);
115          wlmessage_destroy(wlmessage);
116
117          if (bt_yesno == 1)
118                  (cb_1) (proxy);
119          else if (bt_yesno == 0)
120                  (cb_2) (proxy);
121 #else
122          fprintf(stdout, "Do you confirm yes or no ? ");
123          while ( bt_yesno != 0){
124                  if (!fgets(line, sizeof(line), stdin))
125                          continue;
126                  if ( strcmp(line,"yes") == 0){
127                          (cb_1) (proxy);
128                          bt_yesno = 0;
129                  } else if ( strcmp(line,"no") == 0){
130                          (cb_2) (proxy);
131                          bt_yesno = 0;
132                  } else {
133                          fprintf(stdout," yes or no :");
134                  }
135          }
136 #endif
137          err = notification_delete_all_by_type("bluetooth-frwk-bt-service", NOTIFICATION_TYPE_NOTI);
138          if (err != NOTIFICATION_ERROR_NONE) {
139                   fprintf(stdout, "Unable to remove notifications");
140          }
141
142 }
143
144 static void __noti_changed_cb(void *data, notification_type_e type)
145 {
146          notification_h noti = NULL;
147          notification_list_h notification_list = NULL;
148          notification_list_h get_list = NULL;
149          int count = 0, group_id = 0, priv_id = 0, show_noti = 0, num = 1;
150          char *pkgname = NULL;
151          char *title = NULL;
152          char *str_count = NULL;
153          char *content= NULL;
154          bundle *user_data = NULL;
155          DBusGConnection *sys_conn;
156          DBusGProxy *agent_proxy;
157          DBusGProxy *obex_proxy;
158
159
160          const char *device_name = NULL;
161          const char *passkey = NULL;
162          const char *file = NULL;
163          const char *agent_path;
164          const char *event_type = NULL;
165
166          notification_get_list(NOTIFICATION_TYPE_NOTI, -1, &notification_list);
167          if (notification_list) {
168                 get_list = notification_list_get_head(notification_list);
169                 noti = notification_list_get_data(get_list);
170                 notification_get_id(noti, &group_id, &priv_id);
171                 notification_get_pkgname(noti, &pkgname);
172                 if(pkgname == NULL){
173                        notification_get_application(noti, &pkgname);
174                 }
175
176                 notification_get_text(noti, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, &str_count);
177                 if (!str_count) {
178                         count = 0;
179                 } else {
180                         count = atoi(str_count);
181                 }
182                 notification_get_title(noti, &title, NULL);
183                 notification_get_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT , &content);
184                 notification_get_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH,NULL, &user_data);
185
186                 fprintf(stdout, "NOTIFICATION: %s - %s - %s - %i - %i \n", pkgname, title, content, count, num);
187
188                 event_type = bundle_get_val(user_data, "event-type");
189
190                 if(!strcasecmp(event_type, "pin-request")) {
191                         /* Not implemented */
192                         fprintf(stdout," Not implemented\n");
193
194                 } else if (!strcasecmp(event_type, "passkey-confirm-request")){
195                         device_name = (gchar*) bundle_get_val(user_data, "device-name");
196                         passkey = (gchar*) bundle_get_val(user_data, "passkey");
197                         agent_path = bundle_get_val(user_data, "agent-path");
198
199                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
200                         if (sys_conn == NULL) {
201                                 fprintf(stdout,"ERROR: Can't get on system bus");
202                                 return;
203                         }
204
205                         agent_proxy = __bluetooth_create_agent_proxy(sys_conn, agent_path);
206                         if (!agent_proxy){
207                                 fprintf(stdout,"create new agent_proxy failed\n");
208                                 return;
209                         }
210
211                          __display_notification(__notify_passkey_confirm_request_accept_cb, __notify_passkey_confirm_request_cancel_cb,agent_proxy);
212                 } else if (!strcasecmp(event_type, "passkey-request")) {
213                         /* Not implemented */
214                         fprintf(stdout," Not implemented\n");
215
216                 } else if (!strcasecmp(event_type, "passkey-display-request")) {
217                         /* Not implemented */
218                         fprintf(stdout," Not implemented\n");
219
220                 } else if (!strcasecmp(event_type, "authorize-request")) {
221                         device_name = (gchar*) bundle_get_val(user_data, "device-name");
222                         agent_path = bundle_get_val(user_data, "agent-path");
223
224                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
225                         if (sys_conn == NULL) {
226                                 fprintf(stdout,"ERROR: Can't get on system bus");
227                                 return;
228                         }
229
230                         agent_proxy = __bluetooth_create_agent_proxy(sys_conn, agent_path);
231                         if (!agent_proxy){
232                                 fprintf(stdout,"create new agent_proxy failed\n");
233                                 return;
234                         }
235
236                         __display_notification( __notify_authorize_request_accept_cb, __notify_authorize_request_cancel_cb,agent_proxy);
237                 } else if (!strcasecmp(event_type, "app-confirm-request")) {
238                         /* Not implemented */
239                         fprintf(stdout," Not implemented\n");
240
241                 } else if (!strcasecmp(event_type, "push-authorize-request")) {
242                         file = (gchar*) bundle_get_val(user_data, "file");
243                         device_name = (gchar*) bundle_get_val(user_data, "device-name");
244
245                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
246                         if (sys_conn == NULL) {
247                                 fprintf(stdout,"ERROR: Can't get on system bus");
248                                 return;
249                         }
250
251                         obex_proxy = __bluetooth_create_obex_proxy(sys_conn);
252                         if (!obex_proxy){
253                                 fprintf(stdout,"create new obex_proxy failed\n");
254                                 return;
255                         }
256
257                         __display_notification( __notify_push_authorize_request_accept_cb, __notify_push_authorize_request_cancel_cb,obex_proxy);
258                 } else if (!strcasecmp(event_type, "confirm-overwrite-request")) {
259                         /* Not implemented */
260                         fprintf(stdout," Not implemented\n");
261
262                 } else if (!strcasecmp(event_type, "keyboard-passkey-request")) {
263                         /* Not implemented */
264                         fprintf(stdout," Not implemented\n");
265
266                 } else if (!strcasecmp(event_type, "bt-information")) {
267                         /* Not implemented */
268                         fprintf(stdout," Not implemented\n");
269
270                 } else if (!strcasecmp(event_type, "exchange-request")) {
271                         device_name = (gchar*) bundle_get_val(user_data, "device-name");
272                         agent_path = bundle_get_val(user_data, "agent-path");
273
274                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
275                         if (sys_conn == NULL) {
276                                 fprintf(stdout,"ERROR: Can't get on system bus");
277                                 return;
278                         }
279
280                         agent_proxy = __bluetooth_create_agent_proxy(sys_conn, agent_path);
281                         if (!agent_proxy){
282                                 fprintf(stdout,"create new agent_proxy failed\n");
283                                 return;
284                         }
285
286                         __display_notification( __notify_authorize_request_accept_cb, __notify_authorize_request_cancel_cb,agent_proxy);
287                 } else if (!strcasecmp(event_type, "phonebook-request")) {
288                         device_name = bundle_get_val(user_data, "device-name");
289                         agent_path = bundle_get_val(user_data, "agent-path");
290
291                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
292                         if (sys_conn == NULL) {
293                                 fprintf(stdout,"ERROR: Can't get on system bus");
294                                 return;
295                         }
296
297                         agent_proxy = __bluetooth_create_agent_proxy(sys_conn, agent_path);
298                         if (!agent_proxy){
299                                 fprintf(stdout,"create new agent_proxy failed\n");
300                                 return;
301                         }
302
303                         __display_notification( __notify_authorize_request_accept_cb, __notify_authorize_request_cancel_cb,agent_proxy);
304                 } else if (!strcasecmp(event_type, "message-request")) {
305                         device_name = bundle_get_val(user_data, "device-name");
306                         agent_path = bundle_get_val(user_data, "agent-path");
307
308                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
309                         if (sys_conn == NULL) {
310                                 fprintf(stdout,"ERROR: Can't get on system bus");
311                                 return;
312                         }
313
314                         agent_proxy = __bluetooth_create_agent_proxy(sys_conn, agent_path);
315                         if (!agent_proxy){
316                                 fprintf(stdout,"create new agent_proxy failed\n");
317                                 return;
318                         }
319
320                         __display_notification( __notify_authorize_request_accept_cb,  __notify_authorize_request_cancel_cb,agent_proxy);
321                 }
322         }
323         if (notification_list != NULL) {
324                  notification_free_list(notification_list);
325                  notification_list = NULL;
326         }
327
328         return;
329 }
330
331 int
332 main(int argc, char **argv)
333 {
334     if (!ecore_init()) {
335         fprintf(stderr, "ERROR: Cannot init Ecore!\n");
336         return -1;
337     }
338
339     notification_resister_changed_cb(__noti_changed_cb, NULL);
340     ecore_main_loop_begin();
341
342  shutdown:
343     ecore_shutdown();
344     return 0;
345 }
346