Adding a function to get the provider count
[platform/framework/native/shell.git] / src / FShell_AppWidgetManagerProxy.cpp
1 //
2 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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        FShell_AppWidgetManagerProxy.cpp
19  * @brief       This is the implementation for the _AppWidgetManagerProxy class.
20  */
21
22 #include <FBaseRt.h>
23 #include <FBaseInteger.h>
24 #include <FBaseDouble.h>
25 #include <FBaseColHashMap.h>
26 #include <FShellTypes.h>
27 #include <FIo_IpcClient.h>
28 #include <FShell_AppWidgetManagerIpcMessage.h>
29 #include "FShell_AppWidgetProviderManagerImpl.h"
30 #include "FShell_AppWidgetManagerProxy.h"
31
32 namespace Tizen { namespace Shell {
33
34 namespace
35 {
36 const char IPC_SERVER_NAME[] = "osp.shell.ipcserver.appwidgetmanager";
37 };
38
39 using namespace Tizen::Io;
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42
43 _AppWidgetManagerProxy::_AppWidgetManagerProxy()
44         : __pIpcClient(null)
45 {
46         SysLog(NID_SHELL, "");
47 }
48
49 _AppWidgetManagerProxy::~_AppWidgetManagerProxy()
50 {
51         if (__pIpcClient)
52         {
53                 delete __pIpcClient;
54         }
55 }
56
57 _AppWidgetManagerProxy*
58 _AppWidgetManagerProxy::GetInstance()
59 {
60         static std::unique_ptr<_AppWidgetManagerProxy> __pAppWidgetProxy(null);
61         if (__pAppWidgetProxy.get() == null)
62         {
63                 std::unique_ptr<_AppWidgetManagerProxy> pAppWidgetProxy(new (std::nothrow)_AppWidgetManagerProxy);
64                 SysTryReturn(NID_SHELL, pAppWidgetProxy.get(), null, E_OUT_OF_MEMORY, "Allocating new _AppWidgetManagerProxy failed!");
65
66                 result r = pAppWidgetProxy->Construct();
67                 SysTryReturn(NID_SHELL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
68
69                 __pAppWidgetProxy = std::move(pAppWidgetProxy);
70         }
71         else
72         {
73                 if (!__pAppWidgetProxy->IsConnected())
74                 {
75                         result r = __pAppWidgetProxy->Construct();
76                         SysTryReturn(NID_SHELL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
77                 }
78         }
79
80         return __pAppWidgetProxy.get();
81 }
82
83 result
84 _AppWidgetManagerProxy::Construct(void)
85 {
86         SysLog(NID_SHELL, "Enter.");
87
88         std::unique_ptr<_IpcClient> pIpcClient(new (std::nothrow) _IpcClient() );
89         SysTryReturnResult(NID_SHELL, pIpcClient, E_OUT_OF_MEMORY, "_IpcClient creation failed.");
90
91         const int MAX_TRY_COUNT = 2;
92         const int TRY_SLEEP_TIME = 65;
93
94         int count = 0;
95         while (true)
96         {
97                 result r = pIpcClient->Construct(IPC_SERVER_NAME, this);
98                 if (r == E_SUCCESS)
99                 {
100                         SysLog(NID_APP, "Succeeded in connecting service(%s)", IPC_SERVER_NAME);
101                         __pIpcClient = pIpcClient.release();
102                         return E_SUCCESS;
103                 }
104
105                 SysTryReturn(NID_APP, ++count < MAX_TRY_COUNT, E_SYSTEM, r, "[%s] Failed to connect service.(%s)", GetErrorMessage(r), IPC_SERVER_NAME);
106
107                 Tizen::Base::Runtime::Thread::Sleep(TRY_SLEEP_TIME);
108         }
109
110         SysLog(NID_SHELL, "Exit.");
111         return E_SUCCESS;
112 }
113
114 result
115 _AppWidgetManagerProxy::RequestUpdate(const Tizen::App::AppId& appId, const Tizen::Base::String& providerName, const Tizen::Base::String& argument)
116 {
117         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
118         SysLog(NID_SHELL, "Enter.");
119 //      String providerId;
120 //      if( providerName.IsEmpty() == true)
121 //      {
122 //              providerId = appId;
123 //      }
124 //      else
125 //      {
126 //              providerId = appId + "." + providerName;
127 //      }
128
129         result ret = E_SUCCESS;
130         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestUpdate(appId, providerName, argument, &ret));
131         result r = __pIpcClient->SendRequest(*pMsg.get());
132         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
133
134         return ret;
135 }
136
137 result
138 _AppWidgetManagerProxy::RequestUpdateInstance(const Tizen::Base::String& instanceId, const Tizen::Base::String& argument)
139 {
140         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
141         SysLog(NID_SHELL, "Enter.");
142
143         result ret = E_SUCCESS;
144         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestUpdateInstance(instanceId, argument, &ret));
145         result r = __pIpcClient->SendRequest(*pMsg.get());
146         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
147
148         return ret;
149 }
150
151 result
152 _AppWidgetManagerProxy::RequestSharedMemoryId(const Tizen::Base::String& instanceId, int width, int height, int& shmId )
153 {
154         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
155
156         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestSharedMemoryId(instanceId, width, height, &shmId));
157         result r = __pIpcClient->SendRequest(*pMsg.get());
158         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
159         SysLog(NID_SHELL, "shmId(%d) is retrieved from appwidget-service", shmId);
160
161         return E_SUCCESS;
162 }
163
164 result
165 _AppWidgetManagerProxy::RequestSharedMemoryIdForPD(const Tizen::Base::String& instanceId, int width, int height, int& shmId )
166 {
167         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
168
169         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestSharedMemoryIdForPD(instanceId, width, height, &shmId));
170         result r = __pIpcClient->SendRequest(*pMsg.get());
171         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest is failed.");
172         SysLog(NID_SHELL, "shmId(%d) is retrieved from appwidget-service", shmId);
173
174         return E_SUCCESS;
175 }
176
177 result
178 _AppWidgetManagerProxy::RequestSyncSharedMemory(const Tizen::Base::String& instanceId, int width, int height)
179 {
180         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
181         SysLog(NID_SHELL, "");
182
183         result ret = E_FAILURE;
184         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestSyncSharedMemory(instanceId, width, height, &ret));
185         result r = __pIpcClient->SendRequest(*pMsg.get());
186         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
187         return E_SUCCESS;
188 }
189
190 result
191 _AppWidgetManagerProxy::RequestSyncSharedMemoryForPD(const Tizen::Base::String& instanceId)
192 {
193         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
194         SysLog(NID_SHELL, "");
195
196         result ret = E_FAILURE;
197         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestSyncSharedMemoryForPD(instanceId, &ret));
198         result r = __pIpcClient->SendRequest(*pMsg.get());
199         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
200         return E_SUCCESS;
201 }
202
203 result
204 _AppWidgetManagerProxy::RequestReleaseSharedMemory(const Tizen::Base::String& instanceId)
205 {
206         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
207         SysLog(NID_SHELL, "");
208
209         result ret = E_FAILURE;
210         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestReleaseSharedMemory(instanceId, &ret));
211         result r = __pIpcClient->SendRequest(*pMsg.get());
212         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
213         return E_SUCCESS;
214 }
215
216 result
217 _AppWidgetManagerProxy::RequestReleaseSharedMemoryForPD(const Tizen::Base::String& instanceId)
218 {
219         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
220         SysLog(NID_SHELL, "");
221
222         result ret = E_FAILURE;
223         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestReleaseSharedMemoryForPD(instanceId, &ret));
224         result r = __pIpcClient->SendRequest(*pMsg.get());
225         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
226         return E_SUCCESS;
227 }
228
229 result
230 _AppWidgetManagerProxy::RequestProviderCount(const Tizen::Base::String& instanceId, int &count)
231 {
232         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
233         SysLog(NID_SHELL, "");
234
235         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestProviderCount(instanceId, &count));
236         result r = __pIpcClient->SendRequest(*pMsg.get());
237         SysTryReturnResult(NID_SHELL, !IsFailed(r), r, "SendRequest failed.");
238
239         return E_SUCCESS;
240 }
241
242 result
243 _AppWidgetManagerProxy::SendResult(const Tizen::Base::String& instanceId, bool isSucceeded)
244 {
245         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
246         SysLog(NID_SHELL, "");
247
248         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_SendResult(instanceId, isSucceeded));
249         result r = __pIpcClient->SendRequest(*pMsg.get());
250         SysTryReturnResult(NID_SHELL, !IsFailed(r), r, "SendRequest failed.");
251
252         return E_SUCCESS;
253 }
254
255 result
256 _AppWidgetManagerProxy::SendAccessStatus(const Tizen::Base::String& instanceId, int accessStatus)
257 {
258         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
259         SysLog(NID_SHELL, "");
260
261         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_SendAccessStatus(instanceId, accessStatus));
262         result r = __pIpcClient->SendRequest(*pMsg.get());
263         SysTryReturnResult(NID_SHELL, !IsFailed(r), r, "SendRequest failed.");
264
265         return E_SUCCESS;
266 }
267
268 result
269 _AppWidgetManagerProxy::OnTouchEventReceived(const Tizen::Base::String& instanceId, int eventType, double timeStamp, double x, double y )
270 {
271         return _AppWidgetProviderManagerImpl::GetInstance()->ForwardTouchEvent(instanceId, eventType, timeStamp, x, y);
272 }
273
274 result
275 _AppWidgetManagerProxy::OnAppWidgetEventReceived(const String& instanceId, const String& operationId, const HashMap& args)
276 {
277         return _AppWidgetProviderManagerImpl::GetInstance()->HandleAppWidgetRequest(operationId, &args, true);
278 }
279
280 result
281 _AppWidgetManagerProxy::OnTouchEventReceivedForPD(const Tizen::Base::String& instanceId, int eventType, double timeStamp, double x, double y )
282 {
283         return _AppWidgetProviderManagerImpl::GetInstance()->ForwardTouchEventForPD(instanceId, eventType, timeStamp, x, y);
284 }
285
286 void
287 _AppWidgetManagerProxy::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
288 {
289         SysLog(NID_SHELL, "Enter.");
290         IPC_BEGIN_MESSAGE_MAP(_AppWidgetManagerProxy, message)
291                 IPC_MESSAGE_HANDLER_EX(AppWidgetManager_SendAppWidgetEvent, &client, OnAppWidgetEventReceived)
292                 IPC_MESSAGE_HANDLER_EX(AppWidgetManager_SendTouchEvent, &client, OnTouchEventReceived)
293                 IPC_MESSAGE_HANDLER_EX(AppWidgetManager_SendTouchEventForPD, &client, OnTouchEventReceivedForPD )
294         IPC_END_MESSAGE_MAP()
295
296         SysLog(NID_SHELL, "Exit.");
297 }
298
299 bool
300 _AppWidgetManagerProxy::IsConnected(void) const
301 {
302         return (__pIpcClient != null);
303 }
304
305 void
306 _AppWidgetManagerProxy::OnIpcServerDisconnected(_IpcClient& client)
307 {
308         SysLog(NID_SHELL, "IPC Server[%ls] is disconnected.", client.GetName().GetPointer());
309
310         delete __pIpcClient;
311         __pIpcClient = null;
312 }
313
314 }} // Tizen::Shell