tizen 2.4 release
[apps/home/quickpanel.git] / daemon / service / emergency_mode.c
1 /*
2  * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <Elementary.h>
19
20 #include <vconf.h>
21 #include <syspopup_caller.h>
22 #include <pkgmgr-info.h>
23 #include <bundle_internal.h>
24 #include <notification.h>
25 #include <notification_internal.h>
26 #include <notification_list.h>
27
28 #include <tzsh.h>
29 #include <tzsh_quickpanel_service.h>
30
31 #include "common.h"
32 #include "modules.h"
33 #include "datetime.h"
34 #include "emergency_mode.h"
35 #include "quickpanel-ui.h"
36
37 #ifdef QP_SETTING_ENABLE
38 extern QP_Module settings_view_featured;
39 #endif
40 #ifdef QP_BRIGHTNESS_ENABLE
41 extern QP_Module brightness_ctrl;
42 #endif
43
44 #define SETTING_SYSPOPUP "mode-syspopup"
45 #define BT_SHARE_DAEMON "/usr/bin/bluetooth-share"
46 #define BT_SHARE_SERVER "bluetooth-share-opp-server"
47 #define BT_SHARE_CLIENT "bluetooth-share-opp-client"
48 #define SCREEN_SHOT "shot-tizen"
49
50 static struct _info {
51         int is_enabled;
52         Eina_List *permitted_apps;
53 } s_info = {
54         .is_enabled = 0,
55         .permitted_apps = NULL,
56 };
57
58 static void _delete_unpermitted_app(void)
59 {
60         notification_list_h noti_list = NULL;
61         notification_list_h list_traverse = NULL;
62         notification_h noti = NULL;
63
64         notification_get_list(NOTIFICATION_TYPE_NONE, -1, &noti_list);
65
66         list_traverse = noti_list;
67
68         while (list_traverse != NULL) {
69                 noti = notification_list_get_data(list_traverse);
70
71                 quickpanel_emergency_mode_notification_filter(noti, 1);
72
73                 list_traverse = notification_list_get_next(list_traverse);
74         }
75
76         if (noti_list != NULL) {
77                 notification_free_list(noti_list);
78                 noti_list = NULL;
79         }
80 }
81
82 static void _emergency_mode_start(void *data)
83 {
84         struct appdata *ad = data;
85         retif(ad == NULL, , "Invalid parameter!");
86
87         if (s_info.is_enabled) {
88                 return;
89         }
90
91         quickpanel_datetime_datentime_event_set(0);
92 #ifdef QP_SETTING_ENABLE
93         if (settings_view_featured.fini != NULL) {
94                 settings_view_featured.fini(ad);
95         }
96 #endif
97 #ifdef QP_BRIGHTNESS_ENABLE
98         if (brightness_ctrl.fini != NULL) {
99                 brightness_ctrl.fini(ad);
100         }
101 #endif
102
103         _delete_unpermitted_app();
104         s_info.is_enabled = 1;
105         ERR("emergency mode is enabled");
106 }
107
108 static void _emergency_mode_stop(void *data)
109 {
110         struct appdata *ad = data;
111         retif(ad == NULL, , "Invalid parameter!");
112
113         if (!s_info.is_enabled) {
114                 return;
115         }
116
117         quickpanel_datetime_datentime_event_set(1);
118
119 #ifdef QP_SETTING_ENABLE
120         if (settings_view_featured.init != NULL) {
121                 settings_view_featured.init(ad);
122         }
123         if (settings_view_featured.init_job_cb != NULL) {
124                 settings_view_featured.init_job_cb(ad);
125         }
126 #endif
127 #ifdef QP_BRIGHTNESS_ENABLE
128         if (brightness_ctrl.init != NULL) {
129                 brightness_ctrl.init(ad);
130         }
131 #endif
132
133         _delete_unpermitted_app();
134         s_info.is_enabled = 0;
135         ERR("emergency mode is disabled");
136 }
137
138 static void _vconf_cb(keynode_t *node, void *data)
139 {
140         int mode = 0;
141
142         if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &mode) == 0) {
143                 if (mode == SETTING_PSMODE_EMERGENCY) {
144                         _emergency_mode_start(data);
145                 } else {
146                         _emergency_mode_stop(data);
147                 }
148         } else {
149                 ERR("failed to get the value of VCONFKEY_SETAPPL_PSMODE");
150         }
151 }
152
153 static int _app_list_cb(pkgmgrinfo_appinfo_h handle, void *user_data)
154 {
155         char *appid = NULL;
156         char *permitted_appid = NULL;
157         pkgmgrinfo_appinfo_get_appid(handle, &appid);
158
159         permitted_appid = strdup(appid);
160
161         s_info.permitted_apps = eina_list_append(s_info.permitted_apps, permitted_appid);
162         DBG("%s is permitted.", permitted_appid);
163
164         return 0;
165 }
166
167
168 static int _register_permitted_apps(void)
169 {
170         int ret = 0;
171         pkgmgrinfo_appinfo_filter_h handle;
172
173         s_info.permitted_apps = eina_list_append(s_info.permitted_apps, BT_SHARE_DAEMON);
174         s_info.permitted_apps = eina_list_append(s_info.permitted_apps, SCREEN_SHOT);
175         s_info.permitted_apps = eina_list_append(s_info.permitted_apps, BT_SHARE_SERVER);
176         s_info.permitted_apps = eina_list_append(s_info.permitted_apps, BT_SHARE_CLIENT);
177
178         ret = pkgmgrinfo_appinfo_filter_create(&handle);
179         if (ret != PMINFO_R_OK) {
180                 return -1;
181         }
182
183         ret = pkgmgrinfo_appinfo_filter_add_int(handle, PMINFO_APPINFO_PROP_APP_SUPPORT_MODE, 1);
184         if (ret != PMINFO_R_OK) {
185                 pkgmgrinfo_appinfo_filter_destroy(handle);
186                 return -1;
187         }
188
189         ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(handle, _app_list_cb, NULL);
190         if (ret != PMINFO_R_OK) {
191                 pkgmgrinfo_appinfo_filter_destroy(handle);
192                 return -1;
193         }
194
195         pkgmgrinfo_appinfo_filter_destroy(handle);
196         return 0;
197
198 }
199
200 static int _delete_permitted_apps(void)
201 {
202         Eina_List *list = NULL;
203         char *appid = NULL;
204
205         if (!s_info.permitted_apps) {
206                 EINA_LIST_FOREACH(s_info.permitted_apps, list, appid)
207                         free(appid);
208                 eina_list_free(s_info.permitted_apps);
209                 s_info.permitted_apps = NULL;
210         }
211
212         return 0;
213 }
214
215 HAPI void quickpanel_emergency_mode_init(void *data)
216 {
217         int ret = 0;
218         struct appdata *ad = data;
219         retif(ad == NULL, , "Invalid parameter!");
220
221         ret = _register_permitted_apps();
222         msgif(ret !=0, "failed to register permitted apps");
223
224         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
225                         _vconf_cb, ad);
226         msgif(ret != 0, "failed to notify key(VCONFKEY_SETAPPL_PSMODE) : %d", ret);
227
228         if (quickpanel_emergency_mode_is_on()) {
229                 s_info.is_enabled = 1;
230         }
231 }
232
233 HAPI void quickpanel_emergency_mode_fini(void *data)
234 {
235         int ret = 0;
236         struct appdata *ad = data;
237         retif(ad == NULL, , "Invalid parameter!");
238
239         ret = _delete_permitted_apps();
240         msgif(ret !=0, "failed to delete permitted apps");
241
242         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE, _vconf_cb);
243         msgif(ret != 0, "failed to ignore key(VCONFKEY_SETAPPL_PSMODE) : %d", ret);
244 }
245
246 HAPI int quickpanel_emergency_mode_is_permitted_app(const char *appid)
247 {
248         int i = 0;
249         int count = 0;
250         char *permitted_app = NULL;
251         retif(appid == NULL, 0, "Invalid parameter!");
252
253         count = eina_list_count(s_info.permitted_apps);
254         for(i = 0; i < count; i++) {
255                 permitted_app = (char *)eina_list_nth(s_info.permitted_apps, i);
256                 if (permitted_app != NULL && strcmp(permitted_app, appid) == 0) {
257                         return 1;
258                 }
259         }
260
261         return 0;
262 }
263
264 HAPI int quickpanel_emergency_mode_is_on(void)
265 {
266         int mode = 0;
267
268         if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &mode) == 0) {
269                 if (mode == SETTING_PSMODE_EMERGENCY) {
270                         return 1;
271                 }
272         }
273
274         return 0;
275 }
276
277 HAPI int quickpanel_emergency_mode_notification_filter(notification_h noti, int is_delete)
278 {
279         int priv_id = 0;
280         char *pkgname = NULL;
281
282         notification_get_pkgname(noti, &pkgname);
283         notification_get_id(noti, NULL, &priv_id);
284
285         DBG("Emergency mode filter is called: %s", pkgname);
286         if (!quickpanel_emergency_mode_is_permitted_app(pkgname)) {
287                 if (is_delete) {
288                         notification_delete_by_priv_id(pkgname, NOTIFICATION_TYPE_NONE, priv_id);
289                 }
290                 return 1;
291         }
292
293         return 0;
294 }
295
296 HAPI int quickpanel_emergency_mode_syspopup_launch(void)
297 {
298         int ret;
299         bundle *b = NULL;
300
301         DBG("");
302
303         b = bundle_create();
304         if (b == NULL) {
305                 return QP_FAIL;
306         }
307
308         bundle_add(b, "_MODE_SYSTEM_POPUP_TYPE_", "POPUP_EMERGENCY_PSMODE");
309         ret = syspopup_launch(SETTING_SYSPOPUP, b);
310         if (ret < 0) {
311                 ERR("failed to launch syspopup (%s):%d\n", SETTING_SYSPOPUP, ret);
312                 bundle_free(b);
313                 return QP_FAIL;
314         }
315
316         DBG("");
317
318         bundle_free(b);
319         return QP_OK;
320 }