2025-04-30 15:19:44 -07:00
|
|
|
using GLib;
|
|
|
|
|
using Gee;
|
|
|
|
|
|
|
|
|
|
public class Repository : Object
|
|
|
|
|
{
|
|
|
|
|
public signal void conversations_updated();
|
2025-04-30 15:58:47 -07:00
|
|
|
public signal void messages_updated(string conversation_guid);
|
2025-04-30 15:19:44 -07:00
|
|
|
|
|
|
|
|
public static Repository get_instance() {
|
|
|
|
|
if (instance == null) {
|
|
|
|
|
instance = new Repository();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Repository instance = null;
|
|
|
|
|
private DBusService.Repository dbus_repository;
|
|
|
|
|
private uint dbus_watch_id;
|
|
|
|
|
|
|
|
|
|
private Repository() {
|
|
|
|
|
connect_to_dbus.begin((obj, res) => {
|
|
|
|
|
connect_to_dbus.end(res);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~Repository() {
|
|
|
|
|
if (dbus_watch_id > 0) {
|
|
|
|
|
Bus.unwatch_name(dbus_watch_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Conversation[] get_conversations() throws Error {
|
|
|
|
|
if (dbus_repository == null) {
|
|
|
|
|
throw new Error(1337, 1, "Repository not connected");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var conversations = dbus_repository.get_conversations();
|
|
|
|
|
Conversation[] returned_conversations = new Conversation[conversations.length];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < conversations.length; i++) {
|
|
|
|
|
returned_conversations[i] = new Conversation.from_hash_table(conversations[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return returned_conversations;
|
|
|
|
|
}
|
2025-04-30 15:58:47 -07:00
|
|
|
|
|
|
|
|
public Message[] get_messages(string conversation_guid, string last_message_id = "") throws Error {
|
|
|
|
|
if (dbus_repository == null) {
|
|
|
|
|
throw new Error(1337, 1, "Repository not connected");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var messages = dbus_repository.get_messages(conversation_guid, last_message_id);
|
|
|
|
|
Message[] returned_messages = new Message[messages.length];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < messages.length; i++) {
|
|
|
|
|
returned_messages[i] = new Message.from_hash_table(messages[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return returned_messages;
|
|
|
|
|
}
|
2025-04-30 15:19:44 -07:00
|
|
|
|
|
|
|
|
private async void connect_to_dbus() {
|
|
|
|
|
bool connected = false;
|
|
|
|
|
const string path = "/net/buzzert/kordophonecd/daemon";
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
debug("Trying to connect to DBus service at path: %s", path);
|
|
|
|
|
dbus_repository = yield Bus.get_proxy(BusType.SESSION,
|
|
|
|
|
"net.buzzert.kordophonecd",
|
|
|
|
|
path);
|
|
|
|
|
|
|
|
|
|
// Test the connection
|
|
|
|
|
dbus_repository.get_version();
|
|
|
|
|
|
|
|
|
|
// If we get here, connection succeeded
|
|
|
|
|
debug("Connected to DBus service at path: %s", path);
|
|
|
|
|
connected = true;
|
|
|
|
|
|
|
|
|
|
// Listen for updates
|
|
|
|
|
dbus_repository.conversations_updated.connect(() => {
|
|
|
|
|
conversations_updated();
|
|
|
|
|
});
|
2025-04-30 15:58:47 -07:00
|
|
|
|
|
|
|
|
dbus_repository.messages_updated.connect((conversation_guid) => {
|
|
|
|
|
messages_updated(conversation_guid);
|
|
|
|
|
});
|
2025-04-30 15:19:44 -07:00
|
|
|
|
|
|
|
|
// Initial load
|
|
|
|
|
conversations_updated();
|
|
|
|
|
} catch (Error e) {
|
|
|
|
|
debug("Failed to connect to kordophonecd at %s: %s", path, e.message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!connected) {
|
|
|
|
|
warning("Failed to connect to kordophonecd on any known path");
|
|
|
|
|
|
|
|
|
|
// Watch for the service to appear
|
|
|
|
|
dbus_watch_id = Bus.watch_name(BusType.SESSION,
|
|
|
|
|
"net.buzzert.kordophonecd",
|
|
|
|
|
BusNameWatcherFlags.AUTO_START,
|
|
|
|
|
() => {
|
|
|
|
|
connect_to_dbus.begin();
|
|
|
|
|
},
|
|
|
|
|
null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|