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