Source code formating unification
[platform/framework/web/wrt.git] / src / view / common / application_launcher.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    application_launcher.cpp
18  * @author  Lukasz Wrzosek (l.wrzosek@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for application launcher
21  */
22
23 #include "application_launcher.h"
24 #include <app.h>
25 #include <app_manager.h>
26 #include <download.h>
27 #include <youtubehd.h>
28 #include <dpl/singleton_impl.h>
29 #include <appsvc.h>
30
31 IMPLEMENT_SINGLETON(ApplicationLauncher)
32
33 using namespace WrtDB;
34
35 namespace {
36 const char * const SERVICE_EXTRA_DATA_KEY_PATH = "path";
37 const char * const SERVICE_EXTRA_DATA_KEY_COOKIE = "cookie";
38
39 const char * const SCHEME_TYPE_YOUTUBE = "vnd.youtube";
40 const char * const SCHEME_TYPE_RTSP = "rtsp";
41 const char * const SCHEME_TYPE_HTML5_VIDEO = "html5video";
42 }
43
44 ApplicationLauncher::ApplicationLauncher()
45 {}
46
47 ApplicationLauncher::~ApplicationLauncher()
48 {}
49
50 void ApplicationLauncher::OnEventReceived(
51     const ApplicationLauncherEvents::LaunchApplicationByAppService &event)
52 {
53     int result;
54     bundle *args = event.GetArg0();
55     appsvc_res_fn responseCallback = event.GetArg1();
56     void *userData = event.GetArg2();
57     result = appsvc_run_service(args, 0, responseCallback, userData);
58
59     if (result < 0) {
60         LogError("Failed to run AppService : " << result);
61     }
62 }
63
64 void ApplicationLauncher::OnEventReceived(
65     const ApplicationLauncherEvents::LaunchApplicationByPkgname &event)
66 {
67     using namespace ApplicationLauncherPkgname;
68     LogDebug("LaunchApplicationByPkgname");
69     std::string pkgName(event.GetArg0());
70
71     if (PKG_NAME_DOWNLOAD_PROVIDER == pkgName) {
72         std::string url(event.GetArg1());
73         // This value needs for checking video, music contents later.
74         //std::string mime_type(event.GetArg2());
75         std::string cookie(event.GetArg3());
76
77         if ("null" == url) {
78             LogError("url is empty");
79             return;
80         }
81
82         service_h serviceHandle = NULL;
83         int ret = SERVICE_ERROR_NONE;
84
85         // create service
86         ret = service_create(&serviceHandle);
87         if (SERVICE_ERROR_NONE != ret && NULL == serviceHandle) {
88             LogError("Fail to create service");
89             return;
90         }
91
92         // set service operation
93         ret = service_set_operation(serviceHandle, SERVICE_OPERATION_DOWNLOAD);
94         if (SERVICE_ERROR_NONE != ret) {
95             LogError("Fail to set operation [" << ret << "]");
96             service_destroy(serviceHandle);
97             return;
98         }
99
100         // set service uri
101         ret = service_set_uri(serviceHandle, url.c_str());
102         if (SERVICE_ERROR_NONE != ret) {
103             LogError("Fail to set uri [" << ret << "]");
104             service_destroy(serviceHandle);
105             return;
106         }
107
108         // set cookie
109         if (cookie != "null") {
110             ret = service_add_extra_data(serviceHandle,
111                                          SERVICE_EXTRA_DATA_KEY_COOKIE,
112                                          cookie.c_str());
113             if (SERVICE_ERROR_NONE != ret) {
114                 LogError("Fail to add cookie [" << ret << "]");
115                 service_destroy(serviceHandle);
116                 return;
117             }
118         }
119
120         //launch service
121         ret = service_send_launch_request(serviceHandle, NULL, NULL);
122         if (SERVICE_ERROR_NONE != ret) {
123             LogError("Fail to launch service [" << ret << "]");
124             service_destroy(serviceHandle);
125             return;
126         }
127
128         LogDebug("Success launch " << SERVICE_OPERATION_DOWNLOAD);
129         service_destroy(serviceHandle);
130         return;
131     } else if (PKG_NAME_VIDEO_PLAYER == pkgName) {
132         bool isRunning = false;
133         if (APP_MANAGER_ERROR_NONE !=
134             app_manager_is_running(PKG_NAME_VT_MAIN.c_str(), &isRunning))
135         {
136             LogError("Fail to get app running information");
137             return;
138         }
139         if (true == isRunning) {
140             LogError("video-call is running");
141             return;
142         }
143
144         std::string scheme(event.GetArg1());
145         std::string uri(event.GetArg2());
146         std::string cookie(event.GetArg3());
147         const char* url;
148
149         if ("null" == scheme) {
150             LogError("scheme is empty");
151             return;
152         }
153         if ("null" == uri) {
154             LogError("uri is empty");
155             return;
156         }
157
158         LogDebug("scheme: " << scheme);
159         if (SCHEME_TYPE_YOUTUBE == scheme) {
160             YouTubeHD *youtube = new YouTubeHD(m_tizenId);
161             url = youtube->getYouTubeHD(uri.c_str());
162             delete youtube;
163         } else if (SCHEME_TYPE_RTSP == scheme ||
164                    (SCHEME_TYPE_HTML5_VIDEO == scheme))
165         {
166             url = uri.c_str();
167         } else {
168             LogError("scheme is invalid!!");
169             return;
170         }
171
172         service_h serviceHandle = NULL;
173         int ret = SERVICE_ERROR_NONE;
174
175         // create service
176         ret = service_create(&serviceHandle);
177         if (SERVICE_ERROR_NONE != ret && NULL == serviceHandle) {
178             LogError("Fail to create service");
179             return;
180         }
181
182         // set url
183         if (!url || strlen(url) == 0) {
184             LogError("Fail to get url");
185             service_destroy(serviceHandle);
186             return;
187         }
188         ret = service_add_extra_data(serviceHandle,
189                                      SERVICE_EXTRA_DATA_KEY_PATH,
190                                      url);
191         if (SERVICE_ERROR_NONE != ret) {
192             LogError("Fail to set url [" << ret << "]");
193             service_destroy(serviceHandle);
194             return;
195         }
196
197         // set cookie
198         if (SCHEME_TYPE_HTML5_VIDEO == scheme) {
199             if ("null" != cookie) {
200                 ret = service_add_extra_data(serviceHandle,
201                                              SERVICE_EXTRA_DATA_KEY_COOKIE,
202                                              cookie.c_str());
203                 if (SERVICE_ERROR_NONE != ret) {
204                     LogError("Fail to add cookie [" << ret << "]");
205                     service_destroy(serviceHandle);
206                     return;
207                 }
208             }
209         }
210
211         // set package
212         ret = service_set_package(serviceHandle, PKG_NAME_VIDEO_PLAYER.c_str());
213         if (SERVICE_ERROR_NONE != ret) {
214             LogError("Fail to set package [" << ret << "]");
215             service_destroy(serviceHandle);
216             return;
217         }
218
219         //launch service
220         ret = service_send_launch_request(serviceHandle, NULL, NULL);
221         if (SERVICE_ERROR_NONE != ret) {
222             LogError("Fail to launch service [" << ret << "]");
223             service_destroy(serviceHandle);
224             return;
225         }
226
227         LogDebug("Success launch " << PKG_NAME_VIDEO_PLAYER);
228         service_destroy(serviceHandle);
229         return;
230     }
231 }
232
233 void ApplicationLauncher::setWidgetTizenId(const std::string& tizenId)
234 {
235     LogDebug("tizen id: " << tizenId);
236     m_tizenId = tizenId;
237 }