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.
17 * @file task_db_update.cpp
18 * @author Lukasz Wrzosek(l.wrzosek@samsung.com)
20 * @brief Implementation file for installer task database updating
24 #include <widget_install/task_db_update.h>
25 #include <widget_install/job_widget_install.h>
26 #include <widget_install/widget_install_errors.h>
27 #include <widget_install/widget_install_context.h>
28 #include <dpl/wrt-dao-ro/config_parser_data.h>
29 #include <dpl/utils/wrt_utility.h>
30 #include <dpl/wrt-dao-rw/widget_dao.h>
31 #include <ace-dao-rw/AceDAO.h>
32 #include <dpl/foreach.h>
33 #include <dpl/log/log.h>
34 #include <dpl/assert.h>
35 #include <dpl/wrt-dao-ro/global_config.h>
37 //#include <widget_controller.h>
38 #include <Ecore_File.h>
40 #include <dpl/localization/localization_utils.h>
42 using namespace LocalizationUtils;
43 using namespace WrtDB;
46 namespace WidgetInstall {
47 TaskDbUpdate::TaskDbUpdate(InstallerContext& context) :
48 DPL::TaskDecl<TaskDbUpdate>(this),
51 AddStep(&TaskDbUpdate::StepDbUpdate);
52 AddStep(&TaskDbUpdate::StepSetPkgName);
53 AddStep(&TaskDbUpdate::StepCreateDirs);
54 AddStep(&TaskDbUpdate::StepRenamePath);
57 AddAbortStep(&TaskDbUpdate::StepAbortDBUpdate);
58 AddAbortStep(&TaskDbUpdate::StepAbortRenamePath);
61 void TaskDbUpdate::StepSetPkgName()
63 // We shoud send to backend installer widget id and package name of
64 // installed widget so that backend installer can send two information
65 // to menuscreen. This work is needed for menuscreen to update installation
66 // of widget normally from samsung appstore client.
67 Assert(!!m_context.widgetHandle && "Widget Handle should be initialized");
68 std::string l_pkgname;
70 if (!!m_context.widgetConfig.pkgname) {
72 DPL::ToUTF8String(*m_context.widgetConfig.pkgname);
74 LogInfo("package name is generated by WRT");
75 std::ostringstream pkgname_stream;
77 pkgname_stream << WrtDB::GlobalConfig::GetPkgnamePrefix() <<
78 *m_context.widgetHandle;
80 DPL::String pkgname = DPL::FromUTF8String(pkgname_stream.str());
81 m_context.widgetConfig.pkgname = pkgname;
82 l_pkgname = pkgname_stream.str();
86 LogInfo("package name : " << l_pkgname);
87 LogInfo("GUID : " << m_context.widgetConfig.guid);
89 WrtDB::WidgetDAO widgetDao(*m_context.widgetHandle);
90 widgetDao.setPkgName(m_context.widgetConfig.pkgname);
94 void TaskDbUpdate::StepCreateDirs()
96 std::ostringstream widgetPath;
97 DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
98 if (pkgname.IsNull()) {
99 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
102 widgetPath << GlobalConfig::GetUserInstalledWidgetPath() << "/";
103 widgetPath << pkgname << "/";
105 std::string widgetBinPath = widgetPath.str();
106 std::string widgetIconPath = widgetPath.str();
107 std::string widgetSrcPath = widgetPath.str();
109 _WrtMakeDir(widgetPath.str().c_str(), 0755, WRT_FILEUTILS_RECUR);
111 widgetBinPath += GlobalConfig::GetUserWidgetExecPath();
112 _WrtMakeDir(widgetBinPath.c_str(), 0755, WRT_FILEUTILS_RECUR);
114 widgetIconPath += GlobalConfig::GetUserWidgetDesktopIconPath();
115 _WrtMakeDir(widgetIconPath.c_str(), 0755, WRT_FILEUTILS_RECUR);
117 widgetSrcPath += GlobalConfig::GetWidgetSrcPath();
118 _WrtMakeDir(widgetSrcPath.c_str(), 0755, WRT_FILEUTILS_RECUR);
120 m_context.job->UpdateProgress(
121 InstallerContext::INSTALL_DIR_CREATE,
122 "Widget Directory Created");
125 void TaskDbUpdate::StepDbUpdate()
129 // If there is existing model, remove its database data
130 if (true == m_context.existingWidgetInfo.isExist) {
131 WidgetHandle old = m_context.existingWidgetInfo.existingHandle;
132 LogInfo("Unregistering widget...: " << old);
133 WidgetDAO::unregisterWidget(old);
134 LogInfo("Widget unregistered");
137 /* Set install Time */
138 time(&m_context.widgetConfig.installedTime);
140 LogInfo("Registering widget...");
142 m_context.widgetHandle = WidgetDAO::registerWidget(
143 m_context.widgetConfig,
144 m_context.wacSecurity,
145 GetUserAgentLanguageTags());
147 FOREACH (cap, m_context.staticPermittedDevCaps) {
148 LogInfo("staticPermittedDevCaps : " << cap->first
149 << " smack status: " << cap->second);
152 AceDB::AceDAO::setRequestedDevCaps(
153 *(m_context.widgetHandle),
154 m_context.staticPermittedDevCaps);
156 LogInfo("Widget registered");
158 Catch(WidgetDAO::Exception::DatabaseError)
160 LogWarning("Database failure!");
161 ReThrowMsg(Exceptions::DatabaseFailure, "Database failure!");
163 Catch(DPL::DB::SqlConnection::Exception::Base)
165 LogDebug("Database failure!");
166 ReThrowMsg(Exceptions::DatabaseFailure, "Database failure!");
169 m_context.job->UpdateProgress(
170 InstallerContext::INSTALL_DB_UPDATE,
171 "Widget DB UPDATE Finished");
174 void TaskDbUpdate::StepRenamePath()
176 std::ostringstream instDir;
177 DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
178 if (pkgname.IsNull()) {
179 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
182 instDir << GlobalConfig::GetUserInstalledWidgetPath() << "/";
183 instDir << pkgname << "/";
184 instDir << GlobalConfig::GetWidgetSrcPath();
186 LogDebug("Copy file from temp directory to " << instDir.str());
187 if (!_WrtUtilRemoveDir(instDir.str().c_str())) {
188 _WrtUtilChangeDir(GlobalConfig::GetUserInstalledWidgetPath());
189 ThrowMsg(Exceptions::RemovingFolderFailure,
190 "Error occurs during removing existing folder");
193 if (rename(m_context.tempWidgetPath.c_str(), instDir.str().c_str()) < 0) {
194 ThrowMsg(Exceptions::UnknownError,
195 "Error occurs during renaming widget folder");
198 m_context.job->UpdateProgress(
199 InstallerContext::INSTALL_RENAME_PATH,
200 "Widget Rename path Finished");
203 void TaskDbUpdate::StepAbortDBUpdate()
205 LogWarning("[DB Update Task] Aborting... (DB Clean)");
206 Assert(!!m_context.widgetHandle);
209 WidgetDAO::unregisterWidget(*m_context.widgetHandle);
211 LogDebug("Cleaning DB successful!");
213 Catch(DPL::DB::SqlConnection::Exception::Base)
215 //TODO What should happen here?
216 LogError("Failed to handle StepAbortDBClean!");
217 // ReThrowMsg(Exceptions::DbStepFailed, "Failed to handle StepAbortDBClean!");
221 void TaskDbUpdate::StepAbortRenamePath()
223 Assert(!!m_context.widgetHandle);
224 std::ostringstream widgetPath;
225 widgetPath << GlobalConfig::GetUserInstalledWidgetPath() << "/";
226 widgetPath << *m_context.widgetHandle;
228 struct stat fileInfo;
229 if (stat(widgetPath.str().c_str(), &fileInfo) != 0) {
233 if (rename(widgetPath.str().c_str(),
234 m_context.tempWidgetPath.c_str()) < 0) {
235 LogError("Failed to rename");
236 //Ignoring failures in Abort
239 } //namespace WidgetInstall