zbus_notification 0.3.6

zbus binding for org.freedesktop.Notification
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
//! # D-Bus interface proxy for: `org.freedesktop.Notifications`
//!
//! This code was generated by `zbus-xmlgen` `4.1.0` from D-Bus introspection data.
//! Source: `Interface '/org/freedesktop/Notifications' from service 'org.freedesktop.Notifications' on system bus`.
//!
//! Please read the document of [notification-spec](https://specifications.freedesktop.org/notification-spec)

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use glob::glob;
use serde::{Deserialize, Serialize};
use zbus::{interface, object_server::SignalContext, zvariant::OwnedValue};

use std::sync::{Arc, LazyLock, RwLock};

use futures::channel::mpsc::Sender;
use zbus::ConnectionBuilder;

use zbus::zvariant::{SerializeDict, Type};

/// The notification expired
pub const NOTIFICATION_DELETED_BY_EXPIRED: u32 = 1;
/// The notification was dismissed by the user.
pub const NOTIFICATION_DELETED_BY_USER: u32 = 2;

/// The notification was closed by a call to CloseNotification.
pub const NOTIFICATION_CLOSED_BY_DBUS: u32 = 3;

/// Undefined/reserved reasons.
pub const NOTIFICATION_CLOSED_BY_UNKNOWN_REASON: u32 = 4;

static ICON_CACHE: LazyLock<Arc<RwLock<HashMap<String, ImageInfo>>>> =
    LazyLock::new(|| Arc::new(RwLock::new(HashMap::new())));

use std::hash::Hash;

use std::sync::atomic::{self, AtomicU32};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
/// The id of the window.
///
/// Internally Iced reserves `window::Id::MAIN` for the first window spawned.
struct Id(u32);

static COUNT: AtomicU32 = AtomicU32::new(0);

impl Id {
    fn unique() -> Id {
        Id(COUNT.fetch_add(1, atomic::Ordering::Relaxed))
    }
}

/// Describe the image information.
#[derive(Type, Debug, SerializeDict, OwnedValue, Clone)]
struct ImageData {
    width: i32,
    height: i32,
    rowstride: i32,
    has_alpha: bool,
    bits_per_sample: i32,
    channels: i32,
    data: Vec<u8>,
}

/// NotifyMessage about the add and remove
#[derive(Debug, Clone)]
pub enum NotifyMessage {
    UnitAdd(Box<NotifyUnit>),
    UnitRemove(u32),
}

fn lazy_get_icon(icon: &str) -> Option<ImageInfo> {
    let icon_cache = ICON_CACHE.read().unwrap();
    if icon_cache.contains_key(icon) {
        return icon_cache.get(icon).cloned();
    }
    drop(icon_cache);
    let mut icon_cache = ICON_CACHE.write().unwrap();
    if let Some(path) = get_svg_icon("hicolor", icon) {
        icon_cache.insert(icon.to_string(), ImageInfo::Svg(path.clone()));
        return Some(ImageInfo::Svg(path));
    }
    if let Some(path) = get_png_icon("hicolor", icon) {
        icon_cache.insert(icon.to_string(), ImageInfo::Png(path.clone()));
        return Some(ImageInfo::Png(path));
    }
    if let Some(path) = get_jpeg_icon("hicolor", icon) {
        icon_cache.insert(icon.to_string(), ImageInfo::Jpg(path.clone()));
        return Some(ImageInfo::Jpg(path));
    }
    if let Some(path) = get_pixmap_icon(icon) {
        icon_cache.insert(icon.to_string(), ImageInfo::Png(path.clone()));
        return Some(ImageInfo::Png(path));
    }
    None
}

fn get_svg_icon(theme: &str, icon: &str) -> Option<PathBuf> {
    glob(&format!("/usr/share/icons/{theme}/**/**/{icon}.svg"))
        .ok()?
        .next()?
        .ok()
}

fn get_png_icon(theme: &str, icon: &str) -> Option<PathBuf> {
    glob(&format!("/usr/share/icons/{theme}/**/**/{icon}.png"))
        .ok()?
        .next()?
        .ok()
}

