libappstream 0.4.0

Rust bindings for appstream
Documentation
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use crate::{
    ffi, BundleKind, ComponentBox, ComponentKind, FormatStyle, LaunchableKind, PoolFlags,
    ProvidedKind,
};
use glib::{
    object::ObjectType as _,
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::{boxed::Box as Box_, pin::Pin};

glib::wrapper! {
    #[doc(alias = "AsPool")]
    pub struct Pool(Object<ffi::AsPool, ffi::AsPoolClass>);

    match fn {
        type_ => || ffi::as_pool_get_type(),
    }
}

impl Pool {
    pub const NONE: Option<&'static Pool> = None;

    #[doc(alias = "as_pool_new")]
    pub fn new() -> Pool {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::as_pool_new()) }
    }
}

impl Default for Pool {
    fn default() -> Self {
        Self::new()
    }
}

unsafe impl Send for Pool {}
unsafe impl Sync for Pool {}

pub trait PoolExt: IsA<Pool> + 'static {
    #[doc(alias = "as_pool_add_components")]
    fn add_components(&self, cbox: &impl IsA<ComponentBox>) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::as_pool_add_components(
                self.as_ref().to_glib_none().0,
                cbox.as_ref().to_glib_none().0,
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[doc(alias = "as_pool_add_extra_data_location")]
    fn add_extra_data_location(&self, directory: &str, format_style: FormatStyle) {
        unsafe {
            ffi::as_pool_add_extra_data_location(
                self.as_ref().to_glib_none().0,
                directory.to_glib_none().0,
                format_style.into_glib(),
            );
        }
    }

    #[doc(alias = "as_pool_add_flags")]
    fn add_flags(&self, flags: PoolFlags) {
        unsafe {
            ffi::as_pool_add_flags(self.as_ref().to_glib_none().0, flags.into_glib());
        }
    }

    #[doc(alias = "as_pool_build_search_tokens")]
    fn build_search_tokens(&self, search: &str) -> Vec<glib::GString> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(ffi::as_pool_build_search_tokens(
                self.as_ref().to_glib_none().0,
                search.to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "as_pool_clear")]
    fn clear(&self) {
        unsafe {
            ffi::as_pool_clear(self.as_ref().to_glib_none().0);
        }
    }

    #[doc(alias = "as_pool_get_components")]
    #[doc(alias = "get_components")]
    fn components(&self) -> Option<ComponentBox> {
        unsafe { from_glib_full(ffi::as_pool_get_components(self.as_ref().to_glib_none().0)) }
    }

    #[doc(alias = "as_pool_get_components_by_bundle_id")]
    #[doc(alias = "get_components_by_bundle_id")]
    fn components_by_bundle_id(
        &self,
        kind: BundleKind,
        bundle_id: &str,
        match_prefix: bool,
    ) -> Option<ComponentBox> {
        unsafe {
            from_glib_full(ffi::as_pool_get_components_by_bundle_id(
                self.as_ref().to_glib_none().0,
                kind.into_glib(),
                bundle_id.to_glib_none().0,
                match_prefix.into_glib(),
            ))
        }
    }

    #[doc(alias = "as_pool_get_components_by_categories")]
    #[doc(alias = "get_components_by_categories")]
    fn components_by_categories(&self, categories: &[&str]) -> Option<ComponentBox> {
        unsafe {
            from_glib_full(ffi::as_pool_get_components_by_categories(
                self.as_ref().to_glib_none().0,
                categories.to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "as_pool_get_components_by_extends")]
    #[doc(alias = "get_components_by_extends")]
    fn components_by_extends(&self, extended_id: &str) -> Option<ComponentBox> {
        unsafe {
            from_glib_full(ffi::as_pool_get_components_by_extends(
                self.as_ref().to_glib_none().0,
                extended_id.to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "as_pool_get_components_by_id")]
    #[doc(alias = "get_components_by_id")]
    fn components_by_id(&self, cid: &str) -> Option<ComponentBox> {
        unsafe {
            from_glib_full(ffi::as_pool_get_components_by_id(
                self.as_ref().to_glib_none().0,
                cid.to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "as_pool_get_components_by_kind")]
    #[doc(alias = "get_components_by_kind")]
    fn components_by_kind(&self, kind: ComponentKind) -> Option<ComponentBox> {
        unsafe {
            from_glib_full(ffi::as_pool_get_components_by_kind(
                self.as_ref().to_glib_none().0,
                kind.into_glib(),
            ))
        }
    }

    #[doc(alias = "as_pool_get_components_by_launchable")]
    #[doc(alias = "get_components_by_launchable")]
    fn components_by_launchable(&self, kind: LaunchableKind, id: &str) -> Option<ComponentBox> {
        unsafe {
            from_glib_full(ffi::as_pool_get_components_by_launchable(
                self.as_ref().to_glib_none().0,
                kind.into_glib(),
                id.to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "as_pool_get_components_by_provided_item")]
    #[doc(alias = "get_components_by_provided_item")]
    fn components_by_provided_item(&self, kind: ProvidedKind, item: &str) -> Option<ComponentBox> {
        unsafe {
            from_glib_full(ffi::as_pool_get_components_by_provided_item(
                self.as_ref().to_glib_none().0,
                kind.into_glib(),
                item.to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "as_pool_get_flags")]
    #[doc(alias = "get_flags")]
    fn flags(&self) -> PoolFlags {
        unsafe { from_glib(ffi::as_pool_get_flags(self.as_ref().to_glib_none().0)) }
    }

    #[doc(alias = "as_pool_get_locale")]
    #[doc(alias = "get_locale")]
    fn locale(&self) -> Option<glib::GString> {
        unsafe { from_glib_none(ffi::as_pool_get_locale(self.as_ref().to_glib_none().0)) }
    }

    #[doc(alias = "as_pool_is_empty")]
    fn is_empty(&self) -> bool {
        unsafe { from_glib(ffi::as_pool_is_empty(self.as_ref().to_glib_none().0)) }
    }

    #[doc(alias = "as_pool_load")]
    fn load(&self, cancellable: Option<&impl IsA<gio::Cancellable>>) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::as_pool_load(
                self.as_ref().to_glib_none().0,
                cancellable.map(|p| p.as_ref()).to_glib_none().0,
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[doc(alias = "as_pool_load_async")]
    fn load_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
        &self,
        cancellable: Option<&impl IsA<gio::Cancellable>>,
        callback: P,
    ) {
        let main_context = glib::MainContext::ref_thread_default();
        let is_main_context_owner = main_context.is_owner();
        let has_acquired_main_context = (!is_main_context_owner)
            .then(|| main_context.acquire().ok())
            .flatten();
        assert!(
            is_main_context_owner || has_acquired_main_context.is_some(),
            "Async operations only allowed if the thread is owning the MainContext"
        );

        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
        unsafe extern "C" fn load_async_trampoline<P: FnOnce(Result<(), glib::Error>) + 'static>(
            _source_object: *mut glib::gobject_ffi::GObject,
            res: *mut gio::ffi::GAsyncResult,
            user_data: glib::ffi::gpointer,
        ) {
            let mut error = std::ptr::null_mut();
            ffi::as_pool_load_finish(_source_object as *mut _, res, &mut error);
            let result = if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            };
            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
                Box_::from_raw(user_data as *mut _);
            let callback: P = callback.into_inner();
            callback(result);
        }
        let callback = load_async_trampoline::<P>;
        unsafe {
            ffi::as_pool_load_async(
                self.as_ref().to_glib_none().0,
                cancellable.map(|p| p.as_ref()).to_glib_none().0,
                Some(callback),
                Box_::into_raw(user_data) as *mut _,
            );
        }
    }

    fn load_future(
        &self,
    ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
        Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
            obj.load_async(Some(cancellable), move |res| {
                send.resolve(res);
            });
        }))
    }

    #[doc(alias = "as_pool_remove_flags")]
    fn remove_flags(&self, flags: PoolFlags) {
        unsafe {
            ffi::as_pool_remove_flags(self.as_ref().to_glib_none().0, flags.into_glib());
        }
    }

    #[doc(alias = "as_pool_reset_extra_data_locations")]
    fn reset_extra_data_locations(&self) {
        unsafe {
            ffi::as_pool_reset_extra_data_locations(self.as_ref().to_glib_none().0);
        }
    }

    #[doc(alias = "as_pool_search")]
    fn search(&self, search: &str) -> Option<ComponentBox> {
        unsafe {
            from_glib_full(ffi::as_pool_search(
                self.as_ref().to_glib_none().0,
                search.to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "as_pool_set_flags")]
    fn set_flags(&self, flags: PoolFlags) {
        unsafe {
            ffi::as_pool_set_flags(self.as_ref().to_glib_none().0, flags.into_glib());
        }
    }

    #[doc(alias = "as_pool_set_load_std_data_locations")]
    fn set_load_std_data_locations(&self, enabled: bool) {
        unsafe {
            ffi::as_pool_set_load_std_data_locations(
                self.as_ref().to_glib_none().0,
                enabled.into_glib(),
            );
        }
    }

    #[doc(alias = "as_pool_set_locale")]
    fn set_locale(&self, locale: &str) {
        unsafe {
            ffi::as_pool_set_locale(self.as_ref().to_glib_none().0, locale.to_glib_none().0);
        }
    }

    #[doc(alias = "changed")]
    fn connect_changed<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn changed_trampoline<P: IsA<Pool>, F: Fn(&P) + Send + Sync + 'static>(
            this: *mut ffi::AsPool,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Pool::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                c"changed".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    changed_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl<O: IsA<Pool>> PoolExt for O {}