wayle-notification 0.1.4

Desktop notification service with popup management
Documentation
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use std::{fmt, str::FromStr};

/// The urgency level of a notification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum Urgency {
    /// Low urgency. Server implementations may display the notification how they choose.
    Low = 0,
    /// Normal urgency. Server implementations may display the notification how they choose.
    Normal = 1,
    /// Critical urgency. Critical notifications do not automatically expire, as they are
    /// important for the user to see. They are closed only when the user dismisses them,
    /// for example, by clicking on the notification.
    Critical = 2,
}

impl From<u8> for Urgency {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::Low,
            2 => Self::Critical,
            _ => Self::Normal,
        }
    }
}

/// The reason a notification was closed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum ClosedReason {
    /// The notification expired.
    Expired = 1,
    /// The notification was dismissed by the user.
    DismissedByUser = 2,
    /// The notification was closed by a call to CloseNotification.
    Closed = 3,
    /// Undefined/reserved reasons.
    Unknown = 4,
}

impl From<u32> for ClosedReason {
    fn from(value: u32) -> Self {
        match value {
            1 => Self::Expired,
            2 => Self::DismissedByUser,
            3 => Self::Closed,
            _ => Self::Unknown,
        }
    }
}

/// Server capabilities as defined in the specification.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Capabilities {
    /// Supports using icons instead of text for displaying actions.
    ///
    /// Icon usage requires per-notification activation via the "action-icons" hint.
    ActionIcons,
    /// The server will provide the specified actions to the user. Even if this cap is missing,
    /// actions may still be specified by the client, however the server is free to ignore them.
    Actions,
    /// Supports body text.
    ///
    /// Some implementations may only show the summary (for instance, onscreen displays,
    /// marquee/scrollers).
    Body,
    /// The server supports hyperlinks in the notifications.
    BodyHyperlinks,
    /// The server supports images in the notifications.
    BodyImages,
    /// Supports markup in the body text.
    ///
    /// If marked up text is sent to a server that does not provide this capability, the markup
    /// appears as regular text and requires client-side stripping.
    BodyMarkup,
    /// Indicates the server renders animations from all frames in a given image array.
    ///
    /// Clients may specify multiple frames even if this capability and/or "icon-static"
    /// is missing, though the server may ignore them and use only the primary frame.
    IconMulti,
    /// Supports display of exactly 1 frame of any given image array.
    ///
    /// This capability is mutually exclusive with "icon-multi"; specifying both is a
    /// protocol error.
    IconStatic,
    /// Indicates the server supports persistence of notifications.
    ///
    /// Notifications are retained until acknowledged or removed by the user, or recalled
    /// by the sender. This capability allows clients to rely on the server to ensure a
    /// notification is seen, eliminating the need for client-side reminding functions
    /// (such as status icons).
    Persistence,
    /// Indicates the server supports sounds on notifications.
    ///
    /// When present, the server also supports the "sound-file" and "suppress-sound" hints.
    Sound,
    /// Vendor-specific capability.
    Vendor(String),
}

impl FromStr for Capabilities {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "action-icons" => Self::ActionIcons,
            "actions" => Self::Actions,
            "body" => Self::Body,
            "body-hyperlinks" => Self::BodyHyperlinks,
            "body-images" => Self::BodyImages,
            "body-markup" => Self::BodyMarkup,
            "icon-multi" => Self::IconMulti,
            "icon-static" => Self::IconStatic,
            "persistence" => Self::Persistence,
            "sound" => Self::Sound,
            s if s.starts_with("x-") => Self::Vendor(s.to_string()),
            _ => Self::Vendor(format!("x-unknown-{s}")),
        })
    }
}

impl Capabilities {
    /// Convert to string representation for D-Bus.
    pub fn as_str(&self) -> &str {
        match self {
            Self::ActionIcons => "action-icons",
            Self::Actions => "actions",
            Self::Body => "body",
            Self::BodyHyperlinks => "body-hyperlinks",
            Self::BodyImages => "body-images",
            Self::BodyMarkup => "body-markup",
            Self::IconMulti => "icon-multi",
            Self::IconStatic => "icon-static",
            Self::Persistence => "persistence",
            Self::Sound => "sound",
            Self::Vendor(s) => s,
        }
    }
}

impl fmt::Display for Capabilities {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Standard notification categories.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Category {
    /// A generic audio or video call notification that doesn't fit into any other category.
    Call,
    /// An audio or video call was ended.
    CallEnded,
    /// A audio or video call is incoming.
    CallIncoming,
    /// An incoming audio or video call was not answered.
    CallUnanswered,
    /// A generic device-related notification that doesn't fit into any other category.
    Device,
    /// A device, such as a USB device, was added to the system.
    DeviceAdded,
    /// A device had some kind of error.
    DeviceError,
    /// A device, such as a USB device, was removed from the system.
    DeviceRemoved,
    /// A generic e-mail-related notification that doesn't fit into any other category.
    Email,
    /// A new e-mail notification.
    EmailArrived,
    /// A notification stating that an e-mail has bounced.
    EmailBounced,
    /// A generic instant message-related notification that doesn't fit into any other category.
    Im,
    /// An instant message error notification.
    ImError,
    /// A received instant message notification.
    ImReceived,
    /// A generic network notification that doesn't fit into any other category.
    Network,
    /// A network connection notification, such as successful sign-on to a network service.
    ///
    /// Distinct from `device.added` for new network devices.
    NetworkConnected,
    /// A network disconnected notification.
    ///
    /// Distinct from `device.removed` for disconnected network devices.
    NetworkDisconnected,
    /// A network-related or connection-related error.
    NetworkError,
    /// A generic presence change notification that doesn't fit into any other category,
    /// such as going away or idle.
    Presence,
    /// An offline presence change notification.
    PresenceOffline,
    /// An online presence change notification.
    PresenceOnline,
    /// A generic file transfer or download notification that doesn't fit into any other category.
    Transfer,
    /// A file transfer or download complete notification.
    TransferComplete,
    /// A file transfer or download error.
    TransferError,
    /// Vendor-specific category.
    Vendor(String),
}

impl FromStr for Category {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "call" => Self::Call,
            "call.ended" => Self::CallEnded,
            "call.incoming" => Self::CallIncoming,
            "call.unanswered" => Self::CallUnanswered,
            "device" => Self::Device,
            "device.added" => Self::DeviceAdded,
            "device.error" => Self::DeviceError,
            "device.removed" => Self::DeviceRemoved,
            "email" => Self::Email,
            "email.arrived" => Self::EmailArrived,
            "email.bounced" => Self::EmailBounced,
            "im" => Self::Im,
            "im.error" => Self::ImError,
            "im.received" => Self::ImReceived,
            "network" => Self::Network,
            "network.connected" => Self::NetworkConnected,
            "network.disconnected" => Self::NetworkDisconnected,
            "network.error" => Self::NetworkError,
            "presence" => Self::Presence,
            "presence.offline" => Self::PresenceOffline,
            "presence.online" => Self::PresenceOnline,
            "transfer" => Self::Transfer,
            "transfer.complete" => Self::TransferComplete,
            "transfer.error" => Self::TransferError,
            s if s.starts_with("x-") => Self::Vendor(s.to_string()),
            _ => Self::Vendor(format!("x-unknown-{s}")),
        })
    }
}

impl Category {
    /// Convert to string representation for hints.
    pub fn as_str(&self) -> &str {
        match self {
            Self::Call => "call",
            Self::CallEnded => "call.ended",
            Self::CallIncoming => "call.incoming",
            Self::CallUnanswered => "call.unanswered",
            Self::Device => "device",
            Self::DeviceAdded => "device.added",
            Self::DeviceError => "device.error",
            Self::DeviceRemoved => "device.removed",
            Self::Email => "email",
            Self::EmailArrived => "email.arrived",
            Self::EmailBounced => "email.bounced",
            Self::Im => "im",
            Self::ImError => "im.error",
            Self::ImReceived => "im.received",
            Self::Network => "network",
            Self::NetworkConnected => "network.connected",
            Self::NetworkDisconnected => "network.disconnected",
            Self::NetworkError => "network.error",
            Self::Presence => "presence",
            Self::PresenceOffline => "presence.offline",
            Self::PresenceOnline => "presence.online",
            Self::Transfer => "transfer",
            Self::TransferComplete => "transfer.complete",
            Self::TransferError => "transfer.error",
            Self::Vendor(s) => s,
        }
    }
}

