Merge "Change the way to conver Mbs to Wcs and vice versa" into tizen_2.1
[platform/framework/native/appfw.git] / src / system / FSys_SystemClient.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FSys_SettingClient.cpp
20  * @brief               This is the implementation file for _SettingClient class.
21  */
22
23 #include <unique_ptr.h>
24
25 #include "FSys_SystemClient.h"
26
27 #include <FBaseSysLog.h>
28
29 #include <FIo_AppServiceIpcMessages.h>
30 #include "FSys_CommunicationDispatcherClient.h"
31
32 using namespace std;
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Io;
38
39 namespace Tizen { namespace System
40 {
41
42 static const wchar_t* _SYSTEM_SERVICE_ID = L"osp.system.service";
43 static const wchar_t* _SYSTEM_COMMAND_GET_BOOL = L"osp.system.command.system.get.bool";
44 static const wchar_t* _SYSTEM_COMMAND_GET_INT = L"osp.system.command.system.get.int";
45 static const wchar_t* _SYSTEM_COMMAND_GET_STRING = L"osp.system.command.system.get.string";
46
47 static const wchar_t* _SYSTEM_OK = L"osp.system.result.ok";
48 static const wchar_t* _SYSTEM_INVALID_ARG = L"osp.system.result.invalid_arg";
49 static const wchar_t* _SYSTEM_OBJ_NOT_FOUND = L"osp.system.result.obj_not_found";
50 static const wchar_t* _SYSTEM_ERROR = L"osp.system.result.error";
51
52 _SystemClient* _SystemClient::__pSystemClient = null;
53
54 _SystemClient*
55 _SystemClient::GetInstance(void)
56 {
57         if(__pSystemClient == null)
58         {
59                 __pSystemClient = new (std::nothrow) _SystemClient();
60         }
61         return __pSystemClient;
62 }
63
64 _SystemClient::_SystemClient()
65         :__pIpcClient(null)
66 {
67         result r = E_SUCCESS;
68         _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
69         r = pCommunicationDispatcherClient->RegisterCommunicationListener(_SYSTEM_SERVICE_ID, *this);
70         __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
71 }
72
73 _SystemClient::~_SystemClient()
74 {
75 }
76
77 result
78 _SystemClient::ConvertResultCode(String code)
79 {
80         result r = E_SYSTEM;
81         if(code == _SYSTEM_OK)
82         {
83                 r = E_SUCCESS;
84         }
85         else if(code == _SYSTEM_OBJ_NOT_FOUND)
86         {
87                 r = E_OBJ_NOT_FOUND;
88         }
89         return r;
90 }
91
92 result
93 _SystemClient::GetValue(const String& key, bool& value)
94 {
95         result r = E_SUCCESS;
96         String requestKey(key);
97         String serviceId(_SYSTEM_SERVICE_ID);
98         String commandId(_SYSTEM_COMMAND_GET_BOOL);
99
100         _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
101         SysTryReturnResult(NID_SYS, pCommunicationDispatcherClient != null, E_SYSTEM, "It is failed to get Communication Dispatcher.");
102
103         __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
104         SysTryReturnResult(NID_SYS, __pIpcClient != null, E_SYSTEM, "It is failed to get IPC client.");
105
106         ArrayList requestMessages;
107         ArrayList responseMessages;
108
109         requestMessages.Construct();
110         responseMessages.Construct();
111
112         requestMessages.Add(serviceId);
113         requestMessages.Add(commandId);
114         requestMessages.Add(requestKey);
115
116         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
117
118         r = __pIpcClient->SendRequest(*pMsg);
119         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
120
121         String* pResult = (String*)responseMessages.GetAt(2);
122         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
123
124         r = ConvertResultCode(*pResult);
125         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "Error is occured.");
126
127         String* pValue = (String*)responseMessages.GetAt(3);
128         SysTryReturnResult(NID_SYS, pValue != null, E_SYSTEM, "There is no result value.");
129         
130         if(pValue->Equals(L"1", false))
131         {
132                 value = true;
133         }
134         else
135         {
136                 value = false;
137         }
138         responseMessages.RemoveAll(true);
139         return r;
140 }
141
142 result
143 _SystemClient::GetValue(const String& key, int& value)
144 {
145         result r = E_SUCCESS;
146         String requestKey(key);
147         String serviceId(_SYSTEM_SERVICE_ID);
148         String commandId(_SYSTEM_COMMAND_GET_INT);
149
150         _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
151         SysTryReturnResult(NID_SYS, pCommunicationDispatcherClient != null, E_SYSTEM, "It is failed to get Communication Dispatcher.");
152
153         __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
154         SysTryReturnResult(NID_SYS, __pIpcClient != null, E_SYSTEM, "It is failed to get IPC client.");
155
156         ArrayList requestMessages;
157         ArrayList responseMessages;
158
159         requestMessages.Construct();
160         responseMessages.Construct();
161
162         requestMessages.Add(serviceId);
163         requestMessages.Add(commandId);
164         requestMessages.Add(requestKey);
165
166         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
167
168         r = __pIpcClient->SendRequest(*pMsg);
169         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
170
171         String* pResult = (String*)responseMessages.GetAt(2);
172         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
173
174         r = ConvertResultCode(*pResult);
175         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "Error is occured.");
176
177         String* pValue = (String*)responseMessages.GetAt(3);
178         SysTryReturnResult(NID_SYS, pValue != null, E_SYSTEM, "There is no result value.");
179
180         r = Integer::Parse(*pValue, value);
181         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "Result value[%ls] convert is failed.", pValue->GetPointer());
182
183         responseMessages.RemoveAll(true);
184
185         return r;
186 }
187
188 result
189 _SystemClient::GetValue(const String& key, String& value)
190 {
191         result r = E_SUCCESS;
192         String requestKey(key);
193         String serviceId(_SYSTEM_SERVICE_ID);
194         String commandId(_SYSTEM_COMMAND_GET_STRING);
195
196         _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
197         SysTryReturnResult(NID_SYS, pCommunicationDispatcherClient != null, E_SYSTEM, "It is failed to get Communication Dispatcher.");
198
199         __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
200         SysTryReturnResult(NID_SYS, __pIpcClient != null, E_SYSTEM, "It is failed to get IPC client.");
201
202         ArrayList requestMessages;
203         ArrayList responseMessages;
204
205         requestMessages.Construct();
206         responseMessages.Construct();
207
208         requestMessages.Add(serviceId);
209         requestMessages.Add(commandId);
210         requestMessages.Add(requestKey);
211
212         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
213
214         r = __pIpcClient->SendRequest(*pMsg);
215         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
216
217         String* pResult = (String*)responseMessages.GetAt(2);
218         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
219
220         r = ConvertResultCode(*pResult);
221         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "Error is occured.");
222
223         String* pValue = (String*)responseMessages.GetAt(3);
224         SysTryReturnResult(NID_SYS, pValue != null, E_SYSTEM, "There is no result value.");
225
226         value = *pValue;
227
228         responseMessages.RemoveAll(true);
229         return r;
230 }
231
232 void
233 _SystemClient::OnDataReceived(const Tizen::Base::Collection::ArrayList& data)
234 {
235 }
236
237 } } // Tizen::System