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

use glib::{prelude::*, translate::*};
use std::{boxed::Box as Box_, fmt};

#[cfg(feature = "gio_v2_22")]
#[cfg_attr(docsrs, doc(cfg(feature = "gio_v2_22")))]
glib::wrapper! {
    #[doc(alias = "GnomeIdleMonitor")]
    pub struct IdleMonitor(Object<ffi::GnomeIdleMonitor, ffi::GnomeIdleMonitorClass>) @implements gio::Initable;

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

#[cfg(not(any(feature = "gio_v2_22")))]
glib::wrapper! {
    #[doc(alias = "GnomeIdleMonitor")]
    pub struct IdleMonitor(Object<ffi::GnomeIdleMonitor, ffi::GnomeIdleMonitorClass>);

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

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

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

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

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

pub trait IdleMonitorExt: IsA<IdleMonitor> + sealed::Sealed + 'static {
    #[doc(alias = "gnome_idle_monitor_add_idle_watch")]
    fn add_idle_watch(
        &self,
        interval_msec: u64,
        callback: Option<Box_<dyn Fn(&IdleMonitor, u32) + 'static>>,
    ) -> u32 {
        let callback_data: Box_<Option<Box_<dyn Fn(&IdleMonitor, u32) + 'static>>> =
            Box_::new(callback);
        unsafe extern "C" fn callback_func(
            monitor: *mut ffi::GnomeIdleMonitor,
            id: libc::c_uint,
            user_data: glib::ffi::gpointer,
        ) {
            let monitor = from_glib_borrow(monitor);
            let callback: &Option<Box_<dyn Fn(&IdleMonitor, u32) + 'static>> =
                &*(user_data as *mut _);
            if let Some(ref callback) = *callback {
                callback(&monitor, id)
            } else {
                panic!("cannot get closure...")
            }
        }
        let callback = if callback_data.is_some() {
            Some(callback_func as _)
        } else {
            None
        };
        unsafe extern "C" fn notify_func(data: glib::ffi::gpointer) {
            let _callback: Box_<Option<Box_<dyn Fn(&IdleMonitor, u32) + 'static>>> =
                Box_::from_raw(data as *mut _);
        }
        let destroy_call4 = Some(notify_func as _);
        let super_callback0: Box_<Option<Box_<dyn Fn(&IdleMonitor, u32) + 'static>>> =
            callback_data;
        unsafe {
            ffi::gnome_idle_monitor_add_idle_watch(
                self.as_ref().to_glib_none().0,
                interval_msec,
                callback,
                Box_::into_raw(super_callback0) as *mut _,
                destroy_call4,
            )
        }
    }

    #[doc(alias = "gnome_idle_monitor_add_user_active_watch")]
    fn add_user_active_watch(
        &self,
        callback: Option<Box_<dyn Fn(&IdleMonitor, u32) + 'static>>,
    ) -> u32 {
        let callback_data: Box_<Option<Box_<dyn Fn(&IdleMonitor, u32) + 'static>>> =
            Box_::new(callback);
        unsafe extern "C" fn callback_func(
            monitor: *mut ffi::GnomeIdleMonitor,
            id: libc::c_uint,
            user_data: glib::ffi::gpointer,
        ) {
            let monitor = from_glib_borrow(monitor);
            let callback: &Option<Box_<dyn Fn(&IdleMonitor, u32) + 'static>> =
                &*(user_data as *mut _);
            if let Some(ref callback) = *callback {
                callback(&monitor, id)
            } else {
                panic!("cannot get closure...")
            }
        }
        let callback = if callback_data.is_some() {
            Some(callback_func as _)
        } else {
            None
        };
        unsafe extern "C" fn notify_func(data: glib::ffi::gpointer) {
            let _callback: Box_<Option<Box_<dyn Fn(&IdleMonitor, u32) + 'static>>> =
                Box_::from_raw(data as *mut _);
        }
        let destroy_call3 = Some(notify_func as _);
        let super_callback0: Box_<Option<Box_<dyn Fn(&IdleMonitor, u32) + 'static>>> =
            callback_data;
        unsafe {
            ffi::gnome_idle_monitor_add_user_active_watch(
                self.as_ref().to_glib_none().0,
                callback,
                Box_::into_raw(super_callback0) as *mut _,
                destroy_call3,
            )
        }
    }

    #[doc(alias = "gnome_idle_monitor_get_idletime")]
    #[doc(alias = "get_idletime")]
    fn idletime(&self) -> u64 {
        unsafe { ffi::gnome_idle_monitor_get_idletime(self.as_ref().to_glib_none().0) }
    }

    #[doc(alias = "gnome_idle_monitor_remove_watch")]
    fn remove_watch(&self, id: u32) {
        unsafe {
            ffi::gnome_idle_monitor_remove_watch(self.as_ref().to_glib_none().0, id);
        }
    }
}

impl<O: IsA<IdleMonitor>> IdleMonitorExt for O {}

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