aeed341c8ecc9c9badd42e3e4622b073b34bf7ad
[platform/core/appfw/wgt-backend.git] / src / lib / wgt_archive_info.cc
1 // Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "lib/wgt_archive_info.h"
6
7 #include <package-manager-plugin.h>
8 #include <pkgmgr-info.h>
9 #include <vconf.h>
10
11 #include <wgt_manifest_handlers/widget_config_parser.h>
12 #include <wgt_manifest_handlers/application_icons_handler.h>
13 #include <wgt_manifest_handlers/application_manifest_constants.h>
14 #include <wgt_manifest_handlers/permissions_handler.h>
15 #include <wgt_manifest_handlers/tizen_application_handler.h>
16 #include <wgt_manifest_handlers/widget_handler.h>
17
18 #include <common/utils/file_util.h>
19
20 #include <cstdio>
21 #include <cstdlib>
22 #include <fstream>
23 #include <string>
24 #include <vector>
25
26 namespace bf = boost::filesystem;
27 namespace ci = common_installer;
28
29 namespace {
30
31 const char kVconfLanguageKey[] = "db/menu_widget/language";
32
33 bool ExtractPackageArchive(const char* file_path, const bf::path& tmp_dir) {
34   if (!ci::ExtractToTmpDir(file_path, tmp_dir)) {
35     LOG(ERROR) << "Failed to extract";
36     return false;
37   }
38   return true;
39 }
40
41 bool GetPackageInfo(const wgt::parse::WidgetConfigParser& parser,
42     package_manager_pkg_detail_info_t* info) {
43   auto widget_info =
44       std::static_pointer_cast<const wgt::parse::WidgetInfo>(
45           parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
46   auto app_info =
47       std::static_pointer_cast<const wgt::parse::TizenApplicationInfo>(
48           parser.GetManifestData(wgt::parse::TizenApplicationInfo::Key()));
49   if (!widget_info || !app_info) {
50     LOG(ERROR) << "WidgetInfo / TizenApplicationInfo not found";
51     return false;
52   }
53
54   snprintf(info->pkg_type, sizeof(info->pkg_type), "wgt");
55   snprintf(info->version, sizeof(info->version), "%s",
56       widget_info->version().c_str());
57   snprintf(info->author, sizeof(info->version), "%s",
58       widget_info->author().c_str());
59
60   snprintf(info->pkg_name, sizeof(info->pkg_name), "%s",
61       app_info->package().c_str());
62   snprintf(info->pkgid, sizeof(info->pkgid), "%s", app_info->package().c_str());
63   snprintf(info->api_version, sizeof(info->api_version), "%s",
64       app_info->required_version().c_str());
65
66   return true;
67 }
68
69 bool GetPrivilegesInfo(const wgt::parse::WidgetConfigParser& parser,
70     package_manager_pkg_detail_info_t* info) {
71   auto privileges_info =
72       std::static_pointer_cast<const wgt::parse::PermissionsInfo>(
73           parser.GetManifestData(wgt::parse::PermissionsInfo::Key()));
74   if (!privileges_info)
75     return false;
76
77   const auto& privileges = privileges_info->GetAPIPermissions();
78   for (auto& priv : privileges) {
79     info->privilege_list = g_list_append(info->privilege_list,
80         strdup(priv.c_str()));
81   }
82
83   return true;
84 }
85
86 bool GetLabelInfo(const wgt::parse::WidgetConfigParser& parser,
87     const char* locale, package_manager_pkg_detail_info_t* info) {
88   auto widget_info =
89       std::static_pointer_cast<const wgt::parse::WidgetInfo>(
90           parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
91   if (!widget_info)
92     return false;
93
94   std::string name;
95   const auto& labels = widget_info->name_set();
96   if (labels.find(locale) != labels.end()) {
97     name = labels.find(locale)->second;
98     snprintf(info->label, sizeof(info->label), "%s", name.c_str());
99     return true;
100   } else if (labels.find("") != labels.end()) {
101     name = labels.find("")->second;
102     snprintf(info->label, sizeof(info->label), "%s", name.c_str());
103     return true;
104   }
105
106   return false;
107 }
108
109 bool GetDescriptionInfo(const wgt::parse::WidgetConfigParser& parser,
110     const char* locale, package_manager_pkg_detail_info_t* info) {
111   auto widget_info =
112       std::static_pointer_cast<const wgt::parse::WidgetInfo>(
113           parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
114   if (!widget_info)
115     return false;
116
117   std::string desc;
118   const auto& descriptions = widget_info->description_set();
119   if (descriptions.find(locale) != descriptions.end()) {
120     desc = descriptions.find(locale)->second;
121     snprintf(info->pkg_description, sizeof(info->pkg_description), "%s",
122         desc.c_str());
123     return true;
124   } else if (descriptions.find("") != descriptions.end()) {
125     desc = descriptions.find("")->second;
126     snprintf(info->pkg_description, sizeof(info->pkg_description), "%s",
127         desc.c_str());
128     return true;
129   }
130
131   return false;
132 }
133
134 bool ReadIcon(const bf::path& icon, const bf::path& tmp_dir,
135     package_manager_pkg_detail_info_t* info) {
136   bf::path icon_path;
137   if (icon.is_absolute())
138     icon_path = icon;
139   else
140     icon_path = tmp_dir / icon;
141
142   LOG(INFO) << "Icon file path: " << icon_path;
143
144   std::ifstream ifs(icon_path.c_str(),
145       std::ifstream::in | std::ifstream::binary);
146   ifs.seekg(0, ifs.end);
147   int len = ifs.tellg();
148   ifs.seekg(0, ifs.beg);
149
150   if (len <= 0)
151     return false;
152
153   char* buf = static_cast<char*>(malloc(sizeof(char) * len));
154
155   LOG(INFO) << "Reading icon file, " << len << " bytes";
156   ifs.read(buf, len);
157
158   info->icon_buf = buf;
159   info->icon_size = len;
160
161   return true;
162 }
163
164 bool GetIconInfo(const wgt::parse::WidgetConfigParser& parser,
165     const bf::path& tmp_dir, package_manager_pkg_detail_info_t* info) {
166   auto icons_info =
167       std::static_pointer_cast<const wgt::parse::ApplicationIconsInfo>(
168           parser.GetManifestData(wgt::parse::ApplicationIconsInfo::Key()));
169   if (!icons_info)
170     return false;
171
172   return ReadIcon(icons_info->icons().front().path(), tmp_dir, info);
173 }
174
175 }  // namespace
176
177 bool WgtArchiveInfo::GetArchiveInfo(const char* file_path,
178     package_manager_pkg_detail_info_t* info) {
179   bf::path tmp_dir = ci::GenerateTmpDir("/tmp");
180   if (!ci::CreateDir(tmp_dir))
181     return false;
182   LOG(DEBUG) << "Unpack at temporary dir: " << tmp_dir;
183   if (!ExtractPackageArchive(file_path, tmp_dir))
184     return false;
185
186   wgt::parse::WidgetConfigParser parser;
187   bf::path manifest_path = tmp_dir / "config.xml";
188   if (!parser.ParseManifest(manifest_path)) {
189     LOG(ERROR) << "Failed to parse";
190     bf::remove_all(tmp_dir);
191     return false;
192   }
193
194   if (!GetPackageInfo(parser, info)) {
195     LOG(ERROR) << "Failed to get package info";
196     bf::remove_all(tmp_dir);
197     return false;
198   }
199   if (!GetPrivilegesInfo(parser, info))
200     LOG(WARNING) << "Failed to get privileges info";
201   if (!GetIconInfo(parser, tmp_dir, info))
202     LOG(WARNING) << "Failed to get icon info";
203
204   char* locale = vconf_get_str(kVconfLanguageKey);
205   if (!GetLabelInfo(parser, locale, info))
206     LOG(WARNING) << "Failed to get label info";
207   if (!GetDescriptionInfo(parser, locale, info))
208     LOG(WARNING) << "Failed to get description info";
209
210   free(locale);
211   bf::remove_all(tmp_dir);
212
213   return true;
214 }