| 1 |
//! # Examples |
| 2 |
//! |
| 3 |
//! ```rust,no_run |
| 4 |
//! use std::{thread, time}; |
| 5 |
//! |
| 6 |
//! use ashpd::desktop::{ |
| 7 |
//! Icon, |
| 8 |
//! notification::{Action, Button, Notification, NotificationProxy, Priority}, |
| 9 |
//! }; |
| 10 |
//! use futures_util::StreamExt; |
| 11 |
//! use zbus::zvariant::Value; |
| 12 |
//! |
| 13 |
//! async fn run() -> ashpd::Result<()> { |
| 14 |
//! let proxy = NotificationProxy::new().await?; |
| 15 |
//! |
| 16 |
//! let notification_id = "org.gnome.design.Contrast"; |
| 17 |
//! proxy |
| 18 |
//! .add_notification( |
| 19 |
//! notification_id, |
| 20 |
//! Notification::new("Contrast") |
| 21 |
//! .default_action("open") |
| 22 |
//! .default_action_target(100) |
| 23 |
//! .body("color copied to clipboard") |
| 24 |
//! .priority(Priority::High) |
| 25 |
//! .icon(Icon::with_names(&["dialog-question-symbolic"])) |
| 26 |
//! .button(Button::new("Copy", "copy").target(32)) |
| 27 |
//! .button(Button::new("Delete", "delete").target(40)), |
| 28 |
//! ) |
| 29 |
//! .await?; |
| 30 |
//! |
| 31 |
//! let action = proxy |
| 32 |
//! .receive_action_invoked() |
| 33 |
//! .await? |
| 34 |
//! .next() |
| 35 |
//! .await |
| 36 |
//! .expect("Stream exhausted"); |
| 37 |
//! match action.name() { |
| 38 |
//! "copy" => (), // Copy something to clipboard |
| 39 |
//! "delete" => (), // Delete the file |
| 40 |
//! _ => (), |
| 41 |
//! }; |
| 42 |
//! println!("{:#?}", action.id()); |
| 43 |
//! println!( |
| 44 |
//! "{:#?}", |
| 45 |
//! action.parameter().get(0).unwrap().downcast_ref::<u32>() |
| 46 |
//! ); |
| 47 |
//! |
| 48 |
//! proxy.remove_notification(notification_id).await?; |
| 49 |
//! Ok(()) |
| 50 |
//! } |
| 51 |
//! ``` |
| 52 |
|
| 53 |
use std::{fmt, os::fd::AsFd, str::FromStr}; |
| 54 |
|
| 55 |
use futures_util::Stream; |
| 56 |
use serde::{self, Deserialize, Serialize}; |
| 57 |
use zbus::zvariant::{ |
| 58 |
OwnedValue, Type, Value, |
| 59 |
as_value::{self, optional}, |
| 60 |
}; |
| 61 |
|
| 62 |
use super::Icon; |
| 63 |
use crate::{Error, proxy::Proxy}; |
| 64 |
|
| 65 |
#[derive(Debug, Clone, PartialEq, Eq, Type)] |
| 66 |
#[zvariant(signature = "s")] |
| 67 |
/// The content of a notification. |
| 68 |
pub enum Category { |
| 69 |
/// Instant messaging apps message. |
| 70 |
#[doc(alias = "im.message")] |
| 71 |
ImMessage, |
| 72 |
/// Ringing alarm. |
| 73 |
#[doc(alias = "alarm.ringing")] |
| 74 |
AlarmRinging, |
| 75 |
/// Incoming call. |
| 76 |
#[doc(alias = "call.incoming")] |
| 77 |
IncomingCall, |
| 78 |
/// Ongoing call. |
| 79 |
#[doc(alias = "call.ongoing")] |
| 80 |
OngoingCall, |
| 81 |
/// Missed call. |
| 82 |
#[doc(alias = "call.missed")] |
| 83 |
MissedCall, |
| 84 |
/// Extreme weather warning. |
| 85 |
#[doc(alias = "weather.warning.extreme")] |
| 86 |
ExtremeWeather, |
| 87 |
/// Extreme danger broadcast. |
| 88 |
#[doc(alias = "cellbroadcast.danger.extreme")] |
| 89 |
CellNetworkExtremeDanger, |
| 90 |
/// Severe danger broadcast. |
| 91 |
#[doc(alias = "cellbroadcast.danger.severe")] |
| 92 |
CellNetworkSevereDanger, |
| 93 |
/// Amber alert broadcast. |
| 94 |
#[doc(alias = "cellbroadcast.amber-alert")] |
| 95 |
CellNetworkAmberAlert, |
| 96 |
/// Test broadcast. |
| 97 |
#[doc(alias = "cellbroadcast.test")] |
| 98 |
CellNetworkBroadcastTest, |
| 99 |
/// Low battery. |
| 100 |
#[doc(alias = "os.battery.low")] |
| 101 |
LowBattery, |
| 102 |
/// Browser websites notifications. |
| 103 |
#[doc(alias = "browser.web-notification")] |
| 104 |
WebNotification, |
| 105 |
/// Vendor specific. |
| 106 |
Other(String), |
| 107 |
} |
| 108 |
|
| 109 |
impl Serialize for Category { |
| 110 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 111 |
where |
| 112 |
S: serde::Serializer, |
| 113 |
{ |
| 114 |
let category_str = match self { |
| 115 |
Self::ImMessage => "im.message", |
| 116 |
Self::AlarmRinging => "alarm.ringing", |
| 117 |
Self::IncomingCall => "call.incoming", |
| 118 |
Self::OngoingCall => "call.ongoing", |
| 119 |
Self::MissedCall => "call.missed", |
| 120 |
Self::ExtremeWeather => "weather.warning.extreme", |
| 121 |
Self::CellNetworkExtremeDanger => "cellbroadcast.danger.extreme", |
| 122 |
Self::CellNetworkSevereDanger => "cellbroadcast.danger.severe", |
| 123 |
Self::CellNetworkAmberAlert => "cellbroadcast.amber-alert", |
| 124 |
Self::CellNetworkBroadcastTest => "cellbroadcast.test", |
| 125 |
Self::LowBattery => "os.battery.low", |
| 126 |
Self::WebNotification => "browser.web-notification", |
| 127 |
Self::Other(other) => other.as_str(), |
| 128 |
}; |
| 129 |
serializer.serialize_str(category_str) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
impl FromStr for Category { |
| 134 |
type Err = crate::Error; |
| 135 |
|
| 136 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 137 |
match s { |
| 138 |
"im.message" => Ok(Self::ImMessage), |
| 139 |
"alarm.ringing" => Ok(Self::AlarmRinging), |
| 140 |
"call.incoming" => Ok(Self::IncomingCall), |
| 141 |
"call.ongoing" => Ok(Self::OngoingCall), |
| 142 |
"call.missed" => Ok(Self::MissedCall), |
| 143 |
"weather.warning.extreme" => Ok(Self::ExtremeWeather), |
| 144 |
"cellbroadcast.danger.extreme" => Ok(Self::CellNetworkExtremeDanger), |
| 145 |
"cellbroadcast.danger.severe" => Ok(Self::CellNetworkSevereDanger), |
| 146 |
"cellbroadcast.amber-alert" => Ok(Self::CellNetworkAmberAlert), |
| 147 |
"cellbroadcast.test" => Ok(Self::CellNetworkBroadcastTest), |
| 148 |
"os.battery.low" => Ok(Self::LowBattery), |
| 149 |
"browser.web-notification" => Ok(Self::WebNotification), |
| 150 |
_ => Ok(Self::Other(s.to_owned())), |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
impl<'de> Deserialize<'de> for Category { |
| 156 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 157 |
where |
| 158 |
D: serde::Deserializer<'de>, |
| 159 |
{ |
| 160 |
let category = String::deserialize(deserializer)?; |
| 161 |
category |
| 162 |
.parse::<Self>() |
| 163 |
.map_err(|_e| serde::de::Error::custom("Failed to parse category")) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
#[cfg_attr(feature = "glib", derive(glib::Enum))] |
| 168 |
#[cfg_attr(feature = "glib", enum_type(name = "AshpdPriority"))] |
| 169 |
#[derive(Debug, Copy, Clone, Serialize, PartialEq, Eq, Type)] |
| 170 |
#[zvariant(signature = "s")] |
| 171 |
#[serde(rename_all = "lowercase")] |
| 172 |
/// The notification priority |
| 173 |
pub enum Priority { |
| 174 |
/// Low. |
| 175 |
Low, |
| 176 |
/// Normal. |
| 177 |
Normal, |
| 178 |
/// High. |
| 179 |
High, |
| 180 |
/// Urgent. |
| 181 |
Urgent, |
| 182 |
} |
| 183 |
|
| 184 |
impl fmt::Display for Priority { |
| 185 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 186 |
match self { |
| 187 |
Self::Low => write!(f, "Low"), |
| 188 |
Self::Normal => write!(f, "Normal"), |
| 189 |
Self::High => write!(f, "High"), |
| 190 |
Self::Urgent => write!(f, "Urgent"), |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
impl AsRef<str> for Priority { |
| 196 |
fn as_ref(&self) -> &str { |
| 197 |
match self { |
| 198 |
Self::Low => "Low", |
| 199 |
Self::Normal => "Normal", |
| 200 |
Self::High => "High", |
| 201 |
Self::Urgent => "Urgent", |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
impl From<Priority> for &'static str { |
| 207 |
fn from(d: Priority) -> Self { |
| 208 |
match d { |
| 209 |
Priority::Low => "Low", |
| 210 |
Priority::Normal => "Normal", |
| 211 |
Priority::High => "High", |
| 212 |
Priority::Urgent => "Urgent", |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
impl FromStr for Priority { |
| 218 |
type Err = Error; |
| 219 |
|
| 220 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 221 |
match s { |
| 222 |
"Low" | "low" => Ok(Priority::Low), |
| 223 |
"Normal" | "normal" => Ok(Priority::Normal), |
| 224 |
"High" | "high" => Ok(Priority::High), |
| 225 |
"Urgent" | "urgent" => Ok(Priority::Urgent), |
| 226 |
_ => Err(Error::ParseError("Failed to parse priority, invalid value")), |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
#[cfg_attr(feature = "glib", derive(glib::Enum))] |
| 232 |
#[cfg_attr(feature = "glib", enum_type(name = "AshpdNotificationDisplayHint"))] |
| 233 |
#[derive(Debug, Copy, Clone, PartialEq, Eq, Type, Serialize)] |
| 234 |
#[zvariant(signature = "s")] |
| 235 |
#[serde(rename_all = "kebab-case")] |
| 236 |
/// Ways to display a notification. |
| 237 |
pub enum DisplayHint { |
| 238 |
/// Transient. |
| 239 |
#[doc(alias = "transient")] |
| 240 |
Transient, |
| 241 |
/// Tray. |
| 242 |
#[doc(alias = "tray")] |
| 243 |
Tray, |
| 244 |
/// Persistent. |
| 245 |
#[doc(alias = "persistent")] |
| 246 |
Persistent, |
| 247 |
/// Hide on lockscreen. |
| 248 |
#[doc(alias = "hide-on-lockscreen")] |
| 249 |
HideOnLockScreen, |
| 250 |
/// Enable speakerphone. |
| 251 |
#[doc(alias = "hide-content-on-lockscreen")] |
| 252 |
HideContentOnLockScreen, |
| 253 |
/// Show as new. |
| 254 |
#[doc(alias = "show-as-new")] |
| 255 |
ShowAsNew, |
| 256 |
} |
| 257 |
|
| 258 |
#[derive(Serialize, Type, Debug)] |
| 259 |
/// A notification |
| 260 |
#[zvariant(signature = "dict")] |
| 261 |
#[serde(rename_all = "kebab-case")] |
| 262 |
pub struct Notification { |
| 263 |
/// User-visible string to display as the title. |
| 264 |
#[serde(with = "as_value")] |
| 265 |
title: String, |
| 266 |
/// User-visible string to display as the body. |
| 267 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 268 |
body: Option<String>, |
| 269 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 270 |
markup_body: Option<String>, |
| 271 |
/// Serialized icon (e.g using gio::Icon::serialize). |
| 272 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 273 |
icon: Option<Icon>, |
| 274 |
/// The priority for the notification. |
| 275 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 276 |
priority: Option<Priority>, |
| 277 |
/// Name of an action that is exported by the application. |
| 278 |
/// This action will be activated when the user clicks on the notification. |
| 279 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 280 |
default_action: Option<String>, |
| 281 |
/// Target parameter to send along when activating the default action. |
| 282 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 283 |
default_action_target: Option<OwnedValue>, |
| 284 |
/// Array of buttons to add to the notification. |
| 285 |
#[serde(with = "as_value", skip_serializing_if = "Vec::is_empty")] |
| 286 |
buttons: Vec<Button>, |
| 287 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 288 |
category: Option<Category>, |
| 289 |
#[serde(with = "as_value", skip_serializing_if = "Vec::is_empty")] |
| 290 |
display_hint: Vec<DisplayHint>, |
| 291 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 292 |
sound: Option<OwnedValue>, |
| 293 |
} |
| 294 |
|
| 295 |
impl Notification { |
| 296 |
/// Create a new notification. |
| 297 |
/// |
| 298 |
/// # Arguments |
| 299 |
/// |
| 300 |
/// * `title` - the notification title. |
| 301 |
pub fn new(title: &str) -> Self { |
| 302 |
Self { |
| 303 |
title: title.to_owned(), |
| 304 |
body: None, |
| 305 |
markup_body: None, |
| 306 |
priority: None, |
| 307 |
icon: None, |
| 308 |
default_action: None, |
| 309 |
default_action_target: None, |
| 310 |
buttons: Vec::new(), |
| 311 |
category: None, |
| 312 |
display_hint: Vec::new(), |
| 313 |
sound: None, |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
/// Sets the notification body. |
| 318 |
#[must_use] |
| 319 |
pub fn body<'a>(mut self, body: impl Into<Option<&'a str>>) -> Self { |
| 320 |
self.body = body.into().map(ToOwned::to_owned); |
| 321 |
self |
| 322 |
} |
| 323 |
|
| 324 |
/// Same as [`Notification::body`] but supports markup formatting. |
| 325 |
#[must_use] |
| 326 |
pub fn markup_body<'a>(mut self, markup_body: impl Into<Option<&'a str>>) -> Self { |
| 327 |
self.markup_body = markup_body.into().map(ToOwned::to_owned); |
| 328 |
self |
| 329 |
} |
| 330 |
|
| 331 |
/// Sets an icon to the notification. |
| 332 |
#[must_use] |
| 333 |
pub fn icon(mut self, icon: impl Into<Option<Icon>>) -> Self { |
| 334 |
self.icon = icon.into(); |
| 335 |
self |
| 336 |
} |
| 337 |
|
| 338 |
/// Sets the notification sound. |
| 339 |
#[must_use] |
| 340 |
pub fn sound<S>(mut self, sound: impl Into<Option<S>>) -> Self |
| 341 |
where |
| 342 |
S: AsFd, |
| 343 |
{ |
| 344 |
self.sound = sound.into().map(|s| { |
| 345 |
zbus::zvariant::Value::from(zbus::zvariant::Fd::from(s.as_fd())) |
| 346 |
.try_to_owned() |
| 347 |
.unwrap() |
| 348 |
}); |
| 349 |
self |
| 350 |
} |
| 351 |
|
| 352 |
/// Sets the notification category. |
| 353 |
#[must_use] |
| 354 |
pub fn category(mut self, category: impl Into<Option<Category>>) -> Self { |
| 355 |
self.category = category.into(); |
| 356 |
self |
| 357 |
} |
| 358 |
|
| 359 |
#[must_use] |
| 360 |
/// Sets the notification display hints. |
| 361 |
pub fn display_hint(mut self, hints: impl IntoIterator<Item = DisplayHint>) -> Self { |
| 362 |
self.display_hint = hints.into_iter().collect(); |
| 363 |
self |
| 364 |
} |
| 365 |
|
| 366 |
/// Sets the notification priority. |
| 367 |
#[must_use] |
| 368 |
pub fn priority(mut self, priority: impl Into<Option<Priority>>) -> Self { |
| 369 |
self.priority = priority.into(); |
| 370 |
self |
| 371 |
} |
| 372 |
|
| 373 |
/// Sets the default action when the user clicks on the notification. |
| 374 |
#[must_use] |
| 375 |
pub fn default_action<'a>(mut self, default_action: impl Into<Option<&'a str>>) -> Self { |
| 376 |
self.default_action = default_action.into().map(ToOwned::to_owned); |
| 377 |
self |
| 378 |
} |
| 379 |
|
| 380 |
/// Sets a value to be sent in the `action_invoked` signal. |
| 381 |
#[must_use] |
| 382 |
pub fn default_action_target<'a, T: Into<Value<'a>>>( |
| 383 |
mut self, |
| 384 |
default_action_target: impl Into<Option<T>>, |
| 385 |
) -> Self { |
| 386 |
self.default_action_target = default_action_target |
| 387 |
.into() |
| 388 |
.map(|t| t.into().try_to_owned().unwrap()); |
| 389 |
self |
| 390 |
} |
| 391 |
|
| 392 |
/// Adds a new button to the notification. |
| 393 |
#[must_use] |
| 394 |
pub fn button(mut self, button: Button) -> Self { |
| 395 |
self.buttons.push(button); |
| 396 |
self |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
#[derive(Debug, Clone, PartialEq, Eq, Type)] |
| 401 |
#[zvariant(signature = "s")] |
| 402 |
/// The purpose of a button. |
| 403 |
pub enum ButtonPurpose { |
| 404 |
/// Instant messaging reply with text. |
| 405 |
#[doc(alias = "im.reply-with-text")] |
| 406 |
ImReplyWithText, |
| 407 |
/// Accept call. |
| 408 |
#[doc(alias = "call.accept")] |
| 409 |
CallAccept, |
| 410 |
/// Decline call. |
| 411 |
#[doc(alias = "call.decline")] |
| 412 |
CallDecline, |
| 413 |
/// Hangup call. |
| 414 |
#[doc(alias = "call.hang-up")] |
| 415 |
CallHangup, |
| 416 |
/// Enable speakerphone. |
| 417 |
#[doc(alias = "call.enable-speakerphone")] |
| 418 |
CallEnableSpeakerphone, |
| 419 |
/// Disable speakerphone. |
| 420 |
#[doc(alias = "call.disable-speakerphone")] |
| 421 |
CallDisableSpeakerphone, |
| 422 |
/// System custom alert. |
| 423 |
#[doc(alias = "system.custom-alert")] |
| 424 |
SystemCustomAlert, |
| 425 |
/// Vendor specific. |
| 426 |
Other(String), |
| 427 |
} |
| 428 |
|
| 429 |
impl Serialize for ButtonPurpose { |
| 430 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 431 |
where |
| 432 |
S: serde::Serializer, |
| 433 |
{ |
| 434 |
let purpose = match self { |
| 435 |
Self::ImReplyWithText => "im.reply-with-text", |
| 436 |
Self::CallAccept => "call.accept", |
| 437 |
Self::CallDecline => "call.decline", |
| 438 |
Self::CallHangup => "call.hang-up", |
| 439 |
Self::CallEnableSpeakerphone => "call.enable-speakerphone", |
| 440 |
Self::CallDisableSpeakerphone => "call.disable-speakerphone", |
| 441 |
Self::SystemCustomAlert => "system.custom-alert", |
| 442 |
Self::Other(other) => other.as_str(), |
| 443 |
}; |
| 444 |
serializer.serialize_str(purpose) |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
impl FromStr for ButtonPurpose { |
| 449 |
type Err = crate::Error; |
| 450 |
|
| 451 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 452 |
match s { |
| 453 |
"im.reply-with-text" => Ok(Self::ImReplyWithText), |
| 454 |
"call.accept" => Ok(Self::CallAccept), |
| 455 |
"call.decline" => Ok(Self::CallDecline), |
| 456 |
"call.hang-up" => Ok(Self::CallHangup), |
| 457 |
"call.enable-speakerphone" => Ok(Self::CallEnableSpeakerphone), |
| 458 |
"call.disable-speakerphone" => Ok(Self::CallDisableSpeakerphone), |
| 459 |
"system.custom-alert" => Ok(Self::SystemCustomAlert), |
| 460 |
_ => Ok(Self::Other(s.to_owned())), |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
impl<'de> Deserialize<'de> for ButtonPurpose { |
| 466 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 467 |
where |
| 468 |
D: serde::Deserializer<'de>, |
| 469 |
{ |
| 470 |
let purpose = String::deserialize(deserializer)?; |
| 471 |
purpose |
| 472 |
.parse::<Self>() |
| 473 |
.map_err(|_e| serde::de::Error::custom("Failed to parse purpose")) |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
#[derive(Serialize, Type, Debug)] |
| 478 |
/// A notification button |
| 479 |
#[zvariant(signature = "dict")] |
| 480 |
pub struct Button { |
| 481 |
/// User-visible label for the button. Mandatory. |
| 482 |
#[serde(with = "as_value")] |
| 483 |
label: String, |
| 484 |
/// Name of an action that is exported by the application. The action will |
| 485 |
/// be activated when the user clicks on the button. |
| 486 |
#[serde(with = "as_value")] |
| 487 |
action: String, |
| 488 |
/// Target parameter to send along when activating the action. |
| 489 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 490 |
target: Option<OwnedValue>, |
| 491 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 492 |
purpose: Option<ButtonPurpose>, |
| 493 |
} |
| 494 |
|
| 495 |
impl Button { |
| 496 |
/// Create a new notification button. |
| 497 |
/// |
| 498 |
/// # Arguments |
| 499 |
/// |
| 500 |
/// * `label` - the user visible label of the button. |
| 501 |
/// * `action` - the action name to be invoked when the user clicks on the |
| 502 |
/// button. |
| 503 |
pub fn new(label: &str, action: &str) -> Self { |
| 504 |
Self { |
| 505 |
label: label.to_owned(), |
| 506 |
action: action.to_owned(), |
| 507 |
target: None, |
| 508 |
purpose: None, |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
/// The value to send with the action name when the button is clicked. |
| 513 |
#[must_use] |
| 514 |
pub fn target<'a, T: Into<Value<'a>>>(mut self, target: impl Into<Option<T>>) -> Self { |
| 515 |
self.target = target.into().map(|t| t.into().try_to_owned().unwrap()); |
| 516 |
self |
| 517 |
} |
| 518 |
|
| 519 |
/// Sets the button purpose. |
| 520 |
#[must_use] |
| 521 |
pub fn purpose(mut self, purpose: impl Into<Option<ButtonPurpose>>) -> Self { |
| 522 |
self.purpose = purpose.into(); |
| 523 |
self |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
#[derive(Debug, Deserialize, Type)] |
| 528 |
/// An invoked action. |
| 529 |
pub struct Action(String, String, Vec<OwnedValue>); |
| 530 |
|
| 531 |
impl Action { |
| 532 |
/// Notification ID. |
| 533 |
pub fn id(&self) -> &str { |
| 534 |
&self.0 |
| 535 |
} |
| 536 |
|
| 537 |
/// Action name. |
| 538 |
pub fn name(&self) -> &str { |
| 539 |
&self.1 |
| 540 |
} |
| 541 |
|
| 542 |
/// The parameters passed to the action. |
| 543 |
pub fn parameter(&self) -> &[OwnedValue] { |
| 544 |
&self.2 |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
#[derive(Deserialize, Type, Debug, OwnedValue)] |
| 549 |
#[zvariant(signature = "dict")] |
| 550 |
#[serde(rename_all = "kebab-case")] |
| 551 |
// TODO: figure out why this can't use the enums |
| 552 |
struct SupportedOptions { |
| 553 |
#[serde(default, with = "as_value")] |
| 554 |
category: Vec<String>, |
| 555 |
#[serde(default, with = "as_value")] |
| 556 |
button_purpose: Vec<String>, |
| 557 |
} |
| 558 |
|
| 559 |
/// The interface lets sandboxed applications send and withdraw notifications. |
| 560 |
/// |
| 561 |
/// It is not possible for the application to learn if the notification was |
| 562 |
/// actually presented to the user. Not a portal in the strict sense, since |
| 563 |
/// there is no user interaction. |
| 564 |
/// |
| 565 |
/// **Note** in contrast to most other portal requests, notifications are |
| 566 |
/// expected to outlast the running application. If a user clicks on a |
| 567 |
/// notification after the application has exited, it will get activated again. |
| 568 |
/// |
| 569 |
/// Notifications can specify actions that can be activated by the user. |
| 570 |
/// Actions whose name starts with 'app.' are assumed to be exported and will be |
| 571 |
/// activated via the ActivateAction() method in the org.freedesktop.Application |
| 572 |
/// interface. Other actions are activated by sending the |
| 573 |
/// `#org.freedeskop.portal.Notification::ActionInvoked` signal to the |
| 574 |
/// application. |
| 575 |
/// |
| 576 |
/// Wrapper of the DBus interface: [`org.freedesktop.portal.Notification`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Notification.html). |
| 577 |
#[derive(Debug)] |
| 578 |
#[doc(alias = "org.freedesktop.portal.Notification")] |
| 579 |
pub struct NotificationProxy(Proxy<'static>); |
| 580 |
|
| 581 |
impl NotificationProxy { |
| 582 |
/// Create a new instance of [`NotificationProxy`]. |
| 583 |
pub async fn new() -> Result<Self, Error> { |
| 584 |
let proxy = Proxy::new_desktop("org.freedesktop.portal.Notification").await?; |
| 585 |
Ok(Self(proxy)) |
| 586 |
} |
| 587 |
|
| 588 |
/// Create a new instance of [`NotificationProxy`]. |
| 589 |
pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> { |
| 590 |
let proxy = |
| 591 |
Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.Notification") |
| 592 |
.await?; |
| 593 |
Ok(Self(proxy)) |
| 594 |
} |
| 595 |
|
| 596 |
/// Returns the version of the portal interface. |
| 597 |
pub fn version(&self) -> u32 { |
| 598 |
self.0.version() |
| 599 |
} |
| 600 |
|
| 601 |
/// Signal emitted when a particular action is invoked. |
| 602 |
/// |
| 603 |
/// # Specifications |
| 604 |
/// |
| 605 |
/// See also [`ActionInvoked`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Notification.html#org-freedesktop-portal-notification-actioninvoked). |
| 606 |
#[doc(alias = "ActionInvoked")] |
| 607 |
#[doc(alias = "XdpPortal::notification-action-invoked")] |
| 608 |
pub async fn receive_action_invoked(&self) -> Result<impl Stream<Item = Action>, Error> { |
| 609 |
self.0.signal("ActionInvoked").await |
| 610 |
} |
| 611 |
|
| 612 |
/// Sends a notification. |
| 613 |
/// |
| 614 |
/// The ID can be used to later withdraw the notification. |
| 615 |
/// If the application reuses the same ID without withdrawing, the |
| 616 |
/// notification is replaced by the new one. |
| 617 |
/// |
| 618 |
/// # Arguments |
| 619 |
/// |
| 620 |
/// * `id` - Application-provided ID for this notification. |
| 621 |
/// * `notification` - The notification. |
| 622 |
/// |
| 623 |
/// # Specifications |
| 624 |
/// |
| 625 |
/// See also [`AddNotification`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Notification.html#org-freedesktop-portal-notification-addnotification). |
| 626 |
#[doc(alias = "AddNotification")] |
| 627 |
#[doc(alias = "xdp_portal_add_notification")] |
| 628 |
pub async fn add_notification( |
| 629 |
&self, |
| 630 |
id: &str, |
| 631 |
notification: Notification, |
| 632 |
) -> Result<(), Error> { |
| 633 |
self.0.call("AddNotification", &(id, notification)).await |
| 634 |
} |
| 635 |
|
| 636 |
/// Withdraws a notification. |
| 637 |
/// |
| 638 |
/// # Arguments |
| 639 |
/// |
| 640 |
/// * `id` - Application-provided ID for this notification. |
| 641 |
/// |
| 642 |
/// # Specifications |
| 643 |
/// |
| 644 |
/// See also [`RemoveNotification`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Notification.html#org-freedesktop-portal-notification-removenotification). |
| 645 |
#[doc(alias = "RemoveNotification")] |
| 646 |
#[doc(alias = "xdp_portal_remove_notification")] |
| 647 |
pub async fn remove_notification(&self, id: &str) -> Result<(), Error> { |
| 648 |
self.0.call("RemoveNotification", &(id)).await |
| 649 |
} |
| 650 |
|
| 651 |
/// Supported options by the notifications server. |
| 652 |
/// |
| 653 |
/// # Required version |
| 654 |
/// |
| 655 |
/// The method requires the 2nd version implementation of the portal and |
| 656 |
/// would fail with [`Error::RequiresVersion`] otherwise. |
| 657 |
/// |
| 658 |
/// # Specifications |
| 659 |
/// |
| 660 |
/// See also [`SupportedOptions`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Notification.html#org-freedesktop-portal-notification-supportedoptions). |
| 661 |
#[doc(alias = "SupportedOptions")] |
| 662 |
pub async fn supported_options(&self) -> Result<(Vec<Category>, Vec<ButtonPurpose>), Error> { |
| 663 |
let options = self |
| 664 |
.0 |
| 665 |
.property_versioned::<SupportedOptions>("SupportedOptions", 2) |
| 666 |
.await?; |
| 667 |
let categories = options |
| 668 |
.category |
| 669 |
.into_iter() |
| 670 |
.map(|c| Category::from_str(&c).unwrap()) |
| 671 |
.collect(); |
| 672 |
let purposes = options |
| 673 |
.button_purpose |
| 674 |
.into_iter() |
| 675 |
.map(|c| ButtonPurpose::from_str(&c).unwrap()) |
| 676 |
.collect(); |
| 677 |
Ok((categories, purposes)) |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
impl std::ops::Deref for NotificationProxy { |
| 682 |
type Target = zbus::Proxy<'static>; |
| 683 |
|
| 684 |
fn deref(&self) -> &Self::Target { |
| 685 |
&self.0 |
| 686 |
} |
| 687 |
} |