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

use crate::AsyncResult;
use crate::Cancellable;
use glib::object::IsA;
use glib::object::ObjectType as ObjectType_;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem::transmute;

glib::wrapper! {
    #[doc(alias = "GTask")]
    pub struct Task(Object<ffi::GTask, ffi::GTaskClass>) @implements AsyncResult;

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

impl Task {
    //#[doc(alias = "g_task_attach_source")]
    //pub fn attach_source<P: Fn() -> bool + 'static>(&self, source: &glib::Source, callback: P) {
    //    unsafe { TODO: call ffi:g_task_attach_source() }
    //}

    #[doc(alias = "g_task_get_cancellable")]
    #[doc(alias = "get_cancellable")]
    pub fn cancellable(&self) -> Cancellable {
        unsafe { from_glib_none(ffi::g_task_get_cancellable(self.to_glib_none().0)) }
    }

    #[doc(alias = "g_task_get_check_cancellable")]
    #[doc(alias = "get_check_cancellable")]
    pub fn is_check_cancellable(&self) -> bool {
        unsafe { from_glib(ffi::g_task_get_check_cancellable(self.to_glib_none().0)) }
    }

    #[doc(alias = "g_task_get_completed")]
    #[doc(alias = "get_completed")]
    pub fn is_completed(&self) -> bool {
        unsafe { from_glib(ffi::g_task_get_completed(self.to_glib_none().0)) }
    }

    #[doc(alias = "g_task_get_context")]
    #[doc(alias = "get_context")]
    pub fn context(&self) -> glib::MainContext {
        unsafe { from_glib_none(ffi::g_task_get_context(self.to_glib_none().0)) }
    }

    #[cfg(any(feature = "v2_60", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_60")))]
    #[doc(alias = "g_task_get_name")]
    #[doc(alias = "get_name")]
    pub fn name(&self) -> Option<glib::GString> {
        unsafe { from_glib_none(ffi::g_task_get_name(self.to_glib_none().0)) }
    }

    #[doc(alias = "g_task_get_return_on_cancel")]
    #[doc(alias = "get_return_on_cancel")]
    pub fn is_return_on_cancel(&self) -> bool {
        unsafe { from_glib(ffi::g_task_get_return_on_cancel(self.to_glib_none().0)) }
    }

    //#[doc(alias = "g_task_get_source_tag")]
    //#[doc(alias = "get_source_tag")]
    //pub fn source_tag(&self) -> /*Unimplemented*/Option<Fundamental: Pointer> {
    //    unsafe { TODO: call ffi:g_task_get_source_tag() }
    //}

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

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

    //#[doc(alias = "g_task_return_new_error")]
    //pub fn return_new_error(&self, domain: glib::Quark, code: i32, format: &str, : /*Unknown conversion*//*Unimplemented*/Fundamental: VarArgs) {
    //    unsafe { TODO: call ffi:g_task_return_new_error() }
    //}

    //#[doc(alias = "g_task_run_in_thread_sync")]
    //pub fn run_in_thread_sync(&self, task_func: /*Unimplemented*/FnOnce(&Task, &glib::Object, /*Unimplemented*/Option<Fundamental: Pointer>, Option<&Cancellable>)) {
    //    unsafe { TODO: call ffi:g_task_run_in_thread_sync() }
    //}

    #[doc(alias = "g_task_set_check_cancellable")]
    pub fn set_check_cancellable(&self, check_cancellable: bool) {
        unsafe {
            ffi::g_task_set_check_cancellable(self.to_glib_none().0, check_cancellable.into_glib());
        }
    }

    #[cfg(any(feature = "v2_60", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_60")))]
    #[doc(alias = "g_task_set_name")]
    pub fn set_name(&self, name: Option<&str>) {
        unsafe {
            ffi::g_task_set_name(self.to_glib_none().0, name.to_glib_none().0);
        }
    }

    #[doc(alias = "g_task_set_return_on_cancel")]
    pub fn set_return_on_cancel(&self, return_on_cancel: bool) -> bool {
        unsafe {
            from_glib(ffi::g_task_set_return_on_cancel(
                self.to_glib_none().0,
                return_on_cancel.into_glib(),
            ))
        }
    }

    //#[doc(alias = "g_task_set_source_tag")]
    //pub fn set_source_tag(&self, source_tag: /*Unimplemented*/Option<Fundamental: Pointer>) {
    //    unsafe { TODO: call ffi:g_task_set_source_tag() }
    //}

    #[doc(alias = "g_task_is_valid")]
    pub fn is_valid<P: IsA<AsyncResult>, Q: IsA<glib::Object>>(
        result: &P,
        source_object: Option<&Q>,
    ) -> bool {
        unsafe {
            from_glib(ffi::g_task_is_valid(
                result.as_ref().to_glib_none().0,
                source_object.map(|p| p.as_ref()).to_glib_none().0,
            ))
        }
    }

    //#[doc(alias = "g_task_report_error")]
    //pub fn report_error<P: FnOnce(Result<(), glib::Error>) + 'static>(source_object: Option<&glib::Object>, callback: P, source_tag: /*Unimplemented*/Option<Fundamental: Pointer>, error: &mut glib::Error) {
    //    unsafe { TODO: call ffi:g_task_report_error() }
    //}

    //#[doc(alias = "g_task_report_new_error")]
    //pub fn report_new_error<P: FnOnce(Result<(), glib::Error>) + 'static>(source_object: Option<&glib::Object>, callback: P, source_tag: /*Unimplemented*/Option<Fundamental: Pointer>, domain: glib::Quark, code: i32, format: &str, : /*Unknown conversion*//*Unimplemented*/Fundamental: VarArgs) {
    //    unsafe { TODO: call ffi:g_task_report_new_error() }
    //}

    #[doc(alias = "completed")]
    pub fn connect_completed_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_completed_trampoline<F: Fn(&Task) + 'static>(
            this: *mut ffi::GTask,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::completed\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_completed_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for Task {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("Task")
    }
}
