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 APP_URI_BEGIN = L"app://";
45 const DPL::String LOCALE_PREFIX = L"locales/";
47 DPL::Optional<std::string> GetFilePathInWidgetPackageInternal(
48 const std::string& basePath,
51 LogDebug("Looking for file: " << filePath << " in: " << basePath);
53 const LanguageTags& ltags =
54 LanguageTagsProviderSingleton::Instance().getLanguageTags();
56 //Check if string isn't empty
57 if (filePath.size() == 0) {
58 return DPL::Optional<std::string>::Null;
60 //Removing preceding '/'
61 if (filePath[0] == '/') {
64 // In some cases (start file localization) url has unnecessary "/" at the
66 if (filePath[filePath.size() - 1] == '/') {
67 filePath.erase(filePath.size() - 1, 1);
69 //Check if string isn't empty
70 if (filePath.size() == 0) {
71 return DPL::Optional<std::string>::Null;
74 LogDebug("locales size = " << ltags.size());
75 for (LanguageTags::const_iterator it = ltags.begin();
79 LogDebug("Trying locale: " << *it);
80 std::string path = basePath;
81 if (path[path.size() - 1] == '/') {
82 path.erase(path.size() - 1);
86 path += "/" + filePath;
88 path += "/locales/" + DPL::ToUTF8String(*it) + "/" + filePath;
91 LogDebug("Trying locale: " << *it << " | " << path);
93 if (0 == stat(path.c_str(), &buf)) {
94 if ((buf.st_mode & S_IFMT) == S_IFREG) {
95 path.erase(0, basePath.length());
96 return DPL::Optional<std::string>(path);
101 return DPL::Optional<std::string>::Null;
104 DPL::Optional<DPL::String> GetFilePathInWidgetPackageInternal(
105 const DPL::String& basePath,
106 const DPL::String& filePath)
108 DPL::Optional<std::string> path =
109 GetFilePathInWidgetPackageInternal(DPL::ToUTF8String(basePath),
110 DPL::ToUTF8String(filePath));
111 DPL::Optional<DPL::String> dplPath;
113 dplPath = DPL::FromUTF8String(*path);
119 namespace W3CFileLocalization {
120 DPL::Optional<DPL::String> getFilePathInWidgetPackageFromUrl(
121 DbWidgetHandle widgetHandle,
122 const DPL::String &url)
124 return getFilePathInWidgetPackageFromUrl(
125 WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(widgetHandle)),
129 DPL::Optional<DPL::String> getFilePathInWidgetPackageFromUrl(
130 const WrtDB::WidgetPkgName &pkgname,
131 const DPL::String &url)
133 return getFilePathInWidgetPackageFromUrl(
134 WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(pkgname)),
138 DPL::Optional<DPL::String> getFilePathInWidgetPackageFromUrl(
139 WrtDB::WidgetDAOReadOnlyPtr dao,
140 const DPL::String &url)
142 DPL::String req = url;
144 if (req.find(WIDGET_URI_BEGIN) == 0) {
145 req.erase(0, WIDGET_URI_BEGIN.length());
146 } else if (req.find(FILE_URI_BEGIN) == 0) {
147 req.erase(0, FILE_URI_BEGIN.length());
148 if (req.find(dao->getPath()) == 0) {
149 req.erase(0, dao->getPath().length());
151 if (req.find(LOCALE_PREFIX) == 0) {
152 req.erase(0, LOCALE_PREFIX.length());
153 size_t position = req.find('/');
154 // should always be >0 as correct locales path is
155 // always locales/xx/ or locales/xx-XX/
156 if (position != std::string::npos && position > 0) {
157 req.erase(0, position + 1);
160 } else if(req.find(APP_URI_BEGIN) == 0) {
161 req.erase(0, APP_URI_BEGIN.length());
162 DPL::String id = *dao->getTizenAppId();
163 if(req.substr(0, id.size()) != id)
165 LogError("Tizen id does not match, ignoring");
166 return DPL::Optional<DPL::String>::Null;
168 req.erase(0, id.length());
170 LogDebug("Unknown path format, ignoring");
171 return DPL::Optional<DPL::String>::Null;
174 auto widgetPath = dao->getPath();
176 LogDebug("Required path: " << req);
177 DPL::Optional<DPL::String> found =
178 GetFilePathInWidgetPackageInternal(widgetPath, req);
181 LogError("Path not found within current locale in current widget");
182 return DPL::Optional<DPL::String>::Null;
185 found = widgetPath + *found;
190 DPL::Optional<DPL::String> getFilePathInWidgetPackage(
191 WrtDB::DbWidgetHandle widgetHandle,
192 const DPL::String& file)
194 return getFilePathInWidgetPackage(
195 WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(widgetHandle)),
199 DPL::Optional<DPL::String> getFilePathInWidgetPackage(
200 const WrtDB::WidgetPkgName &pkgname,
201 const DPL::String& file)
203 return getFilePathInWidgetPackage(
204 WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(pkgname)),
208 DPL::Optional<DPL::String> getFilePathInWidgetPackage(
209 WrtDB::WidgetDAOReadOnlyPtr dao,
210 const DPL::String& file)
212 return GetFilePathInWidgetPackageInternal(dao->getPath(), file);
215 DPL::OptionalString getStartFile(const WrtDB::WidgetPkgName & pkgname)
217 return getStartFile(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(pkgname)));
220 DPL::OptionalString getStartFile(const WrtDB::DbWidgetHandle handle)
222 return getStartFile(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(handle)));
225 DPL::OptionalString getStartFile(WrtDB::WidgetDAOReadOnlyPtr dao)
227 WidgetDAOReadOnly::LocalizedStartFileList locList =
228 dao->getLocalizedStartFileList();
229 WidgetDAOReadOnly::WidgetStartFileList list = dao->getStartFileList();
230 LanguageTags tagsList =
231 LanguageTagsProviderSingleton::Instance().getLanguageTags();
233 DPL::OptionalString defaultLoc = dao->getDefaultlocale();
235 tagsList.push_back(*defaultLoc);
238 FOREACH(tag, tagsList)
240 FOREACH(sFile, locList)
242 if (*tag == sFile->widgetLocale) {
245 if (it->startFileId == sFile->startFileId) {
253 return DPL::OptionalString::Null;
256 OptionalWidgetIcon getIcon(const WrtDB::WidgetPkgName & pkgname)
258 return getIcon(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(pkgname)));
261 OptionalWidgetIcon getIcon(WrtDB::DbWidgetHandle widgetHandle)
263 return getIcon(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(widgetHandle)));
266 OptionalWidgetIcon getIcon(WrtDB::WidgetDAOReadOnlyPtr dao)
268 WidgetDAOReadOnly::WidgetLocalizedIconList locList =
269 dao->getLocalizedIconList();
270 WidgetDAOReadOnly::WidgetIconList list = dao->getIconList();
271 LanguageTags tagsList =
272 LanguageTagsProviderSingleton::Instance().getLanguageTags();
274 DPL::OptionalString defaultLoc = dao->getDefaultlocale();
276 tagsList.push_back(*defaultLoc);
279 FOREACH(tag, tagsList)
281 FOREACH(icon, locList)
283 if (*tag == icon->widgetLocale) {
286 if (it->iconId == icon->iconId) {
288 ret.src = it->iconSrc;
289 ret.width = it->iconWidth;
290 ret.height = it->iconHeight;
298 return OptionalWidgetIcon::Null;
301 WidgetIconList getValidIconsList(WrtDB::DbWidgetHandle widgetHandle)
303 return getValidIconsList(
304 WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(widgetHandle)));
307 WidgetIconList getValidIconsList(const WrtDB::WidgetPkgName &pkgname)
309 return getValidIconsList(
310 WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(pkgname)));
313 WidgetIconList getValidIconsList(WrtDB::WidgetDAOReadOnlyPtr dao)
315 WidgetDAOReadOnly::WidgetIconList list = dao->getIconList();
317 WidgetIconList outlist;
321 LogDebug(":" << it->iconSrc);
322 if (!!getFilePathInWidgetPackage(dao->getHandle(),
326 ret.src = it->iconSrc;
327 ret.width = it->iconWidth;
328 ret.height = it->iconHeight;
329 outlist.push_back(ret);
335 OptionalWidgetStartFileInfo getStartFileInfo(WrtDB::DbWidgetHandle widgetHandle)
337 return getStartFileInfo(
338 WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(widgetHandle)));
341 OptionalWidgetStartFileInfo getStartFileInfo(
342 const WrtDB::WidgetPkgName &pkgname)
344 return getStartFileInfo(
345 WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(pkgname)));
348 OptionalWidgetStartFileInfo getStartFileInfo(WrtDB::WidgetDAOReadOnlyPtr dao)
350 WidgetStartFileInfo info;
352 WidgetDAOReadOnly::LocalizedStartFileList locList =
353 dao->getLocalizedStartFileList();
354 WidgetDAOReadOnly::WidgetStartFileList list = dao->getStartFileList();
355 const LanguageTags tagsList =
356 LanguageTagsProviderSingleton::Instance().getLanguageTags();
358 FOREACH(tag, tagsList)
360 FOREACH(sFile, locList)
362 if (*tag == sFile->widgetLocale) {
365 if (it->startFileId ==
369 info.encoding = sFile->encoding;
370 info.type = sFile->type;
372 info.localizedPath = it->src;
374 info.localizedPath = L"locales/" + *tag + L"/";
375 info.localizedPath += it->src;
384 return OptionalWidgetStartFileInfo::Null;
387 WidgetLocalizedInfo getLocalizedInfo(const WrtDB::DbWidgetHandle handle)
389 return getLocalizedInfo(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(handle)));
392 WidgetLocalizedInfo getLocalizedInfo(const WrtDB::WidgetPkgName & pkgname)
394 return getLocalizedInfo(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(pkgname)));
397 WidgetLocalizedInfo getLocalizedInfo(WidgetDAOReadOnlyPtr dao)
399 LanguageTags languages =
400 LanguageTagsProviderSingleton::Instance().getLanguageTags();
401 DPL::OptionalString dl = dao->getDefaultlocale();
403 languages.push_back(*dl);
406 WidgetLocalizedInfo result;
407 FOREACH(i, languages)
409 WidgetLocalizedInfo languageResult = dao->getLocalizedInfo(*i);
411 #define OVERWRITE_IF_NULL(FIELD) if (!result.FIELD) { \
412 result.FIELD = languageResult.FIELD; \
415 OVERWRITE_IF_NULL(name);
416 OVERWRITE_IF_NULL(shortName);
417 OVERWRITE_IF_NULL(description);
418 OVERWRITE_IF_NULL(license);
419 OVERWRITE_IF_NULL(licenseHref);
421 #undef OVERWRITE_IF_NULL