Just create the notifications handler
This commit is contained in:
parent
6270ea9717
commit
19ef64f8ed
2 changed files with 128 additions and 125 deletions
|
@ -50,7 +50,10 @@ fn main() {
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
info!("Query command built: {:?}", query_command);
|
info!("Query command built: {:?}", query_command);
|
||||||
|
|
||||||
let mut notification = notify_rust::Notification::new();
|
let interval = settings.interval;
|
||||||
|
let link = settings.link;
|
||||||
|
|
||||||
|
let mut notifications_handler = notification::NotificationsHandler::new(settings);
|
||||||
|
|
||||||
// Initialize the buffer to store the response from cmus, to compare it with the next one.
|
// Initialize the buffer to store the response from cmus, to compare it with the next one.
|
||||||
let mut previous_response = CmusQueryResponse::default();
|
let mut previous_response = CmusQueryResponse::default();
|
||||||
|
@ -58,11 +61,11 @@ fn main() {
|
||||||
loop {
|
loop {
|
||||||
// Get the response from cmus.
|
// Get the response from cmus.
|
||||||
let Ok(response) = cmus::ping_cmus(&mut query_command) else {
|
let Ok(response) = cmus::ping_cmus(&mut query_command) else {
|
||||||
if settings.link {
|
if link {
|
||||||
std::process::exit(0)
|
std::process::exit(0)
|
||||||
} else {
|
} else {
|
||||||
// If the track info is the same as the previous one, just sleep for a while and try again.
|
// If the track info is the same as the previous one, just sleep for a while and try again.
|
||||||
sleep!(settings.interval);
|
sleep!(interval);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -74,8 +77,7 @@ fn main() {
|
||||||
// Update the previous response.
|
// Update the previous response.
|
||||||
previous_response = response;
|
previous_response = response;
|
||||||
|
|
||||||
|
match notifications_handler.show_notification(events, &previous_response) {
|
||||||
match notification::show_notification(events, &settings, &mut notification, &previous_response) {
|
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("Error: {}", e);
|
eprintln!("Error: {}", e);
|
||||||
|
@ -83,6 +85,6 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sleep!(settings.interval);
|
sleep!(interval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,13 +7,28 @@ use log::{debug, info};
|
||||||
use notify_rust::Notification;
|
use notify_rust::Notification;
|
||||||
use crate::cmus::query::CmusQueryResponse;
|
use crate::cmus::query::CmusQueryResponse;
|
||||||
|
|
||||||
#[inline(always)]
|
pub struct NotificationsHandler {
|
||||||
pub fn show_notification(
|
cover_set: bool,
|
||||||
|
notification: Notification,
|
||||||
|
handlers: Vec<notify_rust::NotificationHandle>,
|
||||||
|
settings: Settings,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NotificationsHandler {
|
||||||
|
pub fn new(settings: Settings) -> Self {
|
||||||
|
Self {
|
||||||
|
cover_set: false,
|
||||||
|
notification: Notification::new(),
|
||||||
|
handlers: Vec::new(),
|
||||||
|
settings,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn show_notification(
|
||||||
|
&mut self,
|
||||||
events: Vec<CmusEvent>,
|
events: Vec<CmusEvent>,
|
||||||
settings: &Settings,
|
|
||||||
notification: &mut Notification,
|
|
||||||
response: &CmusQueryResponse,
|
response: &CmusQueryResponse,
|
||||||
) -> Result<(), notify_rust::error::Error> {
|
) -> Result<(), notify_rust::error::Error> {
|
||||||
if events.is_empty() {
|
if events.is_empty() {
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
info!("no events to process");
|
info!("no events to process");
|
||||||
|
@ -21,7 +36,7 @@ pub fn show_notification(
|
||||||
}
|
}
|
||||||
|
|
||||||
//FIXME: Should check if the user has enabled the cover feature or use a static cover.
|
//FIXME: Should check if the user has enabled the cover feature or use a static cover.
|
||||||
update_cover(&events[0], response, settings, notification);
|
self.update_cover(&events[0], response);
|
||||||
|
|
||||||
for event in events {
|
for event in events {
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
|
@ -31,13 +46,13 @@ pub fn show_notification(
|
||||||
CmusEvent::StatusChanged(track) => {
|
CmusEvent::StatusChanged(track) => {
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
debug!("Status changed: {:?}", track.status);
|
debug!("Status changed: {:?}", track.status);
|
||||||
build_status_notification(track, settings, notification);
|
self.build_status_notification(track);
|
||||||
notification.show()?;
|
self.notification.show()?;
|
||||||
}
|
}
|
||||||
CmusEvent::TrackChanged(track) => {
|
CmusEvent::TrackChanged(track) => {
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
debug!("Track changed: {:?}", track);
|
debug!("Track changed: {:?}", track);
|
||||||
build_track_notification(track, settings, notification)?
|
self.build_track_notification(track)?
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
CmusEvent::VolumeChanged { left, right } if settings.show_player_notifications => {
|
CmusEvent::VolumeChanged { left, right } if settings.show_player_notifications => {
|
||||||
|
@ -58,82 +73,68 @@ pub fn show_notification(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn build_status_notification(
|
fn build_status_notification(&mut self, track: Track) {
|
||||||
track: Track,
|
|
||||||
settings: &Settings,
|
|
||||||
notification: &mut Notification,
|
|
||||||
) {
|
|
||||||
// Set the summary and body of the notification.
|
// Set the summary and body of the notification.
|
||||||
notification
|
self.notification
|
||||||
.summary(
|
.summary(
|
||||||
process_template_placeholders(&settings.status_notification_summary, &track).as_str(),
|
process_template_placeholders(&self.settings.status_notification_summary, &track).as_str(),
|
||||||
)
|
)
|
||||||
.body(process_template_placeholders(&settings.status_notification_body, &track).as_str())
|
.body(process_template_placeholders(&self.settings.status_notification_body, &track).as_str())
|
||||||
.timeout(settings.status_notification_timeout as i32 * 1000);
|
.timeout(self.settings.status_notification_timeout as i32 * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn build_track_notification(
|
fn build_track_notification(&mut self, track: Track) -> Result<(), notify_rust::error::Error> {
|
||||||
track: Track,
|
|
||||||
settings: &Settings,
|
|
||||||
notification: &mut Notification,
|
|
||||||
) -> Result<(), notify_rust::error::Error> {
|
|
||||||
// Set the summary and body of the notification.
|
// Set the summary and body of the notification.
|
||||||
notification
|
self.notification
|
||||||
.summary(process_template_placeholders(&settings.summary, &track).as_str())
|
.summary(process_template_placeholders(&self.settings.summary, &track).as_str())
|
||||||
.body(process_template_placeholders(&settings.body, &track).as_str());
|
.body(process_template_placeholders(&self.settings.body, &track).as_str());
|
||||||
|
|
||||||
let n = notification.show()?;
|
let n = self.notification.show()?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! setup_notification {
|
#[inline(always)]
|
||||||
($notification: expr, $settings: expr) => {
|
fn update_cover(&mut self, first_event: &CmusEvent, response: &CmusQueryResponse) {
|
||||||
$notification
|
|
||||||
.appname("cmus-notify")
|
|
||||||
.timeout($settings.timeout as i32 * 1000)
|
|
||||||
.hint(notify_rust::Hint::Category("music".to_string()))
|
|
||||||
.hint(notify_rust::Hint::DesktopEntry("cmus-notify.desktop".to_string()))
|
|
||||||
.hint(notify_rust::Hint::Resident(true));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn update_cover(first_event: &CmusEvent, response: &CmusQueryResponse, settings: &Settings, notification: &mut Notification) {
|
|
||||||
let mut cover = TrackCover::None;
|
|
||||||
// If the track is changed, we need to update the cover.
|
// If the track is changed, we need to update the cover.
|
||||||
match first_event {
|
match first_event {
|
||||||
CmusEvent::TrackChanged(track) => {
|
CmusEvent::TrackChanged(track) => {
|
||||||
cover = track_cover(
|
self.set_cover(track);
|
||||||
&track.path,
|
|
||||||
settings.depth,
|
|
||||||
settings.force_use_external_cover,
|
|
||||||
settings.no_use_external_cover,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
if cover == TrackCover::None {
|
if !self.cover_set {
|
||||||
// If the cover is not found, we need to update it.
|
// If the cover is not found, we need to update it.
|
||||||
if let Ok(track) = &response.track() {
|
if let Ok(track) = response.track() {
|
||||||
cover = track_cover(
|
self.set_cover(&track);
|
||||||
&track.path,
|
|
||||||
settings.depth,
|
|
||||||
settings.force_use_external_cover,
|
|
||||||
settings.no_use_external_cover,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Set the notification cover.
|
|
||||||
if cover != TrackCover::None {
|
}
|
||||||
*notification = Notification::new();
|
|
||||||
// Reset the notification.
|
fn set_cover(&mut self, track: &Track) {
|
||||||
setup_notification!(&mut *notification, &settings);
|
// Reset the notification
|
||||||
cover.set_notification_image(&mut *notification);
|
self.notification = Notification::new();
|
||||||
|
self.notification.appname("cmus-notify")
|
||||||
|
.timeout(self.settings.timeout as i32 * 1000)
|
||||||
|
.hint(notify_rust::Hint::Category("music".to_string()))
|
||||||
|
.hint(notify_rust::Hint::DesktopEntry("cmus.desktop".to_string()))
|
||||||
|
.hint(notify_rust::Hint::Resident(true));
|
||||||
|
|
||||||
|
// Get the track cover and set it to notification
|
||||||
|
track_cover(
|
||||||
|
&track.path,
|
||||||
|
self.settings.depth,
|
||||||
|
self.settings.force_use_external_cover,
|
||||||
|
self.settings.no_use_external_cover,
|
||||||
|
).set_notification_image(&mut self.notification);
|
||||||
|
// Flip the change flag
|
||||||
|
self.cover_set = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue