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 int 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::DbWidgetHandle handle)
161 return getStartFile(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(handle)));
164 DPL::OptionalString getStartFile(WrtDB::WidgetDAOReadOnlyPtr dao)
166 WidgetDAOReadOnly::LocalizedStartFileList locList = dao->getLocalizedStartFileList();
167 WidgetDAOReadOnly::WidgetStartFileList list = dao->getStartFileList();
168 LanguageTags tagsList = LanguageTagsProviderSingleton::Instance().getLanguageTags();
170 DPL::OptionalString defaultLoc = dao->getDefaultlocale();
172 tagsList.push_back(*defaultLoc);
175 FOREACH(tag, tagsList)
177 FOREACH(sFile, locList)
179 if (*tag == sFile->widgetLocale) {
182 if (it->startFileId == sFile->startFileId) {
190 return DPL::OptionalString::Null;
193 OptionalWidgetIcon getIcon(const WrtDB::DbWidgetHandle widgetHandle)
195 WidgetDAOReadOnly dao(widgetHandle);
197 WidgetDAOReadOnly::WidgetLocalizedIconList locList = dao.getLocalizedIconList();
198 WidgetDAOReadOnly::WidgetIconList list = dao.getIconList();
199 LanguageTags tagsList = LanguageTagsProviderSingleton::Instance().getLanguageTags();
201 DPL::OptionalString defaultLoc = dao.getDefaultlocale();
203 tagsList.push_back(*defaultLoc);
206 FOREACH(tag, tagsList)
208 FOREACH(icon, locList)
210 if (*tag == icon->widgetLocale) {
213 if (it->iconId == icon->iconId) {
215 ret.src = it->iconSrc;
216 ret.width = it->iconWidth;
217 ret.height = it->iconHeight;
225 return OptionalWidgetIcon::Null;
228 WidgetIconList getValidIconsList(
229 WrtDB::DbWidgetHandle widgetHandle)
231 WidgetDAOReadOnly dao(widgetHandle);
232 WidgetDAOReadOnly::WidgetIconList list = dao.getIconList();
234 WidgetIconList outlist;
238 LogDebug(":" << it->iconSrc);
239 if (!!getFilePathInWidgetPackage(widgetHandle,
243 ret.src = it->iconSrc;
244 ret.width = it->iconWidth;
245 ret.height = it->iconHeight;
246 outlist.push_back(ret);
252 OptionalWidgetStartFileInfo getStartFileInfo(
253 WrtDB::DbWidgetHandle widgetHandle)
255 WidgetStartFileInfo info;
257 WidgetDAOReadOnly dao(widgetHandle);
258 WidgetDAOReadOnly::LocalizedStartFileList locList =
259 dao.getLocalizedStartFileList();
260 WidgetDAOReadOnly::WidgetStartFileList list = dao.getStartFileList();
261 const LanguageTags tagsList = LanguageTagsProviderSingleton::Instance().getLanguageTags();
263 FOREACH(tag, tagsList)
265 FOREACH(sFile, locList)
267 if (*tag == sFile->widgetLocale) {
270 if (it->startFileId ==
271 sFile->startFileId) {
273 info.encoding = sFile->encoding;
274 info.type = sFile->type;
276 info.localizedPath = it->src;
278 info.localizedPath = L"locales/" + *tag + L"/";
279 info.localizedPath += it->src;
288 return OptionalWidgetStartFileInfo::Null;
291 WidgetLocalizedInfo getLocalizedInfo(const WrtDB::DbWidgetHandle handle)
293 return getLocalizedInfo(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(handle)));
296 WidgetLocalizedInfo getLocalizedInfo(WidgetDAOReadOnlyPtr dao)
298 LanguageTags languages = LanguageTagsProviderSingleton::Instance().getLanguageTags();
299 DPL::OptionalString dl = dao->getDefaultlocale();
301 languages.push_back(*dl);
304 WidgetLocalizedInfo result;
305 FOREACH(i, languages)
307 WidgetLocalizedInfo languageResult = dao->getLocalizedInfo(*i);
309 #define OVERWRITE_IF_NULL(FIELD) if (!result.FIELD) { \
310 result.FIELD = languageResult.FIELD; \
313 OVERWRITE_IF_NULL(name);
314 OVERWRITE_IF_NULL(shortName);
315 OVERWRITE_IF_NULL(description);
316 OVERWRITE_IF_NULL(license);
317 OVERWRITE_IF_NULL(licenseHref);
319 #undef OVERWRITE_IF_NULL