Use GList in manifest_x structures
[platform/core/appfw/app-installers.git] / src / common / step / step_remove_icons.cc
1 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/step/step_remove_icons.h"
6
7 #include <boost/system/error_code.hpp>
8 #include <pkgmgr-info.h>
9
10 #include <cstring>
11
12 #include "common/backup_paths.h"
13 #include "common/utils/file_util.h"
14 #include "common/utils/glist_range.h"
15
16 namespace common_installer {
17 namespace filesystem {
18
19 namespace bs = boost::system;
20 namespace bf = boost::filesystem;
21
22 Step::Status StepRemoveIcons::precheck() {
23   if (!context_->manifest_data.get()) {
24     LOG(ERROR) << "manifest_data attribute is empty";
25     return Status::ERROR;
26   }
27
28   return Status::OK;
29 }
30
31 Step::Status StepRemoveIcons::process() {
32   for (application_x* app :
33        GListRange<application_x*>(context_->manifest_data.get()->application)) {
34     if (strcmp(app->component_type, "uiapp") != 0)
35       continue;
36
37     bf::path app_icon = bf::path(getIconPath(context_->uid.get()))
38       / bf::path(app->appid);
39     if (app->icon) {
40       icon_x* icon = reinterpret_cast<icon_x*>(app->icon->data);
41       app_icon += bf::path(icon->text).extension();
42     } else {
43       app_icon += ".png";
44     }
45     if (bf::exists(app_icon)) {
46       bf::path backup_icon_file = GetBackupPathForIconFile(app_icon);
47       if (!MoveFile(app_icon, backup_icon_file)) {
48         LOG(ERROR) << "Failed to create backup for icon: " << app_icon;
49         return Status::ERROR;
50       }
51       backups_.emplace_back(backup_icon_file, app_icon);
52     }
53   }
54   return Status::OK;
55 }
56
57 Step::Status StepRemoveIcons::clean() {
58   bs::error_code error;
59   if (!backups_.empty()) {
60     LOG(DEBUG) << "Clean up icons files...";
61     for (auto& pair : backups_) {
62       bf::remove(pair.first, error);
63       if (error) {
64         LOG(WARNING) << "Failed to remove: " << pair.first;
65       }
66     }
67   }
68   return Status::OK;
69 }
70
71 Step::Status StepRemoveIcons::undo() {
72   Step::Status ret = Status::OK;
73   if (!backups_.empty()) {
74     LOG(DEBUG) << "Restoring icons files...";
75     for (auto& pair : backups_) {
76       if (!MoveFile(pair.first, pair.second)) {
77         LOG(ERROR) << "Failed to restore: " << pair.second;
78         // We need to try to restore all icons anyway...
79         ret = Status::ERROR;
80       }
81     }
82   }
83   return ret;
84 }
85 }  // namespace filesystem
86 }  // namespace common_installer