Add StepWgtPatchIcon at ManifestPartialInstall/Update steps
[platform/core/appfw/wgt-backend.git] / src / wgt / step / filesystem / step_wgt_patch_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 "wgt/step/filesystem/step_wgt_patch_icons.h"
6
7 #include <pkgmgr-info.h>
8
9 #include "common/utils/file_util.h"
10 #include "common/utils/glist_range.h"
11
12 namespace bf = boost::filesystem;
13 namespace bs = boost::system;
14 namespace ci = common_installer;
15
16 namespace {
17
18 const char kDefaultIconPath[] = "/usr/share/wgt-backend/default.png";
19
20 bool PatchIcon(icon_x* icon, const bf::path& dst_path, bool make_copy) {
21   if (!icon)
22     return false;
23   bs::error_code error;
24   bf::path icon_text(icon->text);
25   bf::path icon_path = dst_path;
26   if (strcmp(icon->lang, DEFAULT_LOCALE)) {
27     icon_path += ".";
28     icon_path += icon->lang;
29   }
30   if (icon_text.has_extension())
31     icon_path += icon_text.extension();
32   else
33     icon_path += ".png";
34   if (make_copy) {
35     bf::copy_file(icon->text, icon_path,
36         bf::copy_option::overwrite_if_exists, error);
37     if (error) {
38       LOG(ERROR) << "Failed to move icon from " << icon->text << " to "
39                  << icon_path;
40       return false;
41     }
42   } else {
43     if (!bf::exists(icon_path)) {
44       LOG(ERROR) << "Can't find icon in " << icon_path;
45       return false;
46     }
47   }
48   if (icon->text)
49     free(const_cast<char*>(icon->text));
50   icon->text = strdup(icon_path.c_str());
51
52   return true;
53 }
54
55 bool GenerateDefaultIcon(const bf::path& icon_path,
56     application_x* app, bool make_copy) {
57   bs::error_code error;
58   if (make_copy) {
59     bf::copy_file(kDefaultIconPath, icon_path,
60             bf::copy_option::overwrite_if_exists, error);
61     if (error) {
62       LOG(ERROR) << "Failed to create default icon for web application";
63       return false;
64     }
65   } else {
66     if (!bf::exists(icon_path)) {
67       LOG(ERROR) << "Can't find icon in " << icon_path;
68       return false;
69     }
70   }
71   icon_x* icon = reinterpret_cast<icon_x*>(calloc(1, sizeof(icon_x)));
72   if (!icon) {
73     LOG(ERROR) << "Out of memory";
74     return false;
75   }
76   icon->text = strdup(icon_path.c_str());
77   icon->lang = strdup(DEFAULT_LOCALE);
78   app->icon = g_list_append(app->icon, icon);
79
80   return true;
81 }
82
83 }  // namespace
84
85 namespace wgt {
86 namespace filesystem {
87
88 common_installer::Step::Status StepWgtPatchIcons::process() {
89   bf::path common_icon_location = context_->GetPkgPath() / "shared" / "res";
90   bs::error_code error;
91   bf::create_directories(common_icon_location, error);
92   if (error) {
93     LOG(ERROR) << "Failed to create common icon location directory";
94     return Status::ICON_ERROR;
95   }
96   for (icon_x* icon :
97       GListRange<icon_x*>(context_->manifest_data.get()->icon)) {
98     bf::path icon_path = common_icon_location /
99         context_->manifest_data.get()->mainapp_id;
100     if (!PatchIcon(icon, icon_path, make_copy_))
101       return Status::ICON_ERROR;
102   }
103   for (application_x* app :
104       GListRange<application_x*>(context_->manifest_data.get()->application)) {
105     if (strcmp(app->type, "webapp") != 0)
106       continue;
107     if (app->icon) {
108       // edit icon->text and copy icons to common location
109       for (auto& icon : GListRange<icon_x*>(app->icon)) {
110         bf::path icon_path = common_icon_location / app->appid;
111         if (!PatchIcon(icon, icon_path, make_copy_))
112           return Status::ICON_ERROR;
113       }
114     } else {
115       LOG(INFO) << "Application provides no icon. Using Tizen default icon.";
116       // create default icon if there is no icon at all
117       bf::path icon_path = common_icon_location / app->appid;
118       icon_path += ".png";
119       if (!GenerateDefaultIcon(icon_path, app, make_copy_)) {
120         LOG(ERROR) << "Failed to create default icon for web application";
121         return Status::ICON_ERROR;
122       }
123     }
124   }
125   return Status::OK;
126 }
127
128 }  // namespace filesystem
129 }  // namespace wgt
130