78c0eadd714a03ddc04e586f9529d19b8cce573f
[platform/framework/native/app-controls.git] / src / calendar-app-control / CalendarAppControlDllEntry.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         CalendarAppControlDllEntry.cpp
20  * @brief       This is the implementation for the CalendarAppControlDllEntry.cpp class.
21  */
22
23 #include <unique_ptr.h>
24 #include <appsvc/appsvc.h>
25
26 #include <FBaseSysLog.h>
27 #include <FAppAppControl.h>
28 #include <FBaseColHashMap.h>
29
30 #include <FBase_StringConverter.h>
31 #include <FIo_FileImpl.h>
32 #include <FSys_EnvironmentImpl.h>
33 #include <FApp_AppControlManager.h>
34 #include <FApp_AppMessageImpl.h>
35 #include <FApp_Aul.h>
36 #include <FApp_AppArg.h>
37
38
39 using namespace Tizen::App;
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Io;
43 using namespace Tizen::System;
44
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 static const wchar_t CALENDAR_ITEM_TYPE[] = L"http://tizen.org/appcontrol/data/social/item_type";
51 static const wchar_t CALENDAR_RESULT_TYPE[] = L"http://tizen.org/appcontrol/data/social/result_type";
52 static const wchar_t CALENDAR_ITEM_ID[] = L"http://tizen.org/appcontrol/data/social/item_id";
53 static const wchar_t CALENDAR_SELECTION_MODE[] = L"http://tizen.org/appcontrol/data/selection_mode";
54
55
56 result _OSP_EXPORT_ StartAppControl(int req, const String&, const String&, const String*, const String*, const IMap*);
57 result _OSP_EXPORT_ TerminateAppControl(int req);
58 void OnAppControlResult(void*, int, service_result_e, void*);
59 void OnAppControlResultTizen(void*, int, service_result_e, void*);
60
61 static int __req = -1;
62 static int __processId = -1;
63
64 static const char __allowedAppControlPickTable[][2][96] =
65 {
66         {"osp.appcontrol.CALENDAR", "osp.appcontrol.operation.PICK"},
67         {"osp.appcontrol.provider.calendar", "osp.appcontrol.operation.pick"},
68         {"tizen.calendar", "http://tizen.org/appcontrol/operation/pick"},
69         {"tizen.calendar", "http://tizen.org/appcontrol/operation/social/pick"},
70         {"tizen.todo", "http://tizen.org/appcontrol/operation/social/pick"},
71 };
72
73 static const char __allowedAppControlViewTable[][2][96] =
74 {
75         {"osp.appcontrol.CALENDAR", "osp.appcontrol.operation.VIEW"},
76         {"osp.appcontrol.provider.calendar", "osp.appcontrol.operation.view"},
77         {"tizen.calendar", "http://tizen.org/appcontrol/operation/social/view"},
78         {"tizen.todo", "http://tizen.org/appcontrol/operation/social/view"},
79 };
80
81 static const char __allowedAppControlEditTable[][2][96] =
82 {
83         {"tizen.calendar", "http://tizen.org/appcontrol/operation/social/edit"},
84 };
85
86 static const char __allowedAppControlVcsViewTable[][2][96] =
87 {
88         {"tizen.calendar", "http://tizen.org/appcontrol/operation/view"},
89         {"tizen.todo", "http://tizen.org/appcontrol/operation/view"},
90 };
91
92 result
93 StartAppControl(int req, const String& aId, const String& oId, const String* pUri, const String* pMime, const IMap* pMap)
94 {
95         SysLog(NID_APP, "StartAppControl: Entry to Calendar AppControl");
96
97         result r = E_SUCCESS;
98         bool hasOutput = false;
99
100         __req = req;
101
102         const bool isCalendarVcsView = _AppControlManager::IsAllowedAppControl(__allowedAppControlVcsViewTable, 2, aId, oId);
103         const bool isCalendarView = _AppControlManager::IsAllowedAppControl(__allowedAppControlViewTable, 4, aId, oId);
104         const bool isCalendarPick = _AppControlManager::IsAllowedAppControl(__allowedAppControlPickTable, 5, aId, oId);
105         const bool isCalendarEdit = _AppControlManager::IsAllowedAppControl(__allowedAppControlEditTable, 1, aId, oId);
106
107         SysTryReturnResult(NID_APP, isCalendarPick || isCalendarEdit || isCalendarView || isCalendarVcsView, E_SYSTEM, "Invalid AppControl entry for (%ls, %ls).", aId.GetPointer(), oId.GetPointer());
108
109         _AppMessageImpl msg;
110         msg.AddData(pMap);
111
112         AppSvcResFn pCb = NULL;
113
114         String package;
115         String operation;
116         String uri;
117         const String* pActualUri = pUri;
118
119         if (isCalendarPick)
120         {
121                 SysLog(NID_APP, "Calendar AppControl : PICK operation.");
122
123                 hasOutput = true;
124
125 #if 0
126                 const String* pItemTypeValue = static_cast<const String*>(pMap->GetValue(String(L"itemType")));
127                 if (pItemTypeValue == null || (*pItemTypeValue != L"event" && *pItemTypeValue != L"todo"))
128                 {
129                         SysLog(NID_APP, "itemType is invalid.");
130                         r = E_SUCCESS;
131                         goto CATCH;
132                 }
133                 const String* pSelectionModeValue = static_cast<const String*>(pMap->GetValue(String(L"selectionMode")));
134                 if (pSelectionModeValue == null || (*pSelectionModeValue != L"single" && *pSelectionModeValue != L"multiple"))
135                 {
136                         SysLog(NID_APP, "selectionMode is invalid.");
137                         r = E_SUCCESS;
138                         goto CATCH;
139                 }
140 #endif
141
142                 pCb = OnAppControlResultTizen;
143                 operation = L"http://tizen.org/appcontrol/operation/social/pick";
144
145                 const String& tmpItemType = msg.GetValue(L"itemType");
146                 if (!tmpItemType.IsEmpty())
147                 {
148                         msg.AddData(CALENDAR_ITEM_TYPE, tmpItemType);
149                 }
150                 else if (aId == L"tizen.todo")
151                 {
152                         msg.AddData(CALENDAR_ITEM_TYPE, L"todo");
153                 }
154                 else
155                 {
156                         msg.AddData(CALENDAR_ITEM_TYPE, L"event");
157                 }
158
159                 const String& tmpSelMode = msg.GetValue(L"selectionMode");
160                 if (!tmpSelMode.IsEmpty())
161                 {
162                         msg.AddData(CALENDAR_SELECTION_MODE, tmpSelMode);
163                 }
164
165                 const String& tmpResultType = msg.GetValue(CALENDAR_RESULT_TYPE);
166                 if (tmpResultType.IsEmpty())
167                 {
168                         msg.AddData(CALENDAR_RESULT_TYPE, L"vcs");
169                 }
170
171         }
172         else if (isCalendarEdit)
173         {
174                 SysLog(NID_APP, "Calendar AppControl : EDIT operation.");
175                 
176                 hasOutput = true;
177                 pCb = OnAppControlResultTizen;
178                 operation = L"http://tizen.org/appcontrol/operation/social/edit";
179
180                 msg.AddData(CALENDAR_ITEM_TYPE, L"event");
181         }
182         else if (isCalendarVcsView || isCalendarView)
183         {
184                 if (pUri == null)
185                 {
186                         const String& path = msg.GetValue(L"path");
187                         if (!path.IsEmpty())
188                         {
189                                 uri = path;
190                                 pActualUri = &uri;
191                         }
192                 }
193
194                 if (pActualUri)
195                 {
196                         SysLog(NID_APP, "Calendar AppControl : VIEW operation (vcs).");
197                         operation = L"http://tizen.org/appcontrol/operation/view";
198                 }
199                 else
200                 {
201                         SysLog(NID_APP, "Calendar AppControl : VIEW operation.");
202
203                         operation = L"http://tizen.org/appcontrol/operation/social/view";
204
205                         const String& tmpItemType = msg.GetValue(L"viewType");
206                         if (!tmpItemType.IsEmpty())
207                         {
208                                 msg.AddData(CALENDAR_ITEM_TYPE, tmpItemType);
209                         }
210                         else if (aId == L"tizen.todo")
211                         {
212                                 msg.AddData(CALENDAR_ITEM_TYPE, L"todo");
213                         }
214                         else
215                         {
216                                 msg.AddData(CALENDAR_ITEM_TYPE, L"event");
217                         }
218
219                         const String& tmpEventId = msg.GetValue(L"eventId");
220                         const String& tmpTodoId = msg.GetValue(L"todoId");
221                         if (!tmpEventId.IsEmpty())
222                         {
223                                 msg.AddData(CALENDAR_ITEM_ID, tmpEventId);
224                         }
225                         else if (!tmpTodoId.IsEmpty())
226                         {
227                                 msg.AddData(CALENDAR_ITEM_ID, tmpTodoId);
228                         }
229                 }
230         }
231
232         package = _AppControlManager::GetAliasAppId(aId);
233         SysLog(NID_APP, "Actual app is %ls.", package.GetPointer());
234
235         __req = req; 
236         __processId = _AppControlManager::GetInstance()->Launch(msg, package, operation, pActualUri, pMime, pCb, 0);
237
238         SysTryReturnResult(NID_APP, __processId >= 0, E_SYSTEM, "StartAppControl: Launching Calendar AppControl is failed.");
239
240         SysLog(NID_APP, "StartAppControl: Launching Calendar AppControl succeeded");
241
242         return E_SUCCESS;
243
244 #if 0
245 CATCH:
246
247         if (hasOutput)
248         {
249                 _AppControlManager::GetInstance()->FinishAppControl(req, APP_CTRL_RESULT_FAILED, null);
250         }
251
252         __req = -1;
253         return r;
254 #endif
255 }
256
257 result
258 TerminateAppControl(int req)
259 {
260         if (__processId >= 0)
261         {
262                 _Aul::TerminateApplicationByPid(__processId);           
263         }
264         return E_SUCCESS;
265 }
266
267 void
268 OnAppControlResult(void* b, int requestCode, service_result_e res, void* userData)
269 {
270         result r = E_SYSTEM;
271         bundle* pBundle = static_cast<bundle*>(b);
272
273         HashMap* pResult = null;
274
275         switch (res)
276         {
277         case SERVICE_RESULT_SUCCEEDED:
278                 {
279                         pResult = new (std::nothrow) HashMap();
280                         SysTryCatch(NID_APP, pResult != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
281
282                         r = pResult->Construct();
283
284                         const char* pData = appsvc_get_data(pBundle, "itemType");
285                         if (pData)
286                         {
287                                 pResult->Add(new (std::nothrow) String(L"itemType"), new (std::nothrow) String(pData));
288                         }
289
290                         int count = 0;
291                         const char** pPaths = null;
292                         pPaths = appsvc_get_data_array(pBundle, "path", &count);
293
294                         if (pPaths)
295                         {
296                                 String* pPathValue = new (std::nothrow) String();
297                                 SysTryCatch(NID_APP, pPathValue != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
298
299                                 for (int i = 0; i< count; i++)
300                                 {
301                                         if (i != 0)
302                                         {
303                                                 pPathValue->Append(L";");
304                                         }
305
306                                         pPathValue->Append(pPaths[i]);
307                                 }
308
309                                 pResult->Add(new (std::nothrow) String(L"path"), pPathValue);
310                         }
311                 }
312                 break;
313         case SERVICE_RESULT_FAILED:
314                 break;
315 #if 0
316         case SERVICE_RESULT_CANCELED:
317                 pResult->Add(*(new (std::nothrow) String(L"Canceled")));
318                 break;
319 #endif
320         default:
321                 break;
322         }
323
324         _AppControlManager::GetInstance()->FinishAppControl(__req, res, pResult);
325
326 CATCH:
327         __req = -1;
328
329         if (pResult)
330         {
331                 pResult->RemoveAll(true);
332                 delete pResult;
333         }
334
335 }
336
337 void
338 OnAppControlResultTizen(void* b, int requestCode, service_result_e res, void* userData)
339 {
340         result r = E_SYSTEM;
341         bundle* pBundle = static_cast<bundle*>(b);
342
343         HashMap* pResult = new (std::nothrow) HashMap();
344         SysTryCatch(NID_APP, pResult != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
345
346         r = pResult->Construct();
347
348         _AppArg::SetArgMap(pBundle, pResult);
349
350         _AppControlManager::GetInstance()->FinishAppControl(__req, res, pResult);
351
352 CATCH:
353         __req = -1;
354 }
355
356
357 #ifdef __cplusplus
358 }
359 #endif