Create/Remove vconf for security settings
[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 #include <vconf.h>
36
37 namespace Jobs {
38 namespace WidgetUninstall {
39
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
66 void TaskRemoveFiles::StepRemoveInstallationDirectory()
67 {
68     LogInfo("StepRemoveInstallationDirectory started");
69
70     m_context.removeStarted = true;
71     std::string widgetDir =
72         m_context.locations->getPackageInstallationDir();
73     if(!WrtUtilRemove(widgetDir)){
74         LogWarning("Removing widget installation directory failed");
75     }
76     std::string dataDir = m_context.locations->getUserDataRootDir();
77     if(!WrtUtilRemove(dataDir)){
78         LogWarning(dataDir + " is already removed");
79     }
80     m_context.job->UpdateProgress(
81         UninstallerContext::UNINSTALL_REMOVE_WIDGETDIR,
82         "Widget INstallation Directory Removal Finished");
83 }
84
85 void TaskRemoveFiles::StepRemoveFinished()
86 {
87     LogInfo("StepRemoveFinished finished");
88
89     m_context.job->UpdateProgress(
90         UninstallerContext::UNINSTALL_REMOVE_FINISHED,
91         "Widget remove steps Finished");
92 }
93
94 void TaskRemoveFiles::StepRemoveDesktop()
95 {
96     std::ostringstream desktopFile;
97
98     desktopFile << GlobalConfig::GetUserWidgetDesktopPath() << "/";
99     desktopFile << m_context.pkgname << ".desktop";
100
101     unlink(desktopFile.str().c_str());
102
103     ail_appinfo_h ai = NULL;
104     ail_error_e ret;
105
106     const char* package = m_context.pkgname.c_str();
107     LogDebug("ail delete : " << package);
108
109     ret = ail_package_get_appinfo(package, &ai);
110     if (ai) {
111         ail_package_destroy_appinfo(ai);
112     }
113
114     if (AIL_ERROR_OK == ret) {
115         if ( 0 > ail_desktop_remove(package)) {
116             LogWarning("Failed to remove ail information : " << package);
117         }
118     }
119
120     m_context.job->UpdateProgress(
121         UninstallerContext::UNINSTALL_REMOVE_DESKTOP,
122         "Widget remove desktop Finished");
123 }
124
125 void TaskRemoveFiles::StepRemoveManifest()
126 {
127     std::ostringstream manifest_name;
128     manifest_name << m_context.pkgname << ".xml";
129     std::ostringstream destFile;
130     destFile << "/opt/share/packages" << "/"; //TODO constant with path
131     destFile << manifest_name.str();
132     int ret1 = pkgmgr_parser_parse_manifest_for_uninstallation(destFile.str().c_str(), NULL);
133     int ret2 = unlink(destFile.str().c_str());
134     if(ret1 != 0)
135     {
136         LogWarning("Manifest file failed to parse for uninstallation");
137     }
138     if(ret2 != 0)
139     {
140         LogWarning("No manifest file found: " << destFile.str());
141     }
142     else
143     {
144         LogDebug("Manifest file removed: " << destFile.str());
145     }
146 }
147
148 void TaskRemoveFiles::StepRemoveExternalLocations()
149 {
150     WidgetDAO dao(m_context.locations->getPkgname());
151     LogDebug("Removing external locations:");
152     WrtDB::ExternalLocationList externalPaths = dao.getWidgetExternalLocations();
153     FOREACH(path, externalPaths)
154     {
155         if(WrtUtilFileExists(*path))
156         {
157             LogDebug("  -> " << *path);
158             int ret = remove(path->c_str());
159             if (ret != 0) {
160                 LogError("Failed to remove the file: " << path->c_str() << " with error: " << strerror(errno));
161             }
162         }
163         else if(WrtUtilDirExists(*path))
164         {
165             LogDebug("  -> " << *path);
166             if(!WrtUtilRemove(*path)){
167                 Throw(Jobs::WidgetUninstall::TaskRemoveFiles::Exception::RemoveFilesFailed);
168             }
169         }
170         else
171         {
172             LogWarning("  -> " << *path << "(no such a path)");
173         }
174     }
175     dao.unregisterAllExternalLocations();
176 }
177
178 void TaskRemoveFiles::StepRemoveVconf()
179 {
180     std::string key =
181         VCONF_KEY_PREFIX
182         + DPL::ToUTF8String(m_context.locations->getPkgname()) ;
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.pkgname);
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