From f78c7f9b981c98676e855ff6a63e33a51927c709 Mon Sep 17 00:00:00 2001 From: Abdallah Gamal Date: Thu, 29 Jun 2023 21:20:10 +0300 Subject: [PATCH] fix: not resolving flatpak application icons --- Cargo.toml | 28 +++++-- src/desktop_file.rs | 177 ++++++++++++++++++++++++++++++++------------ 2 files changed, 154 insertions(+), 51 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 746e5e3a..3e10f9cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] @@ -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" @@ -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" @@ -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" } \ No newline at end of file +stray = { git = "https://github.com/jakestanger/stray", branch = "fix/connection-errors" } diff --git a/src/desktop_file.rs b/src/desktop_file.rs index bcab737b..e071bb51 100644 --- a/src/desktop_file.rs +++ b/src/desktop_file.rs @@ -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>; + +lazy_static! { + static ref DESKTOP_FILES: Mutex> = + 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 { - 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); @@ -19,55 +36,123 @@ fn find_application_dirs() -> Vec { dirs.into_iter().filter(|dir| dir.exists()).collect() } +/// Finds all the desktop files +fn find_desktop_files() -> Vec { + 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 { - 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 { + 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> { - 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 { + 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 { + 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 { - 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) }