gio/auto/
file_monitor.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, File, FileMonitorEvent};
6use glib::{
7    object::ObjectType as _,
8    prelude::*,
9    signal::{connect_raw, SignalHandlerId},
10    translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15    /// Monitors a file or directory for changes.
16    ///
17    /// To obtain a `GFileMonitor` for a file or directory, use
18    /// [`FileExt::monitor()`][crate::prelude::FileExt::monitor()], [`FileExt::monitor_file()`][crate::prelude::FileExt::monitor_file()], or
19    /// [`FileExt::monitor_directory()`][crate::prelude::FileExt::monitor_directory()].
20    ///
21    /// To get informed about changes to the file or directory you are
22    /// monitoring, connect to the [`changed`][struct@crate::FileMonitor#changed] signal. The
23    /// signal will be emitted in the thread-default main context (see
24    /// [`glib::MainContext::push_thread_default()`][crate::glib::MainContext::push_thread_default()]) of the thread that the monitor
25    /// was created in (though if the global default main context is blocked, this
26    /// may cause notifications to be blocked even if the thread-default
27    /// context is still running).
28    ///
29    /// This is an Abstract Base Class, you cannot instantiate it.
30    ///
31    /// ## Properties
32    ///
33    ///
34    /// #### `cancelled`
35    ///  Whether the monitor has been cancelled.
36    ///
37    /// Readable
38    ///
39    ///
40    /// #### `rate-limit`
41    ///  The limit of the monitor to watch for changes, in milliseconds.
42    ///
43    /// Readable | Writeable
44    ///
45    /// ## Signals
46    ///
47    ///
48    /// #### `changed`
49    ///  Emitted when @file has been changed.
50    ///
51    /// If using [`FileMonitorFlags::WATCH_MOVES`][crate::FileMonitorFlags::WATCH_MOVES] on a directory monitor, and
52    /// the information is available (and if supported by the backend),
53    /// @event_type may be [`FileMonitorEvent::Renamed`][crate::FileMonitorEvent::Renamed],
54    /// [`FileMonitorEvent::MovedIn`][crate::FileMonitorEvent::MovedIn] or [`FileMonitorEvent::MovedOut`][crate::FileMonitorEvent::MovedOut].
55    ///
56    /// In all cases @file will be a child of the monitored directory.  For
57    /// renames, @file will be the old name and @other_file is the new
58    /// name.  For "moved in" events, @file is the name of the file that
59    /// appeared and @other_file is the old name that it was moved from (in
60    /// another directory).  For "moved out" events, @file is the name of
61    /// the file that used to be in this directory and @other_file is the
62    /// name of the file at its new location.
63    ///
64    /// It makes sense to treat [`FileMonitorEvent::MovedIn`][crate::FileMonitorEvent::MovedIn] as
65    /// equivalent to [`FileMonitorEvent::Created`][crate::FileMonitorEvent::Created] and
66    /// [`FileMonitorEvent::MovedOut`][crate::FileMonitorEvent::MovedOut] as equivalent to
67    /// [`FileMonitorEvent::Deleted`][crate::FileMonitorEvent::Deleted], with extra information.
68    /// [`FileMonitorEvent::Renamed`][crate::FileMonitorEvent::Renamed] is equivalent to a delete/create
69    /// pair.  This is exactly how the events will be reported in the case
70    /// that the [`FileMonitorFlags::WATCH_MOVES`][crate::FileMonitorFlags::WATCH_MOVES] flag is not in use.
71    ///
72    /// If using the deprecated flag [`FileMonitorFlags::SEND_MOVED`][crate::FileMonitorFlags::SEND_MOVED] flag and @event_type is
73    /// [`FileMonitorEvent::Moved`][crate::FileMonitorEvent::Moved], @file will be set to a #GFile containing the
74    /// old path, and @other_file will be set to a #GFile containing the new path.
75    ///
76    /// In all the other cases, @other_file will be set to #NULL.
77    ///
78    ///
79    ///
80    /// # Implements
81    ///
82    /// [`FileMonitorExt`][trait@crate::prelude::FileMonitorExt], [`trait@glib::ObjectExt`]
83    #[doc(alias = "GFileMonitor")]
84    pub struct FileMonitor(Object<ffi::GFileMonitor, ffi::GFileMonitorClass>);
85
86    match fn {
87        type_ => || ffi::g_file_monitor_get_type(),
88    }
89}
90
91impl FileMonitor {
92    pub const NONE: Option<&'static FileMonitor> = None;
93}
94
95mod sealed {
96    pub trait Sealed {}
97    impl<T: super::IsA<super::FileMonitor>> Sealed for T {}
98}
99
100/// Trait containing all [`struct@FileMonitor`] methods.
101///
102/// # Implementors
103///
104/// [`FileMonitor`][struct@crate::FileMonitor]
105pub trait FileMonitorExt: IsA<FileMonitor> + sealed::Sealed + 'static {
106    /// Cancels a file monitor.
107    ///
108    /// # Returns
109    ///
110    /// always [`true`]
111    #[doc(alias = "g_file_monitor_cancel")]
112    fn cancel(&self) -> bool {
113        unsafe { from_glib(ffi::g_file_monitor_cancel(self.as_ref().to_glib_none().0)) }
114    }
115
116    /// Emits the #GFileMonitor::changed signal if a change
117    /// has taken place. Should be called from file monitor
118    /// implementations only.
119    ///
120    /// Implementations are responsible to call this method from the
121    /// thread-default main context (see [`glib::MainContext::push_thread_default()`][crate::glib::MainContext::push_thread_default()])
122    /// of the thread that the monitor was created in.
123    /// ## `child`
124    /// a #GFile.
125    /// ## `other_file`
126    /// a #GFile.
127    /// ## `event_type`
128    /// a set of #GFileMonitorEvent flags.
129    #[doc(alias = "g_file_monitor_emit_event")]
130    fn emit_event(
131        &self,
132        child: &impl IsA<File>,
133        other_file: &impl IsA<File>,
134        event_type: FileMonitorEvent,
135    ) {
136        unsafe {
137            ffi::g_file_monitor_emit_event(
138                self.as_ref().to_glib_none().0,
139                child.as_ref().to_glib_none().0,
140                other_file.as_ref().to_glib_none().0,
141                event_type.into_glib(),
142            );
143        }
144    }
145
146    /// Returns whether the monitor is canceled.
147    ///
148    /// # Returns
149    ///
150    /// [`true`] if monitor is canceled. [`false`] otherwise.
151    #[doc(alias = "g_file_monitor_is_cancelled")]
152    #[doc(alias = "cancelled")]
153    fn is_cancelled(&self) -> bool {
154        unsafe {
155            from_glib(ffi::g_file_monitor_is_cancelled(
156                self.as_ref().to_glib_none().0,
157            ))
158        }
159    }
160
161    /// Sets the rate limit to which the @self will report
162    /// consecutive change events to the same file.
163    /// ## `limit_msecs`
164    /// a non-negative integer with the limit in milliseconds
165    ///     to poll for changes
166    #[doc(alias = "g_file_monitor_set_rate_limit")]
167    #[doc(alias = "rate-limit")]
168    fn set_rate_limit(&self, limit_msecs: i32) {
169        unsafe {
170            ffi::g_file_monitor_set_rate_limit(self.as_ref().to_glib_none().0, limit_msecs);
171        }
172    }
173
174    /// The limit of the monitor to watch for changes, in milliseconds.
175    #[doc(alias = "rate-limit")]
176    fn rate_limit(&self) -> i32 {
177        ObjectExt::property(self.as_ref(), "rate-limit")
178    }
179
180    /// Emitted when @file has been changed.
181    ///
182    /// If using [`FileMonitorFlags::WATCH_MOVES`][crate::FileMonitorFlags::WATCH_MOVES] on a directory monitor, and
183    /// the information is available (and if supported by the backend),
184    /// @event_type may be [`FileMonitorEvent::Renamed`][crate::FileMonitorEvent::Renamed],
185    /// [`FileMonitorEvent::MovedIn`][crate::FileMonitorEvent::MovedIn] or [`FileMonitorEvent::MovedOut`][crate::FileMonitorEvent::MovedOut].
186    ///
187    /// In all cases @file will be a child of the monitored directory.  For
188    /// renames, @file will be the old name and @other_file is the new
189    /// name.  For "moved in" events, @file is the name of the file that
190    /// appeared and @other_file is the old name that it was moved from (in
191    /// another directory).  For "moved out" events, @file is the name of
192    /// the file that used to be in this directory and @other_file is the
193    /// name of the file at its new location.
194    ///
195    /// It makes sense to treat [`FileMonitorEvent::MovedIn`][crate::FileMonitorEvent::MovedIn] as
196    /// equivalent to [`FileMonitorEvent::Created`][crate::FileMonitorEvent::Created] and
197    /// [`FileMonitorEvent::MovedOut`][crate::FileMonitorEvent::MovedOut] as equivalent to
198    /// [`FileMonitorEvent::Deleted`][crate::FileMonitorEvent::Deleted], with extra information.
199    /// [`FileMonitorEvent::Renamed`][crate::FileMonitorEvent::Renamed] is equivalent to a delete/create
200    /// pair.  This is exactly how the events will be reported in the case
201    /// that the [`FileMonitorFlags::WATCH_MOVES`][crate::FileMonitorFlags::WATCH_MOVES] flag is not in use.
202    ///
203    /// If using the deprecated flag [`FileMonitorFlags::SEND_MOVED`][crate::FileMonitorFlags::SEND_MOVED] flag and @event_type is
204    /// [`FileMonitorEvent::Moved`][crate::FileMonitorEvent::Moved], @file will be set to a #GFile containing the
205    /// old path, and @other_file will be set to a #GFile containing the new path.
206    ///
207    /// In all the other cases, @other_file will be set to #NULL.
208    /// ## `file`
209    /// a #GFile.
210    /// ## `other_file`
211    /// a #GFile or #NULL.
212    /// ## `event_type`
213    /// a #GFileMonitorEvent.
214    #[doc(alias = "changed")]
215    fn connect_changed<F: Fn(&Self, &File, Option<&File>, FileMonitorEvent) + 'static>(
216        &self,
217        f: F,
218    ) -> SignalHandlerId {
219        unsafe extern "C" fn changed_trampoline<
220            P: IsA<FileMonitor>,
221            F: Fn(&P, &File, Option<&File>, FileMonitorEvent) + 'static,
222        >(
223            this: *mut ffi::GFileMonitor,
224            file: *mut ffi::GFile,
225            other_file: *mut ffi::GFile,
226            event_type: ffi::GFileMonitorEvent,
227            f: glib::ffi::gpointer,
228        ) {
229            let f: &F = &*(f as *const F);
230            f(
231                FileMonitor::from_glib_borrow(this).unsafe_cast_ref(),
232                &from_glib_borrow(file),
233                Option::<File>::from_glib_borrow(other_file)
234                    .as_ref()
235                    .as_ref(),
236                from_glib(event_type),
237            )
238        }
239        unsafe {
240            let f: Box_<F> = Box_::new(f);
241            connect_raw(
242                self.as_ptr() as *mut _,
243                b"changed\0".as_ptr() as *const _,
244                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
245                    changed_trampoline::<Self, F> as *const (),
246                )),
247                Box_::into_raw(f),
248            )
249        }
250    }
251
252    #[doc(alias = "cancelled")]
253    fn connect_cancelled_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
254        unsafe extern "C" fn notify_cancelled_trampoline<
255            P: IsA<FileMonitor>,
256            F: Fn(&P) + 'static,
257        >(
258            this: *mut ffi::GFileMonitor,
259            _param_spec: glib::ffi::gpointer,
260            f: glib::ffi::gpointer,
261        ) {
262            let f: &F = &*(f as *const F);
263            f(FileMonitor::from_glib_borrow(this).unsafe_cast_ref())
264        }
265        unsafe {
266            let f: Box_<F> = Box_::new(f);
267            connect_raw(
268                self.as_ptr() as *mut _,
269                b"notify::cancelled\0".as_ptr() as *const _,
270                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
271                    notify_cancelled_trampoline::<Self, F> as *const (),
272                )),
273                Box_::into_raw(f),
274            )
275        }
276    }
277
278    #[doc(alias = "rate-limit")]
279    fn connect_rate_limit_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
280        unsafe extern "C" fn notify_rate_limit_trampoline<
281            P: IsA<FileMonitor>,
282            F: Fn(&P) + 'static,
283        >(
284            this: *mut ffi::GFileMonitor,
285            _param_spec: glib::ffi::gpointer,
286            f: glib::ffi::gpointer,
287        ) {
288            let f: &F = &*(f as *const F);
289            f(FileMonitor::from_glib_borrow(this).unsafe_cast_ref())
290        }
291        unsafe {
292            let f: Box_<F> = Box_::new(f);
293            connect_raw(
294                self.as_ptr() as *mut _,
295                b"notify::rate-limit\0".as_ptr() as *const _,
296                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
297                    notify_rate_limit_trampoline::<Self, F> as *const (),
298                )),
299                Box_::into_raw(f),
300            )
301        }
302    }
303}
304
305impl<O: IsA<FileMonitor>> FileMonitorExt for O {}