1
use std::{collections::HashMap, fmt, hash::Hash, time::Duration};
2

            
3
use ashpd::WindowIdentifier;
4
use serde::Serialize;
5
use zbus::zvariant::{ObjectPath, OwnedObjectPath, Type};
6

            
7
use super::{DBusSecret, DESTINATION, Prompt, Session, Unlockable};
8
use crate::{
9
    AsAttributes,
10
    dbus::{Error, ServiceError},
11
};
12

            
13
#[derive(Type, Clone)]
14
#[zvariant(signature = "o")]
15
#[doc(alias = "org.freedesktop.Secret.Item")]
16
pub struct Item(zbus::Proxy<'static>);
17

            
18
impl zbus::proxy::Defaults for Item {
19
    const INTERFACE: &'static Option<zbus::names::InterfaceName<'static>> = &Some(
20
        zbus::names::InterfaceName::from_static_str_unchecked("org.freedesktop.Secret.Item"),
21
    );
22
    const DESTINATION: &'static Option<zbus::names::BusName<'static>> = &Some(DESTINATION);
23
    const PATH: &'static Option<ObjectPath<'static>> = &None;
24
}
25

            
26
impl From<zbus::Proxy<'static>> for Item {
27
2
    fn from(value: zbus::Proxy<'static>) -> Self {
28
        Self(value)
29
    }
30
}
31

            
32
impl Item {
33
12
    pub async fn new<P>(connection: &zbus::Connection, object_path: P) -> Result<Self, Error>
34
    where
35
        P: TryInto<ObjectPath<'static>>,
36
        P::Error: Into<zbus::Error>,
37
    {
38
24
        zbus::proxy::Builder::new(connection)
39
6
            .path(object_path)?
40
6
            .uncached_properties(&["Label", "Attributes", "Created", "Modified", "Locked"])
41
            .build()
42
18
            .await
43
6
            .map_err(From::from)
44
    }
45

            
46
4
    pub(crate) async fn from_paths<P>(
47
        connection: &zbus::Connection,
48
        paths: Vec<P>,
49
    ) -> Result<Vec<Self>, Error>
50
    where
51
        P: TryInto<ObjectPath<'static>>,
52
        P::Error: Into<zbus::Error>,
53
    {
54
8
        let mut items = Vec::with_capacity(paths.capacity());
55
16
        for path in paths.into_iter() {
56
16
            items.push(Self::new(connection, path).await?);
57
        }
58
4
        Ok(items)
59
    }
60

            
61
2
    pub fn inner(&self) -> &zbus::Proxy<'static> {
62
        &self.0
63
    }
64

            
65
    #[doc(alias = "Locked")]
66
8
    pub async fn is_locked(&self) -> Result<bool, Error> {
67
6
        self.inner()
68
            .get_property("Locked")
69
8
            .await
70
2
            .map_err(From::from)
71
    }
72

            
73
8
    pub async fn label(&self) -> Result<String, Error> {
74
6
        self.inner().get_property("Label").await.map_err(From::from)
75
    }
76

            
77
8
    pub async fn set_label(&self, label: &str) -> Result<(), Error> {
78
8
        self.inner().set_property("Label", label).await?;
79
2
        Ok(())
80
    }
81

            
82
8
    pub async fn created(&self) -> Result<Duration, Error> {
83
8
        let secs = self.inner().get_property::<u64>("Created").await?;
84
2
        Ok(Duration::from_secs(secs))
85
    }
86

            
87
8
    pub async fn modified(&self) -> Result<Duration, Error> {
88
8
        let secs = self.inner().get_property::<u64>("Modified").await?;
89
2
        Ok(Duration::from_secs(secs))
90
    }
91

            
92
8
    pub async fn attributes(&self) -> Result<HashMap<String, String>, Error> {
93
6
        self.inner()
94
            .get_property("Attributes")
95
8
            .await
96
2
            .map_err(From::from)
97
    }
98

            
99
16
    pub async fn set_attributes(&self, attributes: &impl AsAttributes) -> Result<(), Error> {
100
20
        self.inner()
101
4
            .set_property("Attributes", attributes.as_attributes())
102
20
            .await?;
103
4
        Ok(())
104
    }
105

            
106
10
    pub async fn delete(&self, window_id: Option<WindowIdentifier>) -> Result<(), Error> {
107
10
        let prompt_path = self
108
            .inner()
109
2
            .call_method("Delete", &())
110
8
            .await
111
2
            .map_err::<ServiceError, _>(From::from)?
112
            .body()
113
            .deserialize::<OwnedObjectPath>()?;
114
2
        if let Some(prompt) = Prompt::new(self.inner().connection(), prompt_path).await? {
115
6
            let _ = prompt.receive_completed(window_id).await?;
116
        }
117
2
        Ok(())
118
    }
119

            
120
    #[doc(alias = "GetSecret")]
121
8
    pub async fn secret(&self, session: &Session) -> Result<DBusSecret, Error> {
122
14
        let inner = self
123
            .inner()
124
2
            .call_method("GetSecret", &(session))
125
8
            .await
126
4
            .map_err::<ServiceError, _>(From::from)?
127
            .body()
128
            .deserialize::<super::secret::DBusSecretInner>()?;
129
2
        DBusSecret::from_inner(self.inner().connection(), inner).await
130
    }
131

            
132
    #[doc(alias = "SetSecret")]
133
8
    pub async fn set_secret(&self, secret: &DBusSecret) -> Result<(), Error> {
134
12
        self.inner()
135
2
            .call_method("SetSecret", &(secret,))
136
8
            .await
137
6
            .map_err::<ServiceError, _>(From::from)?;
138
2
        Ok(())
139
    }
140
}
141

            
142
impl Serialize for Item {
143
4
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
144
    where
145
        S: serde::Serializer,
146
    {
147
8
        ObjectPath::serialize(self.inner().path(), serializer)
148
    }
149
}
150

            
151
impl PartialEq for Item {
152
2
    fn eq(&self, other: &Self) -> bool {
153
2
        self.inner().path() == other.inner().path()
154
    }
155
}
156

            
157
impl Eq for Item {}
158

            
159
impl Hash for Item {
160
2
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
161
2
        self.inner().path().hash(state);
162
    }
163
}
164

            
165
impl fmt::Debug for Item {
166
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167
        f.debug_tuple("Item")
168
            .field(&self.inner().path().as_str())
169
            .finish()
170
    }
171
}
172

            
173
impl Unlockable for Item {}