Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / 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::WidgetDAOReadOnlyPtr dao(new WrtDB::WidgetDAOReadOnly(getWidgetId()));
95         WrtDB::WidgetLocalizedInfo widgetInfo =
96             W3CFileLocalization::getLocalizedInfo(dao);
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::WIDTH: {
162             DPL::OptionalInt width = dao->getPreferredSize().width;
163             !width ? out << "0" : out << *width;
164             value = out.str();
165             break;
166         }
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
175         case Api::ConfigAttribute::INSTALL_PATH:
176             value = DPL::ToUTF8String(dao->getPath());
177             break;
178
179         case Api::ConfigAttribute::PUBLIC_STORAGE_PATH:
180             value = WrtDB::GlobalConfig::GetPublicVirtualRootPath();
181             break;
182
183         default:
184             LogWarning("Unknown attribute requested");
185             Throw(Commons::InvalidArgumentException);
186         }
187     }
188     Catch(Commons::InvalidArgumentException)
189     {
190         ReThrow(Commons::InvalidArgumentException);
191     }
192     Catch(DPL::Exception)
193     {
194         ReThrow(Commons::SecurityException);
195     }
196
197     LogDebug("exit");
198
199     return value;
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         try {
225             WrtDB::DbWidgetHandle widgetHandle =
226                 WrtDB::WidgetDAOReadOnly::getHandle(w_guid);
227
228             WrtDB::WidgetDAOReadOnly dao(widgetHandle);
229             DPL::OptionalString ver = dao.getVersion();
230             if (!ver || version > DPL::ToUTF8String(*ver)) {
231                 // if there is no version in DB
232                 // or DB version is lover or equal than provided
233                 outStatus = Api::InstallationStatus::STATUS_INSTALLED;
234             } else {
235                 outStatus = Api::InstallationStatus::STATUS_LATEST;
236             }
237         } catch (WrtDB::WidgetDAOReadOnly::Exception::WidgetNotExist) {
238             outStatus = Api::InstallationStatus::STATUS_UNINSTALLED;
239         }
240     }
241     Catch(DPL::Exception)
242     {
243         ReThrow(Commons::SecurityException);
244     }
245
246     return outStatus;
247 }
248
249 Api::Features WidgetDB::getWidgetFeatures() const
250 {
251     LogDebug("entered");
252     WrtDB::DbWidgetFeatureSet features;
253     Try
254     {
255         WrtDB::WidgetDAOReadOnly doa(getWidgetId());
256         features = doa.getFeaturesList();
257     }
258     Catch(DPL::Exception)
259     {
260         // exception handle
261         ReThrow(Commons::SecurityException);
262     }
263
264     Api::Features outFeatures;
265
266     LogDebug("There is no exception");
267
268     FOREACH(it, features)
269     {
270         Api::IWidgetFeaturePtr feature(
271             new WidgetFeature(DPL::ToUTF8String((*it).name),
272                               true));      // requested (WidgetFeature param)
273         outFeatures.push_back(feature);
274     }
275
276     return outFeatures;
277 }
278
279 Api::Features WidgetDB::getRegisteredFeatures() const
280 {
281     Api::Features outRegisteredFeatures;
282     Try
283     {
284         // initialize with our widget features (they are required)
285         outRegisteredFeatures = getWidgetFeatures();
286
287         // searching for another features
288
289         WrtDB::FeatureHandleList features =
290             WrtDB::FeatureDAOReadOnly::GetHandleList();
291
292         bool featureFound = false;
293
294         // throught all features
295         FOREACH(registeredFeature, features)
296         {
297             WrtDB::FeatureHandle handle = *registeredFeature;
298             WrtDB::FeatureDAOReadOnly dao(handle);
299
300             // check if this feature is aleready on outRegisteredFeatures list
301             // and if not, add it
302
303             FOREACH(it, outRegisteredFeatures)
304             {
305                 if (dao.GetName() == (*it)->getName()) {    // *it = shared ptr
306                     // this name is found on list, do not add
307                     featureFound = true;
308                     break;
309                 }
310             }
311
312             if (!featureFound) {
313                 // feature is not on outRegisteredFeature so add it with
314                 // 'required' property false
315                 Api::IWidgetFeaturePtr newFeature(new WidgetFeature(
316                                                       dao.GetName(),
317                                                       false));
318
319                 // TODO what about feature params? aren't they needed
320                 outRegisteredFeatures.push_back(newFeature);
321             }
322             // reset the flag and check another feature
323             featureFound = false;
324         }
325     }
326     Catch(DPL::Exception)
327     {
328         ReThrow(Commons::SecurityException);
329     }
330
331     return outRegisteredFeatures;
332 }
333
334 std::string WidgetDB::getWidgetInstallationPath() const
335 {
336     Try
337     {
338         WrtDB::WidgetDAOReadOnly dao(getWidgetId());
339         std::string result(DPL::ToUTF8String(dao.getFullPath()));
340         result.erase(0, strlen("file://"));
341         std::string::size_type pos = result.size();
342         std::string separator("/");
343         pos = result.rfind(separator, pos);
344         if ((result.size() - pos) > 0 && (result.size() - pos) < 2) {
345             result.erase(pos, result.size());
346         }
347         return result;
348     }
349     Catch(DPL::Exception)
350     {
351         ReThrow(Commons::PlatformException);
352     }
353 }
354
355 std::string WidgetDB::getWidgetPersistentStoragePath() const
356 {
357     Try
358     {
359         WrtDB::WidgetDAOReadOnly dao(getWidgetId());
360         return
361             WrtDB::WidgetConfig::GetWidgetPersistentStoragePath(
362                 dao.getTizenPkgId());
363     }
364     Catch(DPL::Exception)
365     {
366         ReThrow(Commons::PlatformException);
367     }
368 }
369
370 std::string WidgetDB::getWidgetTemporaryStoragePath() const
371 {
372     Try
373     {
374         WrtDB::WidgetDAOReadOnly dao(getWidgetId());
375         return
376             WrtDB::WidgetConfig::GetWidgetTemporaryStoragePath(
377                 dao.getTizenPkgId());
378     }
379     Catch(DPL::Exception)
380     {
381         ReThrow(Commons::PlatformException);
382     }
383 }
384 } // WidgetDB
385 } // WrtDeviceApis