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