b80263a2df8533a7b5e8461190fd9bcfa8ccd4f8
[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) {
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
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   if (icon->text)
43     free(const_cast<char*>(icon->text));
44   icon->text = strdup(icon_path.c_str());
45
46   return true;
47 }
48
49 }  // namespace
50
51 namespace wgt {
52 namespace filesystem {
53
54 common_installer::Step::Status StepWgtPatchIcons::process() {
55   bf::path common_icon_location = context_->GetPkgPath() / "shared" / "res";
56   bs::error_code error;
57   bf::create_directories(common_icon_location, error);
58   if (error) {
59     LOG(ERROR) << "Failed to create common icon location directory";
60     return Status::ICON_ERROR;
61   }
62   for (icon_x* icon :
63       GListRange<icon_x*>(context_->manifest_data.get()->icon)) {
64     bf::path icon_path = common_icon_location /
65         context_->manifest_data.get()->mainapp_id;
66     if (!PatchIcon(icon, icon_path))
67       return Status::ICON_ERROR;
68   }
69   for (application_x* app :
70       GListRange<application_x*>(context_->manifest_data.get()->application)) {
71     if (strcmp(app->type, "webapp") != 0)
72       continue;
73     if (app->icon) {
74       // edit icon->text and copy icons to common location
75       for (auto& icon : GListRange<icon_x*>(app->icon)) {
76         bf::path icon_path = common_icon_location / app->appid;
77         if (!PatchIcon(icon, icon_path))
78           return Status::ICON_ERROR;
79       }
80     } else {
81       LOG(INFO) << "Application provides no icon. Using Tizen default icon.";
82       // create default icon if there is no icon at all
83       bf::path icon_path = common_icon_location / app->appid;
84       icon_path += ".png";
85       bf::copy_file(kDefaultIconPath, icon_path,
86               bf::copy_option::overwrite_if_exists, error);
87       if (error) {
88         LOG(ERROR) << "Failed to create default icon for web application";
89         return Status::ICON_ERROR;
90       }
91       icon_x* icon = reinterpret_cast<icon_x*>(calloc(1, sizeof(icon_x)));
92       if (!icon) {
93         LOG(ERROR) << "Out of memory";
94         return Status::ICON_ERROR;
95       }
96       icon->text = strdup(icon_path.c_str());
97       icon->lang = strdup(DEFAULT_LOCALE);
98       app->icon = g_list_append(app->icon, icon);
99     }
100   }
101   return Status::OK;
102 }
103
104 }  // namespace filesystem
105 }  // namespace wgt
106