use AppInfoCreateFlags;
use AppLaunchContext;
#[cfg(any(feature = "v2_50", feature = "dox"))]
use Cancellable;
use Error;
use File;
use Icon;
use ffi;
#[cfg(feature = "futures")]
#[cfg(any(feature = "v2_50", feature = "dox"))]
use futures_core;
use glib::object::IsA;
use glib::translate::*;
use glib_ffi;
use gobject_ffi;
use std;
#[cfg(feature = "futures")]
#[cfg(any(feature = "v2_50", feature = "dox"))]
use std::boxed::Box as Box_;
use std::mem;
use std::ptr;
glib_wrapper! {
pub struct AppInfo(Object<ffi::GAppInfo, ffi::GAppInfoIface>);
match fn {
get_type => || ffi::g_app_info_get_type(),
}
}
impl AppInfo {
pub fn create_from_commandline<'a, P: AsRef<std::ffi::OsStr>, Q: Into<Option<&'a str>>>(commandline: P, application_name: Q, flags: AppInfoCreateFlags) -> Result<AppInfo, Error> {
let application_name = application_name.into();
let application_name = application_name.to_glib_none();
unsafe {
let mut error = ptr::null_mut();
let ret = ffi::g_app_info_create_from_commandline(commandline.as_ref().to_glib_none().0, application_name.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(ffi::g_app_info_get_all())
}
}
pub fn get_all_for_type(content_type: &str) -> Vec<AppInfo> {
unsafe {
FromGlibPtrContainer::from_glib_full(ffi::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(ffi::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(ffi::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(ffi::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(ffi::g_app_info_get_recommended_for_type(content_type.to_glib_none().0))
}
}
pub fn launch_default_for_uri<'a, P: Into<Option<&'a AppLaunchContext>>>(uri: &str, context: P) -> Result<(), Error> {
let context = context.into();
let context = context.to_glib_none();
unsafe {
let mut error = ptr::null_mut();
let _ = ffi::g_app_info_launch_default_for_uri(uri.to_glib_none().0, context.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<'a, 'b, P: Into<Option<&'a AppLaunchContext>>, Q: Into<Option<&'b Cancellable>>, R: FnOnce(Result<(), Error>) + Send + 'static>(uri: &str, context: P, cancellable: Q, callback: R) {
let context = context.into();
let context = context.to_glib_none();
let cancellable = cancellable.into();
let cancellable = cancellable.to_glib_none();
let user_data: Box<Box<R>> = Box::new(Box::new(callback));
unsafe extern "C" fn launch_default_for_uri_async_trampoline<R: FnOnce(Result<(), Error>) + Send + 'static>(_source_object: *mut gobject_ffi::GObject, res: *mut ffi::GAsyncResult, user_data: glib_ffi::gpointer)
{
let mut error = ptr::null_mut();
let _ = ffi::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<Box<R>> = Box::from_raw(user_data as *mut _);
callback(result);
}
let callback = launch_default_for_uri_async_trampoline::<R>;
unsafe {
ffi::g_app_info_launch_default_for_uri_async(uri.to_glib_none().0, context.0, cancellable.0, Some(callback), Box::into_raw(user_data) as *mut _);
}
}
#[cfg(feature = "futures")]
#[cfg(any(feature = "v2_50", feature = "dox"))]
pub fn launch_default_for_uri_async_future<'a, P: Into<Option<&'a AppLaunchContext>>>(uri: &str, context: P) -> Box_<futures_core::Future<Item = (), Error = Error>> {
use GioFuture;
use fragile::Fragile;
let uri = String::from(uri);
let context = context.into();
let context = context.map(ToOwned::to_owned);
GioFuture::new(&(), move |_obj, send| {
let cancellable = Cancellable::new();
let send = Fragile::new(send);
Self::launch_default_for_uri_async(
&uri,
context.as_ref().map(::std::borrow::Borrow::borrow),
Some(&cancellable),
move |res| {
let _ = send.into_inner().send(res);
},
);
cancellable
})
}
pub fn reset_type_associations(content_type: &str) {
unsafe {
ffi::g_app_info_reset_type_associations(content_type.to_glib_none().0);
}
}
}
pub trait AppInfoExt {
fn add_supports_type(&self, content_type: &str) -> Result<(), 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<String>;
fn get_display_name(&self) -> Option<String>;
fn get_executable(&self) -> Option<std::path::PathBuf>;
fn get_icon(&self) -> Option<Icon>;
fn get_id(&self) -> Option<String>;
fn get_name(&self) -> Option<String>;
#[cfg(any(feature = "v2_34", feature = "dox"))]
fn get_supported_types(&self) -> Vec<String>;
fn launch<'a, P: Into<Option<&'a AppLaunchContext>>>(&self, files: &[File], context: P) -> Result<(), Error>;
fn launch_uris<'a, P: Into<Option<&'a AppLaunchContext>>>(&self, uris: &[&str], context: P) -> Result<(), Error>;
fn remove_supports_type(&self, content_type: &str) -> Result<(), Error>;
fn set_as_default_for_extension<P: AsRef<std::path::Path>>(&self, extension: P) -> Result<(), Error>;
fn set_as_default_for_type(&self, content_type: &str) -> Result<(), Error>;
fn set_as_last_used_for_type(&self, content_type: &str) -> Result<(), 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<(), Error> {
unsafe {
let mut error = ptr::null_mut();
let _ = ffi::g_app_info_add_supports_type(self.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(ffi::g_app_info_can_delete(self.to_glib_none().0))
}
}
fn can_remove_supports_type(&self) -> bool {
unsafe {
from_glib(ffi::g_app_info_can_remove_supports_type(self.to_glib_none().0))
}
}
fn delete(&self) -> bool {
unsafe {
from_glib(ffi::g_app_info_delete(self.to_glib_none().0))
}
}
fn dup(&self) -> Option<AppInfo> {
unsafe {
from_glib_full(ffi::g_app_info_dup(self.to_glib_none().0))
}
}
fn equal<P: IsA<AppInfo>>(&self, appinfo2: &P) -> bool {
unsafe {
from_glib(ffi::g_app_info_equal(self.to_glib_none().0, appinfo2.to_glib_none().0))
}
}
fn get_commandline(&self) -> Option<std::path::PathBuf> {
unsafe {
from_glib_none(ffi::g_app_info_get_commandline(self.to_glib_none().0))
}
}
fn get_description(&self) -> Option<String> {
unsafe {
from_glib_none(ffi::g_app_info_get_description(self.to_glib_none().0))
}
}
fn get_display_name(&self) -> Option<String> {
unsafe {
from_glib_none(ffi::g_app_info_get_display_name(self.to_glib_none().0))
}
}
fn get_executable(&self) -> Option<std::path::PathBuf> {
unsafe {
from_glib_none(ffi::g_app_info_get_executable(self.to_glib_none().0))
}
}
fn get_icon(&self) -> Option<Icon> {
unsafe {
from_glib_none(ffi::g_app_info_get_icon(self.to_glib_none().0))
}
}
fn get_id(&self) -> Option<String> {
unsafe {
from_glib_none(ffi::g_app_info_get_id(self.to_glib_none().0))
}
}
fn get_name(&self) -> Option<String> {
unsafe {
from_glib_none(ffi::g_app_info_get_name(self.to_glib_none().0))
}
}
#[cfg(any(feature = "v2_34", feature = "dox"))]
fn get_supported_types(&self) -> Vec<String> {
unsafe {
FromGlibPtrContainer::from_glib_none(ffi::g_app_info_get_supported_types(self.to_glib_none().0))
}
}
fn launch<'a, P: Into<Option<&'a AppLaunchContext>>>(&self, files: &[File], context: P) -> Result<(), Error> {
let context = context.into();
let context = context.to_glib_none();
unsafe {
let mut error = ptr::null_mut();
let _ = ffi::g_app_info_launch(self.to_glib_none().0, files.to_glib_none().0, context.0, &mut error);
if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) }
}
}
fn launch_uris<'a, P: Into<Option<&'a AppLaunchContext>>>(&self, uris: &[&str], context: P) -> Result<(), Error> {
let context = context.into();
let context = context.to_glib_none();
unsafe {
let mut error = ptr::null_mut();
let _ = ffi::g_app_info_launch_uris(self.to_glib_none().0, uris.to_glib_none().0, context.0, &mut error);
if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) }
}
}
fn remove_supports_type(&self, content_type: &str) -> Result<(), Error> {
unsafe {
let mut error = ptr::null_mut();
let _ = ffi::g_app_info_remove_supports_type(self.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<(), Error> {
unsafe {
let mut error = ptr::null_mut();
let _ = ffi::g_app_info_set_as_default_for_extension(self.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<(), Error> {
unsafe {
let mut error = ptr::null_mut();
let _ = ffi::g_app_info_set_as_default_for_type(self.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<(), Error> {
unsafe {
let mut error = ptr::null_mut();
let _ = ffi::g_app_info_set_as_last_used_for_type(self.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(ffi::g_app_info_should_show(self.to_glib_none().0))
}
}
fn supports_files(&self) -> bool {
unsafe {
from_glib(ffi::g_app_info_supports_files(self.to_glib_none().0))
}
}
fn supports_uris(&self) -> bool {
unsafe {
from_glib(ffi::g_app_info_supports_uris(self.to_glib_none().0))
}
}
}