1use crate::{Cancellable, DBusAuthObserver, DBusConnection, DBusServerFlags, Initable, ffi};
6use glib::{
7 object::ObjectType as _,
8 prelude::*,
9 signal::{SignalHandlerId, connect_raw},
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 unsafe {
111 let f: &F = &*(f as *const F);
112 f(&from_glib_borrow(this), &from_glib_borrow(connection)).into_glib()
113 }
114 }
115 unsafe {
116 let f: Box_<F> = Box_::new(f);
117 connect_raw(
118 self.as_ptr() as *mut _,
119 c"new-connection".as_ptr(),
120 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
121 new_connection_trampoline::<F> as *const (),
122 )),
123 Box_::into_raw(f),
124 )
125 }
126 }
127
128 #[doc(alias = "active")]
129 pub fn connect_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
130 unsafe extern "C" fn notify_active_trampoline<F: Fn(&DBusServer) + 'static>(
131 this: *mut ffi::GDBusServer,
132 _param_spec: glib::ffi::gpointer,
133 f: glib::ffi::gpointer,
134 ) {
135 unsafe {
136 let f: &F = &*(f as *const F);
137 f(&from_glib_borrow(this))
138 }
139 }
140 unsafe {
141 let f: Box_<F> = Box_::new(f);
142 connect_raw(
143 self.as_ptr() as *mut _,
144 c"notify::active".as_ptr(),
145 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
146 notify_active_trampoline::<F> as *const (),
147 )),
148 Box_::into_raw(f),
149 )
150 }
151 }
152
153 #[doc(alias = "client-address")]
154 pub fn connect_client_address_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
155 unsafe extern "C" fn notify_client_address_trampoline<F: Fn(&DBusServer) + 'static>(
156 this: *mut ffi::GDBusServer,
157 _param_spec: glib::ffi::gpointer,
158 f: glib::ffi::gpointer,
159 ) {
160 unsafe {
161 let f: &F = &*(f as *const F);
162 f(&from_glib_borrow(this))
163 }
164 }
165 unsafe {
166 let f: Box_<F> = Box_::new(f);
167 connect_raw(
168 self.as_ptr() as *mut _,
169 c"notify::client-address".as_ptr(),
170 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
171 notify_client_address_trampoline::<F> as *const (),
172 )),
173 Box_::into_raw(f),
174 )
175 }
176 }
177}