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
use crate::{Schema, SchemaAttributeType, SchemaFlags};
use glib::translate::*;
use std::collections::HashMap;

impl Schema {
    /// Using this function is not normally necessary from C code. This is useful
    /// for constructing #SecretSchema structures in bindings.
    ///
    /// A schema represents a set of attributes that are stored with an item. These
    /// schemas are used for interoperability between various services storing the
    /// same types of items.
    ///
    /// Each schema has an @name like `org.gnome.keyring.NetworkPassword`, and
    /// defines a set of attributes names, and types (string, integer, boolean) for
    /// those attributes.
    ///
    /// Each key in the @attributes table should be a attribute name strings, and
    /// the values in the table should be integers from the [`SchemaAttributeType`][crate::SchemaAttributeType]
    /// enumeration, representing the attribute type for each attribute name.
    ///
    /// Normally when looking up passwords only those with matching schema names are
    /// returned. If the schema @flags contain the [`SchemaFlags::DONT_MATCH_NAME`][crate::SchemaFlags::DONT_MATCH_NAME] flag,
    /// then lookups will not check that the schema name matches that on the item, only
    /// the schema's attributes are matched. This is useful when you are looking up items
    /// that are not stored by the libsecret library. Other libraries such as libgnome-keyring
    /// don't store the schema name.
    /// ## `name`
    /// the dotted name of the schema
    /// ## `flags`
    /// the flags for the schema
    /// ## `attribute_names_and_types`
    /// the attribute names and types of those attributes
    ///
    /// # Returns
    ///
    /// the new schema, which should be unreferenced with
    ///   `Schema::unref()` when done
    #[doc(alias = "secret_schema_new")]
    #[doc(alias = "secret_schema_newv")]
    pub fn new(
        name: &str,
        flags: SchemaFlags,
        attribute_names_and_types: HashMap<&str, SchemaAttributeType>,
    ) -> Self {
        unsafe {
            let hash_table = glib::ffi::g_hash_table_new_full(
                Some(glib::ffi::g_str_hash),
                Some(glib::ffi::g_str_equal),
                Some(glib::ffi::g_free),
                None,
            );

            for (name, type_) in attribute_names_and_types {
                let key_ptr: *mut libc::c_char = name.to_glib_full();
                glib::ffi::g_hash_table_insert(
                    hash_table,
                    key_ptr as *mut _,
                    type_.into_glib() as *mut _,
                );
            }

            from_glib_full(ffi::secret_schema_newv(
                name.to_glib_none().0,
                flags.into_glib(),
                hash_table,
            ))
        }
    }
}