2022-12-21 13:42:12 -08:00
|
|
|
use std::net::Ipv4Addr;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2023-04-27 14:24:08 -07:00
|
|
|
use auto_launch::AutoLaunchBuilder;
|
2022-12-22 16:36:32 -08:00
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
use sqlx::SqlitePool;
|
2022-12-21 13:42:12 -08:00
|
|
|
|
2022-12-22 16:36:32 -08:00
|
|
|
use crate::errors::*;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2022-12-21 13:42:12 -08:00
|
|
|
pub struct AppConfig {
|
2022-12-23 11:34:17 -08:00
|
|
|
#[serde(default = "default_listen_addr")]
|
2022-12-21 13:42:12 -08:00
|
|
|
pub listen_addr: Ipv4Addr,
|
2022-12-23 11:34:17 -08:00
|
|
|
#[serde(default = "default_listen_port")]
|
2022-12-21 13:42:12 -08:00
|
|
|
pub listen_port: u16,
|
2022-12-23 11:34:17 -08:00
|
|
|
#[serde(default = "default_rehide_ms")]
|
2022-12-22 16:36:32 -08:00
|
|
|
pub rehide_ms: u64,
|
2022-12-23 11:34:17 -08:00
|
|
|
#[serde(default = "default_start_minimized")]
|
|
|
|
pub start_minimized: bool,
|
2023-04-27 14:24:08 -07:00
|
|
|
#[serde(default = "default_start_on_login")]
|
|
|
|
pub start_on_login: bool,
|
2022-12-22 16:36:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-21 13:42:12 -08:00
|
|
|
impl Default for AppConfig {
|
|
|
|
fn default() -> Self {
|
2022-12-22 16:36:32 -08:00
|
|
|
AppConfig {
|
2022-12-23 11:34:17 -08:00
|
|
|
listen_addr: default_listen_addr(),
|
|
|
|
listen_port: default_listen_port(),
|
|
|
|
rehide_ms: default_rehide_ms(),
|
|
|
|
start_minimized: default_start_minimized(),
|
2023-04-27 14:24:08 -07:00
|
|
|
start_on_login: default_start_on_login(),
|
2022-12-21 13:42:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-26 15:49:08 -07:00
|
|
|
impl AppConfig {
|
|
|
|
pub async fn load(pool: &SqlitePool) -> Result<AppConfig, SetupError> {
|
|
|
|
let res = sqlx::query!("SELECT * from config where name = 'main'")
|
|
|
|
.fetch_optional(pool)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let row = match res {
|
|
|
|
Some(row) => row,
|
|
|
|
None => return Ok(AppConfig::default()),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(serde_json::from_str(&row.data)?)
|
|
|
|
}
|
2022-12-22 16:36:32 -08:00
|
|
|
|
2023-04-26 15:49:08 -07:00
|
|
|
pub async fn save(&self, pool: &SqlitePool) -> Result<(), sqlx::error::Error> {
|
|
|
|
let data = serde_json::to_string(self).unwrap();
|
|
|
|
sqlx::query(
|
|
|
|
"INSERT INTO config (name, data) VALUES ('main', ?)
|
|
|
|
ON CONFLICT (name) DO UPDATE SET data = ?"
|
|
|
|
)
|
|
|
|
.bind(&data)
|
|
|
|
.bind(&data)
|
|
|
|
.execute(pool)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-12-22 16:36:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-28 14:33:04 -07:00
|
|
|
pub fn set_auto_launch(is_configured: bool) -> Result<(), SetupError> {
|
2023-04-27 14:24:08 -07:00
|
|
|
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)
|
2023-04-28 22:34:17 -07:00
|
|
|
.build()?;
|
2023-04-27 14:24:08 -07:00
|
|
|
|
2023-04-28 14:33:04 -07:00
|
|
|
let is_enabled = auto.is_enabled()?;
|
|
|
|
if is_configured && !is_enabled {
|
2023-04-28 22:34:17 -07:00
|
|
|
auto.enable()?;
|
2023-04-27 14:24:08 -07:00
|
|
|
}
|
2023-04-28 14:33:04 -07:00
|
|
|
else if !is_configured && is_enabled {
|
2023-04-28 22:34:17 -07:00
|
|
|
auto.disable()?;
|
2023-04-27 14:24:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-28 22:34:17 -07:00
|
|
|
pub fn get_or_create_db_path() -> Result<PathBuf, DataDirError> {
|
2023-04-30 10:52:46 -07:00
|
|
|
// debug_assertions doesn't always mean we are running in dev
|
2023-04-29 10:01:45 -07:00
|
|
|
if cfg!(debug_assertions) && std::env::var("HOME").is_ok() {
|
2023-04-28 22:34:17 -07:00
|
|
|
return Ok(PathBuf::from("./creddy.db"));
|
2022-12-21 13:42:12 -08:00
|
|
|
}
|
|
|
|
|
2023-04-28 22:34:17 -07:00
|
|
|
let mut path = dirs::data_dir()
|
|
|
|
.ok_or(DataDirError::NotFound)?;
|
2022-12-21 13:42:12 -08:00
|
|
|
|
2023-04-28 22:34:17 -07:00
|
|
|
std::fs::create_dir_all(&path)?;
|
|
|
|
path.push("creddy.db");
|
|
|
|
|
|
|
|
Ok(path)
|
2022-12-22 16:36:32 -08:00
|
|
|
}
|
2022-12-23 11:34:17 -08:00
|
|
|
|
|
|
|
|
|
|
|
fn default_listen_port() -> u16 {
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
12_345
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
19_923
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_listen_addr() -> Ipv4Addr { Ipv4Addr::LOCALHOST }
|
|
|
|
fn default_rehide_ms() -> u64 { 1000 }
|
2023-04-27 14:24:08 -07:00
|
|
|
// 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) }
|