Fix network UI issue and fix P120829-5114,P120829-5115
[apps/core/preloaded/settings.git] / setting-common / src / setting-common-general-func.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 #include <ail.h>
22 #include <setting-common-general-func.h>
23 #include <setting-common-data-slp-setting.h>
24
25 #include <glib.h>
26 #include <iniparser.h>
27 #include <dlfcn.h>
28 #include <libxml/xmlmemory.h>
29 #include <libxml/parser.h>
30 #include <system_info.h>
31
32 #include <system_settings.h>
33
34 char *setting_file_basename(char *path)
35 {
36         if (NULL == path || '\0' == path[0]) {
37                 return NULL;    /* invalid arguement */
38         }
39         char *p = strrchr(path, '/');
40         if (!p) {
41                 return (char *)g_strdup(path);  /*  cannot find '/' */
42         }
43         if ('\0' == p[1]) {
44                 return NULL;    /* end with '/' */
45         }
46         return (char *)g_strdup(p + 1);
47 }
48
49 char *get_pa_usb_connect_mode_str()
50 {
51         int ret = SETTING_RETURN_FAIL;
52         int value;
53
54         ret = vconf_get_int(VCONFKEY_SETAPPL_USB_MODE_INT, &value);
55         setting_retvm_if(SETTING_RETURN_FAIL == ret, NULL, "Failed to get vconf value");        /* file system exceptional handle */
56
57         switch (value)
58         {
59         case SETTING_USB_DEBUG_MODE:
60                 return (char*)g_strdup(_("IDS_ST_BODY_USB_DEBUGGING"));
61         default:
62                 return (char*)g_strdup(_("IDS_COM_POP_DISCONNECTED"));
63         }
64 }
65
66 char *get_brightness_mode_str()
67 {
68         int value, err;
69         setting_get_int_slp_key(INT_SLP_SETTING_AUTOMATIC_BRIGHTNESS, &value,
70                                  &err);
71         if (value != SETTING_BRIGHTNESS_AUTOMATIC_OFF) {
72                 return setting_gettext("IDS_COM_BODY_AUTOMATIC");
73         } else {
74                 return setting_gettext("IDS_COM_BODY_MANUAL");
75         }
76 }
77
78 char *get_pa_backlight_time_str()
79 {
80         int value = 0;
81         char backlight_time_str[MAX_DISPLAY_NAME_LEN_ON_UI];
82
83 #if SUPPORT_LCD_TIMEOUT_KEEPING
84         if (0 != vconf_get_int(VCONFKEY_LCD_TIMEOUT_NORMAL_BACKUP, &value)) {
85                 /* value = 600; */
86                 if(isEmulBin())
87                         value = 0;      /* keep agree with postin file */
88                 else
89                         value = 30;
90                 vconf_set_int(VCONFKEY_LCD_TIMEOUT_NORMAL_BACKUP, value);
91         }
92 #else
93         int err;
94         int ret =
95             setting_get_int_slp_key(INT_SLP_SETTING_LCD_TIMEOUT_NORMAL, &value,
96                                     &err);
97         #if NORMAL
98         if (SETTING_RETURN_FAIL == ret || value < 0) {  /* file system exceptional handle, eg. The vconf file lost due to file system. */
99                 /* value = 600; */
100                 if(isEmulBin())
101                         value = 0;      /* keep agree with postin file */
102                 else
103                         value = 30;
104                 setting_set_int_slp_key(INT_SLP_SETTING_LCD_TIMEOUT_NORMAL,
105                                         value, &err);
106         }
107         #else // for MDM server
108         // adjust value.
109         // if < 15, 15
110         // if 15 < < 30, 15
111         // if 30 < < 60, 30
112         // if 60 < < 120, 60
113         // if 120 < < 300, 120
114         // if 300 < < 600, 300
115         // if > 600, 600
116         if(isEmulBin())
117         {
118                 if (SETTING_RETURN_FAIL == ret || value < 15)
119                         value = 0;
120                 else if(value >= 15 && value < 30)
121                         value = 15;
122                 else if(value >= 30 && value < 60)
123                         value = 30;
124                 else if(value >= 60 && value < 120)
125                         value = 60;
126                 else if(value >= 120 && value < 300)
127                         value = 120;
128                 else if(value >= 300 && value < 600)
129                         value = 300;
130                 else
131                         value = 600;
132         }
133         else
134         {
135                 if (SETTING_RETURN_FAIL == ret || value < 30)
136                         value = 15;
137                 else if(value >= 30 && value < 60)
138                         value = 30;
139                 else if(value >= 60 && value < 120)
140                         value = 60;
141                 else if(value >= 120 && value < 300)
142                         value = 120;
143                 else if(value >= 300 && value < 600)
144                         value = 300;
145                 else
146                         value = 600;
147         }
148
149         setting_set_int_slp_key(INT_SLP_SETTING_LCD_TIMEOUT_NORMAL,
150                                         value, &err);
151         #endif
152 #endif
153
154         if (value == 0) {
155                 snprintf(backlight_time_str, sizeof(backlight_time_str), "%s",
156                          _("IDS_ST_BODY_ALWAYS_ON"));
157         } else if (value == 60) {
158                 snprintf(backlight_time_str, sizeof(backlight_time_str), "%s",
159                          _("IDS_COM_BODY_1_MINUTE"));
160         } else if (value > 60) {
161                 snprintf(backlight_time_str, sizeof(backlight_time_str),
162                          "%d %s", value / 60, (char *)(_("IDS_COM_BODY_MINUTES_LC")));
163         } else {
164                 snprintf(backlight_time_str, sizeof(backlight_time_str),
165                          "%d %s", value, (char *)(_("IDS_COM_BODY_SECONDS_LC")));
166         }
167         return (char *)g_strdup(backlight_time_str);
168 }
169
170 char *get_pa_powersaving_at_str()
171 {
172         int value = 30;
173         char powersaving_at_str[MAX_DISPLAY_NAME_LEN_ON_UI + 1];
174
175         int err;
176         int ret =
177             setting_get_int_slp_key(INT_SLP_SETTING_POWERSAVING_AT, &value,
178                                     &err);
179         if (SETTING_RETURN_FAIL == ret || value < 0) {  /* file system exceptional handle, eg. The vconf file lost due to file system. */
180                 value = 30;     /* keep agree with postin file */
181                 setting_set_int_slp_key(INT_SLP_SETTING_POWERSAVING_AT,
182                                         value, &err);
183         }
184
185         snprintf(powersaving_at_str, MAX_DISPLAY_NAME_LEN_ON_UI, "At %d%% %s",
186                  value, "battery power");
187         return (char *)g_strdup(powersaving_at_str);
188 }
189
190 char *get_pa_screen_timeout_str()
191 {
192         int value = 15;
193         char screen_timeout_str[MAX_DISPLAY_NAME_LEN_ON_UI + 1];
194
195         int err;
196         int ret =
197             setting_get_int_slp_key(INT_SLP_SETTING_POWERSAVING_SCREEN_TIMEOUT,
198                                     &value,
199                                     &err);
200         if (SETTING_RETURN_FAIL == ret || value < 0) {  /* file system exceptional handle, eg. The vconf file lost due to file system. */
201                 value = 15;     /* keep agree with postin file */
202                 setting_set_int_slp_key
203                     (INT_SLP_SETTING_POWERSAVING_SCREEN_TIMEOUT, value, &err);
204         }
205
206         if (value == 60) {
207                 snprintf(screen_timeout_str, MAX_DISPLAY_NAME_LEN_ON_UI, "%s",
208                          _("1 minute"));
209         } else if (value >= 60) {
210                 snprintf(screen_timeout_str, MAX_DISPLAY_NAME_LEN_ON_UI,
211                          "%d %s", value / 60,
212                          (char *)(_("IDS_COM_BODY_MINUTES_LC")));
213         } else {
214                 snprintf(screen_timeout_str, MAX_DISPLAY_NAME_LEN_ON_UI,
215                          "%d %s", value,
216                          (char *)(_("IDS_COM_BODY_SECONDS_LC")));
217         }
218         return (char *)g_strdup(screen_timeout_str);
219 }
220
221 char *get_pa_display_language_str()
222 {
223         int ret = SETTING_RETURN_FAIL;
224         int err;
225         int index;
226
227         Eina_List* list = setting_get_language_list();
228         Eina_List* elist = NULL;
229
230         ret = setting_get_int_slp_key(INT_SLP_SETTING_LANG, &index, &err);
231
232         if (0 == index)
233                 return g_strdup("Automatic");
234
235         setting_lang_entry* pnode;
236
237         char* title = NULL;
238     EINA_LIST_FOREACH( list, elist, pnode)
239     {
240                 if (pnode->number == index)
241                 title = g_strdup(pnode->title);
242     }
243         return title;
244 }
245
246 char *get_pa_Wi_Fi_on_off_str()
247 {
248         int value, err;
249         int ret = SETTING_RETURN_FAIL;
250         ret =
251             setting_get_int_slp_key(INT_SLP_SETTING_WIFI_STATUS, &value, &err);
252
253         if (SETTING_RETURN_FAIL == ret) {       /* file system exceptional handle, eg. The vconf file lost due to file system. */
254                 value = VCONFKEY_WIFI_OFF;
255                 setting_set_int_slp_key(INT_SLP_SETTING_WIFI_STATUS,
256                                         VCONFKEY_WIFI_OFF, &err);
257         }
258
259         if (value) {
260                 char *pa_wifi_device = vconf_get_str(VCONFKEY_WIFI_CONNECTED_AP_NAME);
261                 SETTING_TRACE("pa_wifi_device:%s", pa_wifi_device);
262                 if (NULL != pa_wifi_device && '\0' != pa_wifi_device[0]) {
263                         return pa_wifi_device;
264                 }
265
266                 return (char*)g_strdup(setting_gettext("IDS_COM_BODY_ON_M_STATUS"));
267         } else {
268                 return (char*)g_strdup(setting_gettext("IDS_COM_BODY_OFF_M_STATUS"));
269         }
270 }
271
272 char *get_Mobile_AP_on_off_str()
273 {
274         int value = -1, err;
275         int ret =
276             setting_get_int_slp_key(INT_SLP_SETTING_MOBILE_AP_STATUS, &value,
277                                     &err);
278
279         /*  7 means bitwise-operation */
280         /*
281               - 1 : wi-fi
282               - 2 : bluetooth
283               - 4 : USB
284               4, 2, 1 = 7
285         */
286         if (0 != ret || value < 0 || value > 7) {
287                 value = 0;
288                 setting_set_int_slp_key(INT_SLP_SETTING_MOBILE_AP_STATUS, value,
289                                         &err);
290         }
291
292         if (value != 0) {
293                 return setting_gettext("IDS_COM_BODY_ON_M_STATUS");
294         } else {                /*  0 */
295                 return setting_gettext("IDS_COM_BODY_OFF_M_STATUS");
296         }
297 }
298
299 char *get_BT_on_off_str()
300 {
301         int ret = SETTING_RETURN_FAIL;
302         int value, err;
303         ret = setting_get_int_slp_key(INT_SLP_SETTING_BT_STATUS, &value, &err);
304
305         if (SETTING_RETURN_FAIL == ret) {       /* file system exceptional handle, eg. The vconf file lost due to file system. */
306                 value = VCONFKEY_BT_STATUS_OFF;
307                 setting_set_int_slp_key(INT_SLP_SETTING_BT_STATUS,
308                                         VCONFKEY_BT_STATUS_OFF, &err);
309         }
310
311         if (VCONFKEY_BT_STATUS_OFF == value) {
312                 return setting_gettext("IDS_COM_BODY_OFF_M_STATUS");
313         } else {
314                 return setting_gettext("IDS_COM_BODY_ON_M_STATUS");
315         }
316 }
317
318 char *get_NFC_on_off_str()
319 {
320         int ret = SETTING_RETURN_FAIL;
321         int value = 0, err = 0;
322         ret = setting_get_bool_slp_key(BOOL_SLP_SETTING_NFC_STATUS, &value, &err);
323
324         if (SETTING_RETURN_FAIL == ret) {       /* file system exceptional handle, eg. The vconf file lost due to file system. */
325                 SETTING_TRACE_DEBUG("fail to get nfc status from vconf");
326                 value = VCONFKEY_NFC_STATE_OFF;
327         }
328
329         if (VCONFKEY_NFC_STATE_OFF == value) {
330                 return setting_gettext("IDS_COM_BODY_OFF_M_STATUS");
331         } else {
332                 return setting_gettext("IDS_COM_BODY_ON_M_STATUS");
333         }
334 }
335
336 char *get_pa_time_format_str()
337 {
338         int ret = SETTING_RETURN_FAIL;
339         int value, err;
340         char *format_str[] = { _("IDS_COM_BODY_12_HOURS"),
341                                 _("IDS_ST_BODY_24_HOURS") };
342         ret =
343             setting_get_int_slp_key(INT_SLP_SETTING_REGIONFORMAT_TIME1224,
344                                     &value, &err);
345         setting_retvm_if(SETTING_RETURN_FAIL == ret, NULL, "Failed to get vconf value");        /* file system exceptional handle */
346
347         /*  scope */
348         /*  value 1:12H  2:24H */
349         if (value > 0 && value <= 2)    /*  1, 2 */
350                 return (char *)g_strdup(format_str[value - 1]);
351         else
352                 return (char *)g_strdup(format_str[0]); /*  set to 12H compelsery */
353 }
354
355 /** @deprecated */
356 char *get_pa_date_format_str()
357 {
358         int ret = SETTING_RETURN_FAIL;
359         int value, err;
360         char *format_str[] = { _("IDS_ST_BODY_DDMMYYYY_DOT"),
361                 _("IDS_ST_BODY_MM_DD_YYYY_DOT"),
362                 _("IDS_COM_BODY_YYYYMMDD"), _("IDS_ST_BODY_YYYY_DD_MM_DOT")
363         };
364         ret =
365             setting_get_int_slp_key(INT_SLP_SETTING_DATE_FORMAT, &value, &err);
366         setting_retvm_if(SETTING_RETURN_FAIL == ret, NULL, "Failed to get vconf value");        /* file system exceptional handle */
367         return (char *)g_strdup(format_str[value]);
368 }
369
370 char *get_pa_week_format_str()
371 {
372         int ret = SETTING_RETURN_FAIL;
373         int value, err;
374         char *format_str[] = {
375                 _("IDS_ST_BODY_SUNDAY"),
376                 _("IDS_ST_BODY_MONDAY"),
377         };
378
379         ret =
380             setting_get_int_slp_key(INT_SLP_SETTING_WEEK_FORMAT, &value, &err);
381         setting_retvm_if(SETTING_RETURN_FAIL == ret, NULL, "Failed to get vconf value");        /* file system exceptional handle */
382         return (char *)g_strdup(format_str[value]);
383 }
384
385 char *get_pa_roaming_network_str()
386 {
387         int ret = SETTING_RETURN_FAIL;
388         int value, err;
389         char *roaming_str[] = {
390                 _("IDS_ST_BODY_AUTO_DOWNLOAD"), _("IDS_COM_BODY_MANUAL"),
391                 _("IDS_ST_BODY_ALWAYS_REJECT")
392         };
393         ret =
394             setting_get_int_slp_key(INT_SLP_SETTING_ROAMING_NETWORK, &value,
395                                     &err);
396         setting_retvm_if(SETTING_RETURN_FAIL == ret, NULL, "Failed to get vconf value");        /* file system exceptional handle */
397         return (char *)g_strdup(roaming_str[value]);
398 }
399
400 double get_widgets_factor()
401 {
402         return elm_scale_get();
403 }
404
405 char *setting_gettext(const char *s)
406 {
407         /* fisrt find in app pg */
408
409         if (s == NULL) {
410                 return "NULL";
411         }
412
413         char *p = dgettext(SETTING_PACKAGE, s);
414
415         if (!safeStrCmp(s, p)) {        /* not found */
416                 /* find in system pkg */
417                 p = dgettext(SYSTEM_PACKAGE, s);
418         }
419         return p;
420 }
421
422 bool is_digital_str(const char *cstr)
423 {
424         if (cstr == NULL || cstr[0] == 0) {
425                 return FALSE;
426         }
427
428         int len = (int)(safeStrLen(cstr));
429         int pos = 0;
430         if (cstr[0] == '-' || cstr[0] == '+') {
431                 if (len <= 1) {
432                         return FALSE;
433                 }
434                 pos++;
435         }
436
437         while (pos < len) {
438                 if (cstr[pos] < '0' || cstr[pos] > '9') {
439                         return FALSE;
440                 }
441                 pos++;
442         }
443
444         return TRUE;
445 }
446
447 bool is_substr_ncase(const char *parentstr, const char *substr)
448 {
449         if (NULL == parentstr || '\0' == parentstr[0])
450                 return FALSE;
451
452         int word_len = safeStrLen(parentstr);
453         int search_len = safeStrLen(substr);
454         int i;
455         bool result = FALSE;
456         for (i = 0; i < word_len; i++) {
457                 if (!strncasecmp(substr, &parentstr[i], search_len)) {
458                         result = TRUE;
459                         break;
460                 }
461         }
462         return result;
463 }
464
465 bool is_string_belong_to_array(const char *partern, const char **array,
466                                int array_num)
467 {
468         int idx = 0;
469         for (; idx < array_num; idx++) {
470                 if (!safeStrCmp(partern, array[idx])) {
471                         return TRUE;
472                 }
473         }
474         return FALSE;
475 }
476
477 //NULL, "", "  ", "     " etc.. are all empty strings
478 bool isEmptyStr(const char *str)
479 {
480         //if (NULL == str)
481         //      return TRUE;
482         while (str)
483         {
484                 if (*str != '\0' && *str != ' ')
485                 {
486                         return FALSE;
487                 }
488                 else if (*str == '\0')
489                 {
490                         return TRUE;
491                 }
492                 str++;
493         }
494         return TRUE;
495 }
496
497
498 int safeStrCmp(const char *s1, const char *s2)
499 {
500         /*  Check NULL value first */
501         if (isEmptyStr(s1) && isEmptyStr(s2)) {
502                 return 0;
503         } else if (isEmptyStr(s1)) {
504                 return 1;
505         } else if (isEmptyStr(s2)) {
506                 return SETTING_RETURN_FAIL;
507         }
508
509         return strcmp(s1, s2);
510 }
511
512 int safeStrNCmp(const char *s1, const char *s2, int len)
513 {
514
515         /*  Check NULL value first */
516         if (isEmptyStr(s1) && isEmptyStr(s2)) {
517                 return 0;
518         } else if (isEmptyStr(s1)) {
519                 return 1;
520         } else if (isEmptyStr(s2)) {
521                 return SETTING_RETURN_FAIL;
522         }
523
524         if (0 == len) {
525                 return 0;
526         }
527
528         return strncmp(s1, s2, len);
529 }
530
531 char *safeStrNCat(char *dst, const char *src, int maxlen)
532 {
533         if (dst && !isEmptyStr(src) && maxlen > 0) {
534                 (void) g_strlcat(dst, src, maxlen + 1);
535         }
536
537         return dst;
538 }
539
540
541 char *safeCopyStr(char *dst, const char *src, int maxlen)
542 {
543         if (maxlen < 0) {
544                 return NULL;
545         }
546
547         if (dst) {
548                 int len = 0;
549
550                 if (src) {
551                         int temp = (int)safeStrLen(src);
552                         len = (temp <= maxlen) ? temp : maxlen;
553                         memcpy(dst, src, len);
554                 }
555
556                 dst[len] = '\0';
557         }
558
559         return dst;
560 }
561
562 int safeStrLen(const char *str)
563 {
564         if (isEmptyStr(str)) {
565                 SETTING_TRACE_DEBUG("string is empty");
566                 return 0;
567         } else {
568                 return strlen(str);
569         }
570 }
571
572 /**
573 * get the int value of substring before delim
574 */
575 bool get_substring_int(const char **ipStr, int *ipValue, char delim)
576 {
577         int iValue = *ipValue = 0;
578         const char *str = *ipStr;
579
580         if (str == NULL || str[0] == 0) {       /* empty string */
581                 return FALSE;
582         }
583
584         bool bNegative = FALSE;
585         if ('-' == str[0]) {    /* allow Negative number.. */
586                 bNegative = TRUE;
587                 str++;
588         }
589         if (str[0] < '0' || str[0] > '9') {     /* first elementy is not digital */
590                 *ipStr = str;
591                 return FALSE;
592         }
593         if (str[0] == '0') {    /* first elementy is 0 */
594                 if (str[1] == delim) {
595                         str += 2;
596                         *ipStr = str;
597                         return TRUE;
598                 }
599                 if (str[1] == 0) {
600                         str++;
601                         *ipStr = str;
602                         return TRUE;
603                 }
604                 *ipStr = str;
605                 return FALSE;
606         }
607
608         for (;;) {
609 /****first elementy is not 0*/
610                 iValue = iValue * 10 + str[0] - '0';
611                 *ipValue = iValue;
612                 if (((unsigned int)iValue & 0x80000000) != 0) { /* think about overloading */
613                         break;
614                 }
615                 str++;
616                 if (str[0] == delim) {
617                         str++;
618                         if (bNegative) {
619                                 iValue = -iValue;
620                         }
621                         *ipStr = str;
622                         *ipValue = iValue;
623                         return TRUE;
624                 }
625                 if (str[0] == 0) {
626                         if (bNegative) {
627                                 iValue = -iValue;
628                         }
629                         *ipStr = str;
630                         *ipValue = iValue;
631                         return TRUE;
632                 }
633                 if (str[0] < '0' || str[0] > '9') {
634                         break;
635                 }
636         }
637
638         *ipStr = str;
639         return FALSE;
640 }
641
642 #define MaxIPAddressLength      15
643
644 /**
645 * CHeck Whether a special string Is An IP String
646 *
647 * @param ipstr string representing IP numbers like "aaa.bbb.ccc.ddd"
648 *
649 * @return true if it's IP format string otherwise false
650 */
651 bool is_ip_string(const char *ipstr)
652 {
653         if (NULL == ipstr || 0 == ipstr[0])
654                 return FALSE;
655         int len = (int)safeStrLen(ipstr);
656         if (len > MaxIPAddressLength) {
657                 return FALSE;
658         }
659
660         if (ipstr[len - 1] == '.') {
661                 return FALSE;
662         }
663
664         int ip;
665         int i = 0;
666         for (; i < 4; i++) {
667                 if (!get_substring_int(&ipstr, &ip, '.') || ip > 255) {
668                         SETTING_TRACE("ipstr:%s", ipstr);
669                         return FALSE;
670                 }
671                 SETTING_TRACE("ipstr:%s", ipstr);
672         }
673         if (ipstr[0] != 0) {
674                 return FALSE;
675         }
676
677         return TRUE;
678 }
679
680 int setting_invoke_reset_function(char *lib_name, service_h service, void *ext)
681 {
682         SETTING_TRACE("Enter %s with lib_name:%s", __FUNCTION__, lib_name);
683         int (*reset) (service_h pair, void *ptr);
684         int ret = OPERATE_LIB_SUCESS;
685
686         void *handle = dlopen(lib_name, RTLD_LAZY);
687         if (!handle) {
688                 SETTING_TRACE_ERROR(" >>>>>>>>>>> %s", dlerror());
689                 return LOAD_LIB_FAILED;
690         }
691
692         char *error = NULL;
693         reset = dlsym(handle, "setting_plugin_reset");
694         if ((error = dlerror()) != NULL) {
695                 SETTING_TRACE_ERROR(" >>>>>>>>>>> %s", error);
696                 dlclose(handle);
697                 return UNDEFINED_LIB_SYMBOL;
698         }
699
700         if (reset)
701                 ret = (*reset) (service, ext);   /*  CALL */
702
703         if (ret < 0) ret += UNDEFINED_LIB_SYMBOL;//from -2 on
704
705         dlclose(handle);
706         return ret;
707 }
708
709 int excuteCmd(char* exec_path, int option_num, ...)
710 {
711         char cmd[MAX_COMMON_BUFFER_LEN + 1] = {0, };
712         snprintf(cmd, MAX_COMMON_BUFFER_LEN,
713                  "%s", exec_path);
714
715
716         va_list args;
717         va_start(args, option_num);
718
719         char *para;
720         int argno = 0;
721         for (; argno < option_num; argno++) {
722                 para = va_arg( args, char *);
723                 SETTING_TRACE("Parameter #%d is: %s", argno, para);
724                 if (para)
725                 {
726                         g_strlcat(cmd, " ", MAX_COMMON_BUFFER_LEN + 1);
727                         g_strlcat(cmd, para, MAX_COMMON_BUFFER_LEN + 1);
728                 }
729         }
730
731         va_end(args);
732
733         SETTING_TRACE("excute:%s", cmd);
734         return system(cmd);
735 }
736
737
738 void create_fontlink(const char *linkpath, const char *linkname,
739                     const char *fontfilepath, const char *fontfilename)
740 {
741         char newpath[MAX_COMMON_BUFFER_LEN + 1];
742         char lcpath[MAX_COMMON_BUFFER_LEN + 1];
743
744         int ret =
745             snprintf(newpath, MAX_COMMON_BUFFER_LEN, "%s/%s", linkpath, linkname);
746         ret_if(ret < 0);
747         snprintf(lcpath, MAX_COMMON_BUFFER_LEN, "%s/%s", fontfilepath, fontfilename);
748
749         SETTING_TRACE("newpath:%s", newpath);
750         SETTING_TRACE("lcpath:%s", lcpath);
751
752         remove(newpath);
753         ret = symlink(lcpath, newpath);
754         setting_retm_if(ret != 0, "fail to call symlink");
755 }
756
757 char* get_icon_path(const char *package)
758 {
759         ail_appinfo_h handle;
760
761         char* ret_str;
762         char *icon_path = IMG_DefaultIcon;//The default icon must exist.(it will be installed by Setting App)
763
764         bool result = TRUE;
765         bool destroy = TRUE;
766
767         if (ail_package_get_appinfo(package, &handle) != AIL_ERROR_OK) {
768                 SETTING_TRACE_ERROR("Failed to ail_package_get_appinfo.");
769                 result = FALSE;
770                 destroy = FALSE;
771         }
772
773         if (result && ail_appinfo_get_str(handle, AIL_PROP_ICON_STR, &icon_path) != AIL_ERROR_OK) {
774                 SETTING_TRACE_ERROR("Failed to ail_appinfo_get_str.");
775                 result = FALSE;
776         }
777
778         if (result && access(icon_path, R_OK|F_OK ) !=0 ) {//The file cannot be accessed.
779                 SETTING_TRACE_ERROR("The file[%s] cannot be accessed. To use defaut icon.", icon_path);
780                 icon_path = IMG_DefaultIcon;
781         }
782
783         ret_str = strdup(icon_path);
784         if (destroy && ail_package_destroy_appinfo(handle) != AIL_ERROR_OK) {
785                 SETTING_TRACE_ERROR("Failed to ail_package_destroy_appinfo.");
786         }
787         return ret_str;
788 }
789
790 // SLP : 1
791 // not SLP : 0
792 int is_slp_binary()
793 {
794         dictionary* dic = iniparser_load("/etc/info.ini");
795         char* str = (char *)iniparser_getstr(dic, "Version:Build");
796         char* pos = str;
797         if (str) {
798                 while(*pos++){
799                         if('_' == *pos) {
800                                 *pos = '\0';
801                                 if (!strncmp(str, "SLP", 3)) return 1;
802                         }
803                 }
804         }
805         if (dic) {
806                 iniparser_freedict(dic);
807                 dic = NULL;
808         }
809         return 0;
810 }
811
812 bool isEmulBin()
813 {
814 #if DISABLED_CODE
815         char szEmul[__LEN_STR_EMUL+1];
816         const char* szBinVer = NULL;
817
818         dictionary* dic = iniparser_load(BIN_INFO_FILE_PATH);
819         // when failed to get the info, let's regard the binary as an emulator one
820         if (!dic)
821                 return TRUE;
822
823         szBinVer = (const char*)iniparser_getstr(dic, "Version:Build");
824         if (szBinVer) {
825                 char* str = g_strdup(szBinVer);
826                 if (str) {
827                         char* pPos = str;
828                         while (*pPos++) {
829                                 if ('_' == *pPos)
830                                         *pPos = ' ';
831                         }
832                         sscanf(str, "%*s %4s", szEmul);
833                 }
834                 G_FREE(str);
835         }
836
837         if (dic) {
838                 iniparser_freedict(dic);
839                 dic = NULL;
840         }
841
842         if (!strncmp(szEmul, __STR_EMUL, __LEN_STR_EMUL)) {
843                 return TRUE;
844         } else {
845                 return FALSE;
846         }
847 #else
848         char *model_str = NULL;
849         int ret = system_info_get_value_string(SYSTEM_INFO_KEY_MODEL, &model_str);
850         //SETTING_TRACE("model_str : %s", model_str);
851         if (ret != SYSTEM_INFO_ERROR_NONE) {
852                 SETTING_TRACE_ERROR("fail to call system_info_get_value_string");
853                 FREE(model_str);
854                 return FALSE;
855         }
856
857         if (0 == safeStrCmp(KeyStr_Emulator, model_str)) {
858                 FREE(model_str);
859                 return TRUE;
860         } else {
861                 FREE(model_str);
862                 return FALSE;
863         }
864 #endif
865 }
866
867 int get_popup_btn_response_type(const char *btn_str)
868 {
869         SETTING_TRACE("btn_str:%s", btn_str);
870         POPUP_BTN_RESPONSE_TYPE rsp_type = POPUP_RESPONSE_INVALID;
871         retv_if(!btn_str, rsp_type);
872         if (0 == safeStrCmp(btn_str, _("IDS_COM_BODY_CLOSE"))
873             || 0 == safeStrCmp(btn_str, _("IDS_COM_SK_TRY"))//KeyStr_Try
874             || 0 == safeStrCmp(btn_str, _("IDS_COM_SK_OK"))
875             || 0 == safeStrCmp(btn_str, _("IDS_COM_SK_YES"))
876             || 0 == safeStrCmp(btn_str, _("IDS_ST_BODY_USE_MOTION"))
877             || 0 == safeStrCmp(btn_str, _("IDS_COM_POP_TRY_AGAIN"))
878             || 0 == safeStrCmp(btn_str, _("IDS_COM_SK_DELETE"))
879             || 0 == safeStrCmp(btn_str, _(KeyStr_Save)))
880         {
881                 rsp_type = POPUP_RESPONSE_OK;
882         }
883         else //for others,
884         {
885                 rsp_type = POPUP_RESPONSE_CANCEL;
886         }
887         return rsp_type;
888 }
889
890 #define SUPPORT_STATIC_LOAD 1
891 #if SUPPORT_STATIC_LOAD
892 static char *system_service_list[MAX_SYSTEM_SERVICE_NUM] = {NULL, };
893 #endif
894 void clear_system_service_data()
895 {
896         #if SUPPORT_STATIC_LOAD
897         int idx;
898         for (idx = 0; idx < MAX_SYSTEM_SERVICE_NUM && system_service_list[idx]; idx++)
899         {
900                 FREE(system_service_list[idx]);
901         }
902         #endif
903 }
904
905 bool is_system_service(const char *pkg_name)
906 {
907         //SLP native apps list,
908         if (0 == safeStrCmp(pkg_name, "com.samsung.menu-screen")
909             || 0 == safeStrCmp(pkg_name, "com.samsung.live-data-provider")
910             || 0 == safeStrCmp(pkg_name, "com.samsung.live-magazine")
911             || 0 == safeStrCmp(pkg_name, "com.samsung.pwlock")
912             || 0 == safeStrCmp(pkg_name, "com.samsung.quickpanel")
913             || 0 == safeStrCmp(pkg_name, "com.samsung.vtmain")
914             || 0 == safeStrCmp(pkg_name, "com.samsung.call")
915             || 0 == safeStrCmp(pkg_name, "usb_setting")
916             || 0 == safeStrCmp(pkg_name, "com.samsung.indicator")
917             || 0 == safeStrCmp(pkg_name, "com.samsung.cluster-home")
918            )
919         {
920                 return TRUE;
921         }
922
923         //OSP service app list,
924         bool ret = FALSE;
925 #if SUPPORT_STATIC_LOAD
926         int idx = 0;
927         if (!system_service_list[0]) //just load the file
928         {
929                 FILE *fp = fopen(SYSTEM_SERVICE_LIST_PATH, "r");
930                 if (fp) {
931                         char result[MAX_PKG_NAME_LEN+1] = {0, };
932                         while (fgets(result, MAX_PKG_NAME_LEN, fp))
933                         {
934                                 result[strlen(result) - 1] = '\0';//offset 1
935                                 system_service_list[idx++] = strdup(result);
936                         }
937                         fclose(fp);
938                 }
939
940         }
941
942         for (idx = 0; idx < MAX_SYSTEM_SERVICE_NUM && system_service_list[idx]; idx++)
943         {
944                 if (0 == safeStrCmp(pkg_name, system_service_list[idx])) {
945                         ret = TRUE;
946                         break;
947                 }
948         }
949 #else
950         FILE *fp = fopen(SYSTEM_SERVICE_LIST_PATH, "r");
951         if (fp) {
952                 char result[MAX_PKG_NAME_LEN+1] = {0, };
953                 while (fgets(result, MAX_PKG_NAME_LEN, fp))
954                 {
955                         result[strlen(result) - 1] = '\0';//offset 1
956                         if (0 == safeStrCmp(pkg_name, result)) {
957                                 ret = TRUE;
958                                 break;
959                         }
960                 }
961                 fclose(fp);
962         }
963 #endif
964         SETTING_TRACE("ret:%d", ret);
965         return ret;
966 }
967
968 /**
969   * should use g_free to free returned string
970   */
971 char *get_default_font(int language)
972 {
973         char *font_name = NULL;
974
975         switch(language)
976         {
977         case SETTING_LANG_KOREA:
978                 font_name = g_strdup("SLPSansKorean");
979                 break;
980         case SETTING_LANG_CHINA:
981                 /* output_font : SLPSansFallback*/
982         case SETTING_LANG_CANTONESE:
983                 /* output_font : SLPSansFallback*/
984         case SETTING_LANG_TAIWAN:
985                 font_name = g_strdup("SLPSansFallback");
986                 break;
987         case SETTING_LANG_JAPAN:
988                 font_name = g_strdup("UDGothic");
989                 break;
990         case SETTING_LANG_AUTOMATIC:
991                 /* output_font : HelveticaNeue*/
992         case SETTING_LANG_ENGLISH:
993                 /* output_font : HelveticaNeue*/
994         case SETTING_LANG_GERMAN:
995                 /* output_font : HelveticaNeue*/
996         case SETTING_LANG_DUTCH:
997                 /* output_font : HelveticaNeue*/
998         case SETTING_LANG_SPAINISH:
999                 /* output_font : HelveticaNeue*/
1000         case SETTING_LANG_PORTUGUESE:
1001                 /* output_font : HelveticaNeue*/
1002         case SETTING_LANG_GREEK:
1003                 /* output_font : HelveticaNeue*/
1004         case SETTING_LANG_ITALIAN:
1005                 /* output_font : HelveticaNeue*/
1006         case SETTING_LANG_FRENCH:
1007                 /* output_font : HelveticaNeue*/
1008         case SETTING_LANG_TURKISH:
1009                 /* output_font : HelveticaNeue*/
1010         case SETTING_LANG_RUSSIAN:
1011                 font_name = g_strdup("HelveticaNeue");
1012                 break;
1013         default:
1014                 font_name = g_strdup("HelveticaNeue");
1015                 break;
1016         }
1017         return font_name;
1018 }
1019
1020 void *font_conf_doc_parse(char *doc_name, char *font_name)
1021 {
1022         SETTING_TRACE_BEGIN;
1023         setting_retvm_if(doc_name == NULL, NULL, "Param data is NULL");
1024         setting_retvm_if(font_name == NULL, NULL, "Param data is NULL");
1025         xmlDocPtr doc = NULL;
1026         xmlNodePtr cur = NULL;
1027         xmlNodePtr cur2 = NULL;
1028         xmlNodePtr cur3 = NULL;
1029         xmlChar *key = NULL;
1030
1031         doc = xmlParseFile(doc_name);
1032         setting_retvm_if(doc == NULL, NULL, "Document not parsed successfully.");
1033
1034         cur = xmlDocGetRootElement(doc);
1035
1036         if (cur == NULL) {
1037                 SETTING_TRACE_DEBUG("empty document");
1038                 xmlFreeDoc(doc);
1039                 doc = NULL;
1040                 return NULL;
1041         }
1042
1043         if(xmlStrcmp(cur->name, (const xmlChar *)"fontconfig")) {
1044                 SETTING_TRACE_DEBUG("document of the wrong type, root node != fontconfig");
1045                 xmlFreeDoc(doc);
1046                 doc = NULL;
1047                 return NULL;
1048         }
1049
1050         cur = cur->xmlChildrenNode;
1051
1052         while(cur != NULL)
1053         {
1054                 if((!xmlStrcmp(cur->name, (const xmlChar *)"match")))
1055                 {
1056                         cur2 = cur->xmlChildrenNode;
1057                         while(cur2 != NULL)
1058                         {
1059                                 if((!xmlStrcmp(cur2->name, (const xmlChar *)"edit")))
1060                                 {
1061                                         cur3 = cur2->xmlChildrenNode;
1062                                         while(cur3 != NULL)
1063                                         {
1064                                                 if((!xmlStrcmp(cur3->name, (const xmlChar *)"string")))
1065                                                 {
1066                                                         xmlNodeSetContent(cur3->xmlChildrenNode, (const xmlChar *)font_name);
1067                                                         key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1);
1068                                                         SETTING_TRACE_DEBUG("after changed, string is: %s\n", key);
1069                                                         xmlFree(key);
1070                                                         key = NULL;
1071                                                         return doc;
1072                                                 }
1073                                                 cur3 = cur3->next;
1074                                         }
1075                                 }
1076                                 cur2 = cur2->next;
1077                         }
1078                 }
1079                 cur = cur->next;
1080         }
1081
1082         xmlFreeDoc(doc);
1083         doc = NULL;
1084         return NULL;
1085 }
1086
1087 /**
1088  * should use g_free to free return string
1089  */
1090 char *cur_font_get()
1091 {
1092         SETTING_TRACE_BEGIN;
1093         xmlDocPtr doc = NULL;
1094         xmlNodePtr cur = NULL;
1095         xmlNodePtr cur2 = NULL;
1096         xmlNodePtr cur3 = NULL;
1097         xmlChar *key = NULL;
1098
1099         char *font_name = NULL;
1100
1101         doc = xmlParseFile(SETTING_FONT_CONF_FILE);
1102         setting_retvm_if(doc == NULL, NULL, "Document not parsed successfully.");
1103
1104         cur = xmlDocGetRootElement(doc);
1105
1106         if(cur == NULL) {
1107                 SETTING_TRACE_DEBUG("empty document");
1108                 xmlFreeDoc(doc);
1109                 doc = NULL;
1110                 return NULL;
1111         }
1112
1113         if(xmlStrcmp(cur->name, (const xmlChar *)"fontconfig")) {
1114                 SETTING_TRACE_DEBUG("document of the wrong type, root node != fontconfig");
1115                 xmlFreeDoc(doc);
1116                 doc = NULL;
1117                 return NULL;
1118         }
1119
1120         cur = cur->xmlChildrenNode;
1121
1122         while(cur != NULL)
1123         {
1124                 if((!xmlStrcmp(cur->name, (const xmlChar *)"match")))
1125                 {
1126                         cur2 = cur->xmlChildrenNode;
1127                         while(cur2 != NULL)
1128                         {
1129                                 if((!xmlStrcmp(cur2->name, (const xmlChar *)"edit")))
1130                                 {
1131                                         cur3 = cur2->xmlChildrenNode;
1132                                         while(cur3 != NULL)
1133                                         {
1134                                                 if((!xmlStrcmp(cur3->name, (const xmlChar *)"string")))
1135                                                 {
1136                                                         key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1);
1137                                                         SETTING_TRACE_DEBUG("string is: %s", key);
1138
1139                                                         font_name = g_strdup((char *)key);
1140                                                         xmlFree(key);
1141                                                         key = NULL;
1142                                                         xmlFreeDoc(doc);
1143                                                         doc = NULL;
1144                                                         return font_name;
1145                                                 }
1146                                                 cur3 = cur3->next;
1147                                         }
1148                                 }
1149                                 cur2 = cur2->next;
1150                         }
1151                 }
1152                 cur = cur->next;
1153         }
1154
1155         xmlFreeDoc(doc);
1156         doc = NULL;
1157         return NULL;
1158 }
1159
1160 static int __font_size_get()
1161 {
1162         int font_size = -1;
1163         int value = -1;
1164         int err = -1;
1165         int ret = setting_get_int_slp_key(INT_SLP_SETTING_ACCESSIBILITY_FONT_SIZE, &value, &err);
1166         retvm_if(ret != 0, -1, "get vconf failed");
1167
1168         switch(value) {
1169         case SYSTEM_SETTINGS_FONT_SIZE_SMALL:
1170                 font_size = SMALL_FONT_DPI;
1171                 break;
1172         case SYSTEM_SETTINGS_FONT_SIZE_NORMAL:
1173                 font_size = MIDDLE_FONT_DPI;
1174                 break;
1175         case SYSTEM_SETTINGS_FONT_SIZE_LARGE:
1176                 font_size = LARGE_FONT_DPI;
1177                 break;
1178         case SYSTEM_SETTINGS_FONT_SIZE_HUGE:
1179                 font_size = HUGE_FONT_DPI;
1180                 break;
1181         case SYSTEM_SETTINGS_FONT_SIZE_GIANT:
1182                 font_size = GIANT_FONT_DPI;
1183                 break;
1184         default:
1185                 font_size = MIDDLE_FONT_DPI;
1186                 break;
1187         }
1188         return font_size;
1189 }
1190
1191 void font_config_set(char *font_name)
1192 {
1193         Eina_List *text_classes = NULL;
1194         Elm_Text_Class *etc = NULL;
1195         const Eina_List *l = NULL;
1196         Eina_List *fo_list = NULL;
1197         Elm_Font_Overlay *efo = NULL;
1198         int font_size = __font_size_get();
1199         int size = 0;
1200
1201         text_classes = elm_config_text_classes_list_get();
1202
1203         fo_list = (Eina_List *)elm_config_font_overlay_list_get();
1204
1205         Eina_List *ll = NULL;
1206         Eina_List *l_next = NULL;
1207
1208         Eina_Bool slp_medium_exist = EINA_FALSE;
1209         Eina_Bool slp_roman_exist = EINA_FALSE;
1210         Eina_Bool slp_bold_exist = EINA_FALSE;
1211         Eina_Bool slp_regular_exist = EINA_FALSE;
1212
1213         EINA_LIST_FOREACH_SAFE(fo_list, ll, l_next, efo)
1214         {
1215                 if (!safeStrCmp(efo->text_class, "slp_medium")) {
1216                         elm_config_font_overlay_set(efo->text_class, (const char*)font_name, efo->size);
1217                         slp_medium_exist = EINA_TRUE;
1218                 } else if (!safeStrCmp(efo->text_class, "slp_roman")) {
1219                         elm_config_font_overlay_set(efo->text_class, (const char*)font_name, efo->size);
1220                         slp_roman_exist = EINA_TRUE;
1221                 } else if (!safeStrCmp(efo->text_class, "slp_bold")) {
1222                         elm_config_font_overlay_set(efo->text_class, (const char*)font_name, efo->size);
1223                         slp_bold_exist = EINA_TRUE;
1224                 } else if (!safeStrCmp(efo->text_class, "slp_regular")) {
1225                         elm_config_font_overlay_set(efo->text_class, (const char*)font_name, efo->size);
1226                         slp_regular_exist = EINA_TRUE;
1227                 }
1228         }
1229
1230         /* if slp_XX do not exist, need to set them, font size is -100(100%) */
1231         if (slp_medium_exist == EINA_FALSE) {
1232                 elm_config_font_overlay_set("slp_medium", (const char*)font_name,  MIDDLE_FONT_DPI);
1233         }
1234         if (slp_roman_exist == EINA_FALSE) {
1235                 elm_config_font_overlay_set("slp_roman", (const char*)font_name,  MIDDLE_FONT_DPI);
1236         }
1237         if (slp_bold_exist == EINA_FALSE) {
1238                 elm_config_font_overlay_set("slp_bold", (const char*)font_name,  MIDDLE_FONT_DPI);
1239         }
1240         if (slp_regular_exist == EINA_FALSE) {
1241                 elm_config_font_overlay_set("slp_regular", (const char*)font_name,  MIDDLE_FONT_DPI);
1242         }
1243
1244         EINA_LIST_FOREACH(text_classes, l, etc)
1245         {
1246                 ll = NULL;
1247
1248                 size = font_size;
1249                 EINA_LIST_FOREACH(fo_list, ll, efo)
1250                 {
1251                         if (!safeStrCmp(etc->name, efo->text_class)) {
1252                                 size = efo->size;
1253                         }
1254                 }
1255                 elm_config_font_overlay_set(etc->name, (const char*)font_name, size);
1256         }
1257
1258         elm_config_font_overlay_apply();
1259         elm_config_all_flush();
1260         elm_config_engine_set("software_x11");
1261         elm_config_save();
1262         elm_config_text_classes_list_free(text_classes);
1263         text_classes = NULL;
1264
1265         // vconf update
1266         vconf_set_str(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME, font_name);
1267 }
1268
1269 void font_size_set()
1270 {
1271         Eina_List *text_classes = NULL;
1272         Elm_Text_Class *etc = NULL;
1273         const Eina_List *l = NULL;
1274         int font_size = __font_size_get();
1275         char *font_name = cur_font_get();
1276
1277         if (font_size == -1) {
1278                 SETTING_TRACE_DEBUG("failed to call font_size_get");
1279                 return;
1280         }
1281
1282         text_classes = elm_config_text_classes_list_get();
1283
1284         EINA_LIST_FOREACH(text_classes, l, etc)
1285         {
1286                 elm_config_font_overlay_set(etc->name, font_name, font_size);
1287         }
1288
1289         elm_config_all_flush();
1290         elm_config_engine_set("software_x11");
1291         elm_config_save();
1292         elm_config_text_classes_list_free(text_classes);
1293         text_classes = NULL;
1294         G_FREE(font_name);
1295 }