logind_zbus/manager/
types.rs

1use serde::{Deserialize, Serialize};
2use std::str::FromStr;
3use zbus::fdo;
4use zbus::zvariant::{OwnedObjectPath, OwnedValue, Structure, Type};
5
6use crate::{enum_impl_serde_str, enum_impl_str_conv};
7
8/// Basic user information
9#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize)]
10pub struct UserInfo {
11    /// User ID
12    uid: u32,
13    /// User name
14    name: String,
15    /// DBUS path to this user
16    path: OwnedObjectPath,
17}
18
19impl UserInfo {
20    pub fn uid(&self) -> u32 {
21        self.uid
22    }
23
24    pub fn name(&self) -> &str {
25        &self.name
26    }
27
28    pub fn path(&self) -> &OwnedObjectPath {
29        &self.path
30    }
31}
32
33#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize)]
34pub struct ScheduledShutdown {
35    /// Name of the shutdown
36    id: String,
37    /// Time in micros
38    time: u64,
39}
40
41impl ScheduledShutdown {
42    pub fn id(&self) -> &str {
43        &self.id
44    }
45
46    pub fn time(&self) -> u64 {
47        self.time
48    }
49}
50
51impl TryFrom<OwnedValue> for ScheduledShutdown {
52    type Error = zbus::Error;
53
54    fn try_from(value: OwnedValue) -> Result<Self, Self::Error> {
55        let value = <Structure<'_>>::try_from(value)?;
56        Ok(Self {
57            id: <String>::try_from(value.fields()[0].try_clone()?)?,
58            time: <u64>::try_from(value.fields()[1].try_clone()?)?,
59        })
60    }
61}
62
63#[derive(Debug, PartialEq, Eq, Clone, Copy, Type)]
64#[zvariant(signature = "s")]
65pub enum IsSupported {
66    NA,
67    Yes,
68    No,
69    Challenge,
70}
71enum_impl_serde_str!(IsSupported);
72enum_impl_str_conv!(IsSupported, {
73    "na": NA,
74    "yes": Yes,
75    "no": No,
76    "challenge": Challenge,
77});
78
79#[derive(Debug, PartialEq, Eq, Clone, Type)]
80#[zvariant(signature = "s")]
81pub struct InhibitTypes(Vec<InhibitType>);
82
83impl InhibitTypes {
84    pub fn new(inhibit_types: &[InhibitType]) -> InhibitTypes {
85        Self(inhibit_types.to_vec())
86    }
87
88    pub fn types(&self) -> &Vec<InhibitType> {
89        &self.0
90    }
91}
92
93impl FromStr for InhibitTypes {
94    type Err = fdo::Error;
95
96    fn from_str(s: &str) -> Result<Self, Self::Err> {
97        let mut buf = Vec::new();
98        for chunk in s.split(':') {
99            buf.push(InhibitType::from_str(chunk)?);
100        }
101        Ok(Self(buf))
102    }
103}
104
105impl From<InhibitTypes> for String {
106    fn from(s: InhibitTypes) -> Self {
107        let mut string = String::new();
108        for (i, inhibit) in s.0.iter().enumerate() {
109            if i > 0 && i < s.0.len() {
110                string.push(':');
111            }
112            string.push_str((*inhibit).into());
113        }
114        string
115    }
116}
117
118impl From<&InhibitTypes> for String {
119    fn from(s: &InhibitTypes) -> Self {
120        let mut string = String::new();
121        for (i, inhibit) in s.0.iter().enumerate() {
122            if i > 0 && i < s.0.len() {
123                string.push(':');
124            }
125            string.push_str((*inhibit).into());
126        }
127        string
128    }
129}
130
131impl Serialize for InhibitTypes {
132    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
133    where
134        S: serde::Serializer,
135    {
136        serializer.serialize_str(String::from(self).as_str())
137    }
138}
139
140impl<'de> Deserialize<'de> for InhibitTypes {
141    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
142    where
143        D: serde::Deserializer<'de>,
144    {
145        let s = String::deserialize(deserializer)?;
146        InhibitTypes::from_str(s.as_str()).map_err(serde::de::Error::custom)
147    }
148}
149
150#[derive(Debug, PartialEq, Eq, Copy, Clone, Type)]
151#[zvariant(signature = "s")]
152pub enum InhibitType {
153    Shutdown,
154    Sleep,
155    Idle,
156    HandlePowerKey,
157    HandleSuspendKey,
158    HandleHibernateKey,
159    HandleLidSwitch,
160}
161enum_impl_serde_str!(InhibitType);
162enum_impl_str_conv!(InhibitType, {
163    "shutdown": Shutdown,
164    "sleep": Sleep,
165    "idle": Idle,
166    "handle-power-key": HandlePowerKey,
167    "handle-suspend-key": HandleSuspendKey,
168    "handle-hibernate-key": HandleHibernateKey,
169    "handle-lid-switch": HandleLidSwitch,
170});
171
172#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize)]
173pub struct Inhibitor {
174    /// What this lock is inhibiting
175    what: InhibitTypes,
176    /// The name or ID of what is inhibiting, for example the applicaiton name creating this lock
177    who: String,
178    /// A description of why the lock was created
179    why: String,
180    /// The lock behaviour
181    mode: Mode,
182    user_id: u32,
183    process_id: u32,
184}
185
186impl Inhibitor {
187    pub fn new(
188        what: InhibitTypes,
189        who: String,
190        why: String,
191        mode: Mode,
192        user_id: u32,
193        process_id: u32,
194    ) -> Inhibitor {
195        Inhibitor {
196            what,
197            who,
198            why,
199            mode,
200            user_id,
201            process_id,
202        }
203    }
204
205    pub fn what(&self) -> &InhibitTypes {
206        &self.what
207    }
208
209    pub fn who(&self) -> &str {
210        &self.who
211    }
212
213    pub fn why(&self) -> &str {
214        &self.why
215    }
216
217    pub fn mode(&self) -> Mode {
218        self.mode
219    }
220
221    pub fn user_id(&self) -> u32 {
222        self.user_id
223    }
224
225    pub fn process_id(&self) -> u32 {
226        self.process_id
227    }
228}
229
230/// Used to determine behaviour of inhibitors
231#[derive(Debug, PartialEq, Eq, Copy, Clone, Type)]
232#[zvariant(signature = "s")]
233pub enum Mode {
234    /// Inhibitor is mandatory
235    Block,
236    /// Inhibitor delays to a certain time
237    Delay,
238}
239enum_impl_serde_str!(Mode);
240enum_impl_str_conv!(Mode, {
241    "block": Block,
242    "delay": Delay,
243});
244
245#[derive(Debug, PartialEq, Eq, Type, Serialize, Deserialize)]
246pub struct SessionInfo {
247    /// Session ID
248    sid: String,
249    /// User ID
250    uid: u32,
251    /// Name of session user
252    user: String,
253    /// The session seat label
254    seat: String,
255    /// DBUS path for this session
256    path: OwnedObjectPath,
257}
258
259impl SessionInfo {
260    pub fn sid(&self) -> &str {
261        &self.sid
262    }
263
264    pub fn uid(&self) -> u32 {
265        self.uid
266    }
267
268    pub fn user(&self) -> &str {
269        &self.user
270    }
271
272    pub fn seat(&self) -> &str {
273        &self.seat
274    }
275
276    pub fn path(&self) -> &OwnedObjectPath {
277        &self.path
278    }
279}