2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.tizenopensource.org/license
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <setting-common-general-func.h>
20 #include <setting-common-data-slp-setting.h>
23 #include <iniparser.h>
25 #define SETTING_USB_DEFAULT_MODE 0
26 #define DEFAULT_USB_BUF_SIZE 64
28 char *setting_file_basename(char *path)
30 if (NULL == path || '\0' == path[0]) {
31 return NULL; /* invalid arguement */
33 char *p = strrchr(path, '/');
35 return (char *)g_strdup(path); /* cannot find '/' */
38 return NULL; /* end with '/' */
40 return (char *)g_strdup(p + 1);
43 char *get_pa_usb_connect_mode_str()
45 int ret = SETTING_RETURN_FAIL;
47 char title_default_usb_subtitle[DEFAULT_USB_BUF_SIZE];
49 ret = vconf_get_int(VCONFKEY_SETAPPL_USB_MODE_INT, &value);
50 setting_retvm_if(SETTING_RETURN_FAIL == ret, NULL, "Failed to get vconf value"); /* file system exceptional handle */
54 case SETTING_USB_NONE_MODE:
55 ret = vconf_get_int(VCONFKEY_SETAPPL_USB_SEL_MODE_INT, &selMode);
56 setting_retvm_if(SETTING_RETURN_FAIL == ret, NULL, "Failed to get vconf value");
57 if (SETTING_USB_DEBUG_MODE == selMode)
58 return (char*)g_strdup(_("IDS_ST_BODY_USB_DEBUGGING"));
59 case SETTING_USB_DEFAULT_MODE:
60 snprintf(title_default_usb_subtitle, DEFAULT_USB_BUF_SIZE, "USB %s", _("IDS_COM_BODY_DEFAULT"));
61 return (char*)g_strdup(title_default_usb_subtitle);
62 case SETTING_USB_DEBUG_MODE:
63 return (char*)g_strdup(_("IDS_ST_BODY_USB_DEBUGGING"));
69 char *get_pa_theme_name_str()
71 int ret = SETTING_RETURN_FAIL;
73 char value[SETTING_STR_SLP_LEN] = { 0, };
76 ret = setting_get_string_slp_key(STR_SLP_SETTING_THEME_NAME, value, &err);
77 setting_retvm_if(SETTING_RETURN_FAIL == ret, NULL, "Failed to get vconf value"); /* file system exceptional handle */
79 name = strrchr(value, '/');
80 ext = strstr(value, ".edj");
88 ext = value + safeStrLen(value);
90 if (0 == safeStrCmp("tizen-black-hd", name)) {
91 return (char *)g_strdup(_("IDS_ST_BODY_BLACK"));
92 } else if (0 == safeStrCmp("tizen-hd", name)) {
93 return (char *)g_strdup(_("IDS_ST_BODY_WHITE"));
95 return (char *)g_strndup(name, ext - name);
99 char *get_brightness_mode_str()
102 setting_get_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_BRIGHTNESS, &value,
105 return setting_gettext("IDS_COM_BODY_AUTOMATIC");
107 return setting_gettext("IDS_COM_BODY_MANUAL");
111 char *get_pa_backlight_time_str()
114 char backlight_time_str[MAX_DISPLAY_NAME_LEN_ON_UI];
118 setting_get_int_slp_key(INT_SLP_SETTING_LCD_TIMEOUT_NORMAL, &value,
120 if (SETTING_RETURN_FAIL == ret || value < 0) { /* file system exceptional handle, eg. The vconf file lost due to file system. */
121 value = 30; /* keep agree with postin file */
122 setting_set_int_slp_key(INT_SLP_SETTING_LCD_TIMEOUT_NORMAL,
127 snprintf(backlight_time_str, sizeof(backlight_time_str), "%s",
129 } else if (value >= 60) {
130 snprintf(backlight_time_str, sizeof(backlight_time_str),
131 "%d %s", value / 60, (char *)(_("IDS_COM_BODY_MINUTES_LC")));
133 snprintf(backlight_time_str, sizeof(backlight_time_str),
134 "%d %s", value, (char *)(_("IDS_COM_BODY_SECONDS_LC")));
136 return (char *)g_strdup(backlight_time_str);
139 char *get_pa_powersaving_at_str()
142 char powersaving_at_str[MAX_DISPLAY_NAME_LEN_ON_UI + 1];
146 setting_get_int_slp_key(INT_SLP_SETTING_POWERSAVING_AT, &value,
148 if (SETTING_RETURN_FAIL == ret || value < 0) { /* file system exceptional handle, eg. The vconf file lost due to file system. */
149 value = 30; /* keep agree with postin file */
150 setting_set_int_slp_key(INT_SLP_SETTING_POWERSAVING_AT,
154 snprintf(powersaving_at_str, MAX_DISPLAY_NAME_LEN_ON_UI, "%d%% %s",
155 value, "battery power");
156 return (char *)g_strdup(powersaving_at_str);
159 char *get_pa_screen_timeout_str()
162 char screen_timeout_str[MAX_DISPLAY_NAME_LEN_ON_UI + 1];
166 setting_get_int_slp_key(INT_SLP_SETTING_POWERSAVING_SCREEN_TIMEOUT,
169 if (SETTING_RETURN_FAIL == ret || value < 0) { /* file system exceptional handle, eg. The vconf file lost due to file system. */
170 value = 15; /* keep agree with postin file */
171 setting_set_int_slp_key
172 (INT_SLP_SETTING_POWERSAVING_SCREEN_TIMEOUT, value, &err);
176 snprintf(screen_timeout_str, MAX_DISPLAY_NAME_LEN_ON_UI, "%s",
178 } else if (value >= 60) {
179 snprintf(screen_timeout_str, MAX_DISPLAY_NAME_LEN_ON_UI,
181 (char *)(_("IDS_COM_BODY_MINUTES_LC")));
183 snprintf(screen_timeout_str, MAX_DISPLAY_NAME_LEN_ON_UI,
185 (char *)(_("IDS_COM_BODY_SECONDS_LC")));
187 return (char *)g_strdup(screen_timeout_str);
190 char *get_pa_display_language_str()
192 int ret = SETTING_RETURN_FAIL;
195 setting_lang_entry* table = setting_get_language_table();
196 ret = setting_get_int_slp_key(INT_SLP_SETTING_LANG, &index, &err);
197 return (char *)g_strdup(table[index].title); // "auto"
200 char *setting_pa_main_get_language()
202 setting_lang_entry* table = setting_get_language_table();
203 char* pa_path = vconf_get_str(VCONFKEY_LANGSET);
206 for (i=0; i<SETTING_LANG_MAX; i++) {
207 if ( ! strcmp(table[i].locale, pa_path) )
210 return (char *)g_strdup(table[i].locale);
214 return (char *)g_strdup(table[0].locale); // "auto"
217 char *get_pa_Wi_Fi_on_off_str()
220 int ret = SETTING_RETURN_FAIL;
222 setting_get_int_slp_key(INT_SLP_SETTING_WIFI_STATUS, &value, &err);
224 if (SETTING_RETURN_FAIL == ret) { /* file system exceptional handle, eg. The vconf file lost due to file system. */
225 value = VCONFKEY_WIFI_OFF;
226 setting_set_int_slp_key(INT_SLP_SETTING_WIFI_STATUS,
227 VCONFKEY_WIFI_OFF, &err);
231 char *pa_wifi_device = vconf_get_str(VCONFKEY_WIFI_CONNECTED_AP_NAME);
232 SETTING_TRACE("pa_wifi_device:%s", pa_wifi_device);
233 if (NULL != pa_wifi_device && '\0' != pa_wifi_device[0]) {
234 return pa_wifi_device;
237 return (char*)g_strdup(setting_gettext("IDS_COM_BODY_ON_M_STATUS"));
239 return (char*)g_strdup(setting_gettext("IDS_COM_BODY_OFF_M_STATUS"));
243 char *get_Mobile_AP_on_off_str()
247 setting_get_int_slp_key(INT_SLP_SETTING_MOBILE_AP_STATUS, &value,
250 /* 7 means bitwise-operation */
257 if (0 != ret || value < 0 || value > 7) {
259 setting_set_int_slp_key(INT_SLP_SETTING_MOBILE_AP_STATUS, value,
264 return setting_gettext("IDS_COM_BODY_ON_M_STATUS");
266 return setting_gettext("IDS_COM_BODY_OFF_M_STATUS");
270 char *get_BT_on_off_str()
272 int ret = SETTING_RETURN_FAIL;
274 ret = setting_get_int_slp_key(INT_SLP_SETTING_BT_STATUS, &value, &err);
276 if (SETTING_RETURN_FAIL == ret) { /* file system exceptional handle, eg. The vconf file lost due to file system. */
277 value = VCONFKEY_BT_STATUS_OFF;
278 setting_set_int_slp_key(INT_SLP_SETTING_BT_STATUS,
279 VCONFKEY_BT_STATUS_OFF, &err);
282 if (VCONFKEY_BT_STATUS_OFF == value) {
283 return setting_gettext("IDS_COM_BODY_OFF_M_STATUS");
285 return setting_gettext("IDS_COM_BODY_ON_M_STATUS");
289 char *get_pa_time_format_str()
291 int ret = SETTING_RETURN_FAIL;
293 char *format_str[] = { _("IDS_COM_BODY_12_HOURS"),
294 _("IDS_ST_BODY_24_HOURS") };
296 setting_get_int_slp_key(INT_SLP_SETTING_REGIONFORMAT_TIME1224,
298 setting_retvm_if(SETTING_RETURN_FAIL == ret, NULL, "Failed to get vconf value"); /* file system exceptional handle */
301 /* value 1:12H 2:24H */
302 if (value > 0 && value <= 2) /* 1, 2 */
303 return (char *)g_strdup(format_str[value - 1]);
305 return (char *)g_strdup(format_str[0]); /* set to 12H compelsery */
309 char *get_pa_date_format_str()
311 int ret = SETTING_RETURN_FAIL;
313 char *format_str[] = { _("IDS_ST_BODY_DDMMYYYY_DOT"),
314 _("IDS_ST_BODY_MM_DD_YYYY_DOT"),
315 _("IDS_COM_BODY_YYYYMMDD"), _("IDS_ST_BODY_YYYY_DD_MM_DOT")
318 setting_get_int_slp_key(INT_SLP_SETTING_DATE_FORMAT, &value, &err);
319 setting_retvm_if(SETTING_RETURN_FAIL == ret, NULL, "Failed to get vconf value"); /* file system exceptional handle */
320 return (char *)g_strdup(format_str[value]);
323 char *get_pa_week_format_str()
325 int ret = SETTING_RETURN_FAIL;
327 char *format_str[] = {
328 _("IDS_ST_BODY_SUNDAY"),
329 _("IDS_ST_BODY_MONDAY"),
330 _("IDS_ST_BODY_TUESDAY"),
331 _("IDS_ST_BODY_WEDNESDAY"),
332 _("IDS_ST_BODY_THURSDAY"),
333 _("IDS_ST_BODY_FRIDAY"),
334 _("IDS_ST_BODY_SATURDAY"),
338 setting_get_int_slp_key(INT_SLP_SETTING_WEEK_FORMAT, &value, &err);
339 setting_retvm_if(SETTING_RETURN_FAIL == ret, NULL, "Failed to get vconf value"); /* file system exceptional handle */
340 return (char *)g_strdup(format_str[value]);
343 char *get_pa_roaming_network_str()
345 int ret = SETTING_RETURN_FAIL;
347 char *roaming_str[] = {
348 _("IDS_ST_BODY_AUTO_DOWNLOAD"), _("IDS_COM_BODY_MANUAL"),
349 _("IDS_ST_BODY_ALWAYS_REJECT")
352 setting_get_int_slp_key(INT_SLP_SETTING_ROAMING_NETWORK, &value,
354 setting_retvm_if(SETTING_RETURN_FAIL == ret, NULL, "Failed to get vconf value"); /* file system exceptional handle */
355 return (char *)g_strdup(roaming_str[value]);
358 double get_widgets_factor()
360 return elm_scale_get();
363 static Theme_Type g_theme_type = Theme_Invalid;
366 Theme_Type setting_get_theme()
368 if (Theme_Invalid == g_theme_type) {
369 int ret = SETTING_RETURN_FAIL;
371 char value[SETTING_STR_SLP_LEN] = { 0, };
375 setting_get_string_slp_key(STR_SLP_SETTING_THEME_NAME,
377 setting_retvm_if(SETTING_RETURN_FAIL == ret, Theme_ERROR, "Failed to get vconf value"); /* file system exceptional handle */
379 name = strrchr(value, '/');
380 ext = strstr(value, ".edj");
388 ext = value + safeStrLen(value);
390 /* special theme process.. */
391 if (0 == safeStrCmp("tizen-black", name) || 0 == safeStrCmp("tizen-black-hd", name)) {
392 g_theme_type = Theme_Tizen_Black;
393 } else if (0 == safeStrCmp("tizen", name) || 0 == safeStrCmp("tizen-hd", name)) {
394 g_theme_type = Theme_Tizen;
395 } else if (0 == safeStrCmp("tizen-blue", name)) {
396 g_theme_type = Theme_Tizen_Blue;
397 } else if (0 == safeStrCmp("grey-hd", name)) {
398 g_theme_type = Theme_Tizen_Grey;
400 g_theme_type = Theme_ERROR;
407 char *setting_gettext(const char *s)
409 /* fisrt find in app pg */
415 char *p = dgettext(SETTING_PACKAGE, s);
417 if (!safeStrCmp(s, p)) { /* not found */
418 /* find in system pkg */
419 p = dgettext(SYSTEM_PACKAGE, s);
424 bool is_digital_str(const char *cstr)
426 if (cstr == NULL || cstr[0] == 0) {
430 int len = (int)(safeStrLen(cstr));
432 if (cstr[0] == '-' || cstr[0] == '+') {
440 if (cstr[pos] < '0' || cstr[pos] > '9') {
449 bool is_substr_ncase(const char *parentstr, const char *substr)
451 if (NULL == parentstr || '\0' == parentstr[0])
454 int word_len = safeStrLen(parentstr);
455 int search_len = safeStrLen(substr);
458 for (i = 0; i < word_len; i++) {
459 if (!strncasecmp(substr, &parentstr[i], search_len)) {
467 bool is_string_belong_to_array(const char *partern, const char **array,
471 for (; idx < array_num; idx++) {
472 if (!safeStrCmp(partern, array[idx])) {
479 bool isEmptyStr(const char *str)
481 if (NULL == str || str[0] == '\0') {
488 int safeStrCmp(const char *s1, const char *s2)
490 /* Check NULL value first */
491 if (isEmptyStr(s1) && isEmptyStr(s2)) {
493 } else if (isEmptyStr(s1)) {
495 } else if (isEmptyStr(s2)) {
496 return SETTING_RETURN_FAIL;
499 return strcmp(s1, s2);
502 int safeStrNCmp(const char *s1, const char *s2, int len)
505 /* Check NULL value first */
506 if (isEmptyStr(s1) && isEmptyStr(s2)) {
508 } else if (isEmptyStr(s1)) {
510 } else if (isEmptyStr(s2)) {
511 return SETTING_RETURN_FAIL;
518 return strncmp(s1, s2, len);
521 char *safeStrNCat(char *dst, const char *src, int maxlen)
523 if (dst && !isEmptyStr(src) && maxlen > 0) {
524 (void) g_strlcat(dst, src, maxlen + 1);
531 char *safeCopyStr(char *dst, const char *src, int maxlen)
541 int temp = (int)safeStrLen(src);
542 len = (temp <= maxlen) ? temp : maxlen;
543 memcpy(dst, src, len);
552 int safeStrLen(const char *str)
554 if (isEmptyStr(str)) {
555 SETTING_TRACE_DEBUG("string is empty");
563 * get the int value of substring before delim
565 bool get_substring_int(const char **ipStr, int *ipValue, char delim)
567 int iValue = *ipValue = 0;
568 const char *str = *ipStr;
570 if (str == NULL || str[0] == 0) { /* empty string */
574 bool bNegative = FALSE;
575 if ('-' == str[0]) { /* allow Negative number.. */
579 if (str[0] < '0' || str[0] > '9') { /* first elementy is not digital */
583 if (str[0] == '0') { /* first elementy is 0 */
584 if (str[1] == delim) {
599 /****first elementy is not 0*/
600 iValue = iValue * 10 + str[0] - '0';
602 if (((unsigned int)iValue & 0x80000000) != 0) { /* think about overloading */
606 if (str[0] == delim) {
623 if (str[0] < '0' || str[0] > '9') {
632 #define MaxIPAddressLength 15
635 * CHeck Whether a special string Is An IP String
637 * @param ipstr string representing IP numbers like "aaa.bbb.ccc.ddd"
639 * @return true if it's IP format string otherwise false
641 bool is_ip_string(const char *ipstr)
643 if (NULL == ipstr || 0 == ipstr[0])
645 int len = (int)safeStrLen(ipstr);
646 if (len > MaxIPAddressLength) {
650 if (ipstr[len - 1] == '.') {
657 if (!get_substring_int(&ipstr, &ip, '.') || ip > 255) {
658 SETTING_TRACE("ipstr:%s", ipstr);
661 SETTING_TRACE("ipstr:%s", ipstr);
670 int setting_invoke_reset_function(char *lib_name, bundle *data, void *ext)
672 SETTING_TRACE("Enter %s with lib_name:%s", __FUNCTION__, lib_name);
673 int (*reset) (bundle *pair, void *ptr);
674 int ret = OPERATE_LIB_SUCESS;
676 void *handle = dlopen(lib_name, RTLD_LAZY);
678 SETTING_TRACE_ERROR(" >>>>>>>>>>> %s", dlerror());
679 return LOAD_LIB_FAILED;
683 reset = dlsym(handle, "setting_plugin_reset");
684 if ((error = dlerror()) != NULL) {
685 SETTING_TRACE_ERROR(" >>>>>>>>>>> %s", error);
687 return UNDEFINED_LIB_SYMBOL;
691 ret = (*reset) (data, ext); /* CALL */
693 if (ret < 0) ret += UNDEFINED_LIB_SYMBOL;//from -2 on
699 int excuteCmd(char* exec_path, int option_num, ...)
701 char cmd[MAX_COMMON_BUFFER_LEN + 1] = {0, };
702 snprintf(cmd, MAX_COMMON_BUFFER_LEN,
707 va_start(args, option_num);
711 for (; argno < option_num; argno++) {
712 para = va_arg( args, char *);
713 SETTING_TRACE("Parameter #%d is: %s", argno, para);
716 g_strlcat(cmd, " ", MAX_COMMON_BUFFER_LEN + 1);
717 g_strlcat(cmd, para, MAX_COMMON_BUFFER_LEN + 1);
723 SETTING_TRACE("excute:%s", cmd);
727 void set_font_dpi(short size)
729 char buf[MAX_COMMON_BUFFER_LEN +1];
730 const char *xrespath = "/opt/etc/X11/Xresources";
731 int ret = snprintf(buf, sizeof(buf), "/bin/echo \"Xft.dpi: %d\" > %s", size,
735 ret = system(buf);//echo Xft.dpi: 48 > /opt/etc/X11/Xresources
737 SETTING_TRACE_ERROR("Xft.dpi error \n");
742 snprintf(buf, MAX_COMMON_BUFFER_LEN, "/usr/bin/xrdb -nocpp -load %s", xrespath);
745 SETTING_TRACE_ERROR("xrdb -nocpp -load \n");
748 SETTING_TRACE("xrdb -nocpp -load - DONE \n");
752 void create_fontlink(const char *linkpath, const char *linkname,
753 const char *fontfilepath, const char *fontfilename)
755 char newpath[MAX_COMMON_BUFFER_LEN + 1];
756 char lcpath[MAX_COMMON_BUFFER_LEN + 1];
759 snprintf(newpath, MAX_COMMON_BUFFER_LEN, "%s/%s", linkpath, linkname);
761 snprintf(lcpath, MAX_COMMON_BUFFER_LEN, "%s/%s", fontfilepath, fontfilename);
763 SETTING_TRACE("newpath:%s", newpath);
764 SETTING_TRACE("lcpath:%s", lcpath);
767 symlink(lcpath, newpath);
770 char* get_icon_path(const char *package)
772 ail_appinfo_h handle;
775 char *icon_path = IMG_DefaultIcon;//The default icon must exist.(it will be installed by Setting App)
780 if (ail_package_get_appinfo(package, &handle) != AIL_ERROR_OK) {
781 SETTING_TRACE_ERROR("Failed to ail_package_get_appinfo.");
786 if (result && ail_appinfo_get_str(handle, AIL_PROP_ICON_STR, &icon_path) != AIL_ERROR_OK) {
787 SETTING_TRACE_ERROR("Failed to ail_appinfo_get_str.");
791 if (result && access(icon_path, R_OK|F_OK ) !=0 ) {//The file cannot be accessed.
792 SETTING_TRACE_ERROR("The file[%s] cannot be accessed. To use defaut icon.", icon_path);
793 icon_path = IMG_DefaultIcon;
796 ret_str = strdup(icon_path);
797 if (destroy && ail_package_destroy_appinfo(handle) != AIL_ERROR_OK) {
798 SETTING_TRACE_ERROR("Failed to ail_package_destroy_appinfo.");
805 char szEmul[__LEN_STR_EMUL+1];
806 const char* szBinVer = NULL;
808 dictionary* dic = iniparser_load(BIN_INFO_FILE_PATH);
809 // when failed to get the info, let's regard the binary as an emulator one
813 szBinVer = (const char*)iniparser_getstr(dic, "Version:Build");
815 char* str = g_strdup(szBinVer);
822 sscanf(str, "%*s %4s", szEmul);
828 iniparser_freedict(dic);
832 if (!strncmp(szEmul, __STR_EMUL, __LEN_STR_EMUL)) {
839 int get_popup_btn_response_type(const char *btn_str)
841 SETTING_TRACE("btn_str:%s", btn_str);
842 POPUP_BTN_RESPONSE_TYPE rsp_type = POPUP_RESPONSE_INVALID;
843 retv_if(!btn_str, rsp_type);
844 if (0 == safeStrCmp(btn_str, _("IDS_COM_BODY_CLOSE"))
845 || 0 == safeStrCmp(btn_str, _("Try"))//KeyStr_Try
846 || 0 == safeStrCmp(btn_str, _("IDS_COM_SK_OK"))
847 || 0 == safeStrCmp(btn_str, _("IDS_COM_SK_YES"))
848 || 0 == safeStrCmp(btn_str, _("IDS_ST_BODY_USE_MOTION"))
849 || 0 == safeStrCmp(btn_str, _("IDS_COM_POP_TRY_AGAIN"))
850 || 0 == safeStrCmp(btn_str, _("IDS_COM_SK_DELETE")))
852 rsp_type = POPUP_RESPONSE_OK;
856 rsp_type = POPUP_RESPONSE_CANCEL;