ad9c20991e8d4927b5b97a795df859f556bdcc79
[apps/core/preloaded/quickpanel.git] / daemon / settings / settings.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
19 #include <stdlib.h>
20 #include <glib.h>
21 #include <Elementary.h>
22
23 #include <vconf.h>
24 #include <notification.h>
25 #include <tzsh.h>
26 #include <tzsh_quickpanel_service.h>
27 #include <E_DBus.h>
28
29 #include "common.h"
30 #include "quickpanel-ui.h"
31 #include "quickpanel_def.h"
32 #include "modules.h"
33 #include "settings.h"
34 #include "setting_utils.h"
35 #include "settings_ipc.h"
36 #include "pager.h"
37 #include "pager_common.h"
38 #include "preference.h"
39
40 #ifdef QP_SCREENREADER_ENABLE
41 #include "accessibility.h"
42 #endif
43
44 #ifdef QP_EMERGENCY_MODE_ENABLE
45 #include "emergency_mode.h"
46 #endif
47
48 static int quickpanel_settings_init(void *data);
49 static int quickpanel_settings_fini(void *data);
50 static int quickpanel_settings_suspend(void *data);
51 static int quickpanel_settings_resume(void *data);
52 static void quickpanel_settings_lang_changed(void *data);
53 static void quickpanel_settings_reflesh(void *data);
54 static Eina_Bool _module_is_enabled(QP_Module_Setting *module);
55
56 extern QP_Module_Setting wifi;
57 extern QP_Module_Setting gps;
58 extern QP_Module_Setting bluetooth;
59 extern QP_Module_Setting sound;
60 extern QP_Module_Setting rotate;
61 extern QP_Module_Setting mobile_data;
62 extern QP_Module_Setting flightmode;
63 extern QP_Module_Setting u_power_saving;
64 extern QP_Module_Setting tethering;
65 extern QP_Module_Setting assistive_light;
66
67 QP_Module settings = {
68         .name = "settings",
69         .init = quickpanel_settings_init,
70         .fini = quickpanel_settings_fini,
71         .suspend = quickpanel_settings_suspend,
72         .resume = quickpanel_settings_resume,
73         .lang_changed = quickpanel_settings_lang_changed,
74         .refresh = quickpanel_settings_reflesh,
75 };
76
77 static struct _info {
78         GHashTable *module_table;
79         QP_Module_Setting *modules[];
80 } s_info = {
81         .module_table = NULL,
82         .modules = {
83                 &wifi,
84                 &gps,
85                 &sound,
86                 &rotate,
87                 &bluetooth,
88                 NULL,
89         },
90 };
91
92 static void _module_init(QP_Module_Setting *module)
93 {
94         if (module->init != NULL) {
95                 module->loader = (QP_Setting_Loaded_Item *)calloc(1, sizeof(QP_Setting_Loaded_Item));
96                 module->init(module);
97                 module->is_loaded = EINA_TRUE;
98         }
99 }
100
101 static void _module_fini(QP_Module_Setting *module)
102 {
103         if (module->fini != NULL) {
104                 module->fini(module);
105                 if (module->loader != NULL) {
106                         free(module->loader);
107                         module->loader = NULL;
108                         module->is_loaded = EINA_FALSE;
109                 }
110         }
111 }
112
113 static int _module_count_get(void)
114 {
115         int i, cnt = 0;
116
117         for (i = 0; s_info.modules[i] != NULL; i++) {
118                 cnt++;
119         }
120
121         return cnt;
122 }
123
124 static QP_Module_Setting *_module_get_by_name(const char *name)
125 {
126         retif(name == NULL, NULL, "invalid parameter");
127         retif(s_info.module_table == NULL, NULL, "invalid parameter");
128
129         return g_hash_table_lookup(s_info.module_table, name);
130 }
131
132 static Eina_Bool _module_is_enabled(QP_Module_Setting *module) {
133         retif(module == NULL, EINA_FALSE, "invalid parameter");
134         retif(module->name == NULL, EINA_FALSE, "invalid parameter");
135
136         if (strcmp(module->name, MODULE_BLANK) == 0) {
137                 return EINA_FALSE;
138         }
139         if (module->supported_get) {
140                 if (module->supported_get() == 0)
141                         return EINA_FALSE;
142         }
143         return EINA_TRUE;
144 }
145
146 static char *_preference_get(const char *key)
147 {
148         char line[PREF_LEN_VALUE_MAX + 1] = {0,};
149
150         if (quickpanel_preference_get(key, line) == QP_OK) {
151                 DBG("quicksetting order from file:%s", line);
152                 return strdup(line);
153         }
154
155         return NULL;
156 }
157
158 static int quickpanel_settings_init(void *data)
159 {
160         int i;
161         int mod_count = 0;
162         struct appdata *ad = data;
163         retif(ad == NULL, QP_FAIL, "Invalid parameter!");
164
165         mod_count = _module_count_get();
166         if (s_info.module_table != NULL) {
167                 g_hash_table_remove_all(s_info.module_table);
168                 g_hash_table_destroy(s_info.module_table);
169                 s_info.module_table = NULL;
170         }
171         s_info.module_table = g_hash_table_new_full(g_str_hash, g_str_equal,
172                         (GDestroyNotify)g_free,
173                         NULL);
174         if (s_info.module_table != NULL) {
175                 for (i = 0; i < mod_count; i++) {
176                         if (s_info.modules[i]->supported_get != NULL) {
177                                 if (s_info.modules[i]->supported_get() == 0) {
178                                         continue;
179                                 }
180                         }
181
182                         if (s_info.modules[i]->init != NULL && s_info.modules[i]->name != NULL) {
183                                 ERR("quickbutton %s is initialized", s_info.modules[i]->name);
184                                 DBG("quickbutton %s is initialized", s_info.modules[i]->name);
185                                 g_hash_table_insert(s_info.module_table,
186                                                 g_strdup(s_info.modules[i]->name),
187                                                 s_info.modules[i]);
188                                 _module_init(s_info.modules[i]);
189                         }
190                 }
191         } else {
192                 ERR("failed to create module has table");
193                 return QP_FAIL;
194         }
195
196         quickpanel_settings_ipc_init(ad);
197
198         return QP_OK;
199 }
200
201 static int quickpanel_settings_fini(void *data)
202 {
203         int i;
204         int ret = 0;
205         struct appdata *ad = data;
206         retif(ad == NULL, QP_FAIL, "Invalid parameter!");
207
208         quickpanel_settings_ipc_fini(ad);
209
210         for (i = 0; s_info.modules[i] != NULL; i++) {
211                 if (_module_is_enabled(s_info.modules[i]) == EINA_TRUE) {
212                         _module_fini(s_info.modules[i]);
213                 }
214         }
215
216         if (s_info.module_table) {
217                 g_hash_table_remove_all(s_info.module_table);
218                 g_hash_table_destroy(s_info.module_table);
219                 s_info.module_table = NULL;
220         }
221
222         return ret;
223 }
224
225 static int quickpanel_settings_suspend(void *data)
226 {
227         int i;
228         int ret = 0;
229         struct appdata *ad = data;
230         retif(ad == NULL, QP_FAIL, "Invalid parameter!");
231
232         for (i = 0; s_info.modules[i] != NULL; i++) {
233                 if (_module_is_enabled(s_info.modules[i]) == EINA_TRUE) {
234                         if ((s_info.modules[i])->suspend != NULL) {
235                                 (s_info.modules[i])->suspend(s_info.modules[i]);
236                         }
237                 }
238         }
239
240         return ret;
241 }
242
243 static int quickpanel_settings_resume(void *data)
244 {
245         int i;
246         int ret = 0;
247         struct appdata *ad = data;
248         retif(ad == NULL, QP_FAIL, "Invalid parameter!");
249
250         for (i = 0; s_info.modules[i] != NULL; i++) {
251                 if (_module_is_enabled(s_info.modules[i]) == EINA_TRUE) {
252                         if ((s_info.modules[i])->resume != NULL) {
253                                 (s_info.modules[i])->resume(s_info.modules[i]);
254                         }
255                 }
256         }
257
258         return ret;
259 }
260
261 static void quickpanel_settings_lang_changed(void *data)
262 {
263         int i;
264         struct appdata *ad = data;
265         retif(ad == NULL, , "Invalid parameter!");
266
267         for (i = 0; s_info.modules[i] != NULL; i++) {
268                 if (_module_is_enabled(s_info.modules[i]) == EINA_TRUE) {
269                         if ((s_info.modules[i])->lang_changed != NULL) {
270                                 (s_info.modules[i])->lang_changed(s_info.modules[i]);
271                         }
272                 }
273         }
274 }
275
276 static void quickpanel_settings_reflesh(void *data)
277 {
278         int i;
279         struct appdata *ad = data;
280         retif(ad == NULL, , "Invalid parameter!");
281
282         for (i = 0; s_info.modules[i] != NULL; i++) {
283                 if (_module_is_enabled(s_info.modules[i]) == EINA_TRUE) {
284                         if ((s_info.modules[i])->refresh != NULL) {
285                                 (s_info.modules[i])->refresh(s_info.modules[i]);
286                         }
287                 }
288         }
289 }
290
291 HAPI int quickpanel_settings_featured_list_validation_check(char *order)
292 {
293         int i = 0, is_valid = 0;
294         int order_count = 0;
295         gchar **order_split = NULL;
296         QP_Module_Setting *mod = NULL;
297         retif(order == NULL, is_valid, "Invalid parameter!");
298
299         if (s_info.module_table == NULL) {
300                 return is_valid;
301         }
302
303         order_split = g_strsplit(order, ",", 0);
304
305         if (order_split != NULL) {
306                 order_count = g_strv_length(order_split);
307
308                 if (order_count >= QP_SETTING_NUM_MINIMUM_ICON) {
309                         for (i = 0; i < order_count; i++) {
310                                 mod = _module_get_by_name(order_split[i]);
311                                 if (mod != NULL) {
312                                         is_valid = 1;
313                                 } else {
314                                         is_valid = 0;
315                                         break;
316                                 }
317                         }
318                 }
319
320                 g_strfreev(order_split);
321         }
322
323         return is_valid;
324 }
325
326 HAPI void quickpanel_settings_featured_list_get(Eina_List **list)
327 {
328         int i = 0, seq_count = 0;
329         int num_featured = 0;
330         int seq_added_count = 0;
331         gchar **params = NULL;
332         QP_Module_Setting *module = NULL;
333         retif(list == NULL, , "invalid data.");
334         char *sequence = _preference_get(PREF_QUICKSETTING_ORDER);
335         const char *default_sequence = quickpanel_preference_default_get(PREF_QUICKSETTING_ORDER);
336
337         char *num_featured_str =  _preference_get(PREF_QUICKSETTING_FEATURED_NUM);
338         const char *default_num_featured_str = quickpanel_preference_default_get(PREF_QUICKSETTING_FEATURED_NUM);
339
340         if (sequence != NULL) {
341                 params = g_strsplit(sequence, ",", 0);
342                 free(sequence);
343         } else {
344                 params = g_strsplit(default_sequence, ",", 0);
345         }
346
347         if (num_featured_str != NULL) {
348                 num_featured = atoi(num_featured_str);
349                 free(num_featured_str);
350         } else {
351                 if (default_num_featured_str != NULL) {
352                         num_featured = atoi(default_num_featured_str);
353                 } else {
354                         num_featured = QP_SETTING_NUM_TOTAL_ICON;
355                 }
356         }
357
358         if (params != NULL) {
359                 seq_count = g_strv_length(params);
360
361                 for (i = 0; i < seq_count; i++) {
362                         if (seq_added_count >= num_featured){
363                                 break;
364                         }
365
366                         module = _module_get_by_name(params[i]);
367                         if (module != NULL) {
368                                 if (_module_is_enabled(module) == EINA_TRUE) {
369                                         *list = eina_list_append (*list, module);
370                                         seq_added_count++;
371                                 }
372                         }
373                 }
374
375                 g_strfreev(params);
376         }
377 }
378
379 HAPI void quickpanel_settings_all_list_get(Eina_List **list)
380 {
381         int i = 0, seq_count = 0;
382         gchar **params = NULL;
383         QP_Module_Setting *module = NULL;
384         retif(list == NULL, , "invalid data.");
385         char *sequence = _preference_get(PREF_QUICKSETTING_ORDER);
386         const char *default_sequence = quickpanel_preference_default_get(PREF_QUICKSETTING_ORDER);
387
388         if (sequence != NULL) {
389                 params = g_strsplit(sequence, ",", 0);
390                 free(sequence);
391         } else if (default_sequence != NULL){
392                 params = g_strsplit(default_sequence, ",", 0);
393         }
394
395         if (params != NULL) {
396                 seq_count = g_strv_length(params);
397
398                 for (i = 0; i < seq_count; i++) {
399                         module = _module_get_by_name(params[i]);
400                         if (module != NULL) {
401                                 if (_module_is_enabled(module) == EINA_TRUE) {
402                                         *list = eina_list_append (*list, module);
403                                 }
404                         }
405                 }
406
407                 g_strfreev(params);
408         }
409 }
410
411 HAPI void quickpanel_setting_save_list_to_file(Eina_List *list, int num_featured)
412 {
413         Eina_List *l;
414         Eina_List *l_next;
415         QP_Module_Setting *module = NULL;
416         char buf[32] = {0,};
417         retif(list == NULL, , "invalid parameter");
418
419         int is_first = 1;
420
421         char *base = NULL;
422         char *temp = NULL;
423
424         EINA_LIST_FOREACH_SAFE(list, l, l_next, module) {
425                 if (module == NULL){
426                         continue;
427                 }
428                 if (module->name == NULL) {
429                         continue;
430                 }
431                 if (_module_is_enabled(module) == EINA_FALSE) {
432                         continue;
433                 }
434
435                 if (is_first == 1) {
436                         base = g_strdup(module->name);
437                         is_first = 0;
438                 } else {
439                         temp = g_strconcat(base, ",", module->name, NULL);
440                         if (base != NULL) g_free(base);
441                         base = temp;
442                         temp = NULL;
443                 }
444         }
445
446         if (base != NULL) {
447                 if (quickpanel_preference_set(PREF_QUICKSETTING_ORDER, base) == QP_FAIL) {
448                         ERR("failed to write quicksetting order");
449                 }
450                 g_free(base);
451                 snprintf(buf, sizeof(buf) - 1, "%d", num_featured);
452                 if (quickpanel_preference_set(PREF_QUICKSETTING_FEATURED_NUM, buf) == QP_FAIL) {
453                         ERR("failed to write quicksetting featured num");
454                 }
455         }
456 }
457
458 HAPI QP_Module_Setting *quickpanel_settings_module_get_by_name(const char *name)
459 {
460         return _module_get_by_name(name);
461 }
462
463 HAPI int quickpanel_settings_module_count_get(void)
464 {
465         return _module_count_get();
466 }