Skip to content

Commit 6fa1871

Browse files
swsnrsdroege
authored andcommitted
Bind dbus_register vfunc
1 parent 8a5c50c commit 6fa1871

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, Application};
8+
use crate::{ffi, Application, DBusConnection};
99

1010
pub struct ArgumentList {
1111
pub(crate) ptr: *mut *mut *mut c_char,
@@ -108,6 +108,10 @@ pub trait ApplicationImpl: ObjectImpl + ApplicationImplExt {
108108
fn handle_local_options(&self, options: &VariantDict) -> ExitCode {
109109
self.parent_handle_local_options(options)
110110
}
111+
112+
fn dbus_register(&self, connection: &DBusConnection, object_path: &str) -> Result<(), Error> {
113+
self.parent_dbus_register(connection, object_path)
114+
}
111115
}
112116

113117
mod sealed {
@@ -269,6 +273,33 @@ pub trait ApplicationImplExt: sealed::Sealed + ObjectSubclass {
269273
}
270274
}
271275
}
276+
277+
fn parent_dbus_register(
278+
&self,
279+
connection: &DBusConnection,
280+
object_path: &str,
281+
) -> Result<(), glib::Error> {
282+
unsafe {
283+
let data = Self::type_data();
284+
let parent_class = data.as_ref().parent_class() as *mut ffi::GApplicationClass;
285+
let f = (*parent_class)
286+
.dbus_register
287+
.expect("No parent class implementation for \"dbus_register\"");
288+
let mut err = ptr::null_mut();
289+
let res = f(
290+
self.obj().unsafe_cast_ref::<Application>().to_glib_none().0,
291+
connection.to_glib_none().0,
292+
object_path.to_glib_none().0,
293+
&mut err,
294+
);
295+
if res == glib::ffi::GFALSE {
296+
Err(from_glib_full(err))
297+
} else {
298+
debug_assert!(err.is_null());
299+
Ok(())
300+
}
301+
}
302+
}
272303
}
273304

274305
impl<T: ApplicationImpl> ApplicationImplExt for T {}
@@ -289,6 +320,7 @@ unsafe impl<T: ApplicationImpl> IsSubclassable<T> for Application {
289320
klass.shutdown = Some(application_shutdown::<T>);
290321
klass.startup = Some(application_startup::<T>);
291322
klass.handle_local_options = Some(application_handle_local_options::<T>);
323+
klass.dbus_register = Some(application_dbus_register::<T>)
292324
}
293325
}
294326

@@ -393,6 +425,29 @@ unsafe extern "C" fn application_handle_local_options<T: ApplicationImpl>(
393425
imp.handle_local_options(&from_glib_borrow(options)).into()
394426
}
395427

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

0 commit comments

Comments
 (0)