Create client side code

This commit is contained in:
2023-03-03 00:52:37 +09:00
parent 827ffebc63
commit 3214512bde
6 changed files with 67 additions and 2 deletions

25
Cargo.lock generated
View File

@@ -247,6 +247,8 @@ name = "fldsmdfr"
version = "0.1.0"
dependencies = [
"clap",
"serde",
"serde_json",
"tokio",
"zbus",
]
@@ -378,6 +380,12 @@ dependencies = [
"windows-sys 0.45.0",
]
[[package]]
name = "itoa"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440"
[[package]]
name = "lazy_static"
version = "1.4.0"
@@ -662,6 +670,12 @@ dependencies = [
"windows-sys 0.45.0",
]
[[package]]
name = "ryu"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde"
[[package]]
name = "scopeguard"
version = "1.1.0"
@@ -688,6 +702,17 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cad406b69c91885b5107daf2c29572f6c8cdb3c66826821e286c533490c0bc76"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "serde_repr"
version = "0.1.10"

View File

@@ -5,5 +5,7 @@ edition = "2021"
[dependencies]
clap = { version = "4.1.8", features = ["derive"] }
serde = "1.0.152"
serde_json = "1.0.93"
tokio = { version = "1", features = ["full"] }
zbus = { version = "3", default-features = false, features = ["tokio"]}

18
src/client.rs Normal file
View File

@@ -0,0 +1,18 @@
use std::error::Error;
use zbus::{dbus_proxy, Connection};
#[dbus_proxy(
default_service = "org.freedesktop.Notifications",
interface = "org.freedesktop.Notifications",
default_path = "/org/freedesktop/Notifications"
)]
trait Manager {
fn list(&self, short: bool) -> zbus::Result<u32>;
}
pub async fn list(short: bool) -> Result<(), Box<dyn Error>> {
let connection = Connection::session().await?;
let proxy = ManagerProxy::new(&connection).await?;
println!("{}", proxy.list(short).await?);
Ok(())
}

View File

@@ -1,9 +1,9 @@
use crate::notification::Hints;
use crate::notification::Notification;
use std::collections::HashMap;
use std::error::Error;
use zbus::dbus_interface;
use zbus::Connection;
use crate::notification::Notification;
pub struct NotifyManager {
next_id: u32,
@@ -84,4 +84,13 @@ impl NotifyManager {
"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()
}
}
}

View File

@@ -1,5 +1,6 @@
mod daemon;
mod notification;
mod client;
use clap::{Parser, Subcommand};
use daemon::NotifyManager;
@@ -27,14 +28,22 @@ enum Command {
#[arg(short, long)]
clear: bool,
},
/// List pending notifications
List {
/// Use a quick overview instead of json, one notification per line
#[arg(short, long)]
short: bool,
},
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let cli = Args::parse();
match &cli.command {
match cli.command {
Command::Daemon => NotifyManager::new().start().await,
Command::List { short } => client::list(short).await,
Command::Listen { .. } => unimplemented!(),
}
}

View File

@@ -1,5 +1,7 @@
use zbus::zvariant::{DeserializeDict, SerializeDict, Type};
use serde::Serialize;
#[derive(Serialize)]
pub struct Notification {
pub app_name: String,
pub app_icon: String,