|
|
|
@ -1,12 +1,20 @@
|
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
use tauri::{
|
|
|
|
|
plugin::{Builder, TauriPlugin},
|
|
|
|
|
Manager, Runtime,
|
|
|
|
|
Manager, Runtime, api::path::config_dir,
|
|
|
|
|
};
|
|
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
|
|
fn fls() -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
|
|
|
struct AppConfig {
|
|
|
|
|
pub theme: ApplicationTheme
|
|
|
|
|
#[serde(skip, default="fls")]
|
|
|
|
|
pub write_file: bool,
|
|
|
|
|
pub theme: ApplicationTheme,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
|
|
@ -16,16 +24,77 @@ enum ApplicationTheme {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AppConfig {
|
|
|
|
|
pub fn load() -> Self {
|
|
|
|
|
println!("Mock loading config");
|
|
|
|
|
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
AppConfig {
|
|
|
|
|
theme: ApplicationTheme::Light,
|
|
|
|
|
write_file: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn try_load() -> Option<Self> {
|
|
|
|
|
let path = config_dir()?.join("longmont/client.json");
|
|
|
|
|
|
|
|
|
|
let read_result = fs::read_to_string(path.clone());
|
|
|
|
|
if let Err(error) = read_result {
|
|
|
|
|
println!("Unable to read file {}", path.to_str()?);
|
|
|
|
|
println!("{}", error);
|
|
|
|
|
return None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let parse_result = serde_json::from_str(read_result.unwrap().as_str());
|
|
|
|
|
if let Err(error) = parse_result {
|
|
|
|
|
println!("Invalid syntax in config file {}", path.to_str()?);
|
|
|
|
|
println!("{}", error);
|
|
|
|
|
return None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Some(parse_result.unwrap())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn load() -> Self {
|
|
|
|
|
// Check for config file
|
|
|
|
|
let path = config_dir().expect("cannot find config dir").join("longmont/client.json");
|
|
|
|
|
if !path.exists() {
|
|
|
|
|
println!("No config file found, using defaults");
|
|
|
|
|
let mut config = AppConfig::default();
|
|
|
|
|
config.write_file = true;
|
|
|
|
|
return config
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we can read it, we'll allow writing it back
|
|
|
|
|
if let Some(parsed_config) = AppConfig::try_load() {
|
|
|
|
|
println!("Loaded config file {}", path.to_str().unwrap());
|
|
|
|
|
let mut config = parsed_config.clone();
|
|
|
|
|
config.write_file = true;
|
|
|
|
|
return config
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Otherwise, load defaults and don't allow writing it back
|
|
|
|
|
println!("Error encountered, using defaults");
|
|
|
|
|
AppConfig::default()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn save(&self) {
|
|
|
|
|
println!("Mock saving config")
|
|
|
|
|
if !self.write_file {
|
|
|
|
|
println!("Did not initially load config from a file, not saving");
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let dir_path = config_dir().expect("cannot find config dir").join("longmont");
|
|
|
|
|
let path = dir_path.join("client.json");
|
|
|
|
|
|
|
|
|
|
if !dir_path.exists() {
|
|
|
|
|
fs::create_dir(dir_path).expect("could not create config dir");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let config_str = serde_json::to_string(self).expect("Cannot serialize config");
|
|
|
|
|
|
|
|
|
|
if let Err(err) = fs::write(path.clone(), config_str) {
|
|
|
|
|
println!("Could not write config file {}", path.to_str().unwrap());
|
|
|
|
|
println!("{}", err);
|
|
|
|
|
} else {
|
|
|
|
|
println!("Saved config to {}", path.to_str().unwrap())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|