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::TizenAppId &tzAppId,
131 const DPL::String &url)
133 return getFilePathInWidgetPackageFromUrl(
134 WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(tzAppId)),
138 DPL::Optional<DPL::String> getFilePathInWidgetPackageFromUrl(
139 WrtDB::WidgetDAOReadOnlyPtr dao,
140 const DPL::String &url)
142 DPL::String req = url;
145 DPL::String::size_type pos = req.find_first_of('#');
146 if(pos != DPL::String::npos)
148 suffix = req.substr(pos) + suffix;
149 req.resize(pos); //truncate fragment identifier
152 pos = req.find_first_of('?');
153 if(pos != DPL::String::npos)
155 suffix = req.substr(pos) + suffix;
156 req.resize(pos); //truncate query string
159 if (req.find(WIDGET_URI_BEGIN) == 0) {
160 req.erase(0, WIDGET_URI_BEGIN.length());
161 } else if (req.find(FILE_URI_BEGIN) == 0) {
162 req.erase(0, FILE_URI_BEGIN.length());
163 if (req.find(dao->getPath()) == 0) {
164 req.erase(0, dao->getPath().length());
166 if (req.find(LOCALE_PREFIX) == 0) {
167 req.erase(0, LOCALE_PREFIX.length());
168 size_t position = req.find('/');
169 // should always be >0 as correct locales path is
170 // always locales/xx/ or locales/xx-XX/
171 if (position != std::string::npos && position > 0) {
172 req.erase(0, position + 1);
175 } else if(req.find(APP_URI_BEGIN) == 0) {
176 req.erase(0, APP_URI_BEGIN.length());
177 DPL::String id = *dao->getTizenAppId();
178 if(req.substr(0, id.size()) != id)
180 LogError("Tizen id does not match, ignoring");
181 return DPL::Optional<DPL::String>::Null;
183 req.erase(0, id.length());
185 LogDebug("Unknown path format, ignoring");
186 return DPL::Optional<DPL::String>::Null;
189 auto widgetPath = dao->getPath();
191 LogDebug("Required path: " << req);
192 DPL::Optional<DPL::String> found =
193 GetFilePathInWidgetPackageInternal(widgetPath, req);
196 LogError("Path not found within current locale in current widget");
197 return DPL::Optional<DPL::String>::Null;
200 found = widgetPath + *found + suffix;
205 DPL::Optional<DPL::String> getFilePathInWidgetPackage(
206 WrtDB::DbWidgetHandle widgetHandle,
207 const DPL::String& file)
209 return getFilePathInWidgetPackage(
210 WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(widgetHandle)),
214 DPL::Optional<DPL::String> getFilePathInWidgetPackage(
215 const WrtDB::TizenAppId &tzAppId,
216 const DPL::String& file)
218 return getFilePathInWidgetPackage(
219 WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(tzAppId)),
223 DPL::Optional<DPL::String> getFilePathInWidgetPackage(
224 WrtDB::WidgetDAOReadOnlyPtr dao,
225 const DPL::String& file)
227 return GetFilePathInWidgetPackageInternal(dao->getPath(), file);
230 DPL::OptionalString getStartFile(const WrtDB::TizenAppId & tzAppId)
232 return getStartFile(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(tzAppId)));
235 DPL::OptionalString getStartFile(const WrtDB::DbWidgetHandle handle)
237 return getStartFile(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(handle)));
240 DPL::OptionalString getStartFile(WrtDB::WidgetDAOReadOnlyPtr dao)
242 WidgetDAOReadOnly::LocalizedStartFileList locList =
243 dao->getLocalizedStartFileList();
244 WidgetDAOReadOnly::WidgetStartFileList list = dao->getStartFileList();
245 LanguageTags tagsList =
246 LanguageTagsProviderSingleton::Instance().getLanguageTags();
248 DPL::OptionalString defaultLoc = dao->getDefaultlocale();
250 tagsList.push_back(*defaultLoc);
253 FOREACH(tag, tagsList)
255 FOREACH(sFile, locList)
257 if (*tag == sFile->widgetLocale) {
260 if (it->startFileId == sFile->startFileId) {
268 return DPL::OptionalString::Null;
271 OptionalWidgetIcon getIcon(const WrtDB::TizenAppId & tzAppId)
273 return getIcon(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(tzAppId)));
276 OptionalWidgetIcon getIcon(WrtDB::DbWidgetHandle widgetHandle)
278 return getIcon(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(widgetHandle)));
281 OptionalWidgetIcon getIcon(WrtDB::WidgetDAOReadOnlyPtr dao)
283 WidgetDAOReadOnly::WidgetLocalizedIconList locList =
284 dao->getLocalizedIconList();
285 WidgetDAOReadOnly::WidgetIconList list = dao->getIconList();
286 LanguageTags tagsList =
287 LanguageTagsProviderSingleton::Instance().getLanguageTags();
289 DPL::OptionalString defaultLoc = dao->getDefaultlocale();
291 tagsList.push_back(*defaultLoc);
294 FOREACH(tag, tagsList)
296 FOREACH(icon, locList)
298 if (*tag == icon->widgetLocale) {
301 if (it->iconId == icon->iconId) {
303 ret.src = it->iconSrc;
304 ret.width = it->iconWidth;
305 ret.height = it->iconHeight;
313 return OptionalWidgetIcon::Null;
316 WidgetIconList getValidIconsList(WrtDB::DbWidgetHandle widgetHandle)
318 return getValidIconsList(
319 WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(widgetHandle)));
322 WidgetIconList getValidIconsList(const WrtDB::TizenAppId &tzAppId)
324 return getValidIconsList(
325 WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(tzAppId)));
328 WidgetIconList getValidIconsList(WrtDB::WidgetDAOReadOnlyPtr dao)
330 WidgetDAOReadOnly::WidgetIconList list = dao->getIconList();
332 WidgetIconList outlist;
336 LogDebug(":" << it->iconSrc);
337 if (!!getFilePathInWidgetPackage(dao->getHandle(),
341 ret.src = it->iconSrc;
342 ret.width = it->iconWidth;
343 ret.height = it->iconHeight;
344 outlist.push_back(ret);
350 OptionalWidgetStartFileInfo getStartFileInfo(WrtDB::DbWidgetHandle widgetHandle)
352 return getStartFileInfo(
353 WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(widgetHandle)));
356 OptionalWidgetStartFileInfo getStartFileInfo(
357 const WrtDB::TizenAppId &tzAppId)
359 return getStartFileInfo(
360 WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(tzAppId)));
363 OptionalWidgetStartFileInfo getStartFileInfo(WrtDB::WidgetDAOReadOnlyPtr dao)
365 WidgetStartFileInfo info;
367 WidgetDAOReadOnly::LocalizedStartFileList locList =
368 dao->getLocalizedStartFileList();
369 WidgetDAOReadOnly::WidgetStartFileList list = dao->getStartFileList();
370 const LanguageTags tagsList =
371 LanguageTagsProviderSingleton::Instance().getLanguageTags();
373 FOREACH(tag, tagsList)
375 FOREACH(sFile, locList)
377 if (*tag == sFile->widgetLocale) {
380 if (it->startFileId ==
384 info.encoding = sFile->encoding;
385 info.type = sFile->type;
387 info.localizedPath = it->src;
389 info.localizedPath = L"locales/" + *tag + L"/";
390 info.localizedPath += it->src;
399 return OptionalWidgetStartFileInfo::Null;
402 WidgetLocalizedInfo getLocalizedInfo(const WrtDB::DbWidgetHandle handle)
404 return getLocalizedInfo(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(handle)));
407 WidgetLocalizedInfo getLocalizedInfo(const WrtDB::TizenAppId & tzAppId)
409 return getLocalizedInfo(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(tzAppId)));
412 WidgetLocalizedInfo getLocalizedInfo(WidgetDAOReadOnlyPtr dao)
414 LanguageTags languages =
415 LanguageTagsProviderSingleton::Instance().getLanguageTags();
416 DPL::OptionalString dl = dao->getDefaultlocale();
418 languages.push_back(*dl);
421 WidgetLocalizedInfo result;
422 FOREACH(i, languages)
424 WidgetLocalizedInfo languageResult = dao->getLocalizedInfo(*i);
426 #define OVERWRITE_IF_NULL(FIELD) if (!result.FIELD) { \
427 result.FIELD = languageResult.FIELD; \
430 OVERWRITE_IF_NULL(name);
431 OVERWRITE_IF_NULL(shortName);
432 OVERWRITE_IF_NULL(description);
433 OVERWRITE_IF_NULL(license);
434 OVERWRITE_IF_NULL(licenseHref);
436 #undef OVERWRITE_IF_NULL