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