e10f1ca1955ec5263cb7bae44b0bd45168246633
[framework/osp/app-controls.git] / src / media-app-control / MediaAppControlDLLEntry.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         MediaAppControlDllEntry.cpp
20  * @brief       This is the implementation for the MediaAppControlDllEntry.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 <FApp_AppControlManager.h>
31 #include <FApp_AppMessageImpl.h>
32 #include <FApp_Aul.h>
33
34 #include "ParseUtil.h"
35
36 using namespace Tizen::App;
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Base::Utility;
40
41 #define _DUMMY_APPCONTROL
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 result _OSP_EXPORT_ StartAppControl(int req, const String&, const String&, const String*, const String*, const IMap*);
48 result _OSP_EXPORT_ TerminateAppControl(int req);
49 void OnAppControlResult(void*, int, service_result_e, void*);
50 void OnAppControlResultGallery(void*, int, service_result_e, void*);
51
52 static int __req = -1;
53 static int __processId = -1;
54
55 static const wchar_t __allowedAppControlImageTable[][2][64] =
56 {
57         {L"osp.appcontrol.IMAGE", L"osp.appcontrol.operation.VIEW"},
58         {L"osp.appcontrol.provider.image", L"osp.appcontrol.operation.view"},
59         {L"http://tizen.org/appcontrol/provider/image", L"http://tizen.org/appcontrol/operation/view"},
60         {L"tizen.imageviewer", L"http://tizen.org/appcontrol/operation/view"},
61 };
62
63 static const wchar_t __allowedAppControlVideoTable[][2][64] =
64 {
65         {L"osp.appcontrol.VIDEO", L"osp.appcontrol.operation.PLAY"},
66         {L"osp.appcontrol.provider.video", L"osp.appcontrol.operation.play"},
67         {L"http://tizen.org/appcontrol/provider/video", L"http://tizen.org/appcontrol/operation/play"},
68         {L"tizen.videoplayer", L"http://tizen.org/appcontrol/operation/view"},
69 };
70
71 static const wchar_t __allowedAppControlAudioTable[][2][64] =
72 {
73         {L"osp.appcontrol.AUDIO", L"osp.appcontrol.operation.PLAY"},
74         {L"osp.appcontrol.provider.audio", L"osp.appcontrol.operation.play"},
75         {L"http://tizen.org/appcontrol/provider/audio", L"http://tizen.org/appcontrol/operation/play"},
76         {L"tizen.musicplayer", L"http://tizen.org/appcontrol/operation/view"},
77 };
78
79 static const wchar_t __allowedAppControlMediaTable[][2][64] =
80 {
81         {L"osp.appcontrol.MEDIA", L"osp.appcontrol.operation.PICK"},
82         {L"osp.appcontrol.provider.media", L"osp.appcontrol.operation.pick"},
83         {L"http://tizen.org/appcontrol/provider/media", L"http://tizen.org/appcontrol/operation/pick"},
84         {L"tizen.filemanager", L"http://tizen.org/appcontrol/operation/pick"},
85 };
86
87
88 result
89 StartAppControl(int req, const String& aId, const String& oId, const String* pUriData, const String* pMime, const IMap* pMap)
90 {
91         result r = E_SYSTEM;
92         SysLog(NID_APP, "StartAppControl: Entry to Media AppControl");
93
94         const bool isImage = _AppControlManager::IsAllowedAppControl(__allowedAppControlImageTable, 4, aId, oId);
95         const bool isVideo = _AppControlManager::IsAllowedAppControl(__allowedAppControlVideoTable, 4, aId, oId);
96         const bool isAudio = _AppControlManager::IsAllowedAppControl(__allowedAppControlAudioTable, 4, aId, oId);
97         const bool isMedia = _AppControlManager::IsAllowedAppControl(__allowedAppControlMediaTable, 4, aId, oId);
98
99         if (isImage)
100         {
101                 char* pPath = null;
102                 SysTryCatch(NID_APP, pMap != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The parameter is empty");
103
104                 _AppMessageImpl msg;
105                 msg.AddData(pMap);
106
107                 const String& type = msg.GetValue(KEY_TYPE);
108                 String value = msg.GetValue(KEY_PATH);
109
110                 if (!value.IsEmpty())
111                 {
112                         if (!value.StartsWith(L"file://", 0))
113                         {
114                                 SysLog(NID_APP, "Inserting file scheme for %ls.", value.GetPointer());
115                                 value.Insert(L"file://", 0);
116                         }
117                         pPath = _StringConverter::CopyToCharArrayN(value);              // Store the path
118                 }
119
120                 SysLog(NID_APP, "type(%ls), path(%ls)", type.GetPointer(), value.GetPointer());
121
122                 __processId = _AppControlManager::GetInstance()->Launch(msg, "image-viewer-efl", APPSVC_OPERATION_VIEW, NULL, pPath, NULL, 0);
123
124                 delete [] pPath;
125                 SysTryReturnResult(NID_APP, __processId >= 0, E_SYSTEM, "StartAppControl: Launching Image AppControl is failed.");
126                 r = E_SUCCESS;
127         }
128         else if (isAudio)
129         {
130                 char* pPath = null;
131                 char* pUri = null;
132                 SysTryCatch(NID_APP, pMap != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The parameter is empty");
133
134                 _AppMessageImpl msg;
135                 msg.AddData(pMap);
136
137                 String tmp = msg.GetValue(L"path");
138                 if (!tmp.IsEmpty())
139                 {
140                         if (tmp.StartsWith(L"http://", 0)
141                                         || tmp.StartsWith(L"rtsp://", 0)
142                                         || tmp.StartsWith(L"https://", 0))
143                         {
144                                 // URI
145                                 pUri = _StringConverter::CopyToCharArrayN(tmp);
146                                 int i = 0;
147                                 r = tmp.LastIndexOf(L'.', tmp.GetLength() - 1, i);
148                                 if (r == E_SUCCESS)
149                                 {
150                                         String extension;
151                                         tmp.SubString(i + 1, extension);
152
153                                         String mime;
154                                         r = _AppControlManager::GetMimeFromExt(extension, mime);
155                                         if (!IsFailed(r))
156                                         {
157                                                 if (mime.Equals(L"video/3gpp", true))
158                                                 {
159                                                         SysLog(NID_APP, "Fix-up for audio appcontrol with [video/3gpp].");
160                                                         mime = L"audio/3gpp";
161                                                 }
162                                                 pPath = _StringConverter::CopyToCharArrayN(mime);
163                                                 SysLog(NID_APP, "URI(%s), MIME(%s) for music player", pUri, pPath);
164                                         }
165                                 }
166                         }
167                         else
168                         {
169                                 // normal file path
170                                 pUri = _StringConverter::CopyToCharArrayN(tmp);         // Store the path
171                         }
172                 }
173
174                 // Check the exit on back option.
175                 const String& exit = msg.GetValue(L"ExitOnBack");
176                 if (!exit.IsEmpty())
177                 {
178                         bundle_del(msg.GetBundle(), "__APP_SVC_K_WIN_ID__");
179                 }
180
181                 __processId = _AppControlManager::GetInstance()->Launch(msg, "org.tizen.sound-player", APPSVC_OPERATION_VIEW, pPath, pUri, NULL, 0);
182                 if (__processId <= 0)
183                 {
184                         __processId = _AppControlManager::GetInstance()->Launch(msg, "com.samsung.sound-player", APPSVC_OPERATION_VIEW, pPath, pUri, NULL, 0);
185                 }
186
187                 delete [] pPath;
188                 delete [] pUri;
189                 SysTryReturnResult(NID_APP, __processId >= 0, E_SYSTEM, "StartAppControl: Launching Audio AppControl is failed.");
190                 r = E_SUCCESS;
191         }
192         else if (isVideo)
193         {
194                 char* pPath = null;
195                 SysTryCatch(NID_APP, pMap != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The parameter is empty");
196
197                 _AppMessageImpl msg;
198                 msg.AddData(pMap);
199
200                 String tmp = msg.GetValue(L"path");
201                 if (!tmp.IsEmpty())
202                 {
203                         if (tmp.StartsWith(L"http://", 0)
204                                         || tmp.StartsWith(L"rtsp://", 0)
205                                         || tmp.StartsWith(L"file://", 0)
206                                         || tmp.StartsWith(L"https://", 0))
207                         {
208                                 SysLog(NID_APP, "URI input");
209                         }
210                         else
211                         {
212                                 // [INFO] path should be absolute path
213                                 tmp.Insert(L"file://", 0);
214                         }
215                         pPath = _StringConverter::CopyToCharArrayN(tmp);                // Store the path
216                 }
217
218                 __processId = _AppControlManager::GetInstance()->Launch(msg, "org.tizen.video-player", APPSVC_OPERATION_VIEW, NULL, pPath, NULL, 0);
219                 if (__processId <= 0)
220                 {
221                         __processId = _AppControlManager::GetInstance()->Launch(msg, "com.samsung.video-player", APPSVC_OPERATION_VIEW, NULL, pPath, NULL, 0);
222                 }
223
224                 delete [] pPath;
225                 SysTryReturnResult(NID_APP, __processId >= 0, E_SYSTEM, "StartAppControl: Launching Video AppControl is failed.");      
226                 r = E_SUCCESS;
227         }
228         else if (isMedia)
229         {
230                 const int TYPE_ALL = 0;
231                 const int TYPE_AUDIO = 1;
232                 const int TYPE_VIDEO = 2;
233                 const int TYPE_IMAGE = 3;
234
235                 bool singleSelection = true;
236                 int type = -1;
237
238                 SysTryCatch(NID_APP, pMap != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The parameter is empty");
239
240                 _AppMessageImpl msg;
241                 msg.AddData(pMap);
242
243                 const char* pBuf = appsvc_get_data(msg.GetBundle(), "selectionType");
244                 if (pBuf)
245                 {
246                         singleSelection = !(strncmp(pBuf, "multiple", 9) == 0);
247                 }
248
249                 pBuf = appsvc_get_data(msg.GetBundle(), "type");
250                 if (pBuf)
251                 {
252                         if (strncmp(pBuf, "all", sizeof("all")) == 0)
253                         {
254                                 type = TYPE_ALL;
255                         }
256                         else if (strncmp(pBuf, "image", sizeof("image")) == 0)
257                         {
258                                 type = TYPE_IMAGE;
259                         }
260                         else if (strncmp(pBuf, "video", sizeof("video")) == 0)
261                         {
262                                 type = TYPE_VIDEO;
263                         }
264                         else if (strncmp(pBuf, "audio", sizeof("audio")) == 0)
265                         {
266                                 type = TYPE_AUDIO;
267                         }
268                 }
269
270                 switch (type)
271                 {
272                 case TYPE_ALL:
273                         msg.AddData(L"file_type", L"ALL");
274
275                         msg.AddData(L"select_type", (singleSelection) ? "SINGLE_FILE" : "MULTI_FILE");
276                         __processId = _AppControlManager::GetInstance()->Launch(msg, "myfile-efl", APPSVC_OPERATION_PICK, NULL, NULL, OnAppControlResult, 0);
277
278                         __req = req;
279                         break;
280                 case TYPE_AUDIO:
281                         msg.AddData(L"file_type", L"SOUND");
282                         
283                         msg.AddData(L"select_type", (singleSelection) ? "SINGLE_FILE" : "MULTI_FILE");
284                         __processId = _AppControlManager::GetInstance()->Launch(msg, "myfile-efl", APPSVC_OPERATION_PICK, NULL, NULL, OnAppControlResult, 0);
285
286                         __req = req;
287                         break;
288                 case TYPE_IMAGE:
289                         msg.AddData(L"file-type", L"image");
290
291                         msg.AddData(L"launch-type", (singleSelection) ? "select-one" : "select-multiple");
292                         __processId = _AppControlManager::GetInstance()->Launch(msg, "gallery-efl", APPSVC_OPERATION_PICK, NULL, NULL, OnAppControlResultGallery, 0);
293
294                         __req = req;
295                         break;
296                 case TYPE_VIDEO:
297                         msg.AddData(L"file-type", L"video");
298
299                         msg.AddData(L"launch-type", (singleSelection) ? "select-one" : "select-multiple");
300                         __processId = _AppControlManager::GetInstance()->Launch(msg, "gallery-efl", APPSVC_OPERATION_PICK, NULL, NULL, OnAppControlResultGallery, 0);
301
302                         __req = req;
303                         break;
304                 default:
305                         SysLog(NID_APP, "Wrong type or no type information.");
306                         break;
307                 }
308
309                 SysTryReturnResult(NID_APP, __processId >= 0, E_SYSTEM, "StartAppControl: Launching Media AppControl is failed.");
310                 r = E_SUCCESS;
311         }
312         else if (aId == L"osp.appcontrol.provider.imageeditor")
313         {
314 #if defined (_DUMMY_APPCONTROL)
315                 SysLogException(NID_MEDIA, E_SUCCESS, "NOT IMPLEMENTED");
316                 HashMap* pResult = null;
317                 pResult = new (std::nothrow) HashMap();
318                 SysTryCatch(NID_APP, pResult != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
319
320                 r = pResult->Construct();
321
322                 pResult->Add(new (std::nothrow) String(L"path"), new (std::nothrow) String(L"/opt/media/Images/image2.jpg"));
323                 _AppControlManager::GetInstance()->FinishAppControl(req, 0, pResult);
324                 delete pResult;
325                 r = E_SUCCESS;
326 #endif
327         }
328
329         return r;
330
331 CATCH:
332         return r;
333 }
334
335 result
336 TerminateAppControl(int req)
337 {
338         if (__processId >= 0)
339         {
340                 _Aul::TerminateApplicationByPid(__processId);           
341         }
342         return E_SUCCESS;
343 }
344
345 void
346 OnAppControlResult(void* b, int requestCode, service_result_e res, void* userData)
347 {
348         SysLog(NID_APP, "@@@@@@ OnAppControlResult for myfiles");
349
350         result r = E_SYSTEM;
351         bundle* pBundle = static_cast<bundle*>(b);
352
353         HashMap* pResult = new (std::nothrow) HashMap();
354         SysTryCatch(NID_APP, pResult != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
355
356         r = pResult->Construct();
357
358         switch (res)
359         {
360         case SERVICE_RESULT_SUCCEEDED:
361                 {
362                         const char* const pData = appsvc_get_data(pBundle, "result");
363                         if (pData)
364                         {
365                                 SysLog(NID_APP, "Received data is [%s].", pData);
366
367                                 // [INFO] '?' delimiter for multifile files
368                                 String tmp = pData;
369                                 tmp.Replace(L'?', L';');
370                                 SysLog(NID_APP, "Converted string is [%ls].", tmp.GetPointer());
371                                 pResult->Add(new (std::nothrow) String(L"path"), new (std::nothrow) String(tmp));
372                         }
373                 }
374                 break;
375         case SERVICE_RESULT_FAILED:
376                 break;
377 #if 0
378         case SERVICE_RESULT_CANCELED:
379                 break;
380 #endif
381         default:
382                 break;
383         }
384
385         _AppControlManager::GetInstance()->FinishAppControl(__req, res, pResult);
386
387 CATCH:
388         __req = -1;
389 }
390
391 void
392 OnAppControlResultGallery(void* b, int requestCode, service_result_e res, void* userData)
393 {
394         SysLog(NID_APP, "@@@@@@ OnAppControlResult for gallery");
395
396         // the result from ug does not follow service protocol
397         // success -> ("path", value)
398         // cancel -> ("ug-quit", "normal")
399
400         result r = E_SYSTEM;
401         bundle* pBundle = static_cast<bundle*>(b);
402
403         const char* pData = appsvc_get_data(pBundle, "path");
404
405         if (pData)
406         {
407                 HashMap* pResult = new (std::nothrow) HashMap();
408                 SysTryCatch(NID_APP, pResult != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
409
410                 r = pResult->Construct();
411
412                 SysLog(NID_APP, "Received data is [%s].", pData);
413                 pResult->Add(new (std::nothrow) String(L"path"), new (std::nothrow) String(pData));
414                 _AppControlManager::GetInstance()->FinishAppControl(__req, APP_CTRL_RESULT_SUCCEEDED, pResult);
415         }
416         else
417         {
418                 pData = appsvc_get_data(pBundle, "ug-quit");
419                 SysLog(NID_APP, "The operation cancled or finished (%s)", (pData) ? pData : "no-data");
420                 _AppControlManager::GetInstance()->FinishAppControl(__req, APP_CTRL_RESULT_CANCELED, null);
421         }
422
423 CATCH:
424         __req = -1;
425 }
426
427 #ifdef __cplusplus
428 }
429 #endif