tizen 2.3 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Systeminfo / SystemInfoLocale.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <PlatformException.h>
19 #include <Logger.h>
20 #include <system_settings.h>
21
22 #include "SystemInfoLocale.h"
23 #include "SystemInfoUtil.h"
24
25 namespace DeviceAPI {
26 namespace SystemInfo {
27
28 SystemInfoLocale::SystemInfoLocale()
29 {
30     LOGD("Entered");
31
32     m_language = fetchLanguage();
33     m_country = fetchCountry();
34 }
35
36 SystemInfoLocale::~SystemInfoLocale()
37 {
38     LOGD("Entered");
39 }
40
41 std::string SystemInfoLocale::getLanguage() const
42 {
43     return m_language;
44 }
45
46 std::string SystemInfoLocale::getCountry() const
47 {
48     return m_country;
49 }
50
51 std::string SystemInfoLocale::fetchLanguage()
52 {
53     LOGD("Entered");
54
55     char* language = NULL;
56     std::string str_language = "";
57     std::string log_msg = "";
58     int ret = 0;
59
60     ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &language);
61     if (ret == SYSTEM_SETTINGS_ERROR_NONE) {
62         if (NULL != language) {
63             LOGD("language : %s", language);
64             str_language = language;
65             free(language);
66         }
67     } else {
68         log_msg = "Failed to get language info";
69         LOGE("%s, %d, %s", log_msg.c_str(), ret,
70                 SystemInfoUtil::getSettingErrorMessage(ret).c_str());
71         SystemInfoUtil::throwSettingException(ret, log_msg);
72     }
73     return str_language;
74 }
75
76 std::string SystemInfoLocale::fetchCountry()
77 {
78     LOGD("Entered");
79
80     char* country = NULL;
81     std::string str_country = "";
82     std::string log_msg = "";
83     int ret = 0;
84
85     ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_COUNTRY, &country);
86     if (ret == SYSTEM_SETTINGS_ERROR_NONE) {
87         if (NULL != country) {
88             LOGD("country : %s", country);
89             char* token = NULL;
90             token = strtok(country, ".");
91             if (NULL != token) {
92                 str_country = token;
93             }
94             free(country);
95         }
96     } else {
97         log_msg = "Failed to get country info";
98         LOGE("%s, %d, %s", log_msg.c_str(), ret,
99                 SystemInfoUtil::getSettingErrorMessage(ret).c_str());
100         SystemInfoUtil::throwSettingException(ret, log_msg);
101     }
102     return str_country;
103 }
104
105 } // SystemInfo
106 } // DeviceAPI