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
// 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, Filter};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::boxed::Box as Box_;

glib::wrapper! {
    /// [`FileFilter`][crate::FileFilter] filters files by name or mime type.
    ///
    /// [`FileFilter`][crate::FileFilter] 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()] or [`add_suffix()`][Self::add_suffix()])
    /// or on their mime type (with [`add_mime_type()`][Self::add_mime_type()]).
    ///
    /// 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, file 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 file filter on any [`FilterListModel`][crate::FilterListModel] containing
    /// `GFileInfo` objects.
    ///
    /// # GtkFileFilter as GtkBuildable
    ///
    /// The [`FileFilter`][crate::FileFilter] implementation of the [`Buildable`][crate::Buildable] interface
    /// supports adding rules using the `<mime-types>` and `<patterns>` and
    /// `<suffixes>` elements and listing the rules within. Specifying a
    /// `<mime-type>` or `<pattern>` or `<suffix>` has the same effect as
    /// as calling
    /// [`add_mime_type()`][Self::add_mime_type()] or
    /// [`add_pattern()`][Self::add_pattern()] or
    /// [`add_suffix()`][Self::add_suffix()].
    ///
    /// An example of a UI definition fragment specifying [`FileFilter`][crate::FileFilter]
    /// rules:
    /// ```xml
    /// <object class="GtkFileFilter">
    ///   <property name="name" translatable="yes">Text and Images</property>
    ///   <mime-types>
    ///     <mime-type>text/plain</mime-type>
    ///     <mime-type>image/ *</mime-type>
    ///   </mime-types>
    ///   <patterns>
    ///     <pattern>*.txt</pattern>
    ///   </patterns>
    ///   <suffixes>
    ///     <suffix>png</suffix>
    ///   </suffixes>
    /// </object>
    /// ```
    ///
    /// ## Properties
    ///
    ///
    /// #### `mime-types`
    ///  The MIME types that this filter matches.
    ///
    /// Writeable | Construct Only
    ///
    ///
    /// #### `name`
    ///  The human-readable name of the filter.
    ///
    /// This is the string that will be displayed in the file chooser
    /// user interface if there is a selectable list of filters.
    ///
    /// Readable | Writeable
    ///
    ///
    /// #### `patterns`
    ///  The patterns that this filter matches.
    ///
    /// Writeable | Construct Only
    ///
    ///
    /// #### `suffixes`
    ///  The suffixes that this filter matches.
    ///
    /// Writeable | Construct Only
    ///
    /// # Implements
    ///
    /// [`FilterExt`][trait@crate::prelude::FilterExt], [`trait@glib::ObjectExt`], [`BuildableExt`][trait@crate::prelude::BuildableExt]
    #[doc(alias = "GtkFileFilter")]
    pub struct FileFilter(Object<ffi::GtkFileFilter>) @extends Filter, @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()],
    /// [`add_suffix()`][Self::add_suffix()] or
    /// [`add_pixbuf_formats()`][Self::add_pixbuf_formats()].
    ///
    /// 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_full(ffi::gtk_file_filter_new()) }
    }

    /// Deserialize a file filter from a `GVariant`.
    ///
    /// The variant must be in the format produced by
    /// [`to_gvariant()`][Self::to_gvariant()].
    /// ## `variant`
    /// an `a{sv}` `GVariant`
    ///
    /// # 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 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.
    ///
    /// Note that it depends on the platform whether pattern
    /// matching ignores case or not. On Windows, it does, on
    /// other platforms, it doesn't.
    /// ## `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.
    ///
    /// This is equivalent to calling [`add_mime_type()`][Self::add_mime_type()]
    /// for all the supported mime types.
    #[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);
        }
    }

    /// Adds a suffix match rule to a filter.
    ///
    /// This is similar to adding a match for the pattern
    /// "*.@suffix".
    ///
    /// In contrast to pattern matches, suffix matches
    /// are *always* case-insensitive.
    /// ## `suffix`
    /// filename suffix to match
    #[cfg(feature = "v4_4")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v4_4")))]
    #[doc(alias = "gtk_file_filter_add_suffix")]
    pub fn add_suffix(&self, suffix: &str) {
        unsafe {
            ffi::gtk_file_filter_add_suffix(self.to_glib_none().0, suffix.to_glib_none().0);
        }
    }

    /// Gets the attributes that need to be filled in for the `GFileInfo`
    /// passed to this filter.
    ///
    /// This function will not typically be used by applications;
    /// it is intended principally for use in the implementation
    /// of [`FileChooser`][crate::FileChooser].
    ///
    /// # Returns
    ///
    /// the attributes
    #[doc(alias = "gtk_file_filter_get_attributes")]
    #[doc(alias = "get_attributes")]
    pub fn attributes(&self) -> Vec<glib::GString> {
        unsafe {
            FromGlibPtrContainer::from_glib_none(ffi::gtk_file_filter_get_attributes(
                self.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
    #[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)) }
    }

    /// Sets a human-readable name of the filter.
    ///
    /// This is the string that will be displayed in the file chooser
    /// 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, `GVariant`
    #[doc(alias = "gtk_file_filter_to_gvariant")]
    pub fn to_gvariant(&self) -> glib::Variant {
        unsafe { from_glib_none(ffi::gtk_file_filter_to_gvariant(self.to_glib_none().0)) }
    }

    #[doc(alias = "name")]
    pub fn connect_name_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_name_trampoline<F: Fn(&FileFilter) + 'static>(
            this: *mut ffi::GtkFileFilter,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::name\0".as_ptr() as *const _,
                Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
                    notify_name_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

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