Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve mime type deployment #135

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libappimage/desktop_integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(
integrator/Integrator.cpp
integrator/DesktopEntryEditError.h
integrator/DesktopEntryEditor.cpp
integrator/MimeInfoEditor.cpp
)

if(LIBAPPIMAGE_THUMBNAILER_ENABLED)
Expand Down
108 changes: 83 additions & 25 deletions src/libappimage/desktop_integration/integrator/Integrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "utils/IconHandle.h"
#include "utils/path_utils.h"
#include "DesktopEntryEditor.h"
#include "MimeInfoEditor.h"
#include "Integrator.h"
#include "constants.h"

Expand All @@ -50,6 +51,7 @@ namespace appimage {

ResourcesExtractor resourcesExtractor;
DesktopEntry desktopEntry;
std::map<std::string, appimage::desktop_integration::integrator::MimeInfoEditor> mimeInfoFiles;

Priv(const AppImage& appImage, const bf::path& xdgDataHome)
: appImage(appImage), xdgDataHome(xdgDataHome),
Expand All @@ -58,17 +60,34 @@ namespace appimage {
if (xdgDataHome.empty())
throw DesktopIntegrationError("Invalid XDG_DATA_HOME: " + xdgDataHome.string());

// Extract desktop entry, DesktopIntegrationError will be throw if missing
extractDesktopEntry();
extractMimeInfoFiles();

appImageId = hashPath(appImage.getPath());
}

/**
* Extract desktop entry, DesktopIntegrationError will be throw if missing
azubieta marked this conversation as resolved.
Show resolved Hide resolved
*/
void extractDesktopEntry() {
auto desktopEntryPath = resourcesExtractor.getDesktopEntryPath();
auto desktopEntryData = resourcesExtractor.extractText(desktopEntryPath);
try {
desktopEntry = std::move(DesktopEntry(desktopEntryData));
} catch (const DesktopEntryError& error) {
throw DesktopIntegrationError(std::string("Malformed desktop entry: ") + error.what());
}
}


appImageId = hashPath(appImage.getPath());
/**
* Extract and store mime info files to be used latter
azubieta marked this conversation as resolved.
Show resolved Hide resolved
*/
void extractMimeInfoFiles() {
std::vector<std::string> mimeInfoPaths = resourcesExtractor.getMimeTypePackagesPaths();
for (const std::string& path: mimeInfoPaths) {
azubieta marked this conversation as resolved.
Show resolved Hide resolved
std::string mimeInfoFileData = resourcesExtractor.extractText(path);
mimeInfoFiles.insert(std::make_pair(path, MimeInfoEditor(mimeInfoFileData)));
}
}

/**
Expand Down Expand Up @@ -160,6 +179,11 @@ namespace appimage {
* Icons at usr/share/icons will be preferred if not available the ".DirIcon" will be used.
*/
void deployIcons() {
deployApplicationIcon();
deployMimeTypeIcons();
}

void deployApplicationIcon() const {
static const std::string dirIconPath = ".DirIcon";
static const auto iconsDirPath = "usr/share/icons";

Expand All @@ -177,7 +201,7 @@ namespace appimage {
try {
Logger::warning("Using .DirIcon as default app icon");
auto dirIconData = resourcesExtractor.extract(dirIconPath);
deployApplicationIcon(desktopEntryIconName, dirIconData);;
deployIcon("apps", desktopEntryIconName, dirIconData);;
TheAssassin marked this conversation as resolved.
Show resolved Hide resolved
} catch (const PayloadIteratorError& error) {
Logger::error(error.what());
Logger::error("No icon was generated for: " + appImage.getPath());
Expand All @@ -192,17 +216,40 @@ namespace appimage {
}
}

void deployMimeTypeIcons() {
std::list<std::string> mimeTypeIconNames;
for (const auto& editor : mimeInfoFiles) {
TheAssassin marked this conversation as resolved.
Show resolved Hide resolved
auto names = editor.second.getMimeTypeIconNames();
mimeTypeIconNames.merge(names);
}

std::vector<std::string> mimeTypeIconPaths;
for (const auto& iconName: mimeTypeIconNames) {
auto paths = resourcesExtractor.getIconFilePaths(iconName);
mimeTypeIconPaths.insert(mimeTypeIconPaths.end(), paths.begin(), paths.end());
}

// Generate deploy paths
std::map<std::string, std::string> mimeTypeIconsTargetPaths;
for (const auto& path: mimeTypeIconPaths)
mimeTypeIconsTargetPaths[path] = generateDeployPath(path).string();

resourcesExtractor.extractTo(mimeTypeIconsTargetPaths);
}

/**
* Deploy <iconData> as the main application icon to
* XDG_DATA_HOME/icons/hicolor/<size>/apps/<vendorPrefix>_<appImageId>_<iconName>.<format extension>
* XDG_DATA_HOME/icons/hicolor/<size>/<iconGroup>/<vendorPrefix>_<appImageId>_<iconName>.<format extension>
*
* size: actual icon dimenzions, in case of vectorial image "scalable" is used
* size: actual icon dimensions, in case of vectorial image "scalable" is used
azubieta marked this conversation as resolved.
Show resolved Hide resolved
* format extension: in case of vectorial image "svg" otherwise "png"
*
* @param iconGroup
* @param iconName
* @param iconData
*/
void deployApplicationIcon(const std::string& iconName, std::vector<char>& iconData) const {
void deployIcon(const std::string& iconGroup, const std::string& iconName,
std::vector<char>& iconData) const {
try {
IconHandle icon(iconData);

Expand All @@ -224,30 +271,41 @@ namespace appimage {
iconPath /= (iconSize + "x" + iconSize);
}

iconPath /= "apps";
iconPath /= iconGroup;
iconPath /= iconNameBuilder.str();

auto deployPath = generateDeployPath(iconPath);
icon.save(deployPath.string(), icon.format());
} catch (const IconHandleError& er) {
Logger::error(er.what());
Logger::error("No icon was generated for: " + appImage.getPath());
} catch (const IconHandleError& error) {
Logger::error("Icon deploy failed " + iconGroup + "/" + iconName
+ " from " + appImage.getPath() + " : " + error.what());
}
}

/**
* Append vendor prefix and appImage id to the file names to identify the appImage that owns
* Prepend vendor prefix and appImage id to the file names to identify the appImage that owns
* this file. Replace the default XDG_DATA_DIR by the one at <xdgDataHome>
*
* @param path resource path
* @return path with a prefixed file name
* @return <xdg data home>/<relative path>/<vendor prefix>_<appImageId>_<file name>.<extension>
azubieta marked this conversation as resolved.
Show resolved Hide resolved
*/
bf::path generateDeployPath(bf::path path) const {
// add appImage resource identification prefix to the filename
std::stringstream fileNameBuilder;
fileNameBuilder << VENDOR_PREFIX << "_" << appImageId << "_" << path.filename().string();

// build the relative parent path ignoring the default XDG_DATA_DIR prefix ("usr/share")
boost::filesystem::path relativeParentPath = generateDeployParentPath(path);
azubieta marked this conversation as resolved.
Show resolved Hide resolved

bf::path newPath = xdgDataHome / relativeParentPath / fileNameBuilder.str();
return newPath;
}

/**
* Build the relative parent path ignoring the default XDG_DATA_DIR prefix ("usr/share")
* @param path
* @return
*/
boost::filesystem::path generateDeployParentPath(boost::filesystem::path& path) const {
path.remove_filename();
bf::path relativeParentPath;
const bf::path defaultXdgDataDirPath = "usr/share";
Expand All @@ -258,20 +316,20 @@ namespace appimage {
if (relativeParentPath == defaultXdgDataDirPath)
relativeParentPath.clear();
}

bf::path newPath = xdgDataHome / relativeParentPath / fileNameBuilder.str();
return newPath;
return relativeParentPath;
azubieta marked this conversation as resolved.
Show resolved Hide resolved
}

void deployMimeTypePackages() {
auto mimeTypePackagesPaths = resourcesExtractor.getMimeTypePackagesPaths();
std::map<std::string, std::string> mimeTypePackagesTargetPaths;

// Generate deploy paths
for (const auto& path: mimeTypePackagesPaths)
mimeTypePackagesTargetPaths[path] = generateDeployPath(path).string();

resourcesExtractor.extractTo(mimeTypePackagesTargetPaths);
for (auto& itr: mimeInfoFiles) {
MimeInfoEditor& editor = itr.second;
editor.setDeployId(VENDOR_PREFIX + '_' + appImageId);
boost::filesystem::path deployPath = generateDeployPath(itr.first);
azubieta marked this conversation as resolved.
Show resolved Hide resolved

create_directories(deployPath.parent_path());
std::ofstream out(deployPath.string());
out << editor.edit();
azubieta marked this conversation as resolved.
Show resolved Hide resolved
out.close();
}
}

void setExecutionPermission() {
Expand Down
92 changes: 92 additions & 0 deletions src/libappimage/desktop_integration/integrator/MimeInfoEditor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// libraries
#include <boost/property_tree/xml_parser.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <appimage_handler.h>

// local
#include "utils/Logger.h"
#include "MimeInfoEditor.h"

namespace appimage {
namespace desktop_integration {
namespace integrator {
MimeInfoEditor::MimeInfoEditor(std::string data) {
try {
std::stringstream in(data);
using boost::property_tree::ptree;

// populate tree structure pt
read_xml(in, pt);
} catch (const std::runtime_error& error) {
appimage::utils::Logger::warning(std::string("Unable to read MimeInfo: ") + error.what());
}
}

std::string MimeInfoEditor::edit() {
try {
using boost::property_tree::ptree;

// traverse pt
for (auto& node: pt.get_child("mime-info")) {
if (node.first == "mime-type") {
auto& subTree = node.second;
// get original icon name from the icon entry
std::string originalName = subTree.get<std::string>("icon.<xmlattr>.name", "");

// or fallback to the mime-type name
if (originalName.empty()) {
originalName = subTree.get<std::string>("<xmlattr>.type");
boost::replace_all(originalName, "/", "-");
}

std::string newIconName = deployId + '_' + originalName;

subTree.put("icon.<xmlattr>.name", newIconName);
}
}

std::stringstream out;
write_xml(out, pt);
return out.str();
} catch (const std::runtime_error& error) {
appimage::utils::Logger::warning(std::string("Unable to edit MimeInfo: ") + error.what());
return std::string{};
}
}

void MimeInfoEditor::setDeployId(const std::string& deployId) {
MimeInfoEditor::deployId = deployId;
}

std::list<std::string> MimeInfoEditor::getMimeTypeIconNames() const {
std::list<std::string> icons;

try {
using boost::property_tree::ptree;

// traverse pt
for (auto& node: pt.get_child("mime-info")) {
if (node.first == "mime-type") {
auto& subTree = node.second;
// get original icon name from the icon entry
std::string iconName = subTree.get<std::string>("icon.<xmlattr>.name", "");

// or fallback to the mime-type name
if (iconName.empty()) {
iconName = subTree.get<std::string>("<xmlattr>.type");
boost::replace_all(iconName, "/", "-");
}

icons.push_back(iconName);
}
}
} catch (const std::runtime_error& error) {
appimage::utils::Logger::warning(std::string("Unable to read MimeInfo: ") + error.what());
}

return icons;
}
}
}
}
49 changes: 49 additions & 0 deletions src/libappimage/desktop_integration/integrator/MimeInfoEditor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

// system
#include <list>
#include <string>

// libraries
#include <boost/property_tree/ptree.hpp>

namespace appimage {
namespace desktop_integration {
namespace integrator {
/**
* @brief Edit MimeInfo files to be deployed
*/
class MimeInfoEditor {
public:
/**
* Create an editor instance to modify the
* @param data
* @param deployId
*/
MimeInfoEditor(std::string data);

/**
* Set the deploy id to be prepended to the icon file name
* @param deployId
*/
void setDeployId(const std::string& deployId);

/**
* Apply modification to the MimeInfo file
* @return modified MimeInfo
*/
std::string edit();

/**
* Extract the icon names from from the icon entry or the mime type name
* @return names of the icons related to the mime types
*/
std::list<std::string> getMimeTypeIconNames() const;

private:
boost::property_tree::ptree pt;
std::string deployId;
};
}
}
}
11 changes: 7 additions & 4 deletions src/libappimage/utils/resources_extractor/ResourcesExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,27 @@ namespace appimage {
public:
explicit Priv(const AppImage& appImage) : appImage(appImage), entriesCache(appImage) {}


core::AppImage appImage;

PayloadEntriesCache entriesCache;

bool isFile(const std::string& fileName) const {
return appimage::core::PayloadEntryType::REGULAR == entriesCache.getEntryType(fileName) ||
appimage::core::PayloadEntryType::LINK == entriesCache.getEntryType(fileName);
}

bool isIconFile(const std::string& fileName) const {
return (fileName.find("usr/share/icons") != std::string::npos);
return (fileName.find("usr/share/icons") != std::string::npos) && isFile(fileName);
}

bool isMainDesktopFile(const std::string& fileName) const {
return fileName.find(".desktop") != std::string::npos &&
fileName.find('/') == std::string::npos;
fileName.find('/') == std::string::npos && isFile(fileName);
}

bool isMimeFile(const std::string& fileName) const {
return fileName.find("usr/share/mime/packages") != std::string::npos &&
fileName.find(".xml") == std::string::npos;
fileName.find(".xml") != std::string::npos && isFile(fileName);
}

std::vector<char> readDataFile(std::istream& istream) const {
Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ if(BUILD_TESTING)
# global definitions
add_definitions(
-DTEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data/"
-DNEW_TEST_DATA_DIR="${CMAKE_CURRENT_BINARY_DIR}/data/"
-DGIT_COMMIT="AppImageKit unit tests"
)


add_subdirectory(data)
add_subdirectory(libappimage)

if(ENABLE_COVERAGE)
Expand Down
Loading