Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / backup / step_backup_icons.cc
1 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/step/backup/step_backup_icons.h"
6
7 #include <pkgmgr-info.h>
8
9 #include <filesystem>
10 #include <string>
11
12 #include "common/utils/paths.h"
13 #include "common/utils/file_util.h"
14 #include "common/utils/glist_range.h"
15
16 namespace fs = std::filesystem;
17
18 namespace common_installer {
19 namespace backup {
20
21 StepBackupIcons::StepBackupIcons(InstallerContext* context) : Step(context),
22     extra_icon_path_(nullptr) {}
23
24 Step::Status StepBackupIcons::process() {
25   extra_icon_path_ = getIconPath(context_->uid.get(),
26       context_->is_readonly_package.get());
27   if (!extra_icon_path_)
28     return Status::OK;
29
30   GetIconInfo();
31
32   if (!BackupIcons())
33     return Status::ICON_ERROR;
34
35   LOG(DEBUG) << "Icons backup created";
36   return Status::OK;
37 }
38
39 Step::Status StepBackupIcons::clean() {
40   RemoveBackupIcons();
41   LOG(DEBUG) << "Icons backup removed";
42
43   return Status::OK;
44 }
45
46 Step::Status StepBackupIcons::undo() {
47   for (auto& pair : icons_) {
48     if (!MoveFile(pair.second, pair.first)) {
49       LOG(ERROR) << "Cannot revert icon from backup: " << pair.first;
50       return Status::ICON_ERROR;
51     }
52   }
53   LOG(DEBUG) << "Icons reverted from backup";
54
55   return Status::OK;
56 }
57
58 bool StepBackupIcons::BackupIcons() {
59   for (auto& pair : icons_) {
60     if (!MoveFile(pair.first, pair.second, true)) {
61       LOG(ERROR) << "Cannot create backup for icon: " << pair.first;
62       return false;
63     }
64   }
65
66   return true;
67 }
68
69 void StepBackupIcons::RemoveBackupIcons() {
70   for (auto& pair : icons_)
71     Remove(pair.second);
72 }
73
74 void StepBackupIcons::GetIconInfo() {
75   for (auto iter = fs::directory_iterator(extra_icon_path_);
76       iter != fs::directory_iterator(); ++iter) {
77     if (!fs::is_regular_file(iter->path()))
78       continue;
79
80     AddAppIconToList(iter->path());
81   }
82 }
83
84 void StepBackupIcons::AddAppIconToList(std::filesystem::path path) {
85   for (application_x* app : GListRange<application_x*>(
86       context_->old_manifest_data.get()->application)) {
87     if (!app->icon)
88       continue;
89
90     fs::path filename = path.filename();
91     filename.replace_extension();
92     std::string id = filename.string();
93     if (id == app->appid) {
94       fs::path icon_backup = GetBackupPathForIconFile(path);
95       icons_.emplace_back(path, icon_backup);
96     }
97   }
98 }
99
100 }  // namespace backup
101 }  // namespace common_installer