|
| 1 | +// Take a look at the license at the top of the repository in the LICENSE file. |
| 2 | + |
| 3 | +use crate::{subclass::prelude::*, File, FileMonitor, FileMonitorEvent}; |
| 4 | +use glib::{prelude::*, translate::*}; |
| 5 | + |
| 6 | +pub trait FileMonitorImpl: ObjectImpl { |
| 7 | + fn changed(&self, file: &File, other_file: &File, event_type: FileMonitorEvent) { |
| 8 | + self.parent_changed(file, other_file, event_type) |
| 9 | + } |
| 10 | + fn cancel(&self) -> bool { |
| 11 | + self.parent_cancel() |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +pub trait FileMonitorImplExt: ObjectSubclass { |
| 16 | + fn parent_changed(&self, file: &File, other_file: &File, event_type: FileMonitorEvent); |
| 17 | + fn parent_cancel(&self) -> bool; |
| 18 | +} |
| 19 | + |
| 20 | +impl<T: FileMonitorImpl> FileMonitorImplExt for T { |
| 21 | + fn parent_changed(&self, file: &File, other_file: &File, event_type: FileMonitorEvent) { |
| 22 | + unsafe { |
| 23 | + let data = T::type_data(); |
| 24 | + let parent_class = data.as_ref().parent_class() as *mut ffi::GFileMonitorClass; |
| 25 | + if let Some(f) = (*parent_class).changed { |
| 26 | + f( |
| 27 | + self.obj().unsafe_cast_ref::<FileMonitor>().to_glib_none().0, |
| 28 | + file.to_glib_none().0, |
| 29 | + other_file.to_glib_none().0, |
| 30 | + event_type.into_glib(), |
| 31 | + ) |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + fn parent_cancel(&self) -> bool { |
| 37 | + unsafe { |
| 38 | + let data = T::type_data(); |
| 39 | + let parent_class = data.as_ref().parent_class() as *mut ffi::GFileMonitorClass; |
| 40 | + let f = (*parent_class) |
| 41 | + .cancel |
| 42 | + .expect("No parent class implementation for \"cancel\""); |
| 43 | + from_glib(f(self |
| 44 | + .obj() |
| 45 | + .unsafe_cast_ref::<FileMonitor>() |
| 46 | + .to_glib_none() |
| 47 | + .0)) |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +unsafe impl<T: FileMonitorImpl> IsSubclassable<T> for FileMonitor { |
| 53 | + fn class_init(class: &mut ::glib::Class<Self>) { |
| 54 | + Self::parent_class_init::<T>(class); |
| 55 | + |
| 56 | + let klass = class.as_mut(); |
| 57 | + klass.changed = Some(file_monitor_changed::<T>); |
| 58 | + klass.cancel = Some(file_monitor_cancel::<T>); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +unsafe extern "C" fn file_monitor_changed<T: FileMonitorImpl>( |
| 63 | + ptr: *mut ffi::GFileMonitor, |
| 64 | + file: *mut ffi::GFile, |
| 65 | + other_file: *mut ffi::GFile, |
| 66 | + event: ffi::GFileMonitorEvent, |
| 67 | +) { |
| 68 | + let instance = &*(ptr as *mut T::Instance); |
| 69 | + let imp = instance.imp(); |
| 70 | + |
| 71 | + imp.changed( |
| 72 | + &from_glib_borrow(file), |
| 73 | + &from_glib_borrow(other_file), |
| 74 | + from_glib(event), |
| 75 | + ) |
| 76 | +} |
| 77 | + |
| 78 | +unsafe extern "C" fn file_monitor_cancel<T: FileMonitorImpl>( |
| 79 | + ptr: *mut ffi::GFileMonitor, |
| 80 | +) -> glib::ffi::gboolean { |
| 81 | + let instance = &*(ptr as *mut T::Instance); |
| 82 | + let imp = instance.imp(); |
| 83 | + |
| 84 | + imp.cancel().into_glib() |
| 85 | +} |
0 commit comments