1use std::fmt;
2
3use zbus::Connection;
4
5use crate::dbus::{
6 sdk_manager_interface::SdkManagerInterfaceProxy,
7 sdk_pact_interface::{SdkPactInterfaceProxy, SessionInfo},
8};
9
10#[derive(Debug, Clone, PartialEq)]
11pub enum ManagerError {
12 DbusConnectionFailed(zbus::Error),
13 DbusMessageFailed(zbus::fdo::Error),
14}
15
16impl fmt::Display for ManagerError {
17 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18 match self {
19 ManagerError::DbusConnectionFailed(e) => write!(f, "D-Bus connection failed: {e}"),
20 ManagerError::DbusMessageFailed(e) => write!(f, "D-Bus message failed: {e}"),
21 }
22 }
23}
24
25pub struct Manager {
26 connection: Connection,
27}
28
29impl Manager {
30 pub async fn new() -> Result<Self, ManagerError> {
31 let connection = Connection::session()
32 .await
33 .map_err(ManagerError::DbusConnectionFailed)?;
34
35 Ok(Manager { connection })
36 }
37
38 pub async fn is_playtron(&self) -> Result<bool, ManagerError> {
39 let proxy = self.get_manager_proxy().await?;
40 proxy
41 .is_playtron()
42 .await
43 .map_err(ManagerError::DbusMessageFailed)
44 }
45
46 pub async fn is_keyboard_open(&self) -> Result<bool, ManagerError> {
47 let proxy = self.get_manager_proxy().await?;
48 proxy
49 .is_keyboard_open()
50 .await
51 .map_err(ManagerError::DbusMessageFailed)
52 }
53
54 pub async fn show_keyboard(&self) -> Result<(), ManagerError> {
55 let proxy = self.get_manager_proxy().await?;
56 proxy
57 .show_keyboard()
58 .await
59 .map_err(ManagerError::DbusMessageFailed)
60 }
61
62 pub async fn hide_keyboard(&self) -> Result<(), ManagerError> {
63 let proxy = self.get_manager_proxy().await?;
64 proxy
65 .hide_keyboard()
66 .await
67 .map_err(ManagerError::DbusMessageFailed)
68 }
69
70 pub async fn open_browser(&self, url: String) -> Result<(), ManagerError> {
71 let proxy = self.get_manager_proxy().await?;
72 proxy
73 .open_browser(url)
74 .await
75 .map_err(ManagerError::DbusMessageFailed)
76 }
77
78 pub async fn close_browser(&self) -> Result<(), ManagerError> {
79 let proxy = self.get_manager_proxy().await?;
80 proxy
81 .close_browser()
82 .await
83 .map_err(ManagerError::DbusMessageFailed)
84 }
85
86 async fn get_manager_proxy(&self) -> Result<SdkManagerInterfaceProxy<'_>, ManagerError> {
87 SdkManagerInterfaceProxy::builder(&self.connection)
88 .build()
89 .await
90 .map_err(ManagerError::DbusConnectionFailed)
91 }
92
93 pub async fn create_pact_session(&self) -> Result<SessionInfo, ManagerError> {
94 let proxy = self.get_pact_proxy().await?;
95 proxy
96 .create_session()
97 .await
98 .map_err(ManagerError::DbusMessageFailed)
99 }
100
101 pub async fn get_pact_quote(&self, nonce: Vec<u8>) -> Result<String, ManagerError> {
102 let proxy = self.get_pact_proxy().await?;
103 proxy
104 .get_quote(nonce)
105 .await
106 .map_err(ManagerError::DbusMessageFailed)
107 }
108
109 async fn get_pact_proxy(&self) -> Result<SdkPactInterfaceProxy<'_>, ManagerError> {
110 SdkPactInterfaceProxy::builder(&self.connection)
111 .build()
112 .await
113 .map_err(ManagerError::DbusConnectionFailed)
114 }
115}