2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @author Andrzej Surdej (a.surdej@samsung.com)
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>
40 #include "WidgetFeature.h"
42 namespace { // anonymous
43 const int INVALID_WIDGET_HANDLE = -1;
46 namespace WrtDeviceApis {
49 WidgetDB::WidgetDB(int widgetId) : m_widgetId(widgetId) {}
52 int WidgetDB::getWidgetId() const
57 std::string WidgetDB::getLanguage() const
59 LogDebug("getting language");
61 WrtDB::LanguageTagList languageTags;
62 std::string outLanguage;
65 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
66 languageTags = dao.getLanguageTags();
70 ReThrow(Commons::SecurityException);
73 // get 'en' language or first from the list if 'en' does not exist
74 if (languageTags.size() > 1)
76 FOREACH(lang, languageTags)
78 if ("en" == DPL::ToUTF8String(*lang))
83 if (!languageTags.empty())
85 // get first language tag
86 outLanguage = DPL::ToUTF8String(*(languageTags.begin()));
91 /*License,LicenseHref,Description,Name can be \
92 returned with incorect language*/
93 std::string WidgetDB::getConfigValue(Api::ConfigAttribute attribute) const
99 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
100 std::string language = getLanguage();
101 WrtDB::WidgetLocalizedInfo widgetInfo = dao.getLocalizedInfo(
102 DPL::FromUTF8String(language));
104 std::ostringstream out;
107 case Api::ConfigAttribute::ID: {
108 DPL::OptionalString val = dao.getGUID();
109 value = !val ? "" : DPL::ToUTF8String(*val);
112 case Api::ConfigAttribute::VERSION: {
113 DPL::OptionalString val = dao.getVersion();
114 value = !val ? "" : DPL::ToUTF8String(*val);
117 case Api::ConfigAttribute::DESCRIPTION: {
118 DPL::OptionalString val = widgetInfo.description;
119 value = !val ? "" : DPL::ToUTF8String(*val);
122 case Api::ConfigAttribute::LICENSE: {
123 DPL::OptionalString val = widgetInfo.license;
124 value = !val ? "" : DPL::ToUTF8String(*val);
127 case Api::ConfigAttribute::LICENSE_HREF: {
128 DPL::OptionalString val = widgetInfo.licenseHref;
129 value = !val ? "" : DPL::ToUTF8String(*val);
132 case Api::ConfigAttribute::AUTHOR_NAME: {
133 DPL::OptionalString val = dao.getAuthorName();
134 value = !val ? "" : DPL::ToUTF8String(*val);
137 case Api::ConfigAttribute::AUTHOR_EMAIL: {
138 DPL::OptionalString val = dao.getAuthorEmail();
139 value = !val ? "" : DPL::ToUTF8String(*val);
142 case Api::ConfigAttribute::AUTHOR_HREF: {
143 DPL::OptionalString val = dao.getAuthorHref();
144 value = !val ? "" : DPL::ToUTF8String(*val);
147 case Api::ConfigAttribute::NAME: {
148 DPL::OptionalString val = widgetInfo.name;
149 value = !val ? "" : DPL::ToUTF8String(*val);
152 case Api::ConfigAttribute::SHORT_NAME: {
153 DPL::OptionalString val = widgetInfo.shortName;
154 value = !val ? "" : DPL::ToUTF8String(*val);
157 case Api::ConfigAttribute::ACCESS_NETWORK:
158 value = dao.getAccessNetworkMode() ? "yes" : "no";
161 case Api::ConfigAttribute::WIDTH: {
162 DPL::OptionalInt width = dao.getPreferredSize().width;
163 !width ? out << "0" : out << *width;
167 case Api::ConfigAttribute::HEIGHT: {
168 DPL::OptionalInt height = dao.getPreferredSize().height;
169 !height ? out << "0" : out << *height;
173 case Api::ConfigAttribute::INSTALL_PATH:
174 value = DPL::ToUTF8String(dao.getPath());
177 case Api::ConfigAttribute::PUBLIC_STORAGE_PATH:
178 value = WrtDB::GlobalConfig::GetPublicVirtualRootPath();
182 LogWarning("Unknown attribute requested");
183 Throw(Commons::InvalidArgumentException);
186 Catch(Commons::InvalidArgumentException)
188 ReThrow(Commons::InvalidArgumentException);
190 Catch(DPL::Exception)
192 ReThrow(Commons::SecurityException);
202 std::string WidgetDB::getUserAgent() const
204 LogDebug("Widget User_Agent data get");
206 std::string outUA("not implemented");
210 Api::InstallationStatus WidgetDB::checkInstallationStatus(
211 const std::string& gid,
212 const std::string& name,
213 const std::string& version) const
215 LogDebug("Checking widget ( gid(" << gid << "), name(" << name <<
216 "), version(" << version << ")) installation status");
218 Api::InstallationStatus outStatus;
221 const WrtDB::WidgetGUID w_guid =
222 WrtDB::WidgetGUID(DPL::FromUTF8String(gid));
224 WrtDB::DbWidgetHandle widgetHandle = INVALID_WIDGET_HANDLE;
226 WrtDB::DbWidgetHandleList widgetList = WrtDB::WidgetDAOReadOnly
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;
238 if (INVALID_WIDGET_HANDLE == widgetHandle) {
239 outStatus = Api::InstallationStatus::STATUS_UNINSTALLED;
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;
248 outStatus = Api::InstallationStatus::STATUS_LATEST;
252 Catch(DPL::Exception)
254 ReThrow(Commons::SecurityException);
261 Api::Features WidgetDB::getWidgetFeatures() const
264 WrtDB::DbWidgetFeatureSet features;
267 WrtDB::WidgetDAOReadOnly doa(getWidgetId());
268 features = doa.getFeaturesList();
270 Catch(DPL::Exception)
273 ReThrow(Commons::SecurityException);
276 Api::Features outFeatures;
278 LogDebug("There is no exception");
280 FOREACH(it, features)
282 Api::IWidgetFeaturePtr feature(
283 new WidgetFeature(DPL::ToUTF8String((*it).name),
285 true)); // requested (WidgetFeature param)
286 // iterating multimap
287 FOREACH(iterator, (*it).params)
289 // adding features with key(first) and value(second)
290 feature->addParam(DPL::ToUTF8String(iterator->first),
291 DPL::ToUTF8String(iterator->second));
294 outFeatures.push_back(feature);
300 Api::Features WidgetDB::getRegisteredFeatures() const
302 Api::Features outRegisteredFeatures;
305 // initialize with our widget features (they are required)
306 outRegisteredFeatures = getWidgetFeatures();
308 // searching for another features
310 WrtDB::FeatureHandleList features =
311 WrtDB::FeatureDAOReadOnly::GetHandleList();
313 bool featureFound = false;
315 // throught all features
316 FOREACH(registeredFeature, features)
318 WrtDB::FeatureHandle handle = *registeredFeature;
319 WrtDB::FeatureDAOReadOnly dao(handle);
321 // check if this feature is aleready on outRegisteredFeatures list
322 // and if not, add it
324 FOREACH(it, outRegisteredFeatures)
326 if (dao.GetName() == (*it)->getName()) { // *it = shared ptr
327 // this name is found on list, do not add
334 // feature is not on outRegisteredFeature so add it with
335 // 'required' property false
336 Api::IWidgetFeaturePtr newFeature(new WidgetFeature(
341 // TODO what about feature params? aren't they needed
342 outRegisteredFeatures.push_back(newFeature);
344 // reset the flag and check another feature
345 featureFound = false;
348 Catch(DPL::Exception)
350 ReThrow(Commons::SecurityException);
353 return outRegisteredFeatures;
357 std::string WidgetDB::getWidgetInstallationPath() const
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)
369 result.erase(pos, result.size());
373 Catch(DPL::Exception)
375 ReThrow(Commons::PlatformException);
379 std::string WidgetDB::getWidgetPersistentStoragePath() const
383 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
384 return WrtDB::WidgetConfig::GetWidgetPersistentStoragePath(*dao.getPkgname());
386 Catch(DPL::Exception)
388 ReThrow(Commons::PlatformException);
392 std::string WidgetDB::getWidgetTemporaryStoragePath() const
396 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
397 return WrtDB::WidgetConfig::GetWidgetTemporaryStoragePath(*dao.getPkgname());
399 Catch(DPL::Exception)
401 ReThrow(Commons::PlatformException);