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