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