Use new Bluetooth-frwk bt_agent_reply_sync() APIs
[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 <bundle.h>
6 #ifdef HAVE_WAYLAND
7 #include <libwlmessage.h>
8 #endif
9
10 #include <bundle.h>
11
12 #include <bluetooth.h>
13 #include <dlog.h>
14
15 #define POPUP_TYPE_INFO  "user_info_popup"
16 #define POPUP_TYPE_USERCONFIRM "user_confirm_popup"
17 #define POPUP_TYPE_USERPROMPT "user_agreement_popup"
18
19 static void display_user_information_popup(void) {};
20
21 static void display_user_prompt_popup(void) {};
22
23 static void display_user_confirmation_popup(void) 
24 {
25          notification_error_e err = NOTIFICATION_ERROR_NONE;
26          int bt_yesno = 1;
27
28          char line[4];
29
30 #ifdef HAVE_WAYLAND
31          struct wlmessage *wlmessage = wlmessage_create();
32          wlmessage_set_message(wlmessage, "Do you confirm ?");
33          wlmessage_add_button(wlmessage, 1, "Yes");
34          wlmessage_add_button(wlmessage, 0, "No");
35          wlmessage_set_default_button(wlmessage, 1);
36          bt_yesno = wlmessage_show(wlmessage, NULL);
37          wlmessage_destroy(wlmessage);
38
39          if (bt_yesno == 1)
40                  bt_agent_reply_sync(0);
41          else if (bt_yesno == 0)
42                  bt_agent_reply_sync(1);
43 #else
44          fprintf(stdout, "Do you confirm yes or no ? ");
45          while ( bt_yesno != 0){
46                  if (!fgets(line, sizeof(line), stdin))
47                          continue;
48                  if ( strncmp("yes", line, 3) == 0) {
49                          LOGD("user accepts to pair with device ");
50                          bt_agent_reply_sync(0);
51                          bt_yesno = 0;
52                  } else if ( strncmp("no", line, 2) == 0) {
53                          LOGD("user rejects to pair with device ");
54                          bt_agent_reply_sync(1);
55                          bt_yesno = 0;
56                  } else {
57                          fprintf(stdout," yes or no ?\n");
58                          continue;
59                  }
60          }
61 #endif
62          err = notification_delete_all_by_type("bt-agent", NOTIFICATION_TYPE_NOTI);
63          if (err != NOTIFICATION_ERROR_NONE) {
64                   LOGE("Unable to remove notifications");
65          }
66
67 }
68
69 static void __noti_changed_cb(void *data, notification_type_e type)
70 {
71          notification_h noti = NULL;
72          notification_list_h notification_list = NULL;
73          notification_list_h get_list = NULL;
74          notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
75
76          LOGD("listen to new notifications...");
77
78          int count = 0, group_id = 0, priv_id = 0, num = 1;
79          char *pkgname = NULL;
80          char *title = NULL;
81          char *str_count = NULL;
82          char *content= NULL;
83          bundle *user_data = NULL;
84
85          const char *device_name = NULL;
86          const char *passkey = NULL;
87          const char *agent_path;
88          const char *event_type = NULL;
89
90          char *popup_type = NULL;
91
92          noti_err = notification_get_list(NOTIFICATION_TYPE_NOTI, -1, &notification_list);
93          if (noti_err != NOTIFICATION_ERROR_NONE) {
94                 LOGE("notification_get_list() failed (error code = %d)", noti_err);
95          }
96
97          if (notification_list) {
98                 LOGD("new notificiation received");
99
100                 get_list = notification_list_get_head(notification_list);
101                 noti = notification_list_get_data(get_list);
102                 notification_get_id(noti, &group_id, &priv_id);
103                 notification_get_pkgname(noti, &pkgname);
104                 if(pkgname == NULL){
105                        notification_get_application(noti, &pkgname);
106                 }
107
108                 notification_get_text(noti, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, &str_count);
109                 if (!str_count) {
110                         count = 0;
111                 } else {
112                         count = atoi(str_count);
113                 }
114                 notification_get_title(noti, &title, NULL);
115                 notification_get_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT , &content);
116                 notification_get_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH,NULL, &user_data);
117
118                 fprintf(stdout, "NOTIFICATION: %s - %s - %s - %i - %i \n", pkgname, title, content, count, num);
119
120                 notification_get_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, &popup_type);
121                 LOGD("'%s' notification type [%s]", pkgname, popup_type);
122
123                 if (!strcasecmp(POPUP_TYPE_INFO, popup_type))
124                   display_user_information_popup();
125                 else if (!strcasecmp(POPUP_TYPE_USERCONFIRM, popup_type))
126                   display_user_confirmation_popup();
127                 else if (!strcasecmp(POPUP_TYPE_USERPROMPT, popup_type))
128                   display_user_prompt_popup();
129                 else
130                   LOGE("popup type is not recognized !");
131
132
133                 if (notification_list != NULL) {
134                         notification_free_list(notification_list);
135                         notification_list = NULL;
136                 }
137   }
138   return;
139 }
140
141 int
142 main(int argc, char **argv)
143 {
144     if (!ecore_init()) {
145         LOGE("ERROR: Cannot init Ecore!\n");
146         return -1;
147     }
148
149     bt_initialize();
150
151     bt_agent_register_sync();
152
153     notification_resister_changed_cb(__noti_changed_cb, NULL);
154     ecore_main_loop_begin();
155
156     return 1;
157 }
158