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