Update wrt-installer_0.0.54
[framework/web/wrt-installer.git] / src / jobs / widget_uninstall / job_widget_uninstall.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 #include <dpl/utils/file_utils.h>
18
19 #include <widget_uninstall/job_widget_uninstall.h>
20 #include <widget_uninstall/widget_uninstall_errors.h>
21 #include <widget_uninstall/task_check.h>
22 #include <widget_uninstall/task_db_update.h>
23 #include <widget_uninstall/task_remove_files.h>
24 #include <widget_uninstall/task_smack.h>
25 #include <widget_uninstall/task_uninstall_ospsvc.h>
26 #include <pkg-manager/pkgmgr_signal.h>
27
28 using namespace WrtDB;
29
30 namespace { //anonymous
31 class UninstallerTaskFail :
32     public DPL::TaskDecl<UninstallerTaskFail>
33 {
34   private:
35     bool m_uninstalled;
36
37     void StepFail()
38     {
39         if(m_uninstalled) {
40             ThrowMsg(Jobs::WidgetUninstall::Exceptions::WidgetNotExist,
41                     "Widget does not exist");
42         } else {
43             Throw(Jobs::WidgetUninstall::Exceptions::Base);
44         }
45     }
46
47   public:
48     UninstallerTaskFail(bool uninstalled) :
49         DPL::TaskDecl<UninstallerTaskFail>(this),
50         m_uninstalled(uninstalled)
51     {
52         AddStep(&UninstallerTaskFail::StepFail);
53     }
54 };
55 }
56
57 namespace Jobs {
58 namespace WidgetUninstall {
59 JobWidgetUninstall::JobWidgetUninstall(WidgetHandle widgetHandle,
60         const WidgetUninstallationStruct &uninstallerStruct) :
61     Job(Uninstallation),
62     JobContextBase<WidgetUninstallationStruct>(uninstallerStruct)
63 {
64     using namespace PackageManager;
65     m_context.widgetHandle = widgetHandle;
66     m_context.removeStarted = false;
67     m_context.removeFinished = false;
68     m_context.uninstallStep = UninstallerContext::UNINSTALL_START;
69     m_context.job = this;
70
71     Try
72     {
73         WrtDB::WidgetDAOReadOnly dao(widgetHandle);
74         m_context.pkgname = DPL::ToUTF8String(*dao.getPkgname());
75         m_context.locations = WidgetLocation(m_context.pkgname);
76
77         LogInfo("Widget model exists. Pkg name: " << m_context.pkgname);
78
79         AddTask(new TaskSmack(m_context));
80         AddTask(new TaskCheck(m_context));
81
82         if (dao.getPkgType() == PKG_TYPE_TIZEN_WITHSVCAPP) {
83             AddTask(new TaskUninstallOspsvc(m_context));
84         }
85         AddTask(new TaskRemoveFiles(m_context));
86         AddTask(new TaskDbUpdate(m_context));
87
88         // send start signal of pkgmgr
89         if (PkgmgrSignalSingleton::Instance().setPkgname(m_context.pkgname)) {
90             PkgmgrSignalSingleton::Instance().sendSignal(
91                     PKGMGR_START_KEY,
92                     PKGMGR_START_UNINSTALL);
93         }
94     } Catch(FileUtils::CreateDirectoryException) {
95         LogError("Failed to create temporary path for widget");
96         AddTask(new UninstallerTaskFail(false));
97     } Catch (WidgetDAOReadOnly::Exception::WidgetNotExist) {
98         AddTask(new UninstallerTaskFail(true));
99     } Catch (WidgetDAOReadOnly::Exception::Base) {
100         AddTask(new UninstallerTaskFail(false));
101     }
102 }
103
104 WidgetHandle JobWidgetUninstall::getRemovedWidgetHandle() const
105 {
106     return m_context.widgetHandle;
107 }
108
109 bool JobWidgetUninstall::getRemoveStartedFlag() const
110 {
111     return m_context.removeStarted;
112 }
113
114 bool JobWidgetUninstall::getRemoveFinishedFlag() const
115 {
116     return m_context.removeFinished;
117 }
118
119 void JobWidgetUninstall::SendProgress()
120 {
121     using namespace PackageManager;
122     if (!getRemoveStartedFlag() ||
123         (getRemoveStartedFlag() && getRemoveFinishedFlag())) {
124         if (NULL != getInstallerStruct().progressCallback) {
125             // send progress signal of pkgmgr
126             std::ostringstream percent;
127             percent << static_cast<int>(GetProgressPercent());
128             PkgmgrSignalSingleton::Instance().sendSignal(
129                         PKGMGR_PROGRESS_KEY,
130                         percent.str());
131
132             LogDebug("Call widget uninstall progressCallback");
133             getInstallerStruct().progressCallback(
134                     getInstallerStruct().userParam,
135                     GetProgressPercent(), GetProgressDescription());
136         }
137     }
138 }
139
140 void JobWidgetUninstall::SendFinishedSuccess()
141 {
142     using namespace PackageManager;
143     // send signal of pkgmgr
144     PkgmgrSignalSingleton::Instance().sendSignal(
145                 PKGMGR_END_KEY,
146                 PKGMGR_END_SUCCESS);
147
148     LogDebug("Call widget uninstall success finishedCallback");
149     getInstallerStruct().finishedCallback(getInstallerStruct().userParam,
150             getRemovedWidgetHandle(),Exceptions::Success);
151 }
152
153 void JobWidgetUninstall::SendFinishedFailure()
154 {
155     using namespace PackageManager;
156     LogError("Error in uninstallation step: " << m_exceptionCaught);
157     LogError("Message: " << m_exceptionMessage);
158
159     // send signal of pkgmgr
160     PkgmgrSignalSingleton::Instance().sendSignal(
161                 PKGMGR_END_KEY,
162                 PKGMGR_END_FAILURE);
163
164     LogDebug("Call widget uninstall failure finishedCallback");
165     getInstallerStruct().finishedCallback(getInstallerStruct().userParam,
166         getRemovedWidgetHandle(), m_exceptionCaught); //TODO
167     LogDebug("[JobWidgetUninstall] Asynchronous failure callback status sent");
168 }
169
170 void JobWidgetUninstall::SaveExceptionData(const Jobs::JobExceptionBase &e)
171 {
172     m_exceptionCaught = static_cast<Exceptions::Type>(e.getParam());
173     m_exceptionMessage = e.GetMessage();
174 }
175 } //namespace WidgetUninstall
176 } //namespace Jobs