2025-04-25 16:54:37 -07:00
|
|
|
use directories::ProjectDirs;
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use anyhow::Result;
|
2025-04-25 18:02:54 -07:00
|
|
|
use thiserror::Error;
|
2025-04-25 16:54:37 -07:00
|
|
|
use kordophone_db::{
|
|
|
|
|
database::Database,
|
|
|
|
|
models::Conversation,
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-25 18:02:54 -07:00
|
|
|
use kordophone::api::http_client::HTTPAPIClient;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
|
pub enum DaemonError {
|
|
|
|
|
#[error("Client Not Configured")]
|
|
|
|
|
ClientNotConfigured,
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 23:15:24 -08:00
|
|
|
pub struct Daemon {
|
|
|
|
|
pub version: String,
|
2025-04-25 16:54:37 -07:00
|
|
|
database: Database,
|
2025-04-25 18:02:54 -07:00
|
|
|
client: Option<HTTPAPIClient>,
|
2025-02-11 23:15:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Daemon {
|
2025-04-25 16:54:37 -07:00
|
|
|
pub fn new() -> Result<Self> {
|
|
|
|
|
let database_path = Self::get_database_path();
|
|
|
|
|
log::info!("Database path: {}", database_path.display());
|
|
|
|
|
|
|
|
|
|
// Create the database directory if it doesn't exist
|
|
|
|
|
let database_dir = database_path.parent().unwrap();
|
|
|
|
|
std::fs::create_dir_all(database_dir)?;
|
|
|
|
|
|
|
|
|
|
let database = Database::new(&database_path.to_string_lossy())?;
|
|
|
|
|
|
2025-04-25 18:02:54 -07:00
|
|
|
// TODO: Check to see if we have client settings in the database
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(Self { version: "0.1.0".to_string(), database, client: None })
|
2025-04-25 16:54:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_conversations(&mut self) -> Vec<Conversation> {
|
|
|
|
|
self.database.with_repository(|r| r.all_conversations().unwrap())
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-25 18:02:54 -07:00
|
|
|
pub fn sync_all_conversations(&mut self) -> Result<()> {
|
|
|
|
|
let client = self.client
|
|
|
|
|
.as_mut()
|
|
|
|
|
.ok_or(DaemonError::ClientNotConfigured)?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-25 16:54:37 -07:00
|
|
|
fn get_database_path() -> PathBuf {
|
|
|
|
|
if let Some(proj_dirs) = ProjectDirs::from("com", "kordophone", "kordophone") {
|
|
|
|
|
let data_dir = proj_dirs.data_dir();
|
|
|
|
|
data_dir.join("database.db")
|
|
|
|
|
} else {
|
|
|
|
|
// Fallback to a local path if we can't get the system directories
|
|
|
|
|
PathBuf::from("database.db")
|
|
|
|
|
}
|
2025-02-11 23:15:24 -08:00
|
|
|
}
|
|
|
|
|
}
|