/// An action that can be invoked on a notification.
///
/// Actions are sent over as a list of pairs. Each even element in the list
/// (starting at index 0) represents the identifier for the action. Each odd
/// element in the list is the localized string that will be displayed to the user.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Action {
    /// The identifier for the action. The default action (usually invoked by clicking
    /// the notification) should have a key named "default".
    pub id: String,
    /// The localized string that will be displayed to the user.
    pub label: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn urgency_from_u8_with_zero_returns_low() {
        let result = Urgency::from(0);

        assert_eq!(result, Urgency::Low);
    }

    #[test]
    fn urgency_from_u8_with_two_returns_critical() {
        let result = Urgency::from(2);

        assert_eq!(result, Urgency::Critical);
    }

    #[test]
    fn urgency_from_u8_with_one_returns_normal() {
        let result = Urgency::from(1);

        assert_eq!(result, Urgency::Normal);
    }

    #[test]
    fn urgency_from_u8_with_five_returns_normal() {
        let result = Urgency::from(5);

        assert_eq!(result, Urgency::Normal);
    }

    #[test]
    fn closed_reason_from_u32_with_one_returns_expired() {
        let result = ClosedReason::from(1);

        assert_eq!(result, ClosedReason::Expired);
    }

    #[test]
    fn closed_reason_from_u32_with_two_returns_dismissed_by_user() {
        let result = ClosedReason::from(2);

        assert_eq!(result, ClosedReason::DismissedByUser);
    }

    #[test]
    fn closed_reason_from_u32_with_three_returns_closed() {
        let result = ClosedReason::from(3);

        assert_eq!(result, ClosedReason::Closed);
    }

    #[test]
    fn closed_reason_from_u32_with_zero_returns_unknown() {
        let result = ClosedReason::from(0);

        assert_eq!(result, ClosedReason::Unknown);
    }

    #[test]
    fn closed_reason_from_u32_with_five_returns_unknown() {
        let result = ClosedReason::from(5);

        assert_eq!(result, ClosedReason::Unknown);
    }

    #[test]
    fn capabilities_from_str_with_action_icons_returns_correct_variant() {
        let result = "action-icons".parse::<Capabilities>().unwrap();

        assert_eq!(result, Capabilities::ActionIcons);
    }

    #[test]
    fn capabilities_from_str_with_actions_returns_correct_variant() {
        let result = "actions".parse::<Capabilities>().unwrap();

        assert_eq!(result, Capabilities::Actions);
    }

    #[test]
    fn capabilities_from_str_with_persistence_returns_correct_variant() {
        let result = "persistence".parse::<Capabilities>().unwrap();

        assert_eq!(result, Capabilities::Persistence);
    }

    #[test]
    fn capabilities_from_str_with_vendor_prefix_returns_vendor() {
        let result = "x-custom-cap".parse::<Capabilities>().unwrap();

        assert_eq!(result, Capabilities::Vendor("x-custom-cap".to_string()));
    }

    #[test]
    fn capabilities_from_str_with_unknown_wraps_in_vendor_format() {
        let result = "unknown-capability".parse::<Capabilities>().unwrap();

        assert_eq!(
            result,
            Capabilities::Vendor("x-unknown-unknown-capability".to_string())
        );
    }

    #[test]
    fn capabilities_as_str_for_action_icons_returns_correct_string() {
        let cap = Capabilities::ActionIcons;

        let result = cap.as_str();

        assert_eq!(result, "action-icons");
    }

    #[test]
    fn capabilities_as_str_for_persistence_returns_correct_string() {
        let cap = Capabilities::Persistence;

        let result = cap.as_str();

        assert_eq!(result, "persistence");
    }

    #[test]
    fn capabilities_as_str_with_vendor_returns_inner_string() {
        let cap = Capabilities::Vendor("x-custom".to_string());

        let result = cap.as_str();

        assert_eq!(result, "x-custom");
    }

    #[test]
    fn category_from_str_with_call_returns_correct_variant() {
        let result = "call".parse::<Category>().unwrap();

        assert_eq!(result, Category::Call);
    }

    #[test]
    fn category_from_str_with_email_arrived_returns_correct_variant() {
        let result = "email.arrived".parse::<Category>().unwrap();

        assert_eq!(result, Category::EmailArrived);
    }

    #[test]
    fn category_from_str_with_network_error_returns_correct_variant() {
        let result = "network.error".parse::<Category>().unwrap();

        assert_eq!(result, Category::NetworkError);
    }

    #[test]
    fn category_from_str_with_vendor_prefix_returns_vendor() {
        let result = "x-custom-category".parse::<Category>().unwrap();

        assert_eq!(result, Category::Vendor("x-custom-category".to_string()));
    }

    #[test]
    fn category_from_str_with_unknown_wraps_in_vendor_format() {
        let result = "unknown-category".parse::<Category>().unwrap();

        assert_eq!(
            result,
            Category::Vendor("x-unknown-unknown-category".to_string())
        );
    }

    #[test]
    fn category_as_str_for_call_returns_correct_string() {
        let cat = Category::Call;

        let result = cat.as_str();

        assert_eq!(result, "call");
    }

    #[test]
    fn category_as_str_for_email_arrived_returns_correct_string() {
        let cat = Category::EmailArrived;

        let result = cat.as_str();

        assert_eq!(result, "email.arrived");
    }

    #[test]
    fn category_as_str_with_vendor_returns_inner_string() {
        let cat = Category::Vendor("x-custom".to_string());

        let result = cat.as_str();

        assert_eq!(result, "x-custom");
    }
}