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 /// 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 given array of mime types.
161 /// It can for example be used with
162 /// [Gly.Loader.get_mime_types](https://gnome.pages.gitlab.gnome.org/glycin/libglycin/type_func.Loader.get_mime_types.html).
163 ///
164 /// This is equivalent to calling [`add_mime_type()`][Self::add_mime_type()]
165 /// for all the supported mime types.
166 /// ## `mime_types`
167 /// a [`None`]-terminated array of mime types
168 #[cfg(feature = "v4_22")]
169 #[cfg_attr(docsrs, doc(cfg(feature = "v4_22")))]
170 #[doc(alias = "gtk_file_filter_add_mime_types")]
171 pub fn add_mime_types(&self, mime_types: &[&str]) {
172 unsafe {
173 ffi::gtk_file_filter_add_mime_types(self.to_glib_none().0, mime_types.to_glib_none().0);
174 }
175 }
176
177 /// Adds a rule allowing a shell style glob pattern.
178 ///
179 /// Note that it depends on the platform whether pattern
180 /// matching ignores case or not. On Windows, it does, on
181 /// other platforms, it doesn't.
182 /// ## `pattern`
183 /// a shell style glob pattern
184 #[doc(alias = "gtk_file_filter_add_pattern")]
185 pub fn add_pattern(&self, pattern: &str) {
186 unsafe {
187 ffi::gtk_file_filter_add_pattern(self.to_glib_none().0, pattern.to_glib_none().0);
188 }
189 }
190
191 /// Adds a rule allowing image files in the formats supported by [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf].
192 ///
193 /// This is equivalent to calling [`add_mime_type()`][Self::add_mime_type()]
194 /// for all the supported mime types.
195 ///
196 /// # Deprecated since 4.20
197 ///
198 /// Use the api of your image loading framework (e.g. glycin)
199 /// to enumerate supported formats
200 #[cfg_attr(feature = "v4_20", deprecated = "Since 4.20")]
201 #[allow(deprecated)]
202 #[doc(alias = "gtk_file_filter_add_pixbuf_formats")]
203 pub fn add_pixbuf_formats(&self) {
204 unsafe {
205 ffi::gtk_file_filter_add_pixbuf_formats(self.to_glib_none().0);
206 }
207 }
208
209 /// Adds a suffix match rule to a filter.
210 ///
211 /// This is similar to adding a match for the pattern "*.@suffix"
212 ///
213 /// An exaple to filter files with the suffix ".sub":
214 /// **⚠️ The following code is in c ⚠️**
215 ///
216 /// ```c
217 /// gtk_file_filter_add_suffix (filter, "sub");
218 /// ```
219 ///
220 /// Filters with multiple dots are allowed.
221 ///
222 /// In contrast to pattern matches, suffix matches
223 /// are *always* case-insensitive.
224 /// ## `suffix`
225 /// filename suffix to match
226 #[cfg(feature = "v4_4")]
227 #[cfg_attr(docsrs, doc(cfg(feature = "v4_4")))]
228 #[doc(alias = "gtk_file_filter_add_suffix")]
229 pub fn add_suffix(&self, suffix: &str) {
230 unsafe {
231 ffi::gtk_file_filter_add_suffix(self.to_glib_none().0, suffix.to_glib_none().0);
232 }
233 }
234
235 /// Gets the attributes that need to be filled in for the `GFileInfo`
236 /// passed to this filter.
237 ///
238 /// This function will not typically be used by applications;
239 /// it is intended for use in file chooser implementation.
240 ///
241 /// # Returns
242 ///
243 /// the attributes
244 #[doc(alias = "gtk_file_filter_get_attributes")]
245 #[doc(alias = "get_attributes")]
246 pub fn attributes(&self) -> Vec<glib::GString> {
247 unsafe {
248 FromGlibPtrContainer::from_glib_none(ffi::gtk_file_filter_get_attributes(
249 self.to_glib_none().0,
250 ))
251 }
252 }
253
254 /// Gets the human-readable name for the filter.
255 ///
256 /// See [`set_name()`][Self::set_name()].
257 ///
258 /// # Returns
259 ///
260 /// the human-readable name of the filter
261 #[doc(alias = "gtk_file_filter_get_name")]
262 #[doc(alias = "get_name")]
263 pub fn name(&self) -> Option<glib::GString> {
264 unsafe { from_glib_none(ffi::gtk_file_filter_get_name(self.to_glib_none().0)) }
265 }
266
267 /// Sets a human-readable name of the filter.
268 ///
269 /// This is the string that will be displayed in the user interface
270 /// if there is a selectable list of filters.
271 /// ## `name`
272 /// the human-readable name for the filter
273 #[doc(alias = "gtk_file_filter_set_name")]
274 #[doc(alias = "name")]
275 pub fn set_name(&self, name: Option<&str>) {
276 unsafe {
277 ffi::gtk_file_filter_set_name(self.to_glib_none().0, name.to_glib_none().0);
278 }
279 }
280
281 /// Serialize a file filter to an `a{sv}` variant.
282 ///
283 /// # Returns
284 ///
285 /// a new, floating, `GVariant`
286 #[doc(alias = "gtk_file_filter_to_gvariant")]
287 pub fn to_gvariant(&self) -> glib::Variant {
288 unsafe { from_glib_none(ffi::gtk_file_filter_to_gvariant(self.to_glib_none().0)) }
289 }
290
291 #[doc(alias = "name")]
292 pub fn connect_name_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
293 unsafe extern "C" fn notify_name_trampoline<F: Fn(&FileFilter) + 'static>(
294 this: *mut ffi::GtkFileFilter,
295 _param_spec: glib::ffi::gpointer,
296 f: glib::ffi::gpointer,
297 ) {
298 unsafe {
299 let f: &F = &*(f as *const F);
300 f(&from_glib_borrow(this))
301 }
302 }
303 unsafe {
304 let f: Box_<F> = Box_::new(f);
305 connect_raw(
306 self.as_ptr() as *mut _,
307 c"notify::name".as_ptr(),
308 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
309 notify_name_trampoline::<F> as *const (),
310 )),
311 Box_::into_raw(f),
312 )
313 }
314 }
315}
316
317impl Default for FileFilter {
318 fn default() -> Self {
319 Self::new()
320 }
321}