AppControl 2.0/2.1 argument handling
[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
54
55 result _OSP_EXPORT_ StartAppControl(int req, const String&, const String&, const String*, const String*, const IMap*);
56 result _OSP_EXPORT_ TerminateAppControl(int req);
57 void OnAppControlResult(void*, int, service_result_e, void*);
58 void OnAppControlResultTizen(void*, int, service_result_e, void*);
59
60 static int __req = -1;
61 static int __processId = -1;
62
63 static const char __allowedAppControlPickTable[][2][96] =
64 {
65         {"osp.appcontrol.CALENDAR", "osp.appcontrol.operation.PICK"},
66         {"osp.appcontrol.provider.calendar", "osp.appcontrol.operation.pick"},
67         {"tizen.calendar", "http://tizen.org/appcontrol/operation/pick"},
68         {"tizen.calendar", "http://tizen.org/appcontrol/operation/social/pick"},
69 };
70
71 static const char __allowedAppControlViewTable[][2][96] =
72 {
73         {"osp.appcontrol.CALENDAR", "osp.appcontrol.operation.VIEW"},
74         {"osp.appcontrol.provider.calendar", "osp.appcontrol.operation.view"},
75         {"tizen.calendar", "http://tizen.org/appcontrol/operation/social/view"},
76 };
77
78 static const char __allowedAppControlEditTable[][2][96] =
79 {
80         {"tizen.calendar", "http://tizen.org/appcontrol/operation/social/edit"},
81 };
82
83 static const char __allowedAppControlVcsViewTable[][2][96] =
84 {
85         {"tizen.calendar", "http://tizen.org/appcontrol/operation/view"},
86 };
87
88 result
89 StartAppControl(int req, const String& aId, const String& oId, const String* pUri, const String* pMime, const IMap* pMap)
90 {
91         SysLog(NID_APP, "StartAppControl: Entry to Calendar AppControl");
92
93         result r = E_SUCCESS;
94         bool hasOutput = false;
95
96         __req = req;
97
98         const bool isCalendarVcsView = _AppControlManager::IsAllowedAppControl(__allowedAppControlVcsViewTable, 1, aId, oId);
99         const bool isCalendarView = _AppControlManager::IsAllowedAppControl(__allowedAppControlViewTable, 3, aId, oId);
100         const bool isCalendarPick = _AppControlManager::IsAllowedAppControl(__allowedAppControlPickTable, 4, aId, oId);
101         const bool isCalendarEdit = _AppControlManager::IsAllowedAppControl(__allowedAppControlEditTable, 1, aId, oId);
102
103         SysTryReturnResult(NID_APP, isCalendarPick || isCalendarEdit || isCalendarView || isCalendarVcsView, E_SYSTEM, "Invalid AppControl entry for (%ls, %ls).", aId.GetPointer(), oId.GetPointer());
104
105         _AppMessageImpl msg;
106         msg.AddData(pMap);
107
108         AppSvcResFn pCb = NULL;
109
110         String package;
111         String operation;
112         String uri;
113         const String* pActualUri = pUri;
114
115         if (isCalendarPick)
116         {
117                 SysLog(NID_APP, "Calendar AppControl : PICK operation.");
118
119                 hasOutput = true;
120
121 #if 0
122                 const String* pItemTypeValue = static_cast<const String*>(pMap->GetValue(String(L"itemType")));
123                 if (pItemTypeValue == null || (*pItemTypeValue != L"event" && *pItemTypeValue != L"todo"))
124                 {
125                         SysLog(NID_APP, "itemType is invalid.");
126                         r = E_SUCCESS;
127                         goto CATCH;
128                 }
129                 const String* pSelectionModeValue = static_cast<const String*>(pMap->GetValue(String(L"selectionMode")));
130                 if (pSelectionModeValue == null || (*pSelectionModeValue != L"single" && *pSelectionModeValue != L"multiple"))
131                 {
132                         SysLog(NID_APP, "selectionMode is invalid.");
133                         r = E_SUCCESS;
134                         goto CATCH;
135                 }
136 #endif
137
138                 pCb = OnAppControlResultTizen;
139                 operation = L"http://tizen.org/appcontrol/operation/social/pick";
140
141                 msg.AddData(CALENDAR_ITEM_TYPE, L"event");
142
143                 const String& tmp = msg.GetValue(L"itemType");
144                 if (!tmp.IsEmpty())
145                 {
146                         msg.AddData(CALENDAR_RESULT_TYPE, tmp);
147                 }
148         }
149         else if (isCalendarEdit)
150         {
151                 SysLog(NID_APP, "Calendar AppControl : EDIT operation.");
152                 
153                 hasOutput = true;
154                 pCb = OnAppControlResultTizen;
155                 operation = L"http://tizen.org/appcontrol/operation/social/edit";
156
157                 msg.AddData(CALENDAR_ITEM_TYPE, L"event");
158         }
159         else if (isCalendarVcsView || isCalendarView)
160         {
161                 if (pUri == null)
162                 {
163                         const String& path = msg.GetValue(L"path");
164                         if (!path.IsEmpty())
165                         {
166                                 uri = path;
167                                 pActualUri = &uri;
168                         }
169                 }
170
171                 if (pActualUri)
172                 {
173                         SysLog(NID_APP, "Calendar AppControl : VIEW operation (vcs).");
174                         operation = L"http://tizen.org/appcontrol/operation/view";
175                 }
176                 else
177                 {
178                         SysLog(NID_APP, "Calendar AppControl : VIEW operation.");
179
180                         operation = L"http://tizen.org/appcontrol/operation/social/view";
181
182                         msg.AddData(CALENDAR_ITEM_TYPE, L"event");
183
184                         const String& tmp = msg.GetValue(L"eventId");
185                         if (!tmp.IsEmpty())
186                         {
187                                 msg.AddData(CALENDAR_ITEM_ID, tmp);
188                         }
189                 }
190         }
191         else
192         {
193                 operation = oId;
194         }
195
196         package = _AppControlManager::GetAliasAppId(aId);
197         SysLog(NID_APP, "Actual packageId is %ls.", package.GetPointer());
198
199         __req = req; 
200         __processId = _AppControlManager::GetInstance()->Launch(msg, package, operation, pActualUri, pMime, pCb, 0);
201
202         SysTryReturnResult(NID_APP, __processId >= 0, E_SYSTEM, "StartAppControl: Launching Calendar AppControl is failed.");
203
204         SysLog(NID_APP, "StartAppControl: Launching Calendar AppControl succeeded");
205
206         return E_SUCCESS;
207
208 CATCH:
209
210         if (hasOutput)
211         {
212                 _AppControlManager::GetInstance()->FinishAppControl(req, APP_CTRL_RESULT_FAILED, null);
213         }
214
215         __req = -1;
216         return r;
217 }
218
219 result
220 TerminateAppControl(int req)
221 {
222         if (__processId >= 0)
223         {
224                 _Aul::TerminateApplicationByPid(__processId);           
225         }
226         return E_SUCCESS;
227 }
228
229 void
230 OnAppControlResult(void* b, int requestCode, service_result_e res, void* userData)
231 {
232         result r = E_SYSTEM;
233         bundle* pBundle = static_cast<bundle*>(b);
234
235         HashMap* pResult = null;
236
237         switch (res)
238         {
239         case SERVICE_RESULT_SUCCEEDED:
240                 {
241                         pResult = new (std::nothrow) HashMap();
242                         SysTryCatch(NID_APP, pResult != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
243
244                         r = pResult->Construct();
245
246                         const char* pData = appsvc_get_data(pBundle, "itemType");
247                         if (pData)
248                         {
249                                 pResult->Add(new (std::nothrow) String(L"itemType"), new (std::nothrow) String(pData));
250                         }
251
252                         int count = 0;
253                         const char** pPaths = null;
254                         pPaths = appsvc_get_data_array(pBundle, "path", &count);
255
256                         if (pPaths)
257                         {
258                                 String* pPathValue = new (std::nothrow) String();
259                                 SysTryCatch(NID_APP, pPathValue != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
260
261                                 for (int i = 0; i< count; i++)
262                                 {
263                                         if (i != 0)
264                                         {
265                                                 pPathValue->Append(L";");
266                                         }
267
268                                         pPathValue->Append(pPaths[i]);
269                                 }
270
271                                 pResult->Add(new (std::nothrow) String(L"path"), pPathValue);
272                         }
273                 }
274                 break;
275         case SERVICE_RESULT_FAILED:
276                 break;
277 #if 0
278         case SERVICE_RESULT_CANCELED:
279                 pResult->Add(*(new (std::nothrow) String(L"Canceled")));
280                 break;
281 #endif
282         default:
283                 break;
284         }
285
286         _AppControlManager::GetInstance()->FinishAppControl(__req, res, pResult);
287
288 CATCH:
289         __req = -1;
290
291         if (pResult)
292         {
293                 pResult->RemoveAll(true);
294                 delete pResult;
295         }
296
297 }
298
299 void
300 OnAppControlResultTizen(void* b, int requestCode, service_result_e res, void* userData)
301 {
302         result r = E_SYSTEM;
303         bundle* pBundle = static_cast<bundle*>(b);
304
305         HashMap* pResult = new (std::nothrow) HashMap();
306         SysTryCatch(NID_APP, pResult != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
307
308         r = pResult->Construct();
309
310         _AppArg::SetArgMap(pBundle, pResult);
311
312         _AppControlManager::GetInstance()->FinishAppControl(__req, res, pResult);
313
314 CATCH:
315         __req = -1;
316 }
317
318
319 #ifdef __cplusplus
320 }
321 #endif