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