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";
33 const char kHybridConfigFileName[] = "res/wgt/config.xml";
35 bool ExtractPackageArchive(const char* file_path, const char* file,
36 const bf::path& tmp_dir) {
37 if (!ci::ExtractToTmpDir(file_path, tmp_dir, file)) {
38 LOG(ERROR) << "Failed to extract";
44 bool GetPackageInfo(const wgt::parse::WidgetConfigParser& parser,
45 package_manager_pkg_detail_info_t* info) {
47 std::static_pointer_cast<const wgt::parse::WidgetInfo>(
48 parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
50 std::static_pointer_cast<const wgt::parse::TizenApplicationInfo>(
51 parser.GetManifestData(wgt::parse::TizenApplicationInfo::Key()));
52 if (!widget_info || !app_info) {
53 LOG(ERROR) << "WidgetInfo / TizenApplicationInfo not found";
57 snprintf(info->pkg_type, sizeof(info->pkg_type), "wgt");
58 snprintf(info->version, sizeof(info->version), "%s",
59 widget_info->version().c_str());
60 snprintf(info->author, sizeof(info->version), "%s",
61 widget_info->author().c_str());
63 snprintf(info->pkg_name, sizeof(info->pkg_name), "%s",
64 app_info->package().c_str());
65 snprintf(info->pkgid, sizeof(info->pkgid), "%s", app_info->package().c_str());
66 snprintf(info->api_version, sizeof(info->api_version), "%s",
67 app_info->required_version().c_str());
72 bool GetPrivilegesInfo(const wgt::parse::WidgetConfigParser& parser,
73 package_manager_pkg_detail_info_t* info) {
74 auto privileges_info =
75 std::static_pointer_cast<const wgt::parse::PermissionsInfo>(
76 parser.GetManifestData(wgt::parse::PermissionsInfo::Key()));
80 const auto& privileges = privileges_info->GetAPIPermissions();
81 for (auto& priv : privileges) {
82 info->privilege_list = g_list_append(info->privilege_list,
83 strdup(priv.c_str()));
89 bool GetLabelInfo(const wgt::parse::WidgetConfigParser& parser,
90 const char* locale, package_manager_pkg_detail_info_t* info) {
92 std::static_pointer_cast<const wgt::parse::WidgetInfo>(
93 parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
98 const auto& labels = widget_info->name_set();
99 if (labels.find(locale) != labels.end()) {
100 name = labels.find(locale)->second;
101 snprintf(info->label, sizeof(info->label), "%s", name.c_str());
103 } else if (labels.find("") != labels.end()) {
104 name = labels.find("")->second;
105 snprintf(info->label, sizeof(info->label), "%s", name.c_str());
112 bool GetDescriptionInfo(const wgt::parse::WidgetConfigParser& parser,
113 const char* locale, package_manager_pkg_detail_info_t* info) {
115 std::static_pointer_cast<const wgt::parse::WidgetInfo>(
116 parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
121 const auto& descriptions = widget_info->description_set();
122 if (descriptions.find(locale) != descriptions.end()) {
123 desc = descriptions.find(locale)->second;
124 snprintf(info->pkg_description, sizeof(info->pkg_description), "%s",
127 } else if (descriptions.find("") != descriptions.end()) {
128 desc = descriptions.find("")->second;
129 snprintf(info->pkg_description, sizeof(info->pkg_description), "%s",
137 bool ReadIcon(const bf::path& icon, const bf::path& tmp_dir,
138 package_manager_pkg_detail_info_t* info) {
139 bf::path icon_path = tmp_dir / icon;
141 LOG(INFO) << "Icon file path: " << icon_path;
143 std::ifstream ifs(icon_path.c_str(),
144 std::ifstream::in | std::ifstream::binary);
145 ifs.seekg(0, ifs.end);
146 int len = ifs.tellg();
147 ifs.seekg(0, ifs.beg);
152 char* buf = static_cast<char*>(malloc(sizeof(char) * len));
154 LOG(INFO) << "Reading icon file, " << len << " bytes";
157 info->icon_buf = buf;
158 info->icon_size = len;
163 std::string GetIconInfo(const wgt::parse::WidgetConfigParser& parser) {
165 std::static_pointer_cast<const wgt::parse::ApplicationIconsInfo>(
166 parser.GetManifestData(wgt::parse::ApplicationIconsInfo::Key()));
170 return std::string(icons_info->icons().front().path());
175 bool WgtArchiveInfo::GetArchiveInfo(const char* file_path,
176 package_manager_pkg_detail_info_t* info) {
177 bf::path tmp_dir = ci::GenerateTmpDir("/tmp");
178 if (!ci::CreateDir(tmp_dir))
180 LOG(DEBUG) << "Unpack at temporary dir: " << tmp_dir;
181 bool is_hybrid = false;
182 if (!ExtractPackageArchive(file_path, kHybridConfigFileName, tmp_dir))
184 if (bf::exists(tmp_dir / kHybridConfigFileName)) {
187 if (!ExtractPackageArchive(file_path, kConfigFileName, tmp_dir))
191 wgt::parse::WidgetConfigParser parser;
192 bf::path manifest_path;
194 manifest_path = tmp_dir / kHybridConfigFileName;
196 manifest_path = tmp_dir / kConfigFileName;
197 if (!parser.ParseManifest(manifest_path)) {
198 LOG(ERROR) << "Failed to parse";
199 bf::remove_all(tmp_dir);
203 if (!GetPackageInfo(parser, info)) {
204 LOG(ERROR) << "Failed to get package info";
205 bf::remove_all(tmp_dir);
208 if (!GetPrivilegesInfo(parser, info))
209 LOG(WARNING) << "Failed to get privileges info";
210 std::string icon = GetIconInfo(parser);
212 std::string icon_path;
214 icon_path = "res/wgt/" + icon;
217 if (!ExtractPackageArchive(file_path, icon_path.c_str(), tmp_dir)) {
218 bf::remove_all(tmp_dir);
221 if (!ReadIcon(icon_path, tmp_dir, info)) {
222 LOG(WARNING) << "Failed to get icon info";
223 bf::remove_all(tmp_dir);
228 char* locale = vconf_get_str(kVconfLanguageKey);
229 if (!GetLabelInfo(parser, locale, info))
230 LOG(WARNING) << "Failed to get label info";
231 if (!GetDescriptionInfo(parser, locale, info))
232 LOG(WARNING) << "Failed to get description info";
235 bf::remove_all(tmp_dir);