tizen 2.4 release
[framework/connectivity/mobileap-agent.git] / src / mobileap_notification.c
1 /*
2  * mobileap-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <glib.h>
19 #include <dbus/dbus.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <notification.h>
23 #include <notification_list.h>
24 #include <notification_text_domain.h>
25 #include <notification_internal.h>
26 #include <bluetooth.h>
27 #include <bundle_internal.h>
28
29 #include "mobileap_softap.h"
30 #include "mobileap_notification.h"
31
32 #define MH_NOTI_LAUNCH_PKGNAME  "ug-setting-mobileap-efl"
33 #define MH_NOTI_CALLER_PKGNAME  "mobileap-agent"
34 #define MH_LOCALE_DOMAIN        "ug-setting-mobileap-efl"
35 #define MH_LOCALE_DIR "/usr/ug/res/locale"
36
37
38 static int connected_noti_id = 0;
39 static int timeout_noti_id = 0;
40
41 static int __create_status_noti(const char *content)
42 {
43         if (content == NULL)
44                 return MOBILE_AP_ERROR_INVALID_PARAM;
45
46         notification_error_e ret;
47
48         ret = notification_status_message_post(content);
49         if (ret != NOTIFICATION_ERROR_NONE) {
50                 ERR("notification_status_message_post() is failed : %d\n", ret);
51                 return MOBILE_AP_ERROR_INTERNAL;
52         }
53
54         return MOBILE_AP_ERROR_NONE;
55 }
56
57 int _create_timeout_noti(const char *icon_path)
58 {
59         DBG("+\n");
60
61         notification_h noti = NULL;
62         notification_error_e ret = NOTIFICATION_ERROR_NONE;
63         char *old_icon_path = NULL;
64         char *general_icon_path = NULL;
65
66         if (timeout_noti_id) {
67                 noti = notification_load(MH_NOTI_CALLER_PKGNAME, timeout_noti_id);
68                 if (noti == NULL) {
69                         DBG("Notification can be deleted already\n");
70                 } else {
71                         ret = notification_get_image(noti,
72                                         NOTIFICATION_IMAGE_TYPE_ICON, &old_icon_path);
73                         if (ret == NOTIFICATION_ERROR_NONE) {
74                                 if (g_strcmp0(icon_path, old_icon_path))
75                                         general_icon_path = MH_NOTI_ICON_GENERAL;
76                         }
77
78                         ret = notification_delete(noti);
79                         if (ret != NOTIFICATION_ERROR_NONE) {
80                                 ERR("Fail to notification_delete [%d]\n", ret);
81
82                                 ret = notification_free(noti);
83                                 if (ret != NOTIFICATION_ERROR_NONE)
84                                         ERR("Fail to notification_free [%d]\n", ret);
85                                 return MOBILE_AP_ERROR_INTERNAL;
86                         }
87
88                         ret = notification_free(noti);
89                         if (ret != NOTIFICATION_ERROR_NONE) {
90                                 ERR("Fail to notification_free [%d]\n", ret);
91                         }
92                 }
93
94                 timeout_noti_id = 0;
95         }
96
97         noti = notification_create(NOTIFICATION_TYPE_NOTI);
98         if (!noti) {
99                 ERR("Fail to notification_create\n");
100                 return MOBILE_AP_ERROR_INTERNAL;
101         }
102
103         ret = notification_set_pkgname(noti, MH_NOTI_CALLER_PKGNAME);
104         if (ret != NOTIFICATION_ERROR_NONE) {
105                 ERR("Fail to notification_set_pkgname [%d]\n", ret);
106                 goto FAIL;
107         }
108
109         ret = notification_set_property(noti,
110                         NOTIFICATION_PROP_VOLATILE_DISPLAY);
111         if (ret != NOTIFICATION_ERROR_NONE) {
112                 ERR("Fail to notification_set_property [%d]\n", ret);
113                 goto FAIL;
114         }
115
116         ret = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
117         if (ret != NOTIFICATION_ERROR_NONE) {
118                 ERR("Fail to notification_set_layout [%d]\n", ret);
119                 goto FAIL;
120         }
121
122         ret = notification_set_image(noti,
123                         NOTIFICATION_IMAGE_TYPE_ICON, general_icon_path ?
124                         general_icon_path : icon_path);
125         if (ret != NOTIFICATION_ERROR_NONE) {
126                 ERR("Fail to notification_set_image [%d]\n", ret);
127                 goto FAIL;
128         }
129
130         ret = notification_set_text(noti,
131                         NOTIFICATION_TEXT_TYPE_TITLE, NULL,
132                         MH_STR_CONNECTION_TIMEOUT,
133                         NOTIFICATION_VARIABLE_TYPE_NONE);
134         if (ret != NOTIFICATION_ERROR_NONE) {
135                 ERR("Fail to notification_set_text [%d]\n", ret);
136                 goto FAIL;
137         }
138
139         ret = notification_set_text(noti,
140                         NOTIFICATION_TEXT_TYPE_CONTENT, NULL,
141                         MH_STR_CONFIGURE_TETHERING,
142                         NOTIFICATION_VARIABLE_TYPE_NONE);
143         if (ret != NOTIFICATION_ERROR_NONE) {
144                 ERR("Fail to notification_set_text [%d]\n", ret);
145                 goto FAIL;
146         }
147
148         ret = notification_set_text_domain(noti, MH_LOCALE_DOMAIN, MH_LOCALE_DIR);
149         if (ret != NOTIFICATION_ERROR_NONE) {
150                 ERR("Fail to notification_set_text_domain [%d]\n", ret);
151                 goto FAIL;
152         }
153
154         ret = notification_set_application(noti, MH_NOTI_LAUNCH_PKGNAME);
155         if (ret != NOTIFICATION_ERROR_NONE) {
156                 ERR("Fail to notification_set_application [%d]\n", ret);
157                 goto FAIL;
158         }
159
160         ret = notification_insert(noti, &timeout_noti_id);
161         if (ret != NOTIFICATION_ERROR_NONE) {
162                 ERR("Fail to notification_insert [%d]\n", ret);
163                 goto FAIL;
164         }
165
166         ret = notification_free(noti);
167         if (ret != NOTIFICATION_ERROR_NONE) {
168                 ERR("Fail to notification_free [%d]\n", ret);
169                 goto FAIL;
170         }
171
172         DBG("-\n");
173         return MOBILE_AP_ERROR_NONE;
174
175 FAIL:
176         ret = notification_free(noti);
177         if (ret != NOTIFICATION_ERROR_NONE)
178                 ERR("Fail to notification_free [%d]\n", ret);
179
180         return MOBILE_AP_ERROR_INTERNAL;
181 }
182
183 int _delete_timeout_noti(void)
184 {
185         notification_error_e ret = NOTIFICATION_ERROR_NONE;
186         notification_list_h noti_list = NULL;
187         notification_list_h l = NULL;
188         notification_h noti = NULL;
189         notification_ly_type_e layout;
190
191         DBG("+\n");
192
193         ret = notification_get_detail_list(MH_NOTI_CALLER_PKGNAME,
194                         NOTIFICATION_GROUP_ID_NONE,
195                         NOTIFICATION_PRIV_ID_NONE,
196                         -1,
197                         &noti_list);
198         if (ret != NOTIFICATION_ERROR_NONE) {
199                 ERR("Fail to notification_get_detail_list\n");
200                 return MOBILE_AP_ERROR_INTERNAL;
201         }
202
203         if (noti_list == NULL) {
204                 return MOBILE_AP_ERROR_NONE;
205         }
206
207         for (l = noti_list; l; l = notification_list_get_next(l)) {
208                 noti = notification_list_get_data(l);
209                 if (noti == NULL)
210                         break;
211
212                 ret = notification_get_layout(noti, &layout);
213                 if (ret == NOTIFICATION_ERROR_NONE &&
214                                 layout == NOTIFICATION_LY_NOTI_EVENT_SINGLE) {
215                         DBG("Found timeout noti\n");
216                         notification_delete(noti);
217                 }
218         }
219
220         notification_free_list(noti_list);
221
222         DBG("-\n");
223
224         return MOBILE_AP_ERROR_NONE;
225 }
226
227 int _create_connected_noti(int count, const char *icon_path)
228 {
229         DBG("+\n");
230         notification_h noti = NULL;
231         notification_error_e ret = NOTIFICATION_ERROR_NONE;
232         bundle *b = NULL;
233
234         noti = notification_create(NOTIFICATION_TYPE_ONGOING);
235         if (!noti) {
236                 ERR("Fail to notification_create\n");
237                 return MOBILE_AP_ERROR_INTERNAL;
238         }
239
240         ret = notification_set_pkgname(noti, MH_NOTI_CALLER_PKGNAME);
241         if (ret != NOTIFICATION_ERROR_NONE) {
242                 ERR("Fail to notification_set_pkgname [%d]\n", ret);
243                 goto FAIL;
244         }
245
246         ret = notification_set_property(noti,
247                         NOTIFICATION_PROP_DISABLE_AUTO_DELETE |
248                         NOTIFICATION_PROP_VOLATILE_DISPLAY);
249         if (ret != NOTIFICATION_ERROR_NONE) {
250                 ERR("Fail to notification_set_property [%d]\n", ret);
251                 goto FAIL;
252         }
253
254         b = bundle_create();
255         bundle_add(b, "caller", "notification");
256
257 #ifndef TIZEN_TV
258         appsvc_set_pkgname(b, "ug-setting-mobileap-efl");
259 #endif
260
261         ret = notification_set_execute_option(noti,
262                         NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "Launch", NULL, b);
263         if (ret != NOTIFICATION_ERROR_NONE) {
264                 ERR("Failed to notification_set_execute_option");
265                 goto FAIL;
266         }
267
268         ERR("Successfully added notification");
269
270         ret = notification_set_layout(noti, NOTIFICATION_LY_ONGOING_EVENT);
271         if (ret != NOTIFICATION_ERROR_NONE) {
272                 ERR("Fail to notification_set_image [%d]\n", ret);
273                 goto FAIL;
274         }
275
276         ret = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, icon_path);
277         if (ret != NOTIFICATION_ERROR_NONE) {
278                 ERR("Fail to notification_set_image [%d]\n", ret);
279                 goto FAIL;
280         }
281
282         ret = notification_set_text(noti,
283                         NOTIFICATION_TEXT_TYPE_TITLE, NULL,
284                         MH_STR_TETHERING,
285                         NOTIFICATION_VARIABLE_TYPE_NONE);
286         if (ret != NOTIFICATION_ERROR_NONE) {
287                 ERR("Fail to notification_set_text [%d]\n", ret);
288                 goto FAIL;
289         }
290
291         ret = notification_set_text(noti,
292                         NOTIFICATION_TEXT_TYPE_CONTENT, NULL,
293                         MH_STR_CONNECTED_DEV,
294                         NOTIFICATION_VARIABLE_TYPE_INT, count,
295                         NOTIFICATION_VARIABLE_TYPE_NONE);
296         if (ret != NOTIFICATION_ERROR_NONE) {
297                 ERR("Fail to notification_set_text [%d]\n", ret);
298                 goto FAIL;
299         }
300
301         ret = notification_set_text_domain(noti, MH_LOCALE_DOMAIN, MH_LOCALE_DIR);
302         if (ret != NOTIFICATION_ERROR_NONE) {
303                 ERR("Fail to notification_set_text_domain [%d]\n", ret);
304                 goto FAIL;
305         }
306
307         ret = notification_set_application(noti, MH_NOTI_LAUNCH_PKGNAME);
308         if (ret != NOTIFICATION_ERROR_NONE) {
309                 ERR("Fail to notification_set_application [%d]\n", ret);
310                 goto FAIL;
311         }
312
313         ret = notification_set_display_applist(noti,
314                         NOTIFICATION_DISPLAY_APP_ALL ^ NOTIFICATION_DISPLAY_APP_INDICATOR);
315         if (ret != NOTIFICATION_ERROR_NONE) {
316                 ERR("Fail to notification_set_display_applist [%d]\n", ret);
317                 goto FAIL;
318         }
319
320         ret = notification_insert(noti, &connected_noti_id);
321         if (ret != NOTIFICATION_ERROR_NONE) {
322                 ERR("Fail to notification_insert [%d]\n", ret);
323                 goto FAIL;
324         }
325
326         ret = notification_free(noti);
327         if (ret != NOTIFICATION_ERROR_NONE) {
328                 ERR("Fail to notification_free [%d]\n", ret);
329                 goto FAIL;
330         }
331
332         DBG("-\n");
333         return MOBILE_AP_ERROR_NONE;
334
335 FAIL:
336         if (b != NULL)
337                 bundle_free(b);
338         ret = notification_free(noti);
339         if (ret != NOTIFICATION_ERROR_NONE)
340                 ERR("Fail to notification_free [%d]\n", ret);
341         return MOBILE_AP_ERROR_INTERNAL;
342 }
343
344 int _update_connected_noti(int count, const char *icon_path)
345 {
346         DBG("+\n");
347
348         notification_h noti = NULL;
349         notification_error_e ret = NOTIFICATION_ERROR_NONE;
350
351         noti = notification_load(MH_NOTI_CALLER_PKGNAME, connected_noti_id);
352         if (noti == NULL) {
353                 ERR("notification_load is failed\n");
354                 return MOBILE_AP_ERROR_INTERNAL;
355         }
356
357         ret = notification_set_image(noti,
358                         NOTIFICATION_IMAGE_TYPE_ICON, icon_path);
359         if (ret != NOTIFICATION_ERROR_NONE) {
360                 ERR("Fail to notification_set_image [%d]\n", ret);
361                 goto FAIL;
362         }
363
364         ret = notification_set_text(noti,
365                         NOTIFICATION_TEXT_TYPE_CONTENT, NULL,
366                         MH_STR_CONNECTED_DEV,
367                         NOTIFICATION_VARIABLE_TYPE_INT, count,
368                         NOTIFICATION_VARIABLE_TYPE_NONE);
369         if (ret != NOTIFICATION_ERROR_NONE) {
370                 ERR("Fail to notification_set_text [%d]\n", ret);
371                 goto FAIL;
372         }
373
374         ret = notification_update(noti);
375         if (ret != NOTIFICATION_ERROR_NONE) {
376                 ERR("Fail to notification_update [%d]\n", ret);
377                 goto FAIL;
378         }
379
380         ret = notification_free(noti);
381         if (ret != NOTIFICATION_ERROR_NONE) {
382                 ERR("Fail to notification_free [%d]\n", ret);
383                 return MOBILE_AP_ERROR_INTERNAL;
384         }
385
386         DBG("-\n");
387         return MOBILE_AP_ERROR_NONE;
388
389 FAIL:
390         ret = notification_free(noti);
391         if (ret != NOTIFICATION_ERROR_NONE) {
392                 ERR("Fail to notification_free [%d]\n", ret);
393         }
394
395         return MOBILE_AP_ERROR_INTERNAL;
396 }
397
398 int _delete_connected_noti(void)
399 {
400         DBG("+\n");
401         notification_h noti = NULL;
402         notification_error_e ret;
403
404         noti = notification_load(MH_NOTI_CALLER_PKGNAME, connected_noti_id);
405         if (noti == NULL) {
406                 ERR("notification_load is failed\n");
407                 connected_noti_id = 0;
408                 return MOBILE_AP_ERROR_INTERNAL;
409         }
410         connected_noti_id = 0;
411
412         ret = notification_delete(noti);
413         if (ret != NOTIFICATION_ERROR_NONE) {
414                 ERR("Fail to notification_delete [%d]\n", ret);
415
416                 ret = notification_free(noti);
417                 if (ret != NOTIFICATION_ERROR_NONE)
418                         ERR("Fail to notification_free [%d]\n", ret);
419                 return MOBILE_AP_ERROR_INTERNAL;
420         }
421
422         ret = notification_free(noti);
423         if (ret != NOTIFICATION_ERROR_NONE) {
424                 ERR("Fail to notification_free [%d]\n", ret);
425                 return MOBILE_AP_ERROR_INTERNAL;
426         }
427
428         DBG("-\n");
429         return MOBILE_AP_ERROR_NONE;
430 }
431
432 void _create_tethering_active_noti(void)
433 {
434         int active_count = 0;
435
436         if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI))
437                 active_count++;
438
439         if (_mobileap_is_enabled(MOBILE_AP_STATE_BT))
440                 active_count++;
441
442         if (_mobileap_is_enabled(MOBILE_AP_STATE_USB))
443                 active_count++;
444
445         if (active_count == 1)
446                 __create_status_noti(_("IDS_MOBILEAP_BODY_TETHERING_ACTIVE_ABB"));
447
448         return;
449 }
450
451 void _create_bt_tethering_active_noti(void)
452 {
453         int ret;
454         bt_adapter_visibility_mode_e mode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
455         int duration;
456         char *str1 = NULL;
457         char *str2 = NULL;
458
459         if (!_mobileap_is_enabled(MOBILE_AP_STATE_WIFI) &&
460                         !_mobileap_is_enabled(MOBILE_AP_STATE_USB)) {
461                 str1 = MH_STR_TETHERING_ACTIVE;
462                 __create_status_noti(str1);
463         }
464
465         ret = bt_adapter_get_visibility(&mode, &duration);
466         if (ret != BT_ERROR_NONE) {
467                 ERR("bt_adapter_get_visibility is failed 0x[%X]\n", ret);
468         }
469
470         if (mode == BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE) {
471                 str2 = MH_STR_BT_VISIBILITY;
472                 __create_status_noti(str2);
473         }
474
475         return;
476 }