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"
45 namespace WrtDeviceApis {
48 WidgetDB::WidgetDB(int widgetId) : m_widgetId(widgetId) {}
51 int WidgetDB::getWidgetId() const
56 std::string WidgetDB::getLanguage() const
58 LogDebug("getting language");
60 WrtDB::LanguageTagList languageTags;
61 std::string outLanguage;
64 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
65 languageTags = dao.getLanguageTags();
69 ReThrow(Commons::SecurityException);
72 // get 'en' language or first from the list if 'en' does not exist
73 if (languageTags.size() > 1)
75 FOREACH(lang, languageTags)
77 if ("en" == DPL::ToUTF8String(*lang))
82 if (!languageTags.empty())
84 // get first language tag
85 outLanguage = DPL::ToUTF8String(*(languageTags.begin()));
90 /*License,LicenseHref,Description,Name can be \
91 returned with incorect language*/
92 std::string WidgetDB::getConfigValue(Api::ConfigAttribute attribute) const
98 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
99 WrtDB::WidgetLocalizedInfo widgetInfo =
100 W3CFileLocalization::getLocalizedInfo(getWidgetId());
102 std::ostringstream out;
105 case Api::ConfigAttribute::ID: {
106 DPL::OptionalString val = dao.getGUID();
107 value = !val ? "" : DPL::ToUTF8String(*val);
110 case Api::ConfigAttribute::VERSION: {
111 DPL::OptionalString val = dao.getVersion();
112 value = !val ? "" : DPL::ToUTF8String(*val);
115 case Api::ConfigAttribute::DESCRIPTION: {
116 DPL::OptionalString val = widgetInfo.description;
117 value = !val ? "" : DPL::ToUTF8String(*val);
120 case Api::ConfigAttribute::LICENSE: {
121 DPL::OptionalString val = widgetInfo.license;
122 value = !val ? "" : DPL::ToUTF8String(*val);
125 case Api::ConfigAttribute::LICENSE_HREF: {
126 DPL::OptionalString val = widgetInfo.licenseHref;
127 value = !val ? "" : DPL::ToUTF8String(*val);
130 case Api::ConfigAttribute::AUTHOR_NAME: {
131 DPL::OptionalString val = dao.getAuthorName();
132 value = !val ? "" : DPL::ToUTF8String(*val);
135 case Api::ConfigAttribute::AUTHOR_EMAIL: {
136 DPL::OptionalString val = dao.getAuthorEmail();
137 value = !val ? "" : DPL::ToUTF8String(*val);
140 case Api::ConfigAttribute::AUTHOR_HREF: {
141 DPL::OptionalString val = dao.getAuthorHref();
142 value = !val ? "" : DPL::ToUTF8String(*val);
145 case Api::ConfigAttribute::NAME: {
146 DPL::OptionalString val = widgetInfo.name;
147 value = !val ? "" : DPL::ToUTF8String(*val);
150 case Api::ConfigAttribute::SHORT_NAME: {
151 DPL::OptionalString val = widgetInfo.shortName;
152 value = !val ? "" : DPL::ToUTF8String(*val);
155 case Api::ConfigAttribute::ACCESS_NETWORK:
156 value = dao.getAccessNetworkMode() ? "yes" : "no";
159 case Api::ConfigAttribute::WIDTH: {
160 DPL::OptionalInt width = dao.getPreferredSize().width;
161 !width ? out << "0" : out << *width;
165 case Api::ConfigAttribute::HEIGHT: {
166 DPL::OptionalInt height = dao.getPreferredSize().height;
167 !height ? out << "0" : out << *height;
171 case Api::ConfigAttribute::INSTALL_PATH:
172 value = DPL::ToUTF8String(dao.getPath());
175 case Api::ConfigAttribute::PUBLIC_STORAGE_PATH:
176 value = WrtDB::GlobalConfig::GetPublicVirtualRootPath();
180 LogWarning("Unknown attribute requested");
181 Throw(Commons::InvalidArgumentException);
184 Catch(Commons::InvalidArgumentException)
186 ReThrow(Commons::InvalidArgumentException);
188 Catch(DPL::Exception)
190 ReThrow(Commons::SecurityException);
200 std::string WidgetDB::getUserAgent() const
202 LogDebug("Widget User_Agent data get");
204 std::string outUA("not implemented");
208 Api::InstallationStatus WidgetDB::checkInstallationStatus(
209 const std::string& gid,
210 const std::string& name,
211 const std::string& version) const
213 LogDebug("Checking widget ( gid(" << gid << "), name(" << name <<
214 "), version(" << version << ")) installation status");
216 Api::InstallationStatus outStatus;
219 const WrtDB::WidgetGUID w_guid =
220 WrtDB::WidgetGUID(DPL::FromUTF8String(gid));
223 WrtDB::DbWidgetHandle widgetHandle =
224 WrtDB::WidgetDAOReadOnly::getHandle(w_guid);
226 WrtDB::WidgetDAOReadOnly dao(widgetHandle);
227 DPL::OptionalString ver = dao.getVersion();
228 if (!ver || version > DPL::ToUTF8String(*ver)) {
229 // if there is no version in DB
230 // or DB version is lover or equal than provided
231 outStatus = Api::InstallationStatus::STATUS_INSTALLED;
233 outStatus = Api::InstallationStatus::STATUS_LATEST;
235 }catch (WrtDB::WidgetDAOReadOnly::Exception::WidgetNotExist) {
236 outStatus = Api::InstallationStatus::STATUS_UNINSTALLED;
239 Catch(DPL::Exception)
241 ReThrow(Commons::SecurityException);
248 Api::Features WidgetDB::getWidgetFeatures() const
251 WrtDB::DbWidgetFeatureSet features;
254 WrtDB::WidgetDAOReadOnly doa(getWidgetId());
255 features = doa.getFeaturesList();
257 Catch(DPL::Exception)
260 ReThrow(Commons::SecurityException);
263 Api::Features outFeatures;
265 LogDebug("There is no exception");
267 FOREACH(it, features)
269 Api::IWidgetFeaturePtr feature(
270 new WidgetFeature(DPL::ToUTF8String((*it).name),
272 true)); // requested (WidgetFeature param)
273 // iterating multimap
274 FOREACH(iterator, (*it).params)
276 // adding features with key(first) and value(second)
277 feature->addParam(DPL::ToUTF8String(iterator->first),
278 DPL::ToUTF8String(iterator->second));
281 outFeatures.push_back(feature);
287 Api::Features WidgetDB::getRegisteredFeatures() const
289 Api::Features outRegisteredFeatures;
292 // initialize with our widget features (they are required)
293 outRegisteredFeatures = getWidgetFeatures();
295 // searching for another features
297 WrtDB::FeatureHandleList features =
298 WrtDB::FeatureDAOReadOnly::GetHandleList();
300 bool featureFound = false;
302 // throught all features
303 FOREACH(registeredFeature, features)
305 WrtDB::FeatureHandle handle = *registeredFeature;
306 WrtDB::FeatureDAOReadOnly dao(handle);
308 // check if this feature is aleready on outRegisteredFeatures list
309 // and if not, add it
311 FOREACH(it, outRegisteredFeatures)
313 if (dao.GetName() == (*it)->getName()) { // *it = shared ptr
314 // this name is found on list, do not add
321 // feature is not on outRegisteredFeature so add it with
322 // 'required' property false
323 Api::IWidgetFeaturePtr newFeature(new WidgetFeature(
328 // TODO what about feature params? aren't they needed
329 outRegisteredFeatures.push_back(newFeature);
331 // reset the flag and check another feature
332 featureFound = false;
335 Catch(DPL::Exception)
337 ReThrow(Commons::SecurityException);
340 return outRegisteredFeatures;
344 std::string WidgetDB::getWidgetInstallationPath() const
348 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
349 std::string result(DPL::ToUTF8String(dao.getFullPath()));
350 result.erase(0,strlen("file://"));
351 std::string::size_type pos = result.size();
352 std::string separator("/");
353 pos = result.rfind(separator, pos);
354 if((result.size()-pos) >0 && (result.size()-pos) < 2)
356 result.erase(pos, result.size());
360 Catch(DPL::Exception)
362 ReThrow(Commons::PlatformException);
366 std::string WidgetDB::getWidgetPersistentStoragePath() const
370 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
371 return WrtDB::WidgetConfig::GetWidgetPersistentStoragePath(*dao.getPkgname());
373 Catch(DPL::Exception)
375 ReThrow(Commons::PlatformException);
379 std::string WidgetDB::getWidgetTemporaryStoragePath() const
383 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
384 return WrtDB::WidgetConfig::GetWidgetTemporaryStoragePath(*dao.getPkgname());
386 Catch(DPL::Exception)
388 ReThrow(Commons::PlatformException);