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::WidgetDAOReadOnlyPtr dao(new WrtDB::WidgetDAOReadOnly(getWidgetId()));
95 WrtDB::WidgetLocalizedInfo widgetInfo =
96 W3CFileLocalization::getLocalizedInfo(dao);
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::WIDTH: {
162 DPL::OptionalInt width = dao->getPreferredSize().width;
163 !width ? out << "0" : out << *width;
168 case Api::ConfigAttribute::HEIGHT: {
169 DPL::OptionalInt height = dao->getPreferredSize().height;
170 !height ? out << "0" : out << *height;
175 case Api::ConfigAttribute::INSTALL_PATH:
176 value = DPL::ToUTF8String(dao->getPath());
179 case Api::ConfigAttribute::PUBLIC_STORAGE_PATH:
180 value = WrtDB::GlobalConfig::GetPublicVirtualRootPath();
184 LogWarning("Unknown attribute requested");
185 Throw(Commons::InvalidArgumentException);
188 Catch(Commons::InvalidArgumentException)
190 ReThrow(Commons::InvalidArgumentException);
192 Catch(DPL::Exception)
194 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));
225 WrtDB::DbWidgetHandle widgetHandle =
226 WrtDB::WidgetDAOReadOnly::getHandle(w_guid);
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;
235 outStatus = Api::InstallationStatus::STATUS_LATEST;
237 } catch (WrtDB::WidgetDAOReadOnly::Exception::WidgetNotExist) {
238 outStatus = Api::InstallationStatus::STATUS_UNINSTALLED;
241 Catch(DPL::Exception)
243 ReThrow(Commons::SecurityException);
249 Api::Features WidgetDB::getWidgetFeatures() const
252 WrtDB::DbWidgetFeatureSet features;
255 WrtDB::WidgetDAOReadOnly doa(getWidgetId());
256 features = doa.getFeaturesList();
258 Catch(DPL::Exception)
261 ReThrow(Commons::SecurityException);
264 Api::Features outFeatures;
266 LogDebug("There is no exception");
268 FOREACH(it, features)
270 Api::IWidgetFeaturePtr feature(
271 new WidgetFeature(DPL::ToUTF8String((*it).name),
272 true)); // requested (WidgetFeature param)
273 outFeatures.push_back(feature);
279 Api::Features WidgetDB::getRegisteredFeatures() const
281 Api::Features outRegisteredFeatures;
284 // initialize with our widget features (they are required)
285 outRegisteredFeatures = getWidgetFeatures();
287 // searching for another features
289 WrtDB::FeatureHandleList features =
290 WrtDB::FeatureDAOReadOnly::GetHandleList();
292 bool featureFound = false;
294 // throught all features
295 FOREACH(registeredFeature, features)
297 WrtDB::FeatureHandle handle = *registeredFeature;
298 WrtDB::FeatureDAOReadOnly dao(handle);
300 // check if this feature is aleready on outRegisteredFeatures list
301 // and if not, add it
303 FOREACH(it, outRegisteredFeatures)
305 if (dao.GetName() == (*it)->getName()) { // *it = shared ptr
306 // this name is found on list, do not add
313 // feature is not on outRegisteredFeature so add it with
314 // 'required' property false
315 Api::IWidgetFeaturePtr newFeature(new WidgetFeature(
319 // TODO what about feature params? aren't they needed
320 outRegisteredFeatures.push_back(newFeature);
322 // reset the flag and check another feature
323 featureFound = false;
326 Catch(DPL::Exception)
328 ReThrow(Commons::SecurityException);
331 return outRegisteredFeatures;
334 std::string WidgetDB::getWidgetInstallationPath() const
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());
349 Catch(DPL::Exception)
351 ReThrow(Commons::PlatformException);
355 std::string WidgetDB::getWidgetPersistentStoragePath() const
359 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
361 WrtDB::WidgetConfig::GetWidgetPersistentStoragePath(
362 dao.getTizenPkgId());
364 Catch(DPL::Exception)
366 ReThrow(Commons::PlatformException);
370 std::string WidgetDB::getWidgetTemporaryStoragePath() const
374 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
376 WrtDB::WidgetConfig::GetWidgetTemporaryStoragePath(
377 dao.getTizenPkgId());
379 Catch(DPL::Exception)
381 ReThrow(Commons::PlatformException);