Skip to main content

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