Skip to content

Commit 759c959

Browse files
swsnrsdroege
authored andcommitted
Bind dbus_unregister vfunc
1 parent 6fa1871 commit 759c959

File tree

2 files changed

+72
-18
lines changed

2 files changed

+72
-18
lines changed

examples/gio_dbus_register_object/main.rs

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ mod imp {
3737
<arg type='u' name='delay' direction='in'/>
3838
<arg type='s' name='greet' direction='out'/>
3939
</method>
40+
<method name='GoodBye'></method>
4041
</interface>
4142
</node>
4243
"#;
@@ -56,6 +57,7 @@ mod imp {
5657
enum HelloMethod {
5758
Hello(Hello),
5859
SlowHello(SlowHello),
60+
GoodBye,
5961
}
6062

6163
impl DBusMethodCall for HelloMethod {
@@ -68,6 +70,7 @@ mod imp {
6870
match method {
6971
"Hello" => Ok(params.get::<Hello>().map(Self::Hello)),
7072
"SlowHello" => Ok(params.get::<SlowHello>().map(Self::SlowHello)),
73+
"GoodBye" => Ok(Some(Self::GoodBye)),
7174
_ => Err(glib::Error::new(IOErrorEnum::Failed, "No such method")),
7275
}
7376
.and_then(|p| {
@@ -94,24 +97,35 @@ mod imp {
9497
connection
9598
.register_object("/com/github/gtk_rs/examples/HelloWorld", &example)
9699
.typed_method_call::<HelloMethod>()
97-
.invoke_and_return_future_local(|_, sender, call| {
98-
println!("Method call from {sender:?}");
99-
async {
100-
match call {
101-
HelloMethod::Hello(Hello { name }) => {
102-
let greet = format!("Hello {name}!");
103-
println!("{greet}");
104-
Ok(Some(greet.to_variant()))
105-
}
106-
HelloMethod::SlowHello(SlowHello { name, delay }) => {
107-
glib::timeout_future(Duration::from_secs(delay as u64)).await;
108-
let greet = format!("Hello {name} after {delay} seconds!");
109-
println!("{greet}");
110-
Ok(Some(greet.to_variant()))
100+
.invoke_and_return_future_local(glib::clone!(
101+
#[weak_allow_none(rename_to = app)]
102+
self.obj(),
103+
move |_, sender, call| {
104+
println!("Method call from {sender:?}");
105+
let app = app.clone();
106+
async move {
107+
match call {
108+
HelloMethod::Hello(Hello { name }) => {
109+
let greet = format!("Hello {name}!");
110+
println!("{greet}");
111+
Ok(Some(greet.to_variant()))
112+
}
113+
HelloMethod::SlowHello(SlowHello { name, delay }) => {
114+
glib::timeout_future(Duration::from_secs(delay as u64)).await;
115+
let greet = format!("Hello {name} after {delay} seconds!");
116+
println!("{greet}");
117+
Ok(Some(greet.to_variant()))
118+
}
119+
HelloMethod::GoodBye => {
120+
if let Some(app) = app {
121+
app.quit();
122+
}
123+
Ok(None)
124+
}
111125
}
112126
}
113127
}
114-
})
128+
))
115129
.build()
116130
}
117131
}
@@ -140,9 +154,9 @@ mod imp {
140154
Ok(())
141155
}
142156

143-
fn shutdown(&self) {
157+
fn dbus_unregister(&self, connection: &DBusConnection, object_path: &str) {
158+
self.parent_dbus_unregister(connection, object_path);
144159
if let Some(id) = self.registration_id.take() {
145-
let connection = self.obj().dbus_connection().expect("connection");
146160
if connection.unregister_object(id).is_ok() {
147161
println!("Unregistered object");
148162
} else {
@@ -151,9 +165,16 @@ mod imp {
151165
}
152166
}
153167

168+
fn shutdown(&self) {
169+
self.parent_shutdown();
170+
println!("Good bye!");
171+
}
172+
154173
fn activate(&self) {
155174
println!("Waiting for DBus Hello method to be called. Call the following command from another terminal:");
156175
println!("dbus-send --print-reply --dest=com.github.gtk-rs.examples.RegisterDBusObject /com/github/gtk_rs/examples/HelloWorld com.github.gtk_rs.examples.HelloWorld.Hello string:YourName");
176+
println!("Quit with the following command:");
177+
println!("dbus-send --print-reply --dest=com.github.gtk-rs.examples.RegisterDBusObject /com/github/gtk_rs/examples/HelloWorld com.github.gtk_rs.examples.HelloWorld.GoodBye");
157178
}
158179
}
159180
}

gio/src/subclass/application.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ pub trait ApplicationImpl: ObjectImpl + ApplicationImplExt {
112112
fn dbus_register(&self, connection: &DBusConnection, object_path: &str) -> Result<(), Error> {
113113
self.parent_dbus_register(connection, object_path)
114114
}
115+
116+
fn dbus_unregister(&self, connection: &DBusConnection, object_path: &str) {
117+
self.parent_dbus_unregister(connection, object_path)
118+
}
115119
}
116120

117121
mod sealed {
@@ -300,6 +304,21 @@ pub trait ApplicationImplExt: sealed::Sealed + ObjectSubclass {
300304
}
301305
}
302306
}
307+
308+
fn parent_dbus_unregister(&self, connection: &DBusConnection, object_path: &str) {
309+
unsafe {
310+
let data = Self::type_data();
311+
let parent_class = data.as_ref().parent_class() as *mut ffi::GApplicationClass;
312+
let f = (*parent_class)
313+
.dbus_unregister
314+
.expect("No parent class implementation for \"dbus_unregister\"");
315+
f(
316+
self.obj().unsafe_cast_ref::<Application>().to_glib_none().0,
317+
connection.to_glib_none().0,
318+
object_path.to_glib_none().0,
319+
);
320+
}
321+
}
303322
}
304323

305324
impl<T: ApplicationImpl> ApplicationImplExt for T {}
@@ -320,7 +339,8 @@ unsafe impl<T: ApplicationImpl> IsSubclassable<T> for Application {
320339
klass.shutdown = Some(application_shutdown::<T>);
321340
klass.startup = Some(application_startup::<T>);
322341
klass.handle_local_options = Some(application_handle_local_options::<T>);
323-
klass.dbus_register = Some(application_dbus_register::<T>)
342+
klass.dbus_register = Some(application_dbus_register::<T>);
343+
klass.dbus_unregister = Some(application_dbus_unregister::<T>);
324344
}
325345
}
326346

@@ -448,6 +468,19 @@ unsafe extern "C" fn application_dbus_register<T: ApplicationImpl>(
448468
}
449469
}
450470

471+
unsafe extern "C" fn application_dbus_unregister<T: ApplicationImpl>(
472+
ptr: *mut ffi::GApplication,
473+
connection: *mut ffi::GDBusConnection,
474+
object_path: *const c_char,
475+
) {
476+
let instance = &*(ptr as *mut T::Instance);
477+
let imp = instance.imp();
478+
imp.dbus_unregister(
479+
&from_glib_borrow(connection),
480+
&glib::GString::from_glib_borrow(object_path),
481+
);
482+
}
483+
451484
#[cfg(test)]
452485
mod tests {
453486
use super::*;

0 commit comments

Comments
 (0)