2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file view_logic_uri_support.cpp
18 * @author Pawel Sikorski (p.sikorski@samsung.com)
19 * @brief Implementation file of UriSupport API used by ViewLogic
22 #include "view_logic_uri_support.h"
26 #include <dpl/string.h>
27 #include <dpl/log/log.h>
28 #include <dpl/localization/w3c_file_localization.h>
29 #include <widget_model.h>
32 #include <application_data.h>
33 #include <wrt-commons/custom-handler-dao-ro/CustomHandlerDatabase.h>
34 #include <wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h>
37 namespace ViewModule {
38 namespace UriSupport {
42 SERVICE_DATA_TYPE_OPERATION,
43 SERVICE_DATA_TYPE_URI,
44 SERVICE_DATA_TYPE_MIME,
45 SERVICE_DATA_TYPE_MIME_ELEMENT
48 char const * const SCHEME_TYPE_FILE = "file";
49 char const * const SCHEME_TYPE_WIDGET = "widget";
50 char const * const SERVICE_DATA_WILD_CHAR = "*";
51 char const * const SERVICE_DATA_MIME_TOKEN = "/";
52 const unsigned int SIZE_OF_MIME_ELEMENT = 2;
54 std::list<std::string> parsingMimeData(std::string origin, std::string token)
56 std::list<std::string> parsedData;
60 next = origin.find_first_of(token, current);
61 parsedData.push_back(origin.substr(current, next - current));
63 } while (next != std::string::npos && current < origin.length());
68 * Parses scheme from appsvc. From mailto:test@wacapps.net
69 * it will return list:
73 std::vector<std::string> parseScheme(const std::string &scheme)
75 std::vector<std::string> schemeParts;
76 std::string::size_type pos = scheme.find(":");
77 if (pos != std::string::npos) {
78 schemeParts.push_back(scheme.substr(0, pos));
79 schemeParts.push_back(
80 scheme.substr(pos + 1, scheme.length() - pos - 1));
85 bool compareServiceData(ServiceDataType type,
89 if (SERVICE_DATA_TYPE_OPERATION == type) {
90 return origin == other;
91 } else if (SERVICE_DATA_TYPE_URI == type
92 || SERVICE_DATA_TYPE_MIME_ELEMENT == type)
94 if (SERVICE_DATA_WILD_CHAR == origin) {
97 return origin == other;
99 } else if (SERVICE_DATA_TYPE_MIME == type) {
101 return origin == other;
104 std::list<std::string> vectorOrigin =
105 parsingMimeData(origin, SERVICE_DATA_MIME_TOKEN);
106 if (SIZE_OF_MIME_ELEMENT != vectorOrigin.size()) {
109 std::list<std::string> vectorOther =
110 parsingMimeData(other, SERVICE_DATA_MIME_TOKEN);
111 if (SIZE_OF_MIME_ELEMENT != vectorOther.size()) {
115 FOREACH(it, vectorOrigin) {
116 if (!compareServiceData(SERVICE_DATA_TYPE_MIME_ELEMENT,
118 vectorOther.front()))
122 vectorOther.pop_front();
126 LogError("Wrong data type");
132 std::string prepareUrl(const std::string &url, const std::string &insert)
134 std::string urlFixed = url;
135 if (urlFixed.find("file://") == 0) {
136 urlFixed.erase(0, 6);
138 //replace %s in url with given from appservice
139 int size = snprintf(NULL, 0, urlFixed.c_str(), insert.c_str()) + 1;
141 snprintf(buffer, size, urlFixed.c_str(), insert.c_str());
142 return std::string(buffer);
145 std::string getCustomHandlerProtocolUri(
146 WidgetModel *widgetModel,
147 const std::string &schemeType,
148 const std::string &schemeValue)
150 CustomHandlerDB::Interface::attachDatabaseRO();
151 CustomHandlerDB::CustomHandlerDAOReadOnly handlersDao(widgetModel->TizenId);
152 CustomHandlerDB::CustomHandlerPtr handler =
153 handlersDao.getActivProtocolHandler(
154 DPL::FromASCIIString(schemeType));
155 CustomHandlerDB::Interface::detachDatabase();
157 LogDebug("Found handler, url: " << handler->url);
158 return prepareUrl(DPL::ToUTF8String(handler->base_url) +
159 DPL::ToUTF8String(handler->url), schemeValue);
164 std::string getCustomHandlerContentUri(
165 WidgetModel *widgetModel,
166 const std::string &mime,
167 const std::string &mimeValue)
169 CustomHandlerDB::Interface::attachDatabaseRO();
170 CustomHandlerDB::CustomHandlerDAOReadOnly handlersDao(widgetModel->TizenId);
171 CustomHandlerDB::CustomHandlerPtr handler =
172 handlersDao.getActivContentHandler(
173 DPL::FromASCIIString(mime));
174 CustomHandlerDB::Interface::detachDatabase();
176 LogDebug("Found handler, url: " << handler->url);
177 return prepareUrl(DPL::ToUTF8String(handler->base_url) +
178 DPL::ToUTF8String(handler->url), mimeValue);
183 std::string getAppServiceUri(bundle *bundle, WidgetModel *widgetModel)
186 LogError("Bundle is empty");
187 return std::string("");
190 const char* value = NULL;
191 value = appsvc_get_operation(bundle);
192 std::string operation = value ? value : "";
193 value = appsvc_get_uri(bundle);
194 std::string scheme = value ? value : "";
195 value = appsvc_get_mime(bundle);
196 std::string mime = value ? value : "";
198 LogDebug("operation : " << operation);
199 LogDebug("schemeType : " << scheme);
200 LogDebug("mimetype : " << mime);
202 WidgetApplicationServiceList appServiceList =
203 widgetModel->AppServiceList.Get();
204 FOREACH(appServiceIt, appServiceList) {
205 if (compareServiceData(SERVICE_DATA_TYPE_OPERATION,
206 DPL::ToUTF8String(appServiceIt->operation),
208 compareServiceData(SERVICE_DATA_TYPE_URI,
209 DPL::ToUTF8String(appServiceIt->scheme),
211 compareServiceData(SERVICE_DATA_TYPE_MIME,
212 DPL::ToUTF8String(appServiceIt->mime),
215 return DPL::ToUTF8String(appServiceIt->src);
218 std::vector<std::string> schemeParts = parseScheme(scheme);
219 if (schemeParts.size() > 1) {
220 LogDebug("Scheme parts: " << schemeParts[0] << ", " << schemeParts[1]);
221 return getCustomHandlerProtocolUri(
222 widgetModel, schemeParts[0], schemeParts[1]);
225 value = appsvc_get_data(bundle, APPSVC_DATA_SELECTED);
227 LogDebug("Use mime type for: " << value);
228 return getCustomHandlerContentUri(
229 widgetModel, mime, std::string(value));
231 LogError("Selected file for mime is null, nothind to do");
234 LogDebug("no matching result");
235 return std::string("");
238 std::string getUri(WidgetModel *widgetModel, const std::string &defaultUri)
241 std::string startUri;
242 LogDebug("default uri: " << defaultUri);
243 bundle *originBundle = ApplicationDataSingleton::Instance().getBundle();
244 // search application service
245 startUri = getAppServiceUri(originBundle, widgetModel).c_str();
246 LogInfo("application service start uri is " << startUri);
247 if (startUri == "") {
248 LogInfo("application service doesn't have matched data");
249 startUri = defaultUri;
251 // create encoded bundle data only for uri for application service
252 ApplicationDataSingleton::Instance().setEncodedBundle(originBundle);
255 // insert prefix path
256 std::string preFix = DPL::ToUTF8String(widgetModel->PrefixURL.Get());
258 if (strstr(startUri.c_str(), "http") == startUri.c_str() ||
259 strstr(startUri.c_str(), preFix.c_str()) == startUri.c_str())
261 uri = DPL::FromUTF8String(startUri);
263 uri = widgetModel->PrefixURL.Get() +
264 DPL::FromUTF8String(startUri);
266 return DPL::ToUTF8String(uri).c_str();
269 DPL::OptionalString localizeURI(const DPL::String& inputURI,
270 const WidgetModel* model)
272 auto uri = DPL::ToUTF8String(inputURI);
273 LogDebug("localizing url: " << uri);
275 auto urlcstr = uri.c_str();
277 const char *end = strstr(urlcstr, ":");
279 LogDebug("no schema in link, return null");
281 return DPL::Optional<DPL::String>::Null;
284 std::string scheme(urlcstr, end);
285 if (scheme != SCHEME_TYPE_WIDGET && scheme != SCHEME_TYPE_FILE) {
286 LogDebug("scheme doesn't need to localize");
287 return DPL::OptionalString(inputURI);
290 DPL::Optional<DPL::String> found =
291 W3CFileLocalization::getFilePathInWidgetPackageFromUrl(
293 DPL::FromUTF8String(uri));
295 if (found.IsNull()) {
296 // In this case, path doesn't need to localize. return input uri
297 LogDebug("Path not found within current locale in current widget");
298 return DPL::OptionalString(inputURI);
300 DPL::String uri(L"file://" + *found);
301 LogDebug("Will load resource: " << uri);
302 LogDebug("finished");
303 return DPL::OptionalString(uri);
306 } // namespace UriSupportImplementation
307 } // namespace ViewModule