merge with master
[platform/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/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                               (*it).required,
277                               true));      // requested (WidgetFeature param)
278         // iterating multimap
279         FOREACH(iterator, (*it).params)
280         {
281             // adding features with key(first) and value(second)
282             feature->addParam(DPL::ToUTF8String(iterator->first),
283                               DPL::ToUTF8String(iterator->second));
284         }
285
286         outFeatures.push_back(feature);
287     }
288
289     return outFeatures;
290 }
291
292 Api::Features WidgetDB::getRegisteredFeatures() const
293 {
294     Api::Features outRegisteredFeatures;
295     Try
296     {
297         // initialize with our widget features (they are required)
298         outRegisteredFeatures = getWidgetFeatures();
299
300         // searching for another features
301
302         WrtDB::FeatureHandleList features =
303             WrtDB::FeatureDAOReadOnly::GetHandleList();
304
305         bool featureFound = false;
306
307         // throught all features
308         FOREACH(registeredFeature, features)
309         {
310             WrtDB::FeatureHandle handle = *registeredFeature;
311             WrtDB::FeatureDAOReadOnly dao(handle);
312
313             // check if this feature is aleready on outRegisteredFeatures list
314             // and if not, add it
315
316             FOREACH(it, outRegisteredFeatures)
317             {
318                 if (dao.GetName() == (*it)->getName()) {    // *it = shared ptr
319                     // this name is found on list, do not add
320                     featureFound = true;
321                     break;
322                 }
323             }
324
325             if (!featureFound) {
326                 // feature is not on outRegisteredFeature so add it with
327                 // 'required' property false
328                 Api::IWidgetFeaturePtr newFeature(new WidgetFeature(
329                                                       dao.GetName(),
330                                                       false,
331                                                       false));
332
333                 // TODO what about feature params? aren't they needed
334                 outRegisteredFeatures.push_back(newFeature);
335             }
336             // reset the flag and check another feature
337             featureFound = false;
338         }
339     }
340     Catch(DPL::Exception)
341     {
342         ReThrow(Commons::SecurityException);
343     }
344
345     return outRegisteredFeatures;
346 }
347
348 std::string WidgetDB::getWidgetInstallationPath() const
349 {
350     Try
351     {
352         WrtDB::WidgetDAOReadOnly dao(getWidgetId());
353         std::string result(DPL::ToUTF8String(dao.getFullPath()));
354         result.erase(0, strlen("file://"));
355         std::string::size_type pos = result.size();
356         std::string separator("/");
357         pos = result.rfind(separator, pos);
358         if ((result.size() - pos) > 0 && (result.size() - pos) < 2) {
359             result.erase(pos, result.size());
360         }
361         return result;
362     }
363     Catch(DPL::Exception)
364     {
365         ReThrow(Commons::PlatformException);
366     }
367 }
368
369 std::string WidgetDB::getWidgetPersistentStoragePath() const
370 {
371     Try
372     {
373         WrtDB::WidgetDAOReadOnly dao(getWidgetId());
374         return
375             WrtDB::WidgetConfig::GetWidgetPersistentStoragePath(
376                 dao.getTizenPkgId());
377     }
378     Catch(DPL::Exception)
379     {
380         ReThrow(Commons::PlatformException);
381     }
382 }
383
384 std::string WidgetDB::getWidgetTemporaryStoragePath() const
385 {
386     Try
387     {
388         WrtDB::WidgetDAOReadOnly dao(getWidgetId());
389         return
390             WrtDB::WidgetConfig::GetWidgetTemporaryStoragePath(
391                 dao.getTizenPkgId());
392     }
393     Catch(DPL::Exception)
394     {
395         ReThrow(Commons::PlatformException);
396     }
397 }
398 } // WidgetDB
399 } // WrtDeviceApis