Fix installation of icons for hybrid applications
[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 }  // namespace
21
22 namespace wgt {
23 namespace filesystem {
24
25 common_installer::Step::Status StepWgtPatchIcons::process() {
26   bf::path common_icon_location = context_->pkg_path.get() / "shared" / "res";
27   bs::error_code error;
28   bf::create_directories(common_icon_location, error);
29   for (application_x* app :
30       GListRange<application_x*>(context_->manifest_data.get()->application)) {
31     if (strcmp(app->type, "webapp") != 0)
32       continue;
33     if (app->icon) {
34       // edit icon->text and copy icons to common location
35       for (auto& icon : GListRange<icon_x*>(app->icon)) {
36         bf::path icon_text(icon->text);
37         bf::path icon_path = common_icon_location / app->appid;
38         if (strcmp(icon->lang, DEFAULT_LOCALE)) {
39           icon_path += ".";
40           icon_path += icon->lang;
41         }
42         if (icon_text.has_extension())
43           icon_path += icon_text.extension();
44         else
45           icon_path += ".png";
46
47         bf::copy_file(icon->text, icon_path,
48                       bf::copy_option::overwrite_if_exists, error);
49         if (error) {
50           LOG(ERROR) << "Failed to move icon from " << icon->text << " to "
51                      << icon_path;
52           return Status::ICON_ERROR;
53         }
54         if (icon->text)
55           free(const_cast<char*>(icon->text));
56         icon->text = strdup(icon_path.c_str());
57       }
58     } else {
59       LOG(INFO) << "Application provides no icon. Using Tizen default icon.";
60       // create default icon if there is no icon at all
61       bf::path icon_path = common_icon_location / app->appid;
62       icon_path += ".png";
63       bf::copy_file(kDefaultIconPath, icon_path, error);
64       if (error) {
65         LOG(ERROR) << "Failed to create default icon for web application";
66         return Status::ICON_ERROR;
67       }
68       icon_x* icon = reinterpret_cast<icon_x*>(calloc(1, sizeof(icon_x)));
69       icon->text = strdup(icon_path.c_str());
70       icon->lang = strdup(DEFAULT_LOCALE);
71       app->icon = g_list_append(app->icon, icon);
72     }
73   }
74   return Status::OK;
75 }
76
77 }  // namespace filesystem
78 }  // namespace wgt
79