Merge "Change the way to conver Mbs to Wcs and vice versa" into tizen_2.1
[platform/framework/native/appfw.git] / src / system / FSys_RuntimeClient.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_RuntimeClient.cpp
20  * @brief               This is the implementation file for _RuntimeClient class.
21  */
22
23 #include <unique_ptr.h>
24
25 #include <FBaseInteger.h>
26 #include <FBaseLongLong.h>
27 #include <FBaseSysLog.h>
28
29 #include <FApp_AppInfo.h>
30
31 #include <FIo_AppServiceIpcMessages.h>
32 #include "FSys_CommunicationDispatcherClient.h"
33 #include "FSys_RuntimeClient.h"
34
35 using namespace std;
36
37 using namespace Tizen::App;
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Base::Runtime;
41 using namespace Tizen::Io;
42
43 namespace Tizen { namespace System
44 {
45
46 static const int _RUNTIME_GET_PARAM_TYPE = 1;
47 static const wchar_t* _RUNTIME_SERVICE_ID = L"osp.system.service";
48 static const wchar_t* _RUNTIME_GET_SIZE = L"osp.system.command.runtime.get.size";
49 static const wchar_t* _RUNTIME_RESULT_SUCCESS = L"osp.system.result.success";
50 static const wchar_t* _RUNTIME_RESULT_SYSTEM = L"osp.system.result.system";
51
52 _RuntimeClient* _RuntimeClient::__pRuntimeClient= null;
53
54 class _RuntimeAsyncEventArg : public IEventArg
55 {
56 public:
57         long long value;
58         result rCode;
59 };
60
61 class _RuntimeAsyncEvent : public Event
62 {
63 protected:
64         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
65         {
66                 IRuntimeInfoGetLonglongAsyncResultListener* pListener = dynamic_cast<IRuntimeInfoGetLonglongAsyncResultListener*> (&listener);
67                 const _RuntimeAsyncEventArg* pArg = dynamic_cast<const _RuntimeAsyncEventArg*>(&arg);
68
69                 if(pListener == null || pArg == null)
70                 {
71                         SysLogException(NID_SYS, E_SYSTEM, "It is failed to get listener or arguemnt");
72                         return;
73                 }
74                 pListener->OnResultReceivedForGetValueAsync(pArg->value, pArg->rCode);
75                 RemoveListener(listener);
76         }
77 };
78
79 _RuntimeClient*
80 _RuntimeClient::GetInstance(void)
81 {
82         if(__pRuntimeClient == null)
83         {
84                 __pRuntimeClient = new (std::nothrow) _RuntimeClient();
85         }
86         return __pRuntimeClient;
87 }
88
89 _RuntimeClient::_RuntimeClient()
90         : __msgCount(0)
91         , __pIpcClient(null)
92 {
93         result r = E_SUCCESS;
94         static String RUNTIME_SERVICE_ID(_RUNTIME_SERVICE_ID);
95
96         _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
97         SysTryCatch(NID_SYS, pCommunicationDispatcherClient != null, r = E_SYSTEM, r, "It is failed to get CommunicationDispatcherClient.");
98
99         r = pCommunicationDispatcherClient->RegisterCommunicationListener(RUNTIME_SERVICE_ID, *this);
100         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on CommunicationDispatcherClient.");
101
102         __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
103
104         __asyncEventList.Construct();
105 CATCH:
106         SetLastResult(r);
107 }
108
109 _RuntimeClient::~_RuntimeClient()
110 {
111         result r = E_SUCCESS;
112         String key(_RUNTIME_SERVICE_ID);
113         _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
114         SysTryCatch(NID_SYS, pCommunicationDispatcherClient != null, r = E_SYSTEM, r, "It is failed to get CommunicationDispatcherClient.");
115
116         r = pCommunicationDispatcherClient->UnregisterCommunicationListener(key);
117         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on CommunicationDispatcherClient.");
118
119         __asyncEventList.RemoveAll(true);
120         __pIpcClient = null;
121 CATCH:
122         SetLastResult(r);
123 }
124
125 result
126 _RuntimeClient::GetDirectorySizeValueAsync(const String& path, IRuntimeInfoGetLonglongAsyncResultListener* listener)
127 {
128         result r = E_SUCCESS;
129
130         ArrayList requestMessages;
131         ArrayList responseMessages;
132
133         requestMessages.Construct();
134         responseMessages.Construct();
135
136         String serviceId(_RUNTIME_SERVICE_ID);
137         String commandId(_RUNTIME_GET_SIZE);
138         String messageId;
139         messageId.Append(__msgCount);
140
141         requestMessages.Add(serviceId);
142         requestMessages.Add(commandId);
143         requestMessages.Add(path);
144         requestMessages.Add(messageId);
145
146         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
147         r = __pIpcClient->SendRequest(*pMsg);
148         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
149
150         String* pResult = (String*)responseMessages.GetAt(2);
151         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
152         SysLog(NID_SYS, "Result is %ls.", pResult->GetPointer());
153
154         if(*pResult == _RUNTIME_RESULT_SUCCESS)
155                 r = E_SUCCESS;
156         else
157                 r = E_SYSTEM;
158
159         SysLog(NID_SYS, "r is %s.", GetErrorMessage(r));
160
161         if(r == E_SUCCESS)
162         {
163                 _RuntimeAsyncEvent* pEvent = new (std::nothrow) _RuntimeAsyncEvent();
164                 pEvent->AddListener(*listener);
165
166                 __asyncEventList.Add(new Integer(__msgCount), pEvent);
167                 __msgCount++;
168         }
169         return r;
170 }
171
172 void
173 _RuntimeClient::OnDataReceived(const Tizen::Base::Collection::ArrayList& data)
174 {
175         SysLog(NID_SYS, "Receive result");
176         String* pServiceId = (String*)(data.GetAt(0));
177         String* pResponseId = (String*)(data.GetAt(1));
178         String* pMessageId = (String*)(data.GetAt(2));
179         String* pValue = (String*)(data.GetAt(3));
180         String* pResultCode = (String*)(data.GetAt(4));
181
182         SysTryReturnVoidResult(NID_SYS, pServiceId != null && pResponseId != null && pMessageId != null && pValue != null && pResultCode != null, E_SYSTEM, "There is no result data.");
183
184         int msg_id = 0;
185         Integer::Parse(*pMessageId, msg_id);
186
187         Integer msgKey(msg_id);
188
189         _RuntimeAsyncEvent* pEvent = dynamic_cast <_RuntimeAsyncEvent*> (__asyncEventList.GetValue(msgKey));
190         SysTryReturnVoidResult(NID_SYS, pEvent != null, E_SYSTEM, "There is no registered event.");
191
192         _RuntimeAsyncEventArg* pEventArg = new (std::nothrow) _RuntimeAsyncEventArg();
193
194         LongLong::Parse(*pValue, pEventArg->value);
195
196         if(*pResultCode == _RUNTIME_RESULT_SUCCESS)
197         {
198                 pEventArg->rCode = E_SUCCESS;
199         }
200         else
201         {
202                 pEventArg->rCode = E_SYSTEM;
203         }
204
205         pEvent->Fire(*pEventArg);
206 }
207
208 } } // Tizen::System
209