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/log/log.h>
32 #include <dpl/wrt-dao-ro/property_dao_read_only.h>
33 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
34 #include <dpl/wrt-dao-ro/global_config.h>
35 #include <dpl/wrt-dao-ro/widget_config.h>
36 #include <dpl/wrt-dao-ro/feature_dao_read_only.h>
37 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
38 #include <dpl/wrt-dao-ro/feature_model.h>
39 #include <dpl/wrt-dao-ro/common_dao_types.h>
41 #include <dpl/localization/w3c_file_localization.h>
43 #include "WidgetFeature.h"
45 namespace WrtDeviceApis {
47 WidgetDB::WidgetDB(WidgetHandle widgetId) : m_widgetId(widgetId) {}
49 WidgetHandle WidgetDB::getWidgetId() const
54 std::string WidgetDB::getLanguage() const
56 LogDebug("getting language");
58 WrtDB::LanguageTagList languageTags;
59 std::string outLanguage;
62 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
63 languageTags = dao.getLanguageTags();
67 ReThrow(Commons::SecurityException);
70 // get 'en' language or first from the list if 'en' does not exist
71 if (languageTags.size() > 1) {
72 FOREACH(lang, languageTags)
74 if ("en" == DPL::ToUTF8String(*lang)) {
80 if (!languageTags.empty()) {
81 // get first language tag
82 outLanguage = DPL::ToUTF8String(*(languageTags.begin()));
87 /*License,LicenseHref,Description,Name can be \
88 * returned with incorect language*/
89 std::string WidgetDB::getConfigValue(Api::ConfigAttribute attribute) const
95 WrtDB::WidgetDAOReadOnlyPtr dao(new WrtDB::WidgetDAOReadOnly(getWidgetId()));
96 WrtDB::WidgetLocalizedInfo widgetInfo =
97 W3CFileLocalization::getLocalizedInfo(dao);
99 std::ostringstream out;
102 case Api::ConfigAttribute::ID: {
103 DPL::OptionalString val = dao->getGUID();
104 value = !val ? "" : DPL::ToUTF8String(*val);
108 case Api::ConfigAttribute::VERSION: {
109 DPL::OptionalString val = dao->getVersion();
110 value = !val ? "" : DPL::ToUTF8String(*val);
114 case Api::ConfigAttribute::DESCRIPTION: {
115 DPL::OptionalString val = widgetInfo.description;
116 value = !val ? "" : DPL::ToUTF8String(*val);
120 case Api::ConfigAttribute::LICENSE: {
121 DPL::OptionalString val = widgetInfo.license;
122 value = !val ? "" : DPL::ToUTF8String(*val);
126 case Api::ConfigAttribute::LICENSE_HREF: {
127 DPL::OptionalString val = widgetInfo.licenseHref;
128 value = !val ? "" : DPL::ToUTF8String(*val);
132 case Api::ConfigAttribute::AUTHOR_NAME: {
133 DPL::OptionalString val = dao->getAuthorName();
134 value = !val ? "" : DPL::ToUTF8String(*val);
138 case Api::ConfigAttribute::AUTHOR_EMAIL: {
139 DPL::OptionalString val = dao->getAuthorEmail();
140 value = !val ? "" : DPL::ToUTF8String(*val);
144 case Api::ConfigAttribute::AUTHOR_HREF: {
145 DPL::OptionalString val = dao->getAuthorHref();
146 value = !val ? "" : DPL::ToUTF8String(*val);
150 case Api::ConfigAttribute::NAME: {
151 DPL::OptionalString val = widgetInfo.name;
152 value = !val ? "" : DPL::ToUTF8String(*val);
156 case Api::ConfigAttribute::SHORT_NAME: {
157 DPL::OptionalString val = widgetInfo.shortName;
158 value = !val ? "" : DPL::ToUTF8String(*val);
162 case Api::ConfigAttribute::WIDTH: {
163 DPL::OptionalInt width = dao->getPreferredSize().width;
164 !width ? out << "0" : out << *width;
169 case Api::ConfigAttribute::HEIGHT: {
170 DPL::OptionalInt height = dao->getPreferredSize().height;
171 !height ? out << "0" : out << *height;
176 case Api::ConfigAttribute::INSTALL_PATH:
177 value = DPL::ToUTF8String(dao->getPath());
180 case Api::ConfigAttribute::PUBLIC_STORAGE_PATH:
181 value = WrtDB::GlobalConfig::GetPublicVirtualRootPath();
185 LogWarning("Unknown attribute requested");
186 Throw(Commons::InvalidArgumentException);
189 Catch(Commons::InvalidArgumentException)
191 ReThrow(Commons::InvalidArgumentException);
193 Catch(DPL::Exception)
195 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));
226 WrtDB::DbWidgetHandle widgetHandle =
227 WrtDB::WidgetDAOReadOnly::getHandle(w_guid);
229 WrtDB::WidgetDAOReadOnly dao(widgetHandle);
230 DPL::OptionalString ver = dao.getVersion();
231 if (!ver || version > DPL::ToUTF8String(*ver)) {
232 // if there is no version in DB
233 // or DB version is lover or equal than provided
234 outStatus = Api::InstallationStatus::STATUS_INSTALLED;
236 outStatus = Api::InstallationStatus::STATUS_LATEST;
238 } catch (WrtDB::WidgetDAOReadOnly::Exception::WidgetNotExist) {
239 outStatus = Api::InstallationStatus::STATUS_UNINSTALLED;
242 Catch(DPL::Exception)
244 ReThrow(Commons::SecurityException);
250 Api::Features WidgetDB::getWidgetFeatures() const
253 WrtDB::DbWidgetFeatureSet features;
256 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
257 features = dao.getFeaturesList();
259 Catch(DPL::Exception)
262 ReThrow(Commons::SecurityException);
265 Api::Features outFeatures;
267 LogDebug("There is no exception");
269 FOREACH(it, features)
271 Api::IWidgetFeaturePtr feature(
272 new WidgetFeature(DPL::ToUTF8String((*it).name),
273 true)); // requested (WidgetFeature param)
274 outFeatures.push_back(feature);
280 Api::Features WidgetDB::getRegisteredFeatures() const
282 Api::Features outRegisteredFeatures;
285 // initialize with our widget features (they are required)
286 outRegisteredFeatures = getWidgetFeatures();
288 // searching for another features
290 WrtDB::FeatureHandleList features =
291 WrtDB::FeatureDAOReadOnly::GetHandleList();
293 bool featureFound = false;
295 // throught all features
296 FOREACH(registeredFeature, features)
298 WrtDB::FeatureHandle handle = *registeredFeature;
299 WrtDB::FeatureDAOReadOnly dao(handle);
301 // check if this feature is aleready on outRegisteredFeatures list
302 // and if not, add it
304 FOREACH(it, outRegisteredFeatures)
306 if (dao.GetName() == (*it)->getName()) { // *it = shared ptr
307 // this name is found on list, do not add
314 // feature is not on outRegisteredFeature so add it with
315 // 'required' property false
316 Api::IWidgetFeaturePtr newFeature(new WidgetFeature(
320 // TODO what about feature params? aren't they needed
321 outRegisteredFeatures.push_back(newFeature);
323 // reset the flag and check another feature
324 featureFound = false;
327 Catch(DPL::Exception)
329 ReThrow(Commons::SecurityException);
332 return outRegisteredFeatures;
335 std::string WidgetDB::getWidgetInstallationPath() const
339 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
340 std::string result(DPL::ToUTF8String(dao.getFullPath()));
341 result.erase(0, strlen("file://"));
342 std::string::size_type pos = result.size();
343 std::string separator("/");
344 pos = result.rfind(separator, pos);
345 if ((result.size() - pos) > 0 && (result.size() - pos) < 2) {
346 result.erase(pos, result.size());
350 Catch(DPL::Exception)
352 ReThrow(Commons::PlatformException);
356 std::string WidgetDB::getWidgetPersistentStoragePath() const
360 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
362 WrtDB::WidgetConfig::GetWidgetPersistentStoragePath(
363 dao.getTizenPkgId());
365 Catch(DPL::Exception)
367 ReThrow(Commons::PlatformException);
371 std::string WidgetDB::getWidgetTemporaryStoragePath() const
375 WrtDB::WidgetDAOReadOnly dao(getWidgetId());
377 WrtDB::WidgetConfig::GetWidgetTemporaryStoragePath(
378 dao.getTizenPkgId());
380 Catch(DPL::Exception)
382 ReThrow(Commons::PlatformException);