From 3d34e3e49fe9fda60231131dac2a8e49b701e0b7 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Mon, 4 Mar 2024 11:46:38 -0700 Subject: [PATCH] Parse desktop specific mimeapps files --- src/mime_app.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/mime_app.rs b/src/mime_app.rs index 57104d6..8db768e 100644 --- a/src/mime_app.rs +++ b/src/mime_app.rs @@ -7,7 +7,7 @@ use cosmic::widget; pub use mime_guess::Mime; use once_cell::sync::Lazy; use std::{ - cmp::Ordering, collections::HashMap, path::PathBuf, process, sync::Mutex, time::Instant, + cmp::Ordering, collections::HashMap, env, path::PathBuf, process, sync::Mutex, time::Instant, }; #[derive(Clone, Debug)] @@ -123,18 +123,36 @@ impl MimeAppCache { } } + let mut desktops: Vec = env::var("XDG_CURRENT_DESKTOP") + .unwrap_or_default() + .split(':') + .map(|x| x.to_ascii_lowercase()) + .collect(); + // Load mimeapps.list files // https://specifications.freedesktop.org/mime-apps-spec/mime-apps-spec-latest.html - //TODO: support lookup by desktop (colon separated list in $XDG_CURRENT_DESKTOP, converted to lowercase) + //TODO: ensure correct lookup order let mut mimeapps_paths = Vec::new(); match xdg::BaseDirectories::new() { Ok(xdg_dirs) => { for path in xdg_dirs.find_data_files("applications/mimeapps.list") { mimeapps_paths.push(path); } + for desktop in desktops.iter().rev() { + for path in + xdg_dirs.find_data_files(format!("applications/{desktop}-mimeapps.list")) + { + mimeapps_paths.push(path); + } + } for path in xdg_dirs.find_config_files("mimeapps.list") { mimeapps_paths.push(path); } + for desktop in desktops.iter().rev() { + for path in xdg_dirs.find_config_files(format!("{desktop}-mimeapps.list")) { + mimeapps_paths.push(path); + } + } } Err(err) => { log::warn!("failed to get xdg base directories: {}", err);