Merge "Update deprecated libprivilege-control API functions." into tizen
[platform/framework/native/appfw.git] / src / system / FSys_SystemClient.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 "FSys_SystemClient.h"
25
26 #include <FBaseSysLog.h>
27
28 #include <FIo_AppServiceIpcMessages.h>
29 #include "FSys_CommunicationDispatcherClient.h"
30
31 using namespace std;
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::Io;
37
38 namespace Tizen { namespace System
39 {
40
41 static const wchar_t* _SYSTEM_SERVICE_ID = L"osp.system.service";
42 static const wchar_t* _SYSTEM_COMMAND_GET_BOOL = L"osp.system.command.system.get.bool";
43 static const wchar_t* _SYSTEM_COMMAND_GET_INT = L"osp.system.command.system.get.int";
44 static const wchar_t* _SYSTEM_COMMAND_GET_STRING = L"osp.system.command.system.get.string";
45
46 static const wchar_t* _SYSTEM_OK = L"osp.system.result.ok";
47 static const wchar_t* _SYSTEM_INVALID_ARG = L"osp.system.result.invalid_arg";
48 static const wchar_t* _SYSTEM_OBJ_NOT_FOUND = L"osp.system.result.obj_not_found";
49 static const wchar_t* _SYSTEM_ERROR = L"osp.system.result.error";
50
51 _SystemClient* _SystemClient::__pSystemClient = null;
52
53 _SystemClient*
54 _SystemClient::GetInstance(void)
55 {
56         if(__pSystemClient == null)
57         {
58                 __pSystemClient = new (std::nothrow) _SystemClient();
59         }
60         return __pSystemClient;
61 }
62
63 _SystemClient::_SystemClient()
64         :__pIpcClient(null)
65 {
66         result r = E_SUCCESS;
67         _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
68         r = pCommunicationDispatcherClient->RegisterCommunicationListener(_SYSTEM_SERVICE_ID, *this);
69         __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
70 }
71
72 _SystemClient::~_SystemClient()
73 {
74 }
75
76 result
77 _SystemClient::ConvertResultCode(String code)
78 {
79         result r = E_SYSTEM;
80         if(code == _SYSTEM_OK)
81         {
82                 r = E_SUCCESS;
83         }
84         else if(code == _SYSTEM_OBJ_NOT_FOUND)
85         {
86                 r = E_OBJ_NOT_FOUND;
87         }
88         return r;
89 }
90
91 result
92 _SystemClient::GetValue(const String& key, bool& value)
93 {
94         result r = E_SUCCESS;
95         String requestKey(key);
96         String serviceId(_SYSTEM_SERVICE_ID);
97         String commandId(_SYSTEM_COMMAND_GET_BOOL);
98
99         _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
100         SysTryReturnResult(NID_SYS, pCommunicationDispatcherClient != null, E_SYSTEM, "It is failed to get Communication Dispatcher.");
101
102         __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
103         SysTryReturnResult(NID_SYS, __pIpcClient != null, E_SYSTEM, "It is failed to get IPC client.");
104
105         ArrayList requestMessages;
106         ArrayList responseMessages;
107
108         requestMessages.Construct();
109         responseMessages.Construct();
110
111         requestMessages.Add(serviceId);
112         requestMessages.Add(commandId);
113         requestMessages.Add(requestKey);
114
115         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
116
117         r = __pIpcClient->SendRequest(*(pMsg.get()));
118         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
119
120         String* pResult = (String*)responseMessages.GetAt(2);
121         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
122
123         r = ConvertResultCode(*pResult);
124         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "Error is occured.");
125
126         String* pValue = (String*)responseMessages.GetAt(3);
127         SysTryReturnResult(NID_SYS, pValue != null, E_SYSTEM, "There is no result value.");
128         
129         if(pValue->Equals(L"1", false))
130         {
131                 value = true;
132         }
133         else
134         {
135                 value = false;
136         }
137         responseMessages.RemoveAll(true);
138         return r;
139 }
140
141 result
142 _SystemClient::GetValue(const String& key, int& value)
143 {
144         result r = E_SUCCESS;
145         String requestKey(key);
146         String serviceId(_SYSTEM_SERVICE_ID);
147         String commandId(_SYSTEM_COMMAND_GET_INT);
148
149         _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
150         SysTryReturnResult(NID_SYS, pCommunicationDispatcherClient != null, E_SYSTEM, "It is failed to get Communication Dispatcher.");
151
152         __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
153         SysTryReturnResult(NID_SYS, __pIpcClient != null, E_SYSTEM, "It is failed to get IPC client.");
154
155         ArrayList requestMessages;
156         ArrayList responseMessages;
157
158         requestMessages.Construct();
159         responseMessages.Construct();
160
161         requestMessages.Add(serviceId);
162         requestMessages.Add(commandId);
163         requestMessages.Add(requestKey);
164
165         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
166
167         r = __pIpcClient->SendRequest(*(pMsg.get()));
168         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
169
170         String* pResult = (String*)responseMessages.GetAt(2);
171         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
172
173         r = ConvertResultCode(*pResult);
174         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "Error is occured.");
175
176         String* pValue = (String*)responseMessages.GetAt(3);
177         SysTryReturnResult(NID_SYS, pValue != null, E_SYSTEM, "There is no result value.");
178
179         r = Integer::Parse(*pValue, value);
180         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "Result value[%ls] convert is failed.", pValue->GetPointer());
181
182         responseMessages.RemoveAll(true);
183
184         return r;
185 }
186
187 result
188 _SystemClient::GetValue(const String& key, String& value)
189 {
190         result r = E_SUCCESS;
191         String requestKey(key);
192         String serviceId(_SYSTEM_SERVICE_ID);
193         String commandId(_SYSTEM_COMMAND_GET_STRING);
194
195         _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
196         SysTryReturnResult(NID_SYS, pCommunicationDispatcherClient != null, E_SYSTEM, "It is failed to get Communication Dispatcher.");
197
198         __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
199         SysTryReturnResult(NID_SYS, __pIpcClient != null, E_SYSTEM, "It is failed to get IPC client.");
200
201         ArrayList requestMessages;
202         ArrayList responseMessages;
203
204         requestMessages.Construct();
205         responseMessages.Construct();
206
207         requestMessages.Add(serviceId);
208         requestMessages.Add(commandId);
209         requestMessages.Add(requestKey);
210
211         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
212
213         r = __pIpcClient->SendRequest(*(pMsg.get()));
214         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
215
216         String* pResult = (String*)responseMessages.GetAt(2);
217         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
218
219         r = ConvertResultCode(*pResult);
220         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "Error is occured.");
221
222         String* pValue = (String*)responseMessages.GetAt(3);
223         SysTryReturnResult(NID_SYS, pValue != null, E_SYSTEM, "There is no result value.");
224
225         value = *pValue;
226
227         responseMessages.RemoveAll(true);
228         return r;
229 }
230
231 void
232 _SystemClient::OnDataReceived(const Tizen::Base::Collection::ArrayList& data)
233 {
234 }
235
236 } } // Tizen::System