Apply string localization for web
[platform/framework/native/appfw.git] / src / server / system / setting / providers / FSys_SettingFontProvider.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file        FSys_SettingFontProvider.cpp
19  * @brief       This is the implementation for the _SettingFontProvider class.
20  */
21
22 #include <unique_ptr.h>
23
24 #include <FApp.h>
25 #include <FBase.h>
26 #include <FBaseSysLog.h>
27
28 #include <FBase_StringConverter.h>
29
30 #include "FSys_SettingInfo.h"
31 #include "FSys_SettingFontProvider.h"
32
33 using namespace std;
34
35 using namespace Tizen::App;
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Utility;
38
39 namespace Tizen { namespace System
40 {
41
42 static const wchar_t* _FONT_SIZE = L"http://tizen.org/setting/font.size";
43 static const wchar_t* _FONT_TYPE = L"http://tizen.org/setting/font.type";
44
45 static const wchar_t* _FONT_SIZE_GIANT = L"giant";
46 static const wchar_t* _FONT_SIZE_HUGE = L"huge";
47 static const wchar_t* _FONT_SIZE_LARGE = L"large";
48 static const wchar_t* _FONT_SIZE_MEDIUM = L"medium";
49 static const wchar_t* _FONT_SIZE_SMALL = L"small";
50
51 static const int _FONT_SIZE_GIANT_VCONF = 4;
52 static const int _FONT_SIZE_HUGE_VCONF = 3;
53 static const int _FONT_SIZE_LARGE_VCONF = 2;
54 static const int _FONT_SIZE_MEDIUM_VCONF = 1;
55 static const int _FONT_SIZE_SMALL_VCONF = 0;
56
57 struct charDeleter
58 {
59         void operator()(char* pValue)
60         {
61                 if(pValue != null)
62                 {
63                         free(pValue);
64                         pValue = null;
65                 }
66         }
67 };
68
69 bool
70 _SettingFontProvider::HasKey(const Tizen::Base::String& key)
71 {
72         if(key == _FONT_SIZE || key == _FONT_TYPE)
73         {
74                 return true;
75         }
76
77         return false;
78 }
79
80 _SettingFontProvider::_SettingFontProvider()
81 {
82         int errorCode = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_FONT_TYPE, SettingEventSettingInfo, null);
83         if(errorCode != 0)
84         {
85                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register font type event listener");
86         }
87
88         errorCode = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_FONT_SIZE, SettingEventSettingInfo, null);
89         if(errorCode != 0)
90         {
91                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register font size event listener");
92         }
93 }
94
95
96 _SettingFontProvider::~_SettingFontProvider()
97 {
98         int errorCode = system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_FONT_SIZE);
99         if(errorCode != 0)
100         {
101                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister font size event listener");
102         }
103
104         errorCode = system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_FONT_TYPE);
105         if(errorCode != 0)
106         {
107                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister font type event listener");
108         }
109 }
110
111 result
112 _SettingFontProvider::GetValue(const String& key, String& value)
113 {
114         SysTryReturnResult(NID_SYS, 0, E_SYSTEM, "[E_SYSTEM] It is not working.");
115         result r = E_OBJ_NOT_FOUND;
116
117         if(key == _FONT_SIZE)
118         {
119                 int fontSize = 0;
120                 int res = 0;
121                 res = system_settings_get_value_int(SYSTEM_SETTINGS_KEY_FONT_SIZE, &fontSize);
122                 SysTryReturnResult(NID_SYS, res == SYSTEM_SETTINGS_ERROR_NONE, E_SYSTEM, "It is failed to get font size.");
123                 SysLog(NID_SYS, "current font size is %d.", fontSize);
124
125                 r = E_SUCCESS;
126                 switch (fontSize)
127                 {
128                 case _FONT_SIZE_GIANT_VCONF:
129                         value.Append(_FONT_SIZE_GIANT);
130                         break;
131                 case _FONT_SIZE_HUGE_VCONF:
132                         value.Append(_FONT_SIZE_HUGE);
133                         break;
134                 case _FONT_SIZE_LARGE_VCONF:
135                         value.Append(_FONT_SIZE_LARGE);
136                         break;
137                 case _FONT_SIZE_MEDIUM_VCONF:
138                         value.Append(_FONT_SIZE_MEDIUM);
139                         break;
140                 case _FONT_SIZE_SMALL_VCONF:
141                         value.Append(_FONT_SIZE_SMALL);
142                         break;
143                 default:
144                         r = E_SYSTEM;
145                         break;
146                 }
147
148         }
149         else if(key == _FONT_TYPE)
150         {
151                 r = E_SUCCESS;
152                 char* pTemp = null;
153                 int res = 0;
154                 res = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_FONT_TYPE, &pTemp);
155                 SysTryReturnResult(NID_SYS, pTemp != null, E_SYSTEM, "It is failed to get font type.");
156
157                 unique_ptr<char, charDeleter> pFontType(pTemp);
158                 SysTryReturnResult(NID_SYS, res == SYSTEM_SETTINGS_ERROR_NONE, E_SYSTEM, "It is failed to get font type.");
159                 SysLog(NID_SYS, "current font type is %s.", pTemp);
160                 r = StringUtil::Utf8ToString(pFontType.get(), value);
161                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to convert font type to Utf8.");
162         }
163
164         return r;
165 }
166
167 result
168 _SettingFontProvider::SetValue(const String& key, const String value)
169 {
170         SysTryReturnResult(NID_SYS, 0, E_SYSTEM, "[E_SYSTEM] It is not working.");
171         result r = E_OBJ_NOT_FOUND;
172
173         if(key == _FONT_SIZE)
174         {
175                 int fontSize = 0;
176                 int res = 0;
177                 String lowerValue = value;
178                 lowerValue.ToLowerCase();
179                 r = E_SUCCESS;
180
181                 if (lowerValue == _FONT_SIZE_GIANT)
182                 {
183                         fontSize = _FONT_SIZE_GIANT_VCONF;
184                 }
185                 else if (lowerValue == _FONT_SIZE_HUGE)
186                 {
187                         fontSize = _FONT_SIZE_HUGE_VCONF;
188                 }
189                 else if (lowerValue == _FONT_SIZE_LARGE)
190                 {
191                         fontSize = _FONT_SIZE_LARGE_VCONF;
192                 }
193                 else if (lowerValue == _FONT_SIZE_MEDIUM)
194                 {
195                         fontSize = _FONT_SIZE_MEDIUM_VCONF;
196                 }
197                 else if (lowerValue == _FONT_SIZE_SMALL)
198                 {
199                         fontSize = _FONT_SIZE_SMALL_VCONF;
200                 }
201                 else
202                 {
203                         return E_INVALID_ARG;
204                 }
205                 res = system_settings_set_value_int(SYSTEM_SETTINGS_KEY_FONT_SIZE, fontSize);
206                 SysTryReturnResult(NID_SYS, res == SYSTEM_SETTINGS_ERROR_NONE, E_SYSTEM, "It is failed to set font size.");
207         }
208         else if(key == _FONT_TYPE)
209         {
210                 int res = 0;
211                 r = E_SUCCESS;
212
213                 unique_ptr<char []> pFontType(_StringConverter::CopyToCharArrayN(value));
214                 SysTryReturnResult(NID_SYS, pFontType.get() != null, E_SYSTEM, "It is failed to convert String to string.");
215                 SysLog(NID_SYS, "Requested font type is %s.", pFontType.get());
216                 res = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_FONT_TYPE, pFontType.get());
217                 if(res == SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER)
218                 {
219                         return E_INVALID_ARG;
220                 }
221                 SysTryReturnResult(NID_SYS, res == SYSTEM_SETTINGS_ERROR_NONE, E_SYSTEM, "It is failed to set font type.");
222         }
223         return r;
224 }
225
226 void
227 _SettingFontProvider::SettingEventSettingInfo(system_settings_key_e key, void* userData)
228 {
229         _SettingInfo* pSettingInfo = _SettingInfo::GetInstance();
230         SysTryReturnVoidResult(NID_SYS, pSettingInfo != null, E_SYSTEM, "_SettingInfo is not ready.");
231
232         String settingKey;
233
234         switch (key)
235         {
236         case SYSTEM_SETTINGS_KEY_FONT_SIZE:
237         {
238                 settingKey.Append(_FONT_SIZE);
239                 break;
240         }
241         case SYSTEM_SETTINGS_KEY_FONT_TYPE:
242         {
243                 settingKey.Append(_FONT_TYPE);
244                 break;
245         }
246         default:
247         {
248                 SysLogException(NID_SYS, E_SYSTEM, "No required key[%d] is delivered", key);
249                 return;
250         }
251         }
252
253         result r = pSettingInfo->AnnounceSettingEvent(settingKey);
254         SysTryReturnVoidResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send the event[%ls].", settingKey.GetPointer());
255 }
256
257 }}