Skip to content

Commit

Permalink
fix: not resolving flatpak application icons
Browse files Browse the repository at this point in the history
  • Loading branch information
body20002 authored and JakeStanger committed Jun 29, 2023
1 parent 6db7742 commit f78c7f9
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 51 deletions.
28 changes: 23 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ ipc = ["dep:serde_json"]

http = ["dep:reqwest"]

"config+all" = ["config+json", "config+yaml", "config+toml", "config+corn", "config+ron"]
"config+all" = [
"config+json",
"config+yaml",
"config+toml",
"config+corn",
"config+ron",
]
"config+json" = ["universal-config/json"]
"config+yaml" = ["universal-config/yaml"]
"config+toml" = ["universal-config/toml"]
Expand Down Expand Up @@ -58,7 +64,15 @@ workspaces = ["futures-util"]
gtk = "0.17.0"
gtk-layer-shell = "0.6.0"
glib = "0.17.10"
tokio = { version = "1.28.2", features = ["macros", "rt-multi-thread", "time", "process", "sync", "io-util", "net"] }
tokio = { version = "1.28.2", features = [
"macros",
"rt-multi-thread",
"time",
"process",
"sync",
"io-util",
"net",
] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
tracing-error = "0.2.0"
Expand All @@ -73,7 +87,9 @@ notify = { version = "6.0.1", default-features = false }
wayland-client = "0.30.2"
wayland-protocols = { version = "0.30.0", features = ["unstable", "client"] }
wayland-protocols-wlr = { version = "0.1.0", features = ["client"] }
smithay-client-toolkit = { version = "0.17.0", default-features = false, features = ["calloop"] }
smithay-client-toolkit = { version = "0.17.0", default-features = false, features = [
"calloop",
] }
universal-config = { version = "0.4.0", default_features = false }
ctrlc = "3.4.0"

Expand Down Expand Up @@ -117,7 +133,9 @@ hyprland = { version = "=0.3.1", optional = true }
futures-util = { version = "0.3.21", optional = true }

# shared
regex = { version = "1.8.4", default-features = false, features = ["std"], optional = true } # music, sys_info
regex = { version = "1.8.4", default-features = false, features = [
"std",
], optional = true } # music, sys_info

[patch.crates-io]
stray = { git = "https://github.com/jakestanger/stray", branch = "fix/connection-errors" }
stray = { git = "https://github.com/jakestanger/stray", branch = "fix/connection-errors" }
177 changes: 131 additions & 46 deletions src/desktop_file.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
use std::collections::HashMap;
use std::fs::File;
use std::io;
use std::io::BufRead;
use std::path::PathBuf;
use walkdir::WalkDir;

/// Gets directories that should contain `.desktop` files
use lazy_static::lazy_static;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use tracing::warn;
use walkdir::{DirEntry, WalkDir};

use crate::lock;

type DesktopFile = HashMap<String, Vec<String>>;

lazy_static! {
static ref DESKTOP_FILES: Mutex<HashMap<PathBuf, DesktopFile>> =
Mutex::new(HashMap::new());

/// These are the keys that in the cache
static ref DESKTOP_FILES_LOOK_OUT_KEYS: HashSet<&'static str> =
HashSet::from(["Name", "StartupWMClass", "Exec", "Icon"]);
}

/// Finds directories that should contain `.desktop` files
/// and exist on the filesystem.
fn find_application_dirs() -> Vec<PathBuf> {
let mut dirs = vec![PathBuf::from("/usr/share/applications")];
let user_dir = dirs::data_local_dir();
let mut dirs = vec![
PathBuf::from("/usr/share/applications"), // system installed apps
PathBuf::from("/var/lib/flatpak/exports/share/applications"), // flatpak apps
];

let user_dir = dirs::data_local_dir(); // user installed apps
if let Some(mut user_dir) = user_dir {
user_dir.push("applications");
dirs.push(user_dir);
Expand All @@ -19,55 +36,123 @@ fn find_application_dirs() -> Vec<PathBuf> {
dirs.into_iter().filter(|dir| dir.exists()).collect()
}

/// Finds all the desktop files
fn find_desktop_files() -> Vec<PathBuf> {
let dirs = find_application_dirs();
dirs.into_iter()
.flat_map(|dir| {
WalkDir::new(dir)
.max_depth(5)
.into_iter()
.filter_map(Result::ok)
.map(DirEntry::into_path)
.filter(|file| file.is_file() && file.extension().unwrap_or_default() == "desktop")
})
.collect()
}

/// Attempts to locate a `.desktop` file for an app id
/// (or app class).
///
/// A simple case-insensitive check is performed on filename == `app_id`.
pub fn find_desktop_file(app_id: &str) -> Option<PathBuf> {
let dirs = find_application_dirs();
// this is necessary to invalidate the cache
let files = find_desktop_files();

for dir in dirs {
let mut walker = WalkDir::new(dir).max_depth(5).into_iter();
if let Some(path) = find_desktop_file_by_filename(app_id, &files) {
return Some(path);
}

let entry = walker.find(|entry| {
entry.as_ref().map_or(false, |entry| {
let file_name = entry.file_name().to_string_lossy().to_lowercase();
let test_name = format!("{}.desktop", app_id.to_lowercase());
file_name == test_name
})
});
find_desktop_file_by_filedata(app_id, &files)
}

if let Some(Ok(entry)) = entry {
let path = entry.path().to_owned();
return Some(path);
}
}
/// Finds the correct desktop file using a simple condition check
fn find_desktop_file_by_filename(app_id: &str, files: &[PathBuf]) -> Option<PathBuf> {
let app_id = app_id.to_lowercase();

None
files
.iter()
.find(|file| {
let file_name: String = file
.file_name()
.expect("file name doesn't end with ...")
.to_string_lossy()
.to_lowercase();

file_name.contains(&app_id)
|| app_id
.split(&[' ', ':', '@', '.', '_'][..])
.any(|part| file_name.contains(part)) // this will attempt to find flatpak apps that are like this
// `com.company.app` or `com.app.something`
})
.map(ToOwned::to_owned)
}

/// Parses a desktop file into a flat hashmap of keys/values.
fn parse_desktop_file(path: PathBuf) -> io::Result<HashMap<String, String>> {
let file = File::open(path)?;
let lines = io::BufReader::new(file).lines();
/// Finds the correct desktop file using the keys in `DESKTOP_FILES_LOOK_OUT_KEYS`
fn find_desktop_file_by_filedata(app_id: &str, files: &[PathBuf]) -> Option<PathBuf> {
let app_id = &app_id.to_lowercase();
let mut desktop_files_cache = lock!(DESKTOP_FILES);

let mut map = HashMap::new();
files
.iter()
.filter_map(|file| {
let Some(parsed_desktop_file) = parse_desktop_file(file) else { return None };

for line in lines.flatten() {
if let Some((key, value)) = line.split_once('=') {
map.insert(key.to_string(), value.to_string());
}
}
desktop_files_cache.insert(file.clone(), parsed_desktop_file.clone());
Some((file.clone(), parsed_desktop_file))
})
.find(|(_, desktop_file)| {
desktop_file
.values()
.flatten()
.any(|value| value.to_lowercase().contains(app_id))
})
.map(|(path, _)| path)
}

/// Parses a desktop file into a hashmap of keys/vector(values).
fn parse_desktop_file(path: &Path) -> Option<DesktopFile> {
let Ok(file) = fs::read_to_string(path) else {
warn!("Couldn't Open File: {}", path.display());
return None;
};

let mut desktop_file: DesktopFile = DesktopFile::new();

file.lines()
.filter_map(|line| {
let Some((key, value)) = line.split_once('=') else { return None };

let key = key.trim();
let value = value.trim();

Ok(map)
if DESKTOP_FILES_LOOK_OUT_KEYS.contains(key) {
Some((key, value))
} else {
None
}
})
.for_each(|(key, value)| {
desktop_file
.entry(key.to_string())
.or_insert_with(Vec::new)
.push(value.to_string());
});

Some(desktop_file)
}

/// Attempts to get the icon name from the app's `.desktop` file.
pub fn get_desktop_icon_name(app_id: &str) -> Option<String> {
find_desktop_file(app_id).and_then(|file| {
let map = parse_desktop_file(file);
map.map_or(None, |map| {
map.get("Icon").map(std::string::ToString::to_string)
})
})
let Some(path) = find_desktop_file(app_id) else { return None };

let mut desktop_files_cache = lock!(DESKTOP_FILES);

let desktop_file = match desktop_files_cache.get(&path) {
Some(desktop_file) => desktop_file,
_ => desktop_files_cache
.entry(path.clone())
.or_insert_with(|| parse_desktop_file(&path).expect("desktop_file")),
};

let mut icons = desktop_file.get("Icon").into_iter().flatten();

icons.next().map(std::string::ToString::to_string)
}

0 comments on commit f78c7f9

Please sign in to comment.