--- /dev/null
+// Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a apache 2.0 license that can be
+// found in the LICENSE file.
+
+#include "lib/wgt_archive_info.h"
+
+#include <package-manager-plugin.h>
+#include <pkgmgr-info.h>
+#include <vconf.h>
+
+#include <wgt_manifest_handlers/widget_config_parser.h>
+#include <wgt_manifest_handlers/application_icons_handler.h>
+#include <wgt_manifest_handlers/application_manifest_constants.h>
+#include <wgt_manifest_handlers/permissions_handler.h>
+#include <wgt_manifest_handlers/tizen_application_handler.h>
+#include <wgt_manifest_handlers/widget_handler.h>
+
+#include <common/utils/file_util.h>
+
+#include <cstdio>
+#include <cstdlib>
+#include <fstream>
+#include <string>
+#include <vector>
+
+namespace bf = boost::filesystem;
+namespace ci = common_installer;
+
+namespace {
+
+const char kVconfLanguageKey[] = "db/menu_widget/language";
+
+bool ExtractPackageArchive(const char* file_path, const bf::path& tmp_dir) {
+ if (!ci::ExtractToTmpDir(file_path, tmp_dir)) {
+ LOG(ERROR) << "Failed to extract";
+ return false;
+ }
+ return true;
+}
+
+bool GetPackageInfo(wgt::parse::WidgetConfigParser& parser,
+ package_manager_pkg_detail_info_t* info) {
+ auto widget_info =
+ std::static_pointer_cast<const wgt::parse::WidgetInfo>(
+ parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
+ auto app_info =
+ std::static_pointer_cast<const wgt::parse::TizenApplicationInfo>(
+ parser.GetManifestData(wgt::parse::TizenApplicationInfo::Key()));
+ if (!widget_info || !app_info) {
+ LOG(ERROR) << "WidgetInfo / TizenApplicationInfo not found";
+ return false;
+ }
+
+ snprintf(info->pkg_type, sizeof(info->pkg_type), "wgt");
+ snprintf(info->version, sizeof(info->version), "%s",
+ widget_info->version().c_str());
+ snprintf(info->author, sizeof(info->version), "%s",
+ widget_info->author().c_str());
+
+ snprintf(info->pkg_name, sizeof(info->pkg_name), "%s",
+ app_info->package().c_str());
+ snprintf(info->pkgid, sizeof(info->pkgid), "%s", app_info->package().c_str());
+ snprintf(info->api_version, sizeof(info->api_version), "%s",
+ app_info->required_version().c_str());
+
+ return true;
+}
+
+bool GetPrivilegesInfo(wgt::parse::WidgetConfigParser& parser,
+ package_manager_pkg_detail_info_t* info) {
+ auto privileges_info =
+ std::static_pointer_cast<const wgt::parse::PermissionsInfo>(
+ parser.GetManifestData(wgt::parse::PermissionsInfo::Key()));
+ if (!privileges_info)
+ return false;
+
+ const auto& privileges = privileges_info->GetAPIPermissions();
+ for (auto& priv : privileges) {
+ info->privilege_list = g_list_append(info->privilege_list,
+ strdup(priv.c_str()));
+ }
+
+ return true;
+}
+
+bool GetLabelInfo(wgt::parse::WidgetConfigParser& parser, const char* locale,
+ package_manager_pkg_detail_info_t* info) {
+ auto widget_info =
+ std::static_pointer_cast<const wgt::parse::WidgetInfo>(
+ parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
+ if (!widget_info)
+ return false;
+
+ std::string name;
+ const auto& labels = widget_info->name_set();
+ if (labels.find(locale) != labels.end()) {
+ name = labels.find(locale)->second;
+ snprintf(info->label, sizeof(info->label), "%s", name.c_str());
+ return true;
+ } else if (labels.find("") != labels.end()) {
+ name = labels.find("")->second;
+ snprintf(info->label, sizeof(info->label), "%s", name.c_str());
+ return true;
+ }
+
+ return false;
+}
+
+bool GetDescriptionInfo(wgt::parse::WidgetConfigParser& parser,
+ const char* locale, package_manager_pkg_detail_info_t* info) {
+ auto widget_info =
+ std::static_pointer_cast<const wgt::parse::WidgetInfo>(
+ parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
+ if (!widget_info)
+ return false;
+
+ std::string desc;
+ const auto& descriptions = widget_info->description_set();
+ if (descriptions.find(locale) != descriptions.end()) {
+ desc = descriptions.find(locale)->second;
+ snprintf(info->pkg_description, sizeof(info->pkg_description), "%s",
+ desc.c_str());
+ return true;
+ } else if (descriptions.find("") != descriptions.end()) {
+ desc = descriptions.find("")->second;
+ snprintf(info->pkg_description, sizeof(info->pkg_description), "%s",
+ desc.c_str());
+ return true;
+ }
+
+ return false;
+}
+
+bool ReadIcon(const bf::path& icon, const bf::path& tmp_dir,
+ package_manager_pkg_detail_info_t* info) {
+ bf::path icon_path;
+ if (icon.is_absolute())
+ icon_path = icon;
+ else
+ icon_path = tmp_dir / icon;
+
+ LOG(INFO) << "Icon file path: " << icon_path;
+
+ std::ifstream ifs(icon_path.c_str(),
+ std::ifstream::in | std::ifstream::binary);
+ ifs.seekg(0, ifs.end);
+ int len = ifs.tellg();
+ ifs.seekg(0, ifs.beg);
+
+ if (len <= 0)
+ return false;
+
+ char* buf = static_cast<char*>(malloc(sizeof(char) * len));
+
+ LOG(INFO) << "Reading icon file, " << len << " bytes";
+ ifs.read(buf, len);
+
+ info->icon_buf = buf;
+ info->icon_size = len;
+
+ return true;
+}
+
+bool GetIconInfo(wgt::parse::WidgetConfigParser& parser,
+ bf::path& tmp_dir, package_manager_pkg_detail_info_t* info) {
+ auto icons_info =
+ std::static_pointer_cast<const wgt::parse::ApplicationIconsInfo>(
+ parser.GetManifestData(wgt::parse::ApplicationIconsInfo::Key()));
+ if (!icons_info)
+ return false;
+
+ return ReadIcon(icons_info->icons().front().path(), tmp_dir, info);
+}
+
+} // namespace
+
+bool WgtArchiveInfo::GetArchiveInfo(const char* file_path,
+ package_manager_pkg_detail_info_t* info) {
+ bf::path tmp_dir = ci::GenerateTmpDir("/tmp");
+ if (!ci::CreateDir(tmp_dir))
+ return false;
+ LOG(DEBUG) << "Unpack at temporary dir: " << tmp_dir;
+ if (!ExtractPackageArchive(file_path, tmp_dir))
+ return false;
+
+ wgt::parse::WidgetConfigParser parser;
+ bf::path manifest_path = tmp_dir / "config.xml";
+ if (!parser.ParseManifest(manifest_path)) {
+ LOG(ERROR) << "Failed to parse";
+ bf::remove_all(tmp_dir);
+ return false;
+ }
+
+ if (!GetPackageInfo(parser, info)) {
+ LOG(ERROR) << "Failed to get package info";
+ bf::remove_all(tmp_dir);
+ return false;
+ }
+ if (!GetPrivilegesInfo(parser, info))
+ LOG(WARNING) << "Failed to get privileges info";
+ if (!GetIconInfo(parser, tmp_dir, info))
+ LOG(WARNING) << "Failed to get icon info";
+
+ char* locale = vconf_get_str(kVconfLanguageKey);
+ if (!GetLabelInfo(parser, locale, info))
+ LOG(WARNING) << "Failed to get label info";
+ if (!GetDescriptionInfo(parser, locale, info))
+ LOG(WARNING) << "Failed to get description info";
+
+ free(locale);
+ bf::remove_all(tmp_dir);
+
+ return true;
+}