// 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 gio_sys;
use glib;
use glib::object::IsA;
use glib::translate::*;
use glib::GString;
#[cfg(any(feature = "v2_50", feature = "dox"))]
use glib_sys;
#[cfg(any(feature = "v2_50", feature = "dox"))]
use gobject_sys;
use std;
#[cfg(any(feature = "v2_50", feature = "dox"))]
use std::boxed::Box as Box_;
use std::fmt;
#[cfg(any(feature = "v2_50", feature = "dox"))]
use std::pin::Pin;
use std::ptr;
use AppInfoCreateFlags;
use AppLaunchContext;
#[cfg(any(feature = "v2_50", feature = "dox"))]
use Cancellable;
use File;
use Icon;

glib_wrapper! {
    pub struct AppInfo(Interface<gio_sys::GAppInfo>);

    match fn {
        get_type => || gio_sys::g_app_info_get_type(),
    }
}

impl AppInfo {
    pub fn create_from_commandline<P: AsRef<std::ffi::OsStr>>(
        commandline: P,
        application_name: Option<&str>,
        flags: AppInfoCreateFlags,
    ) -> Result<AppInfo, glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = gio_sys::g_app_info_create_from_commandline(
                commandline.as_ref().to_glib_none().0,
                application_name.to_glib_none().0,
                flags.to_glib(),
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    pub fn get_all() -> Vec<AppInfo> {
        unsafe { FromGlibPtrContainer::from_glib_full(gio_sys::g_app_info_get_all()) }
    }

    pub fn get_all_for_type(content_type: &str) -> Vec<AppInfo> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(gio_sys::g_app_info_get_all_for_type(
                content_type.to_glib_none().0,
            ))
        }
    }

    pub fn get_default_for_type(content_type: &str, must_support_uris: bool) -> Option<AppInfo> {
        unsafe {
            from_glib_full(gio_sys::g_app_info_get_default_for_type(
                content_type.to_glib_none().0,
                must_support_uris.to_glib(),
            ))
        }
    }

    pub fn get_default_for_uri_scheme(uri_scheme: &str) -> Option<AppInfo> {
        unsafe {
            from_glib_full(gio_sys::g_app_info_get_default_for_uri_scheme(
                uri_scheme.to_glib_none().0,
            ))
        }
    }

    pub fn get_fallback_for_type(content_type: &str) -> Vec<AppInfo> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(gio_sys::g_app_info_get_fallback_for_type(
                content_type.to_glib_none().0,
            ))
        }
    }

    pub fn get_recommended_for_type(content_type: &str) -> Vec<AppInfo> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(gio_sys::g_app_info_get_recommended_for_type(
                content_type.to_glib_none().0,
            ))
        }
    }

    pub fn launch_default_for_uri<P: IsA<AppLaunchContext>>(
        uri: &str,
        context: Option<&P>,
    ) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = gio_sys::g_app_info_launch_default_for_uri(
                uri.to_glib_none().0,
                context.map(|p| p.as_ref()).to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[cfg(any(feature = "v2_50", feature = "dox"))]
    pub fn launch_default_for_uri_async<
        P: IsA<AppLaunchContext>,
        Q: IsA<Cancellable>,
        R: FnOnce(Result<(), glib::Error>) + Send + 'static,
    >(
        uri: &str,
        context: Option<&P>,
        cancellable: Option<&Q>,
        callback: R,
    ) {
        let user_data: Box_<R> = Box_::new(callback);
        unsafe extern "C" fn launch_default_for_uri_async_trampoline<
            R: FnOnce(Result<(), glib::Error>) + Send + 'static,
        >(
            _source_object: *mut gobject_sys::GObject,
            res: *mut gio_sys::GAsyncResult,
            user_data: glib_sys::gpointer,
        ) {
            let mut error = ptr::null_mut();
            let _ = gio_sys::g_app_info_launch_default_for_uri_finish(res, &mut error);
            let result = if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            };
            let callback: Box_<R> = Box_::from_raw(user_data as *mut _);
            callback(result);
        }
        let callback = launch_default_for_uri_async_trampoline::<R>;
        unsafe {
            gio_sys::g_app_info_launch_default_for_uri_async(
                uri.to_glib_none().0,
                context.map(|p| p.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 _,
            );
        }
    }

    #[cfg(any(feature = "v2_50", feature = "dox"))]
    pub fn launch_default_for_uri_async_future<P: IsA<AppLaunchContext> + Clone + 'static>(
        uri: &str,
        context: Option<&P>,
    ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
        let uri = String::from(uri);
        let context = context.map(ToOwned::to_owned);
        Box_::pin(crate::GioFuture::new(&(), move |_obj, send| {
            let cancellable = Cancellable::new();
            Self::launch_default_for_uri_async(
                &uri,
                context.as_ref().map(::std::borrow::Borrow::borrow),
                Some(&cancellable),
                move |res| {
                    send.resolve(res);
                },
            );

            cancellable
        }))
    }

    pub fn reset_type_associations(content_type: &str) {
        unsafe {
            gio_sys::g_app_info_reset_type_associations(content_type.to_glib_none().0);
        }
    }
}

pub const NONE_APP_INFO: Option<&AppInfo> = None;

