Skip to main content

gtk/auto/
recent_manager.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::{RecentData, RecentInfo, ffi};
6use glib::{
7    object::ObjectType as _,
8    prelude::*,
9    signal::{SignalHandlerId, connect_raw},
10    translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15    /// [`RecentManager`][crate::RecentManager] provides a facility for adding, removing and
16    /// looking up recently used files. Each recently used file is
17    /// identified by its URI, and has meta-data associated to it, like
18    /// the names and command lines of the applications that have
19    /// registered it, the number of time each application has registered
20    /// the same file, the mime type of the file and whether the file
21    /// should be displayed only by the applications that have
22    /// registered it.
23    ///
24    /// The recently used files list is per user.
25    ///
26    /// The [`RecentManager`][crate::RecentManager] acts like a database of all the recently
27    /// used files. You can create new [`RecentManager`][crate::RecentManager] objects, but
28    /// it is more efficient to use the default manager created by GTK+.
29    ///
30    /// Adding a new recently used file is as simple as:
31    ///
32    ///
33    ///
34    /// **⚠️ The following code is in C ⚠️**
35    ///
36    /// ```C
37    /// GtkRecentManager *manager;
38    ///
39    /// manager = gtk_recent_manager_get_default ();
40    /// gtk_recent_manager_add_item (manager, file_uri);
41    /// ```
42    ///
43    /// The [`RecentManager`][crate::RecentManager] will try to gather all the needed information
44    /// from the file itself through GIO.
45    ///
46    /// Looking up the meta-data associated with a recently used file
47    /// given its URI requires calling [`RecentManagerExt::lookup_item()`][crate::prelude::RecentManagerExt::lookup_item()]:
48    ///
49    ///
50    ///
51    /// **⚠️ The following code is in C ⚠️**
52    ///
53    /// ```C
54    /// GtkRecentManager *manager;
55    /// GtkRecentInfo *info;
56    /// GError *error = NULL;
57    ///
58    /// manager = gtk_recent_manager_get_default ();
59    /// info = gtk_recent_manager_lookup_item (manager, file_uri, &error);
60    /// if (error)
61    ///   {
62    ///     g_warning ("Could not find the file: %s", error->message);
63    ///     g_error_free (error);
64    ///   }
65    /// else
66    ///  {
67    ///    // Use the info object
68    ///    gtk_recent_info_unref (info);
69    ///  }
70    /// ```
71    ///
72    /// In order to retrieve the list of recently used files, you can use
73    /// [`RecentManagerExt::items()`][crate::prelude::RecentManagerExt::items()], which returns a list of [`RecentInfo`][crate::RecentInfo]-structs.
74    ///
75    /// A [`RecentManager`][crate::RecentManager] is the model used to populate the contents of
76    /// one, or more [`RecentChooser`][crate::RecentChooser] implementations.
77    ///
78    /// Note that the maximum age of the recently used files list is
79    /// controllable through the [`gtk-recent-files-max-age`][struct@crate::Settings#gtk-recent-files-max-age]
80    /// property.
81    ///
82    /// Recently used files are supported since GTK+ 2.10.
83    ///
84    /// ## Properties
85    ///
86    ///
87    /// #### `filename`
88    ///  The full path to the file to be used to store and read the
89    /// recently used resources list
90    ///
91    /// Readable | Writable | Construct Only
92    ///
93    ///
94    /// #### `size`
95    ///  The size of the recently used resources list.
96    ///
97    /// Readable
98    ///
99    /// ## Signals
100    ///
101    ///
102    /// #### `changed`
103    ///  Emitted when the current recently used resources manager changes
104    /// its contents, either by calling [`RecentManagerExt::add_item()`][crate::prelude::RecentManagerExt::add_item()] or
105    /// by another application.
106    ///
107    ///
108    ///
109    /// # Implements
110    ///
111    /// [`RecentManagerExt`][trait@crate::prelude::RecentManagerExt], [`trait@glib::ObjectExt`]
112    #[doc(alias = "GtkRecentManager")]
113    pub struct RecentManager(Object<ffi::GtkRecentManager, ffi::GtkRecentManagerClass>);
114
115    match fn {
116        type_ => || ffi::gtk_recent_manager_get_type(),
117    }
118}
119
120impl RecentManager {
121    pub const NONE: Option<&'static RecentManager> = None;
122
123    /// Creates a new recent manager object. Recent manager objects are used to
124    /// handle the list of recently used resources. A [`RecentManager`][crate::RecentManager] object
125    /// monitors the recently used resources list, and emits the “changed” signal
126    /// each time something inside the list changes.
127    ///
128    /// [`RecentManager`][crate::RecentManager] objects are expensive: be sure to create them only when
129    /// needed. You should use [`default()`][Self::default()] instead.
130    ///
131    /// # Returns
132    ///
133    /// A newly created [`RecentManager`][crate::RecentManager] object
134    #[doc(alias = "gtk_recent_manager_new")]
135    pub fn new() -> RecentManager {
136        assert_initialized_main_thread!();
137        unsafe { from_glib_full(ffi::gtk_recent_manager_new()) }
138    }
139
140    // rustdoc-stripper-ignore-next
141    /// Creates a new builder-pattern struct instance to construct [`RecentManager`] objects.
142    ///
143    /// This method returns an instance of [`RecentManagerBuilder`](crate::builders::RecentManagerBuilder) which can be used to create [`RecentManager`] objects.
144    pub fn builder() -> RecentManagerBuilder {
145        RecentManagerBuilder::new()
146    }
147
148    /// Gets a unique instance of [`RecentManager`][crate::RecentManager], that you can share
149    /// in your application without caring about memory management.
150    ///
151    /// # Returns
152    ///
153    /// A unique [`RecentManager`][crate::RecentManager]. Do not ref or
154    ///  unref it.
155    #[doc(alias = "gtk_recent_manager_get_default")]
156    #[doc(alias = "get_default")]
157    #[allow(clippy::should_implement_trait)]
158    pub fn default() -> Option<RecentManager> {
159        assert_initialized_main_thread!();
160        unsafe { from_glib_none(ffi::gtk_recent_manager_get_default()) }
161    }
162}
163
164impl Default for RecentManager {
165    fn default() -> Self {
166        Self::new()
167    }
168}
169
170// rustdoc-stripper-ignore-next
171/// A [builder-pattern] type to construct [`RecentManager`] objects.
172///
173/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
174#[must_use = "The builder must be built to be used"]
175pub struct RecentManagerBuilder {
176    builder: glib::object::ObjectBuilder<'static, RecentManager>,
177}
178
179impl RecentManagerBuilder {
180    fn new() -> Self {
181        Self {
182            builder: glib::object::Object::builder(),
183        }
184    }
185
186    /// The full path to the file to be used to store and read the
187    /// recently used resources list
188    pub fn filename(self, filename: impl Into<glib::GString>) -> Self {
189        Self {
190            builder: self.builder.property("filename", filename.into()),
191        }
192    }
193
194    // rustdoc-stripper-ignore-next
195    /// Build the [`RecentManager`].
196    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
197    pub fn build(self) -> RecentManager {
198        assert_initialized_main_thread!();
199        self.builder.build()
200    }
201}
202
203/// Trait containing all [`struct@RecentManager`] methods.
204///
205/// # Implementors
206///
207/// [`RecentManager`][struct@crate::RecentManager]
208pub trait RecentManagerExt: IsA<RecentManager> + 'static {
209    /// Adds a new resource, pointed by `uri`, into the recently used
210    /// resources list, using the metadata specified inside the
211    /// [`RecentData`][crate::RecentData]-struct passed in `recent_data`.
212    ///
213    /// The passed URI will be used to identify this resource inside the
214    /// list.
215    ///
216    /// In order to register the new recently used resource, metadata about
217    /// the resource must be passed as well as the URI; the metadata is
218    /// stored in a [`RecentData`][crate::RecentData]-struct, which must contain the MIME
219    /// type of the resource pointed by the URI; the name of the application
220    /// that is registering the item, and a command line to be used when
221    /// launching the item.
222    ///
223    /// Optionally, a [`RecentData`][crate::RecentData]-struct might contain a UTF-8 string
224    /// to be used when viewing the item instead of the last component of
225    /// the URI; a short description of the item; whether the item should
226    /// be considered private - that is, should be displayed only by the
227    /// applications that have registered it.
228    /// ## `uri`
229    /// a valid URI
230    /// ## `recent_data`
231    /// metadata of the resource
232    ///
233    /// # Returns
234    ///
235    /// [`true`] if the new item was successfully added to the
236    ///  recently used resources list, [`false`] otherwise
237    #[doc(alias = "gtk_recent_manager_add_full")]
238    fn add_full(&self, uri: &str, recent_data: &RecentData) -> bool {
239        unsafe {
240            from_glib(ffi::gtk_recent_manager_add_full(
241                self.as_ref().to_glib_none().0,
242                uri.to_glib_none().0,
243                recent_data.to_glib_none().0,
244            ))
245        }
246    }
247
248    /// Adds a new resource, pointed by `uri`, into the recently used
249    /// resources list.
250    ///
251    /// This function automatically retrieves some of the needed
252    /// metadata and setting other metadata to common default values;
253    /// it then feeds the data to [`add_full()`][Self::add_full()].
254    ///
255    /// See [`add_full()`][Self::add_full()] if you want to explicitly
256    /// define the metadata for the resource pointed by `uri`.
257    /// ## `uri`
258    /// a valid URI
259    ///
260    /// # Returns
261    ///
262    /// [`true`] if the new item was successfully added
263    ///  to the recently used resources list
264    #[doc(alias = "gtk_recent_manager_add_item")]
265    fn add_item(&self, uri: &str) -> bool {
266        unsafe {
267            from_glib(ffi::gtk_recent_manager_add_item(
268                self.as_ref().to_glib_none().0,
269                uri.to_glib_none().0,
270            ))
271        }
272    }
273
274    /// Gets the list of recently used resources.
275    ///
276    /// # Returns
277    ///
278    /// a list of
279    ///  newly allocated [`RecentInfo`][crate::RecentInfo] objects. Use
280    ///  `gtk_recent_info_unref()` on each item inside the list, and then
281    ///  free the list itself using `g_list_free()`.
282    #[doc(alias = "gtk_recent_manager_get_items")]
283    #[doc(alias = "get_items")]
284    fn items(&self) -> Vec<RecentInfo> {
285        unsafe {
286            FromGlibPtrContainer::from_glib_full(ffi::gtk_recent_manager_get_items(
287                self.as_ref().to_glib_none().0,
288            ))
289        }
290    }
291
292    /// Checks whether there is a recently used resource registered
293    /// with `uri` inside the recent manager.
294    /// ## `uri`
295    /// a URI
296    ///
297    /// # Returns
298    ///
299    /// [`true`] if the resource was found, [`false`] otherwise
300    #[doc(alias = "gtk_recent_manager_has_item")]
301    fn has_item(&self, uri: &str) -> bool {
302        unsafe {
303            from_glib(ffi::gtk_recent_manager_has_item(
304                self.as_ref().to_glib_none().0,
305                uri.to_glib_none().0,
306            ))
307        }
308    }
309
310    /// Searches for a URI inside the recently used resources list, and
311    /// returns a [`RecentInfo`][crate::RecentInfo]-struct containing informations about the resource
312    /// like its MIME type, or its display name.
313    /// ## `uri`
314    /// a URI
315    ///
316    /// # Returns
317    ///
318    /// a [`RecentInfo`][crate::RecentInfo]-struct containing information
319    ///  about the resource pointed by `uri`, or [`None`] if the URI was
320    ///  not registered in the recently used resources list. Free with
321    ///  `gtk_recent_info_unref()`.
322    #[doc(alias = "gtk_recent_manager_lookup_item")]
323    fn lookup_item(&self, uri: &str) -> Result<Option<RecentInfo>, glib::Error> {
324        unsafe {
325            let mut error = std::ptr::null_mut();
326            let ret = ffi::gtk_recent_manager_lookup_item(
327                self.as_ref().to_glib_none().0,
328                uri.to_glib_none().0,
329                &mut error,
330            );
331            if error.is_null() {
332                Ok(from_glib_full(ret))
333            } else {
334                Err(from_glib_full(error))
335            }
336        }
337    }
338
339    /// Changes the location of a recently used resource from `uri` to `new_uri`.
340    ///
341    /// Please note that this function will not affect the resource pointed
342    /// by the URIs, but only the URI used in the recently used resources list.
343    /// ## `uri`
344    /// the URI of a recently used resource
345    /// ## `new_uri`
346    /// the new URI of the recently used resource, or
347    ///  [`None`] to remove the item pointed by `uri` in the list
348    ///
349    /// # Returns
350    ///
351    /// [`true`] on success
352    #[doc(alias = "gtk_recent_manager_move_item")]
353    fn move_item(&self, uri: &str, new_uri: Option<&str>) -> Result<(), glib::Error> {
354        unsafe {
355            let mut error = std::ptr::null_mut();
356            let is_ok = ffi::gtk_recent_manager_move_item(
357                self.as_ref().to_glib_none().0,
358                uri.to_glib_none().0,
359                new_uri.to_glib_none().0,
360                &mut error,
361            );
362            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
363            if error.is_null() {
364                Ok(())
365            } else {
366                Err(from_glib_full(error))
367            }
368        }
369    }
370
371    /// Purges every item from the recently used resources list.
372    ///
373    /// # Returns
374    ///
375    /// the number of items that have been removed from the
376    ///  recently used resources list
377    #[doc(alias = "gtk_recent_manager_purge_items")]
378    fn purge_items(&self) -> Result<i32, glib::Error> {
379        unsafe {
380            let mut error = std::ptr::null_mut();
381            let ret =
382                ffi::gtk_recent_manager_purge_items(self.as_ref().to_glib_none().0, &mut error);
383            if error.is_null() {
384                Ok(ret)
385            } else {
386                Err(from_glib_full(error))
387            }
388        }
389    }
390
391    /// Removes a resource pointed by `uri` from the recently used resources
392    /// list handled by a recent manager.
393    /// ## `uri`
394    /// the URI of the item you wish to remove
395    ///
396    /// # Returns
397    ///
398    /// [`true`] if the item pointed by `uri` has been successfully
399    ///  removed by the recently used resources list, and [`false`] otherwise
400    #[doc(alias = "gtk_recent_manager_remove_item")]
401    fn remove_item(&self, uri: &str) -> Result<(), glib::Error> {
402        unsafe {
403            let mut error = std::ptr::null_mut();
404            let is_ok = ffi::gtk_recent_manager_remove_item(
405                self.as_ref().to_glib_none().0,
406                uri.to_glib_none().0,
407                &mut error,
408            );
409            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
410            if error.is_null() {
411                Ok(())
412            } else {
413                Err(from_glib_full(error))
414            }
415        }
416    }
417
418    /// The full path to the file to be used to store and read the
419    /// recently used resources list
420    fn filename(&self) -> Option<glib::GString> {
421        ObjectExt::property(self.as_ref(), "filename")
422    }
423
424    /// The size of the recently used resources list.
425    fn size(&self) -> i32 {
426        ObjectExt::property(self.as_ref(), "size")
427    }
428
429    /// Emitted when the current recently used resources manager changes
430    /// its contents, either by calling [`add_item()`][Self::add_item()] or
431    /// by another application.
432    #[doc(alias = "changed")]
433    fn connect_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
434        unsafe extern "C" fn changed_trampoline<P: IsA<RecentManager>, F: Fn(&P) + 'static>(
435            this: *mut ffi::GtkRecentManager,
436            f: glib::ffi::gpointer,
437        ) {
438            unsafe {
439                let f: &F = &*(f as *const F);
440                f(RecentManager::from_glib_borrow(this).unsafe_cast_ref())
441            }
442        }
443        unsafe {
444            let f: Box_<F> = Box_::new(f);
445            connect_raw(
446                self.as_ptr() as *mut _,
447                c"changed".as_ptr(),
448                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
449                    changed_trampoline::<Self, F> as *const (),
450                )),
451                Box_::into_raw(f),
452            )
453        }
454    }
455
456    #[doc(alias = "size")]
457    fn connect_size_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
458        unsafe extern "C" fn notify_size_trampoline<P: IsA<RecentManager>, F: Fn(&P) + 'static>(
459            this: *mut ffi::GtkRecentManager,
460            _param_spec: glib::ffi::gpointer,
461            f: glib::ffi::gpointer,
462        ) {
463            unsafe {
464                let f: &F = &*(f as *const F);
465                f(RecentManager::from_glib_borrow(this).unsafe_cast_ref())
466            }
467        }
468        unsafe {
469            let f: Box_<F> = Box_::new(f);
470            connect_raw(
471                self.as_ptr() as *mut _,
472                c"notify::size".as_ptr(),
473                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
474                    notify_size_trampoline::<Self, F> as *const (),
475                )),
476                Box_::into_raw(f),
477            )
478        }
479    }
480}
481
482impl<O: IsA<RecentManager>> RecentManagerExt for O {}