1use gio_sys;
2use glib::object::Cast;
3use glib::object::IsA;
4use glib::signal::{connect_raw, SignalHandlerId};
5use glib::translate::*;
6use glib::GString;
7use glib_sys;
8use libc;
9use std::boxed::Box as Box_;
10use std::mem::transmute;
11use Application;
12use File;
13
14pub trait ApplicationExtManual {
15 fn run(&self, argv: &[String]) -> i32;
16 fn connect_open<F: Fn(&Self, &[File], &str) + 'static>(&self, f: F) -> SignalHandlerId;
17}
18
19impl<O: IsA<Application>> ApplicationExtManual for O {
20 fn run(&self, argv: &[String]) -> i32 {
21 let argc = argv.len() as i32;
22 unsafe {
23 gio_sys::g_application_run(self.as_ref().to_glib_none().0, argc, argv.to_glib_none().0)
24 }
25 }
26
27 fn connect_open<F: Fn(&Self, &[File], &str) + 'static>(&self, f: F) -> SignalHandlerId {
28 unsafe extern "C" fn open_trampoline<P, F: Fn(&P, &[File], &str) + 'static>(
29 this: *mut gio_sys::GApplication,
30 files: *const *mut gio_sys::GFile,
31 n_files: libc::c_int,
32 hint: *mut libc::c_char,
33 f: glib_sys::gpointer,
34 ) where
35 P: IsA<Application>,
36 {
37 let f: &F = &*(f as *const F);
38 let files: Vec<File> = FromGlibContainer::from_glib_none_num(files, n_files as usize);
39 f(
40 &Application::from_glib_borrow(this).unsafe_cast(),
41 &files,
42 &GString::from_glib_borrow(hint),
43 )
44 }
45 unsafe {
46 let f: Box_<F> = Box_::new(f);
47 connect_raw(
48 self.as_ptr() as *mut _,
49 b"open\0".as_ptr() as *const _,
50 Some(transmute(open_trampoline::<Self, F> as usize)),
51 Box_::into_raw(f),
52 )
53 }
54 }
55}