2025-04-30 15:19:44 -07:00
|
|
|
using GLib;
|
|
|
|
|
using Gee;
|
|
|
|
|
|
2025-05-03 01:11:26 -07:00
|
|
|
public class Repository : DBusServiceBase {
|
2025-04-30 15:19:44 -07:00
|
|
|
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;
|
2025-05-03 01:11:26 -07:00
|
|
|
private DBusService.Repository? dbus_repository;
|
2025-04-30 15:19:44 -07:00
|
|
|
|
|
|
|
|
private Repository() {
|
2025-05-03 01:11:26 -07:00
|
|
|
base();
|
2025-04-30 15:19:44 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-03 01:11:26 -07:00
|
|
|
protected override string get_service_name() {
|
|
|
|
|
return "Repository";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Object? get_proxy() throws Error {
|
|
|
|
|
dbus_repository = yield Bus.get_proxy(BusType.SESSION, DBUS_NAME, DBUS_PATH);
|
|
|
|
|
dbus_repository.get_version(); // Test the connection
|
|
|
|
|
return dbus_repository;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void setup_signals() {
|
|
|
|
|
dbus_repository.conversations_updated.connect(() => {
|
|
|
|
|
conversations_updated();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
dbus_repository.messages_updated.connect((conversation_guid) => {
|
|
|
|
|
messages_updated(conversation_guid);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Initial load
|
|
|
|
|
conversations_updated();
|
2025-04-30 15:19:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Conversation[] get_conversations() throws Error {
|
2025-05-03 01:11:26 -07:00
|
|
|
if (!is_connected || dbus_repository == null) {
|
|
|
|
|
throw create_not_connected_error();
|
2025-04-30 15:19:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2025-05-03 01:11:26 -07:00
|
|
|
if (!is_connected || dbus_repository == null) {
|
|
|
|
|
throw create_not_connected_error();
|
2025-04-30 15:58:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-05-02 15:09:12 -07:00
|
|
|
|
|
|
|
|
public string send_message(string conversation_guid, string message) throws Error {
|
2025-05-03 01:11:26 -07:00
|
|
|
if (!is_connected || dbus_repository == null) {
|
|
|
|
|
throw create_not_connected_error();
|
2025-05-02 15:09:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dbus_repository.send_message(conversation_guid, message);
|
|
|
|
|
}
|
2025-04-30 15:19:44 -07:00
|
|
|
}
|