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