Update system
[platform/framework/native/appfw.git] / src / system / FSys_SettingClient.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 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 /**
19  * @file                FSys_SettingClient.cpp
20  * @brief               This is the implementation file for _SettingClient class.
21  */
22
23 #include <unique_ptr.h>
24
25 #include "FSys_SettingClient.h"
26
27 #include <FBaseSysLog.h>
28
29 #include <FApp_AppInfo.h>
30 #include <FIo_AppServiceIpcMessages.h>
31 #include "FSys_CommunicationDispatcherClient.h"
32
33 using namespace std;
34
35 using namespace Tizen::App;
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Base::Runtime;
39 using namespace Tizen::Io;
40
41 namespace Tizen { namespace System
42 {
43 //MOTION
44 static const wchar_t* _MOTION_UI = L"http://tizen.org/setting/motion.ui";
45 static const wchar_t* _ENABLE_MOTION = L"EnableMotion";
46
47 //Font
48 static const wchar_t* _FONT_SIZE = L"http://tizen.org/setting/font.size";
49 static const wchar_t* _FONTSIZE = L"FontSize";
50
51 static const wchar_t* _FONT_TYPE = L"http://tizen.org/setting/font.type";
52 static const wchar_t* _FONTTYPE = L"FontType";
53
54
55 //Locale
56 //Locale Country
57 static const wchar_t* _LOCALE_COUNTRY = L"http://tizen.org/setting/locale.country";
58 static const wchar_t* _COUNTRY = L"Country";
59
60 //Locale Format
61 static const wchar_t* _LOCALE_DATE_FORMAT = L"http://tizen.org/setting/locale.date.format";
62 static const wchar_t* _DATE_FORMAT = L"DateFormat";
63
64 static const wchar_t* _LOCALE_TIME_FORMAT = L"http://tizen.org/setting/locale.time.format";
65 static const wchar_t* _TIME_FORMAT = L"TimeFormat";
66
67 static const wchar_t* _LOCALE_TIME_FORMAT_24HOUR = L"http://tizen.org/setting/locale.time.format.24hour";
68 static const wchar_t* _HOUR_FORMAT_SELECTED = L"24HourFormatSelected";
69
70 //Locale Language
71 static const wchar_t* _LOCALE_LANGUAGE = L"http://tizen.org/setting/locale.language";
72 static const wchar_t* _LANGUAGE = L"Language";
73
74 //Locale Time
75 static const wchar_t* _LOCALE_DATETIME_FORMAT = L"http://tizen.org/setting/locale.date_time.format";
76 static const wchar_t* _DATE_TIME_FORMAT = L"DateTimeFormat";
77
78
79 //Location
80 static const wchar_t* _LOCATION_GPS = L"http://tizen.org/setting/location.gps";
81 static const wchar_t* _GPS_ENABLED = L"GPSEnabled";
82
83 static const wchar_t* _LOCATION_WPS = L"http://tizen.org/setting/location.wps";
84 static const wchar_t* _WPS_ENABLED = L"WPSEnabled";
85
86 //Network
87 static const wchar_t* _NETWORK_FLIGHTMODE = L"http://tizen.org/setting/network.flight_mode";
88 static const wchar_t* _FLIGHT_MODE_ENABLED = L"FlightModeEnabled";
89
90 //Network Telephony
91 static const wchar_t* _NETWORK_TELEPHONY_PACKETSERVICE = L"http://tizen.org/setting/network.telephony.packet_service";
92 static const wchar_t* _PACKET_SERVICE_ALLOWED = L"PacketServiceAllowed";
93
94 static const wchar_t* _NETWORK_TELEPHONY_ROAMING = L"http://tizen.org/setting/network.telephony.roaming";
95 static const wchar_t* _DATA_ROAMING_ENABLED = L"DataRoamingEnabled";
96
97 //Screen
98 static const wchar_t* _SCREEN_WALLPAPER = L"http://tizen.org/setting/screen.wallpaper";
99 static const wchar_t* _WALLPAPER = L"Wallpaper";
100
101 //Sound
102 static const wchar_t* _SOUND_MEDIA_VOLUME = L"http://tizen.org/setting/sound.media.volume";
103 static const wchar_t* _MEDIA_SOUND_VOLUME = L"MediaSoundVolume";
104
105 static const wchar_t* _SOUND_NOTIFICATION_VOLUME = L"http://tizen.org/setting/sound.notification.volume";
106 static const wchar_t* _NOTIFICATION_SOUND_VOLUME = L"NotificationSoundVolume";
107
108 static const wchar_t* _SOUND_RINGTONE = L"http://tizen.org/setting/sound.ringtone";
109 static const wchar_t* _RINGTONE = L"Ringtone";
110
111 static const wchar_t* _SOUND_RINGTONE_VOLUME = L"http://tizen.org/setting/sound.ringtone.volume";
112 static const wchar_t* _RINGTONE_SOUND_VOLUME = L"RingtoneSoundVolume";
113
114 static const wchar_t* _SOUND_SYSTEM_VOLUME = L"http://tizen.org/setting/sound.system.volume";
115 static const wchar_t* _SYSTEM_SOUND_VOLUME = L"SystemSoundVolume";
116
117 static const wchar_t* _SOUND_SILENTMODE = L"http://tizen.org/setting/sound.silent_mode";
118 static const wchar_t* _SILENT_MODE = L"SilentMode";
119
120 //Vibration
121 static const wchar_t* _TOUCH_VIBRATION_LEVEL = L"TouchVibrationLevel";
122 static const wchar_t* _VIBRATOR_LEVEL = L"http://tizen.org/setting/vibrator.level";
123
124 static const wchar_t* _SETTING_COMMAND_FACTORY_RESET = L"osp.setting.command.factory.reset";
125
126 //USB
127 static const wchar_t* _USBMODE = L"UsbMode";
128 static const wchar_t* _USBMODE_MTP = L"MTP";
129
130 static const wchar_t* _SETTING_SERVICE_ID = L"osp.setting.service";
131 static const wchar_t* _SETTING_SERVICE_TYPE_EVENT = L"osp.setting.service.type.event";
132
133 static const wchar_t* _SETTING_COMMAND_GET_BOOL = L"osp.setting.command.get.bool";
134 static const wchar_t* _SETTING_COMMAND_SET_BOOL = L"osp.setting.command.set.bool";
135 static const wchar_t* _SETTING_COMMAND_SET_BOOL_ASYNC = L"osp.setting.command.set.bool.async";
136 static const wchar_t* _SETTING_COMMAND_GET_BOOL_PRIV = L"osp.setting.command.get.bool.priv";
137 static const wchar_t* _SETTING_COMMAND_SET_BOOL_PRIV = L"osp.setting.command.set.bool.priv";
138 static const wchar_t* _SETTING_COMMAND_SET_BOOL_ASYNC_PRIV = L"osp.setting.command.set.bool.async.priv";
139
140 static const wchar_t* _SETTING_COMMAND_GET_INT = L"osp.setting.command.get.int";
141 static const wchar_t* _SETTING_COMMAND_SET_INT = L"osp.setting.command.set.int";
142
143 static const wchar_t* _SETTING_COMMAND_GET_STRING = L"osp.setting.command.get.string";
144 static const wchar_t* _SETTING_COMMAND_SET_STRING = L"osp.setting.command.set.string";
145 static const wchar_t* _SETTING_COMMAND_SET_STRING_PRIV = L"osp.setting.command.set.string.priv";
146 static const wchar_t* _SETTING_COMMAND_SET_STRING_ASYNC = L"osp.setting.command.set.string.async";
147
148 static const wchar_t* _SETTING_COMMAND_ADD_EVENT = L"osp.setting.command.add.event";
149 static const wchar_t* _SETTING_COMMAND_REMOVE_EVENT = L"osp.setting.command.remove.event";
150
151 static const wchar_t* _SETTING_COMMAND_SUPPORTED = L"osp.setting.command.supported";
152
153 static const wchar_t* _SETTING_RESULT_SUCCESS = L"osp.system.result.success";
154 static const wchar_t* _SETTING_RESULT_SYSTEM = L"osp.system.result.system";
155 static const wchar_t* _SETTING_RESULT_INVALID_ARG = L"osp.system.result.invalid_arg";
156 static const wchar_t* _SETTING_RESULT_PRIVILEGE_DENIED = L"osp.system.result.privilege_denied";
157 static const wchar_t* _SETTING_RESULT_UNSUPPORTED_OPERATION = L"osp.system.result.unsupported_operation";
158 static const wchar_t* _SETTING_RESULT_OBJ_NOT_FOUND = L"osp.system.result.obj_not_found";
159 static const wchar_t* _SETTING_RESULT_OBJ_ALREADY_EXIST = L"osp.system.result.obj_already_exist";
160 static const wchar_t* _SETTING_RESULT_IN_PROGRESS = L"osp.system.result.in_progress";
161
162 _SettingClient* _SettingClient::__pSettingClient = null;
163
164 class _SettingEventArg : public IEventArg
165 {
166 public:
167         String KeyName;
168 };
169
170 class _SettingEvent : public Event
171 {
172 public:
173         _SettingEvent()
174         : __count(0)
175         {
176         }
177
178         result AddSettingEventListener(ISettingEventListener& listener)
179         {
180                 result  r = AddListener(listener);
181                 SysTryReturnResult(NID_SYS, __count >= 0, E_SYSTEM, "Listenr count[%d] is error.", __count);
182
183                 if(r == E_SUCCESS)
184                 {
185                         __count++;
186                 }
187                 return r;
188         }
189
190         result RemoveSettingEventListener(ISettingEventListener& listener)
191         {
192                 result  r = RemoveListener(listener);
193                 SysTryReturnResult(NID_SYS, __count > 0, E_SYSTEM, "Listenr count[%d] is error.", __count);
194                 if(r == E_SUCCESS)
195                 {
196                         __count--;
197                 }
198                 return r;
199         }
200
201         int GetCount(void)
202         {
203                 return __count;
204         }
205
206 private:
207         int __count;
208 protected:
209         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
210         {
211                 ISettingEventListener* pListener = dynamic_cast<ISettingEventListener*> (&listener);
212                 const _SettingEventArg* pArg = dynamic_cast<const _SettingEventArg*>(&arg);
213
214                 if(pListener == null || pArg == null)
215                 {
216                         SysLogException(NID_SYS, E_SYSTEM, "It is failed to get listner or arguemnt");
217                         return;
218                 }
219
220                 String value =pArg->KeyName;
221                 pListener->OnSettingChanged(value);
222         }
223 };
224
225 class _SettingAsyncEventArg : public IEventArg
226 {
227 public:
228         String Key;
229         result errorCode;
230 };
231
232 class _SettingAsyncEvent : public Event
233 {
234 protected:
235         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
236         {
237                 ISettingInfoSetValueAsyncResultListener* pListener = dynamic_cast<ISettingInfoSetValueAsyncResultListener*> (&listener);
238                 const _SettingAsyncEventArg* pArg = dynamic_cast<const _SettingAsyncEventArg*>(&arg);
239
240                 if(pListener == null || pArg == null)
241                 {
242                         SysLogException(NID_SYS, E_SYSTEM, "It is failed to get listner or arguemnt");
243                         return;
244                 }
245
246                 result r = pArg->errorCode;
247                 pListener->OnResultReceivedForSetValueAsync(pArg->Key, r);
248                 RemoveListener(listener);
249         }
250 };
251
252 void
253 _SettingClient::InitSettingClient(void)
254 {
255         static _SettingClient settingManager;
256         __pSettingClient = &settingManager;
257 }
258
259 _SettingClient*
260 _SettingClient::GetInstance(void)
261 {
262         static pthread_once_t once_block = PTHREAD_ONCE_INIT;
263         if(__pSettingClient == null)
264         {
265                 pthread_once(&once_block, InitSettingClient);
266         }
267         return __pSettingClient;
268 }
269
270 _SettingClient::_SettingClient()
271         : __pSettingEvent(null)
272         , __pSettingEventForInternal(null)
273         , __subscribed(false)
274         , __pIpcClient(null)
275         , __pListener(null)
276 {
277         result r = E_SUCCESS;
278         static String SETTING_SERVICE_ID(_SETTING_SERVICE_ID);
279
280         __pSettingEvent = new (std::nothrow) _SettingEvent();
281
282         __pSettingEventForInternal = new (std::nothrow) _SettingEvent();
283
284         _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
285         SysTryCatch(NID_SYS, pCommunicationDispatcherClient != null, r = E_SYSTEM, r, "It is failed to get CommunicationDispatcherClient.");
286
287         r = pCommunicationDispatcherClient->RegisterCommunicationListener(SETTING_SERVICE_ID, *this);
288         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on CommunicationDispatcherClient.");
289
290         __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
291
292         __asyncEventList.Construct();
293 CATCH:
294         SetLastResult(r);
295 }
296
297 _SettingClient::~_SettingClient()
298 {
299         result r = E_SUCCESS;
300         String key(_SETTING_SERVICE_ID);
301         _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
302         SysTryCatch(NID_SYS, pCommunicationDispatcherClient != null, r = E_SYSTEM, r, "It is failed to get CommunicationDispatcherClient.");
303
304         r = pCommunicationDispatcherClient->UnregisterCommunicationListener(key);
305         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on CommunicationDispatcherClient.");
306
307         __pIpcClient = null;
308 CATCH:
309         SetLastResult(r);
310 }
311
312 result 
313 _SettingClient::ConvertCode(String code)
314 {
315         result r = E_SUCCESS;
316         if(code == _SETTING_RESULT_SUCCESS)
317         {
318                 r = E_SUCCESS;
319         }
320         else if(code == _SETTING_RESULT_INVALID_ARG)
321         {
322                 r = E_INVALID_ARG;
323         }
324         else if(code == _SETTING_RESULT_PRIVILEGE_DENIED)
325         {
326                 r = E_PRIVILEGE_DENIED;
327         }
328         else if(code == _SETTING_RESULT_UNSUPPORTED_OPERATION)
329         {
330                 r = E_UNSUPPORTED_OPERATION;
331         }
332         else if(code == _SETTING_RESULT_OBJ_NOT_FOUND)
333         {
334                 r = E_OBJ_NOT_FOUND;
335         }
336         else if(code == _SETTING_RESULT_OBJ_ALREADY_EXIST)
337         {
338                 r = E_OBJ_ALREADY_EXIST;
339         }
340         else if(code == _SETTING_RESULT_IN_PROGRESS)
341         {
342                 r = E_IN_PROGRESS;
343         }
344         else if(code == _SETTING_RESULT_SYSTEM)
345         {
346                 r = E_SYSTEM;
347         }
348         else
349         {
350                 r = E_SYSTEM;
351         }
352         return r;
353 }
354
355 String
356 _SettingClient::ConvertKeyOspToTizen(String ospKey)
357 {
358         String tizenKey = ospKey;
359
360         if(tizenKey == _HOUR_FORMAT_SELECTED)
361                 tizenKey = _LOCALE_TIME_FORMAT_24HOUR;
362
363         else if(tizenKey == _ENABLE_MOTION)
364                 tizenKey = _MOTION_UI;
365
366         else if(tizenKey == _DATA_ROAMING_ENABLED)
367                 tizenKey = _NETWORK_TELEPHONY_ROAMING;
368
369         else if(tizenKey == _GPS_ENABLED)
370                 tizenKey = _LOCATION_GPS;
371
372         else if(tizenKey == _PACKET_SERVICE_ALLOWED)
373                 tizenKey = _NETWORK_TELEPHONY_PACKETSERVICE;
374
375         else if(tizenKey == _SILENT_MODE)
376                 tizenKey = _SOUND_SILENTMODE;
377
378         else if(tizenKey == _WPS_ENABLED)
379                 tizenKey = _LOCATION_WPS;
380
381         else if(tizenKey == _FLIGHT_MODE_ENABLED)
382                 tizenKey = _NETWORK_FLIGHTMODE;
383
384         else if(tizenKey == _TOUCH_VIBRATION_LEVEL)
385                 tizenKey = _VIBRATOR_LEVEL;
386
387         else if(tizenKey == _MEDIA_SOUND_VOLUME)
388                 tizenKey = _SOUND_MEDIA_VOLUME;
389
390         else if(tizenKey == _NOTIFICATION_SOUND_VOLUME)
391                 tizenKey = _SOUND_NOTIFICATION_VOLUME;
392
393         else if(tizenKey == _RINGTONE_SOUND_VOLUME)
394                 tizenKey = _SOUND_RINGTONE_VOLUME;
395
396         else if(tizenKey == _SYSTEM_SOUND_VOLUME)
397                 tizenKey = _SOUND_SYSTEM_VOLUME;
398
399         else if(tizenKey == _COUNTRY)
400                 tizenKey = _LOCALE_COUNTRY;
401
402         else if(tizenKey == _DATE_FORMAT)
403                 tizenKey = _LOCALE_DATE_FORMAT;
404
405         else if(tizenKey == _DATE_TIME_FORMAT)
406                 tizenKey = _LOCALE_DATETIME_FORMAT;
407
408         else if(tizenKey == _FONTTYPE)
409                 tizenKey = _FONT_TYPE;
410
411         else if(tizenKey == _FONTSIZE)
412                 tizenKey = _FONT_SIZE;
413
414         else if(tizenKey == _RINGTONE)
415                 tizenKey = _SOUND_RINGTONE;
416
417         else if(tizenKey == _TIME_FORMAT)
418                 tizenKey = _LOCALE_TIME_FORMAT;
419
420         else if(tizenKey == _WALLPAPER)
421                 tizenKey = _SCREEN_WALLPAPER;
422
423         return tizenKey;
424 }
425
426 result
427 _SettingClient::ConvertKeyTizenToOsp(String tizenKey, String& ospKey)
428 {
429         result r = E_SUCCESS;
430         if(tizenKey == _LOCALE_TIME_FORMAT_24HOUR)
431         {
432                 ospKey = _HOUR_FORMAT_SELECTED;
433         }
434         else if(tizenKey == _MOTION_UI)
435         {
436                 ospKey = _ENABLE_MOTION;
437         }
438         else if(tizenKey == _NETWORK_TELEPHONY_ROAMING)
439         {
440                 ospKey = _DATA_ROAMING_ENABLED;
441         }
442         else if(tizenKey == _LOCATION_GPS)
443         {
444                 ospKey = _GPS_ENABLED;
445         }
446         else if(tizenKey == _NETWORK_TELEPHONY_PACKETSERVICE)
447         {
448                 ospKey = _PACKET_SERVICE_ALLOWED;
449         }
450         else if(tizenKey == _SOUND_SILENTMODE)
451         {
452                 ospKey = _SILENT_MODE;
453         }
454         else if(tizenKey == _LOCATION_WPS)
455         {
456                 ospKey = _WPS_ENABLED;
457         }
458         else if(tizenKey == _NETWORK_FLIGHTMODE)
459         {
460                 ospKey = _FLIGHT_MODE_ENABLED;
461         }
462         else if(tizenKey == _VIBRATOR_LEVEL)
463         {
464                 ospKey = _TOUCH_VIBRATION_LEVEL;
465         }
466         else if(tizenKey == _SOUND_MEDIA_VOLUME)
467         {
468                 ospKey = _MEDIA_SOUND_VOLUME;
469         }
470         else if(tizenKey == _SOUND_NOTIFICATION_VOLUME)
471         {
472                 ospKey = _NOTIFICATION_SOUND_VOLUME;
473         }
474         else if(tizenKey == _SOUND_RINGTONE_VOLUME)
475         {
476                 ospKey = _RINGTONE_SOUND_VOLUME;
477         }
478         else if(tizenKey == _SOUND_SYSTEM_VOLUME)
479         {
480                 ospKey = _SYSTEM_SOUND_VOLUME;
481         }
482         else if(tizenKey == _LOCALE_COUNTRY)
483         {
484                 ospKey = _COUNTRY;
485         }
486         else if(tizenKey == _LOCALE_DATE_FORMAT)
487         {
488                 ospKey = _DATE_FORMAT;
489         }
490         else if(tizenKey == _LOCALE_DATETIME_FORMAT)
491         {
492                 ospKey = _DATE_TIME_FORMAT;
493         }
494         else if(tizenKey == _FONT_TYPE)
495         {
496                 ospKey = _FONTTYPE;
497         }
498         else if(tizenKey == _FONT_SIZE)
499         {
500                 ospKey = _FONTSIZE;
501         }
502         else if(tizenKey == _SOUND_RINGTONE)
503         {
504                 ospKey = _RINGTONE;
505         }
506         else if(tizenKey == _LOCALE_TIME_FORMAT)
507         {
508                 ospKey = _TIME_FORMAT;
509         }
510         else if(tizenKey == _SCREEN_WALLPAPER)
511         {
512                 ospKey = _WALLPAPER;
513         }
514         else
515         {
516                 r = E_INVALID_ARG;
517         }
518
519         return r;
520 }
521
522 result
523 _SettingClient::GetOnService(String commandId, String key, String& response)
524 {
525         result r = E_SUCCESS;
526         String requestKey = ConvertKeyOspToTizen(key);
527
528         ArrayList requestMessages;
529         ArrayList responseMessages;
530
531         requestMessages.Construct();
532         responseMessages.Construct();
533
534         String serviceId(_SETTING_SERVICE_ID);
535
536         requestMessages.Add(serviceId);
537         requestMessages.Add(commandId);
538         requestMessages.Add(requestKey);
539
540         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
541
542         r = __pIpcClient->SendRequest(*pMsg);
543         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
544
545         String* pResult = (String*)responseMessages.GetAt(2);
546         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
547         r = ConvertCode(*pResult);
548         if(r == E_SUCCESS)
549         {
550                 String* pValue = (String*)responseMessages.GetAt(3);
551                 SysTryReturnResult(NID_SYS, pValue != null, E_SYSTEM, "There is no result value.");
552                 response = *pValue;
553         }
554         return r;
555 }
556
557 result
558 _SettingClient::SetOnService(String commandId, String key, String& value)
559 {
560         result r = E_SUCCESS;
561         String requestKey = ConvertKeyOspToTizen(key);
562
563         ArrayList requestMessages;
564         ArrayList responseMessages;
565
566         requestMessages.Construct();
567         responseMessages.Construct();
568
569         String serviceId(_SETTING_SERVICE_ID);
570
571         requestMessages.Add(serviceId);
572         requestMessages.Add(commandId);
573         requestMessages.Add(requestKey);
574         requestMessages.Add(value);
575
576         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
577         r = __pIpcClient->SendRequest(*pMsg);
578         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
579
580         String* pResult = (String*)responseMessages.GetAt(2);
581         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
582         SysLog(NID_SYS, "Result is %ls.", pResult->GetPointer());
583         r = ConvertCode(*pResult);
584         SysLog(NID_SYS, "r is %s.", GetErrorMessage(r));
585
586         return r;
587 }
588
589 result
590 _SettingClient::GetValue(const String& key, bool& value)
591 {
592         String response;
593         result r = GetOnService(_SETTING_COMMAND_GET_BOOL, key, response);
594
595         if(r == E_SUCCESS)
596         {
597                 if(response == L"0")
598                 {
599                         value = false;
600                 }
601                 else if(response == L"1")
602                 {
603                         value = true;
604                 }
605                 else
606                 {
607                         SysLogException(NID_SYS, E_SYSTEM, "There are wrong value [%ls].", key.GetPointer());
608                         return E_SYSTEM;
609                 }
610         }
611
612         return r;
613 }
614
615 result
616 _SettingClient::GetValue(const String& key, int& value)
617 {
618         String response;
619         result r = GetOnService(_SETTING_COMMAND_GET_INT, key, response);
620
621         if(r == E_SUCCESS)
622         {
623                 r = Integer::Parse(response, value);
624                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to convert to integer from [%s]", response.GetPointer());
625         }
626
627         return r;
628 }
629
630 result
631 _SettingClient::GetValue(const String& key, long long& value)
632 {
633         return E_OBJ_NOT_FOUND;
634 }
635
636 result
637 _SettingClient::GetValue(const String& key, double& value)
638 {
639         return E_OBJ_NOT_FOUND;
640 }
641
642 result
643 _SettingClient::GetValue(const String& key, UuId& value)
644 {
645         return E_OBJ_NOT_FOUND;
646 }
647
648 result
649 _SettingClient::GetValue(const String& key, String& value)
650 {
651         String response;
652         result r = E_SUCCESS;
653
654         if(key == _USBMODE) //This is compatible issue. USB mode is fixed on Tizen.
655         {
656                 value = _USBMODE_MTP;
657                 return E_SUCCESS;
658         }
659
660         if(key == _LANGUAGE) //This is not full compatible.
661         {
662                 r = GetOnService(_SETTING_COMMAND_GET_STRING, _LOCALE_LANGUAGE, response);
663         }
664         else
665         {
666                 r = GetOnService(_SETTING_COMMAND_GET_STRING, key, response);
667         }
668
669         if(r == E_SUCCESS)
670         {
671                 value = response;
672         }
673
674         if(key == _LANGUAGE) //This is not full compatible.
675         {
676                 r = value.SubString(0, 3, value);
677                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to sub string on %ls.", value.GetPointer());
678         }
679
680         return r;
681 }
682
683 result
684 _SettingClient::GetValueForPrivilegedKey(const String& key, bool& value)
685 {
686         String response;
687         result r = GetOnService(_SETTING_COMMAND_GET_BOOL_PRIV, key, response);
688
689         if(r == E_SUCCESS)
690         {
691                 if(response == L"0")
692                 {
693                         value = false;
694                 }
695                 else if(response == L"1")
696                 {
697                         value = true;
698                 }
699                 else
700                 {
701                         SysLogException(NID_SYS, E_SYSTEM, "There are wrong value [%ls].", key.GetPointer());
702                         return E_SYSTEM;
703                 }
704         }
705
706         return r;
707 }
708
709 result
710 _SettingClient::SetValue(const String& key, const bool& value)
711 {
712         String requestValue;
713         if(value == true)
714         {
715                 requestValue.Append("true");
716         }
717         else
718         {
719                 requestValue.Append("false");
720         }
721         return SetOnService(_SETTING_COMMAND_SET_BOOL, key, requestValue);
722 }
723
724 result
725 _SettingClient::SetValue(const String& key, const int& value)
726 {
727         String requestValue;
728         requestValue.Append(value);
729         return SetOnService(_SETTING_COMMAND_SET_INT, key, requestValue);
730 }
731
732 result
733 _SettingClient::SetValue(const String& key, const String& value)
734 {
735         String requestValue;
736         requestValue.Append(value);
737         return SetOnService(_SETTING_COMMAND_SET_STRING, key, requestValue);
738 }
739
740 result
741 _SettingClient::SetValueForPrivilegedKey(const String& key, bool value)
742 {
743         String requestValue;
744         if(value == true)
745         {
746                 requestValue.Append("true");
747         }
748         else
749         {
750                 requestValue.Append("false");
751         }
752         return SetOnService(_SETTING_COMMAND_SET_BOOL_PRIV, key, requestValue);
753 }
754
755 result
756 _SettingClient::SetValueForPrivilegedKey(const String& key, String value)
757 {
758         String requestValue;
759         requestValue.Append(value);
760         return SetOnService(_SETTING_COMMAND_SET_STRING_PRIV, key, requestValue);
761 }
762
763
764 result
765 _SettingClient::SetValueAsync(const String& key, bool value, ISettingInfoSetValueAsyncResultListener* listener)
766 {
767         result r = E_SUCCESS;
768         String requestValue;
769
770         SysTryReturnResult(NID_SYS, __asyncEventList.ContainsKey(key) == false, E_IN_PROGRESS, "Required key[%ls] is already in progress.", key.GetPointer());
771
772         if(value == true)
773         {
774                 requestValue.Append("true");
775         }
776         else
777         {
778                 requestValue.Append("false");
779         }
780
781         r = SetOnService(_SETTING_COMMAND_SET_BOOL_ASYNC, key, requestValue);
782
783         if(r == E_SUCCESS)
784         {
785                 if(listener != null)
786                 {
787                         _SettingAsyncEvent* pEvent = new (std::nothrow) _SettingAsyncEvent();
788                         pEvent->AddListener(*listener);
789
790                         __asyncEventList.Add(new String(key), pEvent);
791                 }
792         }
793
794         return r;
795 }
796
797 result
798 _SettingClient::SetValueAsyncForPrivilegedKey(const String& key, bool value, ISettingInfoSetValueAsyncResultListener* listener)
799 {
800         result r = E_SUCCESS;
801         String requestValue;
802
803         SysTryReturnResult(NID_SYS, __asyncEventList.ContainsKey(key) == false, E_IN_PROGRESS, "Required key[%ls] is already in progress.", key.GetPointer());
804
805         if(value == true)
806         {
807                 requestValue.Append("true");
808         }
809         else
810         {
811                 requestValue.Append("false");
812         }
813
814         r = SetOnService(_SETTING_COMMAND_SET_BOOL_ASYNC_PRIV, key, requestValue);
815
816         if(r == E_SUCCESS)
817         {
818                 if(listener != null)
819                 {
820                         _SettingAsyncEvent* pEvent = new (std::nothrow) _SettingAsyncEvent();
821                         pEvent->AddListener(*listener);
822
823                         __asyncEventList.Add(new String(key), pEvent);
824                 }
825         }
826
827         return r;
828 }
829
830 void
831 _SettingClient::OnDataReceived(const ArrayList& data)
832 {
833         result r = E_SUCCESS;
834         String* pServiceId = (String*)(data.GetAt(0));
835         String* pResponseId = (String*)(data.GetAt(1));
836         String* pKey = (String*)(data.GetAt(2));
837         String* pValue = null;
838
839         SysTryReturnVoidResult(NID_SYS, pServiceId != null, E_SYSTEM, "There is no service Id.");
840         SysTryReturnVoidResult(NID_SYS, pResponseId != null, E_SYSTEM, "There is response Id.");
841         SysTryReturnVoidResult(NID_SYS, pKey != null, E_SYSTEM, "There is no key.");
842
843         SysLog(NID_SYS, "ServiceId: %ls, ResponseId: %ls, Key: %ls", pServiceId->GetPointer(), pResponseId->GetPointer(), pKey->GetPointer());
844
845         if(*pResponseId ==  _SETTING_COMMAND_SET_BOOL_ASYNC || *pResponseId == _SETTING_COMMAND_SET_BOOL_ASYNC_PRIV
846                 || *pResponseId ==  _SETTING_COMMAND_SET_STRING_ASYNC)
847         {
848
849                 pValue =  (String*)(data.GetAt(3));
850
851                 SysTryReturnVoidResult(NID_SYS, pValue != null, E_SYSTEM, "There is no value.");
852                 SysLog(NID_SYS, "Value: %ls", pValue->GetPointer());
853
854                 _SettingAsyncEvent* pEvent =  dynamic_cast <_SettingAsyncEvent*> (__asyncEventList.GetValue(*pKey));
855                 r = ConvertCode(*pValue);
856
857                 if(pEvent != null)
858                 {
859                         _SettingAsyncEventArg* pSettingAsyncEventArg = new (std::nothrow) _SettingAsyncEventArg;
860                         pSettingAsyncEventArg->Key = *pKey;
861                         pSettingAsyncEventArg->errorCode = r;
862                         pEvent->Fire(*pSettingAsyncEventArg);
863                 }
864                 r = __asyncEventList.Remove(*pKey, true);
865         }
866         else if(*pResponseId == _SETTING_SERVICE_TYPE_EVENT)
867         {
868                 SysTryReturnVoidResult(NID_SYS, __pSettingEvent != null, E_SYSTEM, "Event is not ready.");
869
870                 _SettingEventArg* pSettingEventArg = new (std::nothrow) _SettingEventArg();
871                 pSettingEventArg->KeyName = *pKey;
872
873
874                 if(pSettingEventArg->KeyName == _LOCALE_LANGUAGE
875                         || pSettingEventArg->KeyName == _LOCALE_COUNTRY
876                         || pSettingEventArg->KeyName == _FONT_TYPE
877                         || pSettingEventArg->KeyName == _FONT_SIZE)
878                 {
879                         if(__pSettingEventForInternal != null)
880                         {
881                                 _SettingEventArg* pSettingEventArgForInternal = new (std::nothrow) _SettingEventArg();
882                                 pSettingEventArgForInternal->KeyName = pSettingEventArg->KeyName;
883                                 __pSettingEventForInternal->Fire(*pSettingEventArgForInternal);
884                         }
885                         else
886                         {
887                                 SysLogException(NID_SYS, E_SYSTEM, "Internal Event is not ready.");
888                         }
889                 }
890
891
892                 if(_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat() == true)
893                 {
894                         String ospKey;
895                         r = ConvertKeyTizenToOsp(pSettingEventArg->KeyName, ospKey);
896                         if(r == E_SUCCESS)
897                         {
898                                 pSettingEventArg->KeyName = ospKey;
899                                 __pSettingEvent->Fire(*pSettingEventArg);
900                         }
901                 }
902                 else
903                 {
904                         __pSettingEvent->Fire(*pSettingEventArg);
905                 }
906         }
907 }
908
909 bool
910 _SettingClient::HasKey(const String& key)
911 {
912         String response;
913         result r = GetOnService(_SETTING_COMMAND_SUPPORTED, key, response);
914
915         SysLog(NID_SYS, "HasKey result key:%ls, %ls", key.GetPointer(), response.GetPointer());
916         if(response == L"1" && r == E_SUCCESS)
917         {
918                 return true;
919         }
920         return false;
921 }
922
923 result
924 _SettingClient::ResetToFactoryDefault(void)
925 {
926         String value;
927         String response;
928         value = "none";
929         result r = GetOnService(_SETTING_COMMAND_FACTORY_RESET, value, response);
930         SysLog(NID_SYS, "Reset result is %s[%ls].", GetErrorMessage(r), response.GetPointer());
931         return r;
932 }
933
934 //For OSP_COMPAT mode
935 result
936 _SettingClient::SetVolume(const String& soundCategory, int level)
937 {
938         result r = SetValue(soundCategory, level);
939         if (r == E_INVALID_ARG)
940         {
941                 r = E_OUT_OF_RANGE;
942         }
943         else if(r == E_OBJ_NOT_FOUND)
944         {
945                 r = E_INVALID_ARG;
946         }
947         else if(r == E_UNSUPPORTED_OPERATION)
948         {
949                 r = E_SYSTEM;
950         }
951         return r;
952 }
953
954 //Event Management
955 result
956 _SettingClient::SubscribeEvent(void)
957 {
958         result r = E_SUCCESS;
959         SysTryReturnResult(NID_SYS, __pSettingEvent != null, E_SYSTEM, "listener event is not ready.");
960         _SettingEvent* pSettingEvent = dynamic_cast<_SettingEvent*> (__pSettingEvent);
961         SysTryReturnResult(NID_SYS, pSettingEvent != null, E_SYSTEM, "listener event is not ready.");
962
963         SysTryReturnResult(NID_SYS, __pSettingEventForInternal != null, E_SYSTEM, "listener list is not ready.");
964         _SettingEvent* pSettingEventForInternal = dynamic_cast<_SettingEvent*> (__pSettingEventForInternal);
965         SysTryReturnResult(NID_SYS, pSettingEventForInternal != null, E_SYSTEM, "listener list is not ready.");
966
967         if(pSettingEvent->GetCount() > 0 || pSettingEventForInternal->GetCount() >0)
968         {
969                 if(__subscribed == false)
970                 {
971                         ArrayList requestMessages;
972                         ArrayList responseMessages;
973
974                         requestMessages.Construct();
975                         responseMessages.Construct();
976
977                         String serviceId(_SETTING_SERVICE_ID);
978                         String eventId(_SETTING_COMMAND_ADD_EVENT);
979
980                         requestMessages.Add(serviceId);
981                         requestMessages.Add(eventId);
982
983                         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
984
985                         r = __pIpcClient->SendRequest(*pMsg);
986                         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
987
988                         String* pResult = (String*)responseMessages.GetAt(2);
989                         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
990                         r = ConvertCode(*pResult);
991
992                         if(r == E_SUCCESS)
993                         {
994                                 __subscribed = true;
995                         }
996                 }
997         }
998         else if(pSettingEvent->GetCount() == 0 && pSettingEventForInternal->GetCount() == 0)
999         {
1000                 if(__subscribed == true)
1001                 {
1002                         ArrayList requestMessages;
1003                         ArrayList responseMessages;
1004
1005                         requestMessages.Construct();
1006                         responseMessages.Construct();
1007
1008                         String serviceId(_SETTING_SERVICE_ID);
1009                         String eventId(_SETTING_COMMAND_REMOVE_EVENT);
1010
1011                         requestMessages.Add(serviceId);
1012                         requestMessages.Add(eventId);
1013
1014                         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
1015
1016                         r = __pIpcClient->SendRequest(*pMsg);
1017                         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
1018
1019                         String* pResult = (String*)responseMessages.GetAt(2);
1020                         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
1021                         r = ConvertCode(*pResult);
1022
1023                         if(r == E_SUCCESS)
1024                         {
1025                                 __subscribed = false;
1026                         }
1027                 }
1028         }
1029
1030         return r;
1031 }
1032
1033 result
1034 _SettingClient::AddSettingEventListener(ISettingEventListener& listener)
1035 {
1036         SysTryReturnResult(NID_SYS, __pSettingEvent != null, E_SYSTEM, "listener event is not ready.");
1037         _SettingEvent* pSettingEvent = dynamic_cast<_SettingEvent*> (__pSettingEvent);
1038         SysTryReturnResult(NID_SYS, pSettingEvent != null, E_SYSTEM, "listener event is not ready.");
1039
1040         result r = pSettingEvent->AddSettingEventListener(listener);
1041         if (r == E_INVALID_OPERATION)
1042         {
1043                 r = E_SYSTEM;
1044         }
1045         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add event listener.");
1046
1047         r = SubscribeEvent();
1048         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to request to add event listener.");
1049
1050         return r;
1051 }
1052
1053 result
1054 _SettingClient::RemoveSettingEventListener(ISettingEventListener& listener)
1055 {
1056         SysTryReturnResult(NID_SYS, __pSettingEvent != null, E_SYSTEM, "listener event is not ready.");
1057         _SettingEvent* pSettingEvent = dynamic_cast<_SettingEvent*> (__pSettingEvent);
1058         SysTryReturnResult(NID_SYS, pSettingEvent != null, E_SYSTEM, "listener event is not ready.");
1059
1060         result r = pSettingEvent->RemoveSettingEventListener(listener);
1061
1062         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to remove event listener.");
1063
1064         r = SubscribeEvent();
1065         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to request to remove event listener.");
1066
1067         return r;
1068 }
1069
1070 // Event for internal
1071 result
1072 _SettingClient::AddSettingEventListenerForInternal(ISettingEventListener& listener)
1073 {
1074         SysTryReturnResult(NID_SYS, __pSettingEventForInternal != null, E_SYSTEM, "listener event is not ready.");
1075         _SettingEvent* pSettingEvent = dynamic_cast<_SettingEvent*> (__pSettingEventForInternal);
1076         SysTryReturnResult(NID_SYS, pSettingEvent != null, E_SYSTEM, "listener event is not ready.");
1077
1078         result r = pSettingEvent->AddSettingEventListener(listener);
1079         if (r == E_INVALID_OPERATION)
1080         {
1081                 r = E_SYSTEM;
1082         }
1083
1084         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to add event listener.");
1085
1086         r = SubscribeEvent();
1087         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to request to add event listener.");
1088
1089         return r;
1090 }
1091
1092 result
1093 _SettingClient::RemoveSettingEventListenerForInternal(ISettingEventListener& listener)
1094 {
1095         SysTryReturnResult(NID_SYS, __pSettingEventForInternal != null, E_SYSTEM, "listener list is not ready.");
1096         _SettingEvent* pSettingEvent = dynamic_cast<_SettingEvent*> (__pSettingEventForInternal);
1097         SysTryReturnResult(NID_SYS, pSettingEvent != null, E_SYSTEM, "listener event is not ready.");
1098
1099         result r =  pSettingEvent->RemoveSettingEventListener(listener);
1100
1101         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to remove event listener.");
1102
1103         r = SubscribeEvent();
1104         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to request to remove event listener.");
1105
1106         return r;
1107 }
1108
1109 //For OSP_COMPAT mode
1110 result
1111 _SettingClient::SetSettingEventListener(ISettingEventListener* pListener)
1112 {
1113         result r = E_SUCCESS;
1114         if(__pListener != null)
1115         {
1116                 r = RemoveSettingEventListener(*pListener);
1117                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to unregister exist listener.");
1118                 if(pListener != null)
1119                 {
1120                         r = AddSettingEventListener(*pListener);
1121                         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to register listener.");
1122                 }
1123         }
1124
1125         __pListener = pListener;
1126
1127         return r;
1128 }
1129
1130 } } // Tizen::System