Skip to content

Commit 84be2d4

Browse files
swsnrsdroege
authored andcommitted
Add simple example about DBus signals
1 parent 4787eb3 commit 84be2d4

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

examples/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ path = "gio_futures/main.rs"
3636
name = "gio_futures_await"
3737
path = "gio_futures_await/main.rs"
3838

39+
[[bin]]
40+
name = "gio_dbus_receive_signals"
41+
path = "gio_dbus_receive_signals/main.rs"
42+
3943
[[bin]]
4044
name = "gio_dbus_register_object"
4145
path = "gio_dbus_register_object/main.rs"
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
use gio::prelude::*;
2+
3+
glib::wrapper! {
4+
pub struct SampleApplication(ObjectSubclass<imp::SampleApplication>)
5+
@extends gio::Application,
6+
@implements gio::ActionGroup, gio::ActionMap;
7+
}
8+
9+
impl Default for SampleApplication {
10+
fn default() -> Self {
11+
glib::Object::builder()
12+
.property(
13+
"application-id",
14+
"com.github.gtk-rs.examples.ReceiveDBusSignals",
15+
)
16+
.build()
17+
}
18+
}
19+
20+
mod imp {
21+
use std::cell::RefCell;
22+
23+
use futures::{future, StreamExt};
24+
use gio::prelude::*;
25+
use gio::subclass::prelude::*;
26+
use gio::{bus_get_future, BusType, DBusSignalFlags, WeakSignalSubscription};
27+
28+
const DESKTOP_PORTAL_BUSNAME: &str = "org.freedesktop.portal.Desktop";
29+
const DESKTOP_PORTAL_OBJPATH: &str = "/org/freedesktop/portal/desktop";
30+
const SETTINGS_PORTAL_IFACE: &str = "org.freedesktop.portal.Settings";
31+
32+
#[derive(Default)]
33+
pub struct SampleApplication {
34+
signal_subscription: RefCell<Option<WeakSignalSubscription>>,
35+
}
36+
37+
#[glib::object_subclass]
38+
impl ObjectSubclass for SampleApplication {
39+
const NAME: &'static str = "SampleApplication";
40+
41+
type Type = super::SampleApplication;
42+
43+
type ParentType = gio::Application;
44+
}
45+
46+
impl ObjectImpl for SampleApplication {}
47+
48+
impl ApplicationImpl for SampleApplication {
49+
fn startup(&self) {
50+
self.parent_startup();
51+
52+
self.signal_subscription.replace(Some(
53+
self.obj()
54+
.dbus_connection()
55+
.unwrap()
56+
.subscribe_to_signal(
57+
Some(DESKTOP_PORTAL_BUSNAME),
58+
Some(SETTINGS_PORTAL_IFACE),
59+
Some("SettingChanged"),
60+
Some(DESKTOP_PORTAL_OBJPATH),
61+
None,
62+
DBusSignalFlags::NONE,
63+
|signal| {
64+
println!(
65+
"Callback received signal {}.{} from {} at {} with parameters: {}",
66+
signal.interface_name,
67+
signal.signal_name,
68+
signal.object_path,
69+
signal.sender_name,
70+
signal.parameters
71+
)
72+
},
73+
)
74+
.downgrade(),
75+
));
76+
77+
glib::spawn_future_local(async move {
78+
let session_bus = bus_get_future(BusType::Session).await.unwrap();
79+
session_bus
80+
.receive_signal_parameters::<(String, String, glib::Variant)>(
81+
Some(DESKTOP_PORTAL_BUSNAME),
82+
Some(SETTINGS_PORTAL_IFACE),
83+
Some("SettingChanged"),
84+
Some(DESKTOP_PORTAL_OBJPATH),
85+
None,
86+
DBusSignalFlags::NONE,
87+
)
88+
.for_each(|result| {
89+
let (iface, setting, value) = result.unwrap();
90+
println!("Setting {iface}.{setting} changed to {value}");
91+
future::ready(())
92+
})
93+
.await
94+
});
95+
}
96+
97+
fn activate(&self) {}
98+
}
99+
}
100+
101+
fn main() -> glib::ExitCode {
102+
let app = SampleApplication::default();
103+
let _guard = app.hold();
104+
app.run()
105+
}

0 commit comments

Comments
 (0)