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