Skip to content

Commit

Permalink
Clean up config and logging
Browse files Browse the repository at this point in the history
Application permissions are stored by XDG desktop portal itself.
  • Loading branch information
joshuamegnauth54 committed Aug 18, 2024
1 parent 5104351 commit d2d7b90
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 167 deletions.
8 changes: 2 additions & 6 deletions cosmic-portal-config/src/background.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
// SPDX-License-Identifier: GPL-3.0-only

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Default, PartialEq, Deserialize, Serialize)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Background {
/// App ID and allowed status
pub apps: HashMap<String, bool>,
/// Default preference for NotifyBackground's dialog
pub default_perm: PermissionDialog,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize, Serialize)]
pub enum PermissionDialog {
/// Grant apps permission to run in the background
Allow,
Expand Down
55 changes: 11 additions & 44 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct CosmicPortal {

pub config_handler: Option<cosmic_config::Config>,
pub config: config::Config,
pub tx_conf: Option<tokio::sync::watch::Sender<config::Config>>,

pub access_args: Option<access::AccessDialogArgs>,
pub access_choices: Vec<(Option<usize>, Vec<String>)>,
Expand Down Expand Up @@ -71,10 +72,7 @@ pub enum Msg {
Background(background::Msg),
Portal(subscription::Event),
Output(OutputEvent, WlOutput),
ConfigUpdateBackground {
app_id: String,
choice: Option<background::PermissionResponse>,
},
ConfigNotifyWatcher,
ConfigSetScreenshot(config::screenshot::Screenshot),
/// Update config from external changes
ConfigSubUpdate(config::Config),
Expand Down Expand Up @@ -136,6 +134,7 @@ impl cosmic::Application for CosmicPortal {
core,
config_handler,
config,
tx_conf: None,
access_args: Default::default(),
access_choices: Default::default(),
file_choosers: Default::default(),
Expand Down Expand Up @@ -196,36 +195,15 @@ impl cosmic::Application for CosmicPortal {
subscription::Event::Background(args) => {
background::update_args(self, args).map(cosmic::app::Message::App)
}
subscription::Event::BackgroundGetAppPerm(app_id, tx) => {
let perm = match self.config.background.default_perm {
config::background::PermissionDialog::Allow => {
background::ConfigAppPerm::DefaultAllow
}
config::background::PermissionDialog::Deny => {
background::ConfigAppPerm::DefaultDeny
}
_ => match self.config.background.apps.get(&app_id) {
Some(true) => background::ConfigAppPerm::UserAllow,
Some(false) => background::ConfigAppPerm::UserDeny,
None => background::ConfigAppPerm::Unset,
},
};
cosmic::Command::perform(
async move {
let _ = tx.send(perm).await;
cosmic::app::message::none()
},
|x| x,
)
}
subscription::Event::Config(config) => self.update(Msg::ConfigSubUpdate(config)),
subscription::Event::Accent(_)
| subscription::Event::IsDark(_)
| subscription::Event::HighContrast(_)
| subscription::Event::BackgroundToplevels => cosmic::iced::Command::none(),
subscription::Event::Init(tx) => {
subscription::Event::Init { tx, tx_conf } => {
self.tx = Some(tx);
Command::none()
self.tx_conf = Some(tx_conf);
self.update(Msg::ConfigNotifyWatcher)
}
},
Msg::Screenshot(m) => screenshot::update_msg(self, m).map(cosmic::app::Message::App),
Expand Down Expand Up @@ -305,20 +283,10 @@ impl cosmic::Application for CosmicPortal {

cosmic::iced::Command::none()
}
Msg::ConfigUpdateBackground { app_id, choice } => {
if let (Some(choice), Some(handler)) = (choice, &mut self.config_handler) {
self.config
.background
.apps
.insert(app_id, choice == background::PermissionResponse::Allow);
if let Err(e) = self
.config
.set_background(handler, self.config.background.clone())
{
log::error!("Failed to save background config: {e}");
}
Msg::ConfigNotifyWatcher => {
if let Some(tx) = self.tx_conf.as_mut() {
tx.send_replace(self.config.clone());
}

cosmic::iced::Command::none()
}
Msg::ConfigSetScreenshot(screenshot) => {
Expand All @@ -330,12 +298,11 @@ impl cosmic::Application for CosmicPortal {
}
None => log::error!("Failed to save config: No config handler"),
}

cosmic::iced::Command::none()
self.update(Msg::ConfigNotifyWatcher)
}
Msg::ConfigSubUpdate(config) => {
self.config = config;
cosmic::iced::Command::none()
self.update(Msg::ConfigNotifyWatcher)
}
}
}
Expand Down
Loading

0 comments on commit d2d7b90

Please sign in to comment.