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