Skip to main content

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