Create the list method

This commit is contained in:
2023-03-03 20:46:24 +09:00
parent 3214512bde
commit 1814d7660e
4 changed files with 62 additions and 28 deletions
+16 -5
View File
@@ -1,5 +1,6 @@
use std::error::Error;
use zbus::{dbus_proxy, Connection};
use zbus::{dbus_proxy, Connection, Result};
use crate::{notification::Notification, Format};
#[dbus_proxy(
default_service = "org.freedesktop.Notifications",
@@ -7,12 +8,22 @@ use zbus::{dbus_proxy, Connection};
default_path = "/org/freedesktop/Notifications"
)]
trait Manager {
fn list(&self, short: bool) -> zbus::Result<u32>;
fn list(&self) -> Result<Vec<Notification>>;
}
pub async fn list(short: bool) -> Result<(), Box<dyn Error>> {
// TODO: strip \n on the short format.
// TODO: html unescape dbus messages.
pub async fn list(format: Format) -> Result<()> {
let connection = Connection::session().await?;
let proxy = ManagerProxy::new(&connection).await?;
println!("{}", proxy.list(short).await?);
let pendings = proxy.list().await?;
match format {
Format::Json => println!("{}", serde_json::to_string(&pendings).unwrap()),
Format::Short => {
for notif in &pendings {
println!("{}: {}", notif.summary, notif.body);
}
}
}
Ok(())
}
+14 -15
View File
@@ -1,5 +1,6 @@
use crate::notification::Hints;
use crate::notification::Notification;
use crate::notification::ServerInformation;
use std::collections::HashMap;
use std::error::Error;
use zbus::dbus_interface;
@@ -50,6 +51,7 @@ impl NotifyManager {
self.pendings.insert(
self.next_id,
Notification {
id: self.next_id,
app_name,
app_icon,
summary,
@@ -76,21 +78,18 @@ impl NotifyManager {
]
}
fn get_server_information(&self) -> [&str; 4] {
[
env!("CARGO_PKG_NAME"),
"zoriya",
env!("CARGO_PKG_VERSION"),
"1.2",
]
}
fn list(&self, short: bool) -> String {
if short {
unreachable!()
// self.pendings.values().map(|x| x.summary).join("\n")
} else {
serde_json::to_string(&self.pendings).unwrap()
fn get_server_information(&self) -> ServerInformation {
ServerInformation {
name: env!("CARGO_PKG_NAME").to_string(),
vendor: "zoriya".to_string(),
version: env!("CARGO_PKG_VERSION").to_string(),
spec_version: "1.2".to_string(),
}
}
fn list(&self) -> Vec<&Notification> {
let mut ret: Vec<&Notification> = self.pendings.values().collect();
ret.sort_by(|a, b| a.id.cmp(&b.id));
ret
}
}
+14 -6
View File
@@ -1,8 +1,8 @@
mod client;
mod daemon;
mod notification;
mod client;
use clap::{Parser, Subcommand};
use clap::{Parser, Subcommand, ValueEnum};
use daemon::NotifyManager;
use std::error::Error;
@@ -31,19 +31,27 @@ enum Command {
/// List pending notifications
List {
/// Use a quick overview instead of json, one notification per line
#[arg(short, long)]
short: bool,
/// Select the format to display notifications.
#[arg(short, long, default_value = "short")]
format: Format,
},
}
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
pub enum Format {
/// A short, human-readable display.
Short,
/// A json object with every informations available
Json,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let cli = Args::parse();
match cli.command {
Command::Daemon => NotifyManager::new().start().await,
Command::List { short } => client::list(short).await,
Command::List { format } => client::list(format).await,
Command::Listen { .. } => unimplemented!(),
}
}
+18 -2
View File
@@ -1,8 +1,9 @@
use serde::{Deserialize, Serialize};
use zbus::zvariant::{DeserializeDict, SerializeDict, Type};
use serde::Serialize;
#[derive(Serialize)]
#[derive(Deserialize, Serialize, Type)]
pub struct Notification {
pub id: u32,
pub app_name: String,
pub app_icon: String,
pub summary: String,
@@ -17,3 +18,18 @@ pub struct Hints {
category: Option<String>,
urgency: Option<u8>,
}
#[derive(Debug, Type, Serialize, Deserialize)]
pub struct ServerInformation {
/// The product name of the server.
pub name: String,
/// The vendor name. For example "KDE," "GNOME," "freedesktop.org" or "Microsoft".
pub vendor: String,
/// The server's version number.
pub version: String,
/// The specification version the server is compliant with.
pub spec_version: String,
}