reveal_item_in_dir (linux impl)

pull/2019/head
amrbashir 9 months ago
parent 6766bfdbd4
commit 67c8f42936
No known key found for this signature in database
GPG Key ID: BBD7A47A2003FF33

1
Cargo.lock generated

@ -6596,6 +6596,7 @@ dependencies = [
"url", "url",
"urlpattern", "urlpattern",
"windows 0.54.0", "windows 0.54.0",
"zbus",
] ]
[[package]] [[package]]

@ -23,6 +23,7 @@ dunce = "1"
specta = "=2.0.0-rc.20" specta = "=2.0.0-rc.20"
glob = "0.3" glob = "0.3"
urlpattern = "0.3" urlpattern = "0.3"
zbus = "4"
#tauri-specta = "=2.0.0-rc.11" #tauri-specta = "=2.0.0-rc.11"
[workspace.package] [workspace.package]

@ -51,3 +51,6 @@ features = [
"Win32_System_Com", "Win32_System_Com",
"Win32_System_Registry", "Win32_System_Registry",
] ]
[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"netbsd\", target_os = \"openbsd\"))".dependencies]
zbus = { workspace = true }

@ -7,6 +7,7 @@ use std::path::PathBuf;
use serde::{Serialize, Serializer}; use serde::{Serialize, Serializer};
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error { pub enum Error {
#[cfg(mobile)] #[cfg(mobile)]
#[error(transparent)] #[error(transparent)]
@ -21,15 +22,24 @@ pub enum Error {
UnknownProgramName(String), UnknownProgramName(String),
#[error("Not allowed to open {0}")] #[error("Not allowed to open {0}")]
NotAllowed(String), NotAllowed(String),
/// API not supported on the current platform
#[error("API not supported on the current platform")] #[error("API not supported on the current platform")]
UnsupportedPlatform, UnsupportedPlatform,
#[error(transparent)] #[error(transparent)]
#[cfg(windows)] #[cfg(windows)]
Win32Error(#[from] windows::core::Error), Win32Error(#[from] windows::core::Error),
/// Path doesn't have a parent.
#[error("Path doesn't have a parent: {0}")] #[error("Path doesn't have a parent: {0}")]
NoParent(PathBuf), NoParent(PathBuf),
#[error("Failed to convert path to file:// url")]
FailedToConvertPathToFileUrl,
#[error(transparent)]
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
Zbus(#[from] zbus::Error),
} }
impl Serialize for Error { impl Serialize for Error {

@ -19,15 +19,15 @@ tauri::ios_plugin_binding!(init_plugin_opener);
mod commands; mod commands;
mod error; mod error;
mod open; mod open;
mod reveal_item_in_dir;
mod scope; mod scope;
mod scope_entry; mod scope_entry;
mod show_item_in_dir;
pub use error::Error; pub use error::Error;
type Result<T> = std::result::Result<T, Error>; type Result<T> = std::result::Result<T, Error>;
pub use open::{open, Program}; pub use open::{open, Program};
pub use show_item_in_dir::show_item_in_dir; pub use reveal_item_in_dir::reveal_item_in_dir;
pub struct Opener<R: Runtime> { pub struct Opener<R: Runtime> {
#[allow(dead_code)] #[allow(dead_code)]
@ -51,8 +51,8 @@ impl<R: Runtime> Opener<R> {
.map_err(Into::into) .map_err(Into::into)
} }
pub fn show_item_in_dir<P: AsRef<Path>>(&self, p: P) -> Result<()> { pub fn reveal_item_in_dir<P: AsRef<Path>>(&self, p: P) -> Result<()> {
show_item_in_dir::show_item_in_dir(p) reveal_item_in_dir::reveal_item_in_dir(p)
} }
} }

@ -1,13 +1,12 @@
use std::path::Path; use std::path::Path;
use std::path::PathBuf;
/// Show /// Show
/// ///
/// ## Platform-specific: /// ## Platform-specific:
/// ///
/// - **Android / iOS:** Unsupported. /// - **Android / iOS:** Unsupported.
pub fn show_item_in_dir<P: AsRef<Path>>(p: P) -> crate::Result<()> { pub fn reveal_item_in_dir<P: AsRef<Path>>(path: P) -> crate::Result<()> {
let p = p.as_ref().canonicalize()?; let path = path.as_ref().canonicalize()?;
#[cfg(any( #[cfg(any(
windows, windows,
@ -18,7 +17,7 @@ pub fn show_item_in_dir<P: AsRef<Path>>(p: P) -> crate::Result<()> {
target_os = "netbsd", target_os = "netbsd",
target_os = "openbsd" target_os = "openbsd"
))] ))]
return imp::show_item_in_dir(p); return imp::reveal_item_in_dir(&path);
#[cfg(not(any( #[cfg(not(any(
windows, windows,
@ -51,8 +50,8 @@ mod imp {
}, },
}; };
pub fn show_item_in_dir(p: PathBuf) -> crate::Result<()> { pub fn reveal_item_in_dir(path: &Path) -> crate::Result<()> {
let file = dunce::simplified(&p); let file = dunce::simplified(path);
let _ = unsafe { CoInitialize(None) }; let _ = unsafe { CoInitialize(None) };
@ -105,10 +104,63 @@ mod imp {
target_os = "openbsd" target_os = "openbsd"
))] ))]
mod imp { mod imp {
use std::collections::HashMap;
use super::*; use super::*;
pub fn show_item_in_dir(p: PathBuf) -> crate::Result<()> { pub fn reveal_item_in_dir(path: &Path) -> crate::Result<()> {
Ok(()) let connection = zbus::blocking::Connection::session()?;
reveal_with_filemanager1(path, &connection)
.or_else(|_| reveal_with_open_uri_portal(path, &connection))
}
fn reveal_with_filemanager1(
path: &Path,
connection: &zbus::blocking::Connection,
) -> crate::Result<()> {
let uri = url::Url::from_file_path(path)
.map_err(|_| crate::Error::FailedToConvertPathToFileUrl)?;
#[zbus::proxy(
interface = "org.freedesktop.FileManager1",
default_service = "org.freedesktop.FileManager1",
default_path = "/org/freedesktop/FileManager1"
)]
trait FileManager1 {
async fn ShowItems(&self, name: Vec<&str>, arg2: &str) -> crate::Result<()>;
}
let proxy = FileManager1ProxyBlocking::new(connection)?;
proxy.ShowItems(vec![uri.as_str()], "")
}
fn reveal_with_open_uri_portal(
path: &Path,
connection: &zbus::blocking::Connection,
) -> crate::Result<()> {
let uri = url::Url::from_file_path(path)
.map_err(|_| crate::Error::FailedToConvertPathToFileUrl)?;
#[zbus::proxy(
interface = "org.freedesktop.portal.Desktop",
default_service = "org.freedesktop.portal.OpenURI",
default_path = "/org/freedesktop/portal/desktop"
)]
trait PortalDesktop {
async fn OpenDirectory(
&self,
arg1: &str,
name: &str,
arg3: HashMap<&str, &str>,
) -> crate::Result<()>;
}
let proxy = PortalDesktopProxyBlocking::new(connection)?;
proxy.OpenDirectory("", uri.as_str(), HashMap::new())
} }
} }
@ -116,5 +168,5 @@ mod imp {
mod imp { mod imp {
use super::*; use super::*;
pub fn show_item_in_dir(p: PathBuf) -> crate::Result<()> {} pub fn reveal_item_in_dir(path: &Path) -> crate::Result<()> {}
} }

@ -42,7 +42,7 @@ features = [
] ]
[target."cfg(target_os = \"linux\")".dependencies] [target."cfg(target_os = \"linux\")".dependencies]
zbus = "4" zbus = { workspace = true }
[features] [features]
semver = ["dep:semver"] semver = ["dep:semver"]

Loading…
Cancel
Save