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