Skip to content

Commit

Permalink
notifications: create Hints type
Browse files Browse the repository at this point in the history
  • Loading branch information
fufexan committed Oct 5, 2023
1 parent 1e7d595 commit 1873c2f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 51 deletions.
2 changes: 1 addition & 1 deletion gross/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn main() {
music::music_time();
}
Some(Commands::Notifications) => {
notifications::main();
let _ = notifications::main();
}
Some(Commands::SystemInfo) => {
todo!("SystemInfo command");
Expand Down
2 changes: 1 addition & 1 deletion notifications/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ serde = {version = "1.0.152", features = ["derive"]}
serde_json = "1.0.93"
tokio = { version = "1.26.0", features = ["rt-multi-thread", "macros", "sync", "time"] }
unix-named-pipe = "0.2.0"
zbus = "3.10.0"
zbus = "3.14.1"
25 changes: 8 additions & 17 deletions notifications/src/dbus.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use super::NotificationArc;
use chrono::Local;
use gtk::{prelude::IconThemeExt, IconLookupFlags, IconTheme};
use std::collections::HashMap;
use zbus::zvariant::Value;
use zbus::{dbus_interface, Connection};

use crate::{icon::Icon, Notification};
use crate::{types::Hints, Notification};

const CAPABILITIES: [&str; 6] = [
"icons",
Expand Down Expand Up @@ -46,7 +44,7 @@ impl NotificationServer {
summary: String,
body: String,
actions: Vec<String>,
hints: HashMap<String, Value<'_>>,
hints: Hints,
expire_timeout: i32,
) -> u32 {
gtk::init().expect("Failed to initialize GTK");
Expand All @@ -73,22 +71,18 @@ impl NotificationServer {
}
};

let action_icons = hints.get("action-icons");
let desktop_entry: String = hints
.get("desktop-entry")
.and_then(|v| v.clone().downcast())
.unwrap_or(app_name.clone());
let action_icons = hints.action_icons;
let desktop_entry = hints.desktop_entry;
// let image_data_d = hints.get("image_data");
// let image_data = hints.get("image-data");
// let image_path_d = hints.get("image_path");
// let image_path = hints.get("image-path");

let icon_data_hint = hints.get("icon_data");
let icon_data: Option<Icon> = icon_data_hint.and_then(|v| v.clone().downcast());
let icon_data = hints.icon_data;

let mut image = String::new();
if let Some(icon) = icon_data {
if let Ok(imgpath) = icon.write_image(&app_name, changed_id) {
if icon_data.width != 0 {
if let Ok(imgpath) = icon_data.write_image(&app_name, changed_id) {
image = imgpath;
}
};
Expand All @@ -106,10 +100,7 @@ impl NotificationServer {
time,
body,
actions,
urgency: hints
.get("urgency")
.and_then(|v| v.clone().downcast())
.unwrap_or_default(),
urgency: hints.urgency,
image,
timeout: expire_timeout,
visible: true,
Expand Down
5 changes: 3 additions & 2 deletions notifications/src/icon.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::{fs, path::PathBuf};

use image::{self, DynamicImage, RgbImage, RgbaImage};
use zbus::{export::serde::Serialize, zvariant::Value};
use serde::Deserialize;
use zbus::{export::serde::Serialize, zvariant::Type, zvariant::Value};

#[derive(Debug, Default, Clone, Serialize, Value)]
#[derive(Debug, Default, Clone, Deserialize, Serialize, Value, Type)]
pub struct Icon {
// image width
pub width: i32,
Expand Down
34 changes: 4 additions & 30 deletions notifications/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod dbus;
mod icon;
mod types;

use std::fs;
use std::time::Duration;
Expand All @@ -10,42 +11,15 @@ use tokio::sync::Mutex;

use anyhow::Result;
use nohash_hasher::NoHashHasher;
use zbus::export::serde::Serialize;
use types::Notification;

use crate::dbus::async_run;

type NotificationArc =
Arc<Mutex<HashMap<u32, Notification, BuildHasherDefault<NoHashHasher<u32>>>>>;

#[derive(Serialize)]
pub struct Notifications<'a> {
pub notifications: Vec<&'a Notification>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Notification {
pub id: u32,
pub app_name: String,
pub app_icon: String,
pub summary: String,
pub body: String,
pub time: String,
pub image: String,
pub timeout: i32,
pub urgency: u8,
pub actions: Vec<String>,
pub visible: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Action {
Close,
Clear,
Notify(Notification),
}

#[tokio::main]
async fn main() -> Result<()> {
pub async fn main() -> Result<()> {
let notifications: NotificationArc = Arc::new(Mutex::new(HashMap::with_hasher(
BuildHasherDefault::default(),
)));
Expand Down Expand Up @@ -88,7 +62,7 @@ async fn main() -> Result<()> {
}
});

async_run(notifications.clone()).await?;
async_run(Arc::clone(&notifications)).await?;

Ok(())
}
48 changes: 48 additions & 0 deletions notifications/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::icon::Icon;
use serde::{Deserialize, Serialize};
use zbus::zvariant::Type;

#[derive(Debug, Default, Clone, Deserialize, Serialize, Type)]
pub struct Hints {
pub action_icons: bool,
pub category: String,
pub desktop_entry: String,
pub image_data: Icon,
pub image_path: String,
pub icon_data: Icon,
pub resident: bool,
pub sound_file: String,
pub sound_name: String,
pub suppress_sound: bool,
pub transient: bool,
pub x: i32,
pub y: i32,
pub urgency: u8,
}

#[derive(Serialize)]
pub struct Notifications<'a> {
pub notifications: Vec<&'a Notification>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Notification {
pub id: u32,
pub app_name: String,
pub app_icon: String,
pub summary: String,
pub body: String,
pub time: String,
pub image: String,
pub timeout: i32,
pub urgency: u8,
pub actions: Vec<String>,
pub visible: bool,
}

// #[derive(Debug, Clone, PartialEq)]
// pub enum Action {
// Close,
// Clear,
// Notify(Notification),
// }

0 comments on commit 1873c2f

Please sign in to comment.