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
27mod sealed {
28 pub trait Sealed {}
29 impl<T: super::IsA<super::DBusObject>> Sealed for T {}
30}
31
32pub trait DBusObjectExt: IsA<DBusObject> + sealed::Sealed + 'static {
33 #[doc(alias = "g_dbus_object_get_interface")]
34 #[doc(alias = "get_interface")]
35 fn interface(&self, interface_name: &str) -> Option<DBusInterface> {
36 unsafe {
37 from_glib_full(ffi::g_dbus_object_get_interface(
38 self.as_ref().to_glib_none().0,
39 interface_name.to_glib_none().0,
40 ))
41 }
42 }
43
44 #[doc(alias = "g_dbus_object_get_interfaces")]
45 #[doc(alias = "get_interfaces")]
46 fn interfaces(&self) -> Vec<DBusInterface> {
47 unsafe {
48 FromGlibPtrContainer::from_glib_full(ffi::g_dbus_object_get_interfaces(
49 self.as_ref().to_glib_none().0,
50 ))
51 }
52 }
53
54 #[doc(alias = "g_dbus_object_get_object_path")]
55 #[doc(alias = "get_object_path")]
56 fn object_path(&self) -> glib::GString {
57 unsafe {
58 from_glib_none(ffi::g_dbus_object_get_object_path(
59 self.as_ref().to_glib_none().0,
60 ))
61 }
62 }
63
64 #[doc(alias = "interface-added")]
65 fn connect_interface_added<F: Fn(&Self, &DBusInterface) + 'static>(
66 &self,
67 f: F,
68 ) -> SignalHandlerId {
69 unsafe extern "C" fn interface_added_trampoline<
70 P: IsA<DBusObject>,
71 F: Fn(&P, &DBusInterface) + 'static,
72 >(
73 this: *mut ffi::GDBusObject,
74 interface: *mut ffi::GDBusInterface,
75 f: glib::ffi::gpointer,
76 ) {
77 let f: &F = &*(f as *const F);
78 f(
79 DBusObject::from_glib_borrow(this).unsafe_cast_ref(),
80 &from_glib_borrow(interface),
81 )
82 }
83 unsafe {
84 let f: Box_<F> = Box_::new(f);
85 connect_raw(
86 self.as_ptr() as *mut _,
87 b"interface-added\0".as_ptr() as *const _,
88 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
89 interface_added_trampoline::<Self, F> as *const (),
90 )),
91 Box_::into_raw(f),
92 )
93 }
94 }
95
96 #[doc(alias = "interface-removed")]
97 fn connect_interface_removed<F: Fn(&Self, &DBusInterface) + 'static>(
98 &self,
99 f: F,
100 ) -> SignalHandlerId {
101 unsafe extern "C" fn interface_removed_trampoline<
102 P: IsA<DBusObject>,
103 F: Fn(&P, &DBusInterface) + 'static,
104 >(
105 this: *mut ffi::GDBusObject,
106 interface: *mut ffi::GDBusInterface,
107 f: glib::ffi::gpointer,
108 ) {
109 let f: &F = &*(f as *const F);
110 f(
111 DBusObject::from_glib_borrow(this).unsafe_cast_ref(),
112 &from_glib_borrow(interface),
113 )
114 }
115 unsafe {
116 let f: Box_<F> = Box_::new(f);
117 connect_raw(
118 self.as_ptr() as *mut _,
119 b"interface-removed\0".as_ptr() as *const _,
120 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
121 interface_removed_trampoline::<Self, F> as *const (),
122 )),
123 Box_::into_raw(f),
124 )
125 }
126 }
127}
128
129impl<O: IsA<DBusObject>> DBusObjectExt for O {}