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