1use crate::{ffi, DBusInterface};
6use glib::{
7 object::ObjectType as _,
8 prelude::*,
9 signal::{connect_raw, SignalHandlerId},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 #[doc(alias = "GDBusObject")]
16 pub struct DBusObject(Interface<ffi::GDBusObject, ffi::GDBusObjectIface>);
17
18 match fn {
19 type_ => || ffi::g_dbus_object_get_type(),
20 }
21}
22
23impl DBusObject {
24 pub const NONE: Option<&'static DBusObject> = None;
25}
26
27pub trait DBusObjectExt: IsA<DBusObject> + 'static {
28 #[doc(alias = "g_dbus_object_get_interface")]
29 #[doc(alias = "get_interface")]
30 fn interface(&self, interface_name: &str) -> Option<DBusInterface> {
31 unsafe {
32 from_glib_full(ffi::g_dbus_object_get_interface(
33 self.as_ref().to_glib_none().0,
34 interface_name.to_glib_none().0,
35 ))
36 }
37 }
38
39 #[doc(alias = "g_dbus_object_get_interfaces")]
40 #[doc(alias = "get_interfaces")]
41 fn interfaces(&self) -> Vec<DBusInterface> {
42 unsafe {
43 FromGlibPtrContainer::from_glib_full(ffi::g_dbus_object_get_interfaces(
44 self.as_ref().to_glib_none().0,
45 ))
46 }
47 }
48
49 #[doc(alias = "g_dbus_object_get_object_path")]
50 #[doc(alias = "get_object_path")]
51 fn object_path(&self) -> glib::GString {
52 unsafe {
53 from_glib_none(ffi::g_dbus_object_get_object_path(
54 self.as_ref().to_glib_none().0,
55 ))
56 }
57 }
58
59 #[doc(alias = "interface-added")]
60 fn connect_interface_added<F: Fn(&Self, &DBusInterface) + 'static>(
61 &self,
62 f: F,
63 ) -> SignalHandlerId {
64 unsafe extern "C" fn interface_added_trampoline<
65 P: IsA<DBusObject>,
66 F: Fn(&P, &DBusInterface) + 'static,
67 >(
68 this: *mut ffi::GDBusObject,
69 interface: *mut ffi::GDBusInterface,
70 f: glib::ffi::gpointer,
71 ) {
72 let f: &F = &*(f as *const F);
73 f(
74 DBusObject::from_glib_borrow(this).unsafe_cast_ref(),
75 &from_glib_borrow(interface),
76 )
77 }
78 unsafe {
79 let f: Box_<F> = Box_::new(f);
80 connect_raw(
81 self.as_ptr() as *mut _,
82 c"interface-added".as_ptr() as *const _,
83 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
84 interface_added_trampoline::<Self, F> as *const (),
85 )),
86 Box_::into_raw(f),
87 )
88 }
89 }
90
91 #[doc(alias = "interface-removed")]
92 fn connect_interface_removed<F: Fn(&Self, &DBusInterface) + 'static>(
93 &self,
94 f: F,
95 ) -> SignalHandlerId {
96 unsafe extern "C" fn interface_removed_trampoline<
97 P: IsA<DBusObject>,
98 F: Fn(&P, &DBusInterface) + 'static,
99 >(
100 this: *mut ffi::GDBusObject,
101 interface: *mut ffi::GDBusInterface,
102 f: glib::ffi::gpointer,
103 ) {
104 let f: &F = &*(f as *const F);
105 f(
106 DBusObject::from_glib_borrow(this).unsafe_cast_ref(),
107 &from_glib_borrow(interface),
108 )
109 }
110 unsafe {
111 let f: Box_<F> = Box_::new(f);
112 connect_raw(
113 self.as_ptr() as *mut _,
114 c"interface-removed".as_ptr() as *const _,
115 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
116 interface_removed_trampoline::<Self, F> as *const (),
117 )),
118 Box_::into_raw(f),
119 )
120 }
121 }
122}
123
124impl<O: IsA<DBusObject>> DBusObjectExt for O {}