tizen 2.3.1 release
[framework/web/mobile/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
25 #include <dpl/log/wrt_log.h>
26 #include <dpl/singleton_impl.h>
27
28 #include <app.h>
29 #include <app_manager.h>
30 #include <appsvc.h>
31 #include <notification.h>
32 #include <widget_string.h>
33
34 IMPLEMENT_SINGLETON(ApplicationLauncher)
35
36 namespace {
37 const char * const APP_CONTROL_EXTRA_DATA_KEY_PATH = "path";
38 const char * const APP_CONTROL_EXTRA_DATA_KEY_COOKIE = "cookie";
39 const char * const APP_CONTROL_EXTRA_DATA_KEY_MODE = "mode";
40 const char * const APP_CONTROL_EXTRA_DATA_VALUE_SLIENT = "silent";
41
42 const char * const SCHEME_TYPE_RTSP = "rtsp";
43 const char * const SCHEME_TYPE_HTML5_VIDEO = "html5video";
44 }
45
46 ApplicationLauncher::ApplicationLauncher() :
47     m_windowHandle(0)
48 {
49 }
50
51 ApplicationLauncher::~ApplicationLauncher()
52 {}
53
54 void ApplicationLauncher::OnEventReceived(
55     const ApplicationLauncherEvents::LaunchApplicationByAppService &event)
56 {
57     int result;
58     app_control_h app_control = event.GetArg0();
59     app_control_reply_cb responseCallback = event.GetArg1();
60     void *userData = event.GetArg2();
61
62     if (m_windowHandle) {
63         app_control_set_window(app_control, m_windowHandle);
64     }
65
66     result = app_control_send_launch_request(app_control, responseCallback, userData);
67     if (result != APP_CONTROL_ERROR_NONE) {
68         app_control_destroy(app_control);
69         WrtLogE("Failed to run app_control : %d", result);
70         return;
71     }
72     app_control_destroy(app_control);
73     WrtLogD("Success to run app_control");
74 }
75
76 void ApplicationLauncher::OnEventReceived(
77     const ApplicationLauncherEvents::LaunchApplicationByPkgname &event)
78 {
79     using namespace ApplicationLauncherPkgname;
80     std::string pkgName(event.GetArg0());
81
82     if (PKG_NAME_DOWNLOAD_PROVIDER == pkgName) {
83         std::string url(event.GetArg1());
84         // This value needs for checking video, music contents later.
85         //std::string mime_type(event.GetArg2());
86         std::string cookie(event.GetArg3());
87
88         if ("null" == url) {
89             WrtLogE("url is empty");
90             return;
91         }
92
93         app_control_h app_control = NULL;
94         int ret = APP_CONTROL_ERROR_NONE;
95
96         // create app_control
97         ret = app_control_create(&app_control);
98         if (APP_CONTROL_ERROR_NONE != ret && NULL == app_control) {
99             WrtLogE("Fail to create app_control");
100             return;
101         }
102
103         // set app_control operation
104         ret = app_control_set_operation(app_control, APP_CONTROL_OPERATION_DOWNLOAD);
105         if (APP_CONTROL_ERROR_NONE != ret) {
106             WrtLogE("Fail to set operation [%d]", ret);
107             app_control_destroy(app_control);
108             return;
109         }
110
111         // set app_control uri
112         ret = app_control_set_uri(app_control, url.c_str());
113         if (APP_CONTROL_ERROR_NONE != ret) {
114             WrtLogE("Fail to set uri [%d]", ret);
115             app_control_destroy(app_control);
116             return;
117         }
118
119         // set cookie
120         if (cookie != "null") {
121             ret = app_control_add_extra_data(app_control,
122                                          APP_CONTROL_EXTRA_DATA_KEY_COOKIE,
123                                          cookie.c_str());
124             if (APP_CONTROL_ERROR_NONE != ret) {
125                 WrtLogD("Fail to add cookie [%d]", ret);
126                 app_control_destroy(app_control);
127                 return;
128             }
129         }
130
131         ret = app_control_add_extra_data(app_control,
132                                      APP_CONTROL_EXTRA_DATA_KEY_MODE,
133                                      APP_CONTROL_EXTRA_DATA_VALUE_SLIENT);
134         if (APP_CONTROL_ERROR_NONE != ret) {
135             WrtLogE("Fail to set app_control extra data [%d]", ret);
136             app_control_destroy(app_control);
137             return;
138         }
139
140         if (m_windowHandle) {
141             app_control_set_window(app_control, m_windowHandle);
142         }
143
144         //launch app_control
145         ret = app_control_send_launch_request(app_control, NULL, NULL);
146         if (APP_CONTROL_ERROR_NONE != ret) {
147             WrtLogE("Fail to launch app_control [%d]", ret);
148             app_control_destroy(app_control);
149             return;
150         }
151         app_control_destroy(app_control);
152         notification_status_message_post(WRT_POP_STARTING_DOWNLOADING);
153         return;
154     } else if (PKG_NAME_VIDEO_PLAYER == pkgName) {
155         bool isRunning = false;
156         if (APP_MANAGER_ERROR_NONE !=
157             app_manager_is_running(PKG_NAME_VT_MAIN.c_str(), &isRunning))
158         {
159             WrtLogE("Fail to get app running information");
160             return;
161         }
162         if (true == isRunning) {
163             WrtLogE("video-call is running");
164             return;
165         }
166
167         std::string scheme(event.GetArg1());
168         std::string uri(event.GetArg2());
169         std::string cookie(event.GetArg3());
170         const char* url;
171
172         if ("null" == scheme) {
173             WrtLogE("scheme is empty");
174             return;
175         }
176         if ("null" == uri) {
177             WrtLogE("uri is empty");
178             return;
179         }
180
181         if (SCHEME_TYPE_RTSP == scheme ||
182             SCHEME_TYPE_HTML5_VIDEO == scheme)
183         {
184             url = uri.c_str();
185         } else {
186             WrtLogE("scheme is invalid!!");
187             return;
188         }
189
190         app_control_h app_control = NULL;
191         int ret = APP_CONTROL_ERROR_NONE;
192
193         // create app_control
194         ret = app_control_create(&app_control);
195         if (APP_CONTROL_ERROR_NONE != ret && NULL == app_control) {
196             WrtLogE("Fail to create app_control");
197             return;
198         }
199
200         // set url
201         if (!url || strlen(url) == 0) {
202             WrtLogE("Fail to get url");
203             app_control_destroy(app_control);
204             return;
205         }
206         ret = app_control_add_extra_data(app_control,
207                                      APP_CONTROL_EXTRA_DATA_KEY_PATH,
208                                      url);
209         if (APP_CONTROL_ERROR_NONE != ret) {
210             WrtLogE("Fail to set url [%d]", ret);
211             app_control_destroy(app_control);
212             return;
213         }
214
215         // set cookie
216         if (SCHEME_TYPE_HTML5_VIDEO == scheme) {
217             if ("null" != cookie) {
218                 ret = app_control_add_extra_data(app_control,
219                                              APP_CONTROL_EXTRA_DATA_KEY_COOKIE,
220                                              cookie.c_str());
221                 if (APP_CONTROL_ERROR_NONE != ret) {
222                     WrtLogE("Fail to add cookie [%d]", ret);
223                     app_control_destroy(app_control);
224                     return;
225                 }
226             }
227         }
228
229         // set package
230         ret = app_control_set_app_id(app_control, PKG_NAME_VIDEO_PLAYER.c_str());
231         if (APP_CONTROL_ERROR_NONE != ret) {
232             WrtLogE("Fail to set package app_control [%d]", ret);
233             app_control_destroy(app_control);
234             return;
235         }
236
237         // set window handle when available
238         if (m_windowHandle) {
239             app_control_set_window(app_control, m_windowHandle);
240         }
241
242         //launch app_control
243         ret = app_control_send_launch_request(app_control, NULL, NULL);
244         if (APP_CONTROL_ERROR_NONE != ret) {
245             WrtLogE("Fail to launch app_control [%d]", ret);
246             app_control_destroy(app_control);
247             return;
248         }
249         app_control_destroy(app_control);
250         return;
251     } else {
252         WrtLogE("Not implemented application : %s", pkgName.c_str());
253     }
254
255     WrtLogD("Success to launch application : %s", pkgName.c_str());
256     return;
257 }
258
259 void ApplicationLauncher::setWidgetTizenId(const std::string& tizenId)
260 {
261     m_tizenId = tizenId;
262 }
263
264 void ApplicationLauncher::setWindowHandle(unsigned windowHandle)
265 {
266     m_windowHandle = windowHandle;
267 }