b7e49a3120925b91918ea6d0740b110801116c70
[framework/web/wrt-installer.git] / src / jobs / widget_uninstall / task_remove_files.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_remove_files.cpp
18  * @author  Lukasz Wrzosek(l.wrzosek@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for uninstaller task for removing widget files
21  */
22
23 #include <widget_uninstall/task_remove_files.h>
24 #include <widget_uninstall/job_widget_uninstall.h>
25 #include <widget_uninstall/uninstaller_context.h>
26 #include <dpl/wrt-dao-rw/widget_dao.h>
27 #include <dpl/wrt-dao-ro/widget_config.h>
28 #include <dpl/assert.h>
29 #include <dpl/utils/wrt_utility.h>
30 #include <ail.h>
31 #include <pkgmgr/pkgmgr_parser.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <widget_install_to_external.h>
35
36 namespace Jobs {
37 namespace WidgetUninstall {
38
39 using namespace WrtDB;
40
41 TaskRemoveFiles::TaskRemoveFiles(UninstallerContext& context) :
42     DPL::TaskDecl<TaskRemoveFiles>(this),
43     m_context(context)
44 {
45     if (!m_context.isExternalWidget) {
46         AddStep(&TaskRemoveFiles::StepRemoveInstallationDirectory);
47     } else {
48         AddStep(&TaskRemoveFiles::StepRemoveExternalWidget);
49     }
50     //AddStep(&TaskRemoveFiles::StepRemoveDesktop);
51     AddStep(&TaskRemoveFiles::StepRemoveManifest);
52     AddStep(&TaskRemoveFiles::StepRemoveExternalLocations);
53     AddStep(&TaskRemoveFiles::StepRemoveFinished);
54 }
55
56 TaskRemoveFiles::~TaskRemoveFiles()
57 {
58 }
59
60 void TaskRemoveFiles::StepRemoveInstallationDirectory()
61 {
62     LogInfo("StepRemoveInstallationDirectory started");
63
64     m_context.removeStarted = true;
65     std::string widgetDir =
66         m_context.locations->getPackageInstallationDir();
67     if(!WrtUtilRemove(widgetDir)){
68         LogWarning("Removing widget installation directory failed");
69     }
70     std::string dataDir = m_context.locations->getUserDataRootDir();
71     if(!WrtUtilRemove(dataDir)){
72         LogWarning(dataDir + " is already removed");
73     }
74     m_context.job->UpdateProgress(
75         UninstallerContext::UNINSTALL_REMOVE_WIDGETDIR,
76         "Widget INstallation Directory Removal Finished");
77 }
78
79 void TaskRemoveFiles::StepRemoveFinished()
80 {
81     LogInfo("StepRemoveFinished finished");
82
83     m_context.job->UpdateProgress(
84         UninstallerContext::UNINSTALL_REMOVE_FINISHED,
85         "Widget remove steps Finished");
86 }
87
88 void TaskRemoveFiles::StepRemoveDesktop()
89 {
90     std::ostringstream desktopFile;
91
92     desktopFile << GlobalConfig::GetUserWidgetDesktopPath() << "/";
93     desktopFile << m_context.pkgname << ".desktop";
94
95     unlink(desktopFile.str().c_str());
96
97     ail_appinfo_h ai = NULL;
98     ail_error_e ret;
99
100     const char* package = m_context.pkgname.c_str();
101     LogDebug("ail delete : " << package);
102
103     ret = ail_package_get_appinfo(package, &ai);
104     if (ai) {
105         ail_package_destroy_appinfo(ai);
106     }
107
108     if (AIL_ERROR_OK == ret) {
109         if ( 0 > ail_desktop_remove(package)) {
110             LogWarning("Failed to remove ail information : " << package);
111         }
112     }
113
114     m_context.job->UpdateProgress(
115         UninstallerContext::UNINSTALL_REMOVE_DESKTOP,
116         "Widget remove desktop Finished");
117 }
118
119 void TaskRemoveFiles::StepRemoveManifest()
120 {
121     std::ostringstream manifest_name;
122     manifest_name << m_context.pkgname << ".xml";
123     std::ostringstream destFile;
124     destFile << "/opt/share/packages" << "/"; //TODO constant with path
125     destFile << manifest_name.str();
126     int ret1 = pkgmgr_parser_parse_manifest_for_uninstallation(destFile.str().c_str(), NULL);
127     int ret2 = unlink(destFile.str().c_str());
128     if(ret1 != 0)
129     {
130         LogWarning("Manifest file failed to parse for uninstallation");
131     }
132     if(ret2 != 0)
133     {
134         LogWarning("No manifest file found: " << destFile.str());
135     }
136     else
137     {
138         LogDebug("Manifest file removed: " << destFile.str());
139     }
140 }
141
142 void TaskRemoveFiles::StepRemoveExternalLocations()
143 {
144     WidgetDAO dao(m_context.locations->getPkgname());
145     LogDebug("Removing external locations:");
146     WrtDB::ExternalLocationList externalPaths = dao.getWidgetExternalLocations();
147     FOREACH(path, externalPaths)
148     {
149         if(WrtUtilFileExists(*path))
150         {
151             LogDebug("  -> " << *path);
152             int ret = remove(path->c_str());
153             if (ret != 0) {
154                 LogError("Failed to remove the file: " << path->c_str() << " with error: " << strerror(errno));
155             }
156         }
157         else if(WrtUtilDirExists(*path))
158         {
159             LogDebug("  -> " << *path);
160             if(!WrtUtilRemove(*path)){
161                 Throw(Jobs::WidgetUninstall::TaskRemoveFiles::Exception::RemoveFilesFailed);
162             }
163         }
164         else
165         {
166             LogWarning("  -> " << *path << "(no such a path)");
167         }
168     }
169     dao.unregisterAllExternalLocations();
170 }
171
172 void TaskRemoveFiles::StepRemoveExternalWidget()
173 {
174     Try {
175         WidgetInstallToExtSingleton::Instance().initialize(m_context.pkgname);
176         WidgetInstallToExtSingleton::Instance().uninstallation();
177         WidgetInstallToExtSingleton::Instance().deinitialize();
178     }
179     Catch (WidgetInstallToExt::Exception::ErrorInstallToExt)
180     {
181         Throw(Jobs::WidgetUninstall::TaskRemoveFiles::Exception::RemoveFilesFailed);
182     }
183 }
184
185 } //namespace WidgetUninstall
186 } //namespace Jobs