AppControl 2.0/2.1 argument handling
[framework/osp/app-controls.git] / src / setting-app-control / SettingAppControlDllEntry.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         SettingAppControlDllEntry.cpp
20  * @brief       This is the implementation for the SettingAppControlDllEntry.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 <FApp_AppControlManager.h>
30 #include <FApp_AppMessageImpl.h>
31 #include <FSys_SystemInfoImpl.h>
32 #include <FApp_Aul.h>
33 #include <FApp_AppArg.h>
34
35
36 using namespace Tizen::App;
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39
40 const String CATEGORY_LOCATION = L"Location";
41 const String LOCATION_GPS_ENABLED = L"GPSEnabled";
42 const String LOCATION_WPS_ENABLED = L"WPSEnabled";
43
44 const String CATEGORY_BLUETOOTH_VISIBILITY = L"Bluetooth.Visibility";
45 const String BLUETOOTH_VISIBILITY_OFF = L"GPSEnabled";
46 const String BLUETOOTH_VISIBILITY_ALWAYS_ON = L"GPSEnabled";
47 const String BLUETOOTH_VISIBILITY_1MIN_ON = L"GPSEnabled";
48 const String BLUETOOTH_VISIBILITY_2MIN_ON = L"GPSEnabled";
49
50 const String CATEGORY_FONT_TYPE = L"FontType";
51 const String FONT_TYPE_DEFAULT = L"Unknown";
52
53 const String CATEGORY_FONT_SIZE = L"FontSize";
54 const String FONT_SIZE_SMALL = L"Small";
55 const String FONT_SIZE_MEDIUM = L"Medium";
56 const String FONT_SIZE_LARGE = L"Large";
57
58 static int __req = -1;
59
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63
64 result _OSP_EXPORT_ StartAppControl(int req, const String&, const String&, const String*, const String*, const IMap*);
65 result _OSP_EXPORT_ TerminateAppControl(int req);
66 void OnLocationAppControlResult(void*, int, service_result_e, void*);
67 void OnBluetoothAppControlResult(void*, int, service_result_e, void*);
68 void OnFontSizeAppControlResult(void*, int, service_result_e, void*);
69 void OnFontTypeAppControlResult(void*, int, service_result_e, void*);
70
71 static const char __allowedLegacyAppControlTable[][2][96] =
72 {
73         {"osp.appcontrol.SETTINGS", "osp.appcontrol.operation.DEFAULT"},
74         {"osp.appcontrol.provider.settings", "osp.appcontrol.operation.configure"},
75         {"tizen.settings", "http://tizen.org/appcontrol/operation/configure"},
76 };
77
78 static const char __allowedLocationAppControlTable[][2][96] =
79 {
80         {"tizen.settings", "http://tizen.org/appcontrol/operation/configure/location"},
81 };
82
83 static const char __allowedBluetoothAppControlTable[][2][96] =
84 {
85         {"tizen.settings", "http://tizen.org/appcontrol/operation/configure/bluetooth/visibility"},
86 };
87
88 static const char __allowedFontSizeAppControlTable[][2][96] =
89 {
90         {"tizen.settings", "http://tizen.org/appcontrol/operation/configure/font/size"},
91 };
92
93 static const char __allowedFontTypeAppControlTable[][2][96] =
94 {
95         {"tizen.settings", "http://tizen.org/appcontrol/operation/configure/font/type"},
96 };
97
98
99 static int __processId = -1;
100
101 result
102 StartAppControl(int req, const String& aId, const String& oId, const String* pUri, const String* pMime, const IMap* pMap)
103 {
104         SysLog(NID_APP, "StartAppControl: Entry to Setting AppControl");
105
106         result r = E_SUCCESS;
107
108         const bool isLegacy = _AppControlManager::IsAllowedAppControl(__allowedLegacyAppControlTable, 3, aId, oId);
109         bool isLocation = _AppControlManager::IsAllowedAppControl(__allowedLocationAppControlTable, 1, aId, oId);
110         bool isBluetooth = _AppControlManager::IsAllowedAppControl(__allowedBluetoothAppControlTable, 1, aId, oId);
111         bool isFontSize = _AppControlManager::IsAllowedAppControl(__allowedFontSizeAppControlTable, 1, aId, oId);
112         bool isFontType = _AppControlManager::IsAllowedAppControl(__allowedFontTypeAppControlTable, 1, aId, oId);
113         SysTryReturnResult(NID_APP, isLegacy || isLocation || isBluetooth || isFontSize || isFontType, E_SYSTEM, "Invalid AppControl entry for (%ls, %ls).", aId.GetPointer(), oId.GetPointer());
114
115
116         SysLog(NID_APP, "Setting AppControl.");
117
118         _AppMessageImpl msg;
119         msg.AddData(pMap);
120
121         AppSvcResFn pCb = NULL;
122
123         String operation;
124
125         if (isLegacy)
126         {
127                 String category = msg.GetValue(L"category");
128                 if (category == CATEGORY_LOCATION)
129                 {
130                         operation = L"http://tizen.org/appcontrol/operation/configure/location";
131                         isLocation = true;
132                 }
133                 else if (category == CATEGORY_BLUETOOTH_VISIBILITY)
134                 {
135                         operation = L"http://tizen.org/appcontrol/operation/configure/bluetooth/visibility";
136                         isBluetooth = true;
137                 }
138                 else if (category == CATEGORY_FONT_TYPE)
139                 {
140                         operation = L"http://tizen.org/appcontrol/operation/configure/font/type";
141                         isFontType = true;
142                 }
143                 else if (category == CATEGORY_FONT_SIZE)
144                 {
145                         operation = L"http://tizen.org/appcontrol/operation/configure/font/size";
146                         isFontSize = true;
147                 }
148                 else
149                 {
150                         SysLog(NID_APP, "Unknown category %ls for legacy AppControl.", category.GetPointer());
151                 }
152         }
153         else
154         {
155                 operation = oId;
156         }
157
158         AppId providerAppId;
159
160         if (isLocation)
161         {
162                 pCb = OnLocationAppControlResult;
163                 providerAppId = "tizen.setting_location";
164         }
165         else if (isBluetooth)
166         {
167                 pCb = OnBluetoothAppControlResult;
168                 providerAppId = "tizen.setting_bluetooth";
169         }
170         else if (isFontSize)
171         {
172                 pCb = OnFontSizeAppControlResult;
173                 providerAppId = "tizen.setting_font";
174         }
175         else if (isFontType)
176         {
177                 pCb = OnFontTypeAppControlResult;
178                 providerAppId = "tizen.setting_font";
179         }
180         else
181         {
182                 providerAppId = aId;
183         }
184         
185         const String& package = _AppControlManager::GetAliasAppId(providerAppId);
186         SysLog(NID_APP, "Actual packageId is %ls.", package.GetPointer());
187
188         __req = req;
189         __processId = _AppControlManager::GetInstance()->Launch(msg, package, operation, pUri, pMime, pCb, 0);
190
191         SysTryReturnResult(NID_APP, __processId >= 0, E_SYSTEM, "StartAppControl: Launching Camera AppControl is failed.");
192
193         r = E_SUCCESS;
194         SysLog(NID_APP, "StartAppControl: Launching Camera AppControl succeeded");
195
196         return r;
197 }
198
199 result
200 TerminateAppControl(int req)
201 {
202         if (__processId >= 0)
203         {
204                 _Aul::TerminateApplicationByPid(__processId);           
205         }
206         return E_SUCCESS;
207 }
208
209 void
210 OnLocationAppControlResult(void* b, int requestCode, service_result_e res, void* userData)
211 {
212         SysLog(NID_APP, "Setting AppControl result received.");
213
214         result r = E_SYSTEM;
215         const char* pBuf = NULL;
216         bundle* pBundle = static_cast<bundle*>(b);
217
218         HashMap* pResult = new (std::nothrow) HashMap();
219         SysTryCatch(NID_APP, pResult != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
220
221         r = pResult->Construct();
222
223         _AppArg::SetArgMap(pBundle, pResult);
224
225         pResult->Add(new (std::nothrow) String(L"category"), new (std::nothrow) String(L"Location"));
226
227         pBuf = appsvc_get_data(pBundle, "http://tizen.org/appcontrol/data/location/gps");
228         if (pBuf)
229         {
230                 pResult->Add(new (std::nothrow) String(L"GPS"), new (std::nothrow) String(pBuf));
231         }
232
233         pBuf = appsvc_get_data(pBundle, "http://tizen.org/appcontrol/data/location/wps");
234         if (pBuf)
235         {
236                 pResult->Add(new (std::nothrow) String(L"WPS"), new (std::nothrow) String(pBuf));
237         }
238
239         _AppControlManager::GetInstance()->FinishAppControl(__req, res, pResult);
240
241 CATCH:
242         __req = -1;
243 }
244
245 void
246 OnBluetoothAppControlResult(void* b, int requestCode, service_result_e res, void* userData)
247 {
248         result r = E_SYSTEM;
249         const char* pBuf = NULL;
250         bundle* pBundle = static_cast<bundle*>(b);
251
252         HashMap* pResult = new (std::nothrow) HashMap();
253         SysTryCatch(NID_APP, pResult != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
254
255         r = pResult->Construct();
256
257         _AppArg::SetArgMap(pBundle, pResult);
258
259         pResult->Add(new (std::nothrow) String(L"category"), new (std::nothrow) String(L"Bluetooth.Visibility"));
260
261         pBuf = appsvc_get_data(pBundle, "http://tizen.org/appcontrol/data/bluetooth/visibility");
262         if (pBuf)
263         {
264                 switch (*pBuf)
265                 {
266                         case '0':
267                                 pBuf = "off";
268                                 break;
269                         case '1':
270                                 pBuf = "always";
271                                 break;
272                         case '2':
273                                 pBuf = "time_limited";
274                                 break;
275                         default:
276                                 pBuf = "off";
277                                 break;
278                 }
279                 pResult->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/bluetooth/visibility"), new (std::nothrow) String(pBuf));
280                 pResult->Add(new (std::nothrow) String(L"visibility"), new (std::nothrow) String(pBuf));
281         }
282
283         _AppControlManager::GetInstance()->FinishAppControl(__req, res, pResult);
284
285 CATCH:
286         __req = -1;
287 }
288
289 void
290 OnFontSizeAppControlResult(void* b, int requestCode, service_result_e res, void* userData)
291 {
292         SysLog(NID_APP, "Setting AppControl result received.");
293
294         result r = E_SYSTEM;
295         const char* pBuf = NULL;
296         bundle* pBundle = static_cast<bundle*>(b);
297
298         HashMap* pResult = new (std::nothrow) HashMap();
299         SysTryCatch(NID_APP, pResult != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
300
301         r = pResult->Construct();
302
303         _AppArg::SetArgMap(pBundle, pResult);
304
305         pResult->Add(new (std::nothrow) String(L"category"), new (std::nothrow) String(L"FontSize"));
306
307         pBuf = appsvc_get_data(pBundle, "http://tizen.org/appcontrol/data/font/size");
308         if (pBuf)
309         {
310                 pResult->Add(new (std::nothrow) String(L"size"), new (std::nothrow) String(pBuf));
311         }
312
313         _AppControlManager::GetInstance()->FinishAppControl(__req, res, pResult);
314
315 CATCH:
316         __req = -1;
317 }
318
319
320 void
321 OnFontTypeAppControlResult(void* b, int requestCode, service_result_e res, void* userData)
322 {
323         SysLog(NID_APP, "Setting AppControl result received.");
324
325         result r = E_SYSTEM;
326         const char* pBuf = NULL;
327         bundle* pBundle = static_cast<bundle*>(b);
328
329         HashMap* pResult = new (std::nothrow) HashMap();
330         SysTryCatch(NID_APP, pResult != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
331
332         r = pResult->Construct();
333
334         _AppArg::SetArgMap(pBundle, pResult);
335
336         pResult->Add(new (std::nothrow) String(L"category"), new (std::nothrow) String(L"FontType"));
337
338         pBuf = appsvc_get_data(pBundle, "http://tizen.org/appcontrol/data/font/type");
339         if (pBuf)
340         {
341                 pResult->Add(new (std::nothrow) String(L"type"), new (std::nothrow) String(pBuf));
342         }
343
344         _AppControlManager::GetInstance()->FinishAppControl(__req, res, pResult);
345
346 CATCH:
347         __req = -1;
348 }
349
350 #ifdef __cplusplus
351 }
352 #endif