-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathopen.rs
More file actions
151 lines (135 loc) · 4.28 KB
/
open.rs
File metadata and controls
151 lines (135 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
//! Types and functions related to shell.
use std::{ffi::OsStr, path::Path};
pub(crate) fn open<P: AsRef<OsStr>, S: AsRef<str>>(path: P, with: Option<S>) -> crate::Result<()> {
match with {
Some(program) => ::open::with_detached(path, program.as_ref()),
None => ::open::that_detached(path),
}
.map_err(Into::into)
}
/// Opens URL with the program specified in `with`, or system default if `None`.
///
/// ## Platform-specific:
///
/// - **Android / iOS**: Always opens using default program.
///
/// # Examples
///
/// ```rust,no_run
/// tauri::Builder::default()
/// .setup(|app| {
/// // open the given URL on the system default browser
/// tauri_plugin_opener::open_url("https://github.com/tauri-apps/tauri", None::<&str>)?;
/// Ok(())
/// });
/// ```
pub fn open_url<P: AsRef<str>, S: AsRef<str>>(url: P, with: Option<S>) -> crate::Result<()> {
let url = url.as_ref();
open(url, with)
}
/// Opens path with the program specified in `with`, or system default if `None`.
///
/// ## Platform-specific:
///
/// - **Android / iOS**: Always opens using default program.
///
/// # Examples
///
/// ```rust,no_run
/// tauri::Builder::default()
/// .setup(|app| {
/// // open the given URL on the system default explorer
/// tauri_plugin_opener::open_path("/path/to/file", None::<&str>)?;
/// Ok(())
/// });
/// ```
pub fn open_path<P: AsRef<Path>, S: AsRef<str>>(path: P, with: Option<S>) -> crate::Result<()> {
let path = path.as_ref();
if with.is_none() {
// Returns an IO error if not exists, and besides `exists()` is a shorthand for `metadata()`
let metadata = path.metadata()?;
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
if metadata.is_dir() {
if let Ok(()) = open_dir_dbus(path) {
return Ok(());
}
}
}
open(path, with)
}
/// Opens a dir with the default file manager via D-Bus.
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
fn open_dir_dbus(path: &Path) -> crate::Result<()> {
let connection = zbus::blocking::Connection::session()?;
open_with_filemanager1(path, &connection)
.or_else(|e| open_with_open_uri_portal(path, &connection).map_err(|_| e))
}
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
fn open_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 ShowFolders(&self, uris: Vec<&str>, startup_id: &str) -> crate::Result<()>;
}
let proxy = FileManager1ProxyBlocking::new(connection)?;
proxy.ShowFolders(vec![uri.as_str()], "")
}
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
fn open_with_open_uri_portal(
path: &Path,
connection: &zbus::blocking::Connection,
) -> crate::Result<()> {
use std::collections::HashMap;
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,
parent_window: &str,
uri: &str,
options: HashMap<&str, &str>,
) -> crate::Result<()>;
}
let proxy = PortalDesktopProxyBlocking::new(connection)?;
proxy.OpenDirectory("", uri.as_str(), HashMap::new())
}