Fix that bt-serivce can't find DBUS_SESSION_BUS_ADDRESS
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-agent-notification.c
1 /*
2  * bluetooth-frwk
3  *
4  * Copyright (c) 2013 Intel Corporation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include "bt-service-agent-notification.h"
21
22 const char*
23 error_to_string(notification_error_e error)
24 {
25     if (error == NOTIFICATION_ERROR_INVALID_DATA)
26         return "NOTIFICATION_ERROR_INVALID_DATA";
27     if (error == NOTIFICATION_ERROR_NO_MEMORY)
28         return "NOTIFICATION_ERROR_NO_MEMORY";
29     if (error == NOTIFICATION_ERROR_FROM_DB)
30         return "NOTIFICATION_ERROR_FROM_DB";
31     if (error == NOTIFICATION_ERROR_ALREADY_EXIST_ID)
32         return "NOTIFICATION_ERROR_ALREADY_EXIST_ID";
33     if (error == NOTIFICATION_ERROR_FROM_DBUS)
34         return "NOTIFICATION_ERROR_FROM_DBUS";
35     if (error == NOTIFICATION_ERROR_NOT_EXIST_ID)
36         return "NOTIFICATION_ERROR_NOT_EXIST_ID";
37     if (error == NOTIFICATION_ERROR_IO)
38         return "NOTIFICATION_ERROR_IO";
39     if (error == NOTIFICATION_ERROR_SERVICE_NOT_READY)
40         return "NOTIFICATION_ERROR_SERVICE_NOT_READY";
41     if (error == NOTIFICATION_ERROR_NONE)
42         return "NOTIFICATION_ERROR_NONE";
43
44     return "UNHANDLED ERROR";
45 }
46
47 static int
48 __notification_set_text(notification_h noti, char *title, char *body)
49 {
50     notification_error_e err = NOTIFICATION_ERROR_NONE;
51
52     err = notification_set_text(    noti, NOTIFICATION_TEXT_TYPE_TITLE,
53                                     title,
54                                     NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
55     if (err != NOTIFICATION_ERROR_NONE) {
56         BT_ERR("Unable to set notification title: %s", error_to_string(err));
57         return BT_FAILED;
58     }
59
60     err = notification_set_text(    noti, NOTIFICATION_TEXT_TYPE_CONTENT,
61                                     body,
62                                     NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
63     if (err != NOTIFICATION_ERROR_NONE) {
64         BT_ERR("Unable to set notification content: %s", error_to_string(err));
65         return BT_FAILED;
66     }
67     return BT_SUCCESS;
68 }
69
70 int
71 notification_launch(bundle * user_data)
72 {
73     int ret = BT_SUCCESS;
74     const char *device_name = NULL;
75     const char *passkey = NULL;
76     const char *file = NULL;
77     const char *event_type = NULL;
78     char *title = NULL;
79     char *body = NULL;
80     notification_h notif;
81     notification_error_e err = NOTIFICATION_ERROR_NONE;
82
83     event_type = bundle_get_val(user_data, "event-type");
84     BT_DBG("create notification for '%s' event", event_type);
85
86     notif = notification_new(NOTIFICATION_TYPE_NOTI,
87                              NOTIFICATION_GROUP_ID_NONE,
88                              NOTIFICATION_PRIV_ID_NONE);
89     if (notif == NULL) {
90         BT_ERR("Failed to create notification: %s", error_to_string(err));
91         return BT_FAILED;
92     }
93
94     err = notification_set_pkgname(notif, "bluetooth-frwk-bt-service");
95     if (err != NOTIFICATION_ERROR_NONE) {
96         BT_ERR("Unable to set pkgname: %s", error_to_string(err));
97         return BT_FAILED;
98     }
99
100     err = notification_set_image(notif, NOTIFICATION_IMAGE_TYPE_ICON, BT_ICON);
101     if (err != NOTIFICATION_ERROR_NONE) {
102         BT_ERR("Unable to set notification icon path: %s", error_to_string(err));
103         return BT_FAILED;
104     }
105
106     /* 
107      * Pass the full bundle to the notification
108      */
109     err  = notification_set_execute_option(notif, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, user_data);
110     if (err != NOTIFICATION_ERROR_NONE) {
111         BT_ERR("Unable to set notification icon path: %s", error_to_string(err));
112         return BT_FAILED;
113     }
114
115     if(!strcasecmp(event_type, "pin-request")) {
116         device_name = (gchar*) bundle_get_val(user_data, "device-name");
117
118         title = g_strdup_printf("Bluetooth pairing request");
119         body = g_strdup_printf("Enter PIN to pair with %s (Try 0000 or 1234)", device_name);
120
121         ret = __notification_set_text(notif, title, body);
122
123         g_free(title);
124         g_free(body);
125     } else if (!strcasecmp(event_type, "passkey-confirm-request")){
126         device_name = (gchar*) bundle_get_val(user_data, "device-name");
127         passkey = (gchar*) bundle_get_val(user_data, "passkey");
128
129         title = g_strdup_printf("Bluetooth passkey confirm request");
130         body = g_strdup_printf("Confirm passkey is %s to pair with %s", passkey, device_name);
131
132         ret = __notification_set_text(notif, title, body);
133
134         g_free(title);
135         g_free(body);
136     }  else if (!strcasecmp(event_type, "passkey-request")) {
137         device_name = (gchar*) bundle_get_val(user_data, "device-name");
138
139         title = g_strdup_printf("Bluetooth pairing request");
140         body = g_strdup_printf("Enter PIN to pair with %s (Try 0000 or 1234)", device_name);
141
142         ret = __notification_set_text(notif, title, body);
143
144         g_free(title);
145         g_free(body);
146     } else if (!strcasecmp(event_type, "passkey-display-request")) {
147         device_name = (gchar*) bundle_get_val(user_data, "device-name");
148         passkey = (gchar*) bundle_get_val(user_data, "passkey");
149
150         title = g_strdup_printf("Bluetooth passkey display request");
151         body = g_strdup_printf("Enter %s on %s to pair", passkey, device_name);
152
153         ret = __notification_set_text(notif, title, body);
154
155         g_free(title);
156         g_free(body);
157     } else if (!strcasecmp(event_type, "authorize-request")) {
158         device_name = (gchar*) bundle_get_val(user_data, "device-name");
159
160         title = g_strdup_printf("Bluetooth authorize request");
161         body = g_strdup_printf("Allow %s to connect?", device_name);
162
163         ret = __notification_set_text(notif, title, body);
164
165         g_free(title);
166         g_free(body);
167     } else if (!strcasecmp(event_type, "app-confirm-request")) {
168         /* FIXME Seems to be an osp mechanism so not implemented to be confirmed */
169         BT_DBG("app-confirm-request even_type seems to be an osp mechanism so not implemented in gnome environment; to be confirmed");
170         ret = BT_FAILED;
171     } else if (!strcasecmp(event_type, "push-authorize-request")) {
172         file = (gchar*) bundle_get_val(user_data, "file");
173         device_name = (gchar*) bundle_get_val(user_data, "device-name");
174
175         title = g_strdup_printf("Bluetooth push authorize request");
176         body = g_strdup_printf("Receive %s from %s?", file, device_name);
177
178         ret = __notification_set_text(notif, title, body);
179
180         g_free(title);
181         g_free(body);
182     } else if (!strcasecmp(event_type, "confirm-overwrite-request")) {
183         /* FIXME Seems to be an osp mechanism so not implemented to be confirmed*/
184         BT_DBG("confirm-overwrite-request even_type seems to be an osp mechanism so not implemented in gnome environment; to be confirmed");
185         ret = BT_FAILED;
186     } else if (!strcasecmp(event_type, "keyboard-passkey-request")) {
187         device_name = (gchar*) bundle_get_val(user_data, "device-name");
188         passkey = (gchar*) bundle_get_val(user_data, "passkey");
189
190         title = g_strdup_printf("Bluetooth keyboard passkey request");
191         body = g_strdup_printf("Enter %s on %s to pair", passkey, device_name);
192
193         ret = __notification_set_text(notif, title, body);
194
195         g_free(title);
196         g_free(body);
197     } else if (!strcasecmp(event_type, "bt-information")) {
198         /* FIXME Seems to be an osp mechanism so not implemented to be confirmed */
199         BT_DBG("bt-information even_type seems to be an osp mechanism so not implemented in gnome environment; to be confirmed");
200         ret = BT_FAILED;
201     } else if (!strcasecmp(event_type, "exchange-request")) {
202         device_name = (gchar*) bundle_get_val(user_data, "device-name");
203
204         title = g_strdup_printf("Bluetooth exchange request");
205         body = g_strdup_printf("exchange-request from %s", device_name);
206
207         ret = __notification_set_text(notif, title, body);
208         g_free(title);
209         g_free(body);
210     } else if (!strcasecmp(event_type, "phonebook-request")) {
211         device_name = bundle_get_val(user_data, "device-name");
212
213         title = g_strdup_printf("Bluetooth phonebook request");
214         body = g_strdup_printf("Allow %s phonebook access", device_name);
215
216         ret = __notification_set_text(notif, title, body);
217
218         g_free(title);
219         g_free(body);
220     } else if (!strcasecmp(event_type, "message-request")) {
221         device_name = bundle_get_val(user_data, "device-name");
222
223         title = g_strdup_printf("Bluetooth keyboard passkey request");
224         body = g_strdup_printf("Allow %s to access messages?", device_name);
225
226         ret = __notification_set_text(notif, title, body);
227
228         g_free(title);
229         g_free(body);
230     } else {
231         ret = BT_FAILED;
232     }
233
234     err = notification_insert(notif, NULL);
235     if (err != NOTIFICATION_ERROR_NONE) {
236         BT_ERR("Unable to insert notification: %s\n", error_to_string(err));
237         return BT_FAILED;
238     }
239
240     return ret;
241 }
242