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