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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use glib::object::IsA;
use glib::translate::*;
use std::fmt;
use std::ptr;

glib::wrapper! {
    /// [`Icon`][crate::Icon] is a very minimal interface for icons. It provides functions
    /// for checking the equality of two icons, hashing of icons and
    /// serializing an icon to and from strings.
    ///
    /// [`Icon`][crate::Icon] does not provide the actual pixmap for the icon as this is out
    /// of GIO's scope, however implementations of [`Icon`][crate::Icon] may contain the name
    /// of an icon (see [`ThemedIcon`][crate::ThemedIcon]), or the path to an icon (see [`LoadableIcon`][crate::LoadableIcon]).
    ///
    /// To obtain a hash of a [`Icon`][crate::Icon], see `g_icon_hash()`.
    ///
    /// To check if two `GIcons` are equal, see `g_icon_equal()`.
    ///
    /// For serializing a [`Icon`][crate::Icon], use [`IconExt::serialize()`][crate::prelude::IconExt::serialize()] and
    /// [`deserialize()`][Self::deserialize()].
    ///
    /// If you want to consume [`Icon`][crate::Icon] (for example, in a toolkit) you must
    /// be prepared to handle at least the three following cases:
    /// [`LoadableIcon`][crate::LoadableIcon], [`ThemedIcon`][crate::ThemedIcon] and [`EmblemedIcon`][crate::EmblemedIcon]. It may also make
    /// sense to have fast-paths for other cases (like handling `GdkPixbuf`
    /// directly, for example) but all compliant [`Icon`][crate::Icon] implementations
    /// outside of GIO must implement [`LoadableIcon`][crate::LoadableIcon].
    ///
    /// If your application or library provides one or more [`Icon`][crate::Icon]
    /// implementations you need to ensure that your new implementation also
    /// implements [`LoadableIcon`][crate::LoadableIcon]. Additionally, you must provide an
    /// implementation of [`IconExt::serialize()`][crate::prelude::IconExt::serialize()] that gives a result that is
    /// understood by [`deserialize()`][Self::deserialize()], yielding one of the built-in icon
    /// types.
    ///
    /// # Implements
    ///
    /// [`IconExt`][trait@crate::prelude::IconExt]
    #[doc(alias = "GIcon")]
    pub struct Icon(Interface<ffi::GIcon, ffi::GIconIface>);

    match fn {
        type_ => || ffi::g_icon_get_type(),
    }
}

impl Icon {
    pub const NONE: Option<&'static Icon> = None;

    /// Deserializes a [`Icon`][crate::Icon] previously serialized using [`IconExt::serialize()`][crate::prelude::IconExt::serialize()].
    /// ## `value`
    /// a [`glib::Variant`][struct@crate::glib::Variant] created with [`IconExt::serialize()`][crate::prelude::IconExt::serialize()]
    ///
    /// # Returns
    ///
    /// a [`Icon`][crate::Icon], or [`None`] when deserialization fails.
    #[doc(alias = "g_icon_deserialize")]
    pub fn deserialize(value: &glib::Variant) -> Option<Icon> {
        unsafe { from_glib_full(ffi::g_icon_deserialize(value.to_glib_none().0)) }
    }

    #[doc(alias = "g_icon_hash")]
    pub fn hash(&self) -> u32 {
        unsafe {
            ffi::g_icon_hash(
                ToGlibPtr::<*mut ffi::GIcon>::to_glib_none(self).0 as glib::ffi::gconstpointer,
            )
        }
    }

    /// Generate a [`Icon`][crate::Icon] instance from `str`. This function can fail if
    /// `str` is not valid - see [`IconExt::to_string()`][crate::prelude::IconExt::to_string()] for discussion.
    ///
    /// If your application or library provides one or more [`Icon`][crate::Icon]
    /// implementations you need to ensure that each `GType` is registered
    /// with the type system prior to calling [`for_string()`][Self::for_string()].
    /// ## `str`
    /// A string obtained via [`IconExt::to_string()`][crate::prelude::IconExt::to_string()].
    ///
    /// # Returns
    ///
    /// An object implementing the [`Icon`][crate::Icon]
    ///  interface or [`None`] if `error` is set.
    #[doc(alias = "g_icon_new_for_string")]
    #[doc(alias = "new_for_string")]
    pub fn for_string(str: &str) -> Result<Icon, glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_icon_new_for_string(str.to_glib_none().0, &mut error);
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }
}

