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.
5 #include "wgt/step/filesystem/step_wgt_patch_icons.h"
7 #include <pkgmgr-info.h>
9 #include "common/utils/file_util.h"
10 #include "common/utils/glist_range.h"
12 namespace bf = boost::filesystem;
13 namespace bs = boost::system;
14 namespace ci = common_installer;
18 const char kDefaultIconPath[] = "/usr/share/wgt-backend/default.png";
20 bool PatchIcon(icon_x* icon, const bf::path& dst_path, bool make_copy) {
24 bf::path icon_text(icon->text);
25 bf::path icon_path = dst_path;
26 if (strcmp(icon->lang, DEFAULT_LOCALE)) {
28 icon_path += icon->lang;
30 if (icon_text.has_extension())
31 icon_path += icon_text.extension();
35 bf::copy_file(icon->text, icon_path,
36 bf::copy_option::overwrite_if_exists, error);
38 LOG(ERROR) << "Failed to move icon from " << icon->text << " to "
43 if (!bf::exists(icon_path)) {
44 LOG(ERROR) << "Can't find icon in " << icon_path;
49 free(const_cast<char*>(icon->text));
50 icon->text = strdup(icon_path.c_str());
55 bool GenerateDefaultIcon(const bf::path& icon_path,
56 application_x* app, bool make_copy) {
59 bf::copy_file(kDefaultIconPath, icon_path,
60 bf::copy_option::overwrite_if_exists, error);
62 LOG(ERROR) << "Failed to create default icon for web application";
66 if (!bf::exists(icon_path)) {
67 LOG(ERROR) << "Can't find icon in " << icon_path;
71 icon_x* icon = reinterpret_cast<icon_x*>(calloc(1, sizeof(icon_x)));
73 LOG(ERROR) << "Out of memory";
76 icon->text = strdup(icon_path.c_str());
77 icon->lang = strdup(DEFAULT_LOCALE);
78 app->icon = g_list_append(app->icon, icon);
86 namespace filesystem {
88 common_installer::Step::Status StepWgtPatchIcons::process() {
89 bf::path common_icon_location = context_->GetPkgPath() / "shared" / "res";
91 bf::create_directories(common_icon_location, error);
93 LOG(ERROR) << "Failed to create common icon location directory";
94 return Status::ICON_ERROR;
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;
103 for (application_x* app :
104 GListRange<application_x*>(context_->manifest_data.get()->application)) {
105 if (strcmp(app->type, "webapp") != 0)
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;
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;
119 if (!GenerateDefaultIcon(icon_path, app, make_copy_)) {
120 LOG(ERROR) << "Failed to create default icon for web application";
121 return Status::ICON_ERROR;
128 } // namespace filesystem