diff --git a/src-tauri/linux/DBX.desktop b/src-tauri/linux/DBX.desktop new file mode 100644 index 0000000000..d56a80e6df --- /dev/null +++ b/src-tauri/linux/DBX.desktop @@ -0,0 +1,14 @@ +[Desktop Entry] +Categories={{categories}} +{{#if comment}} +Comment={{comment}} +{{/if}} +Exec={{exec}} %U +StartupWMClass={{exec}} +Icon={{icon}} +Name={{name}} +Terminal=false +Type=Application +{{#if mime_type}} +MimeType={{mime_type}} +{{/if}} diff --git a/src-tauri/src/commands/external_db.rs b/src-tauri/src/commands/external_db.rs index ca5b65080e..81242c3f60 100644 --- a/src-tauri/src/commands/external_db.rs +++ b/src-tauri/src/commands/external_db.rs @@ -44,7 +44,7 @@ fn db_file_path_from_arg(arg: &str, cwd: &Path) -> Option { return None; } - let path = PathBuf::from(arg); + let path = PathBuf::from(super::launch_args::normalize_launch_path_arg(arg)?); if !is_db_file_path(&path) { return None; } @@ -91,6 +91,13 @@ mod tests { assert_eq!(paths, vec!["/work/data/mydb.db"]); } + #[test] + fn accepts_db_file_args_passed_as_file_urls() { + let paths = db_file_paths_from_args(["file:///tmp/a.db", "file:///tmp/my%20data.sqlite"], Path::new("/work")); + + assert_eq!(paths, vec!["/tmp/a.db", "/tmp/my data.sqlite"]); + } + #[test] fn drains_pending_db_file_paths_once() { let state = ExternalDbOpenState::default(); diff --git a/src-tauri/src/commands/external_sql.rs b/src-tauri/src/commands/external_sql.rs index 7e479f1172..02814b069a 100644 --- a/src-tauri/src/commands/external_sql.rs +++ b/src-tauri/src/commands/external_sql.rs @@ -156,7 +156,7 @@ fn sql_file_path_from_arg(arg: &str, cwd: &Path) -> Option { return None; } - let path = PathBuf::from(arg); + let path = PathBuf::from(super::launch_args::normalize_launch_path_arg(arg)?); if !is_sql_file_path(&path) { return None; } @@ -368,6 +368,13 @@ mod tests { assert_eq!(paths, vec!["/work/queries/report.sql"]); } + #[test] + fn accepts_sql_file_args_passed_as_file_urls() { + let paths = sql_file_paths_from_args(["file:///tmp/a.sql", "file:///tmp/my%20report.sql"], Path::new("/work")); + + assert_eq!(paths, vec!["/tmp/a.sql", "/tmp/my report.sql"]); + } + #[test] fn drains_pending_sql_file_paths_once() { let state = ExternalSqlOpenState::default(); diff --git a/src-tauri/src/commands/launch_args.rs b/src-tauri/src/commands/launch_args.rs new file mode 100644 index 0000000000..4528edba9f --- /dev/null +++ b/src-tauri/src/commands/launch_args.rs @@ -0,0 +1,100 @@ +//! Helpers for interpreting the arguments DBX is launched with. +//! +//! The Linux desktop entry uses the `%U` field code so one `Exec=` line can serve +//! both the `dbx://` scheme and the file associations. With `%U` a file manager may +//! hand over `file://` URLs rather than plain paths, so launch arguments are +//! normalized before anything treats them as filesystem paths. + +use percent_encoding::percent_decode_str; + +/// Normalizes a launch argument into a filesystem path. +/// +/// `file://` URLs, optionally with a `localhost` authority, are percent-decoded into +/// local paths. Any other argument is returned unchanged. Returns `None` for a +/// `file://` URL that points at another host, since that is not a local path. +pub fn normalize_launch_path_arg(arg: &str) -> Option { + let Some(rest) = arg.strip_prefix("file://") else { + return Some(arg.to_string()); + }; + + let rest = rest.strip_prefix("localhost").unwrap_or(rest); + if !rest.starts_with('/') { + return None; + } + + Some(percent_decode_str(rest).decode_utf8().ok()?.into_owned()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn passes_plain_paths_through_unchanged() { + assert_eq!(normalize_launch_path_arg("/tmp/query.sql").as_deref(), Some("/tmp/query.sql")); + assert_eq!(normalize_launch_path_arg("query.sql").as_deref(), Some("query.sql")); + assert_eq!(normalize_launch_path_arg("dbx://open").as_deref(), Some("dbx://open")); + } + + #[test] + fn decodes_local_file_urls() { + assert_eq!(normalize_launch_path_arg("file:///tmp/query.sql").as_deref(), Some("/tmp/query.sql")); + assert_eq!(normalize_launch_path_arg("file://localhost/tmp/query.sql").as_deref(), Some("/tmp/query.sql")); + } + + #[test] + fn percent_decodes_file_urls() { + assert_eq!(normalize_launch_path_arg("file:///tmp/my%20query.sql").as_deref(), Some("/tmp/my query.sql")); + assert_eq!(normalize_launch_path_arg("file:///tmp/%E6%9F%A5%E8%AF%A2.sql").as_deref(), Some("/tmp/查询.sql")); + } + + /// The desktop entry is a shipped artifact whose `Exec=` field code is what puts + /// `dbx://` links and associated files into `std::env::args()` on Linux. Without a + /// URL field code both arrive empty, so the contract is pinned here. + #[test] + fn linux_desktop_entry_forwards_urls_and_files() { + let template = include_str!("../../linux/DBX.desktop"); + + let exec = + template.lines().find_map(|line| line.strip_prefix("Exec=")).expect("desktop entry must declare Exec="); + assert!( + exec.ends_with(" %U") || exec.ends_with(" %u"), + "Exec= needs a URL field code or launch arguments are dropped, got {exec:?}" + ); + + let wm_class = template.lines().find_map(|line| line.strip_prefix("StartupWMClass=")); + assert_eq!( + wm_class, + Some("{{exec}}"), + "StartupWMClass must stay the bare executable so window grouping keeps working" + ); + } + + /// Guards the wiring: the template above only ships if the bundler is pointed at it. + #[test] + fn bundle_config_uses_the_linux_desktop_entry() { + let config: serde_json::Value = + serde_json::from_str(include_str!("../../tauri.conf.json")).expect("tauri.conf.json must be valid JSON"); + + for target in ["deb", "rpm"] { + assert_eq!( + config["bundle"]["linux"][target]["desktopTemplate"].as_str(), + Some("linux/DBX.desktop"), + "{target} bundle must use the custom desktop entry" + ); + } + + assert!( + !config["plugins"]["deep-link"]["desktop"]["schemes"] + .as_array() + .map(|schemes| schemes.is_empty()) + .unwrap_or(true), + "a deep-link scheme is what makes the Exec field code load-bearing" + ); + } + + #[test] + fn rejects_file_urls_on_another_host() { + assert_eq!(normalize_launch_path_arg("file://example.com/tmp/query.sql"), None); + } +} diff --git a/src-tauri/src/commands/mod.rs b/src-tauri/src/commands/mod.rs index 275c641917..2a7b268240 100644 --- a/src-tauri/src/commands/mod.rs +++ b/src-tauri/src/commands/mod.rs @@ -24,6 +24,7 @@ pub mod fs_open; pub mod hbase_cmd; pub mod history; pub mod keychain; +pub mod launch_args; pub mod list_sql_files; pub mod mcp; pub mod mcp_bridge; diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 7f2a4231e7..4b218e2049 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -35,6 +35,7 @@ }, "bundle": { "active": true, + "category": "DeveloperTool", "targets": "all", "createUpdaterArtifacts": true, "fileAssociations": [ @@ -52,6 +53,14 @@ "role": "Editor" } ], + "linux": { + "deb": { + "desktopTemplate": "linux/DBX.desktop" + }, + "rpm": { + "desktopTemplate": "linux/DBX.desktop" + } + }, "macOS": { "entitlements": "Entitlements.plist" },