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"
23 #include <dpl/string.h>
24 #include <dpl/log/log.h>
25 #include <dpl/localization/w3c_file_localization.h>
26 //#include <dpl/wrt-dao-ro/common_dao_types.h>
27 #include <widget_model.h>
30 #include <application_data.h>
32 namespace ViewModule {
33 namespace UriSupport {
36 char const * const SCHEME_TYPE_FILE = "file";
37 char const * const SCHEME_TYPE_WIDGET = "widget";
40 std::string getAppServiceUri(bundle *bundle, WidgetModel *widgetModel)
44 LogError("Bundle is empty");
45 return std::string("");
48 const char* value = NULL;
49 value = appsvc_get_operation(bundle);
50 std::string operation = value ? value : "";
51 value = appsvc_get_uri(bundle);
52 std::string scheme = value ? value : "";
53 value = appsvc_get_mime(bundle);
54 std::string mimeType = value ? value : "";
56 // get scheme type from scheme uri
57 std::string schemeType = "";
60 const char *end = strstr(scheme.c_str(), ":");
63 LogError("Lack of scheme");
65 std::string schemeTmp(scheme.c_str(), end);
66 schemeType = schemeTmp;
70 LogDebug("operation : " << operation);
71 LogDebug("schemeType : " << schemeType);
72 LogDebug("mimetype : " << mimeType);
74 WidgetApplicationServiceList appServiceList =
75 widgetModel->AppServiceList.Get();
76 FOREACH(appServiceIt, appServiceList) {
77 if (DPL::ToUTF8String(appServiceIt->operation) == operation &&
78 (DPL::ToUTF8String(appServiceIt->scheme) == schemeType ||
79 DPL::ToUTF8String(appServiceIt->scheme) == "*/*") &&
80 (DPL::ToUTF8String(appServiceIt->mime) == mimeType ||
81 DPL::ToUTF8String(appServiceIt->mime) == "*/*"))
83 // ignore default operation that is reserved by system
84 if (operation == APPSVC_OPERATION_DEFAULT) {
85 return std::string("");
87 return DPL::ToUTF8String(appServiceIt->src);
90 LogDebug("no matching result");
91 return std::string("");
94 std::string getUri(WidgetModel *widgetModel, const std::string &defaultUri)
99 bundle *originBundle = ApplicationDataSingleton::Instance().getBundle();
100 // search application service
101 startUri = getAppServiceUri(originBundle, widgetModel).c_str();
102 LogInfo("application service start uri is " << startUri);
103 if (startUri == "") {
104 LogInfo("application service doesn't have matched data");
105 startUri = defaultUri;
107 // create encoded bundle data only for uri for application service
108 ApplicationDataSingleton::Instance().setEncodedBundle(originBundle);
111 // insert prefix path
112 std::string preFix = DPL::ToUTF8String(widgetModel->PrefixURL.Get());
114 if (strstr(startUri.c_str(), "http") == startUri.c_str() ||
115 strstr( startUri.c_str(), preFix.c_str()) == startUri.c_str()) {
116 uri = DPL::FromUTF8String(startUri);
118 uri = widgetModel->PrefixURL.Get() +
119 DPL::FromUTF8String(startUri);
121 return DPL::ToUTF8String(uri).c_str();
124 DPL::OptionalString localizeURI(const DPL::String& inputURI,
125 const WidgetModel* model)
127 auto uri = DPL::ToUTF8String(inputURI);
128 LogDebug("localizing url: " << uri);
130 auto urlcstr = uri.c_str();
132 const char *end = strstr(urlcstr, ":");
134 LogDebug("no schema in link, return null");
136 return DPL::Optional<DPL::String>::Null;
139 std::string scheme(urlcstr, end);
140 if (scheme != SCHEME_TYPE_WIDGET && scheme != SCHEME_TYPE_FILE) {
141 LogDebug("scheme doesn't need to localize");
142 return DPL::OptionalString(inputURI);
145 DPL::Optional<DPL::String> found =
146 W3CFileLocalization::getFilePathInWidgetPackageFromUrl(
148 model->LanguageTags.Get(),
149 DPL::FromUTF8String(uri));
151 if (found.IsNull()) {
152 // In this case, path doesn't need to localize. return input uri
153 LogDebug("Path not found within current locale in current widget");
154 return DPL::OptionalString(inputURI);
156 DPL::String uri(L"file://" + *found);
157 LogDebug("Will load resource: " << uri);
158 LogDebug("finished");
159 return DPL::OptionalString(uri);
163 } // namespace UriSupportImplementation
164 } // namespace ViewModule