Skip to content

Commit

Permalink
Improve D-Bus access error message for notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
azymohliad committed Feb 4, 2023
1 parent d8a646e commit d2537ab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/ui/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,9 @@ impl Component for Model {

let notifications_panel = notifications::Model::builder()
.launch(())
.detach();
.forward(&sender.input_sender(), |message| match message {
notifications::Output::Toast(n) => Input::Toast(n),
});

let firmware_panel = firmware_panel::Model::builder()
.launch(main_window)
Expand Down
48 changes: 32 additions & 16 deletions src/ui/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ use relm4::{gtk, ComponentParts, ComponentSender, Component, JoinHandle, RelmWid
#[derive(Debug)]
pub enum Input {
Device(Option<Arc<bt::InfiniTime>>),
SwitchProxy(bool),
SessionEnded,
SetNotificationSession(bool),
NotificationSessionEnded,
}

#[derive(Debug)]
pub enum Output {
Toast(&'static str),
}

#[derive(Default)]
Expand All @@ -23,21 +24,35 @@ pub struct Model {
}

impl Model {
fn start_proxy_task(&mut self, sender: ComponentSender<Self>) {
fn start_notifications_task(&mut self, sender: ComponentSender<Self>) {
if let Some(infinitime) = self.infinitime.clone() {
self.stop_proxy_task();
self.stop_notifications_task();
log::info!("Notification session started");
let infinitime = infinitime.clone();
self.task = Some(relm4::spawn(async move {
if let Err(error) = notifications::run_notification_session(&infinitime).await {
log::error!("Notifications session error: {error}");
if let Some(zbus::fdo::Error::AccessDenied(_)) = error.downcast_ref() {
log::warn!(
"Notification session failed: the app doesn't have permissions to monitor \
D-Bus session bus. If you're running it from flatpak, you can grant access with \
command: `flatpak override --socket=session-bus io.gitlab.azymohliad.WatchMate`, \
or via Flatseal"
);
_ = sender.output(Output::Toast(
"Notifications require full D-Bus session bus access.\n\
Please grant it via Flatseal or `flatpak override`"
));
} else {
log::warn!("Notifications session failed: {error}");
_ = sender.output(Output::Toast("Notification session failed"));
}
}
sender.input(Input::SessionEnded);
sender.input(Input::NotificationSessionEnded);
}));
}
}

fn stop_proxy_task(&mut self) {
fn stop_notifications_task(&mut self) {
// TODO: Is it safe to abort, or does it makes sense to
// hook up a message channel to finish gracefully?
if self.task.take().map(|h| h.abort()).is_some() {
Expand Down Expand Up @@ -67,19 +82,20 @@ impl Component for Model {
},

gtk::Switch {
set_state: model.is_enabled,
#[watch]
set_state: model.is_enabled && model.task.is_some(),
set_halign: gtk::Align::End,
set_hexpand: true,
connect_active_notify[sender] => move |switch| {
let state = switch.is_active();
sender.input(Input::SwitchProxy(state));
sender.input(Input::SetNotificationSession(state));
}
}
}
}

fn init(_: Self::Init, root: &Self::Root, sender: ComponentSender<Self>) -> ComponentParts<Self> {
let model = Self { is_enabled: true, ..Default::default() };
let model = Self { is_enabled: false, ..Default::default() };
let widgets = view_output!();
ComponentParts { model, widgets }
}
Expand All @@ -89,19 +105,19 @@ impl Component for Model {
Input::Device(infinitime) => {
self.infinitime = infinitime;
match self.infinitime {
Some(_) if self.is_enabled => self.start_proxy_task(sender),
Some(_) if self.is_enabled => self.start_notifications_task(sender),
Some(_) => {},
None => self.stop_proxy_task(),
None => self.stop_notifications_task(),
}
}
Input::SwitchProxy(state) => {
Input::SetNotificationSession(state) => {
self.is_enabled = state;
match state {
true => self.start_proxy_task(sender),
false => self.stop_proxy_task(),
true => self.start_notifications_task(sender),
false => self.stop_notifications_task(),
}
}
Input::SessionEnded => {
Input::NotificationSessionEnded => {
self.task = None;
}
}
Expand Down

0 comments on commit d2537ab

Please sign in to comment.