fn get_pixmap_icon(icon: &str) -> Option<PathBuf> {
    let path = Path::new(format!("/usr/share/pixmaps/{icon}.png").as_str()).to_owned();
    if path.exists() {
        Some(path)
    } else {
        None
    }
}

fn get_jpeg_icon(theme: &str, icon: &str) -> Option<PathBuf> {
    glob(&format!("/usr/share/icons/{theme}/**/**/{icon}.jpg"))
        .ok()?
        .next()?
        .ok()
}

/// storage the hint of notification
#[derive(Debug, Clone)]
pub struct NotifyHint {
    image_data: Option<ImageData>,
    desktop_entry: Option<String>,
    urgency: Urgency,
}

#[derive(Deserialize, Serialize, Type, Debug, Clone, OwnedValue)]
#[repr(u8)]
pub enum Urgency {
    Low = 0,
    Normal = 1,
    Critical = 2,
}

/// contain the info about image
#[derive(Debug, Clone)]
pub enum ImageInfo {
    /// raw data of image
    Data {
        width: i32,
        height: i32,
        pixels: Vec<u8>,
    },
    /// svg path
    Svg(PathBuf),
    /// png path
    Png(PathBuf),
    /// jpeg path
    Jpg(PathBuf),
}

impl NotifyHint {
    fn desktop_image(&self) -> Option<ImageInfo> {
        self.desktop_entry
            .as_ref()
            .and_then(|icon| lazy_get_icon(icon))
    }
    fn hint_image(&self) -> Option<ImageInfo> {
        self.image_data.as_ref().map(|data| ImageInfo::Data {
            width: data.width,
            height: data.height,
            pixels: data.data.clone(),
        })
    }

    pub fn is_critical(&self) -> bool {
        matches!(self.urgency, Urgency::Critical)
    }
}

/// Describe the information in every time notify send
#[derive(Debug, Clone)]
pub struct NotifyUnit {
    /// application from
    pub app_name: String,
    /// The id of notify
    pub id: u32,
    /// The icon
    pub icon: String,
    /// summery of a notify
    pub summery: String,
    /// the body of notify, use rich text
    pub body: String,
    /// supported actions
    pub actions: Vec<String>,
    /// timeout, if equal to -1, then means it should been always shown
    pub timeout: i32,
    /// other information like image-data
    pub hint: NotifyHint,
}

impl NotifyUnit {
    /// if this notify unit support inline_reply
    pub fn inline_reply_support(&self) -> bool {
        self.actions.contains(&"inline-reply".to_owned())
    }

    /// Get the image inside the unit
    /// It will use the image in hint first
    /// Then use icon from the param by notify
    /// Finally use the application icon
    pub fn image(&self) -> Option<ImageInfo> {
        if let Some(hint_image) = self.hint.hint_image() {
            return Some(hint_image);
        }
        if !self.icon.is_empty() {
            let path = Path::new(&self.icon);
            if path.exists() {
                if self.icon.ends_with("svg") {
                    return Some(ImageInfo::Svg(path.into()));
                } else if self.icon.ends_with("jpg") {
                    return Some(ImageInfo::Jpg(path.into()));
                } else {
                    return Some(ImageInfo::Png(path.into()));
                }
            }
            if let Some(info) = lazy_get_icon(&self.icon) {
                return Some(info);
            }
        }
        self.hint.desktop_image()
    }

    pub fn is_critical(&self) -> bool {
        self.hint.is_critical()
    }
}

/// Set the server info in `get_server_information`
#[derive(Debug, Clone)]
pub struct VersionInfo {
    pub name: String,
    pub vendor: String,
    pub version: String,
    pub spec_version: String,
}

/// Do not care this name. it is the Interface name of `org.freedesktop.Notifications`
#[derive(Debug)]
pub struct LaLaMako<T: From<NotifyMessage> + Send> {
    capabilities: Vec<String>,
    sender: Sender<T>,
    version: VersionInfo,
}

