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