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 w3c_file_localization.cpp
18 * @author Lukasz Wrzosek (l.wrzosek@samsung.com)
22 #include <sys/types.h>
26 #include <dpl/localization/w3c_file_localization.h>
28 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
29 #include <dpl/localization/localization_utils.h>
31 #include <dpl/log/log.h>
32 #include <dpl/string.h>
33 #include <dpl/optional.h>
34 #include <dpl/foreach.h>
36 using namespace WrtDB;
39 const DPL::String FILE_URI_BEGIN = L"file://";
40 const DPL::String WIDGET_URI_BEGIN = L"widget://";
42 DPL::Optional<std::string> GetFilePathInWidgetPackageInternal(
43 const LanguageTagsList &tags,
44 const std::string& basePath,
47 LogDebug("Looking for file: " << filePath << " in: " << basePath);
48 using namespace LocalizationUtils;
50 //Check if string isn't empty
51 if (filePath.size() == 0) { return DPL::Optional<std::string>::Null; }
52 //Removing preceding '/'
53 if (filePath[0] == '/') { filePath.erase(0, 1); }
54 //Check if string isn't empty
55 if (filePath.size() == 0) { return DPL::Optional<std::string>::Null; }
57 LogDebug("locales size = " << tags.size());
58 for (LanguageTagsList::const_iterator it = tags.begin();
61 LogDebug("Trying locale: " << *it);
62 std::string path = basePath;
63 if (path[path.size() - 1] == '/') {
64 path.erase(path.size() - 1);
68 path += "/" + filePath;
70 path += "/locales/" + DPL::ToUTF8String(*it) + "/" + filePath;
73 LogDebug("Trying locale: " << *it << " | " << path);
75 if (0 == stat(path.c_str(), &buf)) {
76 if ((buf.st_mode & S_IFMT) == S_IFREG) {
77 path.erase(0, basePath.length());
78 return DPL::Optional<std::string>(path);
83 return DPL::Optional<std::string>::Null;
86 DPL::Optional<DPL::String> GetFilePathInWidgetPackageInternal(
87 const LanguageTagsList &languageTags,
88 const DPL::String& basePath,
89 const DPL::String& filePath)
91 DPL::Optional<std::string> path =
92 GetFilePathInWidgetPackageInternal(languageTags,
93 DPL::ToUTF8String(basePath),
94 DPL::ToUTF8String(filePath));
95 DPL::Optional<DPL::String> dplPath;
97 dplPath = DPL::FromUTF8String(*path);
103 namespace W3CFileLocalization {
104 DPL::Optional<DPL::String> getFilePathInWidgetPackageFromUrl(
105 DbWidgetHandle widgetHandle,
106 const LanguageTagsList &languageTags,
107 const DPL::String &url)
109 DPL::String req = url;
110 WidgetDAOReadOnly dao(widgetHandle);
112 if (req.find(WIDGET_URI_BEGIN) == 0) {
113 req.erase(0, WIDGET_URI_BEGIN.length());
114 } else if (req.find(FILE_URI_BEGIN) == 0) {
115 req.erase(0, FILE_URI_BEGIN.length());
116 if (req.find(dao.getPath()) == 0) {
117 req.erase(0, dao.getPath().length());
120 LogDebug("Unknown path format, ignoring");
121 return DPL::Optional<DPL::String>::Null;
124 auto widgetPath = dao.getPath();
126 DPL::Optional<DPL::String> found =
127 GetFilePathInWidgetPackageInternal(languageTags, widgetPath, req);
130 LogError("Path not found within current locale in current widget");
131 return DPL::Optional<DPL::String>::Null;
134 found = widgetPath + *found;
139 DPL::Optional<DPL::String> getFilePathInWidgetPackage(
140 WrtDB::DbWidgetHandle widgetHandle,
141 const LanguageTagsList &languageTags,
142 const DPL::String& file)
144 WidgetDAOReadOnly dao(widgetHandle);
145 return GetFilePathInWidgetPackageInternal(languageTags, dao.getPath(), file);
148 DPL::OptionalString getStartFile(const WrtDB::DbWidgetHandle widgetHandle)
150 using namespace LocalizationUtils;
152 WidgetDAOReadOnly dao(widgetHandle);
154 WidgetDAOReadOnly::LocalizedStartFileList locList = dao.getLocalizedStartFileList();
155 WidgetDAOReadOnly::WidgetStartFileList list = dao.getStartFileList();
156 LanguageTagsList tagsList = LocalizationUtils::GetUserAgentLanguageTags();
158 DPL::OptionalString defaultLoc = dao.getDefaultlocale();
160 tagsList.push_back(*defaultLoc);
163 FOREACH(tag, tagsList)
165 FOREACH(sFile, locList)
167 if (*tag == sFile->widgetLocale) {
170 if (it->startFileId == sFile->startFileId) {
178 return DPL::OptionalString::Null;
181 OptionalWidgetIcon getIcon(const WrtDB::DbWidgetHandle widgetHandle)
183 using namespace LocalizationUtils;
184 WidgetDAOReadOnly dao(widgetHandle);
186 WidgetDAOReadOnly::WidgetLocalizedIconList locList = dao.getLocalizedIconList();
187 WidgetDAOReadOnly::WidgetIconList list = dao.getIconList();
188 LanguageTagsList tagsList = LocalizationUtils::GetUserAgentLanguageTags();
190 DPL::OptionalString defaultLoc = dao.getDefaultlocale();
192 tagsList.push_back(*defaultLoc);
195 FOREACH(tag, tagsList)
197 FOREACH(icon, locList)
199 if (*tag == icon->widgetLocale) {
202 if (it->iconId == icon->iconId) {
204 ret.src = it->iconSrc;
205 ret.width = it->iconWidth;
206 ret.height = it->iconHeight;
214 return OptionalWidgetIcon::Null;
217 WidgetIconList getValidIconsList(
218 WrtDB::DbWidgetHandle widgetHandle,
219 const LanguageTagsList &languageTags)
221 using namespace LocalizationUtils;
222 WidgetDAOReadOnly dao(widgetHandle);
223 WidgetDAOReadOnly::WidgetIconList list = dao.getIconList();
225 WidgetIconList outlist;
229 LogDebug(":" << it->iconSrc);
230 if (!!getFilePathInWidgetPackage(widgetHandle,
235 ret.src = it->iconSrc;
236 ret.width = it->iconWidth;
237 ret.height = it->iconHeight;
238 outlist.push_back(ret);
244 OptionalWidgetStartFileInfo getStartFileInfo(
245 WrtDB::DbWidgetHandle widgetHandle,
246 const LanguageTagList &tagsList)
248 using namespace LocalizationUtils;
250 WidgetStartFileInfo info;
252 WidgetDAOReadOnly dao(widgetHandle);
253 WidgetDAOReadOnly::LocalizedStartFileList locList =
254 dao.getLocalizedStartFileList();
255 WidgetDAOReadOnly::WidgetStartFileList list = dao.getStartFileList();
257 FOREACH(tag, tagsList)
259 FOREACH(sFile, locList)
261 if (*tag == sFile->widgetLocale) {
264 if (it->startFileId ==
265 sFile->startFileId) {
267 info.encoding = sFile->encoding;
268 info.type = sFile->type;
270 info.localizedPath = it->src;
272 info.localizedPath = L"locales/" + *tag + L"/";
273 info.localizedPath += it->src;
282 return OptionalWidgetStartFileInfo::Null;
285 WidgetLocalizedInfo getLocalizedInfo(const WrtDB::DbWidgetHandle handle)
287 LanguageTagList languages =
288 LocalizationUtils::GetUserAgentLanguageTags();
289 WidgetDAOReadOnly dao(handle);
290 DPL::OptionalString dl = dao.getDefaultlocale();
292 languages.push_back(*dl);
295 WidgetLocalizedInfo result;
296 FOREACH(i, languages)
298 WidgetLocalizedInfo languageResult = dao.getLocalizedInfo(*i);
300 #define OVERWRITE_IF_NULL(FIELD) if (!result.FIELD) { \
301 result.FIELD = languageResult.FIELD; \
304 OVERWRITE_IF_NULL(name);
305 OVERWRITE_IF_NULL(shortName);
306 OVERWRITE_IF_NULL(description);
307 OVERWRITE_IF_NULL(license);
308 OVERWRITE_IF_NULL(licenseHref);
310 #undef OVERWRITE_IF_NULL