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