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