tizen 2.3 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
231         noti = notification_create(NOTIFICATION_TYPE_ONGOING);
232         if (!noti) {
233                 ERR("Fail to notification_create\n");
234                 return MOBILE_AP_ERROR_INTERNAL;
235         }
236
237         ret = notification_set_pkgname(noti, MH_NOTI_CALLER_PKGNAME);
238         if (ret != NOTIFICATION_ERROR_NONE) {
239                 ERR("Fail to notification_set_pkgname [%d]\n", ret);
240                 goto FAIL;
241         }
242
243         ret = notification_set_property(noti,
244                         NOTIFICATION_PROP_DISABLE_AUTO_DELETE |
245                         NOTIFICATION_PROP_VOLATILE_DISPLAY);
246         if (ret != NOTIFICATION_ERROR_NONE) {
247                 ERR("Fail to notification_set_property [%d]\n", ret);
248                 goto FAIL;
249         }
250
251         ret = notification_set_layout(noti, NOTIFICATION_LY_ONGOING_EVENT);
252         if (ret != NOTIFICATION_ERROR_NONE) {
253                 ERR("Fail to notification_set_image [%d]\n", ret);
254                 goto FAIL;
255         }
256
257         ret = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, icon_path);
258         if (ret != NOTIFICATION_ERROR_NONE) {
259                 ERR("Fail to notification_set_image [%d]\n", ret);
260                 goto FAIL;
261         }
262
263         ret = notification_set_text(noti,
264                         NOTIFICATION_TEXT_TYPE_TITLE, NULL,
265                         MH_STR_TETHERING,
266                         NOTIFICATION_VARIABLE_TYPE_NONE);
267         if (ret != NOTIFICATION_ERROR_NONE) {
268                 ERR("Fail to notification_set_text [%d]\n", ret);
269                 goto FAIL;
270         }
271
272         ret = notification_set_text(noti,
273                         NOTIFICATION_TEXT_TYPE_CONTENT, NULL,
274                         MH_STR_CONNECTED_DEV,
275                         NOTIFICATION_VARIABLE_TYPE_INT, count,
276                         NOTIFICATION_VARIABLE_TYPE_NONE);
277         if (ret != NOTIFICATION_ERROR_NONE) {
278                 ERR("Fail to notification_set_text [%d]\n", ret);
279                 goto FAIL;
280         }
281
282         ret = notification_set_text_domain(noti, MH_LOCALE_DOMAIN, MH_LOCALE_DIR);
283         if (ret != NOTIFICATION_ERROR_NONE) {
284                 ERR("Fail to notification_set_text_domain [%d]\n", ret);
285                 goto FAIL;
286         }
287
288         ret = notification_set_application(noti, MH_NOTI_LAUNCH_PKGNAME);
289         if (ret != NOTIFICATION_ERROR_NONE) {
290                 ERR("Fail to notification_set_application [%d]\n", ret);
291                 goto FAIL;
292         }
293
294         ret = notification_set_display_applist(noti,
295                         NOTIFICATION_DISPLAY_APP_ALL ^ NOTIFICATION_DISPLAY_APP_INDICATOR);
296         if (ret != NOTIFICATION_ERROR_NONE) {
297                 ERR("Fail to notification_set_display_applist [%d]\n", ret);
298                 goto FAIL;
299         }
300
301         ret = notification_insert(noti, &connected_noti_id);
302         if (ret != NOTIFICATION_ERROR_NONE) {
303                 ERR("Fail to notification_insert [%d]\n", ret);
304                 goto FAIL;
305         }
306
307         ret = notification_free(noti);
308         if (ret != NOTIFICATION_ERROR_NONE) {
309                 ERR("Fail to notification_free [%d]\n", ret);
310                 goto FAIL;
311         }
312
313         DBG("-\n");
314         return MOBILE_AP_ERROR_NONE;
315
316 FAIL:
317         ret = notification_free(noti);
318         if (ret != NOTIFICATION_ERROR_NONE)
319                 ERR("Fail to notification_free [%d]\n", ret);
320         return MOBILE_AP_ERROR_INTERNAL;
321 }
322
323 int _update_connected_noti(int count, const char *icon_path)
324 {
325         DBG("+\n");
326
327         notification_h noti = NULL;
328         notification_error_e ret = NOTIFICATION_ERROR_NONE;
329
330         noti = notification_load(MH_NOTI_CALLER_PKGNAME, connected_noti_id);
331         if (noti == NULL) {
332                 ERR("notification_load is failed\n");
333                 return MOBILE_AP_ERROR_INTERNAL;
334         }
335
336         ret = notification_set_image(noti,
337                         NOTIFICATION_IMAGE_TYPE_ICON, icon_path);
338         if (ret != NOTIFICATION_ERROR_NONE) {
339                 ERR("Fail to notification_set_image [%d]\n", ret);
340                 goto FAIL;
341         }
342
343         ret = notification_set_text(noti,
344                         NOTIFICATION_TEXT_TYPE_CONTENT, NULL,
345                         MH_STR_CONNECTED_DEV,
346                         NOTIFICATION_VARIABLE_TYPE_INT, count,
347                         NOTIFICATION_VARIABLE_TYPE_NONE);
348         if (ret != NOTIFICATION_ERROR_NONE) {
349                 ERR("Fail to notification_set_text [%d]\n", ret);
350                 goto FAIL;
351         }
352
353         ret = notification_update(noti);
354         if (ret != NOTIFICATION_ERROR_NONE) {
355                 ERR("Fail to notification_update [%d]\n", ret);
356                 goto FAIL;
357         }
358
359         ret = notification_free(noti);
360         if (ret != NOTIFICATION_ERROR_NONE) {
361                 ERR("Fail to notification_free [%d]\n", ret);
362                 return MOBILE_AP_ERROR_INTERNAL;
363         }
364
365         DBG("-\n");
366         return MOBILE_AP_ERROR_NONE;
367
368 FAIL:
369         ret = notification_free(noti);
370         if (ret != NOTIFICATION_ERROR_NONE) {
371                 ERR("Fail to notification_free [%d]\n", ret);
372         }
373
374         return MOBILE_AP_ERROR_INTERNAL;
375 }
376
377 int _delete_connected_noti(void)
378 {
379         DBG("+\n");
380         notification_h noti = NULL;
381         notification_error_e ret;
382
383         noti = notification_load(MH_NOTI_CALLER_PKGNAME, connected_noti_id);
384         if (noti == NULL) {
385                 ERR("notification_load is failed\n");
386                 connected_noti_id = 0;
387                 return MOBILE_AP_ERROR_INTERNAL;
388         }
389         connected_noti_id = 0;
390
391         ret = notification_delete(noti);
392         if (ret != NOTIFICATION_ERROR_NONE) {
393                 ERR("Fail to notification_delete [%d]\n", ret);
394
395                 ret = notification_free(noti);
396                 if (ret != NOTIFICATION_ERROR_NONE)
397                         ERR("Fail to notification_free [%d]\n", ret);
398                 return MOBILE_AP_ERROR_INTERNAL;
399         }
400
401         ret = notification_free(noti);
402         if (ret != NOTIFICATION_ERROR_NONE) {
403                 ERR("Fail to notification_free [%d]\n", ret);
404                 return MOBILE_AP_ERROR_INTERNAL;
405         }
406
407         DBG("-\n");
408         return MOBILE_AP_ERROR_NONE;
409 }
410
411 void _create_tethering_active_noti(void)
412 {
413         int active_count = 0;
414
415         if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI))
416                 active_count++;
417
418         if (_mobileap_is_enabled(MOBILE_AP_STATE_BT))
419                 active_count++;
420
421         if (_mobileap_is_enabled(MOBILE_AP_STATE_USB))
422                 active_count++;
423
424         if (active_count == 1)
425                 __create_status_noti(_("IDS_MOBILEAP_BODY_TETHERING_ACTIVE_ABB"));
426
427         return;
428 }
429
430 void _create_bt_tethering_active_noti(void)
431 {
432         int ret;
433         bt_adapter_visibility_mode_e mode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
434         int duration;
435         int str_len;
436         char *str1 = NULL;
437         char *str2 = NULL;
438         char *str = NULL;
439
440         if (!_mobileap_is_enabled(MOBILE_AP_STATE_WIFI) &&
441                         !_mobileap_is_enabled(MOBILE_AP_STATE_USB)) {
442                 str1 = MH_STR_TETHERING_ACTIVE;
443                 __create_status_noti(str1);
444         }
445
446         ret = bt_adapter_get_visibility(&mode, &duration);
447         if (ret != BT_ERROR_NONE) {
448                 ERR("bt_adapter_get_visibility is failed 0x[%X]\n", ret);
449         }
450
451         if (mode == BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE) {
452                 str2 = MH_STR_BT_VISIBILITY;
453                 __create_status_noti(str2);
454         }
455
456         return;
457 }