Revert "Overload methods to run with tizen id instead of widgetHandle"
[framework/web/wrt-commons.git] / modules / localization / src / w3c_file_localization.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file    w3c_file_localization.cpp
18  * @author  Lukasz Wrzosek (l.wrzosek@samsung.com)
19  * @version 1.0
20  */
21 #include <stddef.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <assert.h>
26
27 #include <dpl/localization/w3c_file_localization.h>
28
29 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
30 #include <dpl/localization/localization_utils.h>
31
32 #include <dpl/log/log.h>
33 #include <dpl/string.h>
34 #include <dpl/optional.h>
35 #include <dpl/foreach.h>
36
37 #include <LanguageTagsProvider.h>
38
39 using namespace WrtDB;
40
41 namespace {
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/";
45
46 DPL::Optional<std::string> GetFilePathInWidgetPackageInternal(
47         const std::string& basePath,
48         std::string filePath)
49 {
50     LogDebug("Looking for file: " << filePath << "  in: " << basePath);
51
52     const LanguageTags& ltags = LanguageTagsProviderSingleton::Instance().getLanguageTags();
53
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; }
62
63     LogDebug("locales size = " << ltags.size());
64     for (LanguageTags::const_iterator it = ltags.begin();
65             it != ltags.end();
66             ++it) {
67         LogDebug("Trying locale: " << *it);
68         std::string path = basePath;
69         if (path[path.size() - 1] == '/') {
70             path.erase(path.size() - 1);
71         }
72
73         if (it->empty()) {
74             path += "/" + filePath;
75         } else {
76             path += "/locales/" + DPL::ToUTF8String(*it) + "/" + filePath;
77         }
78
79         LogDebug("Trying locale: " << *it << " | " << path);
80         struct stat buf;
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);
85             }
86         }
87     }
88
89     return DPL::Optional<std::string>::Null;
90 }
91
92 DPL::Optional<DPL::String> GetFilePathInWidgetPackageInternal(
93         const DPL::String& basePath,
94         const DPL::String& filePath)
95 {
96     DPL::Optional<std::string> path =
97         GetFilePathInWidgetPackageInternal(DPL::ToUTF8String(basePath),
98                                            DPL::ToUTF8String(filePath));
99     DPL::Optional<DPL::String> dplPath;
100     if (!!path) {
101         dplPath = DPL::FromUTF8String(*path);
102     }
103     return dplPath;
104 }
105 }
106
107 namespace W3CFileLocalization {
108 DPL::Optional<DPL::String> getFilePathInWidgetPackageFromUrl(
109         DbWidgetHandle widgetHandle,
110         const DPL::String &url)
111 {
112     DPL::String req = url;
113     WidgetDAOReadOnly dao(widgetHandle);
114
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());
121         }
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);
129             }
130         }
131     } else {
132         LogDebug("Unknown path format, ignoring");
133         return DPL::Optional<DPL::String>::Null;
134     }
135
136     auto widgetPath = dao.getPath();
137
138     DPL::Optional<DPL::String> found =
139         GetFilePathInWidgetPackageInternal(widgetPath, req);
140
141     if (!found) {
142         LogError("Path not found within current locale in current widget");
143         return DPL::Optional<DPL::String>::Null;
144     }
145
146     found = widgetPath + *found;
147
148     return found;
149 }
150
151 DPL::Optional<DPL::String> getFilePathInWidgetPackage(
152         WrtDB::DbWidgetHandle widgetHandle,
153         const DPL::String& file)
154 {
155     WidgetDAOReadOnly dao(widgetHandle);
156     return GetFilePathInWidgetPackageInternal(dao.getPath(), file);
157 }
158
159 DPL::OptionalString getStartFile(const WrtDB::WidgetPkgName & pkgname)
160 {
161     return getStartFile(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(pkgname)));
162 }
163
164 DPL::OptionalString getStartFile(const WrtDB::DbWidgetHandle handle)
165 {
166     return getStartFile(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(handle)));
167 }
168
169 DPL::OptionalString getStartFile(WrtDB::WidgetDAOReadOnlyPtr dao)
170 {
171     WidgetDAOReadOnly::LocalizedStartFileList locList = dao->getLocalizedStartFileList();
172     WidgetDAOReadOnly::WidgetStartFileList list = dao->getStartFileList();
173     LanguageTags tagsList = LanguageTagsProviderSingleton::Instance().getLanguageTags();
174
175     DPL::OptionalString defaultLoc = dao->getDefaultlocale();
176     if (!!defaultLoc) {
177         tagsList.push_back(*defaultLoc);
178     }
179
180     FOREACH(tag, tagsList)
181     {
182         FOREACH(sFile, locList)
183         {
184             if (*tag == sFile->widgetLocale) {
185                 FOREACH(it, list)
186                 {
187                     if (it->startFileId == sFile->startFileId) {
188                         return it->src;
189                     }
190                 }
191             }
192         }
193     }
194
195     return DPL::OptionalString::Null;
196 }
197
198 OptionalWidgetIcon getIcon(const WrtDB::WidgetPkgName & pkgname)
199 {
200     return getIcon(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(pkgname)));
201 }
202
203 OptionalWidgetIcon getIcon(WrtDB::DbWidgetHandle widgetHandle)
204 {
205     return getIcon(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(widgetHandle)));
206 }
207
208 OptionalWidgetIcon getIcon(WrtDB::WidgetDAOReadOnlyPtr dao)
209 {
210     WidgetDAOReadOnly::WidgetLocalizedIconList locList = dao->getLocalizedIconList();
211     WidgetDAOReadOnly::WidgetIconList list = dao->getIconList();
212     LanguageTags tagsList = LanguageTagsProviderSingleton::Instance().getLanguageTags();
213
214     DPL::OptionalString defaultLoc = dao->getDefaultlocale();
215     if (!!defaultLoc) {
216         tagsList.push_back(*defaultLoc);
217     }
218
219     FOREACH(tag, tagsList)
220     {
221         FOREACH(icon, locList)
222         {
223             if (*tag == icon->widgetLocale) {
224                 FOREACH(it, list)
225                 {
226                     if (it->iconId == icon->iconId) {
227                         WidgetIcon ret;
228                         ret.src = it->iconSrc;
229                         ret.width = it->iconWidth;
230                         ret.height = it->iconHeight;
231                         return ret;
232                     }
233                 }
234             }
235         }
236     }
237
238     return OptionalWidgetIcon::Null;
239 }
240
241 WidgetIconList getValidIconsList(
242         WrtDB::DbWidgetHandle widgetHandle)
243 {
244     WidgetDAOReadOnly dao(widgetHandle);
245     WidgetDAOReadOnly::WidgetIconList list = dao.getIconList();
246
247     WidgetIconList outlist;
248
249     FOREACH(it, list)
250     {
251         LogDebug(":" << it->iconSrc);
252         if (!!getFilePathInWidgetPackage(widgetHandle,
253                                          it->iconSrc))
254         {
255             WidgetIcon ret;
256             ret.src = it->iconSrc;
257             ret.width = it->iconWidth;
258             ret.height = it->iconHeight;
259             outlist.push_back(ret);
260         }
261     }
262     return outlist;
263 }
264
265 OptionalWidgetStartFileInfo getStartFileInfo(
266         WrtDB::DbWidgetHandle widgetHandle)
267 {
268     WidgetStartFileInfo info;
269
270     WidgetDAOReadOnly dao(widgetHandle);
271     WidgetDAOReadOnly::LocalizedStartFileList locList =
272         dao.getLocalizedStartFileList();
273     WidgetDAOReadOnly::WidgetStartFileList list = dao.getStartFileList();
274     const LanguageTags tagsList = LanguageTagsProviderSingleton::Instance().getLanguageTags();
275
276     FOREACH(tag, tagsList)
277     {
278         FOREACH(sFile, locList)
279         {
280             if (*tag == sFile->widgetLocale) {
281                 FOREACH(it, list)
282                 {
283                     if (it->startFileId ==
284                         sFile->startFileId) {
285                         info.file = it->src;
286                         info.encoding = sFile->encoding;
287                         info.type = sFile->type;
288                         if (tag->empty()) {
289                             info.localizedPath = it->src;
290                         } else {
291                             info.localizedPath = L"locales/" + *tag + L"/";
292                             info.localizedPath += it->src;
293                         }
294                         return info;
295                     }
296                 }
297             }
298         }
299     }
300
301     return OptionalWidgetStartFileInfo::Null;
302 }
303
304 WidgetLocalizedInfo getLocalizedInfo(const WrtDB::DbWidgetHandle handle)
305 {
306     return getLocalizedInfo(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(handle)));
307 }
308
309 WidgetLocalizedInfo getLocalizedInfo(const WrtDB::WidgetPkgName & pkgname)
310 {
311     return getLocalizedInfo(WidgetDAOReadOnlyPtr(new WidgetDAOReadOnly(pkgname)));
312 }
313
314 WidgetLocalizedInfo getLocalizedInfo(WidgetDAOReadOnlyPtr dao)
315 {
316     LanguageTags languages = LanguageTagsProviderSingleton::Instance().getLanguageTags();
317     DPL::OptionalString dl = dao->getDefaultlocale();
318     if (!!dl) {
319         languages.push_back(*dl);
320     }
321
322     WidgetLocalizedInfo result;
323     FOREACH(i, languages)
324     {
325         WidgetLocalizedInfo languageResult = dao->getLocalizedInfo(*i);
326
327 #define OVERWRITE_IF_NULL(FIELD) if (!result.FIELD) { \
328         result.FIELD = languageResult.FIELD; \
329 }
330
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);
336
337 #undef OVERWRITE_IF_NULL
338     }
339
340     return result;
341 }
342 }