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