tizen beta release
[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
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25
26 #include <dpl/localization/w3c_file_localization.h>
27
28 #include <dpl/wrt-dao-rw/widget_dao.h>
29 #include <dpl/localization/localization_utils.h>
30
31 #include <dpl/log/log.h>
32 #include <dpl/string.h>
33 #include <dpl/optional.h>
34 #include <dpl/foreach.h>
35
36 using namespace WrtDB;
37
38 namespace {
39 const DPL::String FILE_URI_BEGIN = L"file://";
40 const DPL::String WIDGET_URI_BEGIN = L"widget://";
41
42 DPL::Optional<std::string> GetFilePathInWidgetPackageInternal(
43         const LanguageTagsList &tags,
44         const std::string& basePath,
45         std::string filePath)
46 {
47     LogDebug("Looking for file: " << filePath << "  in: " << basePath);
48     using namespace LocalizationUtils;
49
50     //Check if string isn't empty
51     if (filePath.size() == 0) { return DPL::Optional<std::string>::Null; }
52     //Removing preceding '/'
53     if (filePath[0] == '/') { filePath.erase(0, 1); }
54     //Check if string isn't empty
55     if (filePath.size() == 0) { return DPL::Optional<std::string>::Null; }
56
57     LogDebug("locales size = " << tags.size());
58     for (LanguageTagsList::const_iterator it = tags.begin();
59          it != tags.end();
60          ++it) {
61         LogDebug("Trying locale: " << *it);
62         std::string path = basePath;
63         if (path[path.size() - 1] == '/') {
64             path.erase(path.size() - 1);
65         }
66
67         if (it->empty()) {
68             path += "/" + filePath;
69         } else {
70             path += "/locales/" + DPL::ToUTF8String(*it) + "/" + filePath;
71         }
72
73         LogDebug("Trying locale: " << *it << " | " << path);
74         struct stat buf;
75         if (0 == stat(path.c_str(), &buf)) {
76             if ((buf.st_mode & S_IFMT) == S_IFREG) {
77                 path.erase(0, basePath.length());
78                 return DPL::Optional<std::string>(path);
79             }
80         }
81     }
82
83     return DPL::Optional<std::string>::Null;
84 }
85
86 DPL::Optional<DPL::String> GetFilePathInWidgetPackageInternal(
87         const LanguageTagsList &languageTags,
88         const DPL::String& basePath,
89         const DPL::String& filePath)
90 {
91     DPL::Optional<std::string> path =
92         GetFilePathInWidgetPackageInternal(languageTags,
93                                            DPL::ToUTF8String(basePath),
94                                            DPL::ToUTF8String(filePath));
95     DPL::Optional<DPL::String> dplPath;
96     if (!!path) {
97         dplPath = DPL::FromUTF8String(*path);
98     }
99     return dplPath;
100 }
101 }
102
103 namespace W3CFileLocalization {
104 DPL::Optional<DPL::String> getFilePathInWidgetPackageFromUrl(
105         DbWidgetHandle widgetHandle,
106         const LanguageTagsList &languageTags,
107         const DPL::String &url)
108 {
109     DPL::String req = url;
110     WidgetDAO dao(widgetHandle);
111
112     if (req.find(WIDGET_URI_BEGIN) == 0) {
113         req.erase(0, WIDGET_URI_BEGIN.length());
114     } else if (req.find(FILE_URI_BEGIN) == 0) {
115         req.erase(0, FILE_URI_BEGIN.length());
116         if (req.find(dao.getPath()) == 0) {
117             req.erase(0, dao.getPath().length());
118         }
119     } else {
120         LogDebug("Unknown path format, ignoring");
121         return DPL::Optional<DPL::String>::Null;
122     }
123
124     auto widgetPath = dao.getPath();
125
126     DPL::Optional<DPL::String> found =
127         GetFilePathInWidgetPackageInternal(languageTags, widgetPath, req);
128
129     if (!found) {
130         LogError("Path not found within current locale in current widget");
131         return DPL::Optional<DPL::String>::Null;
132     }
133
134     found = widgetPath + *found;
135
136     return found;
137 }
138
139 DPL::Optional<DPL::String> getFilePathInWidgetPackage(
140         WrtDB::DbWidgetHandle widgetHandle,
141         const LanguageTagsList &languageTags,
142         const DPL::String& file)
143 {
144     WidgetDAO dao(widgetHandle);
145     return GetFilePathInWidgetPackageInternal(languageTags, dao.getPath(), file);
146 }
147
148 DPL::OptionalString getStartFile(const WrtDB::DbWidgetHandle widgetHandle)
149 {
150     using namespace LocalizationUtils;
151
152     WidgetDAO dao(widgetHandle);
153
154     WidgetDAO::LocalizedStartFileList locList = dao.getLocalizedStartFileList();
155     WidgetDAO::WidgetStartFileList list = dao.getStartFileList();
156     LanguageTagsList tagsList = LocalizationUtils::GetUserAgentLanguageTags();
157
158     DPL::OptionalString defaultLoc = dao.getDefaultlocale();
159     if (!!defaultLoc) {
160         tagsList.push_back(*defaultLoc);
161     }
162
163     FOREACH(tag, tagsList)
164     {
165         FOREACH(sFile, locList)
166         {
167             if (*tag == sFile->widgetLocale) {
168                 FOREACH(it, list)
169                 {
170                     if (it->startFileId == sFile->startFileId) {
171                         return it->src;
172                     }
173                 }
174             }
175         }
176     }
177
178     return DPL::OptionalString::Null;
179 }
180
181 OptionalWidgetIcon getIcon(const WrtDB::DbWidgetHandle widgetHandle)
182 {
183     using namespace LocalizationUtils;
184     WidgetDAO dao(widgetHandle);
185
186     WidgetDAO::WidgetLocalizedIconList locList = dao.getLocalizedIconList();
187     WidgetDAO::WidgetIconList list = dao.getIconList();
188     LanguageTagsList tagsList = LocalizationUtils::GetUserAgentLanguageTags();
189
190     DPL::OptionalString defaultLoc = dao.getDefaultlocale();
191     if (!!defaultLoc) {
192         tagsList.push_back(*defaultLoc);
193     }
194
195     FOREACH(tag, tagsList)
196     {
197         FOREACH(icon, locList)
198         {
199             if (*tag == icon->widgetLocale) {
200                 FOREACH(it, list)
201                 {
202                     if (it->iconId == icon->iconId) {
203                         WidgetIcon ret;
204                         ret.src = it->iconSrc;
205                         ret.width = it->iconWidth;
206                         ret.height = it->iconHeight;
207                         return ret;
208                     }
209                 }
210             }
211         }
212     }
213
214     return OptionalWidgetIcon::Null;
215 }
216
217 WidgetIconList getValidIconsList(
218         WrtDB::DbWidgetHandle widgetHandle,
219         const LanguageTagsList &languageTags)
220 {
221     using namespace LocalizationUtils;
222     WidgetDAO dao(widgetHandle);
223     WidgetDAO::WidgetIconList list = dao.getIconList();
224
225     WidgetIconList outlist;
226
227     FOREACH(it, list)
228     {
229         LogDebug(":" << it->iconSrc);
230         if (!!getFilePathInWidgetPackage(widgetHandle,
231                                          languageTags,
232                                          it->iconSrc))
233         {
234             WidgetIcon ret;
235             ret.src = it->iconSrc;
236             ret.width = it->iconWidth;
237             ret.height = it->iconHeight;
238             outlist.push_back(ret);
239         }
240     }
241     return outlist;
242 }
243
244 OptionalWidgetStartFileInfo getStartFileInfo(
245         WrtDB::DbWidgetHandle widgetHandle,
246         const LanguageTagList &tagsList)
247 {
248     using namespace LocalizationUtils;
249
250     WidgetStartFileInfo info;
251
252     WidgetDAO dao(widgetHandle);
253     WidgetDAO::LocalizedStartFileList locList =
254         dao.getLocalizedStartFileList();
255     WidgetDAO::WidgetStartFileList list = dao.getStartFileList();
256
257     FOREACH(tag, tagsList)
258     {
259         FOREACH(sFile, locList)
260         {
261             if (*tag == sFile->widgetLocale) {
262                 FOREACH(it, list)
263                 {
264                     if (it->startFileId ==
265                         sFile->startFileId) {
266                         info.file = it->src;
267                         info.encoding = sFile->encoding;
268                         info.type = sFile->type;
269                         if (tag->empty()) {
270                             info.localizedPath = it->src;
271                         } else {
272                             info.localizedPath = L"locales/" + *tag;
273                             info.localizedPath += it->src;
274                         }
275                         return info;
276                     }
277                 }
278             }
279         }
280     }
281
282     return OptionalWidgetStartFileInfo::Null;
283 }
284
285 WidgetLocalizedInfo getLocalizedInfo(const WrtDB::DbWidgetHandle handle)
286 {
287     LanguageTagList languages =
288         LocalizationUtils::GetUserAgentLanguageTags();
289     WidgetDAO dao(handle);
290     DPL::OptionalString dl = dao.getDefaultlocale();
291     if (!!dl) {
292         languages.push_back(*dl);
293     }
294
295     WidgetLocalizedInfo result;
296     FOREACH(i, languages)
297     {
298         WidgetLocalizedInfo languageResult = dao.getLocalizedInfo(*i);
299
300 #define OVERWRITE_IF_NULL(FIELD) if (!result.FIELD) { \
301         result.FIELD = languageResult.FIELD; \
302 }
303
304         OVERWRITE_IF_NULL(name);
305         OVERWRITE_IF_NULL(shortName);
306         OVERWRITE_IF_NULL(description);
307         OVERWRITE_IF_NULL(license);
308         OVERWRITE_IF_NULL(licenseHref);
309
310 #undef OVERWRITE_IF_NULL
311     }
312
313     return result;
314 }
315 }