canary-notifications protocol and panel opening

This commit is contained in:
mars 2022-12-06 17:09:50 -07:00
parent 2d098f8dea
commit d073dde8ca
3 changed files with 84 additions and 10 deletions

View File

@ -3,7 +3,17 @@ name = "canary-notifications"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[[bin]]
name = "canary-notifications"
path = "src/main.rs"
required-features = ["bin"]
[dependencies] [dependencies]
canary-magpie = { path = "../magpie" } canary-magpie = { path = "../magpie", optional = true, features = ["async"] }
smol = "1.2" serde = { version = "1", features = ["derive"] }
zbus = "3.5" serde_json = "1"
smol = { version = "1.2", optional = true }
zbus = { version = "3.5", optional = true }
[features]
bin = ["dep:canary-magpie", "dep:smol", "dep:zbus"]

View File

@ -0,0 +1,27 @@
// Copyright (c) 2022 Marceline Cramer
// SPDX-License-Identifier: AGPL-3.0-or-later
use serde::{Deserialize, Serialize};
pub use serde;
pub use serde_json;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Contents {
/// The optional name of the application sending the notification.
pub app_name: Option<String>,
/// The summary text briefly describing the notification.
pub summary: String,
/// The optional detailed body text.
pub body: Option<String>,
/// The timeout time in milliseconds since the display of the notification
/// at which the notification should automatically close.
pub timeout: Option<i32>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum OutMsg {}

View File

@ -1,14 +1,18 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::future::pending; use std::future::pending;
use std::path::PathBuf;
use canary_magpie::protocol::*; use canary_magpie::protocol::*;
use canary_notifications::Contents;
use smol::net::unix::UnixStream; use smol::net::unix::UnixStream;
use zbus::{dbus_interface, zvariant::Value, ConnectionBuilder, SignalContext}; use zbus::{dbus_interface, zvariant::Value, ConnectionBuilder, SignalContext};
pub type MagpieClient = ClientMessenger<UnixStream>; pub type MagpieClient = ClientMessenger<UnixStream>;
pub struct Notifications { pub struct Notifications {
module_path: PathBuf,
magpie: MagpieClient, magpie: MagpieClient,
next_id: u32,
} }
#[dbus_interface(name = "org.freedesktop.Notifications")] #[dbus_interface(name = "org.freedesktop.Notifications")]
@ -30,19 +34,40 @@ impl Notifications {
)) ))
} }
fn notify( async fn notify(
&self, &mut self,
app_name: String, app_name: String,
replaces_id: u32, replaces_id: u32,
app_icon: String, app_icon: String,
summary: String, summary: String,
body: String, body: String,
actions: Vec<String>, actions: Vec<String>,
hints: HashMap<String, Value>, hints: HashMap<String, Value<'_>>,
expire_timeout: i32, timeout: i32,
) -> u32 { ) -> u32 {
println!("{}: {}", summary, body); let contents = Contents {
0 app_name: Some(app_name).filter(String::is_empty),
summary,
body: Some(body).filter(String::is_empty),
timeout: Some(timeout).filter(|t| *t == 0),
};
let id = self.next_id;
self.next_id += 1;
let msg = CreatePanel {
id,
protocol: "tebibyte-media.desktop.notification".to_string(),
script: self.module_path.to_owned(),
init_msg: serde_json::to_vec(&contents).unwrap(),
};
self.magpie
.send_async(&MagpieServerMsg::CreatePanel(msg))
.await
.unwrap();
id
} }
fn close_notification(&self, id: u32) {} fn close_notification(&self, id: u32) {}
@ -60,11 +85,23 @@ impl Notifications {
} }
pub fn main() { pub fn main() {
let args: Vec<String> = std::env::args().collect();
let module_path = args
.get(1)
.expect("Please pass a path to a Canary script!")
.to_owned()
.into();
smol::block_on(async { smol::block_on(async {
let sock_path = find_socket(); let sock_path = find_socket();
let socket = UnixStream::connect(sock_path).await.unwrap(); let socket = UnixStream::connect(sock_path).await.unwrap();
let magpie = MagpieClient::new(socket); let magpie = MagpieClient::new(socket);
let notifications = Notifications { magpie };
let notifications = Notifications {
magpie,
next_id: 0,
module_path,
};
let _ = ConnectionBuilder::session() let _ = ConnectionBuilder::session()
.unwrap() .unwrap()