// This file was generated by gir (https://github.com/gtk-rs/gir)
// from ../ev-girs
// from ../gir-files
// DO NOT EDIT

use crate::{ffi, DocumentModel, JobFind, SearchResult};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::boxed::Box as Box_;

glib::wrapper! {
    #[doc(alias = "PpsSearchContext")]
    pub struct SearchContext(Object<ffi::PpsSearchContext, ffi::PpsSearchContextClass>);

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

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

    #[doc(alias = "pps_search_context_new")]
    pub fn new(model: &DocumentModel) -> SearchContext {
        skip_assert_initialized!();
        unsafe { from_glib_full(ffi::pps_search_context_new(model.to_glib_none().0)) }
    }
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::SearchContext>> Sealed for T {}
}

pub trait SearchContextExt: IsA<SearchContext> + sealed::Sealed + 'static {
    #[doc(alias = "pps_search_context_get_options")]
    #[doc(alias = "get_options")]
    fn options(&self) -> papers_document::FindOptions {
        unsafe {
            from_glib(ffi::pps_search_context_get_options(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "pps_search_context_get_result_model")]
    #[doc(alias = "get_result_model")]
    fn result_model(&self) -> Option<gio::ListModel> {
        unsafe {
            from_glib_none(ffi::pps_search_context_get_result_model(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "pps_search_context_get_search_term")]
    #[doc(alias = "get_search_term")]
    #[doc(alias = "search-term")]
    fn search_term(&self) -> Option<glib::GString> {
        unsafe {
            from_glib_none(ffi::pps_search_context_get_search_term(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

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

    #[doc(alias = "pps_search_context_select_result")]
    fn select_result(&self, result: &impl IsA<SearchResult>) {
        unsafe {
            ffi::pps_search_context_select_result(
                self.as_ref().to_glib_none().0,
                result.as_ref().to_glib_none().0,
            );
        }
    }

    #[doc(alias = "pps_search_context_set_options")]
    fn set_options(&self, options: papers_document::FindOptions) {
        unsafe {
            ffi::pps_search_context_set_options(
                self.as_ref().to_glib_none().0,
                options.into_glib(),
            );
        }
    }

    #[doc(alias = "pps_search_context_set_search_term")]
    #[doc(alias = "search-term")]
    fn set_search_term(&self, search_term: &str) {
        unsafe {
            ffi::pps_search_context_set_search_term(
                self.as_ref().to_glib_none().0,
                search_term.to_glib_none().0,
            );
        }
    }

    #[doc(alias = "cleared")]
    fn connect_cleared<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn cleared_trampoline<P: IsA<SearchContext>, F: Fn(&P) + 'static>(
            this: *mut ffi::PpsSearchContext,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(SearchContext::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"cleared\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    cleared_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "finished")]
    fn connect_finished<F: Fn(&Self, &JobFind, i32) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn finished_trampoline<
            P: IsA<SearchContext>,
            F: Fn(&P, &JobFind, i32) + 'static,
        >(
            this: *mut ffi::PpsSearchContext,
            object: *mut ffi::PpsJobFind,
            p0: libc::c_int,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                SearchContext::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(object),
                p0,
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"finished\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    finished_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "result-activated")]
    fn connect_result_activated<F: Fn(&Self, i32, i32) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn result_activated_trampoline<
            P: IsA<SearchContext>,
            F: Fn(&P, i32, i32) + 'static,
        >(
            this: *mut ffi::PpsSearchContext,
            object: libc::c_int,
            p0: libc::c_int,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                SearchContext::from_glib_borrow(this).unsafe_cast_ref(),
                object,
                p0,
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"result-activated\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    result_activated_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "started")]
    fn connect_started<F: Fn(&Self, &JobFind) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn started_trampoline<
            P: IsA<SearchContext>,
            F: Fn(&P, &JobFind) + 'static,
        >(
            this: *mut ffi::PpsSearchContext,
            object: *mut ffi::PpsJobFind,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                SearchContext::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(object),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"started\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    started_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "search-term")]
    fn connect_search_term_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_search_term_trampoline<
            P: IsA<SearchContext>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::PpsSearchContext,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(SearchContext::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::search-term\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_search_term_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl<O: IsA<SearchContext>> SearchContextExt for O {}
