gtk4/auto/
file_filter.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#![allow(deprecated)]
5
6use crate::{ffi, Buildable, Filter};
7use glib::{
8    prelude::*,
9    signal::{connect_raw, SignalHandlerId},
10    translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15    /// Filters files by name or mime type.
16    ///
17    /// [`FileFilter`][crate::FileFilter] can be used to restrict the files being shown in a
18    /// file chooser. Files can be filtered based on their name (with
19    /// [`add_pattern()`][Self::add_pattern()] or [`add_suffix()`][Self::add_suffix()])
20    /// or on their mime type (with [`add_mime_type()`][Self::add_mime_type()]).
21    ///
22    /// Filtering by mime types handles aliasing and subclassing of mime
23    /// types; e.g. a filter for text/plain also matches a file with mime
24    /// type application/rtf, since application/rtf is a subclass of
25    /// text/plain. Note that [`FileFilter`][crate::FileFilter] allows wildcards for the
26    /// subtype of a mime type, so you can e.g. filter for image/\*.
27    ///
28    /// Normally, file filters are used by adding them to a file chooser
29    /// (see [`FileDialog::set_filters()`][crate::FileDialog::set_filters()]), but it is also possible to
30    /// manually use a file filter on any [`FilterListModel`][crate::FilterListModel] containing
31    /// `GFileInfo` objects.
32    ///
33    /// # GtkFileFilter as GtkBuildable
34    ///
35    /// The [`FileFilter`][crate::FileFilter] implementation of the [`Buildable`][crate::Buildable] interface
36    /// supports adding rules using the `<mime-types>` and `<patterns>` and
37    /// `<suffixes>` elements and listing the rules within. Specifying a
38    /// `<mime-type>` or `<pattern>` or `<suffix>` has the same effect as
39    /// as calling
40    /// [`add_mime_type()`][Self::add_mime_type()] or
41    /// [`add_pattern()`][Self::add_pattern()] or
42    /// [`add_suffix()`][Self::add_suffix()].
43    ///
44    /// An example of a UI definition fragment specifying [`FileFilter`][crate::FileFilter]
45    /// rules:
46    /// ```xml
47    /// <object class="GtkFileFilter">
48    ///   <property name="name" translatable="yes">Text and Images</property>
49    ///   <mime-types>
50    ///     <mime-type>text/plain</mime-type>
51    ///     <mime-type>image/ *</mime-type>
52    ///   </mime-types>
53    ///   <patterns>
54    ///     <pattern>*.txt</pattern>
55    ///   </patterns>
56    ///   <suffixes>
57    ///     <suffix>png</suffix>
58    ///   </suffixes>
59    /// </object>
60    /// ```
61    ///
62    /// ## Properties
63    ///
64    ///
65    /// #### `mime-types`
66    ///  The MIME types that this filter matches.
67    ///
68    /// Writeable | Construct Only
69    ///
70    ///
71    /// #### `name`
72    ///  The human-readable name of the filter.
73    ///
74    /// This is the string that will be displayed in the user interface
75    /// if there is a selectable list of filters.
76    ///
77    /// Readable | Writeable
78    ///
79    ///
80    /// #### `patterns`
81    ///  The patterns that this filter matches.
82    ///
83    /// Writeable | Construct Only
84    ///
85    ///
86    /// #### `suffixes`
87    ///  The suffixes that this filter matches.
88    ///
89    /// Writeable | Construct Only
90    ///
91    /// # Implements
92    ///
93    /// [`FilterExt`][trait@crate::prelude::FilterExt], [`trait@glib::ObjectExt`], [`BuildableExt`][trait@crate::prelude::BuildableExt]
94    #[doc(alias = "GtkFileFilter")]
95    pub struct FileFilter(Object<ffi::GtkFileFilter>) @extends Filter, @implements Buildable;
96
97    match fn {
98        type_ => || ffi::gtk_file_filter_get_type(),
99    }
100}
101
102impl FileFilter {
103    /// Creates a new [`FileFilter`][crate::FileFilter] with no rules added to it.
104    ///
105    /// Such a filter doesn’t accept any files, so is not
106    /// particularly useful until you add rules with
107    /// [`add_mime_type()`][Self::add_mime_type()],
108    /// [`add_pattern()`][Self::add_pattern()],
109    /// [`add_suffix()`][Self::add_suffix()] or
110    /// [`add_pixbuf_formats()`][Self::add_pixbuf_formats()].
111    ///
112    /// To create a filter that accepts any file, use:
113    /// **⚠️ The following code is in c ⚠️**
114    ///
115    /// ```c
116    /// GtkFileFilter *filter = gtk_file_filter_new ();
117    /// gtk_file_filter_add_pattern (filter, "*");
118    /// ```
119    ///
120    /// # Returns
121    ///
122    /// a new [`FileFilter`][crate::FileFilter]
123    #[doc(alias = "gtk_file_filter_new")]
124    pub fn new() -> FileFilter {
125        assert_initialized_main_thread!();
126        unsafe { from_glib_full(ffi::gtk_file_filter_new()) }
127    }
128
129    /// Deserialize a file filter from a `GVariant`.
130    ///
131    /// The variant must be in the format produced by
132    /// [`to_gvariant()`][Self::to_gvariant()].
133    /// ## `variant`
134    /// an `a{sv}` `GVariant`
135    ///
136    /// # Returns
137    ///
138    /// a new [`FileFilter`][crate::FileFilter] object
139    #[doc(alias = "gtk_file_filter_new_from_gvariant")]
140    #[doc(alias = "new_from_gvariant")]
141    pub fn from_gvariant(variant: &glib::Variant) -> FileFilter {
142        assert_initialized_main_thread!();
143        unsafe {
144            from_glib_full(ffi::gtk_file_filter_new_from_gvariant(
145                variant.to_glib_none().0,
146            ))
147        }
148    }
149
150    /// Adds a rule allowing a given mime type.
151    /// ## `mime_type`
152    /// name of a MIME type
153    #[doc(alias = "gtk_file_filter_add_mime_type")]
154    pub fn add_mime_type(&self, mime_type: &str) {
155        unsafe {
156            ffi::gtk_file_filter_add_mime_type(self.to_glib_none().0, mime_type.to_glib_none().0);
157        }
158    }
159
160    /// Adds a rule allowing a shell style glob pattern.
161    ///
162    /// Note that it depends on the platform whether pattern
163    /// matching ignores case or not. On Windows, it does, on
164    /// other platforms, it doesn't.
165    /// ## `pattern`
166    /// a shell style glob pattern
167    #[doc(alias = "gtk_file_filter_add_pattern")]
168    pub fn add_pattern(&self, pattern: &str) {
169        unsafe {
170            ffi::gtk_file_filter_add_pattern(self.to_glib_none().0, pattern.to_glib_none().0);
171        }
172    }
173
174    /// Adds a rule allowing image files in the formats supported by [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf].
175    ///
176    /// This is equivalent to calling [`add_mime_type()`][Self::add_mime_type()]
177    /// for all the supported mime types.
178    ///
179    /// # Deprecated since 4.20
180    ///
181    /// Use the api of your image loading framework (e.g. glycin)
182    ///   to enumerate supported formats
183    #[cfg_attr(feature = "v4_20", deprecated = "Since 4.20")]
184    #[allow(deprecated)]
185    #[doc(alias = "gtk_file_filter_add_pixbuf_formats")]
186    pub fn add_pixbuf_formats(&self) {
187        unsafe {
188            ffi::gtk_file_filter_add_pixbuf_formats(self.to_glib_none().0);
189        }
190    }
191
192    /// Adds a suffix match rule to a filter.
193    ///
194    /// This is similar to adding a match for the pattern "*.@suffix"
195    ///
196    /// An exaple to filter files with the suffix ".sub":
197    /// **⚠️ The following code is in c ⚠️**
198    ///
199    /// ```c
200    /// gtk_file_filter_add_suffix (filter, "sub");
201    /// ```
202    ///
203    /// Filters with multiple dots are allowed.
204    ///
205    /// In contrast to pattern matches, suffix matches
206    /// are *always* case-insensitive.
207    /// ## `suffix`
208    /// filename suffix to match
209    #[cfg(feature = "v4_4")]
210    #[cfg_attr(docsrs, doc(cfg(feature = "v4_4")))]
211    #[doc(alias = "gtk_file_filter_add_suffix")]
212    pub fn add_suffix(&self, suffix: &str) {
213        unsafe {
214            ffi::gtk_file_filter_add_suffix(self.to_glib_none().0, suffix.to_glib_none().0);
215        }
216    }
217
218    /// Gets the attributes that need to be filled in for the `GFileInfo`
219    /// passed to this filter.
220    ///
221    /// This function will not typically be used by applications;
222    /// it is intended for use in file chooser implementation.
223    ///
224    /// # Returns
225    ///
226    /// the attributes
227    #[doc(alias = "gtk_file_filter_get_attributes")]
228    #[doc(alias = "get_attributes")]
229    pub fn attributes(&self) -> Vec<glib::GString> {
230        unsafe {
231            FromGlibPtrContainer::from_glib_none(ffi::gtk_file_filter_get_attributes(
232                self.to_glib_none().0,
233            ))
234        }
235    }
236
237    /// Gets the human-readable name for the filter.
238    ///
239    /// See [`set_name()`][Self::set_name()].
240    ///
241    /// # Returns
242    ///
243    /// the human-readable name of the filter
244    #[doc(alias = "gtk_file_filter_get_name")]
245    #[doc(alias = "get_name")]
246    pub fn name(&self) -> Option<glib::GString> {
247        unsafe { from_glib_none(ffi::gtk_file_filter_get_name(self.to_glib_none().0)) }
248    }
249
250    /// Sets a human-readable name of the filter.
251    ///
252    /// This is the string that will be displayed in the user interface
253    /// if there is a selectable list of filters.
254    /// ## `name`
255    /// the human-readable name for the filter
256    #[doc(alias = "gtk_file_filter_set_name")]
257    #[doc(alias = "name")]
258    pub fn set_name(&self, name: Option<&str>) {
259        unsafe {
260            ffi::gtk_file_filter_set_name(self.to_glib_none().0, name.to_glib_none().0);
261        }
262    }
263
264    /// Serialize a file filter to an `a{sv}` variant.
265    ///
266    /// # Returns
267    ///
268    /// a new, floating, `GVariant`
269    #[doc(alias = "gtk_file_filter_to_gvariant")]
270    pub fn to_gvariant(&self) -> glib::Variant {
271        unsafe { from_glib_none(ffi::gtk_file_filter_to_gvariant(self.to_glib_none().0)) }
272    }
273
274    #[doc(alias = "name")]
275    pub fn connect_name_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
276        unsafe extern "C" fn notify_name_trampoline<F: Fn(&FileFilter) + 'static>(
277            this: *mut ffi::GtkFileFilter,
278            _param_spec: glib::ffi::gpointer,
279            f: glib::ffi::gpointer,
280        ) {
281            let f: &F = &*(f as *const F);
282            f(&from_glib_borrow(this))
283        }
284        unsafe {
285            let f: Box_<F> = Box_::new(f);
286            connect_raw(
287                self.as_ptr() as *mut _,
288                c"notify::name".as_ptr() as *const _,
289                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
290                    notify_name_trampoline::<F> as *const (),
291                )),
292                Box_::into_raw(f),
293            )
294        }
295    }
296}
297
298impl Default for FileFilter {
299    fn default() -> Self {
300        Self::new()
301    }
302}