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