Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_create_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/filesystem/step_create_icons.h"
6
7 #include <pkgmgr-info.h>
8
9 #include <filesystem>
10 #include <system_error>
11
12 #include "common/utils/file_util.h"
13 #include "common/utils/glist_range.h"
14
15 namespace fs = std::filesystem;
16
17 namespace common_installer {
18 namespace filesystem {
19
20 Step::Status StepCreateIcons::undo() {
21   for (auto& icon : icons_)
22     RemoveAll(icon);
23
24   return Status::OK;
25 }
26
27 Step::Status StepCreateIcons::process() {
28   const char* extra_icon_path = getIconPath(context_->uid.get(),
29       context_->is_readonly_package.get());
30   if (!extra_icon_path)
31     return Status::OK;
32
33   fs::path destination = extra_icon_path;
34   std::error_code error;
35   if (!fs::exists(destination)) {
36     fs::create_directories(destination, error);
37     if (error) {
38       LOG(ERROR) << "Cannot create directory of application icons: "
39                  << destination;
40       return Status::ERROR;
41     }
42   }
43
44   for (application_x* app :
45       GListRange<application_x*>(context_->manifest_data.get()->application)) {
46     // TODO(t.iwanek): this ignores icon locale as well in same way as other
47     // steps -> icons should be localized
48     if (!app->icon)
49       continue;
50
51     icon_x* icon = reinterpret_cast<icon_x*>(app->icon->data);
52     fs::path source(icon->text);
53     if (!fs::exists(source))
54       continue;
55
56     fs::path destination_path = destination / app->appid;
57     if (source.has_extension())
58       destination_path += source.extension();
59     else
60       destination_path += ".png";
61     fs::copy_file(source, destination_path,
62                   fs::copy_options::overwrite_existing, error);
63     if (error) {
64       LOG(ERROR) << "Cannot create package icon: " << destination_path
65                  << " , error: " << error;
66       return Status::ICON_ERROR;
67     }
68     icons_.push_back(destination_path);
69   }
70   LOG(DEBUG) << "Icon files created";
71   return Status::OK;
72 }
73
74 }  // namespace filesystem
75 }  // namespace common_installer