// 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 glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use glib::GString;
use glib::StaticType;
use glib::ToValue;
use glib_sys;
use light_dm_sys;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem::transmute;

glib_wrapper! {
    pub struct Language(Object<light_dm_sys::LightDMLanguage, light_dm_sys::LightDMLanguageClass, LanguageClass>);

    match fn {
        get_type => || light_dm_sys::lightdm_language_get_type(),
    }
}

#[derive(Clone, Default)]
pub struct LanguageBuilder {
    code: Option<String>,
}

impl LanguageBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn build(self) -> Language {
        let mut properties: Vec<(&str, &dyn ToValue)> = vec![];
        if let Some(ref code) = self.code {
            properties.push(("code", code));
        }
        glib::Object::new(Language::static_type(), &properties)
            .expect("object new")
            .downcast()
            .expect("downcast")
    }

    pub fn code(mut self, code: &str) -> Self {
        self.code = Some(code.to_string());
        self
    }
}

pub const NONE_LANGUAGE: Option<&Language> = None;

pub trait LanguageExt: 'static {
    fn get_code(&self) -> Option<GString>;

    fn get_name(&self) -> Option<GString>;

    fn get_territory(&self) -> Option<GString>;

    fn matches(&self, code: &str) -> bool;

    fn connect_property_name_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    fn connect_property_territory_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<Language>> LanguageExt for O {
    fn get_code(&self) -> Option<GString> {
        unsafe {
            from_glib_none(light_dm_sys::lightdm_language_get_code(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_name(&self) -> Option<GString> {
        unsafe {
            from_glib_none(light_dm_sys::lightdm_language_get_name(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_territory(&self) -> Option<GString> {
        unsafe {
            from_glib_none(light_dm_sys::lightdm_language_get_territory(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn matches(&self, code: &str) -> bool {
        unsafe {
            from_glib(light_dm_sys::lightdm_language_matches(
                self.as_ref().to_glib_none().0,
                code.to_glib_none().0,
            ))
        }
    }

    fn connect_property_name_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_name_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut light_dm_sys::LightDMLanguage,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<Language>,
        {
            let f: &F = &*(f as *const F);
            f(&Language::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::name\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_name_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_property_territory_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_territory_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut light_dm_sys::LightDMLanguage,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<Language>,
        {
            let f: &F = &*(f as *const F);
            f(&Language::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::territory\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_territory_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for Language {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Language")
    }
}
