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>
27 #include <dpl/localization/w3c_file_localization.h>
29 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
30 #include <dpl/localization/localization_utils.h>
32 #include <dpl/log/log.h>
33 #include <dpl/string.h>
34 #include <dpl/optional.h>
35 #include <dpl/foreach.h>
37 #include <LanguageTagsProvider.h>
39 using namespace WrtDB;
42 const DPL::String FILE_URI_BEGIN = L"file://";
43 const DPL::String WIDGET_URI_BEGIN = L"widget://";
44 const DPL::String LOCALE_PREFIX = L"locales/";
46 DPL::Optional<std::string> GetFilePathInWidgetPackageInternal(
47 const std::string& basePath,
50 LogDebug("Looking for file: " << filePath << " in: " << basePath);
52 const LanguageTags& ltags = LanguageTagsProviderSingleton::Instance().getLanguageTags();
54 //Check if string isn't empty
55 if (filePath.size() == 0) { return DPL::Optional<std::string>::Null; }
56 //Removing preceding '/'
57 if (filePath[0] == '/') { filePath.erase(0, 1); }
58 // In some cases (start file localization) url has unnecessary "/" at the end
59 if(filePath[filePath.size()-1] == '/') { filePath.erase(filePath.size()-1, 1); }
60 //Check if string isn't empty
61 if (filePath.size() == 0) { return DPL::Optional<std::string>::Null; }
63 LogDebug("locales size = " << ltags.size());
64 for (LanguageTags::const_iterator it = ltags.begin();
67 LogDebug("Trying locale: " << *it);
68 std::string path = basePath;
69 if (path[path.size() - 1] == '/') {
70 path.erase(path.size() - 1);
74 path += "/" + filePath;
76 path += "/locales/" + DPL::ToUTF8String(*it) + "/" + filePath;
79 LogDebug("Trying locale: " << *it << " | " << path);
81 if (0 == stat(path.c_str(), &buf)) {
82 if ((buf.st_mode & S_IFMT) == S_IFREG) {
83 path.erase(0, basePath.length());
84 return DPL::Optional<std::string>(path);
89 return DPL::Optional<std::string>::Null;
92 DPL::Optional<DPL::String> GetFilePathInWidgetPackageInternal(
93 const DPL::String& basePath,
94 const DPL::String& filePath)
96 DPL::Optional<std::string> path =
97 GetFilePathInWidgetPackageInternal(DPL::ToUTF8String(basePath),
98 DPL::ToUTF8String(filePath));
99 DPL::Optional<DPL::String> dplPath;
101 dplPath = DPL::FromUTF8String(*path);
107 namespace W3CFileLocalization {
108 DPL::Optional<DPL::String> getFilePathInWidgetPackageFromUrl(
109 DbWidgetHandle widgetHandle,
110 const DPL::String &url)
112 DPL::String req = url;
113 WidgetDAOReadOnly dao(widgetHandle);
115 if (req.find(WIDGET_URI_BEGIN) == 0) {
116 req.erase(0, WIDGET_URI_BEGIN.length());
117 } else if (req.find(FILE_URI_BEGIN) == 0) {
118 req.erase(0, FILE_URI_BEGIN.length());
119 if (req.find(dao.getPath()) == 0) {
120 req.erase(0, dao.getPath().length());
122 if (req.find(LOCALE_PREFIX) == 0) {
123 req.erase(0, LOCALE_PREFIX.length());
124 size_t position = req.find('/');
125 // should always be >0 as correct locales path is
126 // always locales/xx/ or locales/xx-XX/
127 if (position != std::string::npos && position > 0) {
128 req.erase(0, position+1);
132 LogDebug("Unknown path format, ignoring");
133 return DPL::Optional<DPL::String>::Null;
136 auto widgetPath = dao.getPath();
138 DPL::Optional<DPL::String> found =
139 GetFilePathInWidgetPackageInternal(widgetPath, req);
142 LogError("Path not found within current locale in current widget");
143 return DPL::Optional<DPL::String>::Null;
146 found = widgetPath + *found;
151 DPL::Optional<DPL::String> getFilePathInWidgetPackage(
152 WrtDB::DbWidgetHandle widgetHandle,
153 const DPL::String& file)
155 WidgetDAOReadOnly dao(widgetHandle);
156 return GetFilePathInWidgetPackageInternal(dao.getPath(), file);
159 DPL::OptionalString getStartFile(const WrtDB::WidgetPkgName & pkgname)
161 return getStartFile(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(pkgname)));
164 DPL::OptionalString getStartFile(const WrtDB::DbWidgetHandle handle)
166 return getStartFile(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(handle)));
169 DPL::OptionalString getStartFile(WrtDB::WidgetDAOReadOnlyPtr dao)
171 WidgetDAOReadOnly::LocalizedStartFileList locList = dao->getLocalizedStartFileList();
172 WidgetDAOReadOnly::WidgetStartFileList list = dao->getStartFileList();
173 LanguageTags tagsList = LanguageTagsProviderSingleton::Instance().getLanguageTags();
175 DPL::OptionalString defaultLoc = dao->getDefaultlocale();
177 tagsList.push_back(*defaultLoc);
180 FOREACH(tag, tagsList)
182 FOREACH(sFile, locList)
184 if (*tag == sFile->widgetLocale) {
187 if (it->startFileId == sFile->startFileId) {
195 return DPL::OptionalString::Null;
198 OptionalWidgetIcon getIcon(const WrtDB::WidgetPkgName & pkgname)
200 return getIcon(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(pkgname)));
203 OptionalWidgetIcon getIcon(WrtDB::DbWidgetHandle widgetHandle)
205 return getIcon(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(widgetHandle)));
208 OptionalWidgetIcon getIcon(WrtDB::WidgetDAOReadOnlyPtr dao)
210 WidgetDAOReadOnly::WidgetLocalizedIconList locList = dao->getLocalizedIconList();
211 WidgetDAOReadOnly::WidgetIconList list = dao->getIconList();
212 LanguageTags tagsList = LanguageTagsProviderSingleton::Instance().getLanguageTags();
214 DPL::OptionalString defaultLoc = dao->getDefaultlocale();
216 tagsList.push_back(*defaultLoc);
219 FOREACH(tag, tagsList)
221 FOREACH(icon, locList)
223 if (*tag == icon->widgetLocale) {
226 if (it->iconId == icon->iconId) {
228 ret.src = it->iconSrc;
229 ret.width = it->iconWidth;
230 ret.height = it->iconHeight;
238 return OptionalWidgetIcon::Null;
241 WidgetIconList getValidIconsList(
242 WrtDB::DbWidgetHandle widgetHandle)
244 WidgetDAOReadOnly dao(widgetHandle);
245 WidgetDAOReadOnly::WidgetIconList list = dao.getIconList();
247 WidgetIconList outlist;
251 LogDebug(":" << it->iconSrc);
252 if (!!getFilePathInWidgetPackage(widgetHandle,
256 ret.src = it->iconSrc;
257 ret.width = it->iconWidth;
258 ret.height = it->iconHeight;
259 outlist.push_back(ret);
265 OptionalWidgetStartFileInfo getStartFileInfo(
266 WrtDB::DbWidgetHandle widgetHandle)
268 WidgetStartFileInfo info;
270 WidgetDAOReadOnly dao(widgetHandle);
271 WidgetDAOReadOnly::LocalizedStartFileList locList =
272 dao.getLocalizedStartFileList();
273 WidgetDAOReadOnly::WidgetStartFileList list = dao.getStartFileList();
274 const LanguageTags tagsList = LanguageTagsProviderSingleton::Instance().getLanguageTags();
276 FOREACH(tag, tagsList)
278 FOREACH(sFile, locList)
280 if (*tag == sFile->widgetLocale) {
283 if (it->startFileId ==
284 sFile->startFileId) {
286 info.encoding = sFile->encoding;
287 info.type = sFile->type;
289 info.localizedPath = it->src;
291 info.localizedPath = L"locales/" + *tag + L"/";
292 info.localizedPath += it->src;
301 return OptionalWidgetStartFileInfo::Null;
304 WidgetLocalizedInfo getLocalizedInfo(const WrtDB::DbWidgetHandle handle)
306 return getLocalizedInfo(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(handle)));
309 WidgetLocalizedInfo getLocalizedInfo(const WrtDB::WidgetPkgName & pkgname)
311 return getLocalizedInfo(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(pkgname)));
314 WidgetLocalizedInfo getLocalizedInfo(WidgetDAOReadOnlyPtr dao)
316 LanguageTags languages = LanguageTagsProviderSingleton::Instance().getLanguageTags();
317 DPL::OptionalString dl = dao->getDefaultlocale();
319 languages.push_back(*dl);
322 WidgetLocalizedInfo result;
323 FOREACH(i, languages)
325 WidgetLocalizedInfo languageResult = dao->getLocalizedInfo(*i);
327 #define OVERWRITE_IF_NULL(FIELD) if (!result.FIELD) { \
328 result.FIELD = languageResult.FIELD; \
331 OVERWRITE_IF_NULL(name);
332 OVERWRITE_IF_NULL(shortName);
333 OVERWRITE_IF_NULL(description);
334 OVERWRITE_IF_NULL(license);
335 OVERWRITE_IF_NULL(licenseHref);
337 #undef OVERWRITE_IF_NULL