From 60799fb544043058822350bd269afac8fa7f7a7c Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Wed, 22 Sep 2021 22:59:30 +0200 Subject: [PATCH] fix(desktop-entries): Adhere to `NotShowIn` restriction Closes #20 --- Cargo.lock | 7 +++++ plugins/Cargo.toml | 1 + plugins/src/desktop_entries/mod.rs | 48 ++++++++++++++++++++---------- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 25febae..4b00c96 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1269,6 +1269,7 @@ dependencies = [ "tracing", "tracing-subscriber", "urlencoding", + "ward", "zbus", "zvariant", ] @@ -1860,6 +1861,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" +[[package]] +name = "ward" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cbcd609d606e1307a1530061482ed2ee3fc9963128990238cefb2013127b61e" + [[package]] name = "wasi" version = "0.10.2+wasi-snapshot-preview1" diff --git a/plugins/Cargo.toml b/plugins/Cargo.toml index 61cdaaa..71aacf0 100644 --- a/plugins/Cargo.toml +++ b/plugins/Cargo.toml @@ -29,3 +29,4 @@ tracing-subscriber = "0.2" urlencoding = "2" zbus = "1" zvariant = "=2.6" # Restrict for 1.47 +ward = "2.1.0" diff --git a/plugins/src/desktop_entries/mod.rs b/plugins/src/desktop_entries/mod.rs index e15a5dc..31787df 100644 --- a/plugins/src/desktop_entries/mod.rs +++ b/plugins/src/desktop_entries/mod.rs @@ -94,26 +94,44 @@ impl App { for (src, path) in DesktopIter::new(default_paths()) { if let Ok(bytes) = std::fs::read_to_string(&path) { if let Ok(entry) = DesktopEntry::decode(&path, &bytes) { - // If defined to only show in a specific DE, avoid showing it if invalid. - let mut desktop_matched = false; - if let Some(desktops) = entry.only_show_in() { - desktop_matched = match current.as_ref() { - Some(current) => desktops - .to_ascii_lowercase() - .split(';') - .any(|desktop| current.iter().any(|c| *c == desktop)), - None => false, - }; + // Do not show if our desktop is defined in `NotShowIn`. + if let Some(not_show_in) = entry.desktop_entry("NotShowIn") { + let current = ward::ward!(current.as_ref(), else { continue }); - if !desktop_matched { + let matched = not_show_in + .to_ascii_lowercase() + .split(';') + .any(|desktop| current.iter().any(|c| *c == desktop)); + + if matched { continue; } } - if entry.no_display() - && (!desktop_matched - || entry.name(None).map_or(false, |v| v == "GNOME Shell")) - { + // Track this condition so that we can override `NoDisplay` if this is true. + let mut only_show_in = false; + + // Do not show if our desktop is not defined in `OnlyShowIn`. + if let Some(desktops) = entry.only_show_in() { + let current = ward::ward!(current.as_ref(), else { continue }); + + only_show_in = desktops + .to_ascii_lowercase() + .split(';') + .any(|desktop| current.iter().any(|c| *c == desktop)); + + if !only_show_in { + continue; + } + } + + // Avoid showing the GNOME Shell entry entirely + if entry.name(None).map_or(false, |v| v == "GNOME Shell") { + continue; + } + + // And also avoid showing anything that's set as `NoDisplay` + if !only_show_in && entry.no_display() { continue; }