Skip to content

Commit 179386b

Browse files
committed
Bind dbus_unregister vfunc
1 parent dd4c895 commit 179386b

File tree

2 files changed

+65
-18
lines changed

2 files changed

+65
-18
lines changed

examples/gio_dbus_register_object/main.rs

Lines changed: 31 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,33 @@ 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+
#[strong(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+
app.quit();
121+
Ok(None)
122+
}
111123
}
112124
}
113125
}
114-
})
126+
))
115127
.build()
116128
}
117129
}
@@ -140,9 +152,9 @@ mod imp {
140152
Ok(true)
141153
}
142154

143-
fn shutdown(&self) {
155+
fn dbus_unregister(&self, connection: &DBusConnection, object_path: &str) {
156+
self.parent_dbus_unregister(connection, object_path);
144157
if let Some(id) = self.registration_id.take() {
145-
let connection = self.obj().dbus_connection().expect("connection");
146158
if connection.unregister_object(id).is_ok() {
147159
println!("Unregistered object");
148160
} else {
@@ -154,6 +166,8 @@ mod imp {
154166
fn activate(&self) {
155167
println!("Waiting for DBus Hello method to be called. Call the following command from another terminal:");
156168
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");
169+
println!("Quit with the following command:");
170+
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");
157171
}
158172
}
159173
}

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<bool, 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 {
@@ -300,6 +304,21 @@ pub trait ApplicationImplExt: ApplicationImpl {
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

@@ -449,6 +469,19 @@ unsafe extern "C" fn application_dbus_register<T: ApplicationImpl>(
449469
}
450470
}
451471

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

0 commit comments

Comments
 (0)