1use gio_sys;
6use glib::object::Cast;
7use glib::object::IsA;
8use glib::signal::connect_raw;
9use glib::signal::SignalHandlerId;
10use glib::translate::*;
11use glib_sys;
12use std::boxed::Box as Box_;
13use std::fmt;
14use std::mem::transmute;
15use Drive;
16use Mount;
17use Volume;
18
19glib_wrapper! {
20 pub struct VolumeMonitor(Object<gio_sys::GVolumeMonitor, gio_sys::GVolumeMonitorClass, VolumeMonitorClass>);
21
22 match fn {
23 get_type => || gio_sys::g_volume_monitor_get_type(),
24 }
25}
26
27impl VolumeMonitor {
28 pub fn get() -> VolumeMonitor {
29 unsafe { from_glib_full(gio_sys::g_volume_monitor_get()) }
30 }
31}
32
33pub const NONE_VOLUME_MONITOR: Option<&VolumeMonitor> = None;
34
35pub trait VolumeMonitorExt: 'static {
36 fn get_connected_drives(&self) -> Vec<Drive>;
37
38 fn get_mount_for_uuid(&self, uuid: &str) -> Option<Mount>;
39
40 fn get_mounts(&self) -> Vec<Mount>;
41
42 fn get_volume_for_uuid(&self, uuid: &str) -> Option<Volume>;
43
44 fn get_volumes(&self) -> Vec<Volume>;
45
46 fn connect_drive_changed<F: Fn(&Self, &Drive) + 'static>(&self, f: F) -> SignalHandlerId;
47
48 fn connect_drive_connected<F: Fn(&Self, &Drive) + 'static>(&self, f: F) -> SignalHandlerId;
49
50 fn connect_drive_disconnected<F: Fn(&Self, &Drive) + 'static>(&self, f: F) -> SignalHandlerId;
51
52 fn connect_drive_eject_button<F: Fn(&Self, &Drive) + 'static>(&self, f: F) -> SignalHandlerId;
53
54 fn connect_drive_stop_button<F: Fn(&Self, &Drive) + 'static>(&self, f: F) -> SignalHandlerId;
55
56 fn connect_mount_added<F: Fn(&Self, &Mount) + 'static>(&self, f: F) -> SignalHandlerId;
57
58 fn connect_mount_changed<F: Fn(&Self, &Mount) + 'static>(&self, f: F) -> SignalHandlerId;
59
60 fn connect_mount_pre_unmount<F: Fn(&Self, &Mount) + 'static>(&self, f: F) -> SignalHandlerId;
61
62 fn connect_mount_removed<F: Fn(&Self, &Mount) + 'static>(&self, f: F) -> SignalHandlerId;
63
64 fn connect_volume_added<F: Fn(&Self, &Volume) + 'static>(&self, f: F) -> SignalHandlerId;
65
66 fn connect_volume_changed<F: Fn(&Self, &Volume) + 'static>(&self, f: F) -> SignalHandlerId;
67
68 fn connect_volume_removed<F: Fn(&Self, &Volume) + 'static>(&self, f: F) -> SignalHandlerId;
69}
70
71impl<O: IsA<VolumeMonitor>> VolumeMonitorExt for O {
72 fn get_connected_drives(&self) -> Vec<Drive> {
73 unsafe {
74 FromGlibPtrContainer::from_glib_full(gio_sys::g_volume_monitor_get_connected_drives(
75 self.as_ref().to_glib_none().0,
76 ))
77 }
78 }
79
80 fn get_mount_for_uuid(&self, uuid: &str) -> Option<Mount> {
81 unsafe {
82 from_glib_full(gio_sys::g_volume_monitor_get_mount_for_uuid(
83 self.as_ref().to_glib_none().0,
84 uuid.to_glib_none().0,
85 ))
86 }
87 }
88
89 fn get_mounts(&self) -> Vec<Mount> {
90 unsafe {
91 FromGlibPtrContainer::from_glib_full(gio_sys::g_volume_monitor_get_mounts(
92 self.as_ref().to_glib_none().0,
93 ))
94 }
95 }
96
97 fn get_volume_for_uuid(&self, uuid: &str) -> Option<Volume> {
98 unsafe {
99 from_glib_full(gio_sys::g_volume_monitor_get_volume_for_uuid(
100 self.as_ref().to_glib_none().0,
101 uuid.to_glib_none().0,
102 ))
103 }
104 }
105
106 fn get_volumes(&self) -> Vec<Volume> {
107 unsafe {
108 FromGlibPtrContainer::from_glib_full(gio_sys::g_volume_monitor_get_volumes(
109 self.as_ref().to_glib_none().0,
110 ))
111 }
112 }
113
114 fn connect_drive_changed<F: Fn(&Self, &Drive) + 'static>(&self, f: F) -> SignalHandlerId {
115 unsafe extern "C" fn drive_changed_trampoline<P, F: Fn(&P, &Drive) + 'static>(
116 this: *mut gio_sys::GVolumeMonitor,
117 drive: *mut gio_sys::GDrive,
118 f: glib_sys::gpointer,
119 ) where
120 P: IsA<VolumeMonitor>,
121 {
122 let f: &F = &*(f as *const F);
123 f(
124 &VolumeMonitor::from_glib_borrow(this).unsafe_cast(),
125 &from_glib_borrow(drive),
126 )
127 }
128 unsafe {
129 let f: Box_<F> = Box_::new(f);
130 connect_raw(
131 self.as_ptr() as *mut _,
132 b"drive-changed\0".as_ptr() as *const _,
133 Some(transmute(drive_changed_trampoline::<Self, F> as usize)),
134 Box_::into_raw(f),
135 )
136 }
137 }
138
139 fn connect_drive_connected<F: Fn(&Self, &Drive) + 'static>(&self, f: F) -> SignalHandlerId {
140 unsafe extern "C" fn drive_connected_trampoline<P, F: Fn(&P, &Drive) + 'static>(
141 this: *mut gio_sys::GVolumeMonitor,
142 drive: *mut gio_sys::GDrive,
143 f: glib_sys::gpointer,
144 ) where
145 P: IsA<VolumeMonitor>,
146 {
147 let f: &F = &*(f as *const F);
148 f(
149 &VolumeMonitor::from_glib_borrow(this).unsafe_cast(),
150 &from_glib_borrow(drive),
151 )
152 }
153 unsafe {
154 let f: Box_<F> = Box_::new(f);
155 connect_raw(
156 self.as_ptr() as *mut _,
157 b"drive-connected\0".as_ptr() as *const _,
158 Some(transmute(drive_connected_trampoline::<Self, F> as usize)),
159 Box_::into_raw(f),
160 )
161 }
162 }
163
164 fn connect_drive_disconnected<F: Fn(&Self, &Drive) + 'static>(&self, f: F) -> SignalHandlerId {
165 unsafe extern "C" fn drive_disconnected_trampoline<P, F: Fn(&P, &Drive) + 'static>(
166 this: *mut gio_sys::GVolumeMonitor,
167 drive: *mut gio_sys::GDrive,
168 f: glib_sys::gpointer,
169 ) where
170 P: IsA<VolumeMonitor>,
171 {
172 let f: &F = &*(f as *const F);
173 f(
174 &VolumeMonitor::from_glib_borrow(this).unsafe_cast(),
175 &from_glib_borrow(drive),
176 )
177 }
178 unsafe {
179 let f: Box_<F> = Box_::new(f);
180 connect_raw(
181 self.as_ptr() as *mut _,
182 b"drive-disconnected\0".as_ptr() as *const _,
183 Some(transmute(drive_disconnected_trampoline::<Self, F> as usize)),
184 Box_::into_raw(f),
185 )
186 }
187 }
188
189 fn connect_drive_eject_button<F: Fn(&Self, &Drive) + 'static>(&self, f: F) -> SignalHandlerId {
190 unsafe extern "C" fn drive_eject_button_trampoline<P, F: Fn(&P, &Drive) + 'static>(
191 this: *mut gio_sys::GVolumeMonitor,
192 drive: *mut gio_sys::GDrive,
193 f: glib_sys::gpointer,
194 ) where
195 P: IsA<VolumeMonitor>,
196 {
197 let f: &F = &*(f as *const F);
198 f(
199 &VolumeMonitor::from_glib_borrow(this).unsafe_cast(),
200 &from_glib_borrow(drive),
201 )
202 }
203 unsafe {
204 let f: Box_<F> = Box_::new(f);
205 connect_raw(
206 self.as_ptr() as *mut _,
207 b"drive-eject-button\0".as_ptr() as *const _,
208 Some(transmute(drive_eject_button_trampoline::<Self, F> as usize)),
209 Box_::into_raw(f),
210 )
211 }
212 }
213
214 fn connect_drive_stop_button<F: Fn(&Self, &Drive) + 'static>(&self, f: F) -> SignalHandlerId {
215 unsafe extern "C" fn drive_stop_button_trampoline<P, F: Fn(&P, &Drive) + 'static>(
216 this: *mut gio_sys::GVolumeMonitor,
217 drive: *mut gio_sys::GDrive,
218 f: glib_sys::gpointer,
219 ) where
220 P: IsA<VolumeMonitor>,
221 {
222 let f: &F = &*(f as *const F);
223 f(
224 &VolumeMonitor::from_glib_borrow(this).unsafe_cast(),
225 &from_glib_borrow(drive),
226 )
227 }
228 unsafe {
229 let f: Box_<F> = Box_::new(f);
230 connect_raw(
231 self.as_ptr() as *mut _,
232 b"drive-stop-button\0".as_ptr() as *const _,
233 Some(transmute(drive_stop_button_trampoline::<Self, F> as usize)),
234 Box_::into_raw(f),
235 )
236 }
237 }
238
239 fn connect_mount_added<F: Fn(&Self, &Mount) + 'static>(&self, f: F) -> SignalHandlerId {
240 unsafe extern "C" fn mount_added_trampoline<P, F: Fn(&P, &Mount) + 'static>(
241 this: *mut gio_sys::GVolumeMonitor,
242 mount: *mut gio_sys::GMount,
243 f: glib_sys::gpointer,
244 ) where
245 P: IsA<VolumeMonitor>,
246 {
247 let f: &F = &*(f as *const F);
248 f(
249 &VolumeMonitor::from_glib_borrow(this).unsafe_cast(),
250 &from_glib_borrow(mount),
251 )
252 }
253 unsafe {
254 let f: Box_<F> = Box_::new(f);
255 connect_raw(
256 self.as_ptr() as *mut _,
257 b"mount-added\0".as_ptr() as *const _,
258 Some(transmute(mount_added_trampoline::<Self, F> as usize)),
259 Box_::into_raw(f),
260 )
261 }
262 }
263
264 fn connect_mount_changed<F: Fn(&Self, &Mount) + 'static>(&self, f: F) -> SignalHandlerId {
265 unsafe extern "C" fn mount_changed_trampoline<P, F: Fn(&P, &Mount) + 'static>(
266 this: *mut gio_sys::GVolumeMonitor,
267 mount: *mut gio_sys::GMount,
268 f: glib_sys::gpointer,
269 ) where
270 P: IsA<VolumeMonitor>,
271 {
272 let f: &F = &*(f as *const F);
273 f(
274 &VolumeMonitor::from_glib_borrow(this).unsafe_cast(),
275 &from_glib_borrow(mount),
276 )
277 }
278 unsafe {
279 let f: Box_<F> = Box_::new(f);
280 connect_raw(
281 self.as_ptr() as *mut _,
282 b"mount-changed\0".as_ptr() as *const _,
283 Some(transmute(mount_changed_trampoline::<Self, F> as usize)),
284 Box_::into_raw(f),
285 )
286 }
287 }
288
289 fn connect_mount_pre_unmount<F: Fn(&Self, &Mount) + 'static>(&self, f: F) -> SignalHandlerId {
290 unsafe extern "C" fn mount_pre_unmount_trampoline<P, F: Fn(&P, &Mount) + 'static>(
291 this: *mut gio_sys::GVolumeMonitor,
292 mount: *mut gio_sys::GMount,
293 f: glib_sys::gpointer,
294 ) where
295 P: IsA<VolumeMonitor>,
296 {
297 let f: &F = &*(f as *const F);
298 f(
299 &VolumeMonitor::from_glib_borrow(this).unsafe_cast(),
300 &from_glib_borrow(mount),
301 )
302 }
303 unsafe {
304 let f: Box_<F> = Box_::new(f);
305 connect_raw(
306 self.as_ptr() as *mut _,
307 b"mount-pre-unmount\0".as_ptr() as *const _,
308 Some(transmute(mount_pre_unmount_trampoline::<Self, F> as usize)),
309 Box_::into_raw(f),
310 )
311 }
312 }
313
314 fn connect_mount_removed<F: Fn(&Self, &Mount) + 'static>(&self, f: F) -> SignalHandlerId {
315 unsafe extern "C" fn mount_removed_trampoline<P, F: Fn(&P, &Mount) + 'static>(
316 this: *mut gio_sys::GVolumeMonitor,
317 mount: *mut gio_sys::GMount,
318 f: glib_sys::gpointer,
319 ) where
320 P: IsA<VolumeMonitor>,
321 {
322 let f: &F = &*(f as *const F);
323 f(
324 &VolumeMonitor::from_glib_borrow(this).unsafe_cast(),
325 &from_glib_borrow(mount),
326 )
327 }
328 unsafe {
329 let f: Box_<F> = Box_::new(f);
330 connect_raw(
331 self.as_ptr() as *mut _,
332 b"mount-removed\0".as_ptr() as *const _,
333 Some(transmute(mount_removed_trampoline::<Self, F> as usize)),
334 Box_::into_raw(f),
335 )
336 }
337 }
338
339 fn connect_volume_added<F: Fn(&Self, &Volume) + 'static>(&self, f: F) -> SignalHandlerId {
340 unsafe extern "C" fn volume_added_trampoline<P, F: Fn(&P, &Volume) + 'static>(
341 this: *mut gio_sys::GVolumeMonitor,
342 volume: *mut gio_sys::GVolume,
343 f: glib_sys::gpointer,
344 ) where
345 P: IsA<VolumeMonitor>,
346 {
347 let f: &F = &*(f as *const F);
348 f(
349 &VolumeMonitor::from_glib_borrow(this).unsafe_cast(),
350 &from_glib_borrow(volume),
351 )
352 }
353 unsafe {
354 let f: Box_<F> = Box_::new(f);
355 connect_raw(
356 self.as_ptr() as *mut _,
357 b"volume-added\0".as_ptr() as *const _,
358 Some(transmute(volume_added_trampoline::<Self, F> as usize)),
359 Box_::into_raw(f),
360 )
361 }
362 }
363
364 fn connect_volume_changed<F: Fn(&Self, &Volume) + 'static>(&self, f: F) -> SignalHandlerId {
365 unsafe extern "C" fn volume_changed_trampoline<P, F: Fn(&P, &Volume) + 'static>(
366 this: *mut gio_sys::GVolumeMonitor,
367 volume: *mut gio_sys::GVolume,
368 f: glib_sys::gpointer,
369 ) where
370 P: IsA<VolumeMonitor>,
371 {
372 let f: &F = &*(f as *const F);
373 f(
374 &VolumeMonitor::from_glib_borrow(this).unsafe_cast(),
375 &from_glib_borrow(volume),
376 )
377 }
378 unsafe {
379 let f: Box_<F> = Box_::new(f);
380 connect_raw(
381 self.as_ptr() as *mut _,
382 b"volume-changed\0".as_ptr() as *const _,
383 Some(transmute(volume_changed_trampoline::<Self, F> as usize)),
384 Box_::into_raw(f),
385 )
386 }
387 }
388
389 fn connect_volume_removed<F: Fn(&Self, &Volume) + 'static>(&self, f: F) -> SignalHandlerId {
390 unsafe extern "C" fn volume_removed_trampoline<P, F: Fn(&P, &Volume) + 'static>(
391 this: *mut gio_sys::GVolumeMonitor,
392 volume: *mut gio_sys::GVolume,
393 f: glib_sys::gpointer,
394 ) where
395 P: IsA<VolumeMonitor>,
396 {
397 let f: &F = &*(f as *const F);
398 f(
399 &VolumeMonitor::from_glib_borrow(this).unsafe_cast(),
400 &from_glib_borrow(volume),
401 )
402 }
403 unsafe {
404 let f: Box_<F> = Box_::new(f);
405 connect_raw(
406 self.as_ptr() as *mut _,
407 b"volume-removed\0".as_ptr() as *const _,
408 Some(transmute(volume_removed_trampoline::<Self, F> as usize)),
409 Box_::into_raw(f),
410 )
411 }
412 }
413}
414
415impl fmt::Display for VolumeMonitor {
416 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
417 write!(f, "VolumeMonitor")
418 }
419}