1#[cfg(feature = "futures")]
6use futures::future;
7use gio_sys;
8use glib::object::IsA;
9use glib::translate::*;
10use glib_sys;
11use gobject_sys;
12#[cfg(feature = "futures")]
13use std::boxed::Box as Box_;
14use std::fmt;
15use std::ptr;
16use Cancellable;
17use Error;
18use IOStream;
19use ProxyAddress;
20
21glib_wrapper! {
22 pub struct Proxy(Interface<gio_sys::GProxy>);
23
24 match fn {
25 get_type => || gio_sys::g_proxy_get_type(),
26 }
27}
28
29impl Proxy {
30 pub fn get_default_for_protocol(protocol: &str) -> Option<Proxy> {
31 unsafe {
32 from_glib_full(gio_sys::g_proxy_get_default_for_protocol(
33 protocol.to_glib_none().0,
34 ))
35 }
36 }
37}
38
39pub const NONE_PROXY: Option<&Proxy> = None;
40
41pub trait ProxyExt: 'static {
42 fn connect<P: IsA<IOStream>, Q: IsA<ProxyAddress>, R: IsA<Cancellable>>(
43 &self,
44 connection: &P,
45 proxy_address: &Q,
46 cancellable: Option<&R>,
47 ) -> Result<IOStream, Error>;
48
49 fn connect_async<
50 P: IsA<IOStream>,
51 Q: IsA<ProxyAddress>,
52 R: IsA<Cancellable>,
53 S: FnOnce(Result<IOStream, Error>) + Send + 'static,
54 >(
55 &self,
56 connection: &P,
57 proxy_address: &Q,
58 cancellable: Option<&R>,
59 callback: S,
60 );
61
62 #[cfg(feature = "futures")]
63 fn connect_async_future<
64 P: IsA<IOStream> + Clone + 'static,
65 Q: IsA<ProxyAddress> + Clone + 'static,
66 >(
67 &self,
68 connection: &P,
69 proxy_address: &Q,
70 ) -> Box_<dyn future::Future<Output = Result<IOStream, Error>> + std::marker::Unpin>;
71
72 fn supports_hostname(&self) -> bool;
73}
74
75impl<O: IsA<Proxy>> ProxyExt for O {
76 fn connect<P: IsA<IOStream>, Q: IsA<ProxyAddress>, R: IsA<Cancellable>>(
77 &self,
78 connection: &P,
79 proxy_address: &Q,
80 cancellable: Option<&R>,
81 ) -> Result<IOStream, Error> {
82 unsafe {
83 let mut error = ptr::null_mut();
84 let ret = gio_sys::g_proxy_connect(
85 self.as_ref().to_glib_none().0,
86 connection.as_ref().to_glib_none().0,
87 proxy_address.as_ref().to_glib_none().0,
88 cancellable.map(|p| p.as_ref()).to_glib_none().0,
89 &mut error,
90 );
91 if error.is_null() {
92 Ok(from_glib_full(ret))
93 } else {
94 Err(from_glib_full(error))
95 }
96 }
97 }
98
99 fn connect_async<
100 P: IsA<IOStream>,
101 Q: IsA<ProxyAddress>,
102 R: IsA<Cancellable>,
103 S: FnOnce(Result<IOStream, Error>) + Send + 'static,
104 >(
105 &self,
106 connection: &P,
107 proxy_address: &Q,
108 cancellable: Option<&R>,
109 callback: S,
110 ) {
111 let user_data: Box<S> = Box::new(callback);
112 unsafe extern "C" fn connect_async_trampoline<
113 S: FnOnce(Result<IOStream, Error>) + Send + 'static,
114 >(
115 _source_object: *mut gobject_sys::GObject,
116 res: *mut gio_sys::GAsyncResult,
117 user_data: glib_sys::gpointer,
118 ) {
119 let mut error = ptr::null_mut();
120 let ret = gio_sys::g_proxy_connect_finish(_source_object as *mut _, res, &mut error);
121 let result = if error.is_null() {
122 Ok(from_glib_full(ret))
123 } else {
124 Err(from_glib_full(error))
125 };
126 let callback: Box<S> = Box::from_raw(user_data as *mut _);
127 callback(result);
128 }
129 let callback = connect_async_trampoline::<S>;
130 unsafe {
131 gio_sys::g_proxy_connect_async(
132 self.as_ref().to_glib_none().0,
133 connection.as_ref().to_glib_none().0,
134 proxy_address.as_ref().to_glib_none().0,
135 cancellable.map(|p| p.as_ref()).to_glib_none().0,
136 Some(callback),
137 Box::into_raw(user_data) as *mut _,
138 );
139 }
140 }
141
142 #[cfg(feature = "futures")]
143 fn connect_async_future<
144 P: IsA<IOStream> + Clone + 'static,
145 Q: IsA<ProxyAddress> + Clone + 'static,
146 >(
147 &self,
148 connection: &P,
149 proxy_address: &Q,
150 ) -> Box_<dyn future::Future<Output = Result<IOStream, Error>> + std::marker::Unpin> {
151 use fragile::Fragile;
152 use GioFuture;
153
154 let connection = connection.clone();
155 let proxy_address = proxy_address.clone();
156 GioFuture::new(self, move |obj, send| {
157 let cancellable = Cancellable::new();
158 let send = Fragile::new(send);
159 obj.connect_async(
160 &connection,
161 &proxy_address,
162 Some(&cancellable),
163 move |res| {
164 let _ = send.into_inner().send(res);
165 },
166 );
167
168 cancellable
169 })
170 }
171
172 fn supports_hostname(&self) -> bool {
173 unsafe {
174 from_glib(gio_sys::g_proxy_supports_hostname(
175 self.as_ref().to_glib_none().0,
176 ))
177 }
178 }
179}
180
181impl fmt::Display for Proxy {
182 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
183 write!(f, "Proxy")
184 }
185}