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