Initialize Tizen 2.3
[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
26 #include "mobileap_agent.h"
27
28 //#define __ENABLE_UG_LAUNCH
29 #define MH_NOTI_APP_NAME        "setting-mobileap-efl"
30 #define MH_AGENT_PKG_NAME       "mobileap-agent"
31
32 static int connected_noti_id = 0;
33 static int timeout_noti_id = 0;
34
35 int _create_timeout_noti(const char *content, const char *title,
36                 const char *icon_path)
37 {
38         DBG("+\n");
39         notification_h noti = NULL;
40         notification_error_e ret = NOTIFICATION_ERROR_NONE;
41
42         if (timeout_noti_id) {
43                 noti = notification_load(APPNAME, timeout_noti_id);
44                 if (noti == NULL) {
45                         DBG("Notification can be deleted already\n");
46                 } else {
47                         ret = notification_delete(noti);
48                         if (ret != NOTIFICATION_ERROR_NONE) {
49                                 ERR("Fail to notification_delete [%d]\n", ret);
50
51                                 ret = notification_free(noti);
52                                 if (ret != NOTIFICATION_ERROR_NONE)
53                                         ERR("Fail to notification_free [%d]\n", ret);
54                                 return MOBILE_AP_ERROR_INTERNAL;
55                         }
56
57                         ret = notification_free(noti);
58                         if (ret != NOTIFICATION_ERROR_NONE) {
59                                 ERR("Fail to notification_free [%d]\n", ret);
60                                 return MOBILE_AP_ERROR_INTERNAL;
61                         }
62                 }
63                 timeout_noti_id = 0;
64         }
65
66         noti = notification_create(NOTIFICATION_TYPE_NOTI);
67         if (!noti) {
68                 ERR("Fail to notification_create\n");
69                 return MOBILE_AP_ERROR_INTERNAL;
70         }
71
72         ret = notification_set_property(noti,
73 #ifdef __ENABLE_UG_LAUNCH
74                         NOTIFICATION_PROP_VOLATILE_DISPLAY);
75 #else
76                         NOTIFICATION_PROP_VOLATILE_DISPLAY |
77                         NOTIFICATION_PROP_DISABLE_APP_LAUNCH);
78 #endif
79         if (ret != NOTIFICATION_ERROR_NONE) {
80                 ERR("Fail to notification_set_property [%d]\n", ret);
81                 goto FAIL;
82         }
83
84         ret = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
85         if (ret != NOTIFICATION_ERROR_NONE) {
86                 ERR("Fail to notification_set_image [%d]\n", ret);
87                 goto FAIL;
88         }
89
90         ret = notification_set_image(noti,
91                         NOTIFICATION_IMAGE_TYPE_ICON, icon_path);
92         if (ret != NOTIFICATION_ERROR_NONE) {
93                 ERR("Fail to notification_set_image [%d]\n", ret);
94                 goto FAIL;
95         }
96
97         ret = notification_set_text(noti,
98                         NOTIFICATION_TEXT_TYPE_TITLE,
99                         title,
100                         NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
101         if (ret != NOTIFICATION_ERROR_NONE) {
102                 ERR("Fail to notification_set_text [%d]\n", ret);
103                 goto FAIL;
104         }
105
106         ret = notification_set_text(noti,
107                         NOTIFICATION_TEXT_TYPE_CONTENT,
108                         content,
109                         NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
110         if (ret != NOTIFICATION_ERROR_NONE) {
111                 ERR("Fail to notification_set_text [%d]\n", ret);
112                 goto FAIL;
113         }
114
115         ret = notification_set_pkgname(noti, APPNAME);
116         if (ret != NOTIFICATION_ERROR_NONE) {
117                 ERR("Fail to notification_set_pkgname [%d]\n", ret);
118                 goto FAIL;
119         }
120 #ifdef __ENABLE_UG_LAUNCH
121         ret = notification_set_application(noti, MH_NOTI_APP_NAME);
122         if (ret != NOTIFICATION_ERROR_NONE) {
123                 ERR("Fail to notification_set_application [%d]\n", ret);
124                 goto FAIL;
125         }
126 #endif
127         ret = notification_insert(noti, &timeout_noti_id);
128         if (ret != NOTIFICATION_ERROR_NONE) {
129                 ERR("Fail to notification_insert [%d]\n", ret);
130                 goto FAIL;
131         }
132
133         ret = notification_free(noti);
134         if (ret != NOTIFICATION_ERROR_NONE) {
135                 ERR("Fail to notification_free [%d]\n", ret);
136                 goto FAIL;
137         }
138
139         DBG("-\n");
140         return MOBILE_AP_ERROR_NONE;
141
142 FAIL:
143         ret = notification_free(noti);
144         if (ret != NOTIFICATION_ERROR_NONE)
145                 ERR("Fail to notification_free [%d]\n", ret);
146
147         return MOBILE_AP_ERROR_INTERNAL;
148 }
149
150 int _delete_timeout_noti(void)
151 {
152         notification_error_e ret = NOTIFICATION_ERROR_NONE;
153         notification_list_h noti_list = NULL;
154         notification_h noti = NULL;
155
156         ret = notification_get_detail_list(MH_AGENT_PKG_NAME,
157                                                              NOTIFICATION_GROUP_ID_NONE,
158                                                              NOTIFICATION_PRIV_ID_NONE,
159                                                              -1,
160                                                              &noti_list);
161         if (ret != NOTIFICATION_ERROR_NONE) {
162                 ERR("Fail to notification_get_detail_list\n");
163                 return MOBILE_AP_ERROR_INTERNAL;
164         }
165
166         if (noti_list) {
167                 noti = notification_list_get_data(noti_list);
168                 if (noti)
169                         notification_delete(noti);
170
171                 notification_free_list(noti_list);
172         }
173
174         return MOBILE_AP_ERROR_NONE;
175 }
176
177 int _create_connected_noti(const char *content, const char *title,
178                 const char *icon_path)
179 {
180         DBG("+\n");
181         notification_h noti = NULL;
182         notification_error_e ret = NOTIFICATION_ERROR_NONE;
183
184         noti = notification_create(NOTIFICATION_TYPE_ONGOING);
185         if (!noti) {
186                 ERR("Fail to notification_create\n");
187                 return MOBILE_AP_ERROR_INTERNAL;
188         }
189
190         ret = notification_set_property(noti,
191                         NOTIFICATION_PROP_DISABLE_AUTO_DELETE |
192 #ifdef __ENABLE_UG_LAUNCH
193                         NOTIFICATION_PROP_VOLATILE_DISPLAY);
194 #else
195                         NOTIFICATION_PROP_VOLATILE_DISPLAY |
196                         NOTIFICATION_PROP_DISABLE_APP_LAUNCH);
197 #endif
198         if (ret != NOTIFICATION_ERROR_NONE) {
199                 ERR("Fail to notification_set_property [%d]\n", ret);
200                 goto FAIL;
201         }
202
203         ret = notification_set_layout(noti, NOTIFICATION_LY_ONGOING_EVENT);
204         if (ret != NOTIFICATION_ERROR_NONE) {
205                 ERR("Fail to notification_set_image [%d]\n", ret);
206                 goto FAIL;
207         }
208
209         ret = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, icon_path);
210         if (ret != NOTIFICATION_ERROR_NONE) {
211                 ERR("Fail to notification_set_image [%d]\n", ret);
212                 goto FAIL;
213         }
214
215         ret = notification_set_text(noti,
216                         NOTIFICATION_TEXT_TYPE_TITLE,
217                         title,
218                         NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
219         if (ret != NOTIFICATION_ERROR_NONE) {
220                 ERR("Fail to notification_set_text [%d]\n", ret);
221                 goto FAIL;
222         }
223
224         ret = notification_set_text(noti,
225                         NOTIFICATION_TEXT_TYPE_CONTENT,
226                         content,
227                         NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
228         if (ret != NOTIFICATION_ERROR_NONE) {
229                 ERR("Fail to notification_set_text [%d]\n", ret);
230                 goto FAIL;
231         }
232
233         ret = notification_set_pkgname(noti, APPNAME);
234         if (ret != NOTIFICATION_ERROR_NONE) {
235                 ERR("Fail to notification_set_pkgname [%d]\n", ret);
236                 goto FAIL;
237         }
238 #ifdef __ENABLE_UG_LAUNCH
239         ret = notification_set_application(noti, MH_NOTI_APP_NAME);
240         if (ret != NOTIFICATION_ERROR_NONE) {
241                 ERR("Fail to notification_set_application [%d]\n", ret);
242                 goto FAIL;
243         }
244 #endif
245         ret = notification_insert(noti, &connected_noti_id);
246         if (ret != NOTIFICATION_ERROR_NONE) {
247                 ERR("Fail to notification_insert [%d]\n", ret);
248                 goto FAIL;
249         }
250
251         ret = notification_free(noti);
252         if (ret != NOTIFICATION_ERROR_NONE) {
253                 ERR("Fail to notification_free [%d]\n", ret);
254                 goto FAIL;
255         }
256
257         DBG("-\n");
258         return MOBILE_AP_ERROR_NONE;
259
260 FAIL:
261         ret = notification_free(noti);
262         if (ret != NOTIFICATION_ERROR_NONE)
263                 ERR("Fail to notification_free [%d]\n", ret);
264         return MOBILE_AP_ERROR_INTERNAL;
265 }
266
267 int _update_connected_noti(const char *content)
268 {
269         DBG("+\n");
270
271         if (content == NULL) {
272                 ERR("Invalid param\n");
273                 return MOBILE_AP_ERROR_INVALID_PARAM;
274         }
275
276         notification_h noti = NULL;
277         notification_error_e ret = NOTIFICATION_ERROR_NONE;
278
279         noti = notification_load(APPNAME, connected_noti_id);
280         if (noti == NULL) {
281                 ERR("notification_load is failed\n");
282                 return MOBILE_AP_ERROR_INTERNAL;
283         }
284
285         ret = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT,
286                         content, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
287         if (ret != NOTIFICATION_ERROR_NONE) {
288                 ERR("Fail to notification_set_text [%d]\n", ret);
289
290                 ret = notification_free(noti);
291                 if (ret != NOTIFICATION_ERROR_NONE)
292                         ERR("Fail to notification_free [%d]\n", ret);
293                 return MOBILE_AP_ERROR_INTERNAL;
294         }
295
296         ret = notification_update(noti);
297         if (ret != NOTIFICATION_ERROR_NONE) {
298                 ERR("Fail to notification_update [%d]\n", ret);
299
300                 ret = notification_free(noti);
301                 if (ret != NOTIFICATION_ERROR_NONE)
302                         ERR("Fail to notification_free [%d]\n", ret);
303                 return MOBILE_AP_ERROR_INTERNAL;
304         }
305
306         ret = notification_free(noti);
307         if (ret != NOTIFICATION_ERROR_NONE) {
308                 ERR("Fail to notification_free [%d]\n", ret);
309                 return MOBILE_AP_ERROR_INTERNAL;
310         }
311
312         DBG("-\n");
313         return MOBILE_AP_ERROR_NONE;
314 }
315
316 int _delete_connected_noti(void)
317 {
318         DBG("+\n");
319         notification_h noti = NULL;
320         notification_error_e ret;
321
322         noti = notification_load(APPNAME, connected_noti_id);
323         if (noti == NULL) {
324                 ERR("notification_load is failed\n");
325                 connected_noti_id = 0;
326                 return MOBILE_AP_ERROR_INTERNAL;
327         }
328         connected_noti_id = 0;
329
330         ret = notification_delete(noti);
331         if (ret != NOTIFICATION_ERROR_NONE) {
332                 ERR("Fail to notification_delete [%d]\n", ret);
333
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         ret = notification_free(noti);
341         if (ret != NOTIFICATION_ERROR_NONE) {
342                 ERR("Fail to notification_free [%d]\n", ret);
343                 return MOBILE_AP_ERROR_INTERNAL;
344         }
345
346         DBG("-\n");
347         return MOBILE_AP_ERROR_NONE;
348 }
349
350 int _create_status_noti(const char *content)
351 {
352         if (content == NULL)
353                 return MOBILE_AP_ERROR_INVALID_PARAM;
354
355         notification_error_e ret;
356
357         ret = notification_status_message_post(content);
358         if (ret != NOTIFICATION_ERROR_NONE) {
359                 ERR("notification_status_message_post() is failed : %d\n", ret);
360                 return MOBILE_AP_ERROR_INTERNAL;
361         }
362
363         return MOBILE_AP_ERROR_NONE;
364 }