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