2022-07-19 11:33:19 -04:00
|
|
|
use anyhow::anyhow;
|
2022-12-15 14:35:31 -05:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-07-19 23:39:19 -04:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
use std::fs::File;
|
2022-12-13 22:46:30 -05:00
|
|
|
use std::path::PathBuf;
|
2022-07-19 11:33:19 -04:00
|
|
|
use xdg::BaseDirectories;
|
|
|
|
|
|
2022-12-12 19:48:31 -05:00
|
|
|
pub const APP_ID: &str = "com.system76.CosmicAppList";
|
|
|
|
|
pub const VERSION: &str = "0.1.0";
|
|
|
|
|
|
2022-12-13 22:46:30 -05:00
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
|
2022-07-19 11:33:19 -04:00
|
|
|
pub enum TopLevelFilter {
|
2022-12-12 19:48:31 -05:00
|
|
|
#[default]
|
2022-07-19 11:33:19 -04:00
|
|
|
ActiveWorkspace,
|
|
|
|
|
ConfiguredOutput,
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 22:46:30 -05:00
|
|
|
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
|
2022-07-19 11:33:19 -04:00
|
|
|
pub struct AppListConfig {
|
|
|
|
|
pub filter_top_levels: Option<TopLevelFilter>,
|
2022-12-12 19:48:31 -05:00
|
|
|
pub favorites: Vec<String>,
|
2022-07-19 11:33:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AppListConfig {
|
2022-12-13 22:46:30 -05:00
|
|
|
// TODO async?
|
2022-07-19 11:33:19 -04:00
|
|
|
/// load config with the provided name
|
|
|
|
|
pub fn load() -> anyhow::Result<AppListConfig> {
|
2022-12-13 22:46:30 -05:00
|
|
|
let mut relative_path = PathBuf::from(APP_ID);
|
|
|
|
|
relative_path.push("config.ron");
|
2022-07-19 23:39:19 -04:00
|
|
|
let file = match BaseDirectories::new()
|
|
|
|
|
.ok()
|
2022-12-13 22:46:30 -05:00
|
|
|
.and_then(|dirs| dirs.find_config_file(relative_path))
|
2022-07-19 23:39:19 -04:00
|
|
|
.and_then(|p| File::open(p).ok())
|
|
|
|
|
{
|
2022-07-19 11:33:19 -04:00
|
|
|
Some(path) => path,
|
|
|
|
|
_ => {
|
|
|
|
|
anyhow::bail!("Failed to load config");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-07-19 23:39:19 -04:00
|
|
|
ron::de::from_reader::<_, AppListConfig>(file)
|
|
|
|
|
.map_err(|err| anyhow!("Failed to parse config file: {}", err))
|
2022-07-19 11:33:19 -04:00
|
|
|
}
|
2022-12-12 19:48:31 -05:00
|
|
|
|
|
|
|
|
pub fn add_favorite(&mut self, id: String) -> anyhow::Result<()> {
|
|
|
|
|
if !self.favorites.contains(&id) {
|
|
|
|
|
self.favorites.push(id);
|
|
|
|
|
}
|
2022-12-13 22:46:30 -05:00
|
|
|
self.save()
|
2022-12-12 19:48:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn remove_favorite(&mut self, id: String) -> anyhow::Result<()> {
|
|
|
|
|
self.favorites.retain(|e| e != &id);
|
2022-12-13 22:46:30 -05:00
|
|
|
self.save()
|
2022-12-12 19:48:31 -05:00
|
|
|
}
|
|
|
|
|
|
2022-12-13 22:46:30 -05:00
|
|
|
// TODO async?
|
|
|
|
|
pub fn save(&self) -> anyhow::Result<()> {
|
|
|
|
|
let bd = BaseDirectories::new()?;
|
|
|
|
|
let mut relative_path = PathBuf::from(APP_ID);
|
|
|
|
|
relative_path.push("config.ron");
|
|
|
|
|
let config_path = bd.place_config_file(relative_path)?;
|
|
|
|
|
let f = File::create(config_path)?;
|
|
|
|
|
ron::ser::to_writer_pretty(f, self, Default::default())?;
|
|
|
|
|
Ok(())
|
2022-12-12 19:48:31 -05:00
|
|
|
}
|
2022-07-19 23:39:19 -04:00
|
|
|
}
|