2025-02-11 23:15:24 -08:00
|
|
|
mod daemon;
|
2025-05-25 18:52:18 -07:00
|
|
|
mod dbus;
|
2025-02-11 23:15:24 -08:00
|
|
|
|
|
|
|
|
use log::LevelFilter;
|
2025-05-25 18:52:18 -07:00
|
|
|
use std::future;
|
2025-02-12 00:32:44 -08:00
|
|
|
|
2025-04-27 22:44:05 -07:00
|
|
|
use daemon::signals::Signal;
|
2025-05-26 15:49:29 -07:00
|
|
|
use daemon::Daemon;
|
2025-04-27 22:44:05 -07:00
|
|
|
|
2025-05-26 15:49:29 -07:00
|
|
|
use dbus::endpoint::DbusRegistry;
|
2025-04-25 18:02:54 -07:00
|
|
|
use dbus::interface;
|
|
|
|
|
use dbus::server_impl::ServerImpl;
|
2025-05-25 18:52:18 -07:00
|
|
|
use dbus_tokio::connection;
|
2025-02-12 00:32:44 -08:00
|
|
|
|
2025-02-11 23:15:24 -08:00
|
|
|
fn initialize_logging() {
|
2025-04-28 16:00:04 -07:00
|
|
|
// Weird: is this the best way to do this?
|
|
|
|
|
let log_level = std::env::var("RUST_LOG")
|
|
|
|
|
.map(|s| s.parse::<LevelFilter>().unwrap_or(LevelFilter::Info))
|
|
|
|
|
.unwrap_or(LevelFilter::Info);
|
|
|
|
|
|
2025-05-25 18:52:18 -07:00
|
|
|
env_logger::Builder::from_default_env()
|
2025-06-12 18:09:58 -07:00
|
|
|
.format_timestamp_millis()
|
2025-04-28 16:00:04 -07:00
|
|
|
.filter_level(log_level)
|
2025-02-11 23:15:24 -08:00
|
|
|
.init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() {
|
|
|
|
|
initialize_logging();
|
|
|
|
|
|
2025-04-25 16:54:37 -07:00
|
|
|
// Create the daemon
|
2025-04-27 12:53:45 -07:00
|
|
|
let mut daemon = Daemon::new()
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
log::error!("Failed to start daemon: {}", e);
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
2025-04-25 18:02:54 -07:00
|
|
|
|
2025-05-25 18:52:18 -07:00
|
|
|
// Initialize dbus session connection
|
|
|
|
|
let (resource, connection) = connection::new_session_sync().unwrap();
|
|
|
|
|
|
|
|
|
|
// The resource is a task that should be spawned onto a tokio compatible
|
|
|
|
|
// reactor ASAP. If the resource ever finishes, you lost connection to D-Bus.
|
|
|
|
|
//
|
|
|
|
|
// To shut down the connection, both call _handle.abort() and drop the connection.
|
|
|
|
|
let _handle = tokio::spawn(async {
|
|
|
|
|
let err = resource.await;
|
|
|
|
|
panic!("Lost connection to D-Bus: {}", err);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Acquire the name
|
|
|
|
|
connection
|
|
|
|
|
.request_name(interface::NAME, false, true, false)
|
|
|
|
|
.await
|
|
|
|
|
.expect("Unable to acquire dbus name");
|
|
|
|
|
|
2025-05-26 15:49:29 -07:00
|
|
|
// Create shared D-Bus registry
|
|
|
|
|
let dbus_registry = DbusRegistry::new(connection.clone());
|
2025-05-25 18:52:18 -07:00
|
|
|
|
2025-05-26 15:49:29 -07:00
|
|
|
// Create and register server implementation
|
2025-06-05 20:19:34 -07:00
|
|
|
let server = ServerImpl::new(daemon.event_sender.clone());
|
2025-05-26 15:49:29 -07:00
|
|
|
|
2025-06-06 16:39:31 -07:00
|
|
|
dbus_registry.register_object(interface::OBJECT_PATH, server, |cr| {
|
|
|
|
|
vec![
|
2025-05-26 15:49:29 -07:00
|
|
|
interface::register_net_buzzert_kordophone_repository(cr),
|
|
|
|
|
interface::register_net_buzzert_kordophone_settings(cr),
|
|
|
|
|
]
|
2025-06-06 16:39:31 -07:00
|
|
|
});
|
2025-02-11 23:15:24 -08:00
|
|
|
|
2025-04-27 22:44:05 -07:00
|
|
|
let mut signal_receiver = daemon.obtain_signal_receiver();
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
use dbus::interface::signals as DbusSignals;
|
|
|
|
|
|
|
|
|
|
while let Some(signal) = signal_receiver.recv().await {
|
|
|
|
|
match signal {
|
|
|
|
|
Signal::ConversationsUpdated => {
|
2025-05-03 22:13:03 -07:00
|
|
|
log::debug!("Sending signal: ConversationsUpdated");
|
2025-05-26 15:49:29 -07:00
|
|
|
dbus_registry
|
2025-05-25 18:52:18 -07:00
|
|
|
.send_signal(interface::OBJECT_PATH, DbusSignals::ConversationsUpdated {})
|
2025-04-27 22:44:05 -07:00
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
|
log::error!("Failed to send signal");
|
|
|
|
|
0
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-04-28 18:39:52 -07:00
|
|
|
|
|
|
|
|
Signal::MessagesUpdated(conversation_id) => {
|
2025-05-25 18:52:18 -07:00
|
|
|
log::debug!(
|
|
|
|
|
"Sending signal: MessagesUpdated for conversation {}",
|
|
|
|
|
conversation_id
|
|
|
|
|
);
|
2025-05-26 15:49:29 -07:00
|
|
|
dbus_registry
|
2025-05-25 18:52:18 -07:00
|
|
|
.send_signal(
|
|
|
|
|
interface::OBJECT_PATH,
|
|
|
|
|
DbusSignals::MessagesUpdated { conversation_id },
|
|
|
|
|
)
|
2025-04-28 18:39:52 -07:00
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
|
log::error!("Failed to send signal");
|
|
|
|
|
0
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-06-06 20:02:09 -07:00
|
|
|
|
|
|
|
|
Signal::AttachmentDownloaded(attachment_id) => {
|
|
|
|
|
log::debug!("Sending signal: AttachmentDownloaded for attachment {}", attachment_id);
|
|
|
|
|
dbus_registry
|
|
|
|
|
.send_signal(interface::OBJECT_PATH, DbusSignals::AttachmentDownloadCompleted { attachment_id })
|
|
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
|
log::error!("Failed to send signal");
|
|
|
|
|
0
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-06-12 17:58:03 -07:00
|
|
|
|
|
|
|
|
Signal::AttachmentUploaded(upload_guid, attachment_guid) => {
|
|
|
|
|
log::debug!("Sending signal: AttachmentUploaded for upload {}, attachment {}", upload_guid, attachment_guid);
|
|
|
|
|
dbus_registry
|
|
|
|
|
.send_signal(interface::OBJECT_PATH, DbusSignals::AttachmentUploadCompleted { upload_guid, attachment_guid })
|
|
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
|
log::error!("Failed to send signal");
|
|
|
|
|
0
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-04-27 22:44:05 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-27 12:53:45 -07:00
|
|
|
daemon.run().await;
|
2025-04-25 21:42:29 -07:00
|
|
|
|
2025-02-11 23:15:24 -08:00
|
|
|
future::pending::<()>().await;
|
|
|
|
|
unreachable!()
|
|
|
|
|
}
|