Skip to content

Commit d2d7b90

Browse files
Clean up config and logging
Application permissions are stored by XDG desktop portal itself.
1 parent 5104351 commit d2d7b90

File tree

4 files changed

+104
-167
lines changed

4 files changed

+104
-167
lines changed

cosmic-portal-config/src/background.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
// SPDX-License-Identifier: GPL-3.0-only
22

3-
use std::collections::HashMap;
4-
53
use serde::{Deserialize, Serialize};
64

7-
#[derive(Debug, Clone, Default, PartialEq, Deserialize, Serialize)]
5+
#[derive(Debug, Clone, Copy, Default, PartialEq, Deserialize, Serialize)]
86
#[serde(deny_unknown_fields)]
97
pub struct Background {
10-
/// App ID and allowed status
11-
pub apps: HashMap<String, bool>,
128
/// Default preference for NotifyBackground's dialog
139
pub default_perm: PermissionDialog,
1410
}
1511

16-
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
12+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize, Serialize)]
1713
pub enum PermissionDialog {
1814
/// Grant apps permission to run in the background
1915
Allow,

src/app.rs

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub struct CosmicPortal {
3131

3232
pub config_handler: Option<cosmic_config::Config>,
3333
pub config: config::Config,
34+
pub tx_conf: Option<tokio::sync::watch::Sender<config::Config>>,
3435

3536
pub access_args: Option<access::AccessDialogArgs>,
3637
pub access_choices: Vec<(Option<usize>, Vec<String>)>,
@@ -71,10 +72,7 @@ pub enum Msg {
7172
Background(background::Msg),
7273
Portal(subscription::Event),
7374
Output(OutputEvent, WlOutput),
74-
ConfigUpdateBackground {
75-
app_id: String,
76-
choice: Option<background::PermissionResponse>,
77-
},
75+
ConfigNotifyWatcher,
7876
ConfigSetScreenshot(config::screenshot::Screenshot),
7977
/// Update config from external changes
8078
ConfigSubUpdate(config::Config),
@@ -136,6 +134,7 @@ impl cosmic::Application for CosmicPortal {
136134
core,
137135
config_handler,
138136
config,
137+
tx_conf: None,
139138
access_args: Default::default(),
140139
access_choices: Default::default(),
141140
file_choosers: Default::default(),
@@ -196,36 +195,15 @@ impl cosmic::Application for CosmicPortal {
196195
subscription::Event::Background(args) => {
197196
background::update_args(self, args).map(cosmic::app::Message::App)
198197
}
199-
subscription::Event::BackgroundGetAppPerm(app_id, tx) => {
200-
let perm = match self.config.background.default_perm {
201-
config::background::PermissionDialog::Allow => {
202-
background::ConfigAppPerm::DefaultAllow
203-
}
204-
config::background::PermissionDialog::Deny => {
205-
background::ConfigAppPerm::DefaultDeny
206-
}
207-
_ => match self.config.background.apps.get(&app_id) {
208-
Some(true) => background::ConfigAppPerm::UserAllow,
209-
Some(false) => background::ConfigAppPerm::UserDeny,
210-
None => background::ConfigAppPerm::Unset,
211-
},
212-
};
213-
cosmic::Command::perform(
214-
async move {
215-
let _ = tx.send(perm).await;
216-
cosmic::app::message::none()
217-
},
218-
|x| x,
219-
)
220-
}
221198
subscription::Event::Config(config) => self.update(Msg::ConfigSubUpdate(config)),
222199
subscription::Event::Accent(_)
223200
| subscription::Event::IsDark(_)
224201
| subscription::Event::HighContrast(_)
225202
| subscription::Event::BackgroundToplevels => cosmic::iced::Command::none(),
226-
subscription::Event::Init(tx) => {
203+
subscription::Event::Init { tx, tx_conf } => {
227204
self.tx = Some(tx);
228-
Command::none()
205+
self.tx_conf = Some(tx_conf);
206+
self.update(Msg::ConfigNotifyWatcher)
229207
}
230208
},
231209
Msg::Screenshot(m) => screenshot::update_msg(self, m).map(cosmic::app::Message::App),
@@ -305,20 +283,10 @@ impl cosmic::Application for CosmicPortal {
305283

306284
cosmic::iced::Command::none()
307285
}
308-
Msg::ConfigUpdateBackground { app_id, choice } => {
309-
if let (Some(choice), Some(handler)) = (choice, &mut self.config_handler) {
310-
self.config
311-
.background
312-
.apps
313-
.insert(app_id, choice == background::PermissionResponse::Allow);
314-
if let Err(e) = self
315-
.config
316-
.set_background(handler, self.config.background.clone())
317-
{
318-
log::error!("Failed to save background config: {e}");
319-
}
286+
Msg::ConfigNotifyWatcher => {
287+
if let Some(tx) = self.tx_conf.as_mut() {
288+
tx.send_replace(self.config.clone());
320289
}
321-
322290
cosmic::iced::Command::none()
323291
}
324292
Msg::ConfigSetScreenshot(screenshot) => {
@@ -330,12 +298,11 @@ impl cosmic::Application for CosmicPortal {
330298
}
331299
None => log::error!("Failed to save config: No config handler"),
332300
}
333-
334-
cosmic::iced::Command::none()
301+
self.update(Msg::ConfigNotifyWatcher)
335302
}
336303
Msg::ConfigSubUpdate(config) => {
337304
self.config = config;
338-
cosmic::iced::Command::none()
305+
self.update(Msg::ConfigNotifyWatcher)
339306
}
340307
}
341308
}

0 commit comments

Comments
 (0)