Skip to content

Commit 45f3bca

Browse files
committed
Bind dbus_register vfunc
1 parent aaf4d8c commit 45f3bca

File tree

2 files changed

+72
-14
lines changed

2 files changed

+72
-14
lines changed

examples/gio_dbus_register_object/main.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,16 @@ mod imp {
8282
}
8383

8484
impl SampleApplication {
85-
fn register_object(&self, connection: &DBusConnection) {
85+
fn register_object(
86+
&self,
87+
connection: &DBusConnection,
88+
) -> Result<gio::RegistrationId, glib::Error> {
8689
let example = gio::DBusNodeInfo::for_xml(EXAMPLE_XML)
8790
.ok()
8891
.and_then(|e| e.lookup_interface("com.github.gtk_rs.examples.HelloWorld"))
8992
.expect("Example interface");
9093

91-
if let Ok(id) = connection
94+
connection
9295
.register_object("/com/github/gtk_rs/examples/HelloWorld", &example)
9396
.typed_method_call::<HelloMethod>()
9497
.invoke_and_return_future_local(|_, sender, call| {
@@ -110,12 +113,6 @@ mod imp {
110113
}
111114
})
112115
.build()
113-
{
114-
println!("Registered object");
115-
self.registration_id.replace(Some(id));
116-
} else {
117-
eprintln!("Could not register object");
118-
}
119116
}
120117
}
121118

@@ -131,10 +128,16 @@ mod imp {
131128
impl ObjectImpl for SampleApplication {}
132129

133130
impl ApplicationImpl for SampleApplication {
134-
fn startup(&self) {
135-
self.parent_startup();
136-
let connection = self.obj().dbus_connection().expect("connection");
137-
self.register_object(&connection);
131+
fn dbus_register(
132+
&self,
133+
connection: &DBusConnection,
134+
object_path: &str,
135+
) -> Result<(), glib::Error> {
136+
self.parent_dbus_register(connection, object_path)?;
137+
self.registration_id
138+
.replace(Some(self.register_object(connection)?));
139+
println!("registered object on session bus");
140+
Ok(())
138141
}
139142

140143
fn shutdown(&self) {

gio/src/subclass/application.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
use std::{ffi::OsString, fmt, ops::Deref, ptr};
44

5-
use glib::{prelude::*, subclass::prelude::*, translate::*, ExitCode, VariantDict};
5+
use glib::{prelude::*, subclass::prelude::*, translate::*, Error, ExitCode, VariantDict};
66
use libc::{c_char, c_int, c_void};
77

8-
use crate::{ffi, ActionGroup, ActionMap, Application};
8+
use crate::{ffi, ActionGroup, ActionMap, Application, DBusConnection};
99

1010
pub struct ArgumentList {
1111
pub(crate) ptr: *mut *mut *mut c_char,
@@ -110,6 +110,10 @@ pub trait ApplicationImpl:
110110
fn handle_local_options(&self, options: &VariantDict) -> ExitCode {
111111
self.parent_handle_local_options(options)
112112
}
113+
114+
fn dbus_register(&self, connection: &DBusConnection, object_path: &str) -> Result<(), Error> {
115+
self.parent_dbus_register(connection, object_path)
116+
}
113117
}
114118

115119
pub trait ApplicationImplExt: ApplicationImpl {
@@ -266,6 +270,33 @@ pub trait ApplicationImplExt: ApplicationImpl {
266270
}
267271
}
268272
}
273+
274+
fn parent_dbus_register(
275+
&self,
276+
connection: &DBusConnection,
277+
object_path: &str,
278+
) -> Result<(), glib::Error> {
279+
unsafe {
280+
let data = Self::type_data();
281+
let parent_class = data.as_ref().parent_class() as *mut ffi::GApplicationClass;
282+
let f = (*parent_class)
283+
.dbus_register
284+
.expect("No parent class implementation for \"dbus_register\"");
285+
let mut err = ptr::null_mut();
286+
let res = f(
287+
self.obj().unsafe_cast_ref::<Application>().to_glib_none().0,
288+
connection.to_glib_none().0,
289+
object_path.to_glib_none().0,
290+
&mut err,
291+
);
292+
if res == glib::ffi::GFALSE {
293+
Err(from_glib_full(err))
294+
} else {
295+
debug_assert!(err.is_null());
296+
Ok(())
297+
}
298+
}
299+
}
269300
}
270301

271302
impl<T: ApplicationImpl> ApplicationImplExt for T {}
@@ -286,6 +317,7 @@ unsafe impl<T: ApplicationImpl> IsSubclassable<T> for Application {
286317
klass.shutdown = Some(application_shutdown::<T>);
287318
klass.startup = Some(application_startup::<T>);
288319
klass.handle_local_options = Some(application_handle_local_options::<T>);
320+
klass.dbus_register = Some(application_dbus_register::<T>)
289321
}
290322
}
291323

@@ -390,6 +422,29 @@ unsafe extern "C" fn application_handle_local_options<T: ApplicationImpl>(
390422
imp.handle_local_options(&from_glib_borrow(options)).into()
391423
}
392424

425+
unsafe extern "C" fn application_dbus_register<T: ApplicationImpl>(
426+
ptr: *mut ffi::GApplication,
427+
connection: *mut ffi::GDBusConnection,
428+
object_path: *const c_char,
429+
error: *mut *mut glib::ffi::GError,
430+
) -> glib::ffi::gboolean {
431+
let instance = &*(ptr as *mut T::Instance);
432+
let imp = instance.imp();
433+
434+
match imp.dbus_register(
435+
&from_glib_borrow(connection),
436+
&glib::GString::from_glib_borrow(object_path),
437+
) {
438+
Ok(()) => glib::ffi::GTRUE,
439+
Err(e) => {
440+
if !error.is_null() {
441+
*error = e.into_glib_ptr();
442+
}
443+
glib::ffi::GFALSE
444+
}
445+
}
446+
}
447+
393448
#[cfg(test)]
394449
mod tests {
395450
use super::*;

0 commit comments

Comments
 (0)