/// Trait containing all [`struct@Icon`] methods.
///
/// # Implementors
///
/// [`BytesIcon`][struct@crate::BytesIcon], [`Emblem`][struct@crate::Emblem], [`EmblemedIcon`][struct@crate::EmblemedIcon], [`FileIcon`][struct@crate::FileIcon], [`Icon`][struct@crate::Icon], [`LoadableIcon`][struct@crate::LoadableIcon], [`ThemedIcon`][struct@crate::ThemedIcon]
pub trait IconExt: 'static {
    #[doc(alias = "g_icon_equal")]
    fn equal(&self, icon2: Option<&impl IsA<Icon>>) -> bool;

    /// Serializes a [`Icon`][crate::Icon] into a [`glib::Variant`][struct@crate::glib::Variant]. An equivalent [`Icon`][crate::Icon] can be retrieved
    /// back by calling [`Icon::deserialize()`][crate::Icon::deserialize()] on the returned value.
    /// As serialization will avoid using raw icon data when possible, it only
    /// makes sense to transfer the [`glib::Variant`][struct@crate::glib::Variant] between processes on the same machine,
    /// (as opposed to over the network), and within the same file system namespace.
    ///
    /// # Returns
    ///
    /// a [`glib::Variant`][struct@crate::glib::Variant], or [`None`] when serialization fails. The [`glib::Variant`][struct@crate::glib::Variant] will not be floating.
    #[doc(alias = "g_icon_serialize")]
    fn serialize(&self) -> Option<glib::Variant>;

    /// Generates a textual representation of `self` that can be used for
    /// serialization such as when passing `self` to a different process or
    /// saving it to persistent storage. Use [`Icon::for_string()`][crate::Icon::for_string()] to
    /// get `self` back from the returned string.
    ///
    /// The encoding of the returned string is proprietary to [`Icon`][crate::Icon] except
    /// in the following two cases
    ///
    /// - If `self` is a [`FileIcon`][crate::FileIcon], the returned string is a native path
    ///  (such as `/path/to/my icon.png`) without escaping
    ///  if the [`File`][crate::File] for `self` is a native file. If the file is not
    ///  native, the returned string is the result of [`FileExt::uri()`][crate::prelude::FileExt::uri()]
    ///  (such as `sftp://path/to/my`20icon``).
    ///
    /// - If `self` is a [`ThemedIcon`][crate::ThemedIcon] with exactly one name and no fallbacks,
    ///  the encoding is simply the name (such as `network-server`).
    ///
    /// # Returns
    ///
    /// An allocated NUL-terminated UTF8 string or
    /// [`None`] if `self` can't be serialized. Use `g_free()` to free.
    #[doc(alias = "g_icon_to_string")]
    fn to_string(&self) -> Option<glib::GString>;
}

impl<O: IsA<Icon>> IconExt for O {
    fn equal(&self, icon2: Option<&impl IsA<Icon>>) -> bool {
        unsafe {
            from_glib(ffi::g_icon_equal(
                self.as_ref().to_glib_none().0,
                icon2.map(|p| p.as_ref()).to_glib_none().0,
            ))
        }
    }

    fn serialize(&self) -> Option<glib::Variant> {
        unsafe { from_glib_full(ffi::g_icon_serialize(self.as_ref().to_glib_none().0)) }
    }

    fn to_string(&self) -> Option<glib::GString> {
        unsafe { from_glib_full(ffi::g_icon_to_string(self.as_ref().to_glib_none().0)) }
    }
}

impl fmt::Display for Icon {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("Icon")
    }
}