Private
Public Access
1
0

kordophoned: better daemon bootstrapping

This commit is contained in:
2025-04-25 16:54:37 -07:00
parent b1f171136a
commit 0c6b55fa38
7 changed files with 202 additions and 67 deletions

View File

@@ -1,9 +1,46 @@
use directories::ProjectDirs;
use std::path::PathBuf;
use anyhow::Result;
use kordophone_db::{
database::Database,
settings::Settings,
models::Conversation,
};
pub struct Daemon {
pub version: String,
database: Database,
}
impl Daemon {
pub fn new() -> Self {
Self { version: "0.1.0".to_string() }
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())?;
Ok(Self { version: "0.1.0".to_string(), database })
}
pub fn get_version(&self) -> String {
self.version.clone()
}
pub fn get_conversations(&mut self) -> Vec<Conversation> {
self.database.with_repository(|r| r.all_conversations().unwrap())
}
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")
}
}
}

View File

@@ -1,5 +1,5 @@
use log::info;
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use crate::{daemon::Daemon, dbus::interface};
use dbus_crossroads::Crossroads;
@@ -13,11 +13,11 @@ use dbus::{
pub struct Endpoint {
connection: Arc<SyncConnection>,
daemon: Arc<Daemon>,
daemon: Arc<Mutex<Daemon>>,
}
impl Endpoint {
pub fn new(daemon: Arc<Daemon>) -> Self {
pub fn new(daemon: Daemon) -> Self {
let (resource, connection) = connection::new_session_sync().unwrap();
// The resource is a task that should be spawned onto a tokio compatible
@@ -29,7 +29,10 @@ impl Endpoint {
panic!("Lost connection to D-Bus: {}", err);
});
Self { connection, daemon }
Self {
connection,
daemon: Arc::new(Mutex::new(daemon))
}
}
pub async fn start(&self) {

View File

@@ -1,16 +1,30 @@
use dbus::arg;
use dbus_tree::MethodErr;
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use crate::daemon::Daemon;
use crate::dbus::interface::NetBuzzertKordophoneServer as DbusServer;
impl DbusServer for Arc<Daemon> {
impl DbusServer for Arc<Mutex<Daemon>> {
fn get_version(&mut self) -> Result<String, MethodErr> {
Ok(self.version.clone())
let daemon = self.lock().map_err(|_| MethodErr::failed("Failed to lock daemon"))?;
Ok(daemon.version.clone())
}
fn get_conversations(&mut self) -> Result<Vec<arg::PropMap>, dbus::MethodErr> {
todo!()
// Get a repository instance and use it to fetch conversations
let mut daemon = self.lock().map_err(|_| MethodErr::failed("Failed to lock daemon"))?;
let conversations = daemon.get_conversations();
// Convert conversations to DBus property maps
let result = conversations.into_iter().map(|conv| {
let mut map = arg::PropMap::new();
map.insert("guid".into(), arg::Variant(Box::new(conv.guid)));
map.insert("display_name".into(), arg::Variant(Box::new(conv.display_name.unwrap_or_default())));
map.insert("unread_count".into(), arg::Variant(Box::new(conv.unread_count as i32)));
map
}).collect();
Ok(result)
}
}

View File

@@ -2,7 +2,6 @@ mod dbus;
mod daemon;
use std::future;
use std::sync::Arc;
use log::LevelFilter;
use daemon::Daemon;
@@ -19,9 +18,15 @@ fn initialize_logging() {
async fn main() {
initialize_logging();
// Daemon is stored in an Arc so it can be shared with other endpoints eventually.
let daemon = Arc::new(Daemon::new());
// Create the daemon
let daemon = Daemon::new()
.map_err(|e| {
log::error!("Failed to start daemon: {}", e);
std::process::exit(1);
})
.unwrap();
// Create the D-Bus endpoint
let endpoint = DbusEndpoint::new(daemon);
endpoint.start().await;