1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::{File, FileMonitorEvent};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::{boxed::Box as Box_, fmt, mem::transmute};

glib::wrapper! {
    /// Monitors a file or directory for changes.
    ///
    /// To obtain a [`FileMonitor`][crate::FileMonitor] for a file or directory, use
    /// [`FileExt::monitor()`][crate::prelude::FileExt::monitor()], [`FileExt::monitor_file()`][crate::prelude::FileExt::monitor_file()], or
    /// [`FileExt::monitor_directory()`][crate::prelude::FileExt::monitor_directory()].
    ///
    /// To get informed about changes to the file or directory you are
    /// monitoring, connect to the [`changed`][struct@crate::FileMonitor#changed] signal. The
    /// signal will be emitted in the
    /// [thread-default main context][g-main-context-push-thread-default]
    /// of the thread that the monitor was created in
    /// (though if the global default main context is blocked, this may
    /// cause notifications to be blocked even if the thread-default
    /// context is still running).
    ///
    /// This is an Abstract Base Class, you cannot instantiate it.
    ///
    /// ## Properties
    ///
    ///
    /// #### `cancelled`
    ///  Readable
    ///
    ///
    /// #### `rate-limit`
    ///  Readable | Writeable
    ///
    /// ## Signals
    ///
    ///
    /// #### `changed`
    ///  Emitted when `file` has been changed.
    ///
    /// If using [`FileMonitorFlags::WATCH_MOVES`][crate::FileMonitorFlags::WATCH_MOVES] on a directory monitor, and
    /// the information is available (and if supported by the backend),
    /// `event_type` may be [`FileMonitorEvent::Renamed`][crate::FileMonitorEvent::Renamed],
    /// [`FileMonitorEvent::MovedIn`][crate::FileMonitorEvent::MovedIn] or [`FileMonitorEvent::MovedOut`][crate::FileMonitorEvent::MovedOut].
    ///
    /// In all cases `file` will be a child of the monitored directory. For
    /// renames, `file` will be the old name and `other_file` is the new
    /// name. For "moved in" events, `file` is the name of the file that
    /// appeared and `other_file` is the old name that it was moved from (in
    /// another directory). For "moved out" events, `file` is the name of
    /// the file that used to be in this directory and `other_file` is the
    /// name of the file at its new location.
    ///
    /// It makes sense to treat [`FileMonitorEvent::MovedIn`][crate::FileMonitorEvent::MovedIn] as
    /// equivalent to [`FileMonitorEvent::Created`][crate::FileMonitorEvent::Created] and
    /// [`FileMonitorEvent::MovedOut`][crate::FileMonitorEvent::MovedOut] as equivalent to
    /// [`FileMonitorEvent::Deleted`][crate::FileMonitorEvent::Deleted], with extra information.
    /// [`FileMonitorEvent::Renamed`][crate::FileMonitorEvent::Renamed] is equivalent to a delete/create
    /// pair. This is exactly how the events will be reported in the case
    /// that the [`FileMonitorFlags::WATCH_MOVES`][crate::FileMonitorFlags::WATCH_MOVES] flag is not in use.
    ///
    /// If using the deprecated flag [`FileMonitorFlags::SEND_MOVED`][crate::FileMonitorFlags::SEND_MOVED] flag and `event_type` is
    /// [`FileMonitorEvent::Moved`][crate::FileMonitorEvent::Moved], `file` will be set to a [`File`][crate::File] containing the
    /// old path, and `other_file` will be set to a [`File`][crate::File] containing the new path.
    ///
    /// In all the other cases, `other_file` will be set to [`None`].
    ///
    ///
    ///
    /// # Implements
    ///
    /// [`FileMonitorExt`][trait@crate::prelude::FileMonitorExt], [`trait@glib::ObjectExt`]
    #[doc(alias = "GFileMonitor")]
    pub struct FileMonitor(Object<ffi::GFileMonitor, ffi::GFileMonitorClass>);

    match fn {
        type_ => || ffi::g_file_monitor_get_type(),
    }
}

impl FileMonitor {
    pub const NONE: Option<&'static FileMonitor> = None;
}

/// Trait containing all [`struct@FileMonitor`] methods.
///
/// # Implementors
///
/// [`FileMonitor`][struct@crate::FileMonitor]
pub trait FileMonitorExt: 'static {
    /// Cancels a file monitor.
    ///
    /// # Returns
    ///
    /// always [`true`]
    #[doc(alias = "g_file_monitor_cancel")]
    fn cancel(&self) -> bool;

    /// Emits the [`changed`][struct@crate::FileMonitor#changed] signal if a change
    /// has taken place. Should be called from file monitor
    /// implementations only.
    ///
    /// Implementations are responsible to call this method from the
    /// [thread-default main context][g-main-context-push-thread-default] of the
    /// thread that the monitor was created in.
    /// ## `child`
    /// a [`File`][crate::File].
    /// ## `other_file`
    /// a [`File`][crate::File].
    /// ## `event_type`
    /// a set of [`FileMonitorEvent`][crate::FileMonitorEvent] flags.
    #[doc(alias = "g_file_monitor_emit_event")]
    fn emit_event(
        &self,
        child: &impl IsA<File>,
        other_file: &impl IsA<File>,
        event_type: FileMonitorEvent,
    );

    /// Returns whether the monitor is canceled.
    ///
    /// # Returns
    ///
    /// [`true`] if monitor is canceled. [`false`] otherwise.
    #[doc(alias = "g_file_monitor_is_cancelled")]
    fn is_cancelled(&self) -> bool;

    /// Sets the rate limit to which the `self` will report
    /// consecutive change events to the same file.
    /// ## `limit_msecs`
    /// a non-negative integer with the limit in milliseconds
    ///  to poll for changes
    #[doc(alias = "g_file_monitor_set_rate_limit")]
    fn set_rate_limit(&self, limit_msecs: i32);

    #[doc(alias = "rate-limit")]
    fn rate_limit(&self) -> i32;

    /// Emitted when `file` has been changed.
    ///
    /// If using [`FileMonitorFlags::WATCH_MOVES`][crate::FileMonitorFlags::WATCH_MOVES] on a directory monitor, and
    /// the information is available (and if supported by the backend),
    /// `event_type` may be [`FileMonitorEvent::Renamed`][crate::FileMonitorEvent::Renamed],
    /// [`FileMonitorEvent::MovedIn`][crate::FileMonitorEvent::MovedIn] or [`FileMonitorEvent::MovedOut`][crate::FileMonitorEvent::MovedOut].
    ///
    /// In all cases `file` will be a child of the monitored directory. For
    /// renames, `file` will be the old name and `other_file` is the new
    /// name. For "moved in" events, `file` is the name of the file that
    /// appeared and `other_file` is the old name that it was moved from (in
    /// another directory). For "moved out" events, `file` is the name of
    /// the file that used to be in this directory and `other_file` is the
    /// name of the file at its new location.
    ///
    /// It makes sense to treat [`FileMonitorEvent::MovedIn`][crate::FileMonitorEvent::MovedIn] as
    /// equivalent to [`FileMonitorEvent::Created`][crate::FileMonitorEvent::Created] and
    /// [`FileMonitorEvent::MovedOut`][crate::FileMonitorEvent::MovedOut] as equivalent to
    /// [`FileMonitorEvent::Deleted`][crate::FileMonitorEvent::Deleted], with extra information.
    /// [`FileMonitorEvent::Renamed`][crate::FileMonitorEvent::Renamed] is equivalent to a delete/create
    /// pair. This is exactly how the events will be reported in the case
    /// that the [`FileMonitorFlags::WATCH_MOVES`][crate::FileMonitorFlags::WATCH_MOVES] flag is not in use.
    ///
    /// If using the deprecated flag [`FileMonitorFlags::SEND_MOVED`][crate::FileMonitorFlags::SEND_MOVED] flag and `event_type` is
    /// [`FileMonitorEvent::Moved`][crate::FileMonitorEvent::Moved], `file` will be set to a [`File`][crate::File] containing the
    /// old path, and `other_file` will be set to a [`File`][crate::File] containing the new path.
    ///
    /// In all the other cases, `other_file` will be set to [`None`].
    /// ## `file`
    /// a [`File`][crate::File].
    /// ## `other_file`
    /// a [`File`][crate::File] or [`None`].
    /// ## `event_type`
    /// a [`FileMonitorEvent`][crate::FileMonitorEvent].
    #[doc(alias = "changed")]
    fn connect_changed<F: Fn(&Self, &File, Option<&File>, FileMonitorEvent) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    #[doc(alias = "cancelled")]
    fn connect_cancelled_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    #[doc(alias = "rate-limit")]
    fn connect_rate_limit_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<FileMonitor>> FileMonitorExt for O {
    fn cancel(&self) -> bool {
        unsafe { from_glib(ffi::g_file_monitor_cancel(self.as_ref().to_glib_none().0)) }
    }

    fn emit_event(
        &self,
        child: &impl IsA<File>,
        other_file: &impl IsA<File>,
        event_type: FileMonitorEvent,
    ) {
        unsafe {
            ffi::g_file_monitor_emit_event(
                self.as_ref().to_glib_none().0,
                child.as_ref().to_glib_none().0,
                other_file.as_ref().to_glib_none().0,
                event_type.into_glib(),
            );
        }
    }

    fn is_cancelled(&self) -> bool {
        unsafe {
            from_glib(ffi::g_file_monitor_is_cancelled(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn set_rate_limit(&self, limit_msecs: i32) {
        unsafe {
            ffi::g_file_monitor_set_rate_limit(self.as_ref().to_glib_none().0, limit_msecs);
        }
    }

    fn rate_limit(&self) -> i32 {
        glib::ObjectExt::property(self.as_ref(), "rate-limit")
    }

    fn connect_changed<F: Fn(&Self, &File, Option<&File>, FileMonitorEvent) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn changed_trampoline<
            P: IsA<FileMonitor>,
            F: Fn(&P, &File, Option<&File>, FileMonitorEvent) + 'static,
        >(
            this: *mut ffi::GFileMonitor,
            file: *mut ffi::GFile,
            other_file: *mut ffi::GFile,
            event_type: ffi::GFileMonitorEvent,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                FileMonitor::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(file),
                Option::<File>::from_glib_borrow(other_file)
                    .as_ref()
                    .as_ref(),
                from_glib(event_type),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"changed\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    changed_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_cancelled_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_cancelled_trampoline<
            P: IsA<FileMonitor>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::GFileMonitor,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(FileMonitor::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::cancelled\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_cancelled_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_rate_limit_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_rate_limit_trampoline<
            P: IsA<FileMonitor>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::GFileMonitor,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(FileMonitor::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::rate-limit\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_rate_limit_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for FileMonitor {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("FileMonitor")
    }
}