Replacing recursive opendir with a utility function
[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
33 namespace Jobs {
34 namespace WidgetUninstall {
35
36 using namespace WrtDB;
37
38 TaskRemoveFiles::TaskRemoveFiles(UninstallerContext& context) :
39     DPL::TaskDecl<TaskRemoveFiles>(this),
40     m_context(context)
41 {
42     AddStep(&TaskRemoveFiles::StepRemoveInstallationDirectory);
43     //AddStep(&TaskRemoveFiles::StepRemoveDesktop);
44     AddStep(&TaskRemoveFiles::StepRemoveManifest);
45     AddStep(&TaskRemoveFiles::StepRemoveExternalLocations);
46     AddStep(&TaskRemoveFiles::StepRemoveFinished);
47 }
48
49 TaskRemoveFiles::~TaskRemoveFiles()
50 {
51 }
52
53 void TaskRemoveFiles::StepRemoveInstallationDirectory()
54 {
55     LogInfo("StepRemoveInstallationDirectory started");
56
57     m_context.removeStarted = true;
58     std::string widgetDir =
59         m_context.locations->getPackageInstallationDir();
60     if(!WrtUtilRemove(widgetDir)){
61         LogWarning("Removing widget installation directory failed");
62     }
63     m_context.job->UpdateProgress(
64         UninstallerContext::UNINSTALL_REMOVE_WIDGETDIR,
65         "Widget INstallation Directory Removal Finished");
66 }
67
68 void TaskRemoveFiles::StepRemoveFinished()
69 {
70     LogInfo("StepRemoveFinished finished");
71
72     m_context.job->UpdateProgress(
73         UninstallerContext::UNINSTALL_REMOVE_FINISHED,
74         "Widget remove steps Finished");
75 }
76
77 void TaskRemoveFiles::StepRemoveDesktop()
78 {
79     std::ostringstream desktopFile;
80
81     desktopFile << GlobalConfig::GetUserWidgetDesktopPath() << "/";
82     desktopFile << m_context.pkgname << ".desktop";
83
84     unlink(desktopFile.str().c_str());
85
86     ail_appinfo_h ai = NULL;
87     ail_error_e ret;
88
89     const char* package = m_context.pkgname.c_str();
90     LogDebug("ail delete : " << package);
91
92     ret = ail_package_get_appinfo(package, &ai);
93     if (ai) {
94         ail_package_destroy_appinfo(ai);
95     }
96
97     if (AIL_ERROR_OK == ret) {
98         if ( 0 > ail_desktop_remove(package)) {
99             LogWarning("Failed to remove ail information : " << package);
100         }
101     }
102
103     m_context.job->UpdateProgress(
104         UninstallerContext::UNINSTALL_REMOVE_DESKTOP,
105         "Widget remove desktop Finished");
106 }
107
108 void TaskRemoveFiles::StepRemoveManifest()
109 {
110     std::ostringstream manifest_name;
111     manifest_name << m_context.pkgname << ".xml";
112     std::ostringstream destFile;
113     destFile << "/opt/share/packages" << "/"; //TODO constant with path
114     destFile << manifest_name.str();
115     int ret1 = pkgmgr_parser_parse_manifest_for_uninstallation(destFile.str().c_str(), NULL);
116     int ret2 = unlink(destFile.str().c_str());
117     if(ret1 != 0)
118     {
119         LogWarning("Manifest file failed to parse for uninstallation");
120     }
121     if(ret2 != 0)
122     {
123         LogWarning("No manifest file found: " << destFile.str());
124     }
125     else
126     {
127         LogDebug("Manifest file removed: " << destFile.str());
128     }
129 }
130
131 void TaskRemoveFiles::StepRemoveExternalLocations()
132 {
133     WidgetDAO dao(m_context.widgetHandle);
134     LogDebug("Removing external locations:");
135     WrtDB::ExternalLocationList externalPaths = dao.getWidgetExternalLocations();
136     FOREACH(path, externalPaths)
137     {
138         if(WrtUtilFileExists(*path))
139         {
140             LogDebug("  -> " << *path);
141             remove(path->c_str());
142         }
143         else if(WrtUtilDirExists(*path))
144         {
145             LogDebug("  -> " << *path);
146             if(!WrtUtilRemove(*path)){
147                 Throw(Jobs::WidgetUninstall::TaskRemoveFiles::Exception::RemoveFilesFailed);
148             }
149         }
150         else
151         {
152             LogWarning("  -> " << *path << "(no such a path)");
153         }
154     }
155     dao.unregisterAllExternalLocations();
156 }
157
158 } //namespace WidgetUninstall
159 } //namespace Jobs