gio/auto/dbus_connection.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#![allow(deprecated)]
5
6#[cfg(unix)]
7#[cfg_attr(docsrs, doc(cfg(unix)))]
8use crate::UnixFDList;
9use crate::{
10 AsyncInitable, AsyncResult, Cancellable, Credentials, DBusAuthObserver, DBusCallFlags,
11 DBusCapabilityFlags, DBusConnectionFlags, DBusMessage, DBusSendMessageFlags, IOStream,
12 Initable, ffi,
13};
14use glib::{
15 object::ObjectType as _,
16 prelude::*,
17 signal::{SignalHandlerId, connect_raw},
18 translate::*,
19};
20use std::{boxed::Box as Box_, pin::Pin};
21
22glib::wrapper! {
23 /// The `GDBusConnection` type is used for D-Bus connections to remote
24 /// peers such as a message buses.
25 ///
26 /// It is a low-level API that offers a lot of flexibility. For instance,
27 /// it lets you establish a connection over any transport that can by represented
28 /// as a [`IOStream`][crate::IOStream].
29 ///
30 /// This class is rarely used directly in D-Bus clients. If you are writing
31 /// a D-Bus client, it is often easier to use the `bus_own_name()`,
32 /// `bus_watch_name()` or [`DBusProxy::for_bus()`][crate::DBusProxy::for_bus()] APIs.
33 ///
34 /// As an exception to the usual GLib rule that a particular object must not
35 /// be used by two threads at the same time, `GDBusConnection`s methods may be
36 /// called from any thread. This is so that [`bus_get()`][crate::bus_get()] and
37 /// [`bus_get_sync()`][crate::bus_get_sync()] can safely return the same `GDBusConnection` when
38 /// called from any thread.
39 ///
40 /// Most of the ways to obtain a `GDBusConnection` automatically initialize it
41 /// (i.e. connect to D-Bus): for instance, [`new()`][Self::new()] and
42 /// [`bus_get()`][crate::bus_get()], and the synchronous versions of those methods, give you
43 /// an initialized connection. Language bindings for GIO should use
44 /// [`Initable::new()`][crate::Initable::new()] or [`AsyncInitable::new_async()`][crate::AsyncInitable::new_async()], which also
45 /// initialize the connection.
46 ///
47 /// If you construct an uninitialized `GDBusConnection`, such as via
48 /// [`glib::Object::new()`][crate::glib::Object::new()], you must initialize it via [`InitableExt::init()`][crate::prelude::InitableExt::init()] or
49 /// [`AsyncInitableExt::init_async()`][crate::prelude::AsyncInitableExt::init_async()] before using its methods or properties.
50 /// Calling methods or accessing properties on a `GDBusConnection` that has not
51 /// completed initialization successfully is considered to be invalid, and leads
52 /// to undefined behaviour. In particular, if initialization fails with a
53 /// `GError`, the only valid thing you can do with that `GDBusConnection` is to
54 /// free it with `GObject::Object::unref()`.
55 ///
56 /// ## An example D-Bus server
57 ///
58 /// Here is an example for a D-Bus server:
59 /// [gdbus-example-server.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-server.c)
60 ///
61 /// ## An example for exporting a subtree
62 ///
63 /// Here is an example for exporting a subtree:
64 /// [gdbus-example-subtree.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-subtree.c)
65 ///
66 /// ## An example for file descriptor passing
67 ///
68 /// Here is an example for passing UNIX file descriptors:
69 /// [gdbus-unix-fd-client.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-unix-fd-client.c)
70 ///
71 /// ## An example for exporting a GObject
72 ///
73 /// Here is an example for exporting a #GObject:
74 /// [gdbus-example-export.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-export.c)
75 ///
76 /// ## Properties
77 ///
78 ///
79 /// #### `address`
80 /// A D-Bus address specifying potential endpoints that can be used
81 /// when establishing the connection.
82 ///
83 /// Writable | Construct Only
84 ///
85 ///
86 /// #### `authentication-observer`
87 /// A #GDBusAuthObserver object to assist in the authentication process or [`None`].
88 ///
89 /// Writable | Construct Only
90 ///
91 ///
92 /// #### `capabilities`
93 /// Flags from the #GDBusCapabilityFlags enumeration
94 /// representing connection features negotiated with the other peer.
95 ///
96 /// Readable
97 ///
98 ///
99 /// #### `closed`
100 /// A boolean specifying whether the connection has been closed.
101 ///
102 /// Readable
103 ///
104 ///
105 /// #### `exit-on-close`
106 /// A boolean specifying whether the process will be terminated (by
107 /// calling `raise(SIGTERM)`) if the connection is closed by the
108 /// remote peer.
109 ///
110 /// Note that #GDBusConnection objects returned by g_bus_get_finish()
111 /// and g_bus_get_sync() will (usually) have this property set to [`true`].
112 ///
113 /// Readable | Writable
114 ///
115 ///
116 /// #### `flags`
117 /// Flags from the #GDBusConnectionFlags enumeration.
118 ///
119 /// Readable | Writable | Construct Only
120 ///
121 ///
122 /// #### `guid`
123 /// The GUID of the peer performing the role of server when
124 /// authenticating.
125 ///
126 /// If you are constructing a #GDBusConnection and pass
127 /// [`DBusConnectionFlags::AUTHENTICATION_SERVER`][crate::DBusConnectionFlags::AUTHENTICATION_SERVER] in the
128 /// #GDBusConnection:flags property then you **must** also set this
129 /// property to a valid guid.
130 ///
131 /// If you are constructing a #GDBusConnection and pass
132 /// [`DBusConnectionFlags::AUTHENTICATION_CLIENT`][crate::DBusConnectionFlags::AUTHENTICATION_CLIENT] in the
133 /// #GDBusConnection:flags property you will be able to read the GUID
134 /// of the other peer here after the connection has been successfully
135 /// initialized.
136 ///
137 /// Note that the
138 /// [D-Bus specification](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses)
139 /// uses the term ‘UUID’ to refer to this, whereas GLib consistently uses the
140 /// term ‘GUID’ for historical reasons.
141 ///
142 /// Despite its name, the format of #GDBusConnection:guid does not follow
143 /// [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122) or the Microsoft
144 /// GUID format.
145 ///
146 /// Readable | Writable | Construct Only
147 ///
148 ///
149 /// #### `stream`
150 /// The underlying #GIOStream used for I/O.
151 ///
152 /// If this is passed on construction and is a #GSocketConnection,
153 /// then the corresponding #GSocket will be put into non-blocking mode.
154 ///
155 /// While the #GDBusConnection is active, it will interact with this
156 /// stream from a worker thread, so it is not safe to interact with
157 /// the stream directly.
158 ///
159 /// Readable | Writable | Construct Only
160 ///
161 ///
162 /// #### `unique-name`
163 /// The unique name as assigned by the message bus or [`None`] if the
164 /// connection is not open or not a message bus connection.
165 ///
166 /// Readable
167 ///
168 /// ## Signals
169 ///
170 ///
171 /// #### `closed`
172 /// Emitted when the connection is closed.
173 ///
174 /// The cause of this event can be
175 ///
176 /// - If g_dbus_connection_close() is called. In this case
177 /// @remote_peer_vanished is set to [`false`] and @error is [`None`].
178 ///
179 /// - If the remote peer closes the connection. In this case
180 /// @remote_peer_vanished is set to [`true`] and @error is set.
181 ///
182 /// - If the remote peer sends invalid or malformed data. In this
183 /// case @remote_peer_vanished is set to [`false`] and @error is set.
184 ///
185 /// Upon receiving this signal, you should give up your reference to
186 /// @connection. You are guaranteed that this signal is emitted only
187 /// once.
188 ///
189 ///
190 ///
191 /// # Implements
192 ///
193 /// [`trait@glib::ObjectExt`], [`AsyncInitableExt`][trait@crate::prelude::AsyncInitableExt], [`InitableExt`][trait@crate::prelude::InitableExt]
194 #[doc(alias = "GDBusConnection")]
195 pub struct DBusConnection(Object<ffi::GDBusConnection>) @implements AsyncInitable, Initable;
196
197 match fn {
198 type_ => || ffi::g_dbus_connection_get_type(),
199 }
200}
201
202impl DBusConnection {
203 /// Synchronously connects and sets up a D-Bus client connection for
204 /// exchanging D-Bus messages with an endpoint specified by @address
205 /// which must be in the
206 /// [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
207 ///
208 /// This constructor can only be used to initiate client-side
209 /// connections - use g_dbus_connection_new_sync() if you need to act
210 /// as the server. In particular, @flags cannot contain the
211 /// [`DBusConnectionFlags::AUTHENTICATION_SERVER`][crate::DBusConnectionFlags::AUTHENTICATION_SERVER],
212 /// [`DBusConnectionFlags::AUTHENTICATION_ALLOW_ANONYMOUS`][crate::DBusConnectionFlags::AUTHENTICATION_ALLOW_ANONYMOUS] or
213 /// [`DBusConnectionFlags::AUTHENTICATION_REQUIRE_SAME_USER`][crate::DBusConnectionFlags::AUTHENTICATION_REQUIRE_SAME_USER] flags.
214 ///
215 /// This is a synchronous failable constructor. See
216 /// g_dbus_connection_new_for_address() for the asynchronous version.
217 ///
218 /// If @observer is not [`None`] it may be used to control the
219 /// authentication process.
220 /// ## `address`
221 /// a D-Bus address
222 /// ## `flags`
223 /// flags describing how to make the connection
224 /// ## `observer`
225 /// a #GDBusAuthObserver or [`None`]
226 /// ## `cancellable`
227 /// a #GCancellable or [`None`]
228 ///
229 /// # Returns
230 ///
231 /// a #GDBusConnection or [`None`] if @error is set.
232 /// Free with g_object_unref().
233 #[doc(alias = "g_dbus_connection_new_for_address_sync")]
234 #[doc(alias = "new_for_address_sync")]
235 pub fn for_address_sync(
236 address: &str,
237 flags: DBusConnectionFlags,
238 observer: Option<&DBusAuthObserver>,
239 cancellable: Option<&impl IsA<Cancellable>>,
240 ) -> Result<DBusConnection, glib::Error> {
241 unsafe {
242 let mut error = std::ptr::null_mut();
243 let ret = ffi::g_dbus_connection_new_for_address_sync(
244 address.to_glib_none().0,
245 flags.into_glib(),
246 observer.to_glib_none().0,
247 cancellable.map(|p| p.as_ref()).to_glib_none().0,
248 &mut error,
249 );
250 if error.is_null() {
251 Ok(from_glib_full(ret))
252 } else {
253 Err(from_glib_full(error))
254 }
255 }
256 }
257
258 /// Synchronously sets up a D-Bus connection for exchanging D-Bus messages
259 /// with the end represented by @stream.
260 ///
261 /// If @stream is a #GSocketConnection, then the corresponding #GSocket
262 /// will be put into non-blocking mode.
263 ///
264 /// The D-Bus connection will interact with @stream from a worker thread.
265 /// As a result, the caller should not interact with @stream after this
266 /// method has been called, except by calling g_object_unref() on it.
267 ///
268 /// If @observer is not [`None`] it may be used to control the
269 /// authentication process.
270 ///
271 /// This is a synchronous failable constructor. See
272 /// g_dbus_connection_new() for the asynchronous version.
273 /// ## `stream`
274 /// a #GIOStream
275 /// ## `guid`
276 /// the GUID to use if authenticating as a server or [`None`]
277 /// ## `flags`
278 /// flags describing how to make the connection
279 /// ## `observer`
280 /// a #GDBusAuthObserver or [`None`]
281 /// ## `cancellable`
282 /// a #GCancellable or [`None`]
283 ///
284 /// # Returns
285 ///
286 /// a #GDBusConnection or [`None`] if @error is set.
287 /// Free with g_object_unref().
288 #[doc(alias = "g_dbus_connection_new_sync")]
289 pub fn new_sync(
290 stream: &impl IsA<IOStream>,
291 guid: Option<&str>,
292 flags: DBusConnectionFlags,
293 observer: Option<&DBusAuthObserver>,
294 cancellable: Option<&impl IsA<Cancellable>>,
295 ) -> Result<DBusConnection, glib::Error> {
296 unsafe {
297 let mut error = std::ptr::null_mut();
298 let ret = ffi::g_dbus_connection_new_sync(
299 stream.as_ref().to_glib_none().0,
300 guid.to_glib_none().0,
301 flags.into_glib(),
302 observer.to_glib_none().0,
303 cancellable.map(|p| p.as_ref()).to_glib_none().0,
304 &mut error,
305 );
306 if error.is_null() {
307 Ok(from_glib_full(ret))
308 } else {
309 Err(from_glib_full(error))
310 }
311 }
312 }
313
314 ///
315 /// g_dbus_connection_call (connection,
316 /// "org.freedesktop.StringThings",
317 /// "/org/freedesktop/StringThings",
318 /// "org.freedesktop.StringThings",
319 /// "TwoStrings",
320 /// g_variant_new ("(ss)",
321 /// "Thing One",
322 /// "Thing Two"),
323 /// NULL,
324 /// G_DBUS_CALL_FLAGS_NONE,
325 /// -1,
326 /// NULL,
327 /// (GAsyncReadyCallback) two_strings_done,
328 /// NULL);
329 /// ]|
330 ///
331 /// This is an asynchronous method. When the operation is finished,
332 /// @callback will be invoked in the thread-default main context
333 /// (see [`glib::MainContext::push_thread_default()`][crate::glib::MainContext::push_thread_default()])
334 /// of the thread you are calling this method from. You can then call
335 /// g_dbus_connection_call_finish() to get the result of the operation.
336 /// See g_dbus_connection_call_sync() for the synchronous version of this
337 /// function.
338 ///
339 /// If @callback is [`None`] then the D-Bus method call message will be sent with
340 /// the [`DBusMessageFlags::NO_REPLY_EXPECTED`][crate::DBusMessageFlags::NO_REPLY_EXPECTED] flag set.
341 /// ## `bus_name`
342 /// a unique or well-known bus name or [`None`] if
343 /// @self is not a message bus connection
344 /// ## `object_path`
345 /// path of remote object
346 /// ## `interface_name`
347 /// D-Bus interface to invoke method on
348 /// ## `method_name`
349 /// the name of the method to invoke
350 /// ## `parameters`
351 /// a #GVariant tuple with parameters for the method
352 /// or [`None`] if not passing parameters
353 /// ## `reply_type`
354 /// the expected type of the reply (which will be a
355 /// tuple), or [`None`]
356 /// ## `flags`
357 /// flags from the #GDBusCallFlags enumeration
358 /// ## `timeout_msec`
359 /// the timeout in milliseconds, -1 to use the default
360 /// timeout or `G_MAXINT` for no timeout
361 /// ## `cancellable`
362 /// a #GCancellable or [`None`]
363 /// ## `callback`
364 /// a #GAsyncReadyCallback to call when the request
365 /// is satisfied or [`None`] if you don't care about the result of the
366 /// method invocation
367 #[doc(alias = "g_dbus_connection_call")]
368 pub fn call<P: FnOnce(Result<glib::Variant, glib::Error>) + 'static>(
369 &self,
370 bus_name: Option<&str>,
371 object_path: &str,
372 interface_name: &str,
373 method_name: &str,
374 parameters: Option<&glib::Variant>,
375 reply_type: Option<&glib::VariantTy>,
376 flags: DBusCallFlags,
377 timeout_msec: i32,
378 cancellable: Option<&impl IsA<Cancellable>>,
379 callback: P,
380 ) {
381 let main_context = glib::MainContext::ref_thread_default();
382 let is_main_context_owner = main_context.is_owner();
383 let has_acquired_main_context = (!is_main_context_owner)
384 .then(|| main_context.acquire().ok())
385 .flatten();
386 assert!(
387 is_main_context_owner || has_acquired_main_context.is_some(),
388 "Async operations only allowed if the thread is owning the MainContext"
389 );
390
391 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
392 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
393 unsafe extern "C" fn call_trampoline<
394 P: FnOnce(Result<glib::Variant, glib::Error>) + 'static,
395 >(
396 _source_object: *mut glib::gobject_ffi::GObject,
397 res: *mut crate::ffi::GAsyncResult,
398 user_data: glib::ffi::gpointer,
399 ) {
400 unsafe {
401 let mut error = std::ptr::null_mut();
402 let ret =
403 ffi::g_dbus_connection_call_finish(_source_object as *mut _, res, &mut error);
404 let result = if error.is_null() {
405 Ok(from_glib_full(ret))
406 } else {
407 Err(from_glib_full(error))
408 };
409 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
410 Box_::from_raw(user_data as *mut _);
411 let callback: P = callback.into_inner();
412 callback(result);
413 }
414 }
415 let callback = call_trampoline::<P>;
416 unsafe {
417 ffi::g_dbus_connection_call(
418 self.to_glib_none().0,
419 bus_name.to_glib_none().0,
420 object_path.to_glib_none().0,
421 interface_name.to_glib_none().0,
422 method_name.to_glib_none().0,
423 parameters.to_glib_none().0,
424 reply_type.to_glib_none().0,
425 flags.into_glib(),
426 timeout_msec,
427 cancellable.map(|p| p.as_ref()).to_glib_none().0,
428 Some(callback),
429 Box_::into_raw(user_data) as *mut _,
430 );
431 }
432 }
433
434 pub fn call_future(
435 &self,
436 bus_name: Option<&str>,
437 object_path: &str,
438 interface_name: &str,
439 method_name: &str,
440 parameters: Option<&glib::Variant>,
441 reply_type: Option<&glib::VariantTy>,
442 flags: DBusCallFlags,
443 timeout_msec: i32,
444 ) -> Pin<Box_<dyn std::future::Future<Output = Result<glib::Variant, glib::Error>> + 'static>>
445 {
446 let bus_name = bus_name.map(ToOwned::to_owned);
447 let object_path = String::from(object_path);
448 let interface_name = String::from(interface_name);
449 let method_name = String::from(method_name);
450 let parameters = parameters.map(ToOwned::to_owned);
451 let reply_type = reply_type.map(ToOwned::to_owned);
452 Box_::pin(crate::GioFuture::new(
453 self,
454 move |obj, cancellable, send| {
455 obj.call(
456 bus_name.as_ref().map(::std::borrow::Borrow::borrow),
457 &object_path,
458 &interface_name,
459 &method_name,
460 parameters.as_ref().map(::std::borrow::Borrow::borrow),
461 reply_type.as_ref().map(::std::borrow::Borrow::borrow),
462 flags,
463 timeout_msec,
464 Some(cancellable),
465 move |res| {
466 send.resolve(res);
467 },
468 );
469 },
470 ))
471 }
472
473 /// error);
474 /// ]|
475 ///
476 /// The calling thread is blocked until a reply is received. See
477 /// g_dbus_connection_call() for the asynchronous version of
478 /// this method.
479 /// ## `bus_name`
480 /// a unique or well-known bus name or [`None`] if
481 /// @self is not a message bus connection
482 /// ## `object_path`
483 /// path of remote object
484 /// ## `interface_name`
485 /// D-Bus interface to invoke method on
486 /// ## `method_name`
487 /// the name of the method to invoke
488 /// ## `parameters`
489 /// a #GVariant tuple with parameters for the method
490 /// or [`None`] if not passing parameters
491 /// ## `reply_type`
492 /// the expected type of the reply, or [`None`]
493 /// ## `flags`
494 /// flags from the #GDBusCallFlags enumeration
495 /// ## `timeout_msec`
496 /// the timeout in milliseconds, -1 to use the default
497 /// timeout or `G_MAXINT` for no timeout
498 /// ## `cancellable`
499 /// a #GCancellable or [`None`]
500 ///
501 /// # Returns
502 ///
503 /// [`None`] if @error is set. Otherwise a non-floating
504 /// #GVariant tuple with return values. Free with g_variant_unref().
505 #[doc(alias = "g_dbus_connection_call_sync")]
506 pub fn call_sync(
507 &self,
508 bus_name: Option<&str>,
509 object_path: &str,
510 interface_name: &str,
511 method_name: &str,
512 parameters: Option<&glib::Variant>,
513 reply_type: Option<&glib::VariantTy>,
514 flags: DBusCallFlags,
515 timeout_msec: i32,
516 cancellable: Option<&impl IsA<Cancellable>>,
517 ) -> Result<glib::Variant, glib::Error> {
518 unsafe {
519 let mut error = std::ptr::null_mut();
520 let ret = ffi::g_dbus_connection_call_sync(
521 self.to_glib_none().0,
522 bus_name.to_glib_none().0,
523 object_path.to_glib_none().0,
524 interface_name.to_glib_none().0,
525 method_name.to_glib_none().0,
526 parameters.to_glib_none().0,
527 reply_type.to_glib_none().0,
528 flags.into_glib(),
529 timeout_msec,
530 cancellable.map(|p| p.as_ref()).to_glib_none().0,
531 &mut error,
532 );
533 if error.is_null() {
534 Ok(from_glib_full(ret))
535 } else {
536 Err(from_glib_full(error))
537 }
538 }
539 }
540
541 /// Like g_dbus_connection_call() but also takes a #GUnixFDList object.
542 ///
543 /// The file descriptors normally correspond to `G_VARIANT_TYPE_HANDLE`
544 /// values in the body of the message. For example, if a message contains
545 /// two file descriptors, @fd_list would have length 2, and
546 /// `g_variant_new_handle (0)` and `g_variant_new_handle (1)` would appear
547 /// somewhere in the body of the message (not necessarily in that order!)
548 /// to represent the file descriptors at indexes 0 and 1 respectively.
549 ///
550 /// When designing D-Bus APIs that are intended to be interoperable,
551 /// please note that non-GDBus implementations of D-Bus can usually only
552 /// access file descriptors if they are referenced in this way by a
553 /// value of type `G_VARIANT_TYPE_HANDLE` in the body of the message.
554 ///
555 /// This method is only available on UNIX.
556 /// ## `bus_name`
557 /// a unique or well-known bus name or [`None`] if
558 /// @self is not a message bus connection
559 /// ## `object_path`
560 /// path of remote object
561 /// ## `interface_name`
562 /// D-Bus interface to invoke method on
563 /// ## `method_name`
564 /// the name of the method to invoke
565 /// ## `parameters`
566 /// a #GVariant tuple with parameters for the method
567 /// or [`None`] if not passing parameters
568 /// ## `reply_type`
569 /// the expected type of the reply, or [`None`]
570 /// ## `flags`
571 /// flags from the #GDBusCallFlags enumeration
572 /// ## `timeout_msec`
573 /// the timeout in milliseconds, -1 to use the default
574 /// timeout or `G_MAXINT` for no timeout
575 /// ## `fd_list`
576 /// a #GUnixFDList or [`None`]
577 /// ## `cancellable`
578 /// a #GCancellable or [`None`]
579 /// ## `callback`
580 /// a #GAsyncReadyCallback to call when the request is
581 /// satisfied or [`None`] if you don't * care about the result of the
582 /// method invocation
583 #[cfg(unix)]
584 #[cfg_attr(docsrs, doc(cfg(unix)))]
585 #[doc(alias = "g_dbus_connection_call_with_unix_fd_list")]
586 pub fn call_with_unix_fd_list<
587 P: FnOnce(Result<(glib::Variant, Option<UnixFDList>), glib::Error>) + 'static,
588 >(
589 &self,
590 bus_name: Option<&str>,
591 object_path: &str,
592 interface_name: &str,
593 method_name: &str,
594 parameters: Option<&glib::Variant>,
595 reply_type: Option<&glib::VariantTy>,
596 flags: DBusCallFlags,
597 timeout_msec: i32,
598 fd_list: Option<&impl IsA<UnixFDList>>,
599 cancellable: Option<&impl IsA<Cancellable>>,
600 callback: P,
601 ) {
602 let main_context = glib::MainContext::ref_thread_default();
603 let is_main_context_owner = main_context.is_owner();
604 let has_acquired_main_context = (!is_main_context_owner)
605 .then(|| main_context.acquire().ok())
606 .flatten();
607 assert!(
608 is_main_context_owner || has_acquired_main_context.is_some(),
609 "Async operations only allowed if the thread is owning the MainContext"
610 );
611
612 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
613 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
614 unsafe extern "C" fn call_with_unix_fd_list_trampoline<
615 P: FnOnce(Result<(glib::Variant, Option<UnixFDList>), glib::Error>) + 'static,
616 >(
617 _source_object: *mut glib::gobject_ffi::GObject,
618 res: *mut crate::ffi::GAsyncResult,
619 user_data: glib::ffi::gpointer,
620 ) {
621 unsafe {
622 let mut error = std::ptr::null_mut();
623 let mut out_fd_list = std::ptr::null_mut();
624 let ret = ffi::g_dbus_connection_call_with_unix_fd_list_finish(
625 _source_object as *mut _,
626 &mut out_fd_list,
627 res,
628 &mut error,
629 );
630 let result = if error.is_null() {
631 Ok((from_glib_full(ret), from_glib_full(out_fd_list)))
632 } else {
633 Err(from_glib_full(error))
634 };
635 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
636 Box_::from_raw(user_data as *mut _);
637 let callback: P = callback.into_inner();
638 callback(result);
639 }
640 }
641 let callback = call_with_unix_fd_list_trampoline::<P>;
642 unsafe {
643 ffi::g_dbus_connection_call_with_unix_fd_list(
644 self.to_glib_none().0,
645 bus_name.to_glib_none().0,
646 object_path.to_glib_none().0,
647 interface_name.to_glib_none().0,
648 method_name.to_glib_none().0,
649 parameters.to_glib_none().0,
650 reply_type.to_glib_none().0,
651 flags.into_glib(),
652 timeout_msec,
653 fd_list.map(|p| p.as_ref()).to_glib_none().0,
654 cancellable.map(|p| p.as_ref()).to_glib_none().0,
655 Some(callback),
656 Box_::into_raw(user_data) as *mut _,
657 );
658 }
659 }
660
661 #[cfg(unix)]
662 #[cfg_attr(docsrs, doc(cfg(unix)))]
663 pub fn call_with_unix_fd_list_future(
664 &self,
665 bus_name: Option<&str>,
666 object_path: &str,
667 interface_name: &str,
668 method_name: &str,
669 parameters: Option<&glib::Variant>,
670 reply_type: Option<&glib::VariantTy>,
671 flags: DBusCallFlags,
672 timeout_msec: i32,
673 fd_list: Option<&(impl IsA<UnixFDList> + Clone + 'static)>,
674 ) -> Pin<
675 Box_<
676 dyn std::future::Future<
677 Output = Result<(glib::Variant, Option<UnixFDList>), glib::Error>,
678 > + 'static,
679 >,
680 > {
681 let bus_name = bus_name.map(ToOwned::to_owned);
682 let object_path = String::from(object_path);
683 let interface_name = String::from(interface_name);
684 let method_name = String::from(method_name);
685 let parameters = parameters.map(ToOwned::to_owned);
686 let reply_type = reply_type.map(ToOwned::to_owned);
687 let fd_list = fd_list.map(ToOwned::to_owned);
688 Box_::pin(crate::GioFuture::new(
689 self,
690 move |obj, cancellable, send| {
691 obj.call_with_unix_fd_list(
692 bus_name.as_ref().map(::std::borrow::Borrow::borrow),
693 &object_path,
694 &interface_name,
695 &method_name,
696 parameters.as_ref().map(::std::borrow::Borrow::borrow),
697 reply_type.as_ref().map(::std::borrow::Borrow::borrow),
698 flags,
699 timeout_msec,
700 fd_list.as_ref().map(::std::borrow::Borrow::borrow),
701 Some(cancellable),
702 move |res| {
703 send.resolve(res);
704 },
705 );
706 },
707 ))
708 }
709
710 /// Like g_dbus_connection_call_sync() but also takes and returns #GUnixFDList objects.
711 /// See g_dbus_connection_call_with_unix_fd_list() and
712 /// g_dbus_connection_call_with_unix_fd_list_finish() for more details.
713 ///
714 /// This method is only available on UNIX.
715 /// ## `bus_name`
716 /// a unique or well-known bus name or [`None`]
717 /// if @self is not a message bus connection
718 /// ## `object_path`
719 /// path of remote object
720 /// ## `interface_name`
721 /// D-Bus interface to invoke method on
722 /// ## `method_name`
723 /// the name of the method to invoke
724 /// ## `parameters`
725 /// a #GVariant tuple with parameters for
726 /// the method or [`None`] if not passing parameters
727 /// ## `reply_type`
728 /// the expected type of the reply, or [`None`]
729 /// ## `flags`
730 /// flags from the #GDBusCallFlags enumeration
731 /// ## `timeout_msec`
732 /// the timeout in milliseconds, -1 to use the default
733 /// timeout or `G_MAXINT` for no timeout
734 /// ## `fd_list`
735 /// a #GUnixFDList or [`None`]
736 /// ## `cancellable`
737 /// a #GCancellable or [`None`]
738 ///
739 /// # Returns
740 ///
741 /// [`None`] if @error is set. Otherwise a non-floating
742 /// #GVariant tuple with return values. Free with g_variant_unref().
743 ///
744 /// ## `out_fd_list`
745 /// return location for a #GUnixFDList or [`None`]
746 #[cfg(unix)]
747 #[cfg_attr(docsrs, doc(cfg(unix)))]
748 #[doc(alias = "g_dbus_connection_call_with_unix_fd_list_sync")]
749 pub fn call_with_unix_fd_list_sync(
750 &self,
751 bus_name: Option<&str>,
752 object_path: &str,
753 interface_name: &str,
754 method_name: &str,
755 parameters: Option<&glib::Variant>,
756 reply_type: Option<&glib::VariantTy>,
757 flags: DBusCallFlags,
758 timeout_msec: i32,
759 fd_list: Option<&impl IsA<UnixFDList>>,
760 cancellable: Option<&impl IsA<Cancellable>>,
761 ) -> Result<(glib::Variant, Option<UnixFDList>), glib::Error> {
762 unsafe {
763 let mut out_fd_list = std::ptr::null_mut();
764 let mut error = std::ptr::null_mut();
765 let ret = ffi::g_dbus_connection_call_with_unix_fd_list_sync(
766 self.to_glib_none().0,
767 bus_name.to_glib_none().0,
768 object_path.to_glib_none().0,
769 interface_name.to_glib_none().0,
770 method_name.to_glib_none().0,
771 parameters.to_glib_none().0,
772 reply_type.to_glib_none().0,
773 flags.into_glib(),
774 timeout_msec,
775 fd_list.map(|p| p.as_ref()).to_glib_none().0,
776 &mut out_fd_list,
777 cancellable.map(|p| p.as_ref()).to_glib_none().0,
778 &mut error,
779 );
780 if error.is_null() {
781 Ok((from_glib_full(ret), from_glib_full(out_fd_list)))
782 } else {
783 Err(from_glib_full(error))
784 }
785 }
786 }
787
788 /// Closes @self. Note that this never causes the process to
789 /// exit (this might only happen if the other end of a shared message
790 /// bus connection disconnects, see #GDBusConnection:exit-on-close).
791 ///
792 /// Once the connection is closed, operations such as sending a message
793 /// will return with the error [`IOErrorEnum::Closed`][crate::IOErrorEnum::Closed]. Closing a connection
794 /// will not automatically flush the connection so queued messages may
795 /// be lost. Use g_dbus_connection_flush() if you need such guarantees.
796 ///
797 /// If @self is already closed, this method fails with
798 /// [`IOErrorEnum::Closed`][crate::IOErrorEnum::Closed].
799 ///
800 /// When @self has been closed, the #GDBusConnection::closed
801 /// signal is emitted in the thread-default main context
802 /// (see [`glib::MainContext::push_thread_default()`][crate::glib::MainContext::push_thread_default()])
803 /// of the thread that @self was constructed in.
804 ///
805 /// This is an asynchronous method. When the operation is finished,
806 /// @callback will be invoked in the thread-default main context
807 /// (see [`glib::MainContext::push_thread_default()`][crate::glib::MainContext::push_thread_default()])
808 /// of the thread you are calling this method from. You can
809 /// then call g_dbus_connection_close_finish() to get the result of the
810 /// operation. See g_dbus_connection_close_sync() for the synchronous
811 /// version.
812 /// ## `cancellable`
813 /// a #GCancellable or [`None`]
814 /// ## `callback`
815 /// a #GAsyncReadyCallback to call when the request is
816 /// satisfied or [`None`] if you don't care about the result
817 #[doc(alias = "g_dbus_connection_close")]
818 pub fn close<P: FnOnce(Result<(), glib::Error>) + 'static>(
819 &self,
820 cancellable: Option<&impl IsA<Cancellable>>,
821 callback: P,
822 ) {
823 let main_context = glib::MainContext::ref_thread_default();
824 let is_main_context_owner = main_context.is_owner();
825 let has_acquired_main_context = (!is_main_context_owner)
826 .then(|| main_context.acquire().ok())
827 .flatten();
828 assert!(
829 is_main_context_owner || has_acquired_main_context.is_some(),
830 "Async operations only allowed if the thread is owning the MainContext"
831 );
832
833 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
834 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
835 unsafe extern "C" fn close_trampoline<P: FnOnce(Result<(), glib::Error>) + 'static>(
836 _source_object: *mut glib::gobject_ffi::GObject,
837 res: *mut crate::ffi::GAsyncResult,
838 user_data: glib::ffi::gpointer,
839 ) {
840 unsafe {
841 let mut error = std::ptr::null_mut();
842 ffi::g_dbus_connection_close_finish(_source_object as *mut _, res, &mut error);
843 let result = if error.is_null() {
844 Ok(())
845 } else {
846 Err(from_glib_full(error))
847 };
848 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
849 Box_::from_raw(user_data as *mut _);
850 let callback: P = callback.into_inner();
851 callback(result);
852 }
853 }
854 let callback = close_trampoline::<P>;
855 unsafe {
856 ffi::g_dbus_connection_close(
857 self.to_glib_none().0,
858 cancellable.map(|p| p.as_ref()).to_glib_none().0,
859 Some(callback),
860 Box_::into_raw(user_data) as *mut _,
861 );
862 }
863 }
864
865 pub fn close_future(
866 &self,
867 ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
868 Box_::pin(crate::GioFuture::new(
869 self,
870 move |obj, cancellable, send| {
871 obj.close(Some(cancellable), move |res| {
872 send.resolve(res);
873 });
874 },
875 ))
876 }
877
878 /// Synchronously closes @self. The calling thread is blocked
879 /// until this is done. See g_dbus_connection_close() for the
880 /// asynchronous version of this method and more details about what it
881 /// does.
882 /// ## `cancellable`
883 /// a #GCancellable or [`None`]
884 ///
885 /// # Returns
886 ///
887 /// [`true`] if the operation succeeded, [`false`] if @error is set
888 #[doc(alias = "g_dbus_connection_close_sync")]
889 pub fn close_sync(
890 &self,
891 cancellable: Option<&impl IsA<Cancellable>>,
892 ) -> Result<(), glib::Error> {
893 unsafe {
894 let mut error = std::ptr::null_mut();
895 let is_ok = ffi::g_dbus_connection_close_sync(
896 self.to_glib_none().0,
897 cancellable.map(|p| p.as_ref()).to_glib_none().0,
898 &mut error,
899 );
900 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
901 if error.is_null() {
902 Ok(())
903 } else {
904 Err(from_glib_full(error))
905 }
906 }
907 }
908
909 /// Emits a signal.
910 ///
911 /// If the parameters GVariant is floating, it is consumed.
912 ///
913 /// This can only fail if @parameters is not compatible with the D-Bus protocol
914 /// ([`IOErrorEnum::InvalidArgument`][crate::IOErrorEnum::InvalidArgument]), or if @self has been closed
915 /// ([`IOErrorEnum::Closed`][crate::IOErrorEnum::Closed]).
916 /// ## `destination_bus_name`
917 /// the unique bus name for the destination
918 /// for the signal or [`None`] to emit to all listeners
919 /// ## `object_path`
920 /// path of remote object
921 /// ## `interface_name`
922 /// D-Bus interface to emit a signal on
923 /// ## `signal_name`
924 /// the name of the signal to emit
925 /// ## `parameters`
926 /// a #GVariant tuple with parameters for the signal
927 /// or [`None`] if not passing parameters
928 ///
929 /// # Returns
930 ///
931 /// [`true`] unless @error is set
932 #[doc(alias = "g_dbus_connection_emit_signal")]
933 pub fn emit_signal(
934 &self,
935 destination_bus_name: Option<&str>,
936 object_path: &str,
937 interface_name: &str,
938 signal_name: &str,
939 parameters: Option<&glib::Variant>,
940 ) -> Result<(), glib::Error> {
941 unsafe {
942 let mut error = std::ptr::null_mut();
943 let is_ok = ffi::g_dbus_connection_emit_signal(
944 self.to_glib_none().0,
945 destination_bus_name.to_glib_none().0,
946 object_path.to_glib_none().0,
947 interface_name.to_glib_none().0,
948 signal_name.to_glib_none().0,
949 parameters.to_glib_none().0,
950 &mut error,
951 );
952 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
953 if error.is_null() {
954 Ok(())
955 } else {
956 Err(from_glib_full(error))
957 }
958 }
959 }
960
961 /// Asynchronously flushes @self, that is, writes all queued
962 /// outgoing messages to the transport and then flushes the transport
963 /// (using g_output_stream_flush_async()). This is useful in programs
964 /// that want to emit a D-Bus signal and then exit immediately. Without
965 /// flushing the connection, there is no guarantee that the message has
966 /// been sent to the networking buffers in the OS kernel.
967 ///
968 /// This is an asynchronous method. When the operation is finished,
969 /// @callback will be invoked in the thread-default main context
970 /// (see [`glib::MainContext::push_thread_default()`][crate::glib::MainContext::push_thread_default()])
971 /// of the thread you are calling this method from. You can
972 /// then call g_dbus_connection_flush_finish() to get the result of the
973 /// operation. See g_dbus_connection_flush_sync() for the synchronous
974 /// version.
975 /// ## `cancellable`
976 /// a #GCancellable or [`None`]
977 /// ## `callback`
978 /// a #GAsyncReadyCallback to call when the
979 /// request is satisfied or [`None`] if you don't care about the result
980 #[doc(alias = "g_dbus_connection_flush")]
981 pub fn flush<P: FnOnce(Result<(), glib::Error>) + 'static>(
982 &self,
983 cancellable: Option<&impl IsA<Cancellable>>,
984 callback: P,
985 ) {
986 let main_context = glib::MainContext::ref_thread_default();
987 let is_main_context_owner = main_context.is_owner();
988 let has_acquired_main_context = (!is_main_context_owner)
989 .then(|| main_context.acquire().ok())
990 .flatten();
991 assert!(
992 is_main_context_owner || has_acquired_main_context.is_some(),
993 "Async operations only allowed if the thread is owning the MainContext"
994 );
995
996 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
997 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
998 unsafe extern "C" fn flush_trampoline<P: FnOnce(Result<(), glib::Error>) + 'static>(
999 _source_object: *mut glib::gobject_ffi::GObject,
1000 res: *mut crate::ffi::GAsyncResult,
1001 user_data: glib::ffi::gpointer,
1002 ) {
1003 unsafe {
1004 let mut error = std::ptr::null_mut();
1005 ffi::g_dbus_connection_flush_finish(_source_object as *mut _, res, &mut error);
1006 let result = if error.is_null() {
1007 Ok(())
1008 } else {
1009 Err(from_glib_full(error))
1010 };
1011 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
1012 Box_::from_raw(user_data as *mut _);
1013 let callback: P = callback.into_inner();
1014 callback(result);
1015 }
1016 }
1017 let callback = flush_trampoline::<P>;
1018 unsafe {
1019 ffi::g_dbus_connection_flush(
1020 self.to_glib_none().0,
1021 cancellable.map(|p| p.as_ref()).to_glib_none().0,
1022 Some(callback),
1023 Box_::into_raw(user_data) as *mut _,
1024 );
1025 }
1026 }
1027
1028 pub fn flush_future(
1029 &self,
1030 ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
1031 Box_::pin(crate::GioFuture::new(
1032 self,
1033 move |obj, cancellable, send| {
1034 obj.flush(Some(cancellable), move |res| {
1035 send.resolve(res);
1036 });
1037 },
1038 ))
1039 }
1040
1041 /// Synchronously flushes @self. The calling thread is blocked
1042 /// until this is done. See g_dbus_connection_flush() for the
1043 /// asynchronous version of this method and more details about what it
1044 /// does.
1045 /// ## `cancellable`
1046 /// a #GCancellable or [`None`]
1047 ///
1048 /// # Returns
1049 ///
1050 /// [`true`] if the operation succeeded, [`false`] if @error is set
1051 #[doc(alias = "g_dbus_connection_flush_sync")]
1052 pub fn flush_sync(
1053 &self,
1054 cancellable: Option<&impl IsA<Cancellable>>,
1055 ) -> Result<(), glib::Error> {
1056 unsafe {
1057 let mut error = std::ptr::null_mut();
1058 let is_ok = ffi::g_dbus_connection_flush_sync(
1059 self.to_glib_none().0,
1060 cancellable.map(|p| p.as_ref()).to_glib_none().0,
1061 &mut error,
1062 );
1063 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
1064 if error.is_null() {
1065 Ok(())
1066 } else {
1067 Err(from_glib_full(error))
1068 }
1069 }
1070 }
1071
1072 /// Gets the capabilities negotiated with the remote peer
1073 ///
1074 /// # Returns
1075 ///
1076 /// zero or more flags from the #GDBusCapabilityFlags enumeration
1077 #[doc(alias = "g_dbus_connection_get_capabilities")]
1078 #[doc(alias = "get_capabilities")]
1079 pub fn capabilities(&self) -> DBusCapabilityFlags {
1080 unsafe {
1081 from_glib(ffi::g_dbus_connection_get_capabilities(
1082 self.to_glib_none().0,
1083 ))
1084 }
1085 }
1086
1087 /// Gets whether the process is terminated when @self is
1088 /// closed by the remote peer. See
1089 /// #GDBusConnection:exit-on-close for more details.
1090 ///
1091 /// # Returns
1092 ///
1093 /// whether the process is terminated when @self is
1094 /// closed by the remote peer
1095 #[doc(alias = "g_dbus_connection_get_exit_on_close")]
1096 #[doc(alias = "get_exit_on_close")]
1097 #[doc(alias = "exit-on-close")]
1098 pub fn exits_on_close(&self) -> bool {
1099 unsafe {
1100 from_glib(ffi::g_dbus_connection_get_exit_on_close(
1101 self.to_glib_none().0,
1102 ))
1103 }
1104 }
1105
1106 /// Gets the flags used to construct this connection
1107 ///
1108 /// # Returns
1109 ///
1110 /// zero or more flags from the #GDBusConnectionFlags enumeration
1111 #[cfg(feature = "v2_60")]
1112 #[cfg_attr(docsrs, doc(cfg(feature = "v2_60")))]
1113 #[doc(alias = "g_dbus_connection_get_flags")]
1114 #[doc(alias = "get_flags")]
1115 pub fn flags(&self) -> DBusConnectionFlags {
1116 unsafe { from_glib(ffi::g_dbus_connection_get_flags(self.to_glib_none().0)) }
1117 }
1118
1119 /// The GUID of the peer performing the role of server when
1120 /// authenticating. See #GDBusConnection:guid for more details.
1121 ///
1122 /// # Returns
1123 ///
1124 /// The GUID. Do not free this string, it is owned by
1125 /// @self.
1126 #[doc(alias = "g_dbus_connection_get_guid")]
1127 #[doc(alias = "get_guid")]
1128 pub fn guid(&self) -> glib::GString {
1129 unsafe { from_glib_none(ffi::g_dbus_connection_get_guid(self.to_glib_none().0)) }
1130 }
1131
1132 /// Retrieves the last serial number assigned to a #GDBusMessage on
1133 /// the current thread. This includes messages sent via both low-level
1134 /// API such as g_dbus_connection_send_message() as well as
1135 /// high-level API such as g_dbus_connection_emit_signal(),
1136 /// g_dbus_connection_call() or g_dbus_proxy_call().
1137 ///
1138 /// # Returns
1139 ///
1140 /// the last used serial or zero when no message has been sent
1141 /// within the current thread
1142 #[doc(alias = "g_dbus_connection_get_last_serial")]
1143 #[doc(alias = "get_last_serial")]
1144 pub fn last_serial(&self) -> u32 {
1145 unsafe { ffi::g_dbus_connection_get_last_serial(self.to_glib_none().0) }
1146 }
1147
1148 /// Gets the credentials of the authenticated peer. This will always
1149 /// return [`None`] unless @self acted as a server
1150 /// (e.g. [`DBusConnectionFlags::AUTHENTICATION_SERVER`][crate::DBusConnectionFlags::AUTHENTICATION_SERVER] was passed)
1151 /// when set up and the client passed credentials as part of the
1152 /// authentication process.
1153 ///
1154 /// In a message bus setup, the message bus is always the server and
1155 /// each application is a client. So this method will always return
1156 /// [`None`] for message bus clients.
1157 ///
1158 /// # Returns
1159 ///
1160 /// a #GCredentials or [`None`] if not
1161 /// available. Do not free this object, it is owned by @self.
1162 #[doc(alias = "g_dbus_connection_get_peer_credentials")]
1163 #[doc(alias = "get_peer_credentials")]
1164 pub fn peer_credentials(&self) -> Option<Credentials> {
1165 unsafe {
1166 from_glib_none(ffi::g_dbus_connection_get_peer_credentials(
1167 self.to_glib_none().0,
1168 ))
1169 }
1170 }
1171
1172 /// Gets the underlying stream used for IO.
1173 ///
1174 /// While the #GDBusConnection is active, it will interact with this
1175 /// stream from a worker thread, so it is not safe to interact with
1176 /// the stream directly.
1177 ///
1178 /// # Returns
1179 ///
1180 /// the stream used for IO
1181 #[doc(alias = "g_dbus_connection_get_stream")]
1182 #[doc(alias = "get_stream")]
1183 pub fn stream(&self) -> IOStream {
1184 unsafe { from_glib_none(ffi::g_dbus_connection_get_stream(self.to_glib_none().0)) }
1185 }
1186
1187 /// Gets the unique name of @self as assigned by the message
1188 /// bus. This can also be used to figure out if @self is a
1189 /// message bus connection.
1190 ///
1191 /// # Returns
1192 ///
1193 /// the unique name or [`None`] if @self is not a message
1194 /// bus connection. Do not free this string, it is owned by
1195 /// @self.
1196 #[doc(alias = "g_dbus_connection_get_unique_name")]
1197 #[doc(alias = "get_unique_name")]
1198 #[doc(alias = "unique-name")]
1199 pub fn unique_name(&self) -> Option<glib::GString> {
1200 unsafe {
1201 from_glib_none(ffi::g_dbus_connection_get_unique_name(
1202 self.to_glib_none().0,
1203 ))
1204 }
1205 }
1206
1207 /// Gets whether @self is closed.
1208 ///
1209 /// # Returns
1210 ///
1211 /// [`true`] if the connection is closed, [`false`] otherwise
1212 #[doc(alias = "g_dbus_connection_is_closed")]
1213 #[doc(alias = "closed")]
1214 pub fn is_closed(&self) -> bool {
1215 unsafe { from_glib(ffi::g_dbus_connection_is_closed(self.to_glib_none().0)) }
1216 }
1217
1218 /// Asynchronously sends @message to the peer represented by @self.
1219 ///
1220 /// Unless @flags contain the
1221 /// [`DBusSendMessageFlags::PRESERVE_SERIAL`][crate::DBusSendMessageFlags::PRESERVE_SERIAL] flag, the serial number
1222 /// will be assigned by @self and set on @message via
1223 /// g_dbus_message_set_serial(). If @out_serial is not [`None`], then the
1224 /// serial number used will be written to this location prior to
1225 /// submitting the message to the underlying transport. While it has a `volatile`
1226 /// qualifier, this is a historical artifact and the argument passed to it should
1227 /// not be `volatile`.
1228 ///
1229 /// If @self is closed then the operation will fail with
1230 /// [`IOErrorEnum::Closed`][crate::IOErrorEnum::Closed]. If @message is not well-formed,
1231 /// the operation fails with [`IOErrorEnum::InvalidArgument`][crate::IOErrorEnum::InvalidArgument].
1232 ///
1233 /// See this [server][`DBusConnection`][crate::DBusConnection]#an-example-d-bus-server]
1234 /// and [client][`DBusConnection`][crate::DBusConnection]#an-example-for-file-descriptor-passing]
1235 /// for an example of how to use this low-level API to send and receive
1236 /// UNIX file descriptors.
1237 ///
1238 /// Note that @message must be unlocked, unless @flags contain the
1239 /// [`DBusSendMessageFlags::PRESERVE_SERIAL`][crate::DBusSendMessageFlags::PRESERVE_SERIAL] flag.
1240 /// ## `message`
1241 /// a #GDBusMessage
1242 /// ## `flags`
1243 /// flags affecting how the message is sent
1244 ///
1245 /// # Returns
1246 ///
1247 /// [`true`] if the message was well-formed and queued for
1248 /// transmission, [`false`] if @error is set
1249 ///
1250 /// ## `out_serial`
1251 /// return location for serial number assigned
1252 /// to @message when sending it or [`None`]
1253 #[doc(alias = "g_dbus_connection_send_message")]
1254 pub fn send_message(
1255 &self,
1256 message: &DBusMessage,
1257 flags: DBusSendMessageFlags,
1258 ) -> Result<u32, glib::Error> {
1259 unsafe {
1260 let mut out_serial = std::mem::MaybeUninit::uninit();
1261 let mut error = std::ptr::null_mut();
1262 let is_ok = ffi::g_dbus_connection_send_message(
1263 self.to_glib_none().0,
1264 message.to_glib_none().0,
1265 flags.into_glib(),
1266 out_serial.as_mut_ptr(),
1267 &mut error,
1268 );
1269 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
1270 if error.is_null() {
1271 Ok(out_serial.assume_init())
1272 } else {
1273 Err(from_glib_full(error))
1274 }
1275 }
1276 }
1277
1278 /// Asynchronously sends @message to the peer represented by @self.
1279 ///
1280 /// Unless @flags contain the
1281 /// [`DBusSendMessageFlags::PRESERVE_SERIAL`][crate::DBusSendMessageFlags::PRESERVE_SERIAL] flag, the serial number
1282 /// will be assigned by @self and set on @message via
1283 /// g_dbus_message_set_serial(). If @out_serial is not [`None`], then the
1284 /// serial number used will be written to this location prior to
1285 /// submitting the message to the underlying transport. While it has a `volatile`
1286 /// qualifier, this is a historical artifact and the argument passed to it should
1287 /// not be `volatile`.
1288 ///
1289 /// If @self is closed then the operation will fail with
1290 /// [`IOErrorEnum::Closed`][crate::IOErrorEnum::Closed]. If @cancellable is canceled, the operation will
1291 /// fail with [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled]. If @message is not well-formed,
1292 /// the operation fails with [`IOErrorEnum::InvalidArgument`][crate::IOErrorEnum::InvalidArgument].
1293 ///
1294 /// This is an asynchronous method. When the operation is finished, @callback
1295 /// will be invoked in the thread-default main context
1296 /// (see [`glib::MainContext::push_thread_default()`][crate::glib::MainContext::push_thread_default()])
1297 /// of the thread you are calling this method from. You can then call
1298 /// g_dbus_connection_send_message_with_reply_finish() to get the result of the operation.
1299 /// See g_dbus_connection_send_message_with_reply_sync() for the synchronous version.
1300 ///
1301 /// Note that @message must be unlocked, unless @flags contain the
1302 /// [`DBusSendMessageFlags::PRESERVE_SERIAL`][crate::DBusSendMessageFlags::PRESERVE_SERIAL] flag.
1303 ///
1304 /// See this [server][`DBusConnection`][crate::DBusConnection]#an-example-d-bus-server]
1305 /// and [client][`DBusConnection`][crate::DBusConnection]#an-example-for-file-descriptor-passing]
1306 /// for an example of how to use this low-level API to send and receive
1307 /// UNIX file descriptors.
1308 /// ## `message`
1309 /// a #GDBusMessage
1310 /// ## `flags`
1311 /// flags affecting how the message is sent
1312 /// ## `timeout_msec`
1313 /// the timeout in milliseconds, -1 to use the default
1314 /// timeout or `G_MAXINT` for no timeout
1315 /// ## `cancellable`
1316 /// a #GCancellable or [`None`]
1317 /// ## `callback`
1318 /// a #GAsyncReadyCallback to call when the request
1319 /// is satisfied or [`None`] if you don't care about the result
1320 ///
1321 /// # Returns
1322 ///
1323 ///
1324 /// ## `out_serial`
1325 /// return location for serial number assigned
1326 /// to @message when sending it or [`None`]
1327 #[doc(alias = "g_dbus_connection_send_message_with_reply")]
1328 pub fn send_message_with_reply<P: FnOnce(Result<DBusMessage, glib::Error>) + 'static>(
1329 &self,
1330 message: &DBusMessage,
1331 flags: DBusSendMessageFlags,
1332 timeout_msec: i32,
1333 cancellable: Option<&impl IsA<Cancellable>>,
1334 callback: P,
1335 ) -> u32 {
1336 let main_context = glib::MainContext::ref_thread_default();
1337 let is_main_context_owner = main_context.is_owner();
1338 let has_acquired_main_context = (!is_main_context_owner)
1339 .then(|| main_context.acquire().ok())
1340 .flatten();
1341 assert!(
1342 is_main_context_owner || has_acquired_main_context.is_some(),
1343 "Async operations only allowed if the thread is owning the MainContext"
1344 );
1345
1346 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
1347 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
1348 unsafe extern "C" fn send_message_with_reply_trampoline<
1349 P: FnOnce(Result<DBusMessage, glib::Error>) + 'static,
1350 >(
1351 _source_object: *mut glib::gobject_ffi::GObject,
1352 res: *mut crate::ffi::GAsyncResult,
1353 user_data: glib::ffi::gpointer,
1354 ) {
1355 unsafe {
1356 let mut error = std::ptr::null_mut();
1357 let ret = ffi::g_dbus_connection_send_message_with_reply_finish(
1358 _source_object as *mut _,
1359 res,
1360 &mut error,
1361 );
1362 let result = if error.is_null() {
1363 Ok(from_glib_full(ret))
1364 } else {
1365 Err(from_glib_full(error))
1366 };
1367 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
1368 Box_::from_raw(user_data as *mut _);
1369 let callback: P = callback.into_inner();
1370 callback(result);
1371 }
1372 }
1373 let callback = send_message_with_reply_trampoline::<P>;
1374 unsafe {
1375 let mut out_serial = std::mem::MaybeUninit::uninit();
1376 ffi::g_dbus_connection_send_message_with_reply(
1377 self.to_glib_none().0,
1378 message.to_glib_none().0,
1379 flags.into_glib(),
1380 timeout_msec,
1381 out_serial.as_mut_ptr(),
1382 cancellable.map(|p| p.as_ref()).to_glib_none().0,
1383 Some(callback),
1384 Box_::into_raw(user_data) as *mut _,
1385 );
1386 out_serial.assume_init()
1387 }
1388 }
1389
1390 pub fn send_message_with_reply_future(
1391 &self,
1392 message: &DBusMessage,
1393 flags: DBusSendMessageFlags,
1394 timeout_msec: i32,
1395 ) -> Pin<Box_<dyn std::future::Future<Output = Result<DBusMessage, glib::Error>> + 'static>>
1396 {
1397 let message = message.clone();
1398 Box_::pin(crate::GioFuture::new(
1399 self,
1400 move |obj, cancellable, send| {
1401 obj.send_message_with_reply(
1402 &message,
1403 flags,
1404 timeout_msec,
1405 Some(cancellable),
1406 move |res| {
1407 send.resolve(res);
1408 },
1409 );
1410 },
1411 ))
1412 }
1413
1414 /// Synchronously sends @message to the peer represented by @self
1415 /// and blocks the calling thread until a reply is received or the
1416 /// timeout is reached. See g_dbus_connection_send_message_with_reply()
1417 /// for the asynchronous version of this method.
1418 ///
1419 /// Unless @flags contain the
1420 /// [`DBusSendMessageFlags::PRESERVE_SERIAL`][crate::DBusSendMessageFlags::PRESERVE_SERIAL] flag, the serial number
1421 /// will be assigned by @self and set on @message via
1422 /// g_dbus_message_set_serial(). If @out_serial is not [`None`], then the
1423 /// serial number used will be written to this location prior to
1424 /// submitting the message to the underlying transport. While it has a `volatile`
1425 /// qualifier, this is a historical artifact and the argument passed to it should
1426 /// not be `volatile`.
1427 ///
1428 /// If @self is closed then the operation will fail with
1429 /// [`IOErrorEnum::Closed`][crate::IOErrorEnum::Closed]. If @cancellable is canceled, the operation will
1430 /// fail with [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled]. If @message is not well-formed,
1431 /// the operation fails with [`IOErrorEnum::InvalidArgument`][crate::IOErrorEnum::InvalidArgument].
1432 ///
1433 /// Note that @error is only set if a local in-process error
1434 /// occurred. That is to say that the returned #GDBusMessage object may
1435 /// be of type [`DBusMessageType::Error`][crate::DBusMessageType::Error]. Use
1436 /// g_dbus_message_to_gerror() to transcode this to a #GError.
1437 ///
1438 /// See this [server][`DBusConnection`][crate::DBusConnection]#an-example-d-bus-server]
1439 /// and [client][`DBusConnection`][crate::DBusConnection]#an-example-for-file-descriptor-passing]
1440 /// for an example of how to use this low-level API to send and receive
1441 /// UNIX file descriptors.
1442 ///
1443 /// Note that @message must be unlocked, unless @flags contain the
1444 /// [`DBusSendMessageFlags::PRESERVE_SERIAL`][crate::DBusSendMessageFlags::PRESERVE_SERIAL] flag.
1445 /// ## `message`
1446 /// a #GDBusMessage
1447 /// ## `flags`
1448 /// flags affecting how the message is sent.
1449 /// ## `timeout_msec`
1450 /// the timeout in milliseconds, -1 to use the default
1451 /// timeout or `G_MAXINT` for no timeout
1452 /// ## `cancellable`
1453 /// a #GCancellable or [`None`]
1454 ///
1455 /// # Returns
1456 ///
1457 /// a locked #GDBusMessage that is the reply
1458 /// to @message or [`None`] if @error is set
1459 ///
1460 /// ## `out_serial`
1461 /// return location for serial number
1462 /// assigned to @message when sending it or [`None`]
1463 #[doc(alias = "g_dbus_connection_send_message_with_reply_sync")]
1464 pub fn send_message_with_reply_sync(
1465 &self,
1466 message: &DBusMessage,
1467 flags: DBusSendMessageFlags,
1468 timeout_msec: i32,
1469 cancellable: Option<&impl IsA<Cancellable>>,
1470 ) -> Result<(DBusMessage, u32), glib::Error> {
1471 unsafe {
1472 let mut out_serial = std::mem::MaybeUninit::uninit();
1473 let mut error = std::ptr::null_mut();
1474 let ret = ffi::g_dbus_connection_send_message_with_reply_sync(
1475 self.to_glib_none().0,
1476 message.to_glib_none().0,
1477 flags.into_glib(),
1478 timeout_msec,
1479 out_serial.as_mut_ptr(),
1480 cancellable.map(|p| p.as_ref()).to_glib_none().0,
1481 &mut error,
1482 );
1483 if error.is_null() {
1484 Ok((from_glib_full(ret), out_serial.assume_init()))
1485 } else {
1486 Err(from_glib_full(error))
1487 }
1488 }
1489 }
1490
1491 /// Sets whether the process should be terminated when @self is
1492 /// closed by the remote peer. See #GDBusConnection:exit-on-close for
1493 /// more details.
1494 ///
1495 /// Note that this function should be used with care. Most modern UNIX
1496 /// desktops tie the notion of a user session with the session bus, and expect
1497 /// all of a user's applications to quit when their bus connection goes away.
1498 /// If you are setting @exit_on_close to [`false`] for the shared session
1499 /// bus connection, you should make sure that your application exits
1500 /// when the user session ends.
1501 /// ## `exit_on_close`
1502 /// whether the process should be terminated
1503 /// when @self is closed by the remote peer
1504 #[doc(alias = "g_dbus_connection_set_exit_on_close")]
1505 #[doc(alias = "exit-on-close")]
1506 pub fn set_exit_on_close(&self, exit_on_close: bool) {
1507 unsafe {
1508 ffi::g_dbus_connection_set_exit_on_close(
1509 self.to_glib_none().0,
1510 exit_on_close.into_glib(),
1511 );
1512 }
1513 }
1514
1515 /// If @self was created with
1516 /// [`DBusConnectionFlags::DELAY_MESSAGE_PROCESSING`][crate::DBusConnectionFlags::DELAY_MESSAGE_PROCESSING], this method
1517 /// starts processing messages. Does nothing on if @self wasn't
1518 /// created with this flag or if the method has already been called.
1519 #[doc(alias = "g_dbus_connection_start_message_processing")]
1520 pub fn start_message_processing(&self) {
1521 unsafe {
1522 ffi::g_dbus_connection_start_message_processing(self.to_glib_none().0);
1523 }
1524 }
1525
1526 #[cfg(not(feature = "v2_60"))]
1527 #[cfg_attr(docsrs, doc(cfg(not(feature = "v2_60"))))]
1528 pub fn flags(&self) -> DBusConnectionFlags {
1529 ObjectExt::property(self, "flags")
1530 }
1531
1532 /// Asynchronously sets up a D-Bus connection for exchanging D-Bus messages
1533 /// with the end represented by @stream.
1534 ///
1535 /// If @stream is a #GSocketConnection, then the corresponding #GSocket
1536 /// will be put into non-blocking mode.
1537 ///
1538 /// The D-Bus connection will interact with @stream from a worker thread.
1539 /// As a result, the caller should not interact with @stream after this
1540 /// method has been called, except by calling g_object_unref() on it.
1541 ///
1542 /// If @observer is not [`None`] it may be used to control the
1543 /// authentication process.
1544 ///
1545 /// When the operation is finished, @callback will be invoked. You can
1546 /// then call g_dbus_connection_new_finish() to get the result of the
1547 /// operation.
1548 ///
1549 /// This is an asynchronous failable constructor. See
1550 /// g_dbus_connection_new_sync() for the synchronous
1551 /// version.
1552 /// ## `stream`
1553 /// a #GIOStream
1554 /// ## `guid`
1555 /// the GUID to use if authenticating as a server or [`None`]
1556 /// ## `flags`
1557 /// flags describing how to make the connection
1558 /// ## `observer`
1559 /// a #GDBusAuthObserver or [`None`]
1560 /// ## `cancellable`
1561 /// a #GCancellable or [`None`]
1562 /// ## `callback`
1563 /// a #GAsyncReadyCallback to call when the request is satisfied
1564 #[doc(alias = "g_dbus_connection_new")]
1565 pub fn new<P: FnOnce(Result<DBusConnection, glib::Error>) + 'static>(
1566 stream: &impl IsA<IOStream>,
1567 guid: Option<&str>,
1568 flags: DBusConnectionFlags,
1569 observer: Option<&DBusAuthObserver>,
1570 cancellable: Option<&impl IsA<Cancellable>>,
1571 callback: P,
1572 ) {
1573 let main_context = glib::MainContext::ref_thread_default();
1574 let is_main_context_owner = main_context.is_owner();
1575 let has_acquired_main_context = (!is_main_context_owner)
1576 .then(|| main_context.acquire().ok())
1577 .flatten();
1578 assert!(
1579 is_main_context_owner || has_acquired_main_context.is_some(),
1580 "Async operations only allowed if the thread is owning the MainContext"
1581 );
1582
1583 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
1584 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
1585 unsafe extern "C" fn new_trampoline<
1586 P: FnOnce(Result<DBusConnection, glib::Error>) + 'static,
1587 >(
1588 _source_object: *mut glib::gobject_ffi::GObject,
1589 res: *mut crate::ffi::GAsyncResult,
1590 user_data: glib::ffi::gpointer,
1591 ) {
1592 unsafe {
1593 let mut error = std::ptr::null_mut();
1594 let ret = ffi::g_dbus_connection_new_finish(res, &mut error);
1595 let result = if error.is_null() {
1596 Ok(from_glib_full(ret))
1597 } else {
1598 Err(from_glib_full(error))
1599 };
1600 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
1601 Box_::from_raw(user_data as *mut _);
1602 let callback: P = callback.into_inner();
1603 callback(result);
1604 }
1605 }
1606 let callback = new_trampoline::<P>;
1607 unsafe {
1608 ffi::g_dbus_connection_new(
1609 stream.as_ref().to_glib_none().0,
1610 guid.to_glib_none().0,
1611 flags.into_glib(),
1612 observer.to_glib_none().0,
1613 cancellable.map(|p| p.as_ref()).to_glib_none().0,
1614 Some(callback),
1615 Box_::into_raw(user_data) as *mut _,
1616 );
1617 }
1618 }
1619
1620 pub fn new_future(
1621 stream: &(impl IsA<IOStream> + Clone + 'static),
1622 guid: Option<&str>,
1623 flags: DBusConnectionFlags,
1624 observer: Option<&DBusAuthObserver>,
1625 ) -> Pin<Box_<dyn std::future::Future<Output = Result<DBusConnection, glib::Error>> + 'static>>
1626 {
1627 let stream = stream.clone();
1628 let guid = guid.map(ToOwned::to_owned);
1629 let observer = observer.map(ToOwned::to_owned);
1630 Box_::pin(crate::GioFuture::new(
1631 &(),
1632 move |_obj, cancellable, send| {
1633 Self::new(
1634 &stream,
1635 guid.as_ref().map(::std::borrow::Borrow::borrow),
1636 flags,
1637 observer.as_ref().map(::std::borrow::Borrow::borrow),
1638 Some(cancellable),
1639 move |res| {
1640 send.resolve(res);
1641 },
1642 );
1643 },
1644 ))
1645 }
1646
1647 /// Asynchronously connects and sets up a D-Bus client connection for
1648 /// exchanging D-Bus messages with an endpoint specified by @address
1649 /// which must be in the
1650 /// [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
1651 ///
1652 /// This constructor can only be used to initiate client-side
1653 /// connections - use g_dbus_connection_new() if you need to act as the
1654 /// server. In particular, @flags cannot contain the
1655 /// [`DBusConnectionFlags::AUTHENTICATION_SERVER`][crate::DBusConnectionFlags::AUTHENTICATION_SERVER],
1656 /// [`DBusConnectionFlags::AUTHENTICATION_ALLOW_ANONYMOUS`][crate::DBusConnectionFlags::AUTHENTICATION_ALLOW_ANONYMOUS] or
1657 /// [`DBusConnectionFlags::AUTHENTICATION_REQUIRE_SAME_USER`][crate::DBusConnectionFlags::AUTHENTICATION_REQUIRE_SAME_USER] flags.
1658 ///
1659 /// When the operation is finished, @callback will be invoked. You can
1660 /// then call g_dbus_connection_new_for_address_finish() to get the result of
1661 /// the operation.
1662 ///
1663 /// If @observer is not [`None`] it may be used to control the
1664 /// authentication process.
1665 ///
1666 /// This is an asynchronous failable constructor. See
1667 /// g_dbus_connection_new_for_address_sync() for the synchronous
1668 /// version.
1669 /// ## `address`
1670 /// a D-Bus address
1671 /// ## `flags`
1672 /// flags describing how to make the connection
1673 /// ## `observer`
1674 /// a #GDBusAuthObserver or [`None`]
1675 /// ## `cancellable`
1676 /// a #GCancellable or [`None`]
1677 /// ## `callback`
1678 /// a #GAsyncReadyCallback to call when the request is satisfied
1679 #[doc(alias = "g_dbus_connection_new_for_address")]
1680 #[doc(alias = "new_for_address")]
1681 pub fn for_address<P: FnOnce(Result<DBusConnection, glib::Error>) + 'static>(
1682 address: &str,
1683 flags: DBusConnectionFlags,
1684 observer: Option<&DBusAuthObserver>,
1685 cancellable: Option<&impl IsA<Cancellable>>,
1686 callback: P,
1687 ) {
1688 let main_context = glib::MainContext::ref_thread_default();
1689 let is_main_context_owner = main_context.is_owner();
1690 let has_acquired_main_context = (!is_main_context_owner)
1691 .then(|| main_context.acquire().ok())
1692 .flatten();
1693 assert!(
1694 is_main_context_owner || has_acquired_main_context.is_some(),
1695 "Async operations only allowed if the thread is owning the MainContext"
1696 );
1697
1698 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
1699 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
1700 unsafe extern "C" fn for_address_trampoline<
1701 P: FnOnce(Result<DBusConnection, glib::Error>) + 'static,
1702 >(
1703 _source_object: *mut glib::gobject_ffi::GObject,
1704 res: *mut crate::ffi::GAsyncResult,
1705 user_data: glib::ffi::gpointer,
1706 ) {
1707 unsafe {
1708 let mut error = std::ptr::null_mut();
1709 let ret = ffi::g_dbus_connection_new_for_address_finish(res, &mut error);
1710 let result = if error.is_null() {
1711 Ok(from_glib_full(ret))
1712 } else {
1713 Err(from_glib_full(error))
1714 };
1715 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
1716 Box_::from_raw(user_data as *mut _);
1717 let callback: P = callback.into_inner();
1718 callback(result);
1719 }
1720 }
1721 let callback = for_address_trampoline::<P>;
1722 unsafe {
1723 ffi::g_dbus_connection_new_for_address(
1724 address.to_glib_none().0,
1725 flags.into_glib(),
1726 observer.to_glib_none().0,
1727 cancellable.map(|p| p.as_ref()).to_glib_none().0,
1728 Some(callback),
1729 Box_::into_raw(user_data) as *mut _,
1730 );
1731 }
1732 }
1733
1734 pub fn for_address_future(
1735 address: &str,
1736 flags: DBusConnectionFlags,
1737 observer: Option<&DBusAuthObserver>,
1738 ) -> Pin<Box_<dyn std::future::Future<Output = Result<DBusConnection, glib::Error>> + 'static>>
1739 {
1740 let address = String::from(address);
1741 let observer = observer.map(ToOwned::to_owned);
1742 Box_::pin(crate::GioFuture::new(
1743 &(),
1744 move |_obj, cancellable, send| {
1745 Self::for_address(
1746 &address,
1747 flags,
1748 observer.as_ref().map(::std::borrow::Borrow::borrow),
1749 Some(cancellable),
1750 move |res| {
1751 send.resolve(res);
1752 },
1753 );
1754 },
1755 ))
1756 }
1757
1758 /// Emitted when the connection is closed.
1759 ///
1760 /// The cause of this event can be
1761 ///
1762 /// - If g_dbus_connection_close() is called. In this case
1763 /// @remote_peer_vanished is set to [`false`] and @error is [`None`].
1764 ///
1765 /// - If the remote peer closes the connection. In this case
1766 /// @remote_peer_vanished is set to [`true`] and @error is set.
1767 ///
1768 /// - If the remote peer sends invalid or malformed data. In this
1769 /// case @remote_peer_vanished is set to [`false`] and @error is set.
1770 ///
1771 /// Upon receiving this signal, you should give up your reference to
1772 /// @connection. You are guaranteed that this signal is emitted only
1773 /// once.
1774 /// ## `remote_peer_vanished`
1775 /// [`true`] if @connection is closed because the
1776 /// remote peer closed its end of the connection
1777 /// ## `error`
1778 /// a #GError with more details about the event or [`None`]
1779 #[doc(alias = "closed")]
1780 pub fn connect_closed<F: Fn(&Self, bool, Option<&glib::Error>) + Send + Sync + 'static>(
1781 &self,
1782 f: F,
1783 ) -> SignalHandlerId {
1784 unsafe extern "C" fn closed_trampoline<
1785 F: Fn(&DBusConnection, bool, Option<&glib::Error>) + Send + Sync + 'static,
1786 >(
1787 this: *mut ffi::GDBusConnection,
1788 remote_peer_vanished: glib::ffi::gboolean,
1789 error: *mut glib::ffi::GError,
1790 f: glib::ffi::gpointer,
1791 ) {
1792 unsafe {
1793 let f: &F = &*(f as *const F);
1794 f(
1795 &from_glib_borrow(this),
1796 from_glib(remote_peer_vanished),
1797 Option::<glib::Error>::from_glib_borrow(error)
1798 .as_ref()
1799 .as_ref(),
1800 )
1801 }
1802 }
1803 unsafe {
1804 let f: Box_<F> = Box_::new(f);
1805 connect_raw(
1806 self.as_ptr() as *mut _,
1807 c"closed".as_ptr(),
1808 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1809 closed_trampoline::<F> as *const (),
1810 )),
1811 Box_::into_raw(f),
1812 )
1813 }
1814 }
1815
1816 #[doc(alias = "capabilities")]
1817 pub fn connect_capabilities_notify<F: Fn(&Self) + Send + Sync + 'static>(
1818 &self,
1819 f: F,
1820 ) -> SignalHandlerId {
1821 unsafe extern "C" fn notify_capabilities_trampoline<
1822 F: Fn(&DBusConnection) + Send + Sync + 'static,
1823 >(
1824 this: *mut ffi::GDBusConnection,
1825 _param_spec: glib::ffi::gpointer,
1826 f: glib::ffi::gpointer,
1827 ) {
1828 unsafe {
1829 let f: &F = &*(f as *const F);
1830 f(&from_glib_borrow(this))
1831 }
1832 }
1833 unsafe {
1834 let f: Box_<F> = Box_::new(f);
1835 connect_raw(
1836 self.as_ptr() as *mut _,
1837 c"notify::capabilities".as_ptr(),
1838 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1839 notify_capabilities_trampoline::<F> as *const (),
1840 )),
1841 Box_::into_raw(f),
1842 )
1843 }
1844 }
1845
1846 #[doc(alias = "closed")]
1847 pub fn connect_closed_notify<F: Fn(&Self) + Send + Sync + 'static>(
1848 &self,
1849 f: F,
1850 ) -> SignalHandlerId {
1851 unsafe extern "C" fn notify_closed_trampoline<
1852 F: Fn(&DBusConnection) + Send + Sync + 'static,
1853 >(
1854 this: *mut ffi::GDBusConnection,
1855 _param_spec: glib::ffi::gpointer,
1856 f: glib::ffi::gpointer,
1857 ) {
1858 unsafe {
1859 let f: &F = &*(f as *const F);
1860 f(&from_glib_borrow(this))
1861 }
1862 }
1863 unsafe {
1864 let f: Box_<F> = Box_::new(f);
1865 connect_raw(
1866 self.as_ptr() as *mut _,
1867 c"notify::closed".as_ptr(),
1868 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1869 notify_closed_trampoline::<F> as *const (),
1870 )),
1871 Box_::into_raw(f),
1872 )
1873 }
1874 }
1875
1876 #[doc(alias = "exit-on-close")]
1877 pub fn connect_exit_on_close_notify<F: Fn(&Self) + Send + Sync + 'static>(
1878 &self,
1879 f: F,
1880 ) -> SignalHandlerId {
1881 unsafe extern "C" fn notify_exit_on_close_trampoline<
1882 F: Fn(&DBusConnection) + Send + Sync + 'static,
1883 >(
1884 this: *mut ffi::GDBusConnection,
1885 _param_spec: glib::ffi::gpointer,
1886 f: glib::ffi::gpointer,
1887 ) {
1888 unsafe {
1889 let f: &F = &*(f as *const F);
1890 f(&from_glib_borrow(this))
1891 }
1892 }
1893 unsafe {
1894 let f: Box_<F> = Box_::new(f);
1895 connect_raw(
1896 self.as_ptr() as *mut _,
1897 c"notify::exit-on-close".as_ptr(),
1898 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1899 notify_exit_on_close_trampoline::<F> as *const (),
1900 )),
1901 Box_::into_raw(f),
1902 )
1903 }
1904 }
1905
1906 #[doc(alias = "unique-name")]
1907 pub fn connect_unique_name_notify<F: Fn(&Self) + Send + Sync + 'static>(
1908 &self,
1909 f: F,
1910 ) -> SignalHandlerId {
1911 unsafe extern "C" fn notify_unique_name_trampoline<
1912 F: Fn(&DBusConnection) + Send + Sync + 'static,
1913 >(
1914 this: *mut ffi::GDBusConnection,
1915 _param_spec: glib::ffi::gpointer,
1916 f: glib::ffi::gpointer,
1917 ) {
1918 unsafe {
1919 let f: &F = &*(f as *const F);
1920 f(&from_glib_borrow(this))
1921 }
1922 }
1923 unsafe {
1924 let f: Box_<F> = Box_::new(f);
1925 connect_raw(
1926 self.as_ptr() as *mut _,
1927 c"notify::unique-name".as_ptr(),
1928 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1929 notify_unique_name_trampoline::<F> as *const (),
1930 )),
1931 Box_::into_raw(f),
1932 )
1933 }
1934 }
1935}
1936
1937unsafe impl Send for DBusConnection {}
1938unsafe impl Sync for DBusConnection {}