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 <dpl/localization/w3c_file_localization.h>
42 #include "WidgetFeature.h"
44 namespace { // anonymous
45 const int INVALID_WIDGET_HANDLE = -1;
48 namespace WrtDeviceApis {
51 WidgetDB::WidgetDB(int widgetId) : m_widgetId(widgetId) {}
54 int WidgetDB::getWidgetId() const
59 std::string WidgetDB::getLanguage() const
61 LogDebug("getting language");
63 WrtDB::LanguageTagList languageTags;
64 std::string outLanguage;
67 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
68 languageTags = dao.getLanguageTags();
72 ReThrow(Commons::SecurityException);
75 // get 'en' language or first from the list if 'en' does not exist
76 if (languageTags.size() > 1)
78 FOREACH(lang, languageTags)
80 if ("en" == DPL::ToUTF8String(*lang))
85 if (!languageTags.empty())
87 // get first language tag
88 outLanguage = DPL::ToUTF8String(*(languageTags.begin()));
93 /*License,LicenseHref,Description,Name can be \
94 returned with incorect language*/
95 std::string WidgetDB::getConfigValue(Api::ConfigAttribute attribute) const
101 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
102 WrtDB::WidgetLocalizedInfo widgetInfo =
103 W3CFileLocalization::getLocalizedInfo(getWidgetId());
105 std::ostringstream out;
108 case Api::ConfigAttribute::ID: {
109 DPL::OptionalString val = dao.getGUID();
110 value = !val ? "" : DPL::ToUTF8String(*val);
113 case Api::ConfigAttribute::VERSION: {
114 DPL::OptionalString val = dao.getVersion();
115 value = !val ? "" : DPL::ToUTF8String(*val);
118 case Api::ConfigAttribute::DESCRIPTION: {
119 DPL::OptionalString val = widgetInfo.description;
120 value = !val ? "" : DPL::ToUTF8String(*val);
123 case Api::ConfigAttribute::LICENSE: {
124 DPL::OptionalString val = widgetInfo.license;
125 value = !val ? "" : DPL::ToUTF8String(*val);
128 case Api::ConfigAttribute::LICENSE_HREF: {
129 DPL::OptionalString val = widgetInfo.licenseHref;
130 value = !val ? "" : DPL::ToUTF8String(*val);
133 case Api::ConfigAttribute::AUTHOR_NAME: {
134 DPL::OptionalString val = dao.getAuthorName();
135 value = !val ? "" : DPL::ToUTF8String(*val);
138 case Api::ConfigAttribute::AUTHOR_EMAIL: {
139 DPL::OptionalString val = dao.getAuthorEmail();
140 value = !val ? "" : DPL::ToUTF8String(*val);
143 case Api::ConfigAttribute::AUTHOR_HREF: {
144 DPL::OptionalString val = dao.getAuthorHref();
145 value = !val ? "" : DPL::ToUTF8String(*val);
148 case Api::ConfigAttribute::NAME: {
149 DPL::OptionalString val = widgetInfo.name;
150 value = !val ? "" : DPL::ToUTF8String(*val);
153 case Api::ConfigAttribute::SHORT_NAME: {
154 DPL::OptionalString val = widgetInfo.shortName;
155 value = !val ? "" : DPL::ToUTF8String(*val);
158 case Api::ConfigAttribute::ACCESS_NETWORK:
159 value = dao.getAccessNetworkMode() ? "yes" : "no";
162 case Api::ConfigAttribute::WIDTH: {
163 DPL::OptionalInt width = dao.getPreferredSize().width;
164 !width ? out << "0" : out << *width;
168 case Api::ConfigAttribute::HEIGHT: {
169 DPL::OptionalInt height = dao.getPreferredSize().height;
170 !height ? out << "0" : out << *height;
174 case Api::ConfigAttribute::INSTALL_PATH:
175 value = DPL::ToUTF8String(dao.getPath());
178 case Api::ConfigAttribute::PUBLIC_STORAGE_PATH:
179 value = WrtDB::GlobalConfig::GetPublicVirtualRootPath();
183 LogWarning("Unknown attribute requested");
184 Throw(Commons::InvalidArgumentException);
187 Catch(Commons::InvalidArgumentException)
189 ReThrow(Commons::InvalidArgumentException);
191 Catch(DPL::Exception)
193 ReThrow(Commons::SecurityException);
203 std::string WidgetDB::getUserAgent() const
205 LogDebug("Widget User_Agent data get");
207 std::string outUA("not implemented");
211 Api::InstallationStatus WidgetDB::checkInstallationStatus(
212 const std::string& gid,
213 const std::string& name,
214 const std::string& version) const
216 LogDebug("Checking widget ( gid(" << gid << "), name(" << name <<
217 "), version(" << version << ")) installation status");
219 Api::InstallationStatus outStatus;
222 const WrtDB::WidgetGUID w_guid =
223 WrtDB::WidgetGUID(DPL::FromUTF8String(gid));
225 WrtDB::DbWidgetHandle widgetHandle = INVALID_WIDGET_HANDLE;
227 WrtDB::DbWidgetHandleList widgetList = WrtDB::WidgetDAOReadOnly
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;
239 if (INVALID_WIDGET_HANDLE == widgetHandle) {
240 outStatus = Api::InstallationStatus::STATUS_UNINSTALLED;
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;
249 outStatus = Api::InstallationStatus::STATUS_LATEST;
253 Catch(DPL::Exception)
255 ReThrow(Commons::SecurityException);
262 Api::Features WidgetDB::getWidgetFeatures() const
265 WrtDB::DbWidgetFeatureSet features;
268 WrtDB::WidgetDAOReadOnly doa(getWidgetId());
269 features = doa.getFeaturesList();
271 Catch(DPL::Exception)
274 ReThrow(Commons::SecurityException);
277 Api::Features outFeatures;
279 LogDebug("There is no exception");
281 FOREACH(it, features)
283 Api::IWidgetFeaturePtr feature(
284 new WidgetFeature(DPL::ToUTF8String((*it).name),
286 true)); // requested (WidgetFeature param)
287 // iterating multimap
288 FOREACH(iterator, (*it).params)
290 // adding features with key(first) and value(second)
291 feature->addParam(DPL::ToUTF8String(iterator->first),
292 DPL::ToUTF8String(iterator->second));
295 outFeatures.push_back(feature);
301 Api::Features WidgetDB::getRegisteredFeatures() const
303 Api::Features outRegisteredFeatures;
306 // initialize with our widget features (they are required)
307 outRegisteredFeatures = getWidgetFeatures();
309 // searching for another features
311 WrtDB::FeatureHandleList features =
312 WrtDB::FeatureDAOReadOnly::GetHandleList();
314 bool featureFound = false;
316 // throught all features
317 FOREACH(registeredFeature, features)
319 WrtDB::FeatureHandle handle = *registeredFeature;
320 WrtDB::FeatureDAOReadOnly dao(handle);
322 // check if this feature is aleready on outRegisteredFeatures list
323 // and if not, add it
325 FOREACH(it, outRegisteredFeatures)
327 if (dao.GetName() == (*it)->getName()) { // *it = shared ptr
328 // this name is found on list, do not add
335 // feature is not on outRegisteredFeature so add it with
336 // 'required' property false
337 Api::IWidgetFeaturePtr newFeature(new WidgetFeature(
342 // TODO what about feature params? aren't they needed
343 outRegisteredFeatures.push_back(newFeature);
345 // reset the flag and check another feature
346 featureFound = false;
349 Catch(DPL::Exception)
351 ReThrow(Commons::SecurityException);
354 return outRegisteredFeatures;
358 std::string WidgetDB::getWidgetInstallationPath() const
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)
370 result.erase(pos, result.size());
374 Catch(DPL::Exception)
376 ReThrow(Commons::PlatformException);
380 std::string WidgetDB::getWidgetPersistentStoragePath() const
384 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
385 return WrtDB::WidgetConfig::GetWidgetPersistentStoragePath(*dao.getPkgname());
387 Catch(DPL::Exception)
389 ReThrow(Commons::PlatformException);
393 std::string WidgetDB::getWidgetTemporaryStoragePath() const
397 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
398 return WrtDB::WidgetConfig::GetWidgetTemporaryStoragePath(*dao.getPkgname());
400 Catch(DPL::Exception)
402 ReThrow(Commons::PlatformException);