8a8cf94c634f5d5147d524df1426b36beed78b20
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_db_update.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file    task_db_update.cpp
18  * @author  Lukasz Wrzosek(l.wrzosek@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task database updating
21  */
22 #include <time.h>
23 #include <sys/stat.h>
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>
36 #include <string>
37 //#include <widget_controller.h>
38 #include <Ecore_File.h>
39 #include <sstream>
40 #include <dpl/localization/localization_utils.h>
41
42 using namespace LocalizationUtils;
43 using namespace WrtDB;
44
45 namespace Jobs {
46 namespace WidgetInstall {
47 TaskDbUpdate::TaskDbUpdate(InstallerContext& context) :
48     DPL::TaskDecl<TaskDbUpdate>(this),
49     m_context(context)
50 {
51     AddStep(&TaskDbUpdate::StepDbUpdate);
52     AddStep(&TaskDbUpdate::StepSetPkgName);
53     AddStep(&TaskDbUpdate::StepCreateDirs);
54     AddStep(&TaskDbUpdate::StepRenamePath);
55
56
57     AddAbortStep(&TaskDbUpdate::StepAbortDBUpdate);
58     AddAbortStep(&TaskDbUpdate::StepAbortRenamePath);
59 }
60
61 void TaskDbUpdate::StepSetPkgName()
62 {
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;
69
70     if (!!m_context.widgetConfig.pkgname) {
71         l_pkgname =
72                 DPL::ToUTF8String(*m_context.widgetConfig.pkgname);
73     } else {
74         LogInfo("package name is generated by WRT");
75         std::ostringstream pkgname_stream;
76
77         pkgname_stream << WrtDB::GlobalConfig::GetPkgnamePrefix() <<
78                 *m_context.widgetHandle;
79
80         DPL::String pkgname = DPL::FromUTF8String(pkgname_stream.str());
81         m_context.widgetConfig.pkgname = pkgname;
82         l_pkgname = pkgname_stream.str();
83     }
84
85
86     LogInfo("package name : " << l_pkgname);
87     LogInfo("GUID : " << m_context.widgetConfig.guid);
88
89     WrtDB::WidgetDAO widgetDao(*m_context.widgetHandle);
90     widgetDao.setPkgName(m_context.widgetConfig.pkgname);
91 }
92
93
94 void TaskDbUpdate::StepCreateDirs()
95 {
96     std::ostringstream widgetPath;
97     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
98     if (pkgname.IsNull()) {
99         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
100     }
101
102     widgetPath << GlobalConfig::GetUserInstalledWidgetPath() << "/";
103     widgetPath << pkgname << "/";
104
105     std::string widgetBinPath = widgetPath.str();
106     std::string widgetIconPath = widgetPath.str();
107     std::string widgetSrcPath = widgetPath.str();
108
109     _WrtMakeDir(widgetPath.str().c_str(), 0755, WRT_FILEUTILS_RECUR);
110
111     widgetBinPath += GlobalConfig::GetUserWidgetExecPath();
112     _WrtMakeDir(widgetBinPath.c_str(), 0755, WRT_FILEUTILS_RECUR);
113
114     widgetIconPath += GlobalConfig::GetUserWidgetDesktopIconPath();
115     _WrtMakeDir(widgetIconPath.c_str(), 0755, WRT_FILEUTILS_RECUR);
116
117     widgetSrcPath += GlobalConfig::GetWidgetSrcPath();
118     _WrtMakeDir(widgetSrcPath.c_str(), 0755, WRT_FILEUTILS_RECUR);
119
120     m_context.job->UpdateProgress(
121         InstallerContext::INSTALL_DIR_CREATE,
122         "Widget Directory Created");
123 }
124
125 void TaskDbUpdate::StepDbUpdate()
126 {
127     Try
128     {
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");
135         }
136
137         /* Set install Time */
138         time(&m_context.widgetConfig.installedTime);
139
140         LogInfo("Registering widget...");
141
142         m_context.widgetHandle = WidgetDAO::registerWidget(
143                 m_context.widgetConfig,
144                 m_context.wacSecurity,
145                 GetUserAgentLanguageTags());
146
147         FOREACH (cap, m_context.staticPermittedDevCaps) {
148             LogInfo("staticPermittedDevCaps : " << cap->first
149                     << " smack status: " << cap->second);
150         }
151
152         AceDB::AceDAO::setRequestedDevCaps(
153             *(m_context.widgetHandle),
154             m_context.staticPermittedDevCaps);
155
156         LogInfo("Widget registered");
157     }
158     Catch(WidgetDAO::Exception::DatabaseError)
159     {
160         LogWarning("Database failure!");
161         ReThrowMsg(Exceptions::DatabaseFailure, "Database failure!");
162     }
163     Catch(DPL::DB::SqlConnection::Exception::Base)
164     {
165         LogDebug("Database failure!");
166         ReThrowMsg(Exceptions::DatabaseFailure, "Database failure!");
167     }
168
169     m_context.job->UpdateProgress(
170         InstallerContext::INSTALL_DB_UPDATE,
171         "Widget DB UPDATE Finished");
172 }
173
174 void TaskDbUpdate::StepRenamePath()
175 {
176     std::ostringstream instDir;
177     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
178     if (pkgname.IsNull()) {
179         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
180     }
181
182     instDir << GlobalConfig::GetUserInstalledWidgetPath() << "/";
183     instDir << pkgname << "/";
184     instDir << GlobalConfig::GetWidgetSrcPath();
185
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");
191     }
192
193     if (rename(m_context.tempWidgetPath.c_str(), instDir.str().c_str()) < 0) {
194         ThrowMsg(Exceptions::UnknownError,
195                  "Error occurs during renaming widget folder");
196     }
197
198     m_context.job->UpdateProgress(
199         InstallerContext::INSTALL_RENAME_PATH,
200         "Widget Rename path Finished");
201 }
202
203 void TaskDbUpdate::StepAbortDBUpdate()
204 {
205     LogWarning("[DB Update Task] Aborting... (DB Clean)");
206     Assert(!!m_context.widgetHandle);
207     Try
208     {
209         WidgetDAO::unregisterWidget(*m_context.widgetHandle);
210
211         LogDebug("Cleaning DB successful!");
212     }
213     Catch(DPL::DB::SqlConnection::Exception::Base)
214     {
215         //TODO What should happen here?
216         LogError("Failed to handle StepAbortDBClean!");
217         //        ReThrowMsg(Exceptions::DbStepFailed, "Failed to handle StepAbortDBClean!");
218     }
219 }
220
221 void TaskDbUpdate::StepAbortRenamePath()
222 {
223     Assert(!!m_context.widgetHandle);
224     std::ostringstream widgetPath;
225     widgetPath << GlobalConfig::GetUserInstalledWidgetPath() << "/";
226     widgetPath << *m_context.widgetHandle;
227
228     struct stat fileInfo;
229     if (stat(widgetPath.str().c_str(), &fileInfo) != 0) {
230         return;
231     }
232
233     if (rename(widgetPath.str().c_str(),
234                m_context.tempWidgetPath.c_str()) < 0) {
235         LogError("Failed to rename");
236         //Ignoring failures in Abort
237     }
238 }
239 } //namespace WidgetInstall
240 } //namespace Jobs