using GLib; using Gee; public class Repository : Object { public signal void conversations_updated(); 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; } 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(); }); // 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); } } }