1 // Take a look at the license at the top of the repository in the LICENSE file. 2 3 use crate::Application; 4 use crate::File; 5 use glib::object::Cast; 6 use glib::object::IsA; 7 use glib::signal::{connect_raw, SignalHandlerId}; 8 use glib::translate::*; 9 use glib::GString; 10 use std::boxed::Box as Box_; 11 use std::mem::transmute; 12 13 pub trait ApplicationExtManual { 14 #[doc(alias = "g_application_run")] run(&self) -> i3215 fn run(&self) -> i32; 16 #[doc(alias = "g_application_run")] run_with_args<S: AsRef<str>>(&self, args: &[S]) -> i3217 fn run_with_args<S: AsRef<str>>(&self, args: &[S]) -> i32; connect_open<F: Fn(&Self, &[File], &str) + 'static>(&self, f: F) -> SignalHandlerId18 fn connect_open<F: Fn(&Self, &[File], &str) + 'static>(&self, f: F) -> SignalHandlerId; 19 } 20 21 impl<O: IsA<Application>> ApplicationExtManual for O { run(&self) -> i3222 fn run(&self) -> i32 { 23 self.run_with_args(&std::env::args().collect::<Vec<_>>()) 24 } 25 run_with_args<S: AsRef<str>>(&self, args: &[S]) -> i3226 fn run_with_args<S: AsRef<str>>(&self, args: &[S]) -> i32 { 27 let argv: Vec<&str> = args.iter().map(|a| a.as_ref()).collect(); 28 let argc = argv.len() as i32; 29 unsafe { 30 ffi::g_application_run(self.as_ref().to_glib_none().0, argc, argv.to_glib_none().0) 31 } 32 } 33 connect_open<F: Fn(&Self, &[File], &str) + 'static>(&self, f: F) -> SignalHandlerId34 fn connect_open<F: Fn(&Self, &[File], &str) + 'static>(&self, f: F) -> SignalHandlerId { 35 unsafe extern "C" fn open_trampoline<P, F: Fn(&P, &[File], &str) + 'static>( 36 this: *mut ffi::GApplication, 37 files: *const *mut ffi::GFile, 38 n_files: libc::c_int, 39 hint: *mut libc::c_char, 40 f: glib::ffi::gpointer, 41 ) where 42 P: IsA<Application>, 43 { 44 let f: &F = &*(f as *const F); 45 let files: Vec<File> = FromGlibContainer::from_glib_none_num(files, n_files as usize); 46 f( 47 Application::from_glib_borrow(this).unsafe_cast_ref(), 48 &files, 49 &GString::from_glib_borrow(hint), 50 ) 51 } 52 unsafe { 53 let f: Box_<F> = Box_::new(f); 54 connect_raw( 55 self.as_ptr() as *mut _, 56 b"open\0".as_ptr() as *const _, 57 Some(transmute::<_, unsafe extern "C" fn()>( 58 open_trampoline::<Self, F> as *const (), 59 )), 60 Box_::into_raw(f), 61 ) 62 } 63 } 64 } 65