1use crate::{ffi, Cancellable, DBusAuthObserver, DBusConnection, DBusServerFlags, Initable};
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 = "GDBusServer")]
16 pub struct DBusServer(Object<ffi::GDBusServer>) @implements Initable;
17
18 match fn {
19 type_ => || ffi::g_dbus_server_get_type(),
20 }
21}
22
23impl DBusServer {
24 #[doc(alias = "g_dbus_server_new_sync")]
25 pub fn new_sync(
26 address: &str,
27 flags: DBusServerFlags,
28 guid: &str,
29 observer: Option<&DBusAuthObserver>,
30 cancellable: Option<&impl IsA<Cancellable>>,
31 ) -> Result<DBusServer, glib::Error> {
32 unsafe {
33 let mut error = std::ptr::null_mut();
34 let ret = ffi::g_dbus_server_new_sync(
35 address.to_glib_none().0,
36 flags.into_glib(),
37 guid.to_glib_none().0,
38 observer.to_glib_none().0,
39 cancellable.map(|p| p.as_ref()).to_glib_none().0,
40 &mut error,
41 );
42 if error.is_null() {
43 Ok(from_glib_full(ret))
44 } else {
45 Err(from_glib_full(error))
46 }
47 }
48 }
49
50 #[doc(alias = "g_dbus_server_get_client_address")]
51 #[doc(alias = "get_client_address")]
52 #[doc(alias = "client-address")]
53 pub fn client_address(&self) -> glib::GString {
54 unsafe { from_glib_none(ffi::g_dbus_server_get_client_address(self.to_glib_none().0)) }
55 }
56
57 #[doc(alias = "g_dbus_server_get_flags")]
58 #[doc(alias = "get_flags")]
59 pub fn flags(&self) -> DBusServerFlags {
60 unsafe { from_glib(ffi::g_dbus_server_get_flags(self.to_glib_none().0)) }
61 }
62
63 #[doc(alias = "g_dbus_server_get_guid")]
64 #[doc(alias = "get_guid")]
65 pub fn guid(&self) -> glib::GString {
66 unsafe { from_glib_none(ffi::g_dbus_server_get_guid(self.to_glib_none().0)) }
67 }
68
69 #[doc(alias = "g_dbus_server_is_active")]
70 #[doc(alias = "active")]
71 pub fn is_active(&self) -> bool {
72 unsafe { from_glib(ffi::g_dbus_server_is_active(self.to_glib_none().0)) }
73 }
74
75 #[doc(alias = "g_dbus_server_start")]
76 pub fn start(&self) {
77 unsafe {
78 ffi::g_dbus_server_start(self.to_glib_none().0);
79 }
80 }
81
82 #[doc(alias = "g_dbus_server_stop")]
83 pub fn stop(&self) {
84 unsafe {
85 ffi::g_dbus_server_stop(self.to_glib_none().0);
86 }
87 }
88
89 pub fn address(&self) -> Option<glib::GString> {
90 ObjectExt::property(self, "address")
91 }
92
93 #[doc(alias = "authentication-observer")]
94 pub fn authentication_observer(&self) -> Option<DBusAuthObserver> {
95 ObjectExt::property(self, "authentication-observer")
96 }
97
98 #[doc(alias = "new-connection")]
99 pub fn connect_new_connection<F: Fn(&Self, &DBusConnection) -> bool + 'static>(
100 &self,
101 f: F,
102 ) -> SignalHandlerId {
103 unsafe extern "C" fn new_connection_trampoline<
104 F: Fn(&DBusServer, &DBusConnection) -> bool + 'static,
105 >(
106 this: *mut ffi::GDBusServer,
107 connection: *mut ffi::GDBusConnection,
108 f: glib::ffi::gpointer,
109 ) -> glib::ffi::gboolean {
110 let f: &F = &*(f as *const F);
111 f(&from_glib_borrow(this), &from_glib_borrow(connection)).into_glib()
112 }
113 unsafe {
114 let f: Box_<F> = Box_::new(f);
115 connect_raw(
116 self.as_ptr() as *mut _,
117 c"new-connection".as_ptr() as *const _,
118 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
119 new_connection_trampoline::<F> as *const (),
120 )),
121 Box_::into_raw(f),
122 )
123 }
124 }
125
126 #[doc(alias = "active")]
127 pub fn connect_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
128 unsafe extern "C" fn notify_active_trampoline<F: Fn(&DBusServer) + 'static>(
129 this: *mut ffi::GDBusServer,
130 _param_spec: glib::ffi::gpointer,
131 f: glib::ffi::gpointer,
132 ) {
133 let f: &F = &*(f as *const F);
134 f(&from_glib_borrow(this))
135 }
136 unsafe {
137 let f: Box_<F> = Box_::new(f);
138 connect_raw(
139 self.as_ptr() as *mut _,
140 c"notify::active".as_ptr() as *const _,
141 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
142 notify_active_trampoline::<F> as *const (),
143 )),
144 Box_::into_raw(f),
145 )
146 }
147 }
148
149 #[doc(alias = "client-address")]
150 pub fn connect_client_address_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
151 unsafe extern "C" fn notify_client_address_trampoline<F: Fn(&DBusServer) + 'static>(
152 this: *mut ffi::GDBusServer,
153 _param_spec: glib::ffi::gpointer,
154 f: glib::ffi::gpointer,
155 ) {
156 let f: &F = &*(f as *const F);
157 f(&from_glib_borrow(this))
158 }
159 unsafe {
160 let f: Box_<F> = Box_::new(f);
161 connect_raw(
162 self.as_ptr() as *mut _,
163 c"notify::client-address".as_ptr() as *const _,
164 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
165 notify_client_address_trampoline::<F> as *const (),
166 )),
167 Box_::into_raw(f),
168 )
169 }
170 }
171}