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 <dpl/ace-dao-rw/AceDAO.h>
33 #include <dpl/assert.h>
34 #include <dpl/wrt-dao-ro/global_config.h>
35 //#include <widget_controller.h>
36 #include <Ecore_File.h>
38 #include <dpl/localization/localization_utils.h>
40 using namespace LocalizationUtils;
41 using namespace WrtDB;
44 namespace WidgetInstall {
45 TaskDbUpdate::TaskDbUpdate(InstallerContext& context) :
46 DPL::TaskDecl<TaskDbUpdate>(this),
49 AddStep(&TaskDbUpdate::StepDbUpdate);
50 AddStep(&TaskDbUpdate::StepSetPkgName);
51 AddStep(&TaskDbUpdate::StepCreateDirs);
52 AddStep(&TaskDbUpdate::StepRenamePath);
55 AddAbortStep(&TaskDbUpdate::StepAbortDBUpdate);
56 AddAbortStep(&TaskDbUpdate::StepAbortRenamePath);
59 void TaskDbUpdate::StepSetPkgName()
61 // We shoud send to backend installer widget id and package name of
62 // installed widget so that backend installer can send two information
63 // to menuscreen. This work is needed for menuscreen to update installation
64 // of widget normally from samsung appstore client.
65 Assert(!!m_context.widgetHandle && "Widget Handle should be initialized");
66 std::string l_pkgname;
68 if (!!m_context.widgetConfig.pkgname) {
70 DPL::ToUTF8String(*m_context.widgetConfig.pkgname);
72 LogInfo("package name is generated by WRT");
73 std::ostringstream pkgname_stream;
75 pkgname_stream << WrtDB::GlobalConfig::GetPkgnamePrefix() <<
76 *m_context.widgetHandle;
78 DPL::String pkgname = DPL::FromUTF8String(pkgname_stream.str());
79 m_context.widgetConfig.pkgname = pkgname;
80 l_pkgname = pkgname_stream.str();
84 LogInfo("package name : " << l_pkgname);
85 LogInfo("GUID : " << m_context.widgetConfig.guid);
87 WrtDB::WidgetDAO widgetDao(*m_context.widgetHandle);
88 widgetDao.setPkgName(m_context.widgetConfig.pkgname);
92 void TaskDbUpdate::StepCreateDirs()
94 std::ostringstream widgetPath;
95 DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
96 if (pkgname.IsNull()) {
97 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
100 widgetPath << GlobalConfig::GetUserInstalledWidgetPath() << "/";
101 widgetPath << pkgname << "/";
103 std::string widgetBinPath = widgetPath.str();
104 std::string widgetIconPath = widgetPath.str();
105 std::string widgetSrcPath = widgetPath.str();
107 _WrtMakeDir(widgetPath.str().c_str(), 0755, WRT_FILEUTILS_RECUR);
109 widgetBinPath += GlobalConfig::GetUserWidgetExecPath();
110 _WrtMakeDir(widgetBinPath.c_str(), 0755, WRT_FILEUTILS_RECUR);
112 widgetIconPath += GlobalConfig::GetUserWidgetDesktopIconPath();
113 _WrtMakeDir(widgetIconPath.c_str(), 0755, WRT_FILEUTILS_RECUR);
115 widgetSrcPath += GlobalConfig::GetWidgetSrcPath();
116 _WrtMakeDir(widgetSrcPath.c_str(), 0755, WRT_FILEUTILS_RECUR);
119 void TaskDbUpdate::StepDbUpdate()
123 // If there is existing model, remove its database data
124 if (true == m_context.existingWidgetInfo.isExist) {
125 WidgetHandle old = m_context.existingWidgetInfo.existingHandle;
126 LogInfo("Unregistering widget...: " << old);
127 WidgetDAO::unregisterWidget(old);
128 LogInfo("Widget unregistered");
131 /* Set install Time */
132 time(&m_context.widgetConfig.installedTime);
134 LogInfo("Registering widget...");
136 m_context.widgetHandle = WidgetDAO::registerWidget(
137 m_context.widgetConfig,
138 m_context.wacSecurity,
139 GetUserAgentLanguageTags());
141 AceDB::AceDAO::setStaticDevCapPermissions(
142 *(m_context.widgetHandle),
143 m_context.staticPermittedDevCaps);
145 LogInfo("Widget registered");
147 Catch(WidgetDAO::Exception::DatabaseError)
149 LogWarning("Database failure!");
150 ReThrowMsg(Exceptions::DatabaseFailure, "Database failure!");
152 Catch(DPL::DB::SqlConnection::Exception::Base)
154 LogDebug("Database failure!");
155 ReThrowMsg(Exceptions::DatabaseFailure, "Database failure!");
158 m_context.job->UpdateProgress(
159 InstallerContext::INSTALL_DB_UPDATE,
160 "Widget DB UPDATE Finished");
163 void TaskDbUpdate::StepRenamePath()
165 std::ostringstream instDir;
166 DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
167 if (pkgname.IsNull()) {
168 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
171 instDir << GlobalConfig::GetUserInstalledWidgetPath() << "/";
172 instDir << pkgname << "/";
173 instDir << GlobalConfig::GetWidgetSrcPath();
175 LogDebug("Copy file from temp directory to " << instDir.str());
176 if (!_WrtUtilRemoveDir(instDir.str().c_str())) {
177 _WrtUtilChangeDir(GlobalConfig::GetUserInstalledWidgetPath());
178 ThrowMsg(Exceptions::RemovingFolderFailure,
179 "Error occurs during removing existing folder");
182 if (rename(m_context.tempWidgetPath.c_str(), instDir.str().c_str()) < 0) {
183 ThrowMsg(Exceptions::UnknownError,
184 "Error occurs during renaming widget folder");
187 m_context.job->UpdateProgress(
188 InstallerContext::INSTALL_RENAME_PATH,
189 "Widget Rename path Finished");
192 void TaskDbUpdate::StepAbortDBUpdate()
194 LogWarning("[DB Update Task] Aborting... (DB Clean)");
195 Assert(!!m_context.widgetHandle);
198 WidgetDAO::unregisterWidget(*m_context.widgetHandle);
200 LogDebug("Cleaning DB successful!");
202 Catch(DPL::DB::SqlConnection::Exception::Base)
204 //TODO What should happen here?
205 LogError("Failed to handle StepAbortDBClean!");
206 // ReThrowMsg(Exceptions::DbStepFailed, "Failed to handle StepAbortDBClean!");
210 void TaskDbUpdate::StepAbortRenamePath()
212 Assert(!!m_context.widgetHandle);
213 std::ostringstream widgetPath;
214 widgetPath << GlobalConfig::GetUserInstalledWidgetPath() << "/";
215 widgetPath << *m_context.widgetHandle;
217 struct stat fileInfo;
218 if (stat(widgetPath.str().c_str(), &fileInfo) != 0) {
222 if (rename(widgetPath.str().c_str(),
223 m_context.tempWidgetPath.c_str()) < 0) {
224 LogError("Failed to rename");
225 //Ignoring failures in Abort
228 } //namespace WidgetInstall