1use crate::{
6 ffi, AsyncResult, BusType, Cancellable, DBusConnection, File, IOErrorEnum, IOStream, Icon,
7 InputStream, Resource, ResourceLookupFlags, SettingsBackend,
8};
9use glib::{prelude::*, translate::*};
10use std::{boxed::Box as Box_, pin::Pin};
11
12#[doc(alias = "g_bus_get")]
13pub fn bus_get<P: FnOnce(Result<DBusConnection, glib::Error>) + 'static>(
14 bus_type: BusType,
15 cancellable: Option<&impl IsA<Cancellable>>,
16 callback: P,
17) {
18 let main_context = glib::MainContext::ref_thread_default();
19 let is_main_context_owner = main_context.is_owner();
20 let has_acquired_main_context = (!is_main_context_owner)
21 .then(|| main_context.acquire().ok())
22 .flatten();
23 assert!(
24 is_main_context_owner || has_acquired_main_context.is_some(),
25 "Async operations only allowed if the thread is owning the MainContext"
26 );
27
28 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
29 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
30 unsafe extern "C" fn bus_get_trampoline<
31 P: FnOnce(Result<DBusConnection, glib::Error>) + 'static,
32 >(
33 _source_object: *mut glib::gobject_ffi::GObject,
34 res: *mut crate::ffi::GAsyncResult,
35 user_data: glib::ffi::gpointer,
36 ) {
37 let mut error = std::ptr::null_mut();
38 let ret = ffi::g_bus_get_finish(res, &mut error);
39 let result = if error.is_null() {
40 Ok(from_glib_full(ret))
41 } else {
42 Err(from_glib_full(error))
43 };
44 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
45 Box_::from_raw(user_data as *mut _);
46 let callback: P = callback.into_inner();
47 callback(result);
48 }
49 let callback = bus_get_trampoline::<P>;
50 unsafe {
51 ffi::g_bus_get(
52 bus_type.into_glib(),
53 cancellable.map(|p| p.as_ref()).to_glib_none().0,
54 Some(callback),
55 Box_::into_raw(user_data) as *mut _,
56 );
57 }
58}
59
60pub fn bus_get_future(
61 bus_type: BusType,
62) -> Pin<Box_<dyn std::future::Future<Output = Result<DBusConnection, glib::Error>> + 'static>> {
63 Box_::pin(crate::GioFuture::new(
64 &(),
65 move |_obj, cancellable, send| {
66 bus_get(bus_type, Some(cancellable), move |res| {
67 send.resolve(res);
68 });
69 },
70 ))
71}
72
73#[doc(alias = "g_bus_get_sync")]
74pub fn bus_get_sync(
75 bus_type: BusType,
76 cancellable: Option<&impl IsA<Cancellable>>,
77) -> Result<DBusConnection, glib::Error> {
78 unsafe {
79 let mut error = std::ptr::null_mut();
80 let ret = ffi::g_bus_get_sync(
81 bus_type.into_glib(),
82 cancellable.map(|p| p.as_ref()).to_glib_none().0,
83 &mut error,
84 );
85 if error.is_null() {
86 Ok(from_glib_full(ret))
87 } else {
88 Err(from_glib_full(error))
89 }
90 }
91}
92
93#[doc(alias = "g_content_type_can_be_executable")]
114pub fn content_type_can_be_executable(type_: &str) -> bool {
115 unsafe {
116 from_glib(ffi::g_content_type_can_be_executable(
117 type_.to_glib_none().0,
118 ))
119 }
120}
121
122#[doc(alias = "g_content_type_equals")]
123pub fn content_type_equals(type1: &str, type2: &str) -> bool {
124 unsafe {
125 from_glib(ffi::g_content_type_equals(
126 type1.to_glib_none().0,
127 type2.to_glib_none().0,
128 ))
129 }
130}
131
132#[doc(alias = "g_content_type_from_mime_type")]
133pub fn content_type_from_mime_type(mime_type: &str) -> Option<glib::GString> {
134 unsafe {
135 from_glib_full(ffi::g_content_type_from_mime_type(
136 mime_type.to_glib_none().0,
137 ))
138 }
139}
140
141#[doc(alias = "g_content_type_get_description")]
142pub fn content_type_get_description(type_: &str) -> glib::GString {
143 unsafe { from_glib_full(ffi::g_content_type_get_description(type_.to_glib_none().0)) }
144}
145
146#[doc(alias = "g_content_type_get_generic_icon_name")]
147pub fn content_type_get_generic_icon_name(type_: &str) -> Option<glib::GString> {
148 unsafe {
149 from_glib_full(ffi::g_content_type_get_generic_icon_name(
150 type_.to_glib_none().0,
151 ))
152 }
153}
154
155#[doc(alias = "g_content_type_get_icon")]
156pub fn content_type_get_icon(type_: &str) -> Icon {
157 unsafe { from_glib_full(ffi::g_content_type_get_icon(type_.to_glib_none().0)) }
158}
159
160#[cfg(feature = "v2_60")]
161#[cfg_attr(docsrs, doc(cfg(feature = "v2_60")))]
162#[doc(alias = "g_content_type_get_mime_dirs")]
163pub fn content_type_get_mime_dirs() -> Vec<glib::GString> {
164 unsafe { FromGlibPtrContainer::from_glib_none(ffi::g_content_type_get_mime_dirs()) }
165}
166
167#[doc(alias = "g_content_type_get_mime_type")]
168pub fn content_type_get_mime_type(type_: &str) -> Option<glib::GString> {
169 unsafe { from_glib_full(ffi::g_content_type_get_mime_type(type_.to_glib_none().0)) }
170}
171
172#[doc(alias = "g_content_type_get_symbolic_icon")]
173pub fn content_type_get_symbolic_icon(type_: &str) -> Icon {
174 unsafe {
175 from_glib_full(ffi::g_content_type_get_symbolic_icon(
176 type_.to_glib_none().0,
177 ))
178 }
179}
180
181#[doc(alias = "g_content_type_guess_for_tree")]
182pub fn content_type_guess_for_tree(root: &impl IsA<File>) -> Vec<glib::GString> {
183 unsafe {
184 FromGlibPtrContainer::from_glib_full(ffi::g_content_type_guess_for_tree(
185 root.as_ref().to_glib_none().0,
186 ))
187 }
188}
189
190#[doc(alias = "g_content_type_is_a")]
191pub fn content_type_is_a(type_: &str, supertype: &str) -> bool {
192 unsafe {
193 from_glib(ffi::g_content_type_is_a(
194 type_.to_glib_none().0,
195 supertype.to_glib_none().0,
196 ))
197 }
198}
199
200#[doc(alias = "g_content_type_is_mime_type")]
201pub fn content_type_is_mime_type(type_: &str, mime_type: &str) -> bool {
202 unsafe {
203 from_glib(ffi::g_content_type_is_mime_type(
204 type_.to_glib_none().0,
205 mime_type.to_glib_none().0,
206 ))
207 }
208}
209
210#[doc(alias = "g_content_type_is_unknown")]
211pub fn content_type_is_unknown(type_: &str) -> bool {
212 unsafe { from_glib(ffi::g_content_type_is_unknown(type_.to_glib_none().0)) }
213}
214
215#[cfg(feature = "v2_60")]
216#[cfg_attr(docsrs, doc(cfg(feature = "v2_60")))]
217#[doc(alias = "g_content_type_set_mime_dirs")]
218pub fn content_type_set_mime_dirs(dirs: &[&str]) {
219 unsafe {
220 ffi::g_content_type_set_mime_dirs(dirs.to_glib_none().0);
221 }
222}
223
224#[doc(alias = "g_content_types_get_registered")]
225pub fn content_types_get_registered() -> Vec<glib::GString> {
226 unsafe { FromGlibPtrContainer::from_glib_full(ffi::g_content_types_get_registered()) }
227}
228
229#[doc(alias = "g_dbus_address_escape_value")]
230pub fn dbus_address_escape_value(string: &str) -> glib::GString {
231 unsafe { from_glib_full(ffi::g_dbus_address_escape_value(string.to_glib_none().0)) }
232}
233
234#[doc(alias = "g_dbus_address_get_for_bus_sync")]
235pub fn dbus_address_get_for_bus_sync(
236 bus_type: BusType,
237 cancellable: Option<&impl IsA<Cancellable>>,
238) -> Result<glib::GString, glib::Error> {
239 unsafe {
240 let mut error = std::ptr::null_mut();
241 let ret = ffi::g_dbus_address_get_for_bus_sync(
242 bus_type.into_glib(),
243 cancellable.map(|p| p.as_ref()).to_glib_none().0,
244 &mut error,
245 );
246 if error.is_null() {
247 Ok(from_glib_full(ret))
248 } else {
249 Err(from_glib_full(error))
250 }
251 }
252}
253
254#[doc(alias = "g_dbus_address_get_stream")]
255pub fn dbus_address_get_stream<
256 P: FnOnce(Result<(IOStream, Option<glib::GString>), glib::Error>) + 'static,
257>(
258 address: &str,
259 cancellable: Option<&impl IsA<Cancellable>>,
260 callback: P,
261) {
262 let main_context = glib::MainContext::ref_thread_default();
263 let is_main_context_owner = main_context.is_owner();
264 let has_acquired_main_context = (!is_main_context_owner)
265 .then(|| main_context.acquire().ok())
266 .flatten();
267 assert!(
268 is_main_context_owner || has_acquired_main_context.is_some(),
269 "Async operations only allowed if the thread is owning the MainContext"
270 );
271
272 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
273 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
274 unsafe extern "C" fn dbus_address_get_stream_trampoline<
275 P: FnOnce(Result<(IOStream, Option<glib::GString>), glib::Error>) + 'static,
276 >(
277 _source_object: *mut glib::gobject_ffi::GObject,
278 res: *mut crate::ffi::GAsyncResult,
279 user_data: glib::ffi::gpointer,
280 ) {
281 let mut error = std::ptr::null_mut();
282 let mut out_guid = std::ptr::null_mut();
283 let ret = ffi::g_dbus_address_get_stream_finish(res, &mut out_guid, &mut error);
284 let result = if error.is_null() {
285 Ok((from_glib_full(ret), from_glib_full(out_guid)))
286 } else {
287 Err(from_glib_full(error))
288 };
289 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
290 Box_::from_raw(user_data as *mut _);
291 let callback: P = callback.into_inner();
292 callback(result);
293 }
294 let callback = dbus_address_get_stream_trampoline::<P>;
295 unsafe {
296 ffi::g_dbus_address_get_stream(
297 address.to_glib_none().0,
298 cancellable.map(|p| p.as_ref()).to_glib_none().0,
299 Some(callback),
300 Box_::into_raw(user_data) as *mut _,
301 );
302 }
303}
304
305pub fn dbus_address_get_stream_future(
306 address: &str,
307) -> Pin<
308 Box_<
309 dyn std::future::Future<Output = Result<(IOStream, Option<glib::GString>), glib::Error>>
310 + 'static,
311 >,
312> {
313 let address = String::from(address);
314 Box_::pin(crate::GioFuture::new(
315 &(),
316 move |_obj, cancellable, send| {
317 dbus_address_get_stream(&address, Some(cancellable), move |res| {
318 send.resolve(res);
319 });
320 },
321 ))
322}
323
324#[doc(alias = "g_dbus_address_get_stream_sync")]
325pub fn dbus_address_get_stream_sync(
326 address: &str,
327 cancellable: Option<&impl IsA<Cancellable>>,
328) -> Result<(IOStream, Option<glib::GString>), glib::Error> {
329 unsafe {
330 let mut out_guid = std::ptr::null_mut();
331 let mut error = std::ptr::null_mut();
332 let ret = ffi::g_dbus_address_get_stream_sync(
333 address.to_glib_none().0,
334 &mut out_guid,
335 cancellable.map(|p| p.as_ref()).to_glib_none().0,
336 &mut error,
337 );
338 if error.is_null() {
339 Ok((from_glib_full(ret), from_glib_full(out_guid)))
340 } else {
341 Err(from_glib_full(error))
342 }
343 }
344}
345
346#[cfg(feature = "v2_68")]
347#[cfg_attr(docsrs, doc(cfg(feature = "v2_68")))]
348#[doc(alias = "g_dbus_escape_object_path")]
349pub fn dbus_escape_object_path(s: &str) -> glib::GString {
350 unsafe { from_glib_full(ffi::g_dbus_escape_object_path(s.to_glib_none().0)) }
351}
352
353#[doc(alias = "g_dbus_generate_guid")]
361pub fn dbus_generate_guid() -> glib::GString {
362 unsafe { from_glib_full(ffi::g_dbus_generate_guid()) }
363}
364
365#[doc(alias = "g_dbus_gvalue_to_gvariant")]
366pub fn dbus_gvalue_to_gvariant(gvalue: &glib::Value, type_: &glib::VariantTy) -> glib::Variant {
367 unsafe {
368 from_glib_full(ffi::g_dbus_gvalue_to_gvariant(
369 gvalue.to_glib_none().0,
370 type_.to_glib_none().0,
371 ))
372 }
373}
374
375#[doc(alias = "g_dbus_gvariant_to_gvalue")]
376pub fn dbus_gvariant_to_gvalue(value: &glib::Variant) -> glib::Value {
377 unsafe {
378 let mut out_gvalue = glib::Value::uninitialized();
379 ffi::g_dbus_gvariant_to_gvalue(value.to_glib_none().0, out_gvalue.to_glib_none_mut().0);
380 out_gvalue
381 }
382}
383
384#[doc(alias = "g_dbus_is_address")]
385pub fn dbus_is_address(string: &str) -> bool {
386 unsafe { from_glib(ffi::g_dbus_is_address(string.to_glib_none().0)) }
387}
388
389#[cfg(feature = "v2_70")]
390#[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
391#[doc(alias = "g_dbus_is_error_name")]
392pub fn dbus_is_error_name(string: &str) -> bool {
393 unsafe { from_glib(ffi::g_dbus_is_error_name(string.to_glib_none().0)) }
394}
395
396#[doc(alias = "g_dbus_is_guid")]
397pub fn dbus_is_guid(string: &str) -> bool {
398 unsafe { from_glib(ffi::g_dbus_is_guid(string.to_glib_none().0)) }
399}
400
401#[doc(alias = "g_dbus_is_interface_name")]
402pub fn dbus_is_interface_name(string: &str) -> bool {
403 unsafe { from_glib(ffi::g_dbus_is_interface_name(string.to_glib_none().0)) }
404}
405
406#[doc(alias = "g_dbus_is_member_name")]
407pub fn dbus_is_member_name(string: &str) -> bool {
408 unsafe { from_glib(ffi::g_dbus_is_member_name(string.to_glib_none().0)) }
409}
410
411#[doc(alias = "g_dbus_is_name")]
412pub fn dbus_is_name(string: &str) -> bool {
413 unsafe { from_glib(ffi::g_dbus_is_name(string.to_glib_none().0)) }
414}
415
416#[doc(alias = "g_dbus_is_supported_address")]
417pub fn dbus_is_supported_address(string: &str) -> Result<(), glib::Error> {
418 unsafe {
419 let mut error = std::ptr::null_mut();
420 let is_ok = ffi::g_dbus_is_supported_address(string.to_glib_none().0, &mut error);
421 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
422 if error.is_null() {
423 Ok(())
424 } else {
425 Err(from_glib_full(error))
426 }
427 }
428}
429
430#[doc(alias = "g_dbus_is_unique_name")]
431pub fn dbus_is_unique_name(string: &str) -> bool {
432 unsafe { from_glib(ffi::g_dbus_is_unique_name(string.to_glib_none().0)) }
433}
434
435#[doc(alias = "g_io_error_from_errno")]
436pub fn io_error_from_errno(err_no: i32) -> IOErrorEnum {
437 unsafe { from_glib(ffi::g_io_error_from_errno(err_no)) }
438}
439
440#[doc(alias = "g_io_modules_scan_all_in_directory")]
451pub fn io_modules_scan_all_in_directory(dirname: impl AsRef<std::path::Path>) {
452 unsafe {
453 ffi::g_io_modules_scan_all_in_directory(dirname.as_ref().to_glib_none().0);
454 }
455}
456
457#[doc(alias = "g_keyfile_settings_backend_new")]
463pub fn keyfile_settings_backend_new(
464 filename: &str,
465 root_path: &str,
466 root_group: Option<&str>,
467) -> SettingsBackend {
468 unsafe {
469 from_glib_full(ffi::g_keyfile_settings_backend_new(
470 filename.to_glib_none().0,
471 root_path.to_glib_none().0,
472 root_group.to_glib_none().0,
473 ))
474 }
475}
476
477#[doc(alias = "g_memory_settings_backend_new")]
478pub fn memory_settings_backend_new() -> SettingsBackend {
479 unsafe { from_glib_full(ffi::g_memory_settings_backend_new()) }
480}
481
482#[doc(alias = "g_null_settings_backend_new")]
483pub fn null_settings_backend_new() -> SettingsBackend {
484 unsafe { from_glib_full(ffi::g_null_settings_backend_new()) }
485}
486
487#[doc(alias = "g_resources_enumerate_children")]
488pub fn resources_enumerate_children(
489 path: &str,
490 lookup_flags: ResourceLookupFlags,
491) -> Result<Vec<glib::GString>, glib::Error> {
492 unsafe {
493 let mut error = std::ptr::null_mut();
494 let ret = ffi::g_resources_enumerate_children(
495 path.to_glib_none().0,
496 lookup_flags.into_glib(),
497 &mut error,
498 );
499 if error.is_null() {
500 Ok(FromGlibPtrContainer::from_glib_full(ret))
501 } else {
502 Err(from_glib_full(error))
503 }
504 }
505}
506
507#[doc(alias = "g_resources_get_info")]
508pub fn resources_get_info(
509 path: &str,
510 lookup_flags: ResourceLookupFlags,
511) -> Result<(usize, u32), glib::Error> {
512 unsafe {
513 let mut size = std::mem::MaybeUninit::uninit();
514 let mut flags = std::mem::MaybeUninit::uninit();
515 let mut error = std::ptr::null_mut();
516 let is_ok = ffi::g_resources_get_info(
517 path.to_glib_none().0,
518 lookup_flags.into_glib(),
519 size.as_mut_ptr(),
520 flags.as_mut_ptr(),
521 &mut error,
522 );
523 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
524 if error.is_null() {
525 Ok((size.assume_init(), flags.assume_init()))
526 } else {
527 Err(from_glib_full(error))
528 }
529 }
530}
531
532#[cfg(feature = "v2_84")]
533#[cfg_attr(docsrs, doc(cfg(feature = "v2_84")))]
534#[doc(alias = "g_resources_has_children")]
535pub fn resources_has_children(path: &str) -> bool {
536 unsafe { from_glib(ffi::g_resources_has_children(path.to_glib_none().0)) }
537}
538
539#[doc(alias = "g_resources_lookup_data")]
540pub fn resources_lookup_data(
541 path: &str,
542 lookup_flags: ResourceLookupFlags,
543) -> Result<glib::Bytes, glib::Error> {
544 unsafe {
545 let mut error = std::ptr::null_mut();
546 let ret = ffi::g_resources_lookup_data(
547 path.to_glib_none().0,
548 lookup_flags.into_glib(),
549 &mut error,
550 );
551 if error.is_null() {
552 Ok(from_glib_full(ret))
553 } else {
554 Err(from_glib_full(error))
555 }
556 }
557}
558
559#[doc(alias = "g_resources_open_stream")]
560pub fn resources_open_stream(
561 path: &str,
562 lookup_flags: ResourceLookupFlags,
563) -> Result<InputStream, glib::Error> {
564 unsafe {
565 let mut error = std::ptr::null_mut();
566 let ret = ffi::g_resources_open_stream(
567 path.to_glib_none().0,
568 lookup_flags.into_glib(),
569 &mut error,
570 );
571 if error.is_null() {
572 Ok(from_glib_full(ret))
573 } else {
574 Err(from_glib_full(error))
575 }
576 }
577}
578
579#[doc(alias = "g_resources_register")]
580pub fn resources_register(resource: &Resource) {
581 unsafe {
582 ffi::g_resources_register(resource.to_glib_none().0);
583 }
584}
585
586#[doc(alias = "g_resources_unregister")]
587pub fn resources_unregister(resource: &Resource) {
588 unsafe {
589 ffi::g_resources_unregister(resource.to_glib_none().0);
590 }
591}
592
593#[cfg(unix)]
594#[cfg_attr(docsrs, doc(cfg(unix)))]
595#[doc(alias = "g_unix_is_mount_path_system_internal")]
596pub fn unix_is_mount_path_system_internal(mount_path: impl AsRef<std::path::Path>) -> bool {
597 unsafe {
598 from_glib(ffi::g_unix_is_mount_path_system_internal(
599 mount_path.as_ref().to_glib_none().0,
600 ))
601 }
602}
603
604#[cfg(unix)]
605#[cfg_attr(docsrs, doc(cfg(unix)))]
606#[doc(alias = "g_unix_is_system_device_path")]
607pub fn unix_is_system_device_path(device_path: impl AsRef<std::path::Path>) -> bool {
608 unsafe {
609 from_glib(ffi::g_unix_is_system_device_path(
610 device_path.as_ref().to_glib_none().0,
611 ))
612 }
613}
614
615#[cfg(unix)]
616#[cfg_attr(docsrs, doc(cfg(unix)))]
617#[doc(alias = "g_unix_is_system_fs_type")]
618pub fn unix_is_system_fs_type(fs_type: &str) -> bool {
619 unsafe { from_glib(ffi::g_unix_is_system_fs_type(fs_type.to_glib_none().0)) }
620}