Skip to content

Commit 7e0bed4

Browse files
committed
Bind dbus_unregister vfunc
1 parent 45f3bca commit 7e0bed4

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
@@ -114,6 +114,10 @@ pub trait ApplicationImpl:
114114
fn dbus_register(&self, connection: &DBusConnection, object_path: &str) -> Result<(), Error> {
115115
self.parent_dbus_register(connection, object_path)
116116
}
117+
118+
fn dbus_unregister(&self, connection: &DBusConnection, object_path: &str) {
119+
self.parent_dbus_unregister(connection, object_path)
120+
}
117121
}
118122

119123
pub trait ApplicationImplExt: ApplicationImpl {
@@ -297,6 +301,21 @@ pub trait ApplicationImplExt: ApplicationImpl {
297301
}
298302
}
299303
}
304+
305+
fn parent_dbus_unregister(&self, connection: &DBusConnection, object_path: &str) {
306+
unsafe {
307+
let data = Self::type_data();
308+
let parent_class = data.as_ref().parent_class() as *mut ffi::GApplicationClass;
309+
let f = (*parent_class)
310+
.dbus_unregister
311+
.expect("No parent class implementation for \"dbus_unregister\"");
312+
f(
313+
self.obj().unsafe_cast_ref::<Application>().to_glib_none().0,
314+
connection.to_glib_none().0,
315+
object_path.to_glib_none().0,
316+
);
317+
}
318+
}
300319
}
301320

302321
impl<T: ApplicationImpl> ApplicationImplExt for T {}
@@ -317,7 +336,8 @@ unsafe impl<T: ApplicationImpl> IsSubclassable<T> for Application {
317336
klass.shutdown = Some(application_shutdown::<T>);
318337
klass.startup = Some(application_startup::<T>);
319338
klass.handle_local_options = Some(application_handle_local_options::<T>);
320-
klass.dbus_register = Some(application_dbus_register::<T>)
339+
klass.dbus_register = Some(application_dbus_register::<T>);
340+
klass.dbus_unregister = Some(application_dbus_unregister::<T>);
321341
}
322342
}
323343

@@ -445,6 +465,19 @@ unsafe extern "C" fn application_dbus_register<T: ApplicationImpl>(
445465
}
446466
}
447467

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

0 commit comments

Comments
 (0)