210 lines
5.3 KiB
Rust
210 lines
5.3 KiB
Rust
use std::path::PathBuf;
|
|
use std::time::Duration;
|
|
|
|
use auto_launch::AutoLaunchBuilder;
|
|
use is_terminal::IsTerminal;
|
|
use serde::{Serialize, Deserialize};
|
|
use sqlx::SqlitePool;
|
|
|
|
use crate::errors::*;
|
|
use crate::kv;
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
pub struct TermConfig {
|
|
pub name: String,
|
|
// we call it exec because it isn't always the actual path,
|
|
// in some cases it's just the name and relies on path-searching
|
|
// it's a string because it can come from the frontend as json
|
|
pub exec: String,
|
|
pub args: Vec<String>,
|
|
}
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
|
|
pub struct Hotkey {
|
|
pub keys: String,
|
|
pub enabled: bool,
|
|
}
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
|
|
pub struct HotkeysConfig {
|
|
// tauri uses strings to represent keybinds, so we will as well
|
|
pub show_window: Hotkey,
|
|
pub launch_terminal: Hotkey,
|
|
}
|
|
|
|
impl HotkeysConfig {
|
|
pub fn disable_all(&mut self) {
|
|
self.show_window.enabled = false;
|
|
self.launch_terminal.enabled = false;
|
|
}
|
|
}
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
pub struct AppConfig {
|
|
#[serde(default = "default_rehide_ms")]
|
|
pub rehide_ms: u64,
|
|
#[serde(default = "default_auto_lock")]
|
|
pub auto_lock: bool,
|
|
#[serde(default = "default_lock_after")]
|
|
pub lock_after: Duration,
|
|
#[serde(default = "default_start_minimized")]
|
|
pub start_minimized: bool,
|
|
#[serde(default = "default_start_on_login")]
|
|
pub start_on_login: bool,
|
|
#[serde(default = "default_term_config")]
|
|
pub terminal: TermConfig,
|
|
#[serde(default = "default_hotkey_config")]
|
|
pub hotkeys: HotkeysConfig,
|
|
}
|
|
|
|
|
|
impl Default for AppConfig {
|
|
fn default() -> Self {
|
|
AppConfig {
|
|
rehide_ms: default_rehide_ms(),
|
|
auto_lock: default_auto_lock(),
|
|
lock_after: default_lock_after(),
|
|
start_minimized: default_start_minimized(),
|
|
start_on_login: default_start_on_login(),
|
|
terminal: default_term_config(),
|
|
hotkeys: default_hotkey_config(),
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
impl AppConfig {
|
|
pub async fn load(pool: &SqlitePool) -> Result<AppConfig, LoadKvError> {
|
|
let config = kv::load(pool, "config")
|
|
.await?
|
|
.unwrap_or_else(|| AppConfig::default());
|
|
|
|
Ok(config)
|
|
}
|
|
|
|
pub async fn save(&self, pool: &SqlitePool) -> Result<(), sqlx::error::Error> {
|
|
kv::save(pool, "config", self).await
|
|
}
|
|
}
|
|
|
|
|
|
pub fn set_auto_launch(is_configured: bool) -> Result<(), SetupError> {
|
|
let path_buf = std::env::current_exe()
|
|
.map_err(|e| auto_launch::Error::Io(e))?;
|
|
let path = path_buf
|
|
.to_string_lossy();
|
|
|
|
let auto = AutoLaunchBuilder::new()
|
|
.set_app_name("Creddy")
|
|
.set_app_path(&path)
|
|
.build()?;
|
|
|
|
let is_enabled = auto.is_enabled()?;
|
|
if is_configured && !is_enabled {
|
|
auto.enable()?;
|
|
}
|
|
else if !is_configured && is_enabled {
|
|
auto.disable()?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
|
|
pub fn get_or_create_db_path() -> Result<PathBuf, DataDirError> {
|
|
let mut path = dirs::data_dir()
|
|
.ok_or(DataDirError::NotFound)?;
|
|
path.push("Creddy");
|
|
|
|
std::fs::create_dir_all(&path)?;
|
|
if cfg!(debug_assertions) && std::io::stdout().is_terminal() {
|
|
path.push("creddy.dev.db");
|
|
}
|
|
else {
|
|
path.push("creddy.db");
|
|
}
|
|
|
|
Ok(path)
|
|
}
|
|
|
|
|
|
fn default_term_config() -> TermConfig {
|
|
#[cfg(windows)]
|
|
{
|
|
let shell = if which::which("pwsh.exe").is_ok() {
|
|
"pwsh.exe".to_string()
|
|
}
|
|
else {
|
|
"powershell.exe".to_string()
|
|
};
|
|
|
|
let (exec, args) = if cfg!(debug_assertions) {
|
|
("conhost.exe".to_string(), vec![shell.clone()])
|
|
} else {
|
|
(shell.clone(), vec![])
|
|
};
|
|
|
|
TermConfig { name: shell, exec, args }
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
{
|
|
for bin in ["gnome-terminal", "konsole"] {
|
|
if let Ok(_) = which::which(bin) {
|
|
return TermConfig {
|
|
name: bin.into(),
|
|
exec: bin.into(),
|
|
args: vec![],
|
|
}
|
|
}
|
|
}
|
|
return TermConfig {
|
|
name: "gnome-terminal".into(),
|
|
exec: "gnome-terminal".into(),
|
|
args: vec![],
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
fn default_hotkey_config() -> HotkeysConfig {
|
|
HotkeysConfig {
|
|
show_window: Hotkey {keys: "alt+shift+C".into(), enabled: true},
|
|
launch_terminal: Hotkey {keys: "alt+shift+T".into(), enabled: true},
|
|
}
|
|
}
|
|
|
|
|
|
fn default_rehide_ms() -> u64 { 1000 }
|
|
fn default_auto_lock() -> bool { true }
|
|
fn default_lock_after() -> Duration { Duration::from_secs(43200) }
|
|
// start minimized and on login only in production mode
|
|
fn default_start_minimized() -> bool { !cfg!(debug_assertions) }
|
|
fn default_start_on_login() -> bool { !cfg!(debug_assertions) }
|
|
|
|
|
|
// struct DurationVisitor;
|
|
|
|
// impl<'de> Visitor<'de> for DurationVisitor {
|
|
// type Value = Duration;
|
|
|
|
// fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
|
|
// write!(formatter, "an integer between 0 and 2^64 - 1")
|
|
// }
|
|
|
|
// fn visit_u64<E: de::Error>(self, v: u64) -> Result<Duration, E> {
|
|
// Ok(Duration::from_secs(v))
|
|
// }
|
|
// }
|
|
|
|
|
|
// fn duration_from_secs<'de, D>(deserializer: D) -> Result<Duration, D::Error>
|
|
// where D: Deserializer<'de>
|
|
// {
|
|
// deserializer.deserialize_u64(DurationVisitor)
|
|
// }
|