Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_mobile / modules / tizen / WidgetDB / WidgetDB.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        WidgetDB.cpp
18  * @author      Andrzej Surdej (a.surdej@samsung.com)
19  * @version     0.1
20  * @brief
21  */
22
23 #include "WidgetDB.h"
24
25 #include <string>
26 #include <set>
27
28 #include <Commons/Exception.h>
29 #include <WidgetDB/IWidgetDB.h>
30 #include <WidgetDB/IWidgetFeature.h>
31 #include <dpl/wrt-dao-ro/property_dao_read_only.h>
32 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
33 #include <dpl/wrt-dao-ro/global_config.h>
34 #include <dpl/wrt-dao-ro/widget_config.h>
35 #include <dpl/wrt-dao-ro/feature_dao_read_only.h>
36 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
37 #include <dpl/wrt-dao-ro/feature_model.h>
38 #include <dpl/wrt-dao-ro/common_dao_types.h>
39
40 #include <dpl/localization/w3c_file_localization.h>
41
42 #include "WidgetFeature.h"
43
44 namespace WrtDeviceApis {
45 namespace WidgetDB {
46 WidgetDB::WidgetDB(int widgetId) : m_widgetId(widgetId) {}
47
48 int WidgetDB::getWidgetId() const
49 {
50     return m_widgetId;
51 }
52
53 std::string WidgetDB::getLanguage() const
54 {
55     LogDebug("getting language");
56
57     WrtDB::LanguageTagList languageTags;
58     std::string outLanguage;
59     Try
60     {
61         WrtDB::WidgetDAOReadOnly dao(getWidgetId());
62         languageTags = dao.getLanguageTags();
63     }
64     Catch(DPL::Exception)
65     {
66         ReThrow(Commons::SecurityException);
67     }
68
69     // get 'en' language or first from the list if 'en' does not exist
70     if (languageTags.size() > 1) {
71         FOREACH(lang, languageTags)
72         {
73             if ("en" == DPL::ToUTF8String(*lang)) {
74                 return "en";
75             }
76         }
77     }
78
79     if (!languageTags.empty()) {
80         // get first language tag
81         outLanguage = DPL::ToUTF8String(*(languageTags.begin()));
82     }
83     return outLanguage;
84 }
85
86 /*License,LicenseHref,Description,Name can be \
87  *  returned with incorect language*/
88 std::string WidgetDB::getConfigValue(Api::ConfigAttribute attribute) const
89 {
90     LogDebug("entered");
91     std::string value;
92     Try
93     {
94         WrtDB::WidgetDAOReadOnly dao(getWidgetId());
95         WrtDB::WidgetLocalizedInfo widgetInfo =
96             W3CFileLocalization::getLocalizedInfo(getWidgetId());
97
98         std::ostringstream out;
99
100         switch (attribute) {
101         case Api::ConfigAttribute::ID: {
102             DPL::OptionalString val = dao.getGUID();
103             value = !val ? "" : DPL::ToUTF8String(*val);
104             break;
105         }
106
107         case Api::ConfigAttribute::VERSION: {
108             DPL::OptionalString val = dao.getVersion();
109             value = !val ? "" : DPL::ToUTF8String(*val);
110             break;
111         }
112
113         case Api::ConfigAttribute::DESCRIPTION: {
114             DPL::OptionalString val = widgetInfo.description;
115             value = !val ? "" : DPL::ToUTF8String(*val);
116             break;
117         }
118
119         case Api::ConfigAttribute::LICENSE: {
120             DPL::OptionalString val = widgetInfo.license;
121             value = !val ? "" : DPL::ToUTF8String(*val);
122             break;
123         }
124
125         case Api::ConfigAttribute::LICENSE_HREF: {
126             DPL::OptionalString val = widgetInfo.licenseHref;
127             value = !val ? "" : DPL::ToUTF8String(*val);
128             break;
129         }
130
131         case Api::ConfigAttribute::AUTHOR_NAME: {
132             DPL::OptionalString val = dao.getAuthorName();
133             value = !val ? "" : DPL::ToUTF8String(*val);
134             break;
135         }
136
137         case Api::ConfigAttribute::AUTHOR_EMAIL: {
138             DPL::OptionalString val = dao.getAuthorEmail();
139             value = !val ? "" : DPL::ToUTF8String(*val);
140             break;
141         }
142
143         case Api::ConfigAttribute::AUTHOR_HREF: {
144             DPL::OptionalString val = dao.getAuthorHref();
145             value = !val ? "" : DPL::ToUTF8String(*val);
146             break;
147         }
148
149         case Api::ConfigAttribute::NAME: {
150             DPL::OptionalString val = widgetInfo.name;
151             value = !val ? "" : DPL::ToUTF8String(*val);
152             break;
153         }
154
155         case Api::ConfigAttribute::SHORT_NAME: {
156             DPL::OptionalString val = widgetInfo.shortName;
157             value = !val ? "" : DPL::ToUTF8String(*val);
158             break;
159         }
160
161         case Api::ConfigAttribute::ACCESS_NETWORK:
162             value = dao.getAccessNetworkMode() ? "yes" : "no";
163             break;
164
165         case Api::ConfigAttribute::WIDTH: {
166             DPL::OptionalInt width = dao.getPreferredSize().width;
167             !width ? out << "0" : out << *width;
168             value = out.str();
169             break;
170         }
171
172         case Api::ConfigAttribute::HEIGHT: {
173             DPL::OptionalInt height = dao.getPreferredSize().height;
174             !height ? out << "0" : out << *height;
175             value = out.str();
176             break;
177         }
178
179         case Api::ConfigAttribute::INSTALL_PATH:
180             value = DPL::ToUTF8String(dao.getPath());
181             break;
182
183         case Api::ConfigAttribute::PUBLIC_STORAGE_PATH:
184             value = WrtDB::GlobalConfig::GetPublicVirtualRootPath();
185             break;
186
187         default:
188             LogWarning("Unknown attribute requested");
189             Throw(Commons::InvalidArgumentException);
190         }
191     }
192     Catch(Commons::InvalidArgumentException)
193     {
194         ReThrow(Commons::InvalidArgumentException);
195     }
196     Catch(DPL::Exception)
197     {
198         ReThrow(Commons::SecurityException);
199     }
200
201     LogDebug("exit");
202
203     return value;
204 }
205
206 std::string WidgetDB::getUserAgent() const
207 {
208     LogDebug("Widget User_Agent data get");
209
210     std::string outUA("not implemented");
211     return outUA;
212 }
213
214 Api::InstallationStatus WidgetDB::checkInstallationStatus(
215     const std::string& gid,
216     const std::string& name,
217     const std::string& version) const
218 {
219     LogDebug("Checking widget ( gid(" << gid << "), name(" << name <<
220              "), version(" << version << ")) installation status");
221
222     Api::InstallationStatus outStatus;
223     Try
224     {
225         const WrtDB::WidgetGUID w_guid =
226             WrtDB::WidgetGUID(DPL::FromUTF8String(gid));
227
228         try {
229             WrtDB::DbWidgetHandle widgetHandle =
230                 WrtDB::WidgetDAOReadOnly::getHandle(w_guid);
231
232             WrtDB::WidgetDAOReadOnly dao(widgetHandle);
233             DPL::OptionalString ver = dao.getVersion();
234             if (!ver || version > DPL::ToUTF8String(*ver)) {
235                 // if there is no version in DB
236                 // or DB version is lover or equal than provided
237                 outStatus = Api::InstallationStatus::STATUS_INSTALLED;
238             } else {
239                 outStatus = Api::InstallationStatus::STATUS_LATEST;
240             }
241         } catch (WrtDB::WidgetDAOReadOnly::Exception::WidgetNotExist) {
242             outStatus = Api::InstallationStatus::STATUS_UNINSTALLED;
243         }
244     }
245     Catch(DPL::Exception)
246     {
247         ReThrow(Commons::SecurityException);
248     }
249
250     return outStatus;
251 }
252
253 Api::Features WidgetDB::getWidgetFeatures() const
254 {
255     LogDebug("entered");
256     WrtDB::DbWidgetFeatureSet features;
257     Try
258     {
259         WrtDB::WidgetDAOReadOnly doa(getWidgetId());
260         features = doa.getFeaturesList();
261     }
262     Catch(DPL::Exception)
263     {
264         // exception handle
265         ReThrow(Commons::SecurityException);
266     }
267
268     Api::Features outFeatures;
269
270     LogDebug("There is no exception");
271
272     FOREACH(it, features)
273     {
274         Api::IWidgetFeaturePtr feature(
275             new WidgetFeature(DPL::ToUTF8String((*it).name),
276                               true));      // requested (WidgetFeature param)
277         outFeatures.push_back(feature);
278     }
279
280     return outFeatures;
281 }
282
283 Api::Features WidgetDB::getRegisteredFeatures() const
284 {
285     Api::Features outRegisteredFeatures;
286     Try
287     {
288         // initialize with our widget features (they are required)
289         outRegisteredFeatures = getWidgetFeatures();
290
291         // searching for another features
292
293         WrtDB::FeatureHandleList features =
294             WrtDB::FeatureDAOReadOnly::GetHandleList();
295
296         bool featureFound = false;
297
298         // throught all features
299         FOREACH(registeredFeature, features)
300         {
301             WrtDB::FeatureHandle handle = *registeredFeature;
302             WrtDB::FeatureDAOReadOnly dao(handle);
303
304             // check if this feature is aleready on outRegisteredFeatures list
305             // and if not, add it
306
307             FOREACH(it, outRegisteredFeatures)
308             {
309                 if (dao.GetName() == (*it)->getName()) {    // *it = shared ptr
310                     // this name is found on list, do not add
311                     featureFound = true;
312                     break;
313                 }
314             }
315
316             if (!featureFound) {
317                 // feature is not on outRegisteredFeature so add it with
318                 // 'required' property false
319                 Api::IWidgetFeaturePtr newFeature(new WidgetFeature(
320                                                       dao.GetName(),
321                                                       false));
322
323                 // TODO what about feature params? aren't they needed
324                 outRegisteredFeatures.push_back(newFeature);
325             }
326             // reset the flag and check another feature
327             featureFound = false;
328         }
329     }
330     Catch(DPL::Exception)
331     {
332         ReThrow(Commons::SecurityException);
333     }
334
335     return outRegisteredFeatures;
336 }
337
338 std::string WidgetDB::getWidgetInstallationPath() const
339 {
340     Try
341     {
342         WrtDB::WidgetDAOReadOnly dao(getWidgetId());
343         std::string result(DPL::ToUTF8String(dao.getFullPath()));
344         result.erase(0, strlen("file://"));
345         std::string::size_type pos = result.size();
346         std::string separator("/");
347         pos = result.rfind(separator, pos);
348         if ((result.size() - pos) > 0 && (result.size() - pos) < 2) {
349             result.erase(pos, result.size());
350         }
351         return result;
352     }
353     Catch(DPL::Exception)
354     {
355         ReThrow(Commons::PlatformException);
356     }
357 }
358
359 std::string WidgetDB::getWidgetPersistentStoragePath() const
360 {
361     Try
362     {
363         WrtDB::WidgetDAOReadOnly dao(getWidgetId());
364         return
365             WrtDB::WidgetConfig::GetWidgetPersistentStoragePath(
366                 dao.getTizenPkgId());
367     }
368     Catch(DPL::Exception)
369     {
370         ReThrow(Commons::PlatformException);
371     }
372 }
373
374 std::string WidgetDB::getWidgetTemporaryStoragePath() const
375 {
376     Try
377     {
378         WrtDB::WidgetDAOReadOnly dao(getWidgetId());
379         return
380             WrtDB::WidgetConfig::GetWidgetTemporaryStoragePath(
381                 dao.getTizenPkgId());
382     }
383     Catch(DPL::Exception)
384     {
385         ReThrow(Commons::PlatformException);
386     }
387 }
388 } // WidgetDB
389 } // WrtDeviceApis