pub trait AppInfoExt: 'static {
    fn add_supports_type(&self, content_type: &str) -> Result<(), glib::Error>;

    fn can_delete(&self) -> bool;

    fn can_remove_supports_type(&self) -> bool;

    fn delete(&self) -> bool;

    fn dup(&self) -> Option<AppInfo>;

    fn equal<P: IsA<AppInfo>>(&self, appinfo2: &P) -> bool;

    fn get_commandline(&self) -> Option<std::path::PathBuf>;

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

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

    fn get_executable(&self) -> Option<std::path::PathBuf>;

    fn get_icon(&self) -> Option<Icon>;

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

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

    fn get_supported_types(&self) -> Vec<GString>;

    fn launch<P: IsA<AppLaunchContext>>(
        &self,
        files: &[File],
        context: Option<&P>,
    ) -> Result<(), glib::Error>;

    fn launch_uris<P: IsA<AppLaunchContext>>(
        &self,
        uris: &[&str],
        context: Option<&P>,
    ) -> Result<(), glib::Error>;

    fn remove_supports_type(&self, content_type: &str) -> Result<(), glib::Error>;

    fn set_as_default_for_extension<P: AsRef<std::path::Path>>(
        &self,
        extension: P,
    ) -> Result<(), glib::Error>;

    fn set_as_default_for_type(&self, content_type: &str) -> Result<(), glib::Error>;

    fn set_as_last_used_for_type(&self, content_type: &str) -> Result<(), glib::Error>;

    fn should_show(&self) -> bool;

    fn supports_files(&self) -> bool;

    fn supports_uris(&self) -> bool;
}

impl<O: IsA<AppInfo>> AppInfoExt for O {
    fn add_supports_type(&self, content_type: &str) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = gio_sys::g_app_info_add_supports_type(
                self.as_ref().to_glib_none().0,
                content_type.to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn can_delete(&self) -> bool {
        unsafe {
            from_glib(gio_sys::g_app_info_can_delete(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn can_remove_supports_type(&self) -> bool {
        unsafe {
            from_glib(gio_sys::g_app_info_can_remove_supports_type(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn delete(&self) -> bool {
        unsafe { from_glib(gio_sys::g_app_info_delete(self.as_ref().to_glib_none().0)) }
    }

    fn dup(&self) -> Option<AppInfo> {
        unsafe { from_glib_full(gio_sys::g_app_info_dup(self.as_ref().to_glib_none().0)) }
    }

    fn equal<P: IsA<AppInfo>>(&self, appinfo2: &P) -> bool {
        unsafe {
            from_glib(gio_sys::g_app_info_equal(
                self.as_ref().to_glib_none().0,
                appinfo2.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_commandline(&self) -> Option<std::path::PathBuf> {
        unsafe {
            from_glib_none(gio_sys::g_app_info_get_commandline(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_description(&self) -> Option<GString> {
        unsafe {
            from_glib_none(gio_sys::g_app_info_get_description(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_display_name(&self) -> Option<GString> {
        unsafe {
            from_glib_none(gio_sys::g_app_info_get_display_name(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_executable(&self) -> Option<std::path::PathBuf> {
        unsafe {
            from_glib_none(gio_sys::g_app_info_get_executable(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_icon(&self) -> Option<Icon> {
        unsafe { from_glib_none(gio_sys::g_app_info_get_icon(self.as_ref().to_glib_none().0)) }
    }

    fn get_id(&self) -> Option<GString> {
        unsafe { from_glib_none(gio_sys::g_app_info_get_id(self.as_ref().to_glib_none().0)) }
    }

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

    fn get_supported_types(&self) -> Vec<GString> {
        unsafe {
            FromGlibPtrContainer::from_glib_none(gio_sys::g_app_info_get_supported_types(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn launch<P: IsA<AppLaunchContext>>(
        &self,
        files: &[File],
        context: Option<&P>,
    ) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = gio_sys::g_app_info_launch(
                self.as_ref().to_glib_none().0,
                files.to_glib_none().0,
                context.map(|p| p.as_ref()).to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn launch_uris<P: IsA<AppLaunchContext>>(
        &self,
        uris: &[&str],
        context: Option<&P>,
    ) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = gio_sys::g_app_info_launch_uris(
                self.as_ref().to_glib_none().0,
                uris.to_glib_none().0,
                context.map(|p| p.as_ref()).to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn remove_supports_type(&self, content_type: &str) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = gio_sys::g_app_info_remove_supports_type(
                self.as_ref().to_glib_none().0,
                content_type.to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn set_as_default_for_extension<P: AsRef<std::path::Path>>(
        &self,
        extension: P,
    ) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = gio_sys::g_app_info_set_as_default_for_extension(
                self.as_ref().to_glib_none().0,
                extension.as_ref().to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn set_as_default_for_type(&self, content_type: &str) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = gio_sys::g_app_info_set_as_default_for_type(
                self.as_ref().to_glib_none().0,
                content_type.to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn set_as_last_used_for_type(&self, content_type: &str) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = gio_sys::g_app_info_set_as_last_used_for_type(
                self.as_ref().to_glib_none().0,
                content_type.to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn should_show(&self) -> bool {
        unsafe {
            from_glib(gio_sys::g_app_info_should_show(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn supports_files(&self) -> bool {
        unsafe {
            from_glib(gio_sys::g_app_info_supports_files(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn supports_uris(&self) -> bool {
        unsafe {
            from_glib(gio_sys::g_app_info_supports_uris(
                self.as_ref().to_glib_none().0,
            ))
        }
    }
}

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