1
use std::fmt;
2

            
3
use ashpd::WindowIdentifier;
4
use futures_util::StreamExt;
5
use serde::Serialize;
6
use zbus::zvariant::{ObjectPath, OwnedValue, Type};
7

            
8
use super::DESTINATION;
9
use crate::dbus::{Error, ServiceError};
10

            
11
#[derive(Type)]
12
#[zvariant(signature = "o")]
13
#[doc(alias = "org.freedesktop.Secret.Prompt")]
14
pub struct Prompt(zbus::Proxy<'static>);
15

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

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

            
30
impl Prompt {
31
2
    pub async fn new<P>(
32
        connection: &zbus::Connection,
33
        object_path: P,
34
    ) -> Result<Option<Self>, Error>
35
    where
36
        P: TryInto<ObjectPath<'static>>,
37
        P::Error: Into<zbus::Error>,
38
    {
39
4
        let path = object_path.try_into().map_err(Into::into)?;
40
6
        if path != ObjectPath::default() {
41
2
            Ok(Some(
42
10
                zbus::proxy::Builder::new(connection)
43
2
                    .path(path)?
44
2
                    .build()
45
6
                    .await?,
46
            ))
47
        } else {
48
2
            Ok(None)
49
        }
50
    }
51

            
52
2
    pub fn inner(&self) -> &zbus::Proxy<'static> {
53
        &self.0
54
    }
55

            
56
8
    pub async fn prompt(&self, window_id: Option<WindowIdentifier>) -> Result<(), Error> {
57
2
        let id = match window_id {
58
            Some(id) => id.to_string(),
59
4
            None => Default::default(),
60
        };
61
10
        self.inner()
62
2
            .call_method("Prompt", &(id))
63
8
            .await
64
6
            .map_err::<ServiceError, _>(From::from)?;
65
2
        Ok(())
66
    }
67

            
68
8
    pub async fn dismiss(&self) -> Result<(), Error> {
69
8
        self.inner()
70
2
            .call_method("Dismiss", &())
71
8
            .await
72
4
            .map_err::<ServiceError, _>(From::from)?;
73
2
        Ok(())
74
    }
75

            
76
2
    pub async fn receive_completed(
77
        &self,
78
        window_id: Option<WindowIdentifier>,
79
    ) -> Result<OwnedValue, Error> {
80
6
        let mut stream = self.inner().receive_signal("Completed").await?;
81
12
        let (value, _) = futures_util::try_join!(
82
6
            async {
83
8
                let message = stream.next().await.unwrap();
84
4
                let (dismissed, result) = message.body().deserialize::<(bool, OwnedValue)>()?;
85
4
                if dismissed {
86
2
                    Err(Error::Dismissed)
87
                } else {
88
2
                    Ok(result)
89
                }
90
            },
91
2
            self.prompt(window_id)
92
        )?;
93
2
        Ok(value)
94
    }
95
}
96

            
97
impl Serialize for Prompt {
98
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
99
    where
100
        S: serde::Serializer,
101
    {
102
        ObjectPath::serialize(self.inner().path(), serializer)
103
    }
104
}
105

            
106
impl fmt::Debug for Prompt {
107
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108
        f.debug_tuple("Prompt")
109
            .field(&self.inner().path().as_str())
110
            .finish()
111
    }
112
}