gio/auto/dbus_server.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use 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 /// `GDBusServer` is a helper for listening to and accepting D-Bus
16 /// connections. This can be used to create a new D-Bus server, allowing two
17 /// peers to use the D-Bus protocol for their own specialized communication.
18 /// A server instance provided in this way will not perform message routing or
19 /// implement the
20 /// [`org.freedesktop.DBus` interface](https://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-messages).
21 ///
22 /// To just export an object on a well-known name on a message bus, such as the
23 /// session or system bus, you should instead use `bus_own_name()`.
24 ///
25 /// An example of peer-to-peer communication with GDBus can be found
26 /// in [gdbus-example-peer.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-peer.c).
27 ///
28 /// Note that a minimal `GDBusServer` will accept connections from any
29 /// peer. In many use-cases it will be necessary to add a
30 /// [`DBusAuthObserver`][crate::DBusAuthObserver] that only accepts connections that have
31 /// successfully authenticated as the same user that is running the
32 /// `GDBusServer`. Since GLib 2.68 this can be achieved more simply by passing
33 /// the `G_DBUS_SERVER_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER` flag to the
34 /// server.
35 ///
36 /// ## Properties
37 ///
38 ///
39 /// #### `active`
40 /// Whether the server is currently active.
41 ///
42 /// Readable
43 ///
44 ///
45 /// #### `address`
46 /// The D-Bus address to listen on.
47 ///
48 /// Readable | Writable | Construct Only
49 ///
50 ///
51 /// #### `authentication-observer`
52 /// A #GDBusAuthObserver object to assist in the authentication process or [`None`].
53 ///
54 /// Readable | Writable | Construct Only
55 ///
56 ///
57 /// #### `client-address`
58 /// The D-Bus address that clients can use.
59 ///
60 /// Readable
61 ///
62 ///
63 /// #### `flags`
64 /// Flags from the #GDBusServerFlags enumeration.
65 ///
66 /// Readable | Writable | Construct Only
67 ///
68 ///
69 /// #### `guid`
70 /// The GUID of the server.
71 ///
72 /// See #GDBusConnection:guid for more details.
73 ///
74 /// Readable | Writable | Construct Only
75 ///
76 ///
77 /// #### `max-message-unix-fds`
78 /// The maximum number of Unix file descriptors that may be received in a
79 /// single D-Bus message on connections accepted by the server.
80 ///
81 /// Readable | Writable | Construct Only
82 ///
83 /// ## Signals
84 ///
85 ///
86 /// #### `new-connection`
87 /// Emitted when a new authenticated connection has been made. Use
88 /// g_dbus_connection_get_peer_credentials() to figure out what
89 /// identity (if any), was authenticated.
90 ///
91 /// If you want to accept the connection, take a reference to the
92 /// @connection object and return [`true`]. When you are done with the
93 /// connection call g_dbus_connection_close() and give up your
94 /// reference. Note that the other peer may disconnect at any time -
95 /// a typical thing to do when accepting a connection is to listen to
96 /// the #GDBusConnection::closed signal.
97 ///
98 /// If #GDBusServer:flags contains [`DBusServerFlags::RUN_IN_THREAD`][crate::DBusServerFlags::RUN_IN_THREAD]
99 /// then the signal is emitted in a new thread dedicated to the
100 /// connection. Otherwise the signal is emitted in the thread-default
101 /// main context (see [`glib::MainContext::push_thread_default()`][crate::glib::MainContext::push_thread_default()])
102 /// of the thread that @server was constructed in.
103 ///
104 /// You are guaranteed that signal handlers for this signal runs
105 /// before incoming messages on @connection are processed. This means
106 /// that it's suitable to call g_dbus_connection_register_object() or
107 /// similar from the signal handler.
108 ///
109 ///
110 ///
111 /// # Implements
112 ///
113 /// [`trait@glib::ObjectExt`], [`InitableExt`][trait@crate::prelude::InitableExt]
114 #[doc(alias = "GDBusServer")]
115 pub struct DBusServer(Object<ffi::GDBusServer>) @implements Initable;
116
117 match fn {
118 type_ => || ffi::g_dbus_server_get_type(),
119 }
120}
121
122impl DBusServer {
123 /// Creates a new D-Bus server that listens on the first address in
124 /// @address that works.
125 ///
126 /// Once constructed, you can use g_dbus_server_get_client_address() to
127 /// get a D-Bus address string that clients can use to connect.
128 ///
129 /// To have control over the available authentication mechanisms and
130 /// the users that are authorized to connect, it is strongly recommended
131 /// to provide a non-[`None`] #GDBusAuthObserver.
132 ///
133 /// Connect to the #GDBusServer::new-connection signal to handle
134 /// incoming connections.
135 ///
136 /// The returned #GDBusServer isn't active - you have to start it with
137 /// g_dbus_server_start().
138 ///
139 /// #GDBusServer is used in this [example](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-peer.c).
140 ///
141 /// This is a synchronous failable constructor. There is currently no
142 /// asynchronous version.
143 /// ## `address`
144 /// A D-Bus address.
145 /// ## `flags`
146 /// Flags from the #GDBusServerFlags enumeration.
147 /// ## `guid`
148 /// A D-Bus GUID.
149 /// ## `observer`
150 /// A #GDBusAuthObserver or [`None`].
151 /// ## `cancellable`
152 /// A #GCancellable or [`None`].
153 ///
154 /// # Returns
155 ///
156 /// A #GDBusServer or [`None`] if @error is set. Free with
157 /// g_object_unref().
158 #[doc(alias = "g_dbus_server_new_sync")]
159 pub fn new_sync(
160 address: &str,
161 flags: DBusServerFlags,
162 guid: &str,
163 observer: Option<&DBusAuthObserver>,
164 cancellable: Option<&impl IsA<Cancellable>>,
165 ) -> Result<DBusServer, glib::Error> {
166 unsafe {
167 let mut error = std::ptr::null_mut();
168 let ret = ffi::g_dbus_server_new_sync(
169 address.to_glib_none().0,
170 flags.into_glib(),
171 guid.to_glib_none().0,
172 observer.to_glib_none().0,
173 cancellable.map(|p| p.as_ref()).to_glib_none().0,
174 &mut error,
175 );
176 if error.is_null() {
177 Ok(from_glib_full(ret))
178 } else {
179 Err(from_glib_full(error))
180 }
181 }
182 }
183
184 /// Gets a
185 /// [D-Bus address](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses)
186 /// string that can be used by clients to connect to @self.
187 ///
188 /// This is valid and non-empty if initializing the #GDBusServer succeeded.
189 ///
190 /// # Returns
191 ///
192 /// A D-Bus address string. Do not free, the string is owned
193 /// by @self.
194 #[doc(alias = "g_dbus_server_get_client_address")]
195 #[doc(alias = "get_client_address")]
196 #[doc(alias = "client-address")]
197 pub fn client_address(&self) -> glib::GString {
198 unsafe { from_glib_none(ffi::g_dbus_server_get_client_address(self.to_glib_none().0)) }
199 }
200
201 /// Gets the flags for @self.
202 ///
203 /// # Returns
204 ///
205 /// A set of flags from the #GDBusServerFlags enumeration.
206 #[doc(alias = "g_dbus_server_get_flags")]
207 #[doc(alias = "get_flags")]
208 pub fn flags(&self) -> DBusServerFlags {
209 unsafe { from_glib(ffi::g_dbus_server_get_flags(self.to_glib_none().0)) }
210 }
211
212 /// Gets the GUID for @self, as provided to g_dbus_server_new_sync().
213 ///
214 /// # Returns
215 ///
216 /// A D-Bus GUID. Do not free this string, it is owned by @self.
217 #[doc(alias = "g_dbus_server_get_guid")]
218 #[doc(alias = "get_guid")]
219 pub fn guid(&self) -> glib::GString {
220 unsafe { from_glib_none(ffi::g_dbus_server_get_guid(self.to_glib_none().0)) }
221 }
222
223 /// Gets whether @self is active.
224 ///
225 /// # Returns
226 ///
227 /// [`true`] if server is active, [`false`] otherwise.
228 #[doc(alias = "g_dbus_server_is_active")]
229 #[doc(alias = "active")]
230 pub fn is_active(&self) -> bool {
231 unsafe { from_glib(ffi::g_dbus_server_is_active(self.to_glib_none().0)) }
232 }
233
234 /// Starts @self.
235 #[doc(alias = "g_dbus_server_start")]
236 pub fn start(&self) {
237 unsafe {
238 ffi::g_dbus_server_start(self.to_glib_none().0);
239 }
240 }
241
242 /// Stops @self.
243 #[doc(alias = "g_dbus_server_stop")]
244 pub fn stop(&self) {
245 unsafe {
246 ffi::g_dbus_server_stop(self.to_glib_none().0);
247 }
248 }
249
250 /// The D-Bus address to listen on.
251 pub fn address(&self) -> Option<glib::GString> {
252 ObjectExt::property(self, "address")
253 }
254
255 /// A #GDBusAuthObserver object to assist in the authentication process or [`None`].
256 #[doc(alias = "authentication-observer")]
257 pub fn authentication_observer(&self) -> Option<DBusAuthObserver> {
258 ObjectExt::property(self, "authentication-observer")
259 }
260
261 /// The maximum number of Unix file descriptors that may be received in a
262 /// single D-Bus message on connections accepted by the server.
263 #[cfg(feature = "v2_92")]
264 #[cfg_attr(docsrs, doc(cfg(feature = "v2_92")))]
265 #[doc(alias = "max-message-unix-fds")]
266 pub fn max_message_unix_fds(&self) -> u32 {
267 ObjectExt::property(self, "max-message-unix-fds")
268 }
269
270 /// Emitted when a new authenticated connection has been made. Use
271 /// g_dbus_connection_get_peer_credentials() to figure out what
272 /// identity (if any), was authenticated.
273 ///
274 /// If you want to accept the connection, take a reference to the
275 /// @connection object and return [`true`]. When you are done with the
276 /// connection call g_dbus_connection_close() and give up your
277 /// reference. Note that the other peer may disconnect at any time -
278 /// a typical thing to do when accepting a connection is to listen to
279 /// the #GDBusConnection::closed signal.
280 ///
281 /// If #GDBusServer:flags contains [`DBusServerFlags::RUN_IN_THREAD`][crate::DBusServerFlags::RUN_IN_THREAD]
282 /// then the signal is emitted in a new thread dedicated to the
283 /// connection. Otherwise the signal is emitted in the thread-default
284 /// main context (see [`glib::MainContext::push_thread_default()`][crate::glib::MainContext::push_thread_default()])
285 /// of the thread that @server was constructed in.
286 ///
287 /// You are guaranteed that signal handlers for this signal runs
288 /// before incoming messages on @connection are processed. This means
289 /// that it's suitable to call g_dbus_connection_register_object() or
290 /// similar from the signal handler.
291 /// ## `connection`
292 /// A #GDBusConnection for the new connection.
293 ///
294 /// # Returns
295 ///
296 /// [`true`] to claim @connection, [`false`] to let other handlers
297 /// run.
298 #[doc(alias = "new-connection")]
299 pub fn connect_new_connection<F: Fn(&Self, &DBusConnection) -> bool + 'static>(
300 &self,
301 f: F,
302 ) -> SignalHandlerId {
303 unsafe extern "C" fn new_connection_trampoline<
304 F: Fn(&DBusServer, &DBusConnection) -> bool + 'static,
305 >(
306 this: *mut ffi::GDBusServer,
307 connection: *mut ffi::GDBusConnection,
308 f: glib::ffi::gpointer,
309 ) -> glib::ffi::gboolean {
310 unsafe {
311 let f: &F = &*(f as *const F);
312 f(&from_glib_borrow(this), &from_glib_borrow(connection)).into_glib()
313 }
314 }
315 unsafe {
316 let f: Box_<F> = Box_::new(f);
317 connect_raw(
318 self.as_ptr() as *mut _,
319 c"new-connection".as_ptr(),
320 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
321 new_connection_trampoline::<F> as *const (),
322 )),
323 Box_::into_raw(f),
324 )
325 }
326 }
327
328 #[doc(alias = "active")]
329 pub fn connect_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
330 unsafe extern "C" fn notify_active_trampoline<F: Fn(&DBusServer) + 'static>(
331 this: *mut ffi::GDBusServer,
332 _param_spec: glib::ffi::gpointer,
333 f: glib::ffi::gpointer,
334 ) {
335 unsafe {
336 let f: &F = &*(f as *const F);
337 f(&from_glib_borrow(this))
338 }
339 }
340 unsafe {
341 let f: Box_<F> = Box_::new(f);
342 connect_raw(
343 self.as_ptr() as *mut _,
344 c"notify::active".as_ptr(),
345 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
346 notify_active_trampoline::<F> as *const (),
347 )),
348 Box_::into_raw(f),
349 )
350 }
351 }
352
353 #[doc(alias = "client-address")]
354 pub fn connect_client_address_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
355 unsafe extern "C" fn notify_client_address_trampoline<F: Fn(&DBusServer) + 'static>(
356 this: *mut ffi::GDBusServer,
357 _param_spec: glib::ffi::gpointer,
358 f: glib::ffi::gpointer,
359 ) {
360 unsafe {
361 let f: &F = &*(f as *const F);
362 f(&from_glib_borrow(this))
363 }
364 }
365 unsafe {
366 let f: Box_<F> = Box_::new(f);
367 connect_raw(
368 self.as_ptr() as *mut _,
369 c"notify::client-address".as_ptr(),
370 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
371 notify_client_address_trampoline::<F> as *const (),
372 )),
373 Box_::into_raw(f),
374 )
375 }
376 }
377}