Skip to content

Commit 2e7c01b

Browse files
zeenixSeeker14491
authored andcommitted
Switch from dbus-rs to zbus
1 parent edf5d9d commit 2e7c01b

File tree

2 files changed

+44
-41
lines changed

2 files changed

+44
-41
lines changed

opener/Cargo.toml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,19 @@ license = "MIT OR Apache-2.0"
1414
maintenance = { status = "passively-maintained" }
1515

1616
[features]
17-
default = ["dbus-vendored"]
18-
reveal = ["dep:url", "dep:dbus", "windows-sys/Win32_System_Com"]
19-
dbus-vendored = ["dbus?/vendored"]
17+
reveal = ["dep:url", "dep:zbus", "windows-sys/Win32_System_Com"]
2018

2119
[target.'cfg(target_os = "linux")'.dependencies]
2220
bstr = "1"
23-
dbus = { version = "0.9", optional = true }
21+
zbus = { version = "5", optional = true, features = ["url"] }
2422
url = { version = "2", optional = true }
2523

2624
[target.'cfg(windows)'.dependencies]
2725
normpath = "1"
2826
windows-sys = { version = "0.59", features = [
29-
"Win32_Foundation",
30-
"Win32_UI_Shell",
31-
"Win32_UI_WindowsAndMessaging",
27+
"Win32_Foundation",
28+
"Win32_UI_Shell",
29+
"Win32_UI_WindowsAndMessaging",
3230
] }
3331

3432
[package.metadata.docs.rs]

opener/src/freedesktop.rs

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,38 @@
77
//! * Path to non-existent file generates an error for both implementations.
88
99
use crate::OpenError;
10-
use dbus::arg::messageitem::MessageItem;
11-
use dbus::arg::{Append, Variant};
12-
use dbus::blocking::Connection;
10+
use std::collections::HashMap;
1311
use std::fs::File;
12+
use std::os::fd::AsFd;
1413
use std::path::Path;
15-
use std::time::Duration;
1614
use std::{error, fmt, io};
1715
use url::Url;
18-
19-
const DBUS_TIMEOUT: Duration = Duration::from_secs(5);
16+
use zbus::blocking::Connection;
2017

2118
// We should prefer the OpenURI interface, because it correctly handles runtimes such as Flatpak.
2219
// However, OpenURI was broken in the original version of the interface (it did not highlight the items).
2320
// This version is still in use by some distributions, which would result in degraded functionality for some users.
2421
// That's why we're first trying to use the FileManager1 interface, falling back to the OpenURI interface.
2522
// Source: https://chromium-review.googlesource.com/c/chromium/src/+/3009959
2623
pub(crate) fn reveal_with_dbus(path: &Path) -> Result<(), OpenError> {
27-
let connection = Connection::new_session().map_err(dbus_to_open_error)?;
24+
let connection = Connection::session().map_err(dbus_to_open_error)?;
2825
reveal_with_filemanager1(path, &connection)
2926
.or_else(|_| reveal_with_open_uri_portal(path, &connection))
3027
}
3128

3229
fn reveal_with_filemanager1(path: &Path, connection: &Connection) -> Result<(), OpenError> {
3330
let uri = path_to_uri(path)?;
34-
let proxy = connection.with_proxy(
35-
"org.freedesktop.FileManager1",
36-
"/org/freedesktop/FileManager1",
37-
DBUS_TIMEOUT,
38-
);
39-
proxy
40-
.method_call(
41-
"org.freedesktop.FileManager1",
42-
"ShowItems",
43-
(vec![uri.as_str()], ""),
44-
)
45-
.map_err(dbus_to_open_error)
31+
let proxy = FileManager1Proxy::new(connection).map_err(dbus_to_open_error)?;
32+
proxy.show_items(&[uri], "").map_err(dbus_to_open_error)
4633
}
4734

4835
fn reveal_with_open_uri_portal(path: &Path, connection: &Connection) -> Result<(), OpenError> {
4936
let file = File::open(path).map_err(OpenError::Io)?;
50-
let proxy = connection.with_proxy(
51-
"org.freedesktop.portal.Desktop",
52-
"/org/freedesktop/portal/desktop",
53-
DBUS_TIMEOUT,
54-
);
37+
let proxy = OpenURIProxy::new(connection).map_err(dbus_to_open_error)?;
5538
proxy
56-
.method_call(
57-
"org.freedesktop.portal.OpenURI",
58-
"OpenDirectory",
59-
("", file, empty_vardict()),
60-
)
39+
.open_directory("", file.as_fd().into(), HashMap::new())
6140
.map_err(dbus_to_open_error)
62-
}
63-
64-
fn empty_vardict() -> impl Append {
65-
dbus::arg::Dict::<&'static str, Variant<MessageItem>, _>::new(std::iter::empty())
41+
.map(|_| ())
6642
}
6743

6844
fn path_to_uri(path: &Path) -> Result<Url, OpenError> {
@@ -77,7 +53,7 @@ fn uri_to_open_error() -> OpenError {
7753
))
7854
}
7955

80-
fn dbus_to_open_error(error: dbus::Error) -> OpenError {
56+
fn dbus_to_open_error(error: zbus::Error) -> OpenError {
8157
OpenError::Io(io::Error::other(error))
8258
}
8359

@@ -91,3 +67,32 @@ impl fmt::Display for FilePathToUriError {
9167
}
9268

9369
impl error::Error for FilePathToUriError {}
70+
71+
/// # D-Bus interface proxy for `org.freedesktop.FileManager1` interface.
72+
#[zbus::proxy(
73+
gen_async = false,
74+
interface = "org.freedesktop.FileManager1",
75+
default_service = "org.freedesktop.FileManager1",
76+
default_path = "/org/freedesktop/FileManager1"
77+
)]
78+
trait FileManager1 {
79+
/// ShowItems method
80+
fn show_items(&self, uris: &[Url], startup_id: &str) -> zbus::Result<()>;
81+
}
82+
83+
/// # D-Bus interface proxy for: `org.freedesktop.portal.OpenURI`
84+
#[zbus::proxy(
85+
gen_async = false,
86+
interface = "org.freedesktop.portal.OpenURI",
87+
default_service = "org.freedesktop.portal.Desktop",
88+
default_path = "/org/freedesktop/portal/desktop"
89+
)]
90+
pub trait OpenURI {
91+
/// OpenDirectory method
92+
fn open_directory(
93+
&self,
94+
parent_window: &str,
95+
fd: zbus::zvariant::Fd<'_>,
96+
options: HashMap<&str, &zbus::zvariant::Value<'_>>,
97+
) -> zbus::Result<zbus::zvariant::OwnedObjectPath>;
98+
}

0 commit comments

Comments
 (0)