tizen beta release
[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 <dpl/ace-dao-rw/AceDAO.h>
32 #include <string>
33 #include <dpl/assert.h>
34 #include <dpl/wrt-dao-ro/global_config.h>
35 //#include <widget_controller.h>
36 #include <Ecore_File.h>
37 #include <sstream>
38 #include <dpl/localization/localization_utils.h>
39
40 using namespace LocalizationUtils;
41 using namespace WrtDB;
42
43 namespace Jobs {
44 namespace WidgetInstall {
45 TaskDbUpdate::TaskDbUpdate(InstallerContext& context) :
46     DPL::TaskDecl<TaskDbUpdate>(this),
47     m_context(context)
48 {
49     AddStep(&TaskDbUpdate::StepDbUpdate);
50     AddStep(&TaskDbUpdate::StepSetPkgName);
51     AddStep(&TaskDbUpdate::StepCreateDirs);
52     AddStep(&TaskDbUpdate::StepRenamePath);
53
54
55     AddAbortStep(&TaskDbUpdate::StepAbortDBUpdate);
56     AddAbortStep(&TaskDbUpdate::StepAbortRenamePath);
57 }
58
59 void TaskDbUpdate::StepSetPkgName()
60 {
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;
67
68     if (!!m_context.widgetConfig.pkgname) {
69         l_pkgname =
70                 DPL::ToUTF8String(*m_context.widgetConfig.pkgname);
71     } else {
72         LogInfo("package name is generated by WRT");
73         std::ostringstream pkgname_stream;
74
75         pkgname_stream << WrtDB::GlobalConfig::GetPkgnamePrefix() <<
76                 *m_context.widgetHandle;
77
78         DPL::String pkgname = DPL::FromUTF8String(pkgname_stream.str());
79         m_context.widgetConfig.pkgname = pkgname;
80         l_pkgname = pkgname_stream.str();
81     }
82
83
84     LogInfo("package name : " << l_pkgname);
85     LogInfo("GUID : " << m_context.widgetConfig.guid);
86
87     WrtDB::WidgetDAO widgetDao(*m_context.widgetHandle);
88     widgetDao.setPkgName(m_context.widgetConfig.pkgname);
89 }
90
91
92 void TaskDbUpdate::StepCreateDirs()
93 {
94     std::ostringstream widgetPath;
95     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
96     if (pkgname.IsNull()) {
97         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
98     }
99
100     widgetPath << GlobalConfig::GetUserInstalledWidgetPath() << "/";
101     widgetPath << pkgname << "/";
102
103     std::string widgetBinPath = widgetPath.str();
104     std::string widgetIconPath = widgetPath.str();
105     std::string widgetSrcPath = widgetPath.str();
106
107     _WrtMakeDir(widgetPath.str().c_str(), 0755, WRT_FILEUTILS_RECUR);
108
109     widgetBinPath += GlobalConfig::GetUserWidgetExecPath();
110     _WrtMakeDir(widgetBinPath.c_str(), 0755, WRT_FILEUTILS_RECUR);
111
112     widgetIconPath += GlobalConfig::GetUserWidgetDesktopIconPath();
113     _WrtMakeDir(widgetIconPath.c_str(), 0755, WRT_FILEUTILS_RECUR);
114
115     widgetSrcPath += GlobalConfig::GetWidgetSrcPath();
116     _WrtMakeDir(widgetSrcPath.c_str(), 0755, WRT_FILEUTILS_RECUR);
117 }
118
119 void TaskDbUpdate::StepDbUpdate()
120 {
121     Try
122     {
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");
129         }
130
131         /* Set install Time */
132         time(&m_context.widgetConfig.installedTime);
133
134         LogInfo("Registering widget...");
135
136         m_context.widgetHandle = WidgetDAO::registerWidget(
137                 m_context.widgetConfig,
138                 m_context.wacSecurity,
139                 GetUserAgentLanguageTags());
140
141         AceDB::AceDAO::setStaticDevCapPermissions(
142             *(m_context.widgetHandle),
143             m_context.staticPermittedDevCaps);
144
145         LogInfo("Widget registered");
146     }
147     Catch(WidgetDAO::Exception::DatabaseError)
148     {
149         LogWarning("Database failure!");
150         ReThrowMsg(Exceptions::DatabaseFailure, "Database failure!");
151     }
152     Catch(DPL::DB::SqlConnection::Exception::Base)
153     {
154         LogDebug("Database failure!");
155         ReThrowMsg(Exceptions::DatabaseFailure, "Database failure!");
156     }
157
158     m_context.job->UpdateProgress(
159         InstallerContext::INSTALL_DB_UPDATE,
160         "Widget DB UPDATE Finished");
161 }
162
163 void TaskDbUpdate::StepRenamePath()
164 {
165     std::ostringstream instDir;
166     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
167     if (pkgname.IsNull()) {
168         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
169     }
170
171     instDir << GlobalConfig::GetUserInstalledWidgetPath() << "/";
172     instDir << pkgname << "/";
173     instDir << GlobalConfig::GetWidgetSrcPath();
174
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");
180     }
181
182     if (rename(m_context.tempWidgetPath.c_str(), instDir.str().c_str()) < 0) {
183         ThrowMsg(Exceptions::UnknownError,
184                  "Error occurs during renaming widget folder");
185     }
186
187     m_context.job->UpdateProgress(
188         InstallerContext::INSTALL_RENAME_PATH,
189         "Widget Rename path Finished");
190 }
191
192 void TaskDbUpdate::StepAbortDBUpdate()
193 {
194     LogWarning("[DB Update Task] Aborting... (DB Clean)");
195     Assert(!!m_context.widgetHandle);
196     Try
197     {
198         WidgetDAO::unregisterWidget(*m_context.widgetHandle);
199
200         LogDebug("Cleaning DB successful!");
201     }
202     Catch(DPL::DB::SqlConnection::Exception::Base)
203     {
204         //TODO What should happen here?
205         LogError("Failed to handle StepAbortDBClean!");
206         //        ReThrowMsg(Exceptions::DbStepFailed, "Failed to handle StepAbortDBClean!");
207     }
208 }
209
210 void TaskDbUpdate::StepAbortRenamePath()
211 {
212     Assert(!!m_context.widgetHandle);
213     std::ostringstream widgetPath;
214     widgetPath << GlobalConfig::GetUserInstalledWidgetPath() << "/";
215     widgetPath << *m_context.widgetHandle;
216
217     struct stat fileInfo;
218     if (stat(widgetPath.str().c_str(), &fileInfo) != 0) {
219         return;
220     }
221
222     if (rename(widgetPath.str().c_str(),
223                m_context.tempWidgetPath.c_str()) < 0) {
224         LogError("Failed to rename");
225         //Ignoring failures in Abort
226     }
227 }
228 } //namespace WidgetInstall
229 } //namespace Jobs