From 1b51d4db18460ababa0b49db3e70a9a51987755a Mon Sep 17 00:00:00 2001 From: Gukhwan Cho Date: Wed, 24 Jun 2015 22:27:04 +0900 Subject: [PATCH] get system settings locale Change-Id: I9536515caf2b30b1fdb68b49a0d1e2fd7e8b95c3 Signed-off-by: Gukhwan Cho --- CMakeLists.txt | 2 +- include/phone_number_types.h | 2 ++ packaging/phonenumber-utils.spec | 3 +- src/phn.c | 59 ++++++++++++++++++++++++-------- src/phn_common.h | 1 + src/phn_location.c | 49 ++++++++++---------------- src/phn_location.h | 8 +++-- src/phn_region_data.c | 56 ++++++++++++++++++++++++------ src/phn_region_data.h | 4 +-- 9 files changed, 123 insertions(+), 61 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dc13772..26e64d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ SET(SRC_FILE src/phn_phonenumber_wrapper.cpp ) INCLUDE(FindPkgConfig) -pkg_check_modules(PN_pkgs REQUIRED dlog icu-i18n glib-2.0 capi-base-common) +pkg_check_modules(PN_pkgs REQUIRED dlog icu-i18n glib-2.0 capi-base-common capi-system-system-settings) INCLUDE_DIRECTORIES(${PN_pkgs_INCLUDE_DIRS}) LINK_DIRECTORIES(${PN_pkgs_LIBRARY_DIRS}) diff --git a/include/phone_number_types.h b/include/phone_number_types.h index 3a7a01a..e6ca45a 100644 --- a/include/phone_number_types.h +++ b/include/phone_number_types.h @@ -67,6 +67,7 @@ typedef enum { PHONE_NUMBER_LANG_THAI, /**< Thai */ PHONE_NUMBER_LANG_TURKISH, /**< Turkish */ PHONE_NUMBER_LANG_VIETNAMESE, /**< Vietnamese */ + PHONE_NUMBER_LANG_SYSTEM, /**< Current System Value (Since 3.0) */ PHONE_NUMBER_LANG_MAX, } phone_number_lang_e; @@ -209,6 +210,7 @@ typedef enum { PHONE_NUMBER_REGION_WESTERN_SAHARA, /**< Western Sahara */ PHONE_NUMBER_REGION_ZAMBIA, /**< Zambia */ PHONE_NUMBER_REGION_ZIMBABWE, /**< Zimbabwe */ + PHONE_NUMBER_REGION_SYSTEM, /**< Current System Value (Since 3.0) */ PHONE_NUMBER_REGION_MAX, } phone_number_region_e; diff --git a/packaging/phonenumber-utils.spec b/packaging/phonenumber-utils.spec index 56aae9f..1bc9c86 100644 --- a/packaging/phonenumber-utils.spec +++ b/packaging/phonenumber-utils.spec @@ -1,6 +1,6 @@ Name: phonenumber-utils Summary: Phone Number Utilities -Version: 0.1.6 +Version: 0.1.7 Release: 0 Group: Telephony/Utilities License: Apache-2.0 @@ -13,6 +13,7 @@ BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(icu-i18n) BuildRequires: pkgconfig(protobuf) BuildRequires: pkgconfig(capi-base-common) +BuildRequires: pkgconfig(capi-system-system-settings) BuildRequires: libphonenumber-devel %description diff --git a/src/phn.c b/src/phn.c index 2e72dc5..960793d 100644 --- a/src/phn.c +++ b/src/phn.c @@ -30,8 +30,8 @@ API int phone_number_get_location_from_number(const char *number, phone_number_region_e region, phone_number_lang_e lang, char **location) { int ret; - const char *region_str = NULL; - const char *lang_str = NULL; + char *region_str = NULL; + char *lang_str = NULL; RETVM_IF(NULL == number || '\0' == *number, PHONE_NUMBER_ERROR_INVALID_PARAMETER, "Invalid parameter (number is NULL)"); @@ -42,35 +42,56 @@ API int phone_number_get_location_from_number(const char *number, RETVM_IF(NULL == location, PHONE_NUMBER_ERROR_INVALID_PARAMETER, "Invalid parameter (location is NULL)"); - region_str = phn_region_data_get_region_str(region); - RETVM_IF(NULL == region_str, PHONE_NUMBER_ERROR_NOT_SUPPORTED, "phn_region_info not found(%d)", region); + ret = phn_region_data_get_region_str(region, ®ion_str); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("phn_region_data_get_region_str() Fail(%d)", ret); + return ret; + } - lang_str = phn_region_data_get_lang_str(lang); - RETVM_IF(NULL == lang_str, PHONE_NUMBER_ERROR_NOT_SUPPORTED, "phn_lang_info not found(%d)", lang); + ret = phn_region_data_get_lang_str(lang, &lang_str); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("phn_region_data_get_lang_str() Fail(%d)", ret); + free(region_str); + return ret; + } char *location_file = NULL; ret = phn_location_find_extra_data(region_str, &location_file); if (PHONE_NUMBER_ERROR_NONE == ret && location_file) { - ret = phn_location_get_location_from_extra_data(location_file, number, region, lang, location); + ret = phn_location_get_location_from_extra_data(location_file, number, + region_str, lang_str, location); free(location_file); - if (PHONE_NUMBER_ERROR_NONE == ret && *location) + if (PHONE_NUMBER_ERROR_NONE == ret && *location) { + free(region_str); + free(lang_str); return PHONE_NUMBER_ERROR_NONE; + } } bool exist = phn_region_data_find_match_info(region, lang); - if (!exist) - lang_str = PHN_REGION_DEFAULT_LANG; + if (false == exist) { + INFO("Language not matched with Region. Set to defualt language."); + free(lang_str); + lang_str = strdup(PHN_REGION_DEFAULT_LANG); + } ret = phn_get_location_from_number(number, region_str, lang_str, location); - RETVM_IF(PHONE_NUMBER_ERROR_NONE != ret, ret, "phn_get_location_from_number() Fail(%d)", ret); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("phn_get_location_from_number() Fail(%d)", ret); + free(region_str); + free(lang_str); + return ret; + } + free(region_str); + free(lang_str); return PHONE_NUMBER_ERROR_NONE; } API int phone_number_get_formatted_number(const char *number, phone_number_region_e region, char **formatted_number) { int ret; - const char *region_str = NULL; + char *region_str = NULL; RETVM_IF(NULL == number || '\0' == *number, PHONE_NUMBER_ERROR_INVALID_PARAMETER, "Invalid parameter (number is NULL)"); @@ -78,12 +99,20 @@ API int phone_number_get_formatted_number(const char *number, phone_number_regio "Invalid parameter (region:%d)", region); RETVM_IF(NULL == formatted_number, PHONE_NUMBER_ERROR_INVALID_PARAMETER, "Invalid parameter (formatted_number is NULL)"); - region_str = phn_region_data_get_region_str(region); - RETVM_IF(NULL == region_str, PHONE_NUMBER_ERROR_NOT_SUPPORTED, "phn_region_info not found(%d)", region); + ret = phn_region_data_get_region_str(region, ®ion_str); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("phn_region_data_get_region_str() Fail(%d)", ret); + return ret; + } ret = phn_get_formatted_number(number, region_str, formatted_number); - RETVM_IF(PHONE_NUMBER_ERROR_NONE != ret, ret, "phn_get_formatted_number() Fail(%d)", ret); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("phn_get_formatted_number() Fail(%d)", ret); + free(region_str); + return ret; + } + free(region_str); return PHONE_NUMBER_ERROR_NONE; } diff --git a/src/phn_common.h b/src/phn_common.h index 10d0b6c..d550140 100644 --- a/src/phn_common.h +++ b/src/phn_common.h @@ -22,6 +22,7 @@ #include "phone_number_errors.h" #define PHN_STR_SHORT_LEN 1024 +#define STRING_EQUAL 0 #define LOG_TAG "PHONE_NUMBER_UTILS" #include diff --git a/src/phn_location.c b/src/phn_location.c index 0e8f6b1..aea8c54 100644 --- a/src/phn_location.c +++ b/src/phn_location.c @@ -29,14 +29,8 @@ #include "phn_region_data.h" #include "phn_location.h" -#define PHN_PATH_MAX_LEN 1024 -#define PHN_COPY_SIZE_MAX 4096 - #define PHN_LOCATION_DIR "/opt/usr/data/phonenumber-utils" -#define PHN_LOCATION_DOWNLOAD_DIR "/opt/usr/data/phonenumber-utils/downloads" #define PHN_LOCATION_FILE_PREFIX "location" -#define PHN_LOCATION_LANG_INDEX_MAX 3 -#define PHN_LOCATION_FILE_MARK 0xfefe0000 #define PHN_LOCATION_CHINA_MOBILE_SUFFIX_OFFSET 10000 #define PHN_LOCATION_CHINA_MOBILE_PREFIX_LEN 3 @@ -58,7 +52,7 @@ struct phn_location_header { int mobile_prefix_len; }; -int phn_location_find_extra_data(const char *region_str, char **p_location_file) +int phn_location_find_extra_data(char *region_str, char **p_location_file) { DIR *dirp = NULL; struct dirent **dir_list; @@ -80,7 +74,7 @@ int phn_location_find_extra_data(const char *region_str, char **p_location_file) while (idx != count) { const char *file_name = dir_list[idx]->d_name; if (0 == strncmp(file_name, location_prefix, strlen(location_prefix))) { - location_file = strdup(file_name); + location_file = g_strdup(file_name); break; } idx++; @@ -97,7 +91,7 @@ int phn_location_find_extra_data(const char *region_str, char **p_location_file) } int phn_location_get_location_from_extra_data(const char *file, const char *number, - phone_number_region_e region, phone_number_lang_e lang, char **p_location) + char *region_str, char *lang_str, char **p_location) { int ret = 0; int city_str_len = 0; @@ -110,30 +104,29 @@ int phn_location_get_location_from_extra_data(const char *file, const char *numb char file_path[PHN_STR_SHORT_LEN] = {0}; /* support region - CN, support lang - zh,en,ko */ - RETVM_IF(region != PHONE_NUMBER_REGION_CHINA, PHONE_NUMBER_ERROR_NOT_SUPPORTED, - "Not supported region(%d)", region); + RETV_IF(NULL == region_str, PHONE_NUMBER_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == lang_str, PHONE_NUMBER_ERROR_INVALID_PARAMETER); + + while (real_number && real_number[0] == '0') + real_number++; + RETVM_IF(NULL == real_number, PHONE_NUMBER_ERROR_INVALID_PARAMETER, "number=%s", + number); + + char lang_region[PHN_STR_SHORT_LEN] = {0}; + snprintf(lang_region, sizeof(lang_region), "%s_%s", lang_str, region_str); int lang_index = 0; - switch (lang) { - case PHONE_NUMBER_LANG_CHINESE: + if (STRING_EQUAL == strcmp(lang_region, PHN_LOCATION_SUPPORT_ZH_CN)) { lang_index = 0; - break; - case PHONE_NUMBER_LANG_ENGLISH: + } else if (STRING_EQUAL == strcmp(lang_region, PHN_LOCATION_SUPPORT_EN_CN)) { lang_index = 1; - break; - case PHONE_NUMBER_LANG_KOREAN: + } else if (STRING_EQUAL == strcmp(lang_region, PHN_LOCATION_SUPPORT_KO_CN)) { lang_index = 2; - break; - default: - ERR("Not supported lang(%d)", lang); + } else { + ERR("Not supported(%s)", lang_region); return PHONE_NUMBER_ERROR_NOT_SUPPORTED; } - while (real_number && real_number[0] == '0') - real_number++; - RETVM_IF(NULL == real_number, PHONE_NUMBER_ERROR_INVALID_PARAMETER, "number=%s", - number); - snprintf(file_path, sizeof(file_path), "%s/%s", PHN_LOCATION_DIR, file); int fd = open(file_path, O_RDONLY); @@ -281,11 +274,7 @@ int phn_location_get_location_from_extra_data(const char *file, const char *numb } } - if (region != PHONE_NUMBER_REGION_CHINA) { - ERR("Invalid region(%d)", region); - close(fd); - return PHONE_NUMBER_ERROR_NO_DATA; - } else if (strlen(number) < PHN_LOCATION_CHINA_MOBILE_NUMBER_MIN_LEN) { + if (strlen(number) < PHN_LOCATION_CHINA_MOBILE_NUMBER_MIN_LEN) { ERR("Invalid number(%s)", number); close(fd); return PHONE_NUMBER_ERROR_INVALID_PARAMETER; diff --git a/src/phn_location.h b/src/phn_location.h index 1a7ae2b..bf7b18e 100644 --- a/src/phn_location.h +++ b/src/phn_location.h @@ -21,8 +21,12 @@ #include "phone_number_types.h" -int phn_location_find_extra_data(const char *region_str, char **p_location_file); +#define PHN_LOCATION_SUPPORT_ZH_CN "zh_CN" +#define PHN_LOCATION_SUPPORT_EN_CN "en_CN" +#define PHN_LOCATION_SUPPORT_KO_CN "ko_CN" + +int phn_location_find_extra_data(char *region_str, char **p_location_file); int phn_location_get_location_from_extra_data(const char *file, const char *number, - phone_number_region_e region, phone_number_lang_e lang, char **p_location); + char *region_str, char *lang_str, char **p_location); #endif /* __PHN_LOCATION_H__ */ diff --git a/src/phn_region_data.c b/src/phn_region_data.c index 34fd94d..d2c5898 100644 --- a/src/phn_region_data.c +++ b/src/phn_region_data.c @@ -16,6 +16,10 @@ * limitations under the License. * */ +#include +#include +#include + #include "phn_common.h" #include "phn_region_data.h" @@ -108,6 +112,7 @@ const struct phn_match_info phn_match_info_table[] = { {PHONE_NUMBER_REGION_TURKEY, PHONE_NUMBER_LANG_TURKISH}, {PHONE_NUMBER_REGION_SAUDI_ARABIA, PHONE_NUMBER_LANG_ARABIC}, {PHONE_NUMBER_REGION_ISLAMIC_REPUBLIC_OF_IRAN, PHONE_NUMBER_LANG_PERSIAN}, + {PHONE_NUMBER_REGION_SYSTEM, PHONE_NUMBER_LANG_SYSTEM}, }; const struct phn_lang_info phn_lang_info_table[] = { @@ -278,25 +283,56 @@ const struct phn_region_info phn_region_info_table[] = { {"ZW", PHONE_NUMBER_REGION_ZIMBABWE}, }; -const char* phn_region_data_get_region_str(phone_number_region_e region) +int phn_region_data_get_region_str(phone_number_region_e region, char **region_str) { - int i; + int ret, i; + + if (PHONE_NUMBER_REGION_SYSTEM == region) { + char *str = NULL; + ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_COUNTRY, &str); + if (str) + *region_str = g_strdup(strchr(str, '_') + 1); + free(str); + if (SYSTEM_SETTINGS_ERROR_NONE != ret) { + ERR("system_settings_get_value_string() Fail(%d)", ret); + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + } + return PHONE_NUMBER_ERROR_NONE; + } for (i = 0; i < sizeof(phn_region_info_table)/sizeof(struct phn_region_info); i++) { - if (phn_region_info_table[i].region == region) - return phn_region_info_table[i].region_str; + if (phn_region_info_table[i].region == region) { + *region_str = g_strdup(phn_region_info_table[i].region_str); + return PHONE_NUMBER_ERROR_NONE; + } } - return NULL; + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; } -const char* phn_region_data_get_lang_str(phone_number_lang_e lang) +int phn_region_data_get_lang_str(phone_number_lang_e lang, char **lang_str) { - int i; + int ret, i; + + if (PHONE_NUMBER_LANG_SYSTEM == lang) { + char *str = NULL; + ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &str); + if (str) + *lang_str = g_strdup(strtok(str, "_")); + free(str); + if (SYSTEM_SETTINGS_ERROR_NONE != ret) { + ERR("system_settings_get_value_string() Fail(%d)", ret); + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + } + return PHONE_NUMBER_ERROR_NONE; + } + for (i = 0; i < sizeof(phn_lang_info_table)/sizeof(struct phn_lang_info); i++) { - if (phn_lang_info_table[i].lang == lang) - return phn_lang_info_table[i].lang_str; + if (phn_lang_info_table[i].lang == lang) { + *lang_str = g_strdup(phn_lang_info_table[i].lang_str); + return PHONE_NUMBER_ERROR_NONE; + } } - return NULL; + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; } bool phn_region_data_find_match_info(phone_number_region_e region, diff --git a/src/phn_region_data.h b/src/phn_region_data.h index ef5da04..3c674df 100644 --- a/src/phn_region_data.h +++ b/src/phn_region_data.h @@ -23,8 +23,8 @@ #define PHN_REGION_DEFAULT_LANG "en" -const char* phn_region_data_get_region_str(phone_number_region_e region); -const char* phn_region_data_get_lang_str(phone_number_lang_e lang); +int phn_region_data_get_region_str(phone_number_region_e region, char **region_str); +int phn_region_data_get_lang_str(phone_number_lang_e lang, char **lang_str); bool phn_region_data_find_match_info(phone_number_region_e region, phone_number_lang_e lang); -- 2.34.1