tizen 2.4 release
[apps/home/settings.git] / setting-common / src / setting-cfg.c
1 /*
2  * setting
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5  *
6  * Contact: MyoungJune Park <mj2004.park@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <setting-cfg.h>
23 #include <setting-debug.h>
24 #include <stdio.h>
25 #include <Elementary.h>
26 #include <setting-common-general-func.h>
27 #include <setting-common-data-type.h>
28 #include <setting-common-data-slp-setting.h>
29 #include <unistd.h>
30 #include <vconf.h>
31
32 #define CFG_FILE_DIR_PATH       "/opt/usr/apps/org.tizen.setting/data/"
33 #define CFG_FILE_PATH           CFG_FILE_DIR_PATH"setting.cfg"
34
35 JsonParser *parser;
36 JsonNode   *root; /* category_list */
37
38 EXPORT_PUBLIC
39 char *setting_cfg_get_path()
40 {
41         return CFG_FILE_PATH;
42 }
43
44 int setting_cfg_file_write(JsonNode *node);
45
46 #define __create_an_item(item_name, icon_path, ug_args, shortcut_appid, defaultPos, item_type,  is_resetable, ug_exist, uuid, click_times, last_clicked) do\
47         {\
48                 if (ug_exist) { \
49                         if (!is_ug_installed_by_ug_args(ug_args)) break;\
50                 } \
51                 menu_item = json_node_new(JSON_NODE_OBJECT); \
52                 object = json_object_new(); \
53                 json_node_take_object(menu_item, object); \
54                 json_object_set_string_member(object, "key_name", item_name); \
55                 json_object_set_string_member(object, "icon_path", icon_path); \
56                 json_object_set_string_member(object, "ug_args", ug_args); \
57                 json_object_set_string_member(object, "shortcut_appid", shortcut_appid); \
58                 json_object_set_int_member(object, "pos", defaultPos); \
59                 json_object_set_int_member(object, "click_times", click_times); \
60                 json_object_set_int_member(object, "item_type", item_type); \
61                 json_object_set_int_member(object, "is_resetable", is_resetable); \
62                 json_object_set_int_member(object, "last_clicked", last_clicked); \
63                 json_object_set_string_member(object, "uuid", uuid); \
64                 json_array_add_element(menu, menu_item); \
65         } while (0);\
66
67 #define __create_a_menu(menu_name)\
68         {\
69                 category = json_node_new(JSON_NODE_OBJECT);\
70                 object = json_object_new();\
71                 json_node_take_object(category, object);\
72                 json_object_set_string_member(object, "name", menu_name);\
73                 menu = json_array_new();\
74                 json_object_set_array_member(object, "menu", menu);\
75                 json_array_add_element(json_node_get_array(category_list), category);\
76         }
77
78
79 int wifi_toggle_get_state(Cfg_Item_State *stat, void *data);
80 int flightmode_toggle_get_state(Cfg_Item_State *stat, void *data);
81 int bluetooth_toggle_get_state(Cfg_Item_State *stat, void *data);
82 int drivingmode_toggle_get_state(Cfg_Item_State *stat, void *data);
83 int network_restriction_mode_toggle_get_state(Cfg_Item_State *stat, void *data);
84 int personalmode_toggle_get_state(Cfg_Item_State *stat, void *data);
85 int handfree_toggle_get_state(Cfg_Item_State *stat, void *data);
86 int nfc_toggle_get_state(Cfg_Item_State *stat, void *data);
87
88 /*/////////////////////////////////////////////////////////////////////////////////////////// */
89 /* wifi - DONE */
90 EXPORT_PUBLIC
91 cfg_func_table wifi_tfunc = {
92         .get_item_state = wifi_toggle_get_state,
93         .set_item_state = NULL,
94         .set_item_update_ui = NULL,
95 };
96
97 /* flight mode - DONE */
98 EXPORT_PUBLIC
99 cfg_func_table flightmode_tfunc = {
100         .get_item_state = flightmode_toggle_get_state,
101         .set_item_state = NULL,
102         .set_item_update_ui = NULL,
103 };
104
105 /* bluetooth - ***** FAILED ***** */
106 EXPORT_PUBLIC
107 cfg_func_table bluetooth_tfunc = {
108         .get_item_state = bluetooth_toggle_get_state,
109         .set_item_state = NULL,
110         .set_item_update_ui = NULL,
111 };
112
113
114 /* driving mode - Done */
115 EXPORT_PUBLIC
116 cfg_func_table drivingmode_tfunc = {
117         .get_item_state = drivingmode_toggle_get_state,
118         .set_item_state = NULL,
119         .set_item_update_ui = NULL,
120 };
121
122 /* network restrictrion mode - Done */
123 EXPORT_PUBLIC
124 cfg_func_table network_restruction_mode_tfunc = {
125         .get_item_state = network_restriction_mode_toggle_get_state,
126         .set_item_state = NULL,
127         .set_item_update_ui = NULL,
128 };
129
130 /* personal mode ?? - partially done */
131 EXPORT_PUBLIC
132 cfg_func_table personalmode_tfunc = {
133         .get_item_state = personalmode_toggle_get_state,
134         .set_item_state = NULL,
135         .set_item_update_ui = NULL,
136 };
137
138 /* NFC - ??? */
139 EXPORT_PUBLIC
140 cfg_func_table nfc_tfunc = {
141         .get_item_state = nfc_toggle_get_state,
142         .set_item_state = NULL,
143         .set_item_update_ui = NULL,
144 };
145
146
147 EXPORT_PUBLIC
148 void setting_cfg_print(void)
149 {
150         JsonNode *node = root;
151
152         JsonGenerator *generator = json_generator_new();
153         g_object_set(generator, "pretty", TRUE, NULL);
154         json_generator_set_root(generator, node);
155         gchar *data = json_generator_to_data(generator, NULL);
156         SETTING_TRACE("%s", (char *)data);
157
158         g_free(data);
159         g_object_unref(generator);
160 }
161
162
163 /* create config file from scratch */
164 static Setting_Cfg_Node_T s_cfg_node_array[] = {
165         {KeyStr_Connections, NULL, "move://Tab2.top", Cfg_Item_Pos_Level0, 0, 0, Cfg_Item_Title_Node, NULL, NULL, NULL, uuid_Connectivity, 0, NULL},
166         {KeyStr_WiFi, IMG_WiFi, "wifi-efl-ug", Cfg_Item_Pos_Level0, Cfg_Item_Resetable,   0, Cfg_Item_AppLauncher_Node, NULL, KeyStr_Connections, &wifi_tfunc, uuid_WiFi, 0, "wifi-efl-ug"},
167         {KeyStr_Bluetooth, IMG_Bluetooth, "ug-bluetooth-efl", Cfg_Item_Pos_Level0, Cfg_Item_Resetable,  0, Cfg_Item_AppLauncher_Node, NULL, KeyStr_Connections, &bluetooth_tfunc, uuid_Bluetooth, 0, "ug-bluetooth-efl"},
168 #ifdef TIZEN_BUILD_TARGET
169         {KeyStr_FlightMode, IMG_FlightMode, "setting-flightmode-efl", Cfg_Item_Pos_Level0, Cfg_Item_unResetable,  0, Cfg_Item_Ui_Node_Toggle, NULL, KeyStr_Connections, &flightmode_tfunc, uuid_FlightMode, 0, NULL},
170 #endif
171 #ifdef TIZEN_BUILD_TARGET
172         {KeyStr_MobileAP, IMG_MobileAP, "setting-mobileap-efl", Cfg_Item_Pos_Level0, Cfg_Item_Resetable,  0, Cfg_Item_AppLauncher_Node, NULL, KeyStr_Connections, NULL, uuid_MobileAP, 0, NULL},
173 #ifdef _M0
174         {KeyStr_NFC, IMG_NFC, "setting-nfc-efl|type:nfc", Cfg_Item_Pos_Level0, Cfg_Item_unResetable,  0, Cfg_Item_Ug_Node_Toggle, NULL, KeyStr_Connections, &nfc_tfunc, uuid_NFC, 0, "setting-nfc-efl"},
175 #endif
176 #endif
177 #ifdef TIZEN_BUILD_TARGET
178         {KeyStr_MobileNetworks, IMG_Network, "setting-network-efl", Cfg_Item_Pos_Level0, Cfg_Item_Resetable, 0,  Cfg_Item_Ug_Node, NULL, KeyStr_Connections, NULL, uuid_Network, 0, NULL},
179         /*More connections */
180 #endif
181         {KeyStr_MoreConnections, IMG_More_Connections, "setting-moreconnections-efl", Cfg_Item_Pos_Level0, Cfg_Item_Resetable, 0,  Cfg_Item_Ug_Node, NULL, KeyStr_Connections, NULL, uuid_Network, 0, NULL},
182
183         /* Group:Device */
184         {KeyStr_Device, NULL, "move://Tab3.top", Cfg_Item_Pos_Level0, 0, 0, Cfg_Item_Title_Node, NULL, NULL, NULL, uuid_SoundDisplay, 0, NULL},
185         {KeyStr_Sounds, IMG_Sounds, "setting-profile-efl", Cfg_Item_Pos_Level0, Cfg_Item_Resetable, 0, Cfg_Item_Ug_Node, NULL, KeyStr_Device, NULL, uuid_SoundDisplay, 0, NULL},
186         {KeyStr_Display, IMG_Display, "setting-display-efl|viewtype:main", Cfg_Item_Pos_Level0, Cfg_Item_Resetable, 0, Cfg_Item_Ug_Node, NULL, KeyStr_Device, NULL, uuid_Display, 0, "org.tizen.setting.display"},
187         {KeyStr_ApplicationsItem, IMG_Applications, "setting-applications-efl", Cfg_Item_Pos_Level0, Cfg_Item_Resetable, 0, Cfg_Item_Ug_Node, NULL, KeyStr_Device, NULL, uuid_Display, 0, "org.tizen.setting.display"},
188
189         /* Group: Personal */
190         {KeyStr_Personal, NULL, "move://Tab3.top", Cfg_Item_Pos_Level0, 0, 0, Cfg_Item_Title_Node, NULL, NULL, NULL, uuid_Personal, 0, NULL},
191         {KeyStr_Wallpaper, IMG_Wallpaper, "org.tizen.wallpaper-ui-service", Cfg_Item_Pos_Level0, Cfg_Item_Resetable, 0, Cfg_Item_AppLauncher_Node, NULL, KeyStr_Personal, NULL, uuid_HomeAndLockscreen, 0, "org.tizen.setting.wallpaper-ui-service"},
192         {KeyStr_LockScreen, IMG_LockedScreen, "setting-security-efl", Cfg_Item_Pos_Level0, Cfg_Item_Resetable, 0, Cfg_Item_AppLauncher_Node, NULL, KeyStr_Personal, NULL, uuid_LockedScreen, 0, "ug-lockscreen-options-efl"},
193 #if 0
194         /*Accessbility - not supported since 2015.8.12 */
195         {KeyStr_Accessibility, IMG_Accessibility, "setting-accessibility-efl", Cfg_Item_Pos_Level0, Cfg_Item_unResetable, 0, Cfg_Item_AppLauncher_Node, NULL, KeyStr_Personal, NULL, uuid_Accounts, 0, "setting-accessibility-efl"},
196 #endif
197         {KeyStr_Accounts, IMG_Accounts, "setting-myaccount-efl|mode:account_list", Cfg_Item_Pos_Level0, Cfg_Item_unResetable, 0, Cfg_Item_AppLauncher_Node, NULL, KeyStr_Personal, NULL, uuid_Accounts, 0, "setting-myaccount-efl"},
198
199         /* Group: System */
200         {KeyStr_System, NULL, "move://Tab4.top", Cfg_Item_Pos_Level0, 0, 0, Cfg_Item_Title_Node, NULL, NULL, NULL, uuid_DeviceMange, 0,  NULL},
201         {KeyStr_LanguageInput, IMG_LanguageInput, "setting-phone-efl|viewtype:language", Cfg_Item_Pos_Level0, Cfg_Item_Resetable, 0, Cfg_Item_AppLauncher_Node, NULL, KeyStr_System, NULL, uuid_LanguageKeyboard, 0, "org.tizen.setting.language"},
202 #if SUPPORT_STORAGE
203         {KeyStr_Storage, IMG_Storage, "setting-storage-efl|caller:setting", Cfg_Item_Pos_Level0, Cfg_Item_unResetable, 0, Cfg_Item_AppLauncher_Node, NULL, KeyStr_System, NULL, uuid_Storage, 0, "setting-storage-efl"},
204 #endif
205         {KeyStr_DateTime, IMG_DateTime, "setting-time-efl", Cfg_Item_Pos_Level0, Cfg_Item_Resetable, 0, Cfg_Item_Ug_Node, NULL, KeyStr_System, NULL, uuid_DateTime, 0, NULL},
206         //{KeyStr_DeveloperOption, IMG_USBconnection, "setting-developeroption-efl|viewtype:usb", Cfg_Item_Pos_Level0, Cfg_Item_Resetable, 0, Cfg_Item_Ug_Node, NULL, KeyStr_DeviceMange, NULL, uuid_DeveloperOption, 0, "org.tizen.setting.developeroptions"},
207         {KeyStr_AboutDevice, IMG_AboutDevice, "setting-about-efl", Cfg_Item_Pos_Level0, Cfg_Item_Resetable, 0, Cfg_Item_Ug_Node, NULL, KeyStr_System, NULL, uuid_AboutPhone, 0, NULL},
208
209 #if 0
210         {KeyStr_Security, IMG_Security, "setting-security-efl", Cfg_Item_Pos_Level0, Cfg_Item_Resetable, 0, Cfg_Item_Ug_Node, NULL, KeyStr_DeviceMange, NULL, uuid_Security, 0,  NULL},
211         /* Downloaded App */
212         {KeyStr_DownloadedAPPs, NULL, "move://Tab6.top", Cfg_Item_Pos_Level0, 0, 0, Cfg_Item_Title_Node, NULL, NULL, NULL, uuid_APP, 0, NULL},
213 #endif
214 };
215
216 EXPORT_PUBLIC Setting_Cfg_Node_T *get_cfg_node_by_keystr(const char *keystr)
217 {
218         int size = get_cfg_array_size();
219
220         int i;
221         for (i = 0; i < size; i++) {
222                 #if 0
223                 SETTING_TRACE("keystr: %s",keystr);
224                 SETTING_TRACE("s_cfg_node_array[i].key_name: %s",s_cfg_node_array[i].key_name);
225                 SETTING_TRACE("---------> _(keystr): %s",_(keystr));
226                 SETTING_TRACE("---------> _(s_cfg_node_array[i].key_name): %s",_(s_cfg_node_array[i].key_name));
227                 #endif
228                 if (0 == safeStrCmp(_(keystr), _(s_cfg_node_array[i].key_name))) {
229                 #if 0
230                         SETTING_TRACE("MATCH !!!!!!!!!!!!!!");
231                 #endif
232                         return &(s_cfg_node_array[i]);
233                 }
234         }
235         return NULL;
236 }
237
238 EXPORT_PUBLIC int get_index_by_keystr(const char *keystr)
239 {
240         int size = get_cfg_array_size();
241         /*SETTING_TRACE(" size of array : %d ", size); */
242
243         int i;
244         /*SETTING_TRACE("keystr:%s", keystr); */
245         for (i = 0; i < size; i++) {
246                 /*SETTING_TRACE("s_cfg_node_array[%d].key_name:%s", i, s_cfg_node_array[i].key_name); */
247                 if (0 == safeStrCmp(keystr, s_cfg_node_array[i].key_name)) {
248                         return i;
249                 }
250         }
251         /*not found */
252         return -1;
253 }
254
255
256 EXPORT_PUBLIC char *get_iconpath_by_keystr(const char *keystr)
257 {
258         int size = get_cfg_array_size();
259
260         int i;
261         /*SETTING_TRACE("keystr:%s", keystr); */
262         for (i = 0; i < size; i++) {
263                 if (0 == safeStrCmp(keystr, _(s_cfg_node_array[i].key_name))) {
264                         if (s_cfg_node_array[i].icon_path) {
265                                 return s_cfg_node_array[i].icon_path;
266                         } else {
267                                 goto END;
268                         }
269                 }
270         }
271
272 END:
273         return IMG_SETTING;
274 }
275
276 /* plistitem->title */
277 EXPORT_PUBLIC void *get_node_pointer(char *name)
278 {
279         int size = get_cfg_array_size();
280
281         int i;
282         for (i = 0; i < size; i++) {
283                 if (0 == safeStrCmp(name, _(s_cfg_node_array[i].key_name))) {
284                         return s_cfg_node_array[i].data;
285                 }
286         }
287         return NULL;
288 }
289
290
291 EXPORT_PUBLIC int set_node_pointer(char *name, void *ptr)
292 {
293         int size = get_cfg_array_size();
294
295         int i;
296         for (i = 0; i < size; i++) {
297                 if (0 == safeStrCmp(name, s_cfg_node_array[i].key_name)) {
298                         /*return i; */
299                         s_cfg_node_array[i].data = ptr;
300                         return 0;
301                 }
302         }
303
304         return -1;
305 }
306
307 EXPORT_PUBLIC int set_cfg_node_tfunc(char *name, void *ptr)
308 {
309         int size = get_cfg_array_size();
310
311         int i;
312         for (i = 0; i < size; i++) {
313                 if (0 == safeStrCmp(_(name), _(s_cfg_node_array[i].key_name))) {
314                         /*return i; */
315                         s_cfg_node_array[i].tfunc = (cfg_func_table *)ptr;
316                         return 0;
317                 }
318         }
319
320         return -1;
321 }
322
323 EXPORT_PUBLIC int set_cfg_node_tfunc_set_ptr(char *name, int (*set_item_state)(Cfg_Item_State stat, void *item, void *data))
324 {
325         int size = get_cfg_array_size();
326
327         int i;
328         for (i = 0; i < size; i++) {
329                 if (0 == safeStrCmp(_(name), _(s_cfg_node_array[i].key_name))) {
330                         /*return i; */
331                         s_cfg_node_array[i].tfunc->set_item_state = set_item_state;
332                         return 0;
333                 }
334         }
335
336         return -1;
337 }
338
339 EXPORT_PUBLIC
340 Setting_Cfg_Node_T *get_cfg_node_first()
341 {
342         return &s_cfg_node_array[0];
343 }
344
345 EXPORT_PUBLIC
346 int get_cfg_array_size()
347 {
348         int arr_size = sizeof(s_cfg_node_array);
349         int node_size = sizeof(s_cfg_node_array[0]);
350
351         return arr_size / node_size;
352 }
353
354 /**
355 * @brief
356
357 Cfg_Item_Title_Node
358 Cfg_Item_App_Node
359 Cfg_Item_Ui_Node
360 Cfg_Item_Ug_Node
361 *
362 * @param check_ug
363 *
364 * @return
365 */
366 EXPORT_PUBLIC
367 int setting_cfg_create(bool check_ug_exist)
368 {
369         SETTING_TRACE_BEGIN;
370         JsonNode *category_list, *category, *menu_item;
371         JsonArray *menu = NULL;
372         JsonObject *object = NULL;
373
374         /* Category list */
375         category_list = json_node_new(JSON_NODE_ARRAY);
376         json_node_take_array(category_list, json_array_new());
377
378         Setting_Cfg_Node_T *pitem = get_cfg_node_first();       /* head */
379         int size = get_cfg_array_size();
380         SETTING_TRACE(" size of array : %d ", size);
381
382         int i;
383         for (i = 0; i < size; i++) {
384                 /* Check Emul and block Wifi & Bluetooth menu */
385                 if (isEmulBin() &&
386                     (!safeStrCmp(pitem[i].key_name, KeyStr_Bluetooth) || !safeStrCmp(pitem[i].key_name, KeyStr_WiFi))) {
387                         SETTING_TRACE("Skip %s", pitem[i].key_name);
388                         continue;
389                 }
390
391                 SETTING_TRACE(" index : %d ", i);
392                 Cfg_Item_Type item_type = pitem[i].item_type % 10;
393                 if (item_type == Cfg_Item_Title_Node) {
394                         SETTING_TRACE(" add title : %s ", pitem[i].key_name);
395
396                         /* __create_a_menu HEADER */
397                         __create_a_menu(pitem[i].key_name);
398
399                 } else if (item_type == Cfg_Item_App_Node
400                            || item_type == Cfg_Item_Ui_Node
401                            || item_type == Cfg_Item_AppLauncher_Node) {
402
403                         __create_an_item(pitem[i].key_name, pitem[i].icon_path, pitem[i].ug_args, pitem[i].shortcut_appid, pitem[i].pos, pitem[i].item_type, pitem[i].reset_type, false, pitem[i].uuid, pitem[i].click_times, pitem[i].last_clicked);
404                         /*SETTING_TRACE(" add menu - name : %s (%d) - APP ", pitem[i].key_name,pitem[i].item_type); */
405
406                 } else if (item_type == Cfg_Item_Ug_Node) {
407                         /* app-control */
408                         __create_an_item(pitem[i].key_name, pitem[i].icon_path, pitem[i].ug_args, pitem[i].shortcut_appid, pitem[i].pos, pitem[i].item_type, pitem[i].reset_type, check_ug_exist, pitem[i].uuid, pitem[i].click_times, pitem[i].last_clicked);
409                         /*SETTING_TRACE(" add menu - name : %s (%d) - UG ", pitem[i].key_name,pitem[i].item_type); */
410                 }
411         }
412         /* write to file */
413         int ret = setting_cfg_file_write(category_list);
414         json_node_free(category_list);
415         return ret;
416 }
417
418
419 /* load file */
420 EXPORT_PUBLIC
421 int setting_cfg_file_read(void)
422 {
423         struct stat sb;
424         memset(&sb, 0, sizeof(struct stat));
425         int r = stat(CFG_FILE_PATH, &sb);
426         if (0 != r) {
427                 SETTING_TRACE("ERROR, r:%d", r);
428         }
429         SETTING_TRACE("sb.st_size:%d", sb.st_size);
430
431         parser = json_parser_new();  /* to be freed on exit */
432         /* file -> parser */
433         GError *error = NULL;
434         /*gboolean ret = json_parser_load_from_file( parser, CFG_FILE_PATH, &error ); */
435         if (!sb.st_size || FALSE == json_parser_load_from_file(parser, CFG_FILE_PATH, &error)) {
436                 if (error)
437                         SETTING_TRACE_ERROR("error->message:%s", (char *)(error->message));
438                 /*return FALSE; */
439
440                 /* The file is existing and can be accessed normally, but it was
441                  * loaded failed as a json script, it means setting.cfg is
442                  * damaged(not the normal formatting ), we need to remove
443                  * the file and recreate in next running time*/
444                 SETTING_TRACE_ERROR("The file[%s] is existing and can be accessed "\
445                                     "normally, but it was 0 size or loaded failed as a json "\
446                                     "script, it means setting.cfg is damaged (the "\
447                                     "formatting ia abnormal), we need to remove the"\
448                                     " file and recreate in next running time!",
449                                     CFG_FILE_PATH);
450
451                 SETTING_TRACE("Trying to removing the damaged file.");
452                 if (remove(CFG_FILE_PATH) != 0) {
453                         SETTING_TRACE_ERROR("Error to remove the damaged file");
454                         return FALSE;
455                 }
456
457                 if (FALSE == setting_cfg_create(true)) {
458                         SETTING_TRACE_ERROR("Error to create a new config file");
459                         return FALSE;
460                 }
461                 /*if the failed is  caused by reading the file recursively invoking */
462                 return setting_cfg_file_read();
463         }
464
465         /* parser -> root */
466         root = json_parser_get_root(parser);   /* to be freed on exit*/
467         return TRUE;
468 }
469
470 /**
471 * @brief dump JSON to file
472 */
473 EXPORT_PUBLIC
474 int setting_cfg_file_write(JsonNode *node)
475 {
476         GError *error = NULL;
477         JsonGenerator *generator = json_generator_new();
478         json_generator_set_root(generator, node);
479         g_object_set(generator, "pretty", TRUE, NULL);   /*write file in indent format */
480         gboolean ret = json_generator_to_file(generator, CFG_FILE_PATH, &error);
481         g_object_unref(generator);
482
483         /* ***BEGIN***  DAC black screen SAMSUNG 2010/8/9 add
484          *add read permission
485          *chown -R inhouse:inhouse /home/inhouse/setting.cfg
486          */
487         if (FALSE == ret) {
488                 SETTING_TRACE_ERROR("Error writing file %s!", CFG_FILE_PATH);
489                 return FALSE;
490         }
491         return TRUE;
492 }
493
494 /*   public functions  */
495 EXPORT_PUBLIC
496 int setting_cfg_init(void)
497 {
498         if (!access(CFG_FILE_PATH, R_OK | W_OK | F_OK)) { /* succeed to access */
499                 if (!setting_cfg_file_read()) { /* return FALSE */
500                         if (remove(CFG_FILE_PATH)) {
501                                 return Cfg_Error_Type_RemoveCfg_Failed;
502                         }
503                         return Cfg_Error_Type_ReadCfg_Failed;
504                 }
505
506                 return Cfg_Error_Type_Sucess;
507         } else { /* fail to access */
508                 switch (errno) {
509                                 /* file non-existing case */
510                         case ENOENT:
511                                 SETTING_TRACE_ERROR("non-existing [%s]", CFG_FILE_PATH);
512                                 if (!ecore_file_is_dir(CFG_FILE_DIR_PATH)) {
513                                         SETTING_TRACE_ERROR("non-existing [%s]", CFG_FILE_DIR_PATH);
514                                         Eina_Bool flag = ecore_file_mkdir(CFG_FILE_DIR_PATH);
515                                         if (flag == EINA_FALSE) {
516                                                 SETTING_TRACE_ERROR(">failed to create dir");
517                                         } else {
518                                                 SETTING_TRACE_ERROR(">OK to create dir");
519                                         }
520                                 }
521
522                                 if (!setting_cfg_create(true)) { /* return FALSE */
523                                         SETTING_TRACE_ERROR("Error to create a new config file");
524                                         return Cfg_Error_Type_CreateCfg_Failed;
525                                 }
526
527                                 if (!setting_cfg_file_read()) { /* return FALSE */
528                                         SETTING_TRACE_ERROR("Error to read config file");
529                                         if (remove(CFG_FILE_PATH)) {
530                                                 return Cfg_Error_Type_RemoveCfg_Failed;
531                                         }
532                                         return Cfg_Error_Type_ReadCfg_Failed;
533                                 }
534
535                                 return Cfg_Error_Type_Sucess;
536                                 /* other cases */
537                         case EACCES:
538                         case EROFS:
539                         default:
540                                 return Cfg_Error_Type_DirPermissionDenied;
541                 }
542         }
543 }
544
545 EXPORT_PUBLIC
546 void setting_cfg_exit(void)
547 {
548         g_object_unref(parser);
549 }
550
551 EXPORT_PUBLIC
552 int setting_cfg_file_update(void)
553 {
554         if (access(CFG_FILE_PATH, W_OK | F_OK) != 0) {
555                 return FALSE;
556         }
557         return setting_cfg_file_write(root);
558 }
559
560 static void _update_nodes_table_by_uuid(char *uuid, int click_times, int last_clicked)
561 {
562         int size = get_cfg_array_size();
563
564         int i;
565         for (i = 0; i < size; i++) {
566                 if (0 == safeStrCmp(uuid, (s_cfg_node_array[i].uuid))) {
567                         s_cfg_node_array[i].click_times = click_times;
568                         s_cfg_node_array[i].last_clicked = last_clicked;
569                         return;
570                 }
571         }
572 }
573
574 EXPORT_PUBLIC
575 int setting_cfg_migrate(void)
576 {
577         /*1. read old cfg file */
578         struct stat sb;
579         memset(&sb, 0, sizeof(struct stat));
580         int r = stat(CFG_FILE_PATH, &sb);
581         if (0 != r) {
582                 SETTING_TRACE("ERROR, r:%d", r);
583         }
584         SETTING_TRACE("sb.st_size:%d", sb.st_size);
585
586         parser = json_parser_new();
587         GError *error = NULL;
588         if (!sb.st_size || FALSE == json_parser_load_from_file(parser, CFG_FILE_PATH, &error)) {
589                 if (error)
590                         SETTING_TRACE_ERROR("error->message:%s", (char *)(error->message));
591
592                 /*if read old cfg file unseccessfully, remove it */
593                 SETTING_TRACE("Read old cfg fail, Trying to remove it");
594                 if (remove(CFG_FILE_PATH) != 0) {
595                         SETTING_TRACE_ERROR("Error to remove the damaged file");
596                 }
597                 /*re-create cfg file */
598                 SETTING_TRACE("Trying to re-create cfg file.");
599                 if (FALSE == setting_cfg_create(true)) {
600                         SETTING_TRACE_ERROR("Error to create a new config file");
601                 }
602                 g_object_unref(parser);
603                 return FALSE;
604         }
605         root = json_parser_get_root(parser);
606
607         /*2. update click_times & last_clicked in s_cfg_node_array with old cfg */
608         int i, j, click_times, last_clicked;
609         JsonObject *category_obj;
610         JsonArray *menu;
611         JsonObject *menu_item;
612         char *uuid;
613         for (i = 0; i < setting_cfg_get_category_length(); i++) {
614                 for (j = 0; j < setting_cfg_get_menu_length(i); j++) {
615                         category_obj = json_array_get_object_element(json_node_get_array(root), i);
616                         menu = json_object_get_array_member(category_obj, "menu");
617                         menu_item = json_array_get_object_element(menu, j);
618                         uuid = (char *)json_object_get_string_member(menu_item, "uuid");
619                         click_times =  json_object_get_int_member(menu_item, "click_times");
620                         last_clicked =  json_object_get_int_member(menu_item, "last_clicked");
621                         if (click_times != 0 || last_clicked != 0) {
622                                 _update_nodes_table_by_uuid(uuid, click_times, last_clicked);
623                         }
624                 }
625         }
626
627         /*3. create new cfg file */
628         if (!setting_cfg_create(true)) {
629                 SETTING_TRACE_ERROR("fail to create new cfg file");
630                 setting_cfg_exit();
631                 return FALSE;
632         }
633
634         setting_cfg_exit();
635         return TRUE;
636 }
637
638 EXPORT_PUBLIC
639 int setting_cfg_get_category_length(void)
640 {
641         return json_array_get_length(json_node_get_array(root));
642 }
643
644 EXPORT_PUBLIC
645 char *setting_cfg_get_category_name(int category_index)
646 {
647         JsonObject *category_obj = json_array_get_object_element(json_node_get_array(root), category_index);
648         return (char *)json_object_get_string_member(category_obj, "name");
649 }
650
651 EXPORT_PUBLIC
652 int setting_cfg_get_menu_length(int category_index)
653 {
654         JsonObject *category_obj = json_array_get_object_element(json_node_get_array(root), category_index);
655         JsonArray *menu = json_object_get_array_member(category_obj, "menu");
656         return json_array_get_length(menu);
657 }
658
659 EXPORT_PUBLIC
660 char *setting_cfg_get_keyname_idx(int category_index, int menu_index)
661 {
662         JsonObject *category_obj = json_array_get_object_element(json_node_get_array(root), category_index);
663         JsonArray *menu = json_object_get_array_member(category_obj, "menu");
664         JsonObject *menu_item = json_array_get_object_element(menu, menu_index);
665         return (char *)json_object_get_string_member(menu_item, "key_name");
666 }
667
668 EXPORT_PUBLIC
669 int setting_cfg_get_itemtype_idx(int category_index, int menu_index)
670 {
671         JsonObject *category_obj = json_array_get_object_element(json_node_get_array(root), category_index);
672         JsonArray *menu = json_object_get_array_member(category_obj, "menu");
673         JsonObject *menu_item = json_array_get_object_element(menu, menu_index);
674         return json_object_get_int_member(menu_item, "item_type");
675 }
676
677 EXPORT_PUBLIC
678 char *setting_cfg_get_string_field_idx(int category_index, int menu_index, char *field_name)
679 {
680         JsonObject *category_obj = json_array_get_object_element(json_node_get_array(root), category_index);
681         JsonArray *menu = json_object_get_array_member(category_obj, "menu");
682         JsonObject *menu_item = json_array_get_object_element(menu, menu_index);
683
684         char *data = (char *)json_object_get_string_member(menu_item, field_name);
685         if (!safeStrCmp(data, "(null)")) { /* pass NULL to ug_args when create setting.cfg. */
686                 return NULL;
687         }
688         return data;
689 }
690
691 EXPORT_PUBLIC
692 int setting_cfg_get_int_field_idx(int category_index, int menu_index , char *field_name)
693 {
694         JsonObject *category_obj = json_array_get_object_element(json_node_get_array(root), category_index);
695         JsonArray *menu = json_object_get_array_member(category_obj, "menu");
696         JsonObject *menu_item = json_array_get_object_element(menu, menu_index);
697         return json_object_get_int_member(menu_item, field_name);
698 }
699
700 EXPORT_PUBLIC
701 char *setting_cfg_get_icon_path_idx(int category_index, int menu_index)
702 {
703         return setting_cfg_get_string_field_idx(category_index, menu_index, "icon_path");
704 }
705
706 EXPORT_PUBLIC
707 char *setting_cfg_get_shortcut_appid_idx(int category_index, int menu_index)
708 {
709         return setting_cfg_get_string_field_idx(category_index, menu_index, "shortcut_appid");
710 }
711
712 EXPORT_PUBLIC
713 char *setting_cfg_get_icon_path(char *keyname)
714 {
715         int i, j;
716         for (i = 0; i < setting_cfg_get_category_length(); i++) {
717                 for (j = 0; j < setting_cfg_get_menu_length(i); j++) {
718                         if (safeStrCmp(setting_cfg_get_keyname_idx(i, j), keyname) == 0)
719                                 return setting_cfg_get_icon_path_idx(i, j);
720                 }
721         }
722         return NULL;
723 }
724
725 EXPORT_PUBLIC
726 char *setting_cfg_get_ug_args_idx(int category_index, int menu_index)
727 {
728         return setting_cfg_get_string_field_idx(category_index, menu_index, "ug_args");
729 }
730
731 EXPORT_PUBLIC
732 char *setting_cfg_get_ug_args(char *keyname)
733 {
734         int i, j;
735         for (i = 0; i < setting_cfg_get_category_length(); i++) {
736                 for (j = 0; j < setting_cfg_get_menu_length(i); j++) {
737                         if (safeStrCmp(setting_cfg_get_keyname_idx(i, j), keyname) == 0)
738                                 return setting_cfg_get_ug_args_idx(i, j);
739                 }
740         }
741         return NULL;
742 }
743
744 EXPORT_PUBLIC
745 char *setting_cfg_get_ugpath_idx(int category_index, int menu_index)
746 {
747         return setting_cfg_get_string_field_idx(category_index, menu_index, "ug_path");
748 }
749
750 EXPORT_PUBLIC
751 char *setting_cfg_get_ugpath(char *keyname)
752 {
753         int i, j;
754         for (i = 0; i < setting_cfg_get_category_length(); i++) {
755                 for (j = 0; j < setting_cfg_get_menu_length(i); j++) {
756                         if (safeStrCmp(setting_cfg_get_keyname_idx(i, j), keyname) == 0)
757                                 return setting_cfg_get_ugpath_idx(i, j);
758                 }
759         }
760         return NULL;
761 }
762
763 EXPORT_PUBLIC
764 int setting_cfg_get_pos_idx(int category_index, int menu_index)
765 {
766         return setting_cfg_get_int_field_idx(category_index, menu_index, "pos");
767 }
768
769 EXPORT_PUBLIC
770 int setting_cfg_get_pos(char *keyname)
771 {
772         int i, j;
773         for (i = 0; i < setting_cfg_get_category_length(); i++) {
774                 for (j = 0; j < setting_cfg_get_menu_length(i); j++) {
775                         if (safeStrCmp(setting_cfg_get_keyname_idx(i, j), keyname) == 0)
776                                 return setting_cfg_get_pos_idx(i, j);
777                 }
778         }
779         return 0;
780 }
781
782 EXPORT_PUBLIC
783 void setting_cfg_set_pos_idx(int category_index, int menu_index, int pos)
784 {
785         JsonObject *category_obj = json_array_get_object_element(json_node_get_array(root), category_index);
786         JsonArray *menu = json_object_get_array_member(category_obj, "menu");
787         JsonObject *menu_item = json_array_get_object_element(menu, menu_index);
788         json_object_remove_member(menu_item, "pos");
789         json_object_set_int_member(menu_item, "pos", pos);
790 }
791
792 EXPORT_PUBLIC
793 void setting_cfg_set_pos(char *keyname, int pos)
794 {
795         int i, j;
796         for (i = 0; i < setting_cfg_get_category_length(); i++) {
797                 for (j = 0; j < setting_cfg_get_menu_length(i); j++) {
798                         if (safeStrCmp(setting_cfg_get_keyname_idx(i, j), keyname) == 0) {
799                                 setting_cfg_set_pos_idx(i, j, pos);
800                                 return ;
801                         }
802                 }
803         }
804
805 }
806
807 EXPORT_PUBLIC
808 int setting_cfg_get_last_clicked_idx(int category_index, int menu_index)
809 {
810         return setting_cfg_get_int_field_idx(category_index, menu_index, "last_clicked");
811 }
812
813 EXPORT_PUBLIC
814 int setting_cfg_get_last_clicked(char *keyname)
815 {
816         int i, j;
817         for (i = 0; i < setting_cfg_get_category_length(); i++) {
818                 for (j = 0; j < setting_cfg_get_menu_length(i); j++) {
819                         if (safeStrCmp(setting_cfg_get_keyname_idx(i, j), keyname) == 0)
820                                 return setting_cfg_get_last_clicked_idx(i, j);
821                 }
822         }
823         return 0;
824 }
825
826 EXPORT_PUBLIC
827 void setting_cfg_set_last_clicked_idx(int category_index, int menu_index, int last_clicked)
828 {
829         JsonObject *category_obj = json_array_get_object_element(json_node_get_array(root), category_index);
830         JsonArray *menu = json_object_get_array_member(category_obj, "menu");
831         JsonObject *menu_item = json_array_get_object_element(menu, menu_index);
832         json_object_remove_member(menu_item, "last_clicked");
833         json_object_set_int_member(menu_item, "last_clicked", last_clicked);
834 }
835
836 EXPORT_PUBLIC
837 void setting_cfg_set_last_clicked(char *keyname, int last_clicked)
838 {
839         int i, j;
840         for (i = 0; i < setting_cfg_get_category_length(); i++) {
841                 for (j = 0; j < setting_cfg_get_menu_length(i); j++) {
842                         if (safeStrCmp(setting_cfg_get_keyname_idx(i, j), keyname) == 0) {
843                                 setting_cfg_set_last_clicked_idx(i, j, last_clicked);
844                                 return ;
845                         }
846                 }
847         }
848
849 }
850
851 EXPORT_PUBLIC
852 int setting_cfg_get_click_times_idx(int category_index, int menu_index)
853 {
854         return setting_cfg_get_int_field_idx(category_index, menu_index, "click_times");
855 }
856
857 EXPORT_PUBLIC
858 int setting_cfg_get_click_times(char *keyname)
859 {
860         int i, j;
861         for (i = 0; i < setting_cfg_get_category_length(); i++) {
862                 for (j = 0; j < setting_cfg_get_menu_length(i); j++) {
863                         if (safeStrCmp(setting_cfg_get_keyname_idx(i, j), keyname) == 0)
864                                 return setting_cfg_get_click_times_idx(i, j);
865                 }
866         }
867         return 0;
868 }
869
870 EXPORT_PUBLIC
871 void setting_cfg_set_click_times_idx(int category_index, int menu_index, int click_times)
872 {
873         JsonObject *category_obj = json_array_get_object_element(json_node_get_array(root), category_index);
874         JsonArray *menu = json_object_get_array_member(category_obj, "menu");
875         JsonObject *menu_item = json_array_get_object_element(menu, menu_index);
876         json_object_remove_member(menu_item, "click_times");
877         json_object_set_int_member(menu_item, "click_times", click_times);
878 }
879
880 EXPORT_PUBLIC
881 void setting_cfg_set_click_times(char *keyname, int click_times)
882 {
883         int i, j;
884         for (i = 0; i < setting_cfg_get_category_length(); i++) {
885                 for (j = 0; j < setting_cfg_get_menu_length(i); j++) {
886                         if (safeStrCmp(setting_cfg_get_keyname_idx(i, j), keyname) == 0) {
887                                 setting_cfg_set_click_times_idx(i, j, click_times);
888                                 time_t ctime = time(NULL);
889                                 setting_cfg_set_last_clicked_idx(i, j, (int)ctime);
890                                 return ;
891                         }
892                 }
893         }
894
895 }
896
897 EXPORT_PUBLIC
898 int setting_cfg_get_resetable_flag_idx(int category_index, int menu_index)
899 {
900         return setting_cfg_get_int_field_idx(category_index, menu_index,
901                                              "is_resetable");
902 }
903
904 EXPORT_PUBLIC
905 void setting_cfg_add_downloaded_app(char *keyname, char *icon_path, char *ug_args, int pos)
906 {
907         int i;
908         JsonNode *menu_item;
909         JsonObject *object;
910         JsonArray *menu;
911
912         for (i = 0; i < setting_cfg_get_category_length(); i++) {
913                 if (safeStrCmp(setting_cfg_get_category_name(i), "Downloaded App") == 0) {
914                         menu_item = json_node_new(JSON_NODE_OBJECT);
915                         object = json_object_new();
916                         json_node_take_object(menu_item, object);
917                         json_object_set_string_member(object, "key_name", keyname);
918                         json_object_set_string_member(object, "icon_path", icon_path);
919                         json_object_set_string_member(object, "ug_args", ug_args);
920                         json_object_set_int_member(object, "pos", pos);
921
922                         object = json_array_get_object_element(json_node_get_array(root), i);
923                         menu = json_object_get_array_member(object, "menu");
924                         json_array_add_element(menu, menu_item);
925                 }
926         }
927 }
928
929 void setting_cfg_remove_downloaded_app(char *keyname)
930 {
931         int i, j;
932         JsonObject *object;
933         JsonArray *menu;
934
935         for (i = 0; i < setting_cfg_get_category_length(); i++) {
936                 if (safeStrCmp(setting_cfg_get_category_name(i), "Downloaded App") == 0) {
937                         for (j = 0; j < setting_cfg_get_menu_length(i); j++) {
938                                 if (safeStrCmp(setting_cfg_get_keyname_idx(i, j), keyname) == 0) {
939                                         object = json_array_get_object_element(json_node_get_array(root), i);
940                                         menu = json_object_get_array_member(object, "menu");
941                                         json_array_remove_element(menu, j);
942                                         return ;
943                                 }
944                         }
945                 }
946         }
947 }
948
949 EXPORT_PUBLIC
950 bool is_ug_installed_by_ug_args(void *data)
951 {
952         retv_if(!data, TRUE);/*if passing NULL,drawing it */
953         char *ug_args = data;
954         char *path = get_ug_path_from_ug_args(ug_args);
955         int is_installed = ug_is_installed(path);
956
957         FREE(path);
958         return is_installed;
959 }
960
961 /*other relative function*/
962 EXPORT_PUBLIC
963 char *get_ug_path_from_ug_args(void *data)
964 {
965         /*SETTING_TRACE_BEGIN;*/
966         char *p = (char *)data;
967         if (NULL == p || '\0' == p[0]) {
968                 return NULL;
969         }
970
971         char *q = strchr(p, '|');
972         char *path = NULL;
973
974         if (q) {                /* copy out the content before '|'; eg, ab|cd */
975                 path = (char *)calloc(1, q - p + 1);
976                 setting_retvm_if(!path, NULL, "calloc failed");
977                 safeCopyStr(path, p, q - p);
978                 /* redundant handle */
979                 path[q - p] = '\0';
980         } else {                /* copy out all the content. eg.abb */
981
982                 path = (char *)calloc(1, safeStrLen(p) + 1);
983                 setting_retvm_if(!path, NULL, "calloc failed");
984                 safeCopyStr(path, p, safeStrLen(p));
985         }
986         /*SETTING_TRACE("get the ug['%s']", path);*/
987         return path;
988 }
989
990 EXPORT_PUBLIC
991 app_control_h get_bundle_from_ug_args(void *data)
992 {
993         /*SETTING_TRACE_BEGIN;*/
994         char *p = (char *)data;
995         if (NULL == p || '\0' == p[0]) {
996                 return NULL;
997         }
998         char *m = NULL;
999         char *q = strchr(p, '|');
1000         if (q) {/* (key, value) pairs exit. eg: ug_args = "sevenemail-setting-efl|caller:setting; cmd:main option" */
1001                 /* alloc data */
1002                 app_control_h svc;
1003                 if (app_control_create(&svc))
1004                         return NULL;
1005                 int str_len = safeStrLen(p) + 1;
1006                 char *v_key = (char *)calloc(1, str_len);
1007                 if (!v_key) {
1008                         app_control_destroy(svc);
1009                         return NULL;
1010                 }
1011
1012                 char *v_value = (char *)calloc(1, str_len);
1013                 if (!v_value) {
1014                         app_control_destroy(svc);
1015                         FREE(v_key);
1016                         return NULL;
1017                 }
1018
1019                 p = ++q;
1020                 while (p) {
1021                         q = strchr(p, ';');
1022                         if (q) {        /* not the last field */
1023                                 m = strchr(p, ':');     /* ziduannei */
1024                                 if (m) {
1025                                         safeCopyStr(v_key, p, m - p);
1026
1027                                         ++m;
1028                                         safeCopyStr(v_value, m, q - m);
1029                                         SETTING_TRACE
1030                                         ("To add ('%s', '%s') to bundle data",
1031                                          v_key, v_value);
1032                                         app_control_add_extra_data(svc, v_key, v_value);
1033                                 } else {
1034                                         SETTING_TRACE_ERROR
1035                                         ("invalid key-value format!!\n");
1036                                         break;  /* end the whole while */
1037                                 }
1038                         } else {        /* the last field */
1039
1040                                 m = strchr(p, ':');     /* ziduannei */
1041                                 if (m) {
1042                                         safeCopyStr(v_key, p, m - p);
1043                                         ++m;
1044                                         safeCopyStr(v_value, m, safeStrLen(m));
1045
1046                                         app_control_add_extra_data(svc, v_key, v_value);
1047                                 } else {
1048                                         SETTING_TRACE_ERROR
1049                                         ("invalid key-value format!!\n");
1050                                 }
1051                                 break;  /* end the whole while */
1052                         }
1053
1054                         memset(v_key, '\0', str_len);
1055                         memset(v_value, '\0', str_len);
1056                         p = ++q;
1057                 }
1058
1059                 FREE(v_key);
1060                 FREE(v_value);
1061                 return svc;
1062         } else {                /* eg: ug_args = "setting-browser-efl" */
1063
1064                 /*SETTING_TRACE("(key, value) pairs not exit");*/
1065                 return NULL;
1066         }
1067
1068 }
1069
1070 EXPORT_PUBLIC void setting_cfg_dump_basic_info()
1071 {
1072 #if 1
1073         bool fileExit = (0 == access(CFG_FILE_PATH, R_OK | W_OK | F_OK));
1074         SETTING_TRACE("%s's exist:%d", CFG_FILE_PATH, fileExit);
1075
1076         struct stat sb;
1077         memset(&sb, 0, sizeof(struct stat));
1078         int r = stat(CFG_FILE_PATH, &sb);
1079         if (0 != r) {
1080                 SETTING_TRACE("ERROR, r:%d", r);
1081         }
1082         SETTING_TRACE("sb.st_size:%d", sb.st_size);
1083 #endif
1084 }
1085
1086
1087 /**
1088 * @brief helper function for search
1089 *
1090 * @param key_name
1091 * @param url
1092 * @param icon_path
1093 * @param item_type
1094 * @param data
1095 *
1096 * @return
1097 */
1098 EXPORT_PUBLIC Setting_Cfg_Node_T *setting_plugin_search_item_add(char *key_name, char *url, char *icon_path, int item_type, void *data)
1099 {
1100         Setting_Cfg_Node_T *node = calloc(1, sizeof(Setting_Cfg_Node_T));
1101         if (node) {
1102                 if (key_name)   node->key_name = strdup(key_name);
1103                 if (url)                node->ug_args = strdup(url);
1104                 if (icon_path)  node->icon_path = strdup(icon_path);
1105                 node->pos = 0;
1106                 node->reset_type = 0;
1107                 node->click_times = 0;
1108                 node->item_type = item_type;
1109                 node->data = data;
1110                 node->sub_index = NULL;
1111                 node->tfunc = NULL;
1112         }
1113         return node;
1114 }
1115
1116 EXPORT_PUBLIC Setting_Cfg_Node_T *setting_plugin_search_item_subindex_add(char *key_name, char *url, char *icon_path, int item_type, void *data, char *sub_index)
1117 {
1118         Setting_Cfg_Node_T *node = calloc(1, sizeof(Setting_Cfg_Node_T));
1119         if (node) {
1120                 if (key_name)   node->key_name = strdup(key_name);
1121                 if (url)                node->ug_args = strdup(url);
1122                 if (icon_path)  node->icon_path = strdup(icon_path);
1123                 node->pos = 0;
1124                 node->reset_type = 0;
1125                 node->click_times = 0;
1126                 node->item_type = item_type;
1127                 node->data = data;
1128                 if (sub_index) node->sub_index = strdup(sub_index);
1129                 node->tfunc = NULL;
1130         }
1131         return node;
1132 }
1133
1134 EXPORT_PUBLIC Cfg_Item_Type setting_search_type(char *type)
1135 {
1136         Cfg_Item_Type ret_code = Cfg_Item_Node_Error;
1137         if (type) {
1138                 if (!safeStrCmp(type, SEARCH_TYPE_ERROR))
1139                         ret_code = Cfg_Item_Node_Error;
1140                 else if (!safeStrCmp(type, SEARCH_TYPE_UG_NODE))
1141                         ret_code = Cfg_Item_Ug_Node;
1142                 else if (!safeStrCmp(type, SEARCH_TYPE_UI_NODE))
1143                         ret_code = Cfg_Item_Ui_Node;
1144                 else if (!safeStrCmp(type, SEARCH_TYPE_TITLE_NODE))
1145                         ret_code = Cfg_Item_Title_Node;
1146                 else if (!safeStrCmp(type, SEARCH_TYPE_APP_NODE))
1147                         ret_code = Cfg_Item_App_Node;
1148                 else if (!safeStrCmp(type, SEARCH_TYPE_VIEW_NODE))
1149                         ret_code = Cfg_Item_View_Node;
1150                 else if (!safeStrCmp(type, SEARCH_TYPE_APPLAUNCHER_NODE))
1151                         ret_code = Cfg_Item_AppLauncher_Node;
1152                 else if (!safeStrCmp(type, SEARCH_TYPE_APPLAUNCHER_VIEW_NODE))
1153                         ret_code = Cfg_Item_AppLauncher_View_Node;
1154                 else if (!safeStrCmp(type, SEARCH_TYPE_UG_NODE_TOGGLE))
1155                         ret_code = Cfg_Item_Ug_Node_Toggle;
1156                 else if (!safeStrCmp(type, SEARCH_TYPE_UI_NODE_TOGGLE))
1157                         ret_code = Cfg_Item_Ui_Node_Toggle;
1158                 else if (!safeStrCmp(type, SEARCH_TYPE_APPLAUNCHER_NODE_TOGGLE))
1159                         ret_code = Cfg_Item_AppLauncher_Node_Toggle;
1160                 else if (!safeStrCmp(type, SEARCH_TYPE_VIEW_NODE_TOGGLE))
1161                         ret_code = Cfg_Item_View_Node_Toggle;
1162         }
1163
1164         return ret_code;
1165 }
1166
1167 /*////////////////////////////////////////////////////////////////////////// */
1168 /*
1169  * @return 0 on success, -1 on error
1170  */
1171 EXPORT_PUBLIC
1172 int wifi_toggle_get_state(Cfg_Item_State *stat, void *data)
1173 {
1174         SETTING_TRACE_BEGIN;
1175         int err;
1176         int value = -1;
1177         setting_get_int_slp_key(INT_SLP_SETTING_WIFI_STATUS, &value, &err);
1178
1179         if (value == VCONFKEY_WIFI_OFF)
1180                 *stat = Cfg_Item_Off;
1181         else
1182                 *stat = Cfg_Item_On;
1183
1184         return err;
1185 }
1186
1187 EXPORT_PUBLIC
1188 int flightmode_toggle_get_state(Cfg_Item_State *stat, void *data)
1189 {
1190         SETTING_TRACE_BEGIN;
1191         int err;
1192         int value = -1;
1193         setting_get_bool_slp_key(BOOL_SLP_SETTING_FLIGHT_MODE, &value, &err);
1194
1195         if (value == 1)
1196                 *stat = Cfg_Item_On;
1197         else
1198                 *stat = Cfg_Item_Off;
1199
1200         return err;
1201 }
1202
1203
1204 EXPORT_PUBLIC
1205 int bluetooth_toggle_get_state(Cfg_Item_State *stat, void *data)
1206 {
1207         SETTING_TRACE_BEGIN;
1208         int err;
1209         int value = -1;
1210         setting_get_int_slp_key(INT_SLP_SETTING_BT_STATUS, &value, &err);
1211
1212         if (value == 1)
1213                 *stat = Cfg_Item_On;
1214         else
1215                 *stat = Cfg_Item_Off;
1216
1217         return err;
1218 }
1219
1220
1221 EXPORT_PUBLIC
1222 int drivingmode_toggle_get_state(Cfg_Item_State *stat, void *data)
1223 {
1224         SETTING_TRACE_BEGIN;
1225         int err = 0;
1226         *stat = Cfg_Item_Off;
1227
1228         int value;
1229         int ret = vconf_get_bool(VCONFKEY_SETAPPL_DM_DRIVING_MODE, &value);
1230         if (ret != 0) {
1231                 SETTING_TRACE_ERROR("get vconf failed");
1232                 *stat = Cfg_Item_Error;
1233                 return -1;
1234         }
1235
1236         *stat = (value) ? Cfg_Item_On : Cfg_Item_Off;
1237         return err;
1238 }
1239
1240 EXPORT_PUBLIC
1241 int network_restriction_mode_toggle_get_state(Cfg_Item_State *stat, void *data)
1242 {
1243         SETTING_TRACE_BEGIN;
1244         int err;
1245         int value = -1;
1246         setting_get_bool_slp_key(BOOL_SLP_SETTING_NET_RESTRICTION_MODE, &value, &err);
1247
1248         if (value == 1)
1249                 *stat = Cfg_Item_On;
1250         else
1251                 *stat = Cfg_Item_Off;
1252
1253         return err;
1254 }
1255
1256 /*////////////////////////////////////////////////////////////////////////// */
1257 /* personal mode */
1258
1259 EXPORT_PUBLIC
1260 int personalmode_toggle_get_state(Cfg_Item_State *stat, void *data)
1261 {
1262         SETTING_TRACE_BEGIN;
1263         int err = 0;
1264         *stat = Cfg_Item_Off;
1265
1266         int value;
1267         int ret = vconf_get_bool(VCONFKEY_SETAPPL_PERSONAL_MODE_STATUS_BOOL, &value);
1268         if (ret != 0) {
1269                 SETTING_TRACE_ERROR("get vconf failed");
1270                 *stat = Cfg_Item_Error;
1271                 return -1;
1272         }
1273
1274         *stat = (value) ? Cfg_Item_On : Cfg_Item_Off;
1275         return err;
1276 }
1277
1278 /*////////////////////////////////////////////////////////////////////////// */
1279 /* NFC */
1280
1281 EXPORT_PUBLIC
1282 int nfc_toggle_get_state(Cfg_Item_State *stat, void *data)
1283 {
1284         SETTING_TRACE_BEGIN;
1285         int err;
1286         int value = -1;
1287         setting_get_bool_slp_key(BOOL_SLP_SETTING_NFC_STATUS, &value, &err);
1288
1289         if (value == 1)
1290                 *stat = Cfg_Item_On;
1291         else
1292                 *stat = Cfg_Item_Off;
1293
1294         return err;
1295 }