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