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 WrtDeviceApis {
46 WidgetDB::WidgetDB(int widgetId) : m_widgetId(widgetId) {}
48 int WidgetDB::getWidgetId() const
53 std::string WidgetDB::getLanguage() const
55 LogDebug("getting language");
57 WrtDB::LanguageTagList languageTags;
58 std::string outLanguage;
61 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
62 languageTags = dao.getLanguageTags();
66 ReThrow(Commons::SecurityException);
69 // get 'en' language or first from the list if 'en' does not exist
70 if (languageTags.size() > 1) {
71 FOREACH(lang, languageTags)
73 if ("en" == DPL::ToUTF8String(*lang)) {
79 if (!languageTags.empty()) {
80 // get first language tag
81 outLanguage = DPL::ToUTF8String(*(languageTags.begin()));
86 /*License,LicenseHref,Description,Name can be \
87 * returned with incorect language*/
88 std::string WidgetDB::getConfigValue(Api::ConfigAttribute attribute) const
94 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
95 WrtDB::WidgetLocalizedInfo widgetInfo =
96 W3CFileLocalization::getLocalizedInfo(getWidgetId());
98 std::ostringstream out;
101 case Api::ConfigAttribute::ID: {
102 DPL::OptionalString val = dao.getGUID();
103 value = !val ? "" : DPL::ToUTF8String(*val);
107 case Api::ConfigAttribute::VERSION: {
108 DPL::OptionalString val = dao.getVersion();
109 value = !val ? "" : DPL::ToUTF8String(*val);
113 case Api::ConfigAttribute::DESCRIPTION: {
114 DPL::OptionalString val = widgetInfo.description;
115 value = !val ? "" : DPL::ToUTF8String(*val);
119 case Api::ConfigAttribute::LICENSE: {
120 DPL::OptionalString val = widgetInfo.license;
121 value = !val ? "" : DPL::ToUTF8String(*val);
125 case Api::ConfigAttribute::LICENSE_HREF: {
126 DPL::OptionalString val = widgetInfo.licenseHref;
127 value = !val ? "" : DPL::ToUTF8String(*val);
131 case Api::ConfigAttribute::AUTHOR_NAME: {
132 DPL::OptionalString val = dao.getAuthorName();
133 value = !val ? "" : DPL::ToUTF8String(*val);
137 case Api::ConfigAttribute::AUTHOR_EMAIL: {
138 DPL::OptionalString val = dao.getAuthorEmail();
139 value = !val ? "" : DPL::ToUTF8String(*val);
143 case Api::ConfigAttribute::AUTHOR_HREF: {
144 DPL::OptionalString val = dao.getAuthorHref();
145 value = !val ? "" : DPL::ToUTF8String(*val);
149 case Api::ConfigAttribute::NAME: {
150 DPL::OptionalString val = widgetInfo.name;
151 value = !val ? "" : DPL::ToUTF8String(*val);
155 case Api::ConfigAttribute::SHORT_NAME: {
156 DPL::OptionalString val = widgetInfo.shortName;
157 value = !val ? "" : DPL::ToUTF8String(*val);
161 case Api::ConfigAttribute::ACCESS_NETWORK:
162 value = dao.getAccessNetworkMode() ? "yes" : "no";
165 case Api::ConfigAttribute::WIDTH: {
166 DPL::OptionalInt width = dao.getPreferredSize().width;
167 !width ? out << "0" : out << *width;
172 case Api::ConfigAttribute::HEIGHT: {
173 DPL::OptionalInt height = dao.getPreferredSize().height;
174 !height ? out << "0" : out << *height;
179 case Api::ConfigAttribute::INSTALL_PATH:
180 value = DPL::ToUTF8String(dao.getPath());
183 case Api::ConfigAttribute::PUBLIC_STORAGE_PATH:
184 value = WrtDB::GlobalConfig::GetPublicVirtualRootPath();
188 LogWarning("Unknown attribute requested");
189 Throw(Commons::InvalidArgumentException);
192 Catch(Commons::InvalidArgumentException)
194 ReThrow(Commons::InvalidArgumentException);
196 Catch(DPL::Exception)
198 ReThrow(Commons::SecurityException);
206 std::string WidgetDB::getUserAgent() const
208 LogDebug("Widget User_Agent data get");
210 std::string outUA("not implemented");
214 Api::InstallationStatus WidgetDB::checkInstallationStatus(
215 const std::string& gid,
216 const std::string& name,
217 const std::string& version) const
219 LogDebug("Checking widget ( gid(" << gid << "), name(" << name <<
220 "), version(" << version << ")) installation status");
222 Api::InstallationStatus outStatus;
225 const WrtDB::WidgetGUID w_guid =
226 WrtDB::WidgetGUID(DPL::FromUTF8String(gid));
229 WrtDB::DbWidgetHandle widgetHandle =
230 WrtDB::WidgetDAOReadOnly::getHandle(w_guid);
232 WrtDB::WidgetDAOReadOnly dao(widgetHandle);
233 DPL::OptionalString ver = dao.getVersion();
234 if (!ver || version > DPL::ToUTF8String(*ver)) {
235 // if there is no version in DB
236 // or DB version is lover or equal than provided
237 outStatus = Api::InstallationStatus::STATUS_INSTALLED;
239 outStatus = Api::InstallationStatus::STATUS_LATEST;
241 } catch (WrtDB::WidgetDAOReadOnly::Exception::WidgetNotExist) {
242 outStatus = Api::InstallationStatus::STATUS_UNINSTALLED;
245 Catch(DPL::Exception)
247 ReThrow(Commons::SecurityException);
253 Api::Features WidgetDB::getWidgetFeatures() const
256 WrtDB::DbWidgetFeatureSet features;
259 WrtDB::WidgetDAOReadOnly doa(getWidgetId());
260 features = doa.getFeaturesList();
262 Catch(DPL::Exception)
265 ReThrow(Commons::SecurityException);
268 Api::Features outFeatures;
270 LogDebug("There is no exception");
272 FOREACH(it, features)
274 Api::IWidgetFeaturePtr feature(
275 new WidgetFeature(DPL::ToUTF8String((*it).name),
276 true)); // requested (WidgetFeature param)
277 outFeatures.push_back(feature);
283 Api::Features WidgetDB::getRegisteredFeatures() const
285 Api::Features outRegisteredFeatures;
288 // initialize with our widget features (they are required)
289 outRegisteredFeatures = getWidgetFeatures();
291 // searching for another features
293 WrtDB::FeatureHandleList features =
294 WrtDB::FeatureDAOReadOnly::GetHandleList();
296 bool featureFound = false;
298 // throught all features
299 FOREACH(registeredFeature, features)
301 WrtDB::FeatureHandle handle = *registeredFeature;
302 WrtDB::FeatureDAOReadOnly dao(handle);
304 // check if this feature is aleready on outRegisteredFeatures list
305 // and if not, add it
307 FOREACH(it, outRegisteredFeatures)
309 if (dao.GetName() == (*it)->getName()) { // *it = shared ptr
310 // this name is found on list, do not add
317 // feature is not on outRegisteredFeature so add it with
318 // 'required' property false
319 Api::IWidgetFeaturePtr newFeature(new WidgetFeature(
323 // TODO what about feature params? aren't they needed
324 outRegisteredFeatures.push_back(newFeature);
326 // reset the flag and check another feature
327 featureFound = false;
330 Catch(DPL::Exception)
332 ReThrow(Commons::SecurityException);
335 return outRegisteredFeatures;
338 std::string WidgetDB::getWidgetInstallationPath() const
342 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
343 std::string result(DPL::ToUTF8String(dao.getFullPath()));
344 result.erase(0, strlen("file://"));
345 std::string::size_type pos = result.size();
346 std::string separator("/");
347 pos = result.rfind(separator, pos);
348 if ((result.size() - pos) > 0 && (result.size() - pos) < 2) {
349 result.erase(pos, result.size());
353 Catch(DPL::Exception)
355 ReThrow(Commons::PlatformException);
359 std::string WidgetDB::getWidgetPersistentStoragePath() const
363 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
365 WrtDB::WidgetConfig::GetWidgetPersistentStoragePath(
366 dao.getTizenPkgId());
368 Catch(DPL::Exception)
370 ReThrow(Commons::PlatformException);
374 std::string WidgetDB::getWidgetTemporaryStoragePath() const
378 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
380 WrtDB::WidgetConfig::GetWidgetTemporaryStoragePath(
381 dao.getTizenPkgId());
383 Catch(DPL::Exception)
385 ReThrow(Commons::PlatformException);