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