fix(desktop-entries): Adhere to NotShowIn restriction

Closes #20
This commit is contained in:
Michael Aaron Murphy 2021-09-22 22:59:30 +02:00 committed by Michael Murphy
parent 7450393e97
commit 60799fb544
3 changed files with 41 additions and 15 deletions

7
Cargo.lock generated
View file

@ -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"

View file

@ -29,3 +29,4 @@ tracing-subscriber = "0.2"
urlencoding = "2"
zbus = "1"
zvariant = "=2.6" # Restrict for 1.47
ward = "2.1.0"

View file

@ -94,26 +94,44 @@ impl<W: AsyncWrite + Unpin> App<W> {
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;
}