Fix system issues
[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
762 result
763 _SettingClient::SetValueAsync(const String& key, bool value, ISettingInfoSetValueAsyncResultListener* listener)
764 {
765         result r = E_SUCCESS;
766         String requestValue;
767
768         SysTryReturnResult(NID_SYS, __asyncEventList.ContainsKey(key) == false, E_IN_PROGRESS, "Required key[%ls] is already in progress.", key.GetPointer());
769
770         if(value == true)
771         {
772                 requestValue.Append("true");
773         }
774         else
775         {
776                 requestValue.Append("false");
777         }
778
779         r = SetOnService(_SETTING_COMMAND_SET_BOOL_ASYNC, key, requestValue);
780
781         if(r == E_SUCCESS)
782         {
783                 if(listener != null)
784                 {
785                         _SettingAsyncEvent* pEvent = new (std::nothrow) _SettingAsyncEvent();
786                         pEvent->AddListener(*listener);
787
788                         __asyncEventList.Add(new String(key), pEvent);
789                 }
790         }
791
792         return r;
793 }
794
795 result
796 _SettingClient::SetValueAsyncForPrivilegedKey(const String& key, bool value, ISettingInfoSetValueAsyncResultListener* listener)
797 {
798         result r = E_SUCCESS;
799         String requestValue;
800
801         SysTryReturnResult(NID_SYS, __asyncEventList.ContainsKey(key) == false, E_IN_PROGRESS, "Required key[%ls] is already in progress.", key.GetPointer());
802
803         if(value == true)
804         {
805                 requestValue.Append("true");
806         }
807         else
808         {
809                 requestValue.Append("false");
810         }
811
812         r = SetOnService(_SETTING_COMMAND_SET_BOOL_ASYNC_PRIV, key, requestValue);
813
814         if(r == E_SUCCESS)
815         {
816                 if(listener != null)
817                 {
818                         _SettingAsyncEvent* pEvent = new (std::nothrow) _SettingAsyncEvent();
819                         pEvent->AddListener(*listener);
820
821                         __asyncEventList.Add(new String(key), pEvent);
822                 }
823         }
824
825         return r;
826 }
827
828 void
829 _SettingClient::OnDataReceived(const ArrayList& data)
830 {
831         result r = E_SUCCESS;
832         String* pServiceId = (String*)(data.GetAt(0));
833         String* pResponseId = (String*)(data.GetAt(1));
834         String* pKey = (String*)(data.GetAt(2));
835         String* pValue = null;
836
837         SysTryReturnVoidResult(NID_SYS, pServiceId != null, E_SYSTEM, "There is no service Id.");
838         SysTryReturnVoidResult(NID_SYS, pResponseId != null, E_SYSTEM, "There is response Id.");
839         SysTryReturnVoidResult(NID_SYS, pKey != null, E_SYSTEM, "There is no key.");
840
841         SysLog(NID_SYS, "ServiceId: %ls, ResponseId: %ls, Key: %ls", pServiceId->GetPointer(), pResponseId->GetPointer(), pKey->GetPointer());
842
843         if(*pResponseId ==  _SETTING_COMMAND_SET_BOOL_ASYNC || *pResponseId == _SETTING_COMMAND_SET_BOOL_ASYNC_PRIV
844                 || *pResponseId ==  _SETTING_COMMAND_SET_STRING_ASYNC)
845         {
846
847                 pValue =  (String*)(data.GetAt(3));
848
849                 SysTryReturnVoidResult(NID_SYS, pValue != null, E_SYSTEM, "There is no value.");
850                 SysLog(NID_SYS, "Value: %ls", pValue->GetPointer());
851
852                 _SettingAsyncEvent* pEvent =  dynamic_cast <_SettingAsyncEvent*> (__asyncEventList.GetValue(*pKey));
853                 r = ConvertCode(*pValue);
854
855                 if(pEvent != null)
856                 {
857                         _SettingAsyncEventArg* pSettingAsyncEventArg = new (std::nothrow) _SettingAsyncEventArg;
858                         pSettingAsyncEventArg->Key = *pKey;
859                         pSettingAsyncEventArg->errorCode = r;
860                         pEvent->Fire(*pSettingAsyncEventArg);
861                 }
862                 r = __asyncEventList.Remove(*pKey, true);
863         }
864         else if(*pResponseId == _SETTING_SERVICE_TYPE_EVENT)
865         {
866                 SysTryReturnVoidResult(NID_SYS, __pSettingEvent != null, E_SYSTEM, "Event is not ready.");
867
868                 _SettingEventArg* pSettingEventArg = new (std::nothrow) _SettingEventArg();
869                 pSettingEventArg->KeyName = *pKey;
870
871
872                 if(pSettingEventArg->KeyName == _LOCALE_LANGUAGE
873                         || pSettingEventArg->KeyName == _LOCALE_COUNTRY
874                         || pSettingEventArg->KeyName == _FONT_TYPE
875                         || pSettingEventArg->KeyName == _FONT_SIZE)
876                 {
877                         if(__pSettingEventForInternal != null)
878                         {
879                                 _SettingEventArg* pSettingEventArgForInternal = new (std::nothrow) _SettingEventArg();
880                                 pSettingEventArgForInternal->KeyName = pSettingEventArg->KeyName;
881                                 __pSettingEventForInternal->Fire(*pSettingEventArgForInternal);
882                         }
883                         else
884                         {
885                                 SysLogException(NID_SYS, E_SYSTEM, "Internal Event is not ready.");
886                         }
887                 }
888
889
890                 if(_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat() == true)
891                 {
892                         String ospKey;
893                         r = ConvertKeyTizenToOsp(pSettingEventArg->KeyName, ospKey);
894                         if(r == E_SUCCESS)
895                         {
896                                 pSettingEventArg->KeyName = ospKey;
897                                 __pSettingEvent->Fire(*pSettingEventArg);
898                         }
899                 }
900                 else
901                 {
902                         __pSettingEvent->Fire(*pSettingEventArg);
903                 }
904         }
905 }
906
907 bool
908 _SettingClient::HasKey(const String& key)
909 {
910         String response;
911         result r = GetOnService(_SETTING_COMMAND_SUPPORTED, key, response);
912
913         SysLog(NID_SYS, "HasKey result key:%ls, %ls", key.GetPointer(), response.GetPointer());
914         if(response == L"1" && r == E_SUCCESS)
915         {
916                 return true;
917         }
918         return false;
919 }
920
921 result
922 _SettingClient::ResetToFactoryDefault(void)
923 {
924         return E_SUCCESS;
925 }
926
927 //For OSP_COMPAT mode
928 result
929 _SettingClient::SetVolume(const String& soundCategory, int level)
930 {
931         result r = SetValue(soundCategory, level);
932         if (r == E_INVALID_ARG)
933         {
934                 r = E_OUT_OF_RANGE;
935         }
936         else if(r == E_OBJ_NOT_FOUND)
937         {
938                 r = E_INVALID_ARG;
939         }
940         else if(r == E_UNSUPPORTED_OPERATION)
941         {
942                 r = E_SYSTEM;
943         }
944         return r;
945 }
946
947 //Event Management
948 result
949 _SettingClient::SubscribeEvent(void)
950 {
951         result r = E_SUCCESS;
952         SysTryReturnResult(NID_SYS, __pSettingEvent != null, E_SYSTEM, "listener event is not ready.");
953         _SettingEvent* pSettingEvent = dynamic_cast<_SettingEvent*> (__pSettingEvent);
954         SysTryReturnResult(NID_SYS, pSettingEvent != null, E_SYSTEM, "listener event is not ready.");
955
956         SysTryReturnResult(NID_SYS, __pSettingEventForInternal != null, E_SYSTEM, "listener list is not ready.");
957         _SettingEvent* pSettingEventForInternal = dynamic_cast<_SettingEvent*> (__pSettingEventForInternal);
958         SysTryReturnResult(NID_SYS, pSettingEventForInternal != null, E_SYSTEM, "listener list is not ready.");
959
960         if(pSettingEvent->GetCount() > 0 || pSettingEventForInternal->GetCount() >0)
961         {
962                 if(__subscribed == false)
963                 {
964                         ArrayList requestMessages;
965                         ArrayList responseMessages;
966
967                         requestMessages.Construct();
968                         responseMessages.Construct();
969
970                         String serviceId(_SETTING_SERVICE_ID);
971                         String eventId(_SETTING_COMMAND_ADD_EVENT);
972
973                         requestMessages.Add(serviceId);
974                         requestMessages.Add(eventId);
975
976                         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
977
978                         r = __pIpcClient->SendRequest(*pMsg);
979                         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
980
981                         String* pResult = (String*)responseMessages.GetAt(2);
982                         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
983                         r = ConvertCode(*pResult);
984
985                         if(r == E_SUCCESS)
986                         {
987                                 __subscribed = true;
988                         }
989                 }
990         }
991         else if(pSettingEvent->GetCount() == 0 && pSettingEventForInternal->GetCount() == 0)
992         {
993                 if(__subscribed == true)
994                 {
995                         ArrayList requestMessages;
996                         ArrayList responseMessages;
997
998                         requestMessages.Construct();
999                         responseMessages.Construct();
1000
1001                         String serviceId(_SETTING_SERVICE_ID);
1002                         String eventId(_SETTING_COMMAND_REMOVE_EVENT);
1003
1004                         requestMessages.Add(serviceId);
1005                         requestMessages.Add(eventId);
1006
1007                         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
1008
1009                         r = __pIpcClient->SendRequest(*pMsg);
1010                         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
1011
1012                         String* pResult = (String*)responseMessages.GetAt(2);
1013                         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
1014                         r = ConvertCode(*pResult);
1015
1016                         if(r == E_SUCCESS)
1017                         {
1018                                 __subscribed = false;
1019                         }
1020                 }
1021         }
1022
1023         return r;
1024 }
1025
1026 result
1027 _SettingClient::AddSettingEventListener(ISettingEventListener& listener)
1028 {
1029         SysTryReturnResult(NID_SYS, __pSettingEvent != null, E_SYSTEM, "listener event is not ready.");
1030         _SettingEvent* pSettingEvent = dynamic_cast<_SettingEvent*> (__pSettingEvent);
1031         SysTryReturnResult(NID_SYS, pSettingEvent != null, E_SYSTEM, "listener event is not ready.");
1032
1033         result r = pSettingEvent->AddSettingEventListener(listener);
1034         if (r == E_INVALID_OPERATION)
1035         {
1036                 r = E_SYSTEM;
1037         }
1038         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add event listener.");
1039
1040         r = SubscribeEvent();
1041         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to request to add event listener.");
1042
1043         return r;
1044 }
1045
1046 result
1047 _SettingClient::RemoveSettingEventListener(ISettingEventListener& listener)
1048 {
1049         SysTryReturnResult(NID_SYS, __pSettingEvent != null, E_SYSTEM, "listener event is not ready.");
1050         _SettingEvent* pSettingEvent = dynamic_cast<_SettingEvent*> (__pSettingEvent);
1051         SysTryReturnResult(NID_SYS, pSettingEvent != null, E_SYSTEM, "listener event is not ready.");
1052
1053         result r = pSettingEvent->RemoveSettingEventListener(listener);
1054
1055         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to remove event listener.");
1056
1057         r = SubscribeEvent();
1058         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to request to remove event listener.");
1059
1060         return r;
1061 }
1062
1063 // Event for internal
1064 result
1065 _SettingClient::AddSettingEventListenerForInternal(ISettingEventListener& listener)
1066 {
1067         SysTryReturnResult(NID_SYS, __pSettingEventForInternal != null, E_SYSTEM, "listener event is not ready.");
1068         _SettingEvent* pSettingEvent = dynamic_cast<_SettingEvent*> (__pSettingEventForInternal);
1069         SysTryReturnResult(NID_SYS, pSettingEvent != null, E_SYSTEM, "listener event is not ready.");
1070
1071         result r = pSettingEvent->AddSettingEventListener(listener);
1072         if (r == E_INVALID_OPERATION)
1073         {
1074                 r = E_SYSTEM;
1075         }
1076
1077         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to add event listener.");
1078
1079         r = SubscribeEvent();
1080         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to request to add event listener.");
1081
1082         return r;
1083 }
1084
1085 result
1086 _SettingClient::RemoveSettingEventListenerForInternal(ISettingEventListener& listener)
1087 {
1088         SysTryReturnResult(NID_SYS, __pSettingEventForInternal != null, E_SYSTEM, "listener list is not ready.");
1089         _SettingEvent* pSettingEvent = dynamic_cast<_SettingEvent*> (__pSettingEventForInternal);
1090         SysTryReturnResult(NID_SYS, pSettingEvent != null, E_SYSTEM, "listener event is not ready.");
1091
1092         result r =  pSettingEvent->RemoveSettingEventListener(listener);
1093
1094         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to remove event listener.");
1095
1096         r = SubscribeEvent();
1097         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It is failed to request to remove event listener.");
1098
1099         return r;
1100 }
1101
1102 //For OSP_COMPAT mode
1103 result
1104 _SettingClient::SetSettingEventListener(ISettingEventListener* pListener)
1105 {
1106         result r = E_SUCCESS;
1107         if(__pListener != null)
1108         {
1109                 r = RemoveSettingEventListener(*pListener);
1110                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to unregister exist listener.");
1111                 if(pListener != null)
1112                 {
1113                         r = AddSettingEventListener(*pListener);
1114                         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to register listener.");
1115                 }
1116         }
1117
1118         __pListener = pListener;
1119
1120         return r;
1121 }
1122
1123 } } // Tizen::System