Skip to main content

gio/auto/
functions.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::{
6    AsyncResult, BusType, Cancellable, DBusConnection, File, IOErrorEnum, IOModule, IOModuleScope,
7    IOStream, Icon, InputStream, Resource, ResourceLookupFlags, SettingsBackend, ffi,
8};
9use glib::{prelude::*, translate::*};
10use std::{boxed::Box as Box_, pin::Pin};
11
12/// Asynchronously connects to the message bus specified by @bus_type.
13///
14/// When the operation is finished, @callback will be invoked. You can
15/// then call g_bus_get_finish() to get the result of the operation.
16///
17/// This is an asynchronous failable function. See g_bus_get_sync() for
18/// the synchronous version.
19/// ## `bus_type`
20/// a #GBusType
21/// ## `cancellable`
22/// a #GCancellable or [`None`]
23/// ## `callback`
24/// a #GAsyncReadyCallback to call when the request is satisfied
25#[doc(alias = "g_bus_get")]
26pub fn bus_get<P: FnOnce(Result<DBusConnection, glib::Error>) + 'static>(
27    bus_type: BusType,
28    cancellable: Option<&impl IsA<Cancellable>>,
29    callback: P,
30) {
31    let main_context = glib::MainContext::ref_thread_default();
32    let is_main_context_owner = main_context.is_owner();
33    let has_acquired_main_context = (!is_main_context_owner)
34        .then(|| main_context.acquire().ok())
35        .flatten();
36    assert!(
37        is_main_context_owner || has_acquired_main_context.is_some(),
38        "Async operations only allowed if the thread is owning the MainContext"
39    );
40
41    let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
42        Box_::new(glib::thread_guard::ThreadGuard::new(callback));
43    unsafe extern "C" fn bus_get_trampoline<
44        P: FnOnce(Result<DBusConnection, glib::Error>) + 'static,
45    >(
46        _source_object: *mut glib::gobject_ffi::GObject,
47        res: *mut crate::ffi::GAsyncResult,
48        user_data: glib::ffi::gpointer,
49    ) {
50        unsafe {
51            let mut error = std::ptr::null_mut();
52            let ret = ffi::g_bus_get_finish(res, &mut error);
53            let result = if error.is_null() {
54                Ok(from_glib_full(ret))
55            } else {
56                Err(from_glib_full(error))
57            };
58            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
59                Box_::from_raw(user_data as *mut _);
60            let callback: P = callback.into_inner();
61            callback(result);
62        }
63    }
64    let callback = bus_get_trampoline::<P>;
65    unsafe {
66        ffi::g_bus_get(
67            bus_type.into_glib(),
68            cancellable.map(|p| p.as_ref()).to_glib_none().0,
69            Some(callback),
70            Box_::into_raw(user_data) as *mut _,
71        );
72    }
73}
74
75pub fn bus_get_future(
76    bus_type: BusType,
77) -> Pin<Box_<dyn std::future::Future<Output = Result<DBusConnection, glib::Error>> + 'static>> {
78    Box_::pin(crate::GioFuture::new(
79        &(),
80        move |_obj, cancellable, send| {
81            bus_get(bus_type, Some(cancellable), move |res| {
82                send.resolve(res);
83            });
84        },
85    ))
86}
87
88/// Synchronously connects to the message bus specified by @bus_type.
89/// Note that the returned object may shared with other callers,
90/// e.g. if two separate parts of a process calls this function with
91/// the same @bus_type, they will share the same object.
92///
93/// This is a synchronous failable function. See g_bus_get() and
94/// g_bus_get_finish() for the asynchronous version.
95///
96/// The returned object is a singleton, that is, shared with other
97/// callers of g_bus_get() and g_bus_get_sync() for @bus_type. In the
98/// event that you need a private message bus connection, use
99/// g_dbus_address_get_for_bus_sync() and
100/// g_dbus_connection_new_for_address() with
101/// G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT and
102/// G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION flags.
103///
104/// Note that the returned #GDBusConnection object will (usually) have
105/// the #GDBusConnection:exit-on-close property set to [`true`].
106/// ## `bus_type`
107/// a #GBusType
108/// ## `cancellable`
109/// a #GCancellable or [`None`]
110///
111/// # Returns
112///
113/// a #GDBusConnection or [`None`] if @error is set.
114///     Free with g_object_unref().
115#[doc(alias = "g_bus_get_sync")]
116pub fn bus_get_sync(
117    bus_type: BusType,
118    cancellable: Option<&impl IsA<Cancellable>>,
119) -> Result<DBusConnection, glib::Error> {
120    unsafe {
121        let mut error = std::ptr::null_mut();
122        let ret = ffi::g_bus_get_sync(
123            bus_type.into_glib(),
124            cancellable.map(|p| p.as_ref()).to_glib_none().0,
125            &mut error,
126        );
127        if error.is_null() {
128            Ok(from_glib_full(ret))
129        } else {
130            Err(from_glib_full(error))
131        }
132    }
133}
134
135//#[doc(alias = "g_bus_own_name")]
136//pub fn bus_own_name(bus_type: BusType, name: &str, flags: BusNameOwnerFlags, bus_acquired_handler: Option<Box_<dyn Fn(&DBusConnection, &str) + 'static>>, name_acquired_handler: Option<Box_<dyn Fn(&DBusConnection, &str) + 'static>>, name_lost_handler: Option<Box_<dyn Fn(&DBusConnection, &str) + 'static>>) -> u32 {
137//    unsafe { TODO: call ffi:g_bus_own_name() }
138//}
139
140//#[doc(alias = "g_bus_own_name_on_connection")]
141//pub fn bus_own_name_on_connection(connection: &DBusConnection, name: &str, flags: BusNameOwnerFlags, name_acquired_handler: Option<Box_<dyn Fn(&DBusConnection, &str) + 'static>>, name_lost_handler: Option<Box_<dyn Fn(&DBusConnection, &str) + 'static>>) -> u32 {
142//    unsafe { TODO: call ffi:g_bus_own_name_on_connection() }
143//}
144
145//#[doc(alias = "g_bus_watch_name")]
146//pub fn bus_watch_name(bus_type: BusType, name: &str, flags: BusNameWatcherFlags, name_appeared_handler: Option<Box_<dyn Fn(&DBusConnection, &str, &str) + 'static>>, name_vanished_handler: Option<Box_<dyn Fn(&DBusConnection, &str) + 'static>>) -> u32 {
147//    unsafe { TODO: call ffi:g_bus_watch_name() }
148//}
149
150//#[doc(alias = "g_bus_watch_name_on_connection")]
151//pub fn bus_watch_name_on_connection(connection: &DBusConnection, name: &str, flags: BusNameWatcherFlags, name_appeared_handler: Option<Box_<dyn Fn(&DBusConnection, &str, &str) + 'static>>, name_vanished_handler: Option<Box_<dyn Fn(&DBusConnection, &str) + 'static>>) -> u32 {
152//    unsafe { TODO: call ffi:g_bus_watch_name_on_connection() }
153//}
154
155/// Checks if a content type can be executable. Note that for instance
156/// things like text files can be executables (i.e. scripts and batch files).
157/// ## `type_`
158/// a content type string
159///
160/// # Returns
161///
162/// [`true`] if the file type corresponds to a type that
163///     can be executable, [`false`] otherwise.
164#[doc(alias = "g_content_type_can_be_executable")]
165pub fn content_type_can_be_executable(type_: &str) -> bool {
166    unsafe {
167        from_glib(ffi::g_content_type_can_be_executable(
168            type_.to_glib_none().0,
169        ))
170    }
171}
172
173/// Compares two content types for equality.
174/// ## `type1`
175/// a content type string
176/// ## `type2`
177/// a content type string
178///
179/// # Returns
180///
181/// [`true`] if the two strings are identical or equivalent,
182///     [`false`] otherwise.
183#[doc(alias = "g_content_type_equals")]
184pub fn content_type_equals(type1: &str, type2: &str) -> bool {
185    unsafe {
186        from_glib(ffi::g_content_type_equals(
187            type1.to_glib_none().0,
188            type2.to_glib_none().0,
189        ))
190    }
191}
192
193/// Tries to find a content type based on the mime type name.
194/// ## `mime_type`
195/// a mime type string
196///
197/// # Returns
198///
199/// Newly allocated string with content type or
200///     [`None`]. Free with g_free()
201#[doc(alias = "g_content_type_from_mime_type")]
202pub fn content_type_from_mime_type(mime_type: &str) -> Option<glib::GString> {
203    unsafe {
204        from_glib_full(ffi::g_content_type_from_mime_type(
205            mime_type.to_glib_none().0,
206        ))
207    }
208}
209
210/// Gets the human readable description of the content type.
211/// ## `type_`
212/// a content type string
213///
214/// # Returns
215///
216/// a short description of the content type @type_. Free the
217///     returned string with g_free()
218#[doc(alias = "g_content_type_get_description")]
219pub fn content_type_get_description(type_: &str) -> glib::GString {
220    unsafe { from_glib_full(ffi::g_content_type_get_description(type_.to_glib_none().0)) }
221}
222
223/// Gets the generic icon name for a content type.
224///
225/// See the
226/// [shared-mime-info](http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec)
227/// specification for more on the generic icon name.
228/// ## `type_`
229/// a content type string
230///
231/// # Returns
232///
233/// the registered generic icon name for the given @type_,
234///     or [`None`] if unknown. Free with g_free()
235#[doc(alias = "g_content_type_get_generic_icon_name")]
236pub fn content_type_get_generic_icon_name(type_: &str) -> Option<glib::GString> {
237    unsafe {
238        from_glib_full(ffi::g_content_type_get_generic_icon_name(
239            type_.to_glib_none().0,
240        ))
241    }
242}
243
244/// Gets the icon for a content type.
245/// ## `type_`
246/// a content type string
247///
248/// # Returns
249///
250/// #GIcon corresponding to the content type. Free the returned
251///     object with g_object_unref()
252#[doc(alias = "g_content_type_get_icon")]
253pub fn content_type_get_icon(type_: &str) -> Icon {
254    unsafe { from_glib_full(ffi::g_content_type_get_icon(type_.to_glib_none().0)) }
255}
256
257/// Get the list of directories which MIME data is loaded from. See
258/// g_content_type_set_mime_dirs() for details.
259///
260/// # Returns
261///
262/// [`None`]-terminated list of
263///    directories to load MIME data from, including any `mime/` subdirectory,
264///    and with the first directory to try listed first
265#[cfg(feature = "v2_60")]
266#[cfg_attr(docsrs, doc(cfg(feature = "v2_60")))]
267#[doc(alias = "g_content_type_get_mime_dirs")]
268pub fn content_type_get_mime_dirs() -> Vec<glib::GString> {
269    unsafe { FromGlibPtrContainer::from_glib_none(ffi::g_content_type_get_mime_dirs()) }
270}
271
272/// Gets the mime type for the content type, if one is registered.
273/// ## `type_`
274/// a content type string
275///
276/// # Returns
277///
278/// the registered mime type for the
279///     given @type_, or [`None`] if unknown; free with g_free().
280#[doc(alias = "g_content_type_get_mime_type")]
281pub fn content_type_get_mime_type(type_: &str) -> Option<glib::GString> {
282    unsafe { from_glib_full(ffi::g_content_type_get_mime_type(type_.to_glib_none().0)) }
283}
284
285/// Gets the symbolic icon for a content type.
286/// ## `type_`
287/// a content type string
288///
289/// # Returns
290///
291/// symbolic #GIcon corresponding to the content type.
292///     Free the returned object with g_object_unref()
293#[doc(alias = "g_content_type_get_symbolic_icon")]
294pub fn content_type_get_symbolic_icon(type_: &str) -> Icon {
295    unsafe {
296        from_glib_full(ffi::g_content_type_get_symbolic_icon(
297            type_.to_glib_none().0,
298        ))
299    }
300}
301
302/// Tries to guess the type of the tree with root @root, by
303/// looking at the files it contains. The result is an array
304/// of content types, with the best guess coming first.
305///
306/// The types returned all have the form x-content/foo, e.g.
307/// x-content/audio-cdda (for audio CDs) or x-content/image-dcf
308/// (for a camera memory card). See the
309/// [shared-mime-info](http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec)
310/// specification for more on x-content types.
311///
312/// This function is useful in the implementation of
313/// g_mount_guess_content_type().
314/// ## `root`
315/// the root of the tree to guess a type for
316///
317/// # Returns
318///
319/// an [`None`]-terminated
320///     array of zero or more content types. Free with g_strfreev()
321#[doc(alias = "g_content_type_guess_for_tree")]
322pub fn content_type_guess_for_tree(root: &impl IsA<File>) -> Vec<glib::GString> {
323    unsafe {
324        FromGlibPtrContainer::from_glib_full(ffi::g_content_type_guess_for_tree(
325            root.as_ref().to_glib_none().0,
326        ))
327    }
328}
329
330/// Determines if @type_ is a subset of @supertype.
331/// ## `type_`
332/// a content type string
333/// ## `supertype`
334/// a content type string
335///
336/// # Returns
337///
338/// [`true`] if @type_ is a kind of @supertype,
339///     [`false`] otherwise.
340#[doc(alias = "g_content_type_is_a")]
341pub fn content_type_is_a(type_: &str, supertype: &str) -> bool {
342    unsafe {
343        from_glib(ffi::g_content_type_is_a(
344            type_.to_glib_none().0,
345            supertype.to_glib_none().0,
346        ))
347    }
348}
349
350/// Determines if @type_ is a subset of @mime_type.
351/// Convenience wrapper around g_content_type_is_a().
352/// ## `type_`
353/// a content type string
354/// ## `mime_type`
355/// a mime type string
356///
357/// # Returns
358///
359/// [`true`] if @type_ is a kind of @mime_type,
360///     [`false`] otherwise.
361#[doc(alias = "g_content_type_is_mime_type")]
362pub fn content_type_is_mime_type(type_: &str, mime_type: &str) -> bool {
363    unsafe {
364        from_glib(ffi::g_content_type_is_mime_type(
365            type_.to_glib_none().0,
366            mime_type.to_glib_none().0,
367        ))
368    }
369}
370
371/// Checks if the content type is the generic "unknown" type.
372/// On UNIX this is the "application/octet-stream" mimetype,
373/// while on win32 it is "*" and on OSX it is a dynamic type
374/// or octet-stream.
375/// ## `type_`
376/// a content type string
377///
378/// # Returns
379///
380/// [`true`] if the type is the unknown type.
381#[doc(alias = "g_content_type_is_unknown")]
382pub fn content_type_is_unknown(type_: &str) -> bool {
383    unsafe { from_glib(ffi::g_content_type_is_unknown(type_.to_glib_none().0)) }
384}
385
386///
387///
388///   return g_test_run ();
389/// ]|
390/// ## `dirs`
391/// [`None`]-terminated list of
392///    directories to load MIME data from, including any `mime/` subdirectory,
393///    and with the first directory to try listed first
394#[cfg(feature = "v2_60")]
395#[cfg_attr(docsrs, doc(cfg(feature = "v2_60")))]
396#[doc(alias = "g_content_type_set_mime_dirs")]
397pub fn content_type_set_mime_dirs(dirs: &[&str]) {
398    unsafe {
399        ffi::g_content_type_set_mime_dirs(dirs.to_glib_none().0);
400    }
401}
402
403/// Gets a list of strings containing all the registered content types
404/// known to the system. The list and its data should be freed using
405/// `g_list_free_full (list, g_free)`.
406///
407/// # Returns
408///
409/// list of the registered
410///     content types
411#[doc(alias = "g_content_types_get_registered")]
412pub fn content_types_get_registered() -> Vec<glib::GString> {
413    unsafe { FromGlibPtrContainer::from_glib_full(ffi::g_content_types_get_registered()) }
414}
415
416/// Escape @string so it can appear in a D-Bus address as the value
417/// part of a key-value pair.
418///
419/// For instance, if @string is `/run/bus-for-:0`,
420/// this function would return `/run/bus-for-`3A0``,
421/// which could be used in a D-Bus address like
422/// `unix:nonce-tcp:host=127.0.0.1,port=42,noncefile=/run/bus-for-`3A0``.
423/// ## `string`
424/// an unescaped string to be included in a D-Bus address
425///     as the value in a key-value pair
426///
427/// # Returns
428///
429/// a copy of @string with all
430///     non-optionally-escaped bytes escaped
431#[doc(alias = "g_dbus_address_escape_value")]
432pub fn dbus_address_escape_value(string: &str) -> glib::GString {
433    unsafe { from_glib_full(ffi::g_dbus_address_escape_value(string.to_glib_none().0)) }
434}
435
436/// Synchronously looks up the D-Bus address for the well-known message
437/// bus instance specified by @bus_type. This may involve using various
438/// platform specific mechanisms.
439///
440/// The returned address will be in the
441/// [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
442/// ## `bus_type`
443/// a #GBusType
444/// ## `cancellable`
445/// a #GCancellable or [`None`]
446///
447/// # Returns
448///
449/// a valid D-Bus address string for @bus_type or
450///     [`None`] if @error is set
451#[doc(alias = "g_dbus_address_get_for_bus_sync")]
452pub fn dbus_address_get_for_bus_sync(
453    bus_type: BusType,
454    cancellable: Option<&impl IsA<Cancellable>>,
455) -> Result<glib::GString, glib::Error> {
456    unsafe {
457        let mut error = std::ptr::null_mut();
458        let ret = ffi::g_dbus_address_get_for_bus_sync(
459            bus_type.into_glib(),
460            cancellable.map(|p| p.as_ref()).to_glib_none().0,
461            &mut error,
462        );
463        if error.is_null() {
464            Ok(from_glib_full(ret))
465        } else {
466            Err(from_glib_full(error))
467        }
468    }
469}
470
471/// Asynchronously connects to an endpoint specified by @address and
472/// sets up the connection so it is in a state to run the client-side
473/// of the D-Bus authentication conversation. @address must be in the
474/// [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
475///
476/// When the operation is finished, @callback will be invoked. You can
477/// then call g_dbus_address_get_stream_finish() to get the result of
478/// the operation.
479///
480/// This is an asynchronous failable function. See
481/// g_dbus_address_get_stream_sync() for the synchronous version.
482/// ## `address`
483/// A valid D-Bus address.
484/// ## `cancellable`
485/// A #GCancellable or [`None`].
486/// ## `callback`
487/// A #GAsyncReadyCallback to call when the request is satisfied.
488#[doc(alias = "g_dbus_address_get_stream")]
489pub fn dbus_address_get_stream<
490    P: FnOnce(Result<(IOStream, Option<glib::GString>), glib::Error>) + 'static,
491>(
492    address: &str,
493    cancellable: Option<&impl IsA<Cancellable>>,
494    callback: P,
495) {
496    let main_context = glib::MainContext::ref_thread_default();
497    let is_main_context_owner = main_context.is_owner();
498    let has_acquired_main_context = (!is_main_context_owner)
499        .then(|| main_context.acquire().ok())
500        .flatten();
501    assert!(
502        is_main_context_owner || has_acquired_main_context.is_some(),
503        "Async operations only allowed if the thread is owning the MainContext"
504    );
505
506    let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
507        Box_::new(glib::thread_guard::ThreadGuard::new(callback));
508    unsafe extern "C" fn dbus_address_get_stream_trampoline<
509        P: FnOnce(Result<(IOStream, Option<glib::GString>), glib::Error>) + 'static,
510    >(
511        _source_object: *mut glib::gobject_ffi::GObject,
512        res: *mut crate::ffi::GAsyncResult,
513        user_data: glib::ffi::gpointer,
514    ) {
515        unsafe {
516            let mut error = std::ptr::null_mut();
517            let mut out_guid = std::ptr::null_mut();
518            let ret = ffi::g_dbus_address_get_stream_finish(res, &mut out_guid, &mut error);
519            let result = if error.is_null() {
520                Ok((from_glib_full(ret), from_glib_full(out_guid)))
521            } else {
522                Err(from_glib_full(error))
523            };
524            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
525                Box_::from_raw(user_data as *mut _);
526            let callback: P = callback.into_inner();
527            callback(result);
528        }
529    }
530    let callback = dbus_address_get_stream_trampoline::<P>;
531    unsafe {
532        ffi::g_dbus_address_get_stream(
533            address.to_glib_none().0,
534            cancellable.map(|p| p.as_ref()).to_glib_none().0,
535            Some(callback),
536            Box_::into_raw(user_data) as *mut _,
537        );
538    }
539}
540
541pub fn dbus_address_get_stream_future(
542    address: &str,
543) -> Pin<
544    Box_<
545        dyn std::future::Future<Output = Result<(IOStream, Option<glib::GString>), glib::Error>>
546            + 'static,
547    >,
548> {
549    let address = String::from(address);
550    Box_::pin(crate::GioFuture::new(
551        &(),
552        move |_obj, cancellable, send| {
553            dbus_address_get_stream(&address, Some(cancellable), move |res| {
554                send.resolve(res);
555            });
556        },
557    ))
558}
559
560/// Synchronously connects to an endpoint specified by @address and
561/// sets up the connection so it is in a state to run the client-side
562/// of the D-Bus authentication conversation. @address must be in the
563/// [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
564///
565/// A server is not required to set a GUID, so @out_guid may be set to [`None`]
566/// even on success.
567///
568/// This is a synchronous failable function. See
569/// g_dbus_address_get_stream() for the asynchronous version.
570/// ## `address`
571/// A valid D-Bus address.
572/// ## `cancellable`
573/// A #GCancellable or [`None`].
574///
575/// # Returns
576///
577/// A #GIOStream or [`None`] if @error is set.
578///
579/// ## `out_guid`
580/// [`None`] or return location to store the GUID extracted from @address, if any.
581#[doc(alias = "g_dbus_address_get_stream_sync")]
582pub fn dbus_address_get_stream_sync(
583    address: &str,
584    cancellable: Option<&impl IsA<Cancellable>>,
585) -> Result<(IOStream, Option<glib::GString>), glib::Error> {
586    unsafe {
587        let mut out_guid = std::ptr::null_mut();
588        let mut error = std::ptr::null_mut();
589        let ret = ffi::g_dbus_address_get_stream_sync(
590            address.to_glib_none().0,
591            &mut out_guid,
592            cancellable.map(|p| p.as_ref()).to_glib_none().0,
593            &mut error,
594        );
595        if error.is_null() {
596            Ok((from_glib_full(ret), from_glib_full(out_guid)))
597        } else {
598            Err(from_glib_full(error))
599        }
600    }
601}
602
603/// This is a language binding friendly version of g_dbus_escape_object_path_bytestring().
604/// ## `s`
605/// the string to escape
606///
607/// # Returns
608///
609/// an escaped version of @s. Free with g_free().
610#[cfg(feature = "v2_68")]
611#[cfg_attr(docsrs, doc(cfg(feature = "v2_68")))]
612#[doc(alias = "g_dbus_escape_object_path")]
613pub fn dbus_escape_object_path(s: &str) -> glib::GString {
614    unsafe { from_glib_full(ffi::g_dbus_escape_object_path(s.to_glib_none().0)) }
615}
616
617//#[cfg(feature = "v2_68")]
618//#[cfg_attr(docsrs, doc(cfg(feature = "v2_68")))]
619//#[doc(alias = "g_dbus_escape_object_path_bytestring")]
620//pub fn dbus_escape_object_path_bytestring(bytes: &[u8]) -> glib::GString {
621//    unsafe { TODO: call ffi:g_dbus_escape_object_path_bytestring() }
622//}
623
624/// . The terms are interchangeable.
625///
626/// Note that D-Bus GUIDs do not follow
627/// [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122).
628///
629/// # Returns
630///
631/// A valid D-Bus GUID. Free with g_free().
632#[doc(alias = "g_dbus_generate_guid")]
633pub fn dbus_generate_guid() -> glib::GString {
634    unsafe { from_glib_full(ffi::g_dbus_generate_guid()) }
635}
636
637/// Converts a #GValue to a #GVariant of the type indicated by the @type_
638/// parameter.
639///
640/// The conversion is using the following rules:
641///
642/// - `G_TYPE_STRING`: 's', 'o', 'g' or 'ay'
643/// - `G_TYPE_STRV`: 'as', 'ao' or 'aay'
644/// - `G_TYPE_BOOLEAN`: 'b'
645/// - `G_TYPE_UCHAR`: 'y'
646/// - `G_TYPE_INT`: 'i', 'n'
647/// - `G_TYPE_UINT`: 'u', 'q'
648/// - `G_TYPE_INT64`: 'x'
649/// - `G_TYPE_UINT64`: 't'
650/// - `G_TYPE_DOUBLE`: 'd'
651/// - `G_TYPE_VARIANT`: Any #GVariantType
652///
653/// This can fail if e.g. @gvalue is of type `G_TYPE_STRING` and @type_
654/// is 'i', i.e. `G_VARIANT_TYPE_INT32`. It will also fail for any #GType
655/// (including e.g. `G_TYPE_OBJECT` and `G_TYPE_BOXED` derived-types) not
656/// in the table above.
657///
658/// Note that if @gvalue is of type `G_TYPE_VARIANT` and its value is
659/// [`None`], the empty #GVariant instance (never [`None`]) for @type_ is
660/// returned (e.g. 0 for scalar types, the empty string for string types,
661/// '/' for object path types, the empty array for any array type and so on).
662///
663/// See the g_dbus_gvariant_to_gvalue() function for how to convert a
664/// #GVariant to a #GValue.
665/// ## `gvalue`
666/// A #GValue to convert to a #GVariant
667/// ## `type_`
668/// A #GVariantType
669///
670/// # Returns
671///
672/// A #GVariant (never floating) of
673///     #GVariantType @type_ holding the data from @gvalue or an empty #GVariant
674///     in case of failure. Free with g_variant_unref().
675#[doc(alias = "g_dbus_gvalue_to_gvariant")]
676pub fn dbus_gvalue_to_gvariant(gvalue: &glib::Value, type_: &glib::VariantTy) -> glib::Variant {
677    unsafe {
678        from_glib_full(ffi::g_dbus_gvalue_to_gvariant(
679            gvalue.to_glib_none().0,
680            type_.to_glib_none().0,
681        ))
682    }
683}
684
685/// Converts a #GVariant to a #GValue. If @value is floating, it is consumed.
686///
687/// The rules specified in the g_dbus_gvalue_to_gvariant() function are
688/// used - this function is essentially its reverse form. So, a #GVariant
689/// containing any basic or string array type will be converted to a #GValue
690/// containing a basic value or string array. Any other #GVariant (handle,
691/// variant, tuple, dict entry) will be converted to a #GValue containing that
692/// #GVariant.
693///
694/// The conversion never fails - a valid #GValue is always returned in
695/// @out_gvalue.
696/// ## `value`
697/// A #GVariant.
698///
699/// # Returns
700///
701///
702/// ## `out_gvalue`
703/// Return location pointing to a zero-filled (uninitialized) #GValue.
704#[doc(alias = "g_dbus_gvariant_to_gvalue")]
705pub fn dbus_gvariant_to_gvalue(value: &glib::Variant) -> glib::Value {
706    unsafe {
707        let mut out_gvalue = glib::Value::uninitialized();
708        ffi::g_dbus_gvariant_to_gvalue(value.to_glib_none().0, out_gvalue.to_glib_none_mut().0);
709        out_gvalue
710    }
711}
712
713/// Checks if @string is a
714/// [D-Bus address](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
715///
716/// This doesn't check if @string is actually supported by #GDBusServer
717/// or #GDBusConnection - use g_dbus_is_supported_address() to do more
718/// checks.
719/// ## `string`
720/// A string.
721///
722/// # Returns
723///
724/// [`true`] if @string is a valid D-Bus address, [`false`] otherwise.
725#[doc(alias = "g_dbus_is_address")]
726pub fn dbus_is_address(string: &str) -> bool {
727    unsafe { from_glib(ffi::g_dbus_is_address(string.to_glib_none().0)) }
728}
729
730/// Check whether @string is a valid D-Bus error name.
731///
732/// This function returns the same result as g_dbus_is_interface_name(),
733/// because D-Bus error names are defined to have exactly the
734/// same syntax as interface names.
735/// ## `string`
736/// The string to check.
737///
738/// # Returns
739///
740/// [`true`] if valid, [`false`] otherwise.
741#[cfg(feature = "v2_70")]
742#[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
743#[doc(alias = "g_dbus_is_error_name")]
744pub fn dbus_is_error_name(string: &str) -> bool {
745    unsafe { from_glib(ffi::g_dbus_is_error_name(string.to_glib_none().0)) }
746}
747
748/// Checks if @string is a D-Bus GUID.
749///
750/// See the documentation for g_dbus_generate_guid() for more information about
751/// the format of a GUID.
752/// ## `string`
753/// The string to check.
754///
755/// # Returns
756///
757/// [`true`] if @string is a GUID, [`false`] otherwise.
758#[doc(alias = "g_dbus_is_guid")]
759pub fn dbus_is_guid(string: &str) -> bool {
760    unsafe { from_glib(ffi::g_dbus_is_guid(string.to_glib_none().0)) }
761}
762
763/// Checks if @string is a valid D-Bus interface name.
764/// ## `string`
765/// The string to check.
766///
767/// # Returns
768///
769/// [`true`] if valid, [`false`] otherwise.
770#[doc(alias = "g_dbus_is_interface_name")]
771pub fn dbus_is_interface_name(string: &str) -> bool {
772    unsafe { from_glib(ffi::g_dbus_is_interface_name(string.to_glib_none().0)) }
773}
774
775/// Checks if @string is a valid D-Bus member (e.g. signal or method) name.
776/// ## `string`
777/// The string to check.
778///
779/// # Returns
780///
781/// [`true`] if valid, [`false`] otherwise.
782#[doc(alias = "g_dbus_is_member_name")]
783pub fn dbus_is_member_name(string: &str) -> bool {
784    unsafe { from_glib(ffi::g_dbus_is_member_name(string.to_glib_none().0)) }
785}
786
787/// Checks if @string is a valid D-Bus bus name (either unique or well-known).
788/// ## `string`
789/// The string to check.
790///
791/// # Returns
792///
793/// [`true`] if valid, [`false`] otherwise.
794#[doc(alias = "g_dbus_is_name")]
795pub fn dbus_is_name(string: &str) -> bool {
796    unsafe { from_glib(ffi::g_dbus_is_name(string.to_glib_none().0)) }
797}
798
799/// Like g_dbus_is_address() but also checks if the library supports the
800/// transports in @string and that key/value pairs for each transport
801/// are valid. See the specification of the
802/// [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
803/// ## `string`
804/// A string.
805///
806/// # Returns
807///
808/// [`true`] if @string is a valid D-Bus address that is
809/// supported by this library, [`false`] if @error is set.
810#[doc(alias = "g_dbus_is_supported_address")]
811pub fn dbus_is_supported_address(string: &str) -> Result<(), glib::Error> {
812    unsafe {
813        let mut error = std::ptr::null_mut();
814        let is_ok = ffi::g_dbus_is_supported_address(string.to_glib_none().0, &mut error);
815        debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
816        if error.is_null() {
817            Ok(())
818        } else {
819            Err(from_glib_full(error))
820        }
821    }
822}
823
824/// Checks if @string is a valid D-Bus unique bus name.
825/// ## `string`
826/// The string to check.
827///
828/// # Returns
829///
830/// [`true`] if valid, [`false`] otherwise.
831#[doc(alias = "g_dbus_is_unique_name")]
832pub fn dbus_is_unique_name(string: &str) -> bool {
833    unsafe { from_glib(ffi::g_dbus_is_unique_name(string.to_glib_none().0)) }
834}
835
836///
837///   int saved_errno;
838///
839///   ret = read (blah);
840///   saved_errno = errno;
841///
842///   g_io_error_from_errno (saved_errno);
843/// ]|
844/// ## `err_no`
845/// Error number as defined in errno.h.
846///
847/// # Returns
848///
849/// #GIOErrorEnum value for the given `errno.h` error number
850#[doc(alias = "g_io_error_from_errno")]
851pub fn io_error_from_errno(err_no: i32) -> IOErrorEnum {
852    unsafe { from_glib(ffi::g_io_error_from_errno(err_no)) }
853}
854
855/// Loads all the modules in the specified directory.
856///
857/// If don't require all modules to be initialized (and thus registering
858/// all gtypes) then you can use g_io_modules_scan_all_in_directory()
859/// which allows delayed/lazy loading of modules.
860/// ## `dirname`
861/// pathname for a directory containing modules
862///     to load.
863///
864/// # Returns
865///
866/// a list of #GIOModules loaded
867///      from the directory,
868///      All the modules are loaded into memory, if you want to
869///      unload them (enabling on-demand loading) you must call
870///      g_type_module_unuse() on all the modules. Free the list
871///      with g_list_free().
872#[doc(alias = "g_io_modules_load_all_in_directory")]
873pub fn io_modules_load_all_in_directory(dirname: impl AsRef<std::path::Path>) -> Vec<IOModule> {
874    unsafe {
875        FromGlibPtrContainer::from_glib_full(ffi::g_io_modules_load_all_in_directory(
876            dirname.as_ref().to_glib_none().0,
877        ))
878    }
879}
880
881/// Loads all the modules in the specified directory.
882///
883/// If don't require all modules to be initialized (and thus registering
884/// all gtypes) then you can use g_io_modules_scan_all_in_directory()
885/// which allows delayed/lazy loading of modules.
886/// ## `dirname`
887/// pathname for a directory containing modules
888///     to load.
889/// ## `scope`
890/// a scope to use when scanning the modules.
891///
892/// # Returns
893///
894/// a list of #GIOModules loaded
895///      from the directory,
896///      All the modules are loaded into memory, if you want to
897///      unload them (enabling on-demand loading) you must call
898///      g_type_module_unuse() on all the modules. Free the list
899///      with g_list_free().
900#[doc(alias = "g_io_modules_load_all_in_directory_with_scope")]
901pub fn io_modules_load_all_in_directory_with_scope(
902    dirname: impl AsRef<std::path::Path>,
903    scope: &mut IOModuleScope,
904) -> Vec<IOModule> {
905    unsafe {
906        FromGlibPtrContainer::from_glib_full(ffi::g_io_modules_load_all_in_directory_with_scope(
907            dirname.as_ref().to_glib_none().0,
908            scope.to_glib_none_mut().0,
909        ))
910    }
911}
912
913/// Scans all the modules in the specified directory, ensuring that
914/// any extension point implemented by a module is registered.
915///
916/// This may not actually load and initialize all the types in each
917/// module, some modules may be lazily loaded and initialized when
918/// an extension point it implements is used with e.g.
919/// g_io_extension_point_get_extensions() or
920/// g_io_extension_point_get_extension_by_name().
921///
922/// If you need to guarantee that all types are loaded in all the modules,
923/// use g_io_modules_load_all_in_directory().
924/// ## `dirname`
925/// pathname for a directory containing modules
926///     to scan.
927#[doc(alias = "g_io_modules_scan_all_in_directory")]
928pub fn io_modules_scan_all_in_directory(dirname: impl AsRef<std::path::Path>) {
929    unsafe {
930        ffi::g_io_modules_scan_all_in_directory(dirname.as_ref().to_glib_none().0);
931    }
932}
933
934/// Scans all the modules in the specified directory, ensuring that
935/// any extension point implemented by a module is registered.
936///
937/// This may not actually load and initialize all the types in each
938/// module, some modules may be lazily loaded and initialized when
939/// an extension point it implements is used with e.g.
940/// g_io_extension_point_get_extensions() or
941/// g_io_extension_point_get_extension_by_name().
942///
943/// If you need to guarantee that all types are loaded in all the modules,
944/// use g_io_modules_load_all_in_directory().
945/// ## `dirname`
946/// pathname for a directory containing modules
947///     to scan.
948/// ## `scope`
949/// a scope to use when scanning the modules
950#[doc(alias = "g_io_modules_scan_all_in_directory_with_scope")]
951pub fn io_modules_scan_all_in_directory_with_scope(
952    dirname: impl AsRef<std::path::Path>,
953    scope: &mut IOModuleScope,
954) {
955    unsafe {
956        ffi::g_io_modules_scan_all_in_directory_with_scope(
957            dirname.as_ref().to_glib_none().0,
958            scope.to_glib_none_mut().0,
959        );
960    }
961}
962
963/// Creates a keyfile-backed [`SettingsBackend`][crate::SettingsBackend].
964///
965/// The filename of the keyfile to use is given by @filename.
966///
967/// All settings read to or written from the backend must fall under the
968/// path given in @root_path (which must start and end with a slash and
969/// not contain two consecutive slashes).  @root_path may be `"/"`.
970///
971/// If @root_group is non-`NULL` then it specifies the name of the keyfile
972/// group used for keys that are written directly below @root_path.  For
973/// example, if @root_path is `"/apps/example/"` and @root_group is
974/// `"toplevel"`, then setting the key `"/apps/example/enabled"` to true will
975/// cause the following to appear in the keyfile:
976///
977/// ```text
978/// [toplevel]
979/// enabled=true
980/// ```
981///
982/// If @root_group is `NULL` then it is not permitted to store keys
983/// directly below the @root_path.
984///
985/// For keys not stored directly below @root_path (ie: in a sub-path),
986/// the name of the subpath (with the final slash stripped) is used as
987/// the name of the keyfile group.  To continue the example, if
988/// `"/apps/example/profiles/default/font-size"` were set to
989/// `12` then the following would appear in the keyfile:
990///
991/// ```text
992/// [profiles/default]
993/// font-size=12
994/// ```
995///
996/// The backend will refuse writes (and return writability as being
997/// false) for keys outside of @root_path and, in the event that
998/// @root_group is `NULL`, also for keys directly under @root_path.
999/// Writes will also be refused if the backend detects that it has the
1000/// inability to rewrite the keyfile (ie: the containing directory is not
1001/// writable).
1002///
1003/// There is no checking done for your key namespace clashing with the
1004/// syntax of the key file format.  For example, if you have `[` or `]`
1005/// characters in your path names or `=` in your key names you may be in
1006/// trouble.
1007///
1008/// The backend reads default values from a keyfile called `defaults` in
1009/// the directory specified by the `GKeyfileSettingsBackend:defaults-dir`
1010/// property, and a list of locked keys from a text file with the name `locks` in
1011/// the same location.
1012/// ## `filename`
1013/// the filename of the keyfile
1014/// ## `root_path`
1015/// the path under which all settings keys appear
1016/// ## `root_group`
1017/// the group name corresponding to @root_path, or
1018///    `NULL` to disallow storing keys directly beneath @root_path
1019///
1020/// # Returns
1021///
1022/// a keyfile-backed [`SettingsBackend`][crate::SettingsBackend]
1023#[doc(alias = "g_keyfile_settings_backend_new")]
1024pub fn keyfile_settings_backend_new(
1025    filename: &str,
1026    root_path: &str,
1027    root_group: Option<&str>,
1028) -> SettingsBackend {
1029    unsafe {
1030        from_glib_full(ffi::g_keyfile_settings_backend_new(
1031            filename.to_glib_none().0,
1032            root_path.to_glib_none().0,
1033            root_group.to_glib_none().0,
1034        ))
1035    }
1036}
1037
1038/// Creates a memory-backed #GSettingsBackend.
1039///
1040/// This backend allows changes to settings, but does not write them
1041/// to any backing storage, so the next time you run your application,
1042/// the memory backend will start out with the default values again.
1043///
1044/// # Returns
1045///
1046/// a newly created #GSettingsBackend
1047#[doc(alias = "g_memory_settings_backend_new")]
1048pub fn memory_settings_backend_new() -> SettingsBackend {
1049    unsafe { from_glib_full(ffi::g_memory_settings_backend_new()) }
1050}
1051
1052/// Creates a readonly #GSettingsBackend.
1053///
1054/// This backend does not allow changes to settings, so all settings
1055/// will always have their default values.
1056///
1057/// # Returns
1058///
1059/// a newly created #GSettingsBackend
1060#[doc(alias = "g_null_settings_backend_new")]
1061pub fn null_settings_backend_new() -> SettingsBackend {
1062    unsafe { from_glib_full(ffi::g_null_settings_backend_new()) }
1063}
1064
1065/// Returns all the names of children at the specified @path in the set of
1066/// globally registered resources.
1067///
1068/// The return result is a `NULL` terminated list of strings which should
1069/// be released with `strfreev()`.
1070///
1071/// @lookup_flags controls the behaviour of the lookup.
1072/// ## `path`
1073/// A path name inside the resource
1074/// ## `lookup_flags`
1075/// A [`ResourceLookupFlags`][crate::ResourceLookupFlags]
1076///
1077/// # Returns
1078///
1079/// an array of constant strings
1080#[doc(alias = "g_resources_enumerate_children")]
1081pub fn resources_enumerate_children(
1082    path: &str,
1083    lookup_flags: ResourceLookupFlags,
1084) -> Result<Vec<glib::GString>, glib::Error> {
1085    unsafe {
1086        let mut error = std::ptr::null_mut();
1087        let ret = ffi::g_resources_enumerate_children(
1088            path.to_glib_none().0,
1089            lookup_flags.into_glib(),
1090            &mut error,
1091        );
1092        if error.is_null() {
1093            Ok(FromGlibPtrContainer::from_glib_full(ret))
1094        } else {
1095            Err(from_glib_full(error))
1096        }
1097    }
1098}
1099
1100/// Looks for a file at the specified @path in the set of
1101/// globally registered resources and if found returns information about it.
1102///
1103/// @lookup_flags controls the behaviour of the lookup.
1104/// ## `path`
1105/// A path name inside the resource
1106/// ## `lookup_flags`
1107/// A [`ResourceLookupFlags`][crate::ResourceLookupFlags]
1108///
1109/// # Returns
1110///
1111/// `TRUE` if the file was found, `FALSE` if there were errors
1112///
1113/// ## `size`
1114/// a location to place the length of the contents of the file,
1115///    or `NULL` if the length is not needed
1116///
1117/// ## `flags`
1118/// a location to place the [`ResourceFlags`][crate::ResourceFlags] about the file,
1119///    or `NULL` if the flags are not needed
1120#[doc(alias = "g_resources_get_info")]
1121pub fn resources_get_info(
1122    path: &str,
1123    lookup_flags: ResourceLookupFlags,
1124) -> Result<(usize, u32), glib::Error> {
1125    unsafe {
1126        let mut size = std::mem::MaybeUninit::uninit();
1127        let mut flags = std::mem::MaybeUninit::uninit();
1128        let mut error = std::ptr::null_mut();
1129        let is_ok = ffi::g_resources_get_info(
1130            path.to_glib_none().0,
1131            lookup_flags.into_glib(),
1132            size.as_mut_ptr(),
1133            flags.as_mut_ptr(),
1134            &mut error,
1135        );
1136        debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
1137        if error.is_null() {
1138            Ok((size.assume_init(), flags.assume_init()))
1139        } else {
1140            Err(from_glib_full(error))
1141        }
1142    }
1143}
1144
1145/// Returns whether the specified @path in the set of
1146/// globally registered resources has children.
1147/// ## `path`
1148/// A pathname
1149///
1150/// # Returns
1151///
1152/// [`true`] if @patch has children
1153#[cfg(feature = "v2_84")]
1154#[cfg_attr(docsrs, doc(cfg(feature = "v2_84")))]
1155#[doc(alias = "g_resources_has_children")]
1156pub fn resources_has_children(path: &str) -> bool {
1157    unsafe { from_glib(ffi::g_resources_has_children(path.to_glib_none().0)) }
1158}
1159
1160/// Looks for a file at the specified @path in the set of
1161/// globally registered resources and returns a [`glib::Bytes`][crate::glib::Bytes] that
1162/// lets you directly access the data in memory.
1163///
1164/// The data is always followed by a zero byte, so you
1165/// can safely use the data as a C string. However, that byte
1166/// is not included in the size of the [`glib::Bytes`][crate::glib::Bytes].
1167///
1168/// For uncompressed resource files this is a pointer directly into
1169/// the resource bundle, which is typically in some read-only data section
1170/// in the program binary. For compressed files we allocate memory on
1171/// the heap and automatically uncompress the data.
1172///
1173/// @lookup_flags controls the behaviour of the lookup.
1174/// ## `path`
1175/// A path name inside the resource
1176/// ## `lookup_flags`
1177/// A [`ResourceLookupFlags`][crate::ResourceLookupFlags]
1178///
1179/// # Returns
1180///
1181/// [`glib::Bytes`][crate::glib::Bytes] or `NULL` on error
1182#[doc(alias = "g_resources_lookup_data")]
1183pub fn resources_lookup_data(
1184    path: &str,
1185    lookup_flags: ResourceLookupFlags,
1186) -> Result<glib::Bytes, glib::Error> {
1187    unsafe {
1188        let mut error = std::ptr::null_mut();
1189        let ret = ffi::g_resources_lookup_data(
1190            path.to_glib_none().0,
1191            lookup_flags.into_glib(),
1192            &mut error,
1193        );
1194        if error.is_null() {
1195            Ok(from_glib_full(ret))
1196        } else {
1197            Err(from_glib_full(error))
1198        }
1199    }
1200}
1201
1202/// Looks for a file at the specified @path in the set of
1203/// globally registered resources and returns a [`InputStream`][crate::InputStream]
1204/// that lets you read the data.
1205///
1206/// @lookup_flags controls the behaviour of the lookup.
1207/// ## `path`
1208/// A path name inside the resource
1209/// ## `lookup_flags`
1210/// A [`ResourceLookupFlags`][crate::ResourceLookupFlags]
1211///
1212/// # Returns
1213///
1214/// [`InputStream`][crate::InputStream] or `NULL` on error
1215#[doc(alias = "g_resources_open_stream")]
1216pub fn resources_open_stream(
1217    path: &str,
1218    lookup_flags: ResourceLookupFlags,
1219) -> Result<InputStream, glib::Error> {
1220    unsafe {
1221        let mut error = std::ptr::null_mut();
1222        let ret = ffi::g_resources_open_stream(
1223            path.to_glib_none().0,
1224            lookup_flags.into_glib(),
1225            &mut error,
1226        );
1227        if error.is_null() {
1228            Ok(from_glib_full(ret))
1229        } else {
1230            Err(from_glib_full(error))
1231        }
1232    }
1233}
1234
1235/// Registers the resource with the process-global set of resources.
1236///
1237/// Once a resource is registered the files in it can be accessed
1238/// with the global resource lookup functions like
1239/// [`resources_lookup_data()`][crate::resources_lookup_data()].
1240/// ## `resource`
1241/// A [`Resource`][crate::Resource]
1242#[doc(alias = "g_resources_register")]
1243pub fn resources_register(resource: &Resource) {
1244    unsafe {
1245        ffi::g_resources_register(resource.to_glib_none().0);
1246    }
1247}
1248
1249/// Unregisters the resource from the process-global set of resources.
1250/// ## `resource`
1251/// A [`Resource`][crate::Resource]
1252#[doc(alias = "g_resources_unregister")]
1253pub fn resources_unregister(resource: &Resource) {
1254    unsafe {
1255        ffi::g_resources_unregister(resource.to_glib_none().0);
1256    }
1257}