#[interface(name = "org.freedesktop.Notifications")]
impl<T: From<NotifyMessage> + Send + 'static> LaLaMako<T> {
    // CloseNotification method
    async fn close_notification(
        &mut self,
        #[zbus(signal_context)] ctx: SignalContext<'_>,
        id: u32,
    ) -> zbus::fdo::Result<()> {
        Self::notification_closed(&ctx, id, NOTIFICATION_DELETED_BY_USER)
            .await
            .ok();
        self.sender
            .try_send(NotifyMessage::UnitRemove(id).into())
            .ok();
        Ok(())
    }

    /// GetCapabilities method
    fn get_capabilities(&self) -> Vec<String> {
        self.capabilities.clone()
    }

    /// GetServerInformation method
    fn get_server_information(&self) -> (String, String, String, String) {
        let VersionInfo {
            name,
            vendor,
            version,
            spec_version,
        } = &self.version;
        (
            name.clone(),
            vendor.clone(),
            version.clone(),
            spec_version.clone(),
        )
    }

    // Notify method
    #[allow(clippy::too_many_arguments)]
    fn notify(
        &mut self,
        app_name: &str,
        replaced_id: u32,
        icon: &str,
        summery: &str,
        body: &str,
        actions: Vec<&str>,
        mut hints: std::collections::HashMap<&str, OwnedValue>,
        timeout: i32,
    ) -> zbus::fdo::Result<u32> {
        let id = if replaced_id == 0 {
            Id::unique()
        } else {
            Id(replaced_id)
        };
        let mut image_data: Option<ImageData> =
            hints.remove("image-data").and_then(|v| v.try_into().ok());
        if image_data.is_none() {
            // why send data here...
            image_data = hints.remove("icon_data").and_then(|v| v.try_into().ok());
        }
        let desktop_entry: Option<String> = hints
            .remove("desktop-entry")
            .and_then(|v| v.try_into().ok());

        let urgency = hints
            .remove("urgency")
            .and_then(|v| v.try_into().ok())
            .unwrap_or(Urgency::Low);

        self.sender
            .try_send(
                NotifyMessage::UnitAdd(Box::new(NotifyUnit {
                    app_name: app_name.to_string(),
                    id: id.0,
                    icon: icon.to_string(),
                    summery: summery.to_string(),
                    body: body.to_string(),
                    actions: actions.iter().map(|a| a.to_string()).collect(),
                    timeout,
                    hint: NotifyHint {
                        image_data,
                        desktop_entry,
                        urgency,
                    },
                }))
                .into(),
            )
            .ok();
        Ok(id.0)
    }

    /// Invoke Action
    #[zbus(signal)]
    pub async fn action_invoked(
        ctx: &SignalContext<'_>,
        id: u32,
        action_key: &str,
    ) -> zbus::Result<()>;

    /// Notification Reply
    #[zbus(signal)]
    pub async fn notification_replied(
        ctx: &SignalContext<'_>,
        id: u32,
        text: &str,
    ) -> zbus::Result<()>;

    /// NotificationClosed signal
    #[zbus(signal)]
    pub async fn notification_closed(
        ctx: &SignalContext<'_>,
        id: u32,
        reason: u32,
    ) -> zbus::Result<()>;
}

/// org.freedesktop.Notifications server path
pub const NOTIFICATION_SERVICE_PATH: &str = "/org/freedesktop/Notifications";
/// org.freedesktop.Notifications server name
pub const NOTIFICATION_SERVICE_NAME: &str = "/org/freedesktop/Notifications";
/// org.freedesktop.Notifications interface name
pub const NOTIFICATION_SERVICE_INTERFACE: &str = "/org/freedesktop/Notifications";

/// action_invoked signal
pub const ACTION_INVOKED: &str = "action_invoked";

/// notification_closed signal
pub const NOTIFICATION_CLOSED: &str = "notification_closed";

/// default action name
pub const DEFAULT_ACTION: &str = "default";

/// start a connection
pub async fn start_connection<T: From<NotifyMessage> + Send + 'static>(
    sender: Sender<T>,
    capabilities: Vec<String>,
    version: VersionInfo,
) -> Result<zbus::Connection, zbus::Error> {
    ConnectionBuilder::session()?
        .name("org.freedesktop.Notifications")?
        .serve_at(
            "/org/freedesktop/Notifications",
            LaLaMako {
                sender,
                capabilities,
                version,
            },
        )?
        .build()
        .await
}