//! # Examples //! //! ```rust,no_run //! use std::{thread, time}; //! //! use ashpd::desktop::{ //! Icon, //! notification::{Action, Button, Notification, NotificationProxy, Priority}, //! }; //! use futures_util::StreamExt; //! use zbus::zvariant::Value; //! //! async fn run() -> ashpd::Result<()> { //! let proxy = NotificationProxy::new().await?; //! //! let notification_id = "org.gnome.design.Contrast"; //! proxy //! .add_notification( //! notification_id, //! Notification::new("Contrast") //! .default_action("open") //! .default_action_target(100) //! .body("color copied to clipboard") //! .priority(Priority::High) //! .icon(Icon::with_names(&["dialog-question-symbolic"])) //! .button(Button::new("Copy", "copy").target(32)) //! .button(Button::new("Delete", "delete").target(40)), //! ) //! .await?; //! //! let action = proxy //! .receive_action_invoked() //! .await? //! .next() //! .await //! .expect("Stream exhausted"); //! match action.name() { //! "copy" => (), // Copy something to clipboard //! "delete" => (), // Delete the file //! _ => (), //! }; //! println!("{:#?}", action.id()); //! println!( //! "{:#?}", //! action.parameter().get(0).unwrap().downcast_ref::() //! ); //! //! proxy.remove_notification(notification_id).await?; //! Ok(()) //! } //! ``` use std::{fmt, os::fd::AsFd, str::FromStr}; use futures_util::Stream; use serde::{self, Deserialize, Serialize}; use zbus::zvariant::{ OwnedValue, Type, Value, as_value::{self, optional}, }; use super::Icon; use crate::{Error, proxy::Proxy}; #[derive(Debug, Clone, PartialEq, Eq, Type)] #[zvariant(signature = "s")] /// The content of a notification. pub enum Category { /// Instant messaging apps message. #[doc(alias = "im.message")] ImMessage, /// Ringing alarm. #[doc(alias = "alarm.ringing")] AlarmRinging, /// Incoming call. #[doc(alias = "call.incoming")] IncomingCall, /// Ongoing call. #[doc(alias = "call.ongoing")] OngoingCall, /// Missed call. #[doc(alias = "call.missed")] MissedCall, /// Extreme weather warning. #[doc(alias = "weather.warning.extreme")] ExtremeWeather, /// Extreme danger broadcast. #[doc(alias = "cellbroadcast.danger.extreme")] CellNetworkExtremeDanger, /// Severe danger broadcast. #[doc(alias = "cellbroadcast.danger.severe")] CellNetworkSevereDanger, /// Amber alert broadcast. #[doc(alias = "cellbroadcast.amber-alert")] CellNetworkAmberAlert, /// Test broadcast. #[doc(alias = "cellbroadcast.test")] CellNetworkBroadcastTest, /// Low battery. #[doc(alias = "os.battery.low")] LowBattery, /// Browser websites notifications. #[doc(alias = "browser.web-notification")] WebNotification, /// Vendor specific. Other(String), } impl Serialize for Category { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { let category_str = match self { Self::ImMessage => "im.message", Self::AlarmRinging => "alarm.ringing", Self::IncomingCall => "call.incoming", Self::OngoingCall => "call.ongoing", Self::MissedCall => "call.missed", Self::ExtremeWeather => "weather.warning.extreme", Self::CellNetworkExtremeDanger => "cellbroadcast.danger.extreme", Self::CellNetworkSevereDanger => "cellbroadcast.danger.severe", Self::CellNetworkAmberAlert => "cellbroadcast.amber-alert", Self::CellNetworkBroadcastTest => "cellbroadcast.test", Self::LowBattery => "os.battery.low", Self::WebNotification => "browser.web-notification", Self::Other(other) => other.as_str(), }; serializer.serialize_str(category_str) } } impl FromStr for Category { type Err = crate::Error; fn from_str(s: &str) -> Result { match s { "im.message" => Ok(Self::ImMessage), "alarm.ringing" => Ok(Self::AlarmRinging), "call.incoming" => Ok(Self::IncomingCall), "call.ongoing" => Ok(Self::OngoingCall), "call.missed" => Ok(Self::MissedCall), "weather.warning.extreme" => Ok(Self::ExtremeWeather), "cellbroadcast.danger.extreme" => Ok(Self::CellNetworkExtremeDanger), "cellbroadcast.danger.severe" => Ok(Self::CellNetworkSevereDanger), "cellbroadcast.amber-alert" => Ok(Self::CellNetworkAmberAlert), "cellbroadcast.test" => Ok(Self::CellNetworkBroadcastTest), "os.battery.low" => Ok(Self::LowBattery), "browser.web-notification" => Ok(Self::WebNotification), _ => Ok(Self::Other(s.to_owned())), } } } impl<'de> Deserialize<'de> for Category { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de>, { let category = String::deserialize(deserializer)?; category .parse::() .map_err(|_e| serde::de::Error::custom("Failed to parse category")) } } #[cfg_attr(feature = "glib", derive(glib::Enum))] #[cfg_attr(feature = "glib", enum_type(name = "AshpdPriority"))] #[derive(Debug, Copy, Clone, Serialize, PartialEq, Eq, Type)] #[zvariant(signature = "s")] #[serde(rename_all = "lowercase")] /// The notification priority pub enum Priority { /// Low. Low, /// Normal. Normal, /// High. High, /// Urgent. Urgent, } impl fmt::Display for Priority { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Low => write!(f, "Low"), Self::Normal => write!(f, "Normal"), Self::High => write!(f, "High"), Self::Urgent => write!(f, "Urgent"), } } } impl AsRef for Priority { fn as_ref(&self) -> &str { match self { Self::Low => "Low", Self::Normal => "Normal", Self::High => "High", Self::Urgent => "Urgent", } } } impl From for &'static str { fn from(d: Priority) -> Self { match d { Priority::Low => "Low", Priority::Normal => "Normal", Priority::High => "High", Priority::Urgent => "Urgent", } } } impl FromStr for Priority { type Err = Error; fn from_str(s: &str) -> Result { match s { "Low" | "low" => Ok(Priority::Low), "Normal" | "normal" => Ok(Priority::Normal), "High" | "high" => Ok(Priority::High), "Urgent" | "urgent" => Ok(Priority::Urgent), _ => Err(Error::ParseError("Failed to parse priority, invalid value")), } } } #[cfg_attr(feature = "glib", derive(glib::Enum))] #[cfg_attr(feature = "glib", enum_type(name = "AshpdNotificationDisplayHint"))] #[derive(Debug, Copy, Clone, PartialEq, Eq, Type, Serialize)] #[zvariant(signature = "s")] #[serde(rename_all = "kebab-case")] /// Ways to display a notification. pub enum DisplayHint { /// Transient. #[doc(alias = "transient")] Transient, /// Tray. #[doc(alias = "tray")] Tray, /// Persistent. #[doc(alias = "persistent")] Persistent, /// Hide on lockscreen. #[doc(alias = "hide-on-lockscreen")] HideOnLockScreen, /// Enable speakerphone. #[doc(alias = "hide-content-on-lockscreen")] HideContentOnLockScreen, /// Show as new. #[doc(alias = "show-as-new")] ShowAsNew, } #[derive(Serialize, Type, Debug)] /// A notification #[zvariant(signature = "dict")] #[serde(rename_all = "kebab-case")] pub struct Notification { /// User-visible string to display as the title. #[serde(with = "as_value")] title: String, /// User-visible string to display as the body. #[serde(with = "optional", skip_serializing_if = "Option::is_none")] body: Option, #[serde(with = "optional", skip_serializing_if = "Option::is_none")] markup_body: Option, /// Serialized icon (e.g using gio::Icon::serialize). #[serde(with = "optional", skip_serializing_if = "Option::is_none")] icon: Option, /// The priority for the notification. #[serde(with = "optional", skip_serializing_if = "Option::is_none")] priority: Option, /// Name of an action that is exported by the application. /// This action will be activated when the user clicks on the notification. #[serde(with = "optional", skip_serializing_if = "Option::is_none")] default_action: Option, /// Target parameter to send along when activating the default action. #[serde(with = "optional", skip_serializing_if = "Option::is_none")] default_action_target: Option, /// Array of buttons to add to the notification. #[serde(with = "as_value", skip_serializing_if = "Vec::is_empty")] buttons: Vec