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