-
Notifications
You must be signed in to change notification settings - Fork 35
fix icon import #198
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
Open
AnthonyLloydDotNet
wants to merge
2
commits into
AppImageCommunity:master
Choose a base branch
from
AnthonyLloydDotNet:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix icon import #198
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
|
|
||
| // local | ||
| #include <appimage/core/AppImage.h> | ||
| #include <appimage/core/PayloadEntryType.h> | ||
| #include <appimage/desktop_integration/exceptions.h> | ||
| #include <appimage/utils/ResourcesExtractor.h> | ||
| #include <constants.h> | ||
|
|
@@ -187,27 +188,114 @@ namespace appimage { | |
| throw DesktopIntegrationError("Icon field contains path"); | ||
| } | ||
|
|
||
| auto iconPaths = resourcesExtractor.getIconFilePaths(desktopEntryIconName); | ||
| // First try to find icons in the hicolor directory | ||
| std::vector<std::string> hicolorIconPaths; | ||
|
|
||
| // Try both the desktop entry icon name and the base name without prefix | ||
| std::string baseName = desktopEntryIconName; | ||
| size_t lastDot = baseName.find_last_of('.'); | ||
| if (lastDot != std::string::npos) { | ||
| baseName = baseName.substr(lastDot + 1); | ||
| } | ||
|
|
||
| // Get all potential icon paths | ||
| auto iconPaths = resourcesExtractor.getIconFilePaths(baseName + ".png"); | ||
|
|
||
| // Filter to only include hicolor icons from apps directory | ||
| for (const auto& path : iconPaths) { | ||
| if (path.find("usr/share/icons/hicolor") != std::string::npos && | ||
| path.find("/apps/") != std::string::npos) { | ||
| hicolorIconPaths.push_back(path); | ||
| } | ||
| } | ||
|
|
||
| // If the main app icon is not usr/share/icons we should deploy the .DirIcon in its place | ||
| if (iconPaths.empty()) { | ||
| Logger::warning(std::string("No icons found at \"") + iconsDirPath + "\""); | ||
| // If no hicolor icons found, try using .DirIcon | ||
| if (hicolorIconPaths.empty()) { | ||
| Logger::warning(std::string("No hicolor icons found at \"") + iconsDirPath + "/hicolor\""); | ||
|
|
||
| try { | ||
| Logger::warning("Using .DirIcon as default app icon"); | ||
| auto dirIconData = resourcesExtractor.extract(dirIconPath); | ||
| deployApplicationIcon(desktopEntryIconName, dirIconData);; | ||
| // Since .DirIcon might be a symlink, try to extract it directly | ||
| try { | ||
| // Try to extract the icon data - this will automatically follow symlinks | ||
| auto iconData = resourcesExtractor.extract(dirIconPath); | ||
| Logger::warning("Using " + dirIconPath + " as default app icon"); | ||
| deployApplicationIcon(desktopEntryIconName, iconData); | ||
| } catch (const PayloadIteratorError& error) { | ||
| Logger::error(error.what()); | ||
| Logger::error("No icon was generated for: " + appImage.getPath()); | ||
| } | ||
| } catch (const PayloadIteratorError& error) { | ||
| Logger::error(error.what()); | ||
| Logger::error("No icon was generated for: " + appImage.getPath()); | ||
| } | ||
| } else { | ||
| // Generate the target paths were the Desktop Entry icons will be deployed | ||
| std::map<std::string, std::string> iconFilesTargetPaths; | ||
| for (const auto& itr: iconPaths) | ||
| iconFilesTargetPaths[itr] = generateDeployPath(itr).string(); | ||
| return; | ||
| } | ||
|
|
||
| // Find the largest size icon | ||
| std::string largestIconPath; | ||
| int largestSize = 0; | ||
| for (const auto& path : hicolorIconPaths) { | ||
| // Extract size from path like "usr/share/icons/hicolor/512x512/apps/" | ||
| std::string sizeStr; | ||
| size_t sizePos = path.find("/hicolor/"); | ||
| if (sizePos != std::string::npos) { | ||
| sizePos = path.find('/', sizePos + 9); | ||
| if (sizePos != std::string::npos) { | ||
| size_t endPos = path.find('/', sizePos + 1); | ||
| if (endPos != std::string::npos) { | ||
| sizeStr = path.substr(sizePos + 1, endPos - sizePos - 1); | ||
| // Handle NxN format | ||
| size_t xPos = sizeStr.find('x'); | ||
| if (xPos != std::string::npos) { | ||
| try { | ||
| int size = std::stoi(sizeStr.substr(0, xPos)); | ||
| if (size > largestSize) { | ||
| largestSize = size; | ||
| largestIconPath = path; | ||
| } | ||
| } catch (...) { | ||
| // Skip invalid size format | ||
| continue; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+237
to
+264
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block of code is really hard to read and understand and does not actually validate the path structure. But given the amount of nested functionality, this also should be a separate function. |
||
|
|
||
| resourcesExtractor.extractTo(iconFilesTargetPaths); | ||
| // Deploy all hicolor icons to their corresponding directories | ||
| std::map<std::string, std::string> iconFilesTargetPaths; | ||
| for (const auto& itr: hicolorIconPaths) { | ||
| iconFilesTargetPaths[itr] = generateDeployPath(itr).string(); | ||
| } | ||
| resourcesExtractor.extractTo(iconFilesTargetPaths); | ||
|
|
||
| // Create symlink for the largest icon | ||
| if (!largestIconPath.empty()) { | ||
| std::filesystem::path targetPath = generateDeployPath(largestIconPath); | ||
|
|
||
| // Extract base name from icon name (e.g. "co.anysphere.cursor" -> "cursor") | ||
| std::string symlinkIconName = desktopEntryIconName; | ||
| size_t lastDot = symlinkIconName.find_last_of('.'); | ||
| if (lastDot != std::string::npos) { | ||
| symlinkIconName = symlinkIconName.substr(lastDot + 1); | ||
| } | ||
|
|
||
| // Create the symlink path using the desktop entry icon name | ||
| std::filesystem::path symlinkPath = xdgDataHome / "icons/hicolor" / | ||
| (std::to_string(largestSize) + "x" + std::to_string(largestSize)) / | ||
| "apps" / (VENDOR_PREFIX + "_" + appImageId + "_" + symlinkIconName + ".png"); | ||
|
|
||
| // Create parent directories if they don't exist | ||
| std::filesystem::create_directories(symlinkPath.parent_path()); | ||
|
|
||
| // Remove existing symlink if it exists | ||
| if (std::filesystem::exists(symlinkPath)) { | ||
| std::filesystem::remove(symlinkPath); | ||
| } | ||
|
|
||
| // Create the symlink | ||
| std::filesystem::create_symlink(targetPath, symlinkPath); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -231,10 +319,17 @@ namespace appimage { | |
|
|
||
| std::stringstream iconNameBuilder; | ||
|
|
||
| // Extract base name from icon name (e.g. "co.anysphere.cursor" -> "cursor") | ||
| std::string baseIconName = iconName; | ||
| size_t lastDot = baseIconName.find_last_of('.'); | ||
| if (lastDot != std::string::npos) { | ||
| baseIconName = baseIconName.substr(lastDot + 1); | ||
| } | ||
|
|
||
| // we don't trust the icon name inside the desktop file, so we sanitize the filename before | ||
| // calculating the integrated icon's path | ||
| // this keeps the filename understandable while mitigating risks for potential attacks | ||
| iconNameBuilder << StringSanitizer(iconName).sanitizeForPath(); | ||
| iconNameBuilder << StringSanitizer(baseIconName).sanitizeForPath(); | ||
|
|
||
| // in case of vectorial images use ".svg" as extension and "scalable" as size | ||
| if (icon.format() == "svg") { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This bit is duplicated at least three times. It should be a utility function.