Source code formating unification
[platform/framework/web/wrt.git] / src / view / common / view_logic_apps_support.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    view_logic_apps_support.cpp
18  * @author  Pawel Sikorski (p.sikorski@samsung.com)
19  * @brief   Implementation file of AppsSupport class used by ViewLogic
20  */
21
22 #include "view_logic_apps_support.h"
23 #include <dpl/assert.h>
24 #include <dpl/log/log.h>
25
26 #include <appsvc.h>
27 #include <widget_model.h>
28 #include <application_launcher.h>
29
30 namespace ViewModule {
31 namespace {
32 const char* const SCHEME_TYPE_HTML5_VIDEO = "html5video";
33 const char* const HTTP_STREAMING_MPEG_MIMETYPE = "application/x-mpegurl";
34 const char* const HTTP_STREAMING_APPLE_MIMETYPE =
35     "application/vnd.apple.mpegurl";
36 }
37
38 //Implementation class
39 class AppsSupportImplementation
40 {
41   private:
42     WidgetModel *m_widgetModel;
43     bool m_initialized;
44
45     struct HTML5Video {
46         const char* path;
47         const char* cookie;
48     };
49
50   public:
51     AppsSupportImplementation() :
52         m_widgetModel(NULL),
53         m_initialized(false)
54     {}
55
56     ~AppsSupportImplementation()
57     {
58         Assert(!m_initialized &&
59                "AppsSupport has to be deinitialized prior destroying!");
60     }
61
62     void initialize(WidgetModel *widgetModel)
63     {
64         Assert(!m_initialized && "Already initialized!");
65
66         LogDebug("Initializing Apps Support");
67         Assert(widgetModel && "Passed widgetModel is NULL!");
68         m_widgetModel = widgetModel;
69
70         ApplicationLauncherSingleton::Instance().Touch();
71
72         ApplicationLauncherSingleton::Instance().setWidgetTizenId(
73             DPL::ToUTF8String(m_widgetModel->TizenId));
74
75         LogDebug("Initialized");
76         m_initialized = true;
77     }
78
79     void deinitialize()
80     {
81         Assert(m_initialized && "Not initialized!");
82         LogDebug("Deinitialized");
83         m_widgetModel = NULL;
84         m_initialized = false;
85     }
86
87     bool httpMultimediaRequest(std::string mimeType, std::string uri)
88     {
89         LogInfo("httpMultimediaRequest called");
90
91         if ("null" == mimeType || "null" == uri) {
92             LogError("uri/mimeType is null");
93             return false;
94         }
95
96         bundle *args = NULL;
97         args = bundle_create();
98         void *userData = NULL;
99         appsvc_res_fn responseCallback = NULL;
100
101         // ignore case match of string of mime type
102         // if needed, define appsvc response callback
103         // and its user data per mimetype
104         if (!strcasecmp(mimeType.c_str(), HTTP_STREAMING_APPLE_MIMETYPE) ||
105             !strcasecmp(mimeType.c_str(), HTTP_STREAMING_MPEG_MIMETYPE))
106         {
107             appsvc_set_operation(args, APPSVC_OPERATION_VIEW);
108             appsvc_set_mime(args, mimeType.c_str());
109             appsvc_set_uri(args, uri.c_str());
110         } else if (!strcasecmp(mimeType.c_str(), "video/mp4") ||
111                    !strcasecmp(mimeType.c_str(), "vidoe/3gp"))
112         {
113             CONTROLLER_POST_EVENT(
114                 ApplicationLauncher,
115                 ApplicationLauncherEvents::LaunchApplicationByPkgname(
116                     ApplicationLauncherPkgname::PKG_NAME_VIDEO_PLAYER,
117                     SCHEME_TYPE_HTML5_VIDEO,
118                     uri,
119                     "null"));
120         } else {
121             LogInfo("Not Supported MIME type in WRT");
122             bundle_free(args);
123             return false;
124         }
125
126         CONTROLLER_POST_EVENT(
127             ApplicationLauncher,
128             ApplicationLauncherEvents::LaunchApplicationByAppService(
129                 args,
130                 responseCallback,
131                 userData));
132
133         return true;
134     }
135
136     void downloadRequest(const char *url,
137                          const char *mimeType,
138                          const char *userParam)
139     {
140         LogInfo("Download info : " << url << "(" <<
141                 mimeType << ", " << userParam << ")");
142
143         // ignore case match of string of mime type
144         int isAppServiceable = httpMultimediaRequest(
145                 mimeType ? std::string(mimeType) : "null",
146                 url ? std::string(url) : "null");
147
148         if (isAppServiceable) {
149             LogInfo("Application Service start");
150             return;
151         }
152
153         CONTROLLER_POST_EVENT(
154             ApplicationLauncher,
155             ApplicationLauncherEvents::LaunchApplicationByPkgname(
156                 ApplicationLauncherPkgname::PKG_NAME_DOWNLOAD_PROVIDER,
157                 url ? std::string(url) : "null",
158                 mimeType ? mimeType : "null",
159                 userParam ? userParam : "null"));
160     }
161
162     void html5VideoRequest(void* event_info)
163     {
164         LogInfo("html5VideoRequestCallback called");
165         Assert(event_info);
166         HTML5Video* video = static_cast<HTML5Video*>(event_info);
167
168         LogDebug("video->path : " << video->path);
169         LogDebug("video->cookie : " << video->cookie);
170         if (NULL == video->path) {
171             LogError("path is null");
172             return;
173         }
174         CONTROLLER_POST_EVENT(
175             ApplicationLauncher,
176             ApplicationLauncherEvents::LaunchApplicationByPkgname(
177                 ApplicationLauncherPkgname::PKG_NAME_VIDEO_PLAYER,
178                 SCHEME_TYPE_HTML5_VIDEO,
179                 video->path ? video->path : "null",
180                 video->cookie ? video->cookie : "null"));
181     }
182 };
183
184 AppsSupport::AppsSupport() : m_impl(new AppsSupportImplementation)
185 {}
186
187 AppsSupport::~AppsSupport()
188 {}
189
190 void AppsSupport::initialize(WidgetModel *widgetModel)
191 {
192     m_impl->initialize(widgetModel);
193 }
194
195 void AppsSupport::deinitialize()
196 {
197     m_impl->deinitialize();
198 }
199
200 void AppsSupport::html5VideoRequest(void* event_info)
201 {
202     m_impl->html5VideoRequest(event_info);
203 }
204
205 void AppsSupport::downloadRequest(const char *url,
206                                   const char *mimeType,
207                                   const char *userParam)
208 {
209     m_impl->downloadRequest(url, mimeType, userParam);
210 }
211 } //namespace