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