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