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.
5 #include "lib/wgt_archive_info.h"
7 #include <package-manager-plugin.h>
8 #include <pkgmgr-info.h>
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>
18 #include <common/utils/file_util.h>
26 namespace bf = boost::filesystem;
27 namespace ci = common_installer;
31 const char kVconfLanguageKey[] = "db/menu_widget/language";
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";
41 bool GetPackageInfo(const wgt::parse::WidgetConfigParser& parser,
42 package_manager_pkg_detail_info_t* info) {
44 std::static_pointer_cast<const wgt::parse::WidgetInfo>(
45 parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
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";
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());
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());
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()));
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()));
86 bool GetLabelInfo(const wgt::parse::WidgetConfigParser& parser,
87 const char* locale, package_manager_pkg_detail_info_t* info) {
89 std::static_pointer_cast<const wgt::parse::WidgetInfo>(
90 parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
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());
100 } else if (labels.find("") != labels.end()) {
101 name = labels.find("")->second;
102 snprintf(info->label, sizeof(info->label), "%s", name.c_str());
109 bool GetDescriptionInfo(const wgt::parse::WidgetConfigParser& parser,
110 const char* locale, package_manager_pkg_detail_info_t* info) {
112 std::static_pointer_cast<const wgt::parse::WidgetInfo>(
113 parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
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",
124 } else if (descriptions.find("") != descriptions.end()) {
125 desc = descriptions.find("")->second;
126 snprintf(info->pkg_description, sizeof(info->pkg_description), "%s",
134 bool ReadIcon(const bf::path& icon, const bf::path& tmp_dir,
135 package_manager_pkg_detail_info_t* info) {
137 if (icon.is_absolute())
140 icon_path = tmp_dir / icon;
142 LOG(INFO) << "Icon file path: " << icon_path;
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);
153 char* buf = static_cast<char*>(malloc(sizeof(char) * len));
155 LOG(INFO) << "Reading icon file, " << len << " bytes";
158 info->icon_buf = buf;
159 info->icon_size = len;
164 bool GetIconInfo(const wgt::parse::WidgetConfigParser& parser,
165 const bf::path& tmp_dir, package_manager_pkg_detail_info_t* info) {
167 std::static_pointer_cast<const wgt::parse::ApplicationIconsInfo>(
168 parser.GetManifestData(wgt::parse::ApplicationIconsInfo::Key()));
172 return ReadIcon(icons_info->icons().front().path(), tmp_dir, info);
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))
182 LOG(DEBUG) << "Unpack at temporary dir: " << tmp_dir;
183 if (!ExtractPackageArchive(file_path, tmp_dir))
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);
194 if (!GetPackageInfo(parser, info)) {
195 LOG(ERROR) << "Failed to get package info";
196 bf::remove_all(tmp_dir);
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";
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";
211 bf::remove_all(tmp_dir);