Add code versioning for "SettingInfo TC"
[platform/framework/native/appfw.git] / src / system / FSys_SettingInfoImpl.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_SettingInfoImpl.cpp
19  * @brief               This is the implementation file for _SysSettingInfoImpl class.
20  */
21 #include <unique_ptr.h>
22 #include <system/system_settings.h>
23
24 #include <FBaseColArrayList.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseUtilStringUtil.h>
27 #include <FIoFile.h>
28
29 #include <FIo_FileImpl.h>
30 #include <FApp_AppInfo.h>
31 #include <FBase_StringConverter.h>
32 #include <FBase_NativeError.h>
33 #include <FSys_SettingInfoImpl.h>
34
35 #include "FSys_SettingClient.h"
36
37 using namespace std;
38
39 using namespace Tizen::App;
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Io;
43 using namespace Tizen::Base::Utility;
44
45 namespace Tizen { namespace System
46 {
47 //Reserved key
48 static const wchar_t* _SETTING_SCREEN_WALLPAPER = L"http://tizen.org/setting/screen.wallpaper";
49 static const wchar_t* _SETTING_SYSTEM_SOUND_VOLUME = L"SystemSoundVolume";
50 static const wchar_t* _SETTING_SOUND_SYSTEM_VOLUME= L"http://tizen.org/setting/sound.system.volume";
51 static const wchar_t* _SETTING_MEDIA_SOUND_VOLUME = L"MediaSoundVolume";
52 static const wchar_t* _SETTING_SOUND_MEDIA_VOLUME = L"http://tizen.org/setting/sound.media.volume";
53 static const wchar_t* _SETTING_RINGTONE_SOUND_VOLUME = L"RingtoneSoundVolume";
54 static const wchar_t* _SETTING_SOUND_RINGTONE = L"http://tizen.org/setting/sound.ringtone";
55 static const wchar_t* _SETTING_SOUND_RINGTONE_VOLUME = L"http://tizen.org/setting/sound.ringtone.volume";
56 static const wchar_t* _SETTING_NOTIFICATION_SOUND_VOLUME = L"NotificationSoundVolume";
57 static const wchar_t* _SETTING_SOUND_NOTIFICATION_VOLUME = L"http://tizen.org/setting/sound.notification.volume";
58
59 //Font
60 static const wchar_t* _FONT_SIZE = L"http://tizen.org/setting/font.size";
61 static const wchar_t* _FONTSIZE = L"FontSize";
62 static const wchar_t* _FONT_TYPE = L"http://tizen.org/setting/font.type";
63 static const wchar_t* _FONTTYPE = L"FontType";
64
65 static const wchar_t* _FONT_SIZE_GIANT = L"giant";
66 static const wchar_t* _FONT_SIZE_HUGE = L"huge";
67 static const wchar_t* _FONT_SIZE_LARGE = L"large";
68 static const wchar_t* _FONT_SIZE_MEDIUM = L"medium";
69 static const wchar_t* _FONT_SIZE_SMALL = L"small";
70
71 static const int _FONT_SIZE_GIANT_VCONF = 4;
72 static const int _FONT_SIZE_HUGE_VCONF = 3;
73 static const int _FONT_SIZE_LARGE_VCONF = 2;
74 static const int _FONT_SIZE_MEDIUM_VCONF = 1;
75 static const int _FONT_SIZE_SMALL_VCONF = 0;
76
77
78 static _SettingClient* pSettingClient = null;
79
80 void
81 _SettingInfoImpl::InitSettingClient(void)
82 {
83         if(pSettingClient == null)
84         {
85                 pSettingClient =  _SettingClient::GetInstance();
86         }
87 }
88 result
89 _SettingInfoImpl::GetValue(const Tizen::Base::String& key, Tizen::Base::String& value)
90 {
91         result r = E_SUCCESS;
92
93         if (key == _FONT_SIZE || key == _FONT_TYPE || key == _FONTSIZE || key == _FONTTYPE)
94         {
95                 String tizenKey(key);
96                 if(tizenKey == _FONTSIZE)
97                 {
98                         tizenKey = _FONT_SIZE;
99                 }
100                 else if(tizenKey == _FONTTYPE)
101                 {
102                         tizenKey = _FONT_TYPE;
103                 }
104
105                 r = GetValueForFont(tizenKey, value);
106
107                 if(key == _FONTSIZE)
108                 {
109                         if(_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
110                         {
111                                 if(value ==  _FONT_SIZE_GIANT || value == _FONT_SIZE_HUGE)
112                                 {
113                                         value = _FONT_SIZE_LARGE; //OSP not support giant or huge size of font.
114                                 }
115                         }
116                 }
117         }
118         else
119         {
120                 InitSettingClient();
121                 SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
122                 r = pSettingClient->GetValue(key, value);
123
124                 if(key == L"Wallpaper")
125                 {
126                         if(_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
127                         {
128                                 String physicalFilePath = value;
129                                 r = Tizen::Io::_FileImpl::ConvertPhysicalToVirtualPath(physicalFilePath, value);
130                                 SysTryReturn(NID_SYS, r == E_SUCCESS, r, r, "[%s] It is failed to converting virtual path", GetErrorMessage(r));
131                         }
132                 }
133         }
134         return r;
135 }
136
137 result
138 _SettingInfoImpl::GetValue(const Tizen::Base::String& key, int& value)
139 {
140         InitSettingClient();
141         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
142         return pSettingClient->GetValue(key, value);
143 }
144
145 result
146 _SettingInfoImpl::GetValue(const Tizen::Base::String& key, long long& value)
147 {
148         InitSettingClient();
149         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
150         return pSettingClient->GetValue(key, value);
151 }
152
153 result
154 _SettingInfoImpl::GetValue(const Tizen::Base::String& key, double& value)
155 {
156         InitSettingClient();
157         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
158         return pSettingClient->GetValue(key, value);
159 }
160
161 result
162 _SettingInfoImpl::GetValue(const Tizen::Base::String& key, bool& value)
163 {
164         InitSettingClient();
165         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
166         return pSettingClient->GetValue(key, value);
167 }
168
169 result
170 _SettingInfoImpl::GetValue(const Tizen::Base::String& key, Tizen::Base::UuId& value)
171 {
172         InitSettingClient();
173         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
174         return pSettingClient->GetValue(key, value);
175 }
176
177 result
178 _SettingInfoImpl::SetWallpaper(const Tizen::Base::String& filePath)
179 {
180         InitSettingClient();
181         result r = E_SUCCESS;
182
183         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
184
185         String physicalFilePath = filePath;
186
187         if(Tizen::App::_AppInfo::GetApiVersion() == _API_VERSION_2_0 && Tizen::App::_AppInfo::IsOspCompat())
188         {
189                 r = Tizen::Io::_FileImpl::ConvertVirtualToPhysicalPath(filePath, physicalFilePath);
190                 SysTryReturn(NID_SYS, r == E_SUCCESS, r, r, "[%s] It is failed to converting physical path", GetErrorMessage(r));
191         }
192
193         r = pSettingClient->SetValue(_SETTING_SCREEN_WALLPAPER, physicalFilePath);
194
195         if(r == E_INVALID_ARG)
196                 r = E_FILE_NOT_FOUND;
197         else if(r == E_OBJ_NOT_FOUND)
198                 r = E_SYSTEM;
199         else if(r == E_UNSUPPORTED_OPERATION)
200                 r = E_SYSTEM;
201
202         return r;
203 }
204
205 result
206 _SettingInfoImpl::GetValueForPrivilegedKey(const String& key, bool& value)
207 {
208         InitSettingClient();
209         
210         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
211         return pSettingClient->GetValueForPrivilegedKey(key, value);
212 }
213
214 result
215 _SettingInfoImpl::SetValue(const String& key, bool value)
216 {
217         InitSettingClient();
218         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
219         return pSettingClient->SetValue(key, value);
220 }
221
222 result
223 _SettingInfoImpl::SetValue(const String& key, int value)
224 {
225         InitSettingClient();
226         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
227         return pSettingClient->SetValue(key, value);
228 }
229
230
231 result
232 _SettingInfoImpl::SetValue(const String& key, String value)
233 {
234         result r = E_SUCCESS;
235         if (key == _FONT_SIZE || key == _FONT_TYPE)
236         {
237                 r = SetValueForFont(key, value);
238         }
239         else
240         {
241                 InitSettingClient();
242                 SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
243
244                 if(key == _SETTING_SCREEN_WALLPAPER)
245                 {
246                         if(_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
247                         {
248                                 String virtualFilePath = value;
249                                 r = Tizen::Io::_FileImpl::ConvertVirtualToPhysicalPath(virtualFilePath, value);
250                                 SysTryReturn(NID_SYS, r == E_SUCCESS, r, r, "[%s] It is failed to converting physical path", GetErrorMessage(r));
251                         }
252                 }
253                 return pSettingClient->SetValue(key, value);
254         }
255         return r;
256 }
257
258 result
259 _SettingInfoImpl::SetValueForPrivilegedKey(const String& key, bool value)
260 {
261         InitSettingClient();
262         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
263         return pSettingClient->SetValueForPrivilegedKey(key, value);
264 }
265
266 result
267 _SettingInfoImpl::SetValueForPrivilegedKey(const String& key, String value)
268 {
269         InitSettingClient();
270         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
271         return pSettingClient->SetValueForPrivilegedKey(key, value);
272 }
273
274 bool
275 _SettingInfoImpl::HasKey(const String& key)
276 {
277         InitSettingClient();
278         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
279         return pSettingClient->HasKey(key);
280 }
281 result
282 _SettingInfoImpl::ResetToFactoryDefault(void)
283 {
284         InitSettingClient();
285         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
286         return pSettingClient->ResetToFactoryDefault();
287 }
288
289 result
290 _SettingInfoImpl::SetRingtone(const Tizen::Base::String& filePath)
291 {
292         result r = E_SUCCESS;
293         InitSettingClient();
294         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
295         r = pSettingClient->SetValue(_SETTING_SOUND_RINGTONE, filePath);
296
297         if(r == E_INVALID_ARG)
298                 r = E_FILE_NOT_FOUND;
299         else if(r == E_OBJ_NOT_FOUND)
300                 r = E_SYSTEM;
301         else if(r == E_UNSUPPORTED_OPERATION)
302                 r = E_SYSTEM;
303
304         return r;
305 }
306
307 result
308 _SettingInfoImpl::SetVolume(const Tizen::Base::String& soundCategory, int level)
309 {
310         result r = E_SUCCESS;
311         String key;
312
313         if (soundCategory == _SETTING_SYSTEM_SOUND_VOLUME)
314         {
315                 key = _SETTING_SOUND_SYSTEM_VOLUME;
316         }
317         else if (soundCategory == _SETTING_MEDIA_SOUND_VOLUME)
318         {
319                 key = _SETTING_SOUND_MEDIA_VOLUME;
320         }
321         else if (soundCategory == _SETTING_RINGTONE_SOUND_VOLUME)
322         {
323                 key = _SETTING_SOUND_RINGTONE_VOLUME;
324         }
325         else if (soundCategory == _SETTING_NOTIFICATION_SOUND_VOLUME)
326         {
327                 key = _SETTING_SOUND_NOTIFICATION_VOLUME;
328         }
329         else
330         {
331                 key = soundCategory;
332         }
333
334         InitSettingClient();
335         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
336         r = pSettingClient->SetValue(key, level);
337
338         if (r == E_OBJ_NOT_FOUND)
339         {
340                 r = E_INVALID_ARG;
341         }
342         else if(r == E_INVALID_ARG)
343         {
344                 r = E_OUT_OF_RANGE;
345         }
346         else if(r == E_UNSUPPORTED_OPERATION)
347         {
348                 r = E_SYSTEM;
349         }
350         return r;
351 }
352
353 result
354 _SettingInfoImpl::AddSettingEventListener(ISettingEventListener& listener)
355 {
356         InitSettingClient();
357         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
358         return pSettingClient->AddSettingEventListener(listener);
359 }
360
361 result
362 _SettingInfoImpl::RemoveSettingEventListener(ISettingEventListener& listener)
363 {
364         InitSettingClient();
365         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
366         return pSettingClient->RemoveSettingEventListener(listener);
367 }
368
369 result
370 _SettingInfoImpl::AddSettingEventListenerForInternal(ISettingEventListener& listener)
371 {
372         InitSettingClient();
373         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
374         return pSettingClient->AddSettingEventListenerForInternal(listener);
375 }
376
377 result
378 _SettingInfoImpl::RemoveSettingEventListenerForInternal(ISettingEventListener& listener)
379 {
380         InitSettingClient();
381         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
382         return pSettingClient->RemoveSettingEventListenerForInternal(listener);
383 }
384
385 result
386 _SettingInfoImpl::SetSettingEventListener(ISettingEventListener* pListener)
387 {
388         InitSettingClient();
389         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
390         return pSettingClient->SetSettingEventListener(pListener);
391 }
392
393 result
394 _SettingInfoImpl::SetValueAsync(const Tizen::Base::String& key, bool value, ISettingInfoSetValueAsyncResultListener* listener)
395 {
396         InitSettingClient();
397         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
398         return pSettingClient->SetValueAsync(key, value, listener);
399 }
400
401 result
402 _SettingInfoImpl::SetValueAsyncForPrivilegedKey(const Tizen::Base::String& key, bool value, ISettingInfoSetValueAsyncResultListener* listener)
403 {
404         InitSettingClient();
405         SysTryReturnResult(NID_SYS, pSettingClient != null, E_SYSTEM, "It is failed to intialize setting manager");
406         return pSettingClient->SetValueAsyncForPrivilegedKey(key, value, listener);
407 }
408
409 _SettingInfoImpl*
410 _SettingInfoImpl::GetInstance(SettingInfo& settinginfo)
411 {
412         return settinginfo.__pSettingInfoImpl;
413 }
414
415 const _SettingInfoImpl*
416 _SettingInfoImpl::GetInstance(const SettingInfo& settinginfo)
417 {
418         return settinginfo.__pSettingInfoImpl;
419 }
420
421 result
422 _SettingInfoImpl::GetValueForFont(const Tizen::Base::String& key, Tizen::Base::String& value)
423 {
424         result r = E_OBJ_NOT_FOUND;
425
426         if (key == _FONT_SIZE)
427         {
428                 int fontSize = 0;
429                 int res = 0;
430                 res = system_settings_get_value_int(SYSTEM_SETTINGS_KEY_FONT_SIZE, &fontSize);
431
432                 SysTryReturnResult(NID_SYS, res == SYSTEM_SETTINGS_ERROR_NONE, E_SYSTEM, "It is failed to get font size.");
433
434                 r = E_SUCCESS;
435                 switch (fontSize)
436                 {
437                 case _FONT_SIZE_GIANT_VCONF:
438                         value.Append(_FONT_SIZE_GIANT);
439                         break;
440                 case _FONT_SIZE_HUGE_VCONF:
441                         value.Append(_FONT_SIZE_HUGE);
442                         break;
443                 case _FONT_SIZE_LARGE_VCONF:
444                         value.Append(_FONT_SIZE_LARGE);
445                         break;
446                 case _FONT_SIZE_MEDIUM_VCONF:
447                         value.Append(_FONT_SIZE_MEDIUM);
448                         break;
449                 case _FONT_SIZE_SMALL_VCONF:
450                         value.Append(_FONT_SIZE_SMALL);
451                         break;
452                 default:
453                         r = E_SYSTEM;
454                         break;
455                 }
456
457         }
458         else if (key == _FONT_TYPE)
459         {
460                 r = E_SUCCESS;
461                 char* pFontType = null;
462                 int res = 0;
463                 res = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_FONT_TYPE, &pFontType);
464                 SysTryReturnResult(NID_SYS, res == SYSTEM_SETTINGS_ERROR_NONE, E_SYSTEM, "Failed to get font type");
465                 r = StringUtil::Utf8ToString(pFontType, value);
466                 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] StringUtil::Utf8ToString failed", GetErrorMessage(r));
467                 free(pFontType);
468         }
469         return r;
470 }
471
472
473 result
474 _SettingInfoImpl::SetValueForFont(const Tizen::Base::String& key, Tizen::Base::String value)
475 {
476         result r = E_OBJ_NOT_FOUND;
477
478         if(key == _FONT_SIZE)
479         {
480                 int fontSize = 0;
481                 int res = 0;
482                 String lowerValue = value;
483                 lowerValue.ToLowerCase();
484                 r = E_SUCCESS;
485
486                 if (lowerValue == _FONT_SIZE_GIANT)
487                 {
488                         fontSize = _FONT_SIZE_GIANT_VCONF;
489                 }
490                 else if (lowerValue == _FONT_SIZE_HUGE)
491                 {
492                         fontSize = _FONT_SIZE_HUGE_VCONF;
493                 }
494                 else if (lowerValue == _FONT_SIZE_LARGE)
495                 {
496                         fontSize = _FONT_SIZE_LARGE_VCONF;
497                 }
498                 else if (lowerValue == _FONT_SIZE_MEDIUM)
499                 {
500                         fontSize = _FONT_SIZE_MEDIUM_VCONF;
501                 }
502                 else if (lowerValue == _FONT_SIZE_SMALL)
503                 {
504                         fontSize = _FONT_SIZE_SMALL_VCONF;
505                 }
506                 else
507                 {
508                         return E_INVALID_ARG;
509                 }
510                 res = system_settings_set_value_int(SYSTEM_SETTINGS_KEY_FONT_SIZE, fontSize);
511                 SysTryReturnResult(NID_SYS, res == SYSTEM_SETTINGS_ERROR_NONE, E_SYSTEM, "It is failed to set font size.");
512         }
513         else if(key == _FONT_TYPE)
514         {
515                 int res = 0;
516                 r = E_SUCCESS;
517
518                 unique_ptr<char[]> pFontType(_StringConverter::CopyToCharArrayN(value));
519                 SysTryReturnResult(NID_SYS, pFontType.get() != null, E_SYSTEM, "It is failed to convert String to string.");
520                 SysLog(NID_SYS, "Requested font type is %s.", pFontType.get());
521                 res = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_FONT_TYPE, pFontType.get());
522                 SysTryReturnResult(NID_SYS, res != SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "It is not valid font type.");
523                 SysTryReturnResult(NID_SYS, res == SYSTEM_SETTINGS_ERROR_NONE, E_SYSTEM, "It is failed to set font type.");
524         }
525         return r;
526 }
527
528
529 } } // Tizen::System