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
// 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::Buildable;
use crate::FileFilterFlags;
use crate::FileFilterInfo;
use glib::translate::*;
use std::boxed::Box as Box_;
use std::fmt;

glib::wrapper! {
    /// A GtkFileFilter can be used to restrict the files being shown in a
    /// [`FileChooser`][crate::FileChooser]. Files can be filtered based on their name (with
    /// [`add_pattern()`][Self::add_pattern()]), on their mime type (with
    /// [`add_mime_type()`][Self::add_mime_type()]), or by a custom filter function
    /// (with [`add_custom()`][Self::add_custom()]).
    ///
    /// Filtering by mime types handles aliasing and subclassing of mime
    /// types; e.g. a filter for text/plain also matches a file with mime
    /// type application/rtf, since application/rtf is a subclass of
    /// text/plain. Note that [`FileFilter`][crate::FileFilter] allows wildcards for the
    /// subtype of a mime type, so you can e.g. filter for image/\*.
    ///
    /// Normally, filters are used by adding them to a [`FileChooser`][crate::FileChooser],
    /// see [`FileChooserExt::add_filter()`][crate::prelude::FileChooserExt::add_filter()], but it is also possible
    /// to manually use a filter on a file with [`filter()`][Self::filter()].
    ///
    /// # GtkFileFilter as GtkBuildable
    ///
    /// The GtkFileFilter implementation of the GtkBuildable interface
    /// supports adding rules using the ``<mime-types>``, ``<patterns>`` and
    /// ``<applications>`` elements and listing the rules within. Specifying
    /// a ``<mime-type>`` or ``<pattern>`` has the same effect as as calling
    /// [`add_mime_type()`][Self::add_mime_type()] or [`add_pattern()`][Self::add_pattern()].
    ///
    /// An example of a UI definition fragment specifying GtkFileFilter
    /// rules:
    ///
    ///
    ///
    /// **⚠️ The following code is in xml ⚠️**
    ///
    /// ```xml
    /// <object class="GtkFileFilter">
    ///   <mime-types>
    ///     <mime-type>text/plain</mime-type>
    ///     <mime-type>image/ *</mime-type>
    ///   </mime-types>
    ///   <patterns>
    ///     <pattern>*.txt</pattern>
    ///     <pattern>*.png</pattern>
    ///   </patterns>
    /// </object>
    /// ```
    ///
    /// # Implements
    ///
    /// [`trait@glib::ObjectExt`], [`BuildableExt`][trait@crate::prelude::BuildableExt], [`BuildableExtManual`][trait@crate::prelude::BuildableExtManual]
    #[doc(alias = "GtkFileFilter")]
    pub struct FileFilter(Object<ffi::GtkFileFilter>) @implements Buildable;

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

impl FileFilter {
    /// Creates a new [`FileFilter`][crate::FileFilter] with no rules added to it.
    /// Such a filter doesn’t accept any files, so is not
    /// particularly useful until you add rules with
    /// [`add_mime_type()`][Self::add_mime_type()], [`add_pattern()`][Self::add_pattern()],
    /// or [`add_custom()`][Self::add_custom()]. To create a filter
    /// that accepts any file, use:
    ///
    ///
    /// **⚠️ The following code is in C ⚠️**
    ///
    /// ```C
    /// GtkFileFilter *filter = gtk_file_filter_new ();
    /// gtk_file_filter_add_pattern (filter, "*");
    /// ```
    ///
    /// # Returns
    ///
    /// a new [`FileFilter`][crate::FileFilter]
    #[doc(alias = "gtk_file_filter_new")]
    pub fn new() -> FileFilter {
        assert_initialized_main_thread!();
        unsafe { from_glib_none(ffi::gtk_file_filter_new()) }
    }

    /// Deserialize a file filter from an a{sv} variant in
    /// the format produced by [`to_gvariant()`][Self::to_gvariant()].
    /// ## `variant`
    /// an a{sv} [`glib::Variant`][struct@crate::glib::Variant]
    ///
    /// # Returns
    ///
    /// a new [`FileFilter`][crate::FileFilter] object
    #[doc(alias = "gtk_file_filter_new_from_gvariant")]
    #[doc(alias = "new_from_gvariant")]
    pub fn from_gvariant(variant: &glib::Variant) -> FileFilter {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::gtk_file_filter_new_from_gvariant(
                variant.to_glib_none().0,
            ))
        }
    }

    /// Adds rule to a filter that allows files based on a custom callback
    /// function. The bitfield `needed` which is passed in provides information
    /// about what sorts of information that the filter function needs;
    /// this allows GTK+ to avoid retrieving expensive information when
    /// it isn’t needed by the filter.
    /// ## `needed`
    /// bitfield of flags indicating the information that the custom
    ///  filter function needs.
    /// ## `func`
    /// callback function; if the function returns [`true`], then
    ///  the file will be displayed.
    /// ## `notify`
    /// function to call to free `data` when it is no longer needed.
    #[doc(alias = "gtk_file_filter_add_custom")]
    pub fn add_custom<P: Fn(&FileFilterInfo) -> bool + 'static>(
        &self,
        needed: FileFilterFlags,
        func: P,
    ) {
        let func_data: Box_<P> = Box_::new(func);
        unsafe extern "C" fn func_func<P: Fn(&FileFilterInfo) -> bool + 'static>(
            filter_info: *const ffi::GtkFileFilterInfo,
            data: glib::ffi::gpointer,
        ) -> glib::ffi::gboolean {
            let filter_info = from_glib_borrow(filter_info);
            let callback: &P = &*(data as *mut _);
            let res = (*callback)(&filter_info);
            res.into_glib()
        }
        let func = Some(func_func::<P> as _);
        unsafe extern "C" fn notify_func<P: Fn(&FileFilterInfo) -> bool + 'static>(
            data: glib::ffi::gpointer,
        ) {
            let _callback: Box_<P> = Box_::from_raw(data as *mut _);
        }
        let destroy_call4 = Some(notify_func::<P> as _);
        let super_callback0: Box_<P> = func_data;
        unsafe {
            ffi::gtk_file_filter_add_custom(
                self.to_glib_none().0,
                needed.into_glib(),
                func,
                Box_::into_raw(super_callback0) as *mut _,
                destroy_call4,
            );
        }
    }

    /// Adds a rule allowing a given mime type to `self`.
    /// ## `mime_type`
    /// name of a MIME type
    #[doc(alias = "gtk_file_filter_add_mime_type")]
    pub fn add_mime_type(&self, mime_type: &str) {
        unsafe {
            ffi::gtk_file_filter_add_mime_type(self.to_glib_none().0, mime_type.to_glib_none().0);
        }
    }

    /// Adds a rule allowing a shell style glob to a filter.
    /// ## `pattern`
    /// a shell style glob
    #[doc(alias = "gtk_file_filter_add_pattern")]
    pub fn add_pattern(&self, pattern: &str) {
        unsafe {
            ffi::gtk_file_filter_add_pattern(self.to_glib_none().0, pattern.to_glib_none().0);
        }
    }

    /// Adds a rule allowing image files in the formats supported
    /// by GdkPixbuf.
    #[doc(alias = "gtk_file_filter_add_pixbuf_formats")]
    pub fn add_pixbuf_formats(&self) {
        unsafe {
            ffi::gtk_file_filter_add_pixbuf_formats(self.to_glib_none().0);
        }
    }

    /// Tests whether a file should be displayed according to `self`.
    /// The [`FileFilterInfo`][crate::FileFilterInfo] `filter_info` should include
    /// the fields returned from [`needed()`][Self::needed()].
    ///
    /// This function will not typically be used by applications; it
    /// is intended principally for use in the implementation of
    /// [`FileChooser`][crate::FileChooser].
    /// ## `filter_info`
    /// a [`FileFilterInfo`][crate::FileFilterInfo] containing information
    ///  about a file.
    ///
    /// # Returns
    ///
    /// [`true`] if the file should be displayed
    #[doc(alias = "gtk_file_filter_filter")]
    pub fn filter(&self, filter_info: &FileFilterInfo) -> bool {
        unsafe {
            from_glib(ffi::gtk_file_filter_filter(
                self.to_glib_none().0,
                filter_info.to_glib_none().0,
            ))
        }
    }

    /// Gets the human-readable name for the filter. See [`set_name()`][Self::set_name()].
    ///
    /// # Returns
    ///
    /// The human-readable name of the filter,
    ///  or [`None`]. This value is owned by GTK+ and must not
    ///  be modified or freed.
    #[doc(alias = "gtk_file_filter_get_name")]
    #[doc(alias = "get_name")]
    pub fn name(&self) -> Option<glib::GString> {
        unsafe { from_glib_none(ffi::gtk_file_filter_get_name(self.to_glib_none().0)) }
    }

    /// Gets the fields that need to be filled in for the [`FileFilterInfo`][crate::FileFilterInfo]
    /// passed to [`filter()`][Self::filter()]
    ///
    /// This function will not typically be used by applications; it
    /// is intended principally for use in the implementation of
    /// [`FileChooser`][crate::FileChooser].
    ///
    /// # Returns
    ///
    /// bitfield of flags indicating needed fields when
    ///  calling [`filter()`][Self::filter()]
    #[doc(alias = "gtk_file_filter_get_needed")]
    #[doc(alias = "get_needed")]
    pub fn needed(&self) -> FileFilterFlags {
        unsafe { from_glib(ffi::gtk_file_filter_get_needed(self.to_glib_none().0)) }
    }

    /// Sets the human-readable name of the filter; this is the string
    /// that will be displayed in the file selector user interface if
    /// there is a selectable list of filters.
    /// ## `name`
    /// the human-readable-name for the filter, or [`None`]
    ///  to remove any existing name.
    #[doc(alias = "gtk_file_filter_set_name")]
    pub fn set_name(&self, name: Option<&str>) {
        unsafe {
            ffi::gtk_file_filter_set_name(self.to_glib_none().0, name.to_glib_none().0);
        }
    }

    /// Serialize a file filter to an a{sv} variant.
    ///
    /// # Returns
    ///
    /// a new, floating, [`glib::Variant`][struct@crate::glib::Variant]
    #[doc(alias = "gtk_file_filter_to_gvariant")]
    pub fn to_gvariant(&self) -> Option<glib::Variant> {
        unsafe { from_glib_none(ffi::gtk_file_filter_to_gvariant(self.to_glib_none().0)) }
    }
}

impl Default for FileFilter {
    fn default() -> Self {
        Self::new()
    }
}

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