1#[cfg(feature = "futures")]
6use futures::future;
7use gio_sys;
8use glib::object::Cast;
9use glib::object::IsA;
10use glib::signal::connect_raw;
11use glib::signal::SignalHandlerId;
12use glib::translate::*;
13use glib_sys;
14use gobject_sys;
15use std::boxed::Box as Box_;
16use std::fmt;
17use std::mem::transmute;
18use std::ptr;
19use Cancellable;
20use Error;
21#[cfg(any(feature = "v2_44", feature = "dox"))]
22use NetworkConnectivity;
23use SocketConnectable;
24
25glib_wrapper! {
26 pub struct NetworkMonitor(Interface<gio_sys::GNetworkMonitor>);
27
28 match fn {
29 get_type => || gio_sys::g_network_monitor_get_type(),
30 }
31}
32
33impl NetworkMonitor {
34 pub fn get_default() -> Option<NetworkMonitor> {
35 unsafe { from_glib_none(gio_sys::g_network_monitor_get_default()) }
36 }
37}
38
39pub const NONE_NETWORK_MONITOR: Option<&NetworkMonitor> = None;
40
41pub trait NetworkMonitorExt: 'static {
42 fn can_reach<P: IsA<SocketConnectable>, Q: IsA<Cancellable>>(
43 &self,
44 connectable: &P,
45 cancellable: Option<&Q>,
46 ) -> Result<(), Error>;
47
48 fn can_reach_async<
49 P: IsA<SocketConnectable>,
50 Q: IsA<Cancellable>,
51 R: FnOnce(Result<(), Error>) + Send + 'static,
52 >(
53 &self,
54 connectable: &P,
55 cancellable: Option<&Q>,
56 callback: R,
57 );
58
59 #[cfg(feature = "futures")]
60 fn can_reach_async_future<P: IsA<SocketConnectable> + Clone + 'static>(
61 &self,
62 connectable: &P,
63 ) -> Box_<dyn future::Future<Output = Result<(), Error>> + std::marker::Unpin>;
64
65 #[cfg(any(feature = "v2_44", feature = "dox"))]
66 fn get_connectivity(&self) -> NetworkConnectivity;
67
68 fn get_network_available(&self) -> bool;
69
70 #[cfg(any(feature = "v2_46", feature = "dox"))]
71 fn get_network_metered(&self) -> bool;
72
73 fn connect_network_changed<F: Fn(&Self, bool) + 'static>(&self, f: F) -> SignalHandlerId;
74
75 #[cfg(any(feature = "v2_44", feature = "dox"))]
76 fn connect_property_connectivity_notify<F: Fn(&Self) + 'static>(&self, f: F)
77 -> SignalHandlerId;
78
79 fn connect_property_network_available_notify<F: Fn(&Self) + 'static>(
80 &self,
81 f: F,
82 ) -> SignalHandlerId;
83
84 #[cfg(any(feature = "v2_46", feature = "dox"))]
85 fn connect_property_network_metered_notify<F: Fn(&Self) + 'static>(
86 &self,
87 f: F,
88 ) -> SignalHandlerId;
89}
90
91impl<O: IsA<NetworkMonitor>> NetworkMonitorExt for O {
92 fn can_reach<P: IsA<SocketConnectable>, Q: IsA<Cancellable>>(
93 &self,
94 connectable: &P,
95 cancellable: Option<&Q>,
96 ) -> Result<(), Error> {
97 unsafe {
98 let mut error = ptr::null_mut();
99 let _ = gio_sys::g_network_monitor_can_reach(
100 self.as_ref().to_glib_none().0,
101 connectable.as_ref().to_glib_none().0,
102 cancellable.map(|p| p.as_ref()).to_glib_none().0,
103 &mut error,
104 );
105 if error.is_null() {
106 Ok(())
107 } else {
108 Err(from_glib_full(error))
109 }
110 }
111 }
112
113 fn can_reach_async<
114 P: IsA<SocketConnectable>,
115 Q: IsA<Cancellable>,
116 R: FnOnce(Result<(), Error>) + Send + 'static,
117 >(
118 &self,
119 connectable: &P,
120 cancellable: Option<&Q>,
121 callback: R,
122 ) {
123 let user_data: Box<R> = Box::new(callback);
124 unsafe extern "C" fn can_reach_async_trampoline<
125 R: FnOnce(Result<(), Error>) + Send + 'static,
126 >(
127 _source_object: *mut gobject_sys::GObject,
128 res: *mut gio_sys::GAsyncResult,
129 user_data: glib_sys::gpointer,
130 ) {
131 let mut error = ptr::null_mut();
132 let _ = gio_sys::g_network_monitor_can_reach_finish(
133 _source_object as *mut _,
134 res,
135 &mut error,
136 );
137 let result = if error.is_null() {
138 Ok(())
139 } else {
140 Err(from_glib_full(error))
141 };
142 let callback: Box<R> = Box::from_raw(user_data as *mut _);
143 callback(result);
144 }
145 let callback = can_reach_async_trampoline::<R>;
146 unsafe {
147 gio_sys::g_network_monitor_can_reach_async(
148 self.as_ref().to_glib_none().0,
149 connectable.as_ref().to_glib_none().0,
150 cancellable.map(|p| p.as_ref()).to_glib_none().0,
151 Some(callback),
152 Box::into_raw(user_data) as *mut _,
153 );
154 }
155 }
156
157 #[cfg(feature = "futures")]
158 fn can_reach_async_future<P: IsA<SocketConnectable> + Clone + 'static>(
159 &self,
160 connectable: &P,
161 ) -> Box_<dyn future::Future<Output = Result<(), Error>> + std::marker::Unpin> {
162 use fragile::Fragile;
163 use GioFuture;
164
165 let connectable = connectable.clone();
166 GioFuture::new(self, move |obj, send| {
167 let cancellable = Cancellable::new();
168 let send = Fragile::new(send);
169 obj.can_reach_async(&connectable, Some(&cancellable), move |res| {
170 let _ = send.into_inner().send(res);
171 });
172
173 cancellable
174 })
175 }
176
177 #[cfg(any(feature = "v2_44", feature = "dox"))]
178 fn get_connectivity(&self) -> NetworkConnectivity {
179 unsafe {
180 from_glib(gio_sys::g_network_monitor_get_connectivity(
181 self.as_ref().to_glib_none().0,
182 ))
183 }
184 }
185
186 fn get_network_available(&self) -> bool {
187 unsafe {
188 from_glib(gio_sys::g_network_monitor_get_network_available(
189 self.as_ref().to_glib_none().0,
190 ))
191 }
192 }
193
194 #[cfg(any(feature = "v2_46", feature = "dox"))]
195 fn get_network_metered(&self) -> bool {
196 unsafe {
197 from_glib(gio_sys::g_network_monitor_get_network_metered(
198 self.as_ref().to_glib_none().0,
199 ))
200 }
201 }
202
203 fn connect_network_changed<F: Fn(&Self, bool) + 'static>(&self, f: F) -> SignalHandlerId {
204 unsafe extern "C" fn network_changed_trampoline<P, F: Fn(&P, bool) + 'static>(
205 this: *mut gio_sys::GNetworkMonitor,
206 network_available: glib_sys::gboolean,
207 f: glib_sys::gpointer,
208 ) where
209 P: IsA<NetworkMonitor>,
210 {
211 let f: &F = &*(f as *const F);
212 f(
213 &NetworkMonitor::from_glib_borrow(this).unsafe_cast(),
214 from_glib(network_available),
215 )
216 }
217 unsafe {
218 let f: Box_<F> = Box_::new(f);
219 connect_raw(
220 self.as_ptr() as *mut _,
221 b"network-changed\0".as_ptr() as *const _,
222 Some(transmute(network_changed_trampoline::<Self, F> as usize)),
223 Box_::into_raw(f),
224 )
225 }
226 }
227
228 #[cfg(any(feature = "v2_44", feature = "dox"))]
229 fn connect_property_connectivity_notify<F: Fn(&Self) + 'static>(
230 &self,
231 f: F,
232 ) -> SignalHandlerId {
233 unsafe extern "C" fn notify_connectivity_trampoline<P, F: Fn(&P) + 'static>(
234 this: *mut gio_sys::GNetworkMonitor,
235 _param_spec: glib_sys::gpointer,
236 f: glib_sys::gpointer,
237 ) where
238 P: IsA<NetworkMonitor>,
239 {
240 let f: &F = &*(f as *const F);
241 f(&NetworkMonitor::from_glib_borrow(this).unsafe_cast())
242 }
243 unsafe {
244 let f: Box_<F> = Box_::new(f);
245 connect_raw(
246 self.as_ptr() as *mut _,
247 b"notify::connectivity\0".as_ptr() as *const _,
248 Some(transmute(
249 notify_connectivity_trampoline::<Self, F> as usize,
250 )),
251 Box_::into_raw(f),
252 )
253 }
254 }
255
256 fn connect_property_network_available_notify<F: Fn(&Self) + 'static>(
257 &self,
258 f: F,
259 ) -> SignalHandlerId {
260 unsafe extern "C" fn notify_network_available_trampoline<P, F: Fn(&P) + 'static>(
261 this: *mut gio_sys::GNetworkMonitor,
262 _param_spec: glib_sys::gpointer,
263 f: glib_sys::gpointer,
264 ) where
265 P: IsA<NetworkMonitor>,
266 {
267 let f: &F = &*(f as *const F);
268 f(&NetworkMonitor::from_glib_borrow(this).unsafe_cast())
269 }
270 unsafe {
271 let f: Box_<F> = Box_::new(f);
272 connect_raw(
273 self.as_ptr() as *mut _,
274 b"notify::network-available\0".as_ptr() as *const _,
275 Some(transmute(
276 notify_network_available_trampoline::<Self, F> as usize,
277 )),
278 Box_::into_raw(f),
279 )
280 }
281 }
282
283 #[cfg(any(feature = "v2_46", feature = "dox"))]
284 fn connect_property_network_metered_notify<F: Fn(&Self) + 'static>(
285 &self,
286 f: F,
287 ) -> SignalHandlerId {
288 unsafe extern "C" fn notify_network_metered_trampoline<P, F: Fn(&P) + 'static>(
289 this: *mut gio_sys::GNetworkMonitor,
290 _param_spec: glib_sys::gpointer,
291 f: glib_sys::gpointer,
292 ) where
293 P: IsA<NetworkMonitor>,
294 {
295 let f: &F = &*(f as *const F);
296 f(&NetworkMonitor::from_glib_borrow(this).unsafe_cast())
297 }
298 unsafe {
299 let f: Box_<F> = Box_::new(f);
300 connect_raw(
301 self.as_ptr() as *mut _,
302 b"notify::network-metered\0".as_ptr() as *const _,
303 Some(transmute(
304 notify_network_metered_trampoline::<Self, F> as usize,
305 )),
306 Box_::into_raw(f),
307 )
308 }
309 }
310}
311
312impl fmt::Display for NetworkMonitor {
313 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
314 write!(f, "NetworkMonitor")
315 }
316}