2023-06-29 21:20:10 +03:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use std::collections::{HashMap, HashSet};
|
2023-07-04 01:53:17 +02:00
|
|
|
use std::env;
|
2023-06-29 21:20:10 +03:00
|
|
|
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
|
2022-08-14 14:30:13 +01:00
|
|
|
/// and exist on the filesystem.
|
|
|
|
fn find_application_dirs() -> Vec<PathBuf> {
|
2023-06-29 21:20:10 +03:00
|
|
|
let mut dirs = vec![
|
|
|
|
PathBuf::from("/usr/share/applications"), // system installed apps
|
|
|
|
PathBuf::from("/var/lib/flatpak/exports/share/applications"), // flatpak apps
|
|
|
|
];
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2023-07-04 01:53:17 +02:00
|
|
|
let xdg_dirs = env::var_os("XDG_DATA_DIRS");
|
|
|
|
if let Some(xdg_dirs) = xdg_dirs {
|
|
|
|
for mut xdg_dir in env::split_paths(&xdg_dirs).map(PathBuf::from) {
|
|
|
|
xdg_dir.push("applications");
|
|
|
|
dirs.push(xdg_dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 21:20:10 +03:00
|
|
|
let user_dir = dirs::data_local_dir(); // user installed apps
|
2022-08-14 14:30:13 +01:00
|
|
|
if let Some(mut user_dir) = user_dir {
|
|
|
|
user_dir.push("applications");
|
|
|
|
dirs.push(user_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
dirs.into_iter().filter(|dir| dir.exists()).collect()
|
|
|
|
}
|
|
|
|
|
2023-06-29 21:20:10 +03:00
|
|
|
/// 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()
|
|
|
|
}
|
|
|
|
|
2022-08-14 14:30:13 +01:00
|
|
|
/// Attempts to locate a `.desktop` file for an app id
|
|
|
|
pub fn find_desktop_file(app_id: &str) -> Option<PathBuf> {
|
2023-06-29 21:20:10 +03:00
|
|
|
// this is necessary to invalidate the cache
|
|
|
|
let files = find_desktop_files();
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2023-07-05 23:18:43 +01:00
|
|
|
find_desktop_file_by_filename(app_id, &files)
|
|
|
|
.or_else(|| find_desktop_file_by_filedata(app_id, &files))
|
2023-06-29 21:20:10 +03:00
|
|
|
}
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2023-06-29 21:20:10 +03:00
|
|
|
/// Finds the correct desktop file using a simple condition check
|
|
|
|
fn find_desktop_file_by_filename(app_id: &str, files: &[PathBuf]) -> Option<PathBuf> {
|
2023-07-05 23:18:43 +01:00
|
|
|
let with_names = files
|
|
|
|
.iter()
|
|
|
|
.map(|f| {
|
|
|
|
(
|
|
|
|
f,
|
|
|
|
f.file_stem()
|
|
|
|
.unwrap_or_default()
|
|
|
|
.to_string_lossy()
|
|
|
|
.to_lowercase(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2023-07-05 23:18:43 +01:00
|
|
|
with_names
|
2023-06-29 21:20:10 +03:00
|
|
|
.iter()
|
2023-07-05 23:18:43 +01:00
|
|
|
// first pass - check for exact match
|
|
|
|
.find(|(_, name)| name.eq_ignore_ascii_case(app_id))
|
|
|
|
// second pass - check for substring
|
|
|
|
.or_else(|| {
|
|
|
|
with_names.iter().find(|(_, name)| {
|
|
|
|
// this will attempt to find flatpak apps that are in the format
|
|
|
|
// `com.company.app` or `com.app.something`
|
|
|
|
app_id
|
2023-06-29 21:20:10 +03:00
|
|
|
.split(&[' ', ':', '@', '.', '_'][..])
|
2023-07-26 21:52:05 +01:00
|
|
|
.any(|part| name.eq_ignore_ascii_case(part))
|
2023-07-05 23:18:43 +01:00
|
|
|
})
|
2023-06-29 21:20:10 +03:00
|
|
|
})
|
2023-07-05 23:18:43 +01:00
|
|
|
.map(|(file, _)| file.into())
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
2023-06-29 21:20:10 +03:00
|
|
|
/// 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);
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2023-07-05 23:18:43 +01:00
|
|
|
let files = files
|
2023-06-29 21:20:10 +03:00
|
|
|
.iter()
|
|
|
|
.filter_map(|file| {
|
2023-08-25 22:55:00 +01:00
|
|
|
let Some(parsed_desktop_file) = parse_desktop_file(file) else {
|
|
|
|
return None;
|
|
|
|
};
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2023-06-29 21:20:10 +03:00
|
|
|
desktop_files_cache.insert(file.clone(), parsed_desktop_file.clone());
|
|
|
|
Some((file.clone(), parsed_desktop_file))
|
|
|
|
})
|
2023-07-05 23:18:43 +01:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let file = files
|
|
|
|
.iter()
|
|
|
|
// first pass - check name key for exact match
|
2023-06-29 21:20:10 +03:00
|
|
|
.find(|(_, desktop_file)| {
|
|
|
|
desktop_file
|
2023-07-05 23:18:43 +01:00
|
|
|
.get("Name")
|
|
|
|
.map(|names| names.iter().any(|name| name.eq_ignore_ascii_case(app_id)))
|
|
|
|
.unwrap_or_default()
|
|
|
|
})
|
|
|
|
// second pass - check name key for substring
|
|
|
|
.or_else(|| {
|
|
|
|
files.iter().find(|(_, desktop_file)| {
|
|
|
|
desktop_file
|
|
|
|
.get("Name")
|
|
|
|
.map(|names| {
|
|
|
|
names
|
|
|
|
.iter()
|
|
|
|
.any(|name| name.to_lowercase().contains(app_id))
|
|
|
|
})
|
|
|
|
.unwrap_or_default()
|
|
|
|
})
|
2023-06-29 21:20:10 +03:00
|
|
|
})
|
2023-07-05 23:18:43 +01:00
|
|
|
// third pass - check all keys for substring
|
|
|
|
.or_else(|| {
|
|
|
|
files.iter().find(|(_, desktop_file)| {
|
|
|
|
desktop_file
|
|
|
|
.values()
|
|
|
|
.flatten()
|
|
|
|
.any(|value| value.to_lowercase().contains(app_id))
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
file.map(|(path, _)| path).cloned()
|
2023-06-29 21:20:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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| {
|
2023-08-25 22:55:00 +01:00
|
|
|
let Some((key, value)) = line.split_once('=') else {
|
|
|
|
return None;
|
|
|
|
};
|
2023-06-29 21:20:10 +03:00
|
|
|
|
|
|
|
let key = key.trim();
|
|
|
|
let value = value.trim();
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2023-06-29 21:20:10 +03:00
|
|
|
if DESKTOP_FILES_LOOK_OUT_KEYS.contains(key) {
|
|
|
|
Some((key, value))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.for_each(|(key, value)| {
|
|
|
|
desktop_file
|
|
|
|
.entry(key.to_string())
|
2023-10-17 20:22:19 +01:00
|
|
|
.or_default()
|
2023-06-29 21:20:10 +03:00
|
|
|
.push(value.to_string());
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(desktop_file)
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to get the icon name from the app's `.desktop` file.
|
2023-01-29 17:46:02 +00:00
|
|
|
pub fn get_desktop_icon_name(app_id: &str) -> Option<String> {
|
2023-08-25 22:55:00 +01:00
|
|
|
let Some(path) = find_desktop_file(app_id) else {
|
|
|
|
return None;
|
|
|
|
};
|
2023-06-29 21:20:10 +03:00
|
|
|
|
|
|
|
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)
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|