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