1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::{prelude::*, SessionItem};
use glib::translate::*;
use std::ptr;

impl SessionItem {
    /// Extract a metadata value matching `format`.
    ///
    /// `format` must not reference the [`glib::Variant`][struct@crate::glib::Variant], which means you need to make
    /// copies of data, such as "s" instead of "&s".
    /// ## `key`
    /// the key for the metadata value
    /// ## `format`
    /// the format of the value
    ///
    /// # Returns
    ///
    /// [`true`] if `key` was found with `format` and parameters were set.
    #[doc(alias = "panel_session_item_get_metadata")]
    #[doc(alias = "get_metadata")]
    #[inline]
    pub fn metadata<T: FromVariant>(&self, key: &str) -> Option<T> {
        self.metadata_value(key, Some(&*T::static_variant_type()))
            .and_then(|t| t.get())
    }

    /// If the item contains a metadata value for `key`.
    ///
    /// Checks if a value exists for a metadata key and retrieves the [`glib::VariantType`][crate::glib::VariantType]
    /// for that key.
    /// ## `key`
    /// the name of the metadata
    ///
    /// # Returns
    ///
    /// [`true`] if `self` contains metadata named `key` and `value_type` is set
    ///  to the value's [`glib::VariantType`][crate::glib::VariantType]. Otherwise [`false`] and `value_type` is unchanged.
    ///
    /// ## `value_type`
    /// a location for a [`glib::VariantType`][crate::glib::VariantType] or [`None`]
    #[doc(alias = "panel_session_item_has_metadata")]
    pub fn has_metadata(&self, key: &str) -> bool {
        unsafe {
            from_glib(ffi::panel_session_item_has_metadata(
                self.to_glib_none().0,
                key.to_glib_none().0,
                ptr::null_mut(),
            ))
        }
    }

    #[doc(alias = "panel_session_item_has_metadata")]
    pub fn metadata_type(&self, key: &str) -> Option<glib::VariantType> {
        unsafe {
            let mut value_type = ptr::null();
            let ret = from_glib(ffi::panel_session_item_has_metadata(
                self.to_glib_none().0,
                key.to_glib_none().0,
                &mut value_type,
            ));
            if ret {
                Some(from_glib_none(value_type))
            } else {
                None
            }
        }
    }

    /// A variadic helper to set metadata.
    ///
    /// The format should be identical to [`glib::Variant::new()`][crate::glib::Variant::new()].
    #[doc(alias = "panel_session_item_set_metadata")]
    #[inline]
    pub fn set_metadata<T: ToVariant>(&self, key: &str, value: Option<T>) {
        self.set_metadata_value(key, value.map(|v| v.to_variant()).as_ref())
    }
}