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