From 3767a61f2abfb3628d7471e7c513142f99c504b3 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 16 Oct 2024 18:57:44 -0600 Subject: [PATCH] Try to open with mime apps before trying xdg-open --- src/app.rs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/app.rs b/src/app.rs index 6341d70..f71d5c0 100644 --- a/src/app.rs +++ b/src/app.rs @@ -528,15 +528,15 @@ pub struct App { impl App { fn open_file(&mut self, path: &PathBuf) { - let mut found_desktop_exec = false; let mime = mime_icon::mime_for_path(path); if mime == "application/x-desktop" { + // Try opening desktop application match freedesktop_entry_parser::parse_entry(path) { Ok(entry) => match entry.section("Desktop Entry").attr("Exec") { Some(exec) => match mime_app::exec_to_command(exec, None) { Some(mut command) => match spawn_detached(&mut command) { Ok(()) => { - found_desktop_exec = true; + return; } Err(err) => { log::warn!("failed to execute {:?}: {}", path, err); @@ -555,6 +555,7 @@ impl App { } } } else if mime == "application/x-executable" || mime == "application/vnd.appimage" { + // Try opening executable let mut command = std::process::Command::new(path); match spawn_detached(&mut command) { Ok(()) => {} @@ -571,10 +572,15 @@ impl App { } }, } - found_desktop_exec = true; + return; } - if !found_desktop_exec { - match open::that_detached(path) { + + // Try mime apps, which should be faster than xdg-open + for app in mime_app::mime_apps(&mime) { + let Some(mut command) = app.command(Some(path.clone().into())) else { + continue; + }; + match spawn_detached(&mut command) { Ok(()) => { let _ = recently_used_xbel::update_recently_used( path, @@ -582,12 +588,28 @@ impl App { "cosmic-files".to_string(), None, ); + return; } Err(err) => { - log::warn!("failed to open {:?}: {}", path, err); + log::warn!("failed to open {:?} with {:?}: {}", path, app.id, err); } } } + + // Fall back to using open crate + match open::that_detached(path) { + Ok(()) => { + let _ = recently_used_xbel::update_recently_used( + path, + App::APP_ID.to_string(), + "cosmic-files".to_string(), + None, + ); + } + Err(err) => { + log::warn!("failed to open {:?}: {}", path, err); + } + } } fn open_tab_entity(