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