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";
32 const char kConfigFileName[] = "config.xml";
34 bool ExtractPackageArchive(const char* file_path, const char* file,
35 const bf::path& tmp_dir) {
36 if (!ci::ExtractToTmpDir(file_path, tmp_dir, file)) {
37 LOG(ERROR) << "Failed to extract";
43 bool GetPackageInfo(const wgt::parse::WidgetConfigParser& parser,
44 package_manager_pkg_detail_info_t* info) {
46 std::static_pointer_cast<const wgt::parse::WidgetInfo>(
47 parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
49 std::static_pointer_cast<const wgt::parse::TizenApplicationInfo>(
50 parser.GetManifestData(wgt::parse::TizenApplicationInfo::Key()));
51 if (!widget_info || !app_info) {
52 LOG(ERROR) << "WidgetInfo / TizenApplicationInfo not found";
56 snprintf(info->pkg_type, sizeof(info->pkg_type), "wgt");
57 snprintf(info->version, sizeof(info->version), "%s",
58 widget_info->version().c_str());
59 snprintf(info->author, sizeof(info->version), "%s",
60 widget_info->author().c_str());
62 snprintf(info->pkg_name, sizeof(info->pkg_name), "%s",
63 app_info->package().c_str());
64 snprintf(info->pkgid, sizeof(info->pkgid), "%s", app_info->package().c_str());
65 snprintf(info->api_version, sizeof(info->api_version), "%s",
66 app_info->required_version().c_str());
71 bool GetPrivilegesInfo(const wgt::parse::WidgetConfigParser& parser,
72 package_manager_pkg_detail_info_t* info) {
73 auto privileges_info =
74 std::static_pointer_cast<const wgt::parse::PermissionsInfo>(
75 parser.GetManifestData(wgt::parse::PermissionsInfo::Key()));
79 const auto& privileges = privileges_info->GetAPIPermissions();
80 for (auto& priv : privileges) {
81 info->privilege_list = g_list_append(info->privilege_list,
82 strdup(priv.c_str()));
88 bool GetLabelInfo(const wgt::parse::WidgetConfigParser& parser,
89 const char* locale, package_manager_pkg_detail_info_t* info) {
91 std::static_pointer_cast<const wgt::parse::WidgetInfo>(
92 parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
97 const auto& labels = widget_info->name_set();
98 if (labels.find(locale) != labels.end()) {
99 name = labels.find(locale)->second;
100 snprintf(info->label, sizeof(info->label), "%s", name.c_str());
102 } else if (labels.find("") != labels.end()) {
103 name = labels.find("")->second;
104 snprintf(info->label, sizeof(info->label), "%s", name.c_str());
111 bool GetDescriptionInfo(const wgt::parse::WidgetConfigParser& parser,
112 const char* locale, package_manager_pkg_detail_info_t* info) {
114 std::static_pointer_cast<const wgt::parse::WidgetInfo>(
115 parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
120 const auto& descriptions = widget_info->description_set();
121 if (descriptions.find(locale) != descriptions.end()) {
122 desc = descriptions.find(locale)->second;
123 snprintf(info->pkg_description, sizeof(info->pkg_description), "%s",
126 } else if (descriptions.find("") != descriptions.end()) {
127 desc = descriptions.find("")->second;
128 snprintf(info->pkg_description, sizeof(info->pkg_description), "%s",
136 bool ReadIcon(const bf::path& icon, const bf::path& tmp_dir,
137 package_manager_pkg_detail_info_t* info) {
138 bf::path icon_path = tmp_dir / icon;
140 LOG(INFO) << "Icon file path: " << icon_path;
142 std::ifstream ifs(icon_path.c_str(),
143 std::ifstream::in | std::ifstream::binary);
144 ifs.seekg(0, ifs.end);
145 int len = ifs.tellg();
146 ifs.seekg(0, ifs.beg);
151 char* buf = static_cast<char*>(malloc(sizeof(char) * len));
153 LOG(INFO) << "Reading icon file, " << len << " bytes";
156 info->icon_buf = buf;
157 info->icon_size = len;
162 std::string GetIconInfo(const wgt::parse::WidgetConfigParser& parser) {
164 std::static_pointer_cast<const wgt::parse::ApplicationIconsInfo>(
165 parser.GetManifestData(wgt::parse::ApplicationIconsInfo::Key()));
169 return std::string(icons_info->icons().front().path());
174 bool WgtArchiveInfo::GetArchiveInfo(const char* file_path,
175 package_manager_pkg_detail_info_t* info) {
176 bf::path tmp_dir = ci::GenerateTmpDir("/tmp");
177 if (!ci::CreateDir(tmp_dir))
179 LOG(DEBUG) << "Unpack at temporary dir: " << tmp_dir;
180 if (!ExtractPackageArchive(file_path, kConfigFileName, tmp_dir))
183 wgt::parse::WidgetConfigParser parser;
184 bf::path manifest_path = tmp_dir / kConfigFileName;
185 if (!parser.ParseManifest(manifest_path)) {
186 LOG(ERROR) << "Failed to parse";
187 bf::remove_all(tmp_dir);
191 if (!GetPackageInfo(parser, info)) {
192 LOG(ERROR) << "Failed to get package info";
193 bf::remove_all(tmp_dir);
196 if (!GetPrivilegesInfo(parser, info))
197 LOG(WARNING) << "Failed to get privileges info";
198 std::string icon = GetIconInfo(parser);
200 if (!ExtractPackageArchive(file_path, icon.c_str(), tmp_dir))
202 if (!ReadIcon(icon, tmp_dir, info)) {
203 LOG(WARNING) << "Failed to get icon info";
208 char* locale = vconf_get_str(kVconfLanguageKey);
209 if (!GetLabelInfo(parser, locale, info))
210 LOG(WARNING) << "Failed to get label info";
211 if (!GetDescriptionInfo(parser, locale, info))
212 LOG(WARNING) << "Failed to get description info";
215 bf::remove_all(tmp_dir);