From b7ca33a37de5dd2ecffa7575681c6009996b7ba2 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Tue, 1 Feb 2022 17:38:15 +0100 Subject: Initialize and use dbus interfaces async related #1155 --- main/src/ui/application.vala | 20 ++++++++++++---- main/src/ui/notifier_freedesktop.vala | 43 ++++++++++++----------------------- 2 files changed, 29 insertions(+), 34 deletions(-) (limited to 'main') diff --git a/main/src/ui/application.vala b/main/src/ui/application.vala index 4f97b7b3..62f01bcb 100644 --- a/main/src/ui/application.vala +++ b/main/src/ui/application.vala @@ -47,11 +47,21 @@ public class Dino.Ui.Application : Gtk.Application, Dino.Application { } NotificationEvents notification_events = stream_interactor.get_module(NotificationEvents.IDENTITY); - notification_events.register_notification_provider(new GNotificationsNotifier(stream_interactor)); - FreeDesktopNotifier? free_desktop_notifier = FreeDesktopNotifier.try_create(stream_interactor); - if (free_desktop_notifier != null) { - notification_events.register_notification_provider(free_desktop_notifier); - } + get_notifications_dbus.begin((_, res) => { + // It might take a bit to get the interface. NotificationEvents will queue any notifications in the meantime. + try { + DBusNotifications? dbus_notifications = get_notifications_dbus.end(res); + if (dbus_notifications != null) { + FreeDesktopNotifier free_desktop_notifier = new FreeDesktopNotifier(stream_interactor, dbus_notifications); + notification_events.register_notification_provider.begin(free_desktop_notifier); + } else { + notification_events.register_notification_provider.begin(new GNotificationsNotifier(stream_interactor)); + } + } catch (Error e) { + debug("Failed accessing fdo notification server: %s", e.message); + } + }); + notification_events.notify_content_item.connect((content_item, conversation) => { // Set urgency hint also if (normal) notifications are disabled // Don't set urgency hint in GNOME, produces "Window is active" notification diff --git a/main/src/ui/notifier_freedesktop.vala b/main/src/ui/notifier_freedesktop.vala index e8e2ba1d..dfc5b28b 100644 --- a/main/src/ui/notifier_freedesktop.vala +++ b/main/src/ui/notifier_freedesktop.vala @@ -17,16 +17,16 @@ public class Dino.Ui.FreeDesktopNotifier : NotificationProvider, Object { private HashMap> action_listeners = new HashMap>(); private HashMap call_notifications = new HashMap(Call.hash_func, Call.equals_func); - private FreeDesktopNotifier(StreamInteractor stream_interactor) { + public FreeDesktopNotifier(StreamInteractor stream_interactor, DBusNotifications dbus_notifications) { this.stream_interactor = stream_interactor; + this.dbus_notifications = dbus_notifications; + init_dbus_notifications.begin(); } - private void set_dbus_notifications(DBusNotifications dbus_notifications) throws Error { - this.dbus_notifications = dbus_notifications; - + private async void init_dbus_notifications() throws Error { string[] caps; - dbus_notifications.get_capabilities(out caps); + yield dbus_notifications.get_capabilities(out caps); foreach (string cap in caps) { switch (cap) { case "body-markup": @@ -49,21 +49,6 @@ public class Dino.Ui.FreeDesktopNotifier : NotificationProvider, Object { }); } - public static FreeDesktopNotifier? try_create(StreamInteractor stream_interactor) { - DBusNotifications? dbus_notifications = get_notifications_dbus(); - if (dbus_notifications == null) return null; - - try { - FreeDesktopNotifier notifier = new FreeDesktopNotifier(stream_interactor); - notifier.set_dbus_notifications(dbus_notifications); - return notifier; - } catch (Error e) { - debug("Failed accessing fdo notification server: %s", e.message); - } - - return null; - } - public double get_priority() { return 1; } @@ -111,7 +96,7 @@ public class Dino.Ui.FreeDesktopNotifier : NotificationProvider, Object { hash_table["desktop-entry"] = new Variant.string(Dino.Application.get_default().get_application_id()); string[] actions = new string[] {"default", "Open conversation"}; try { - uint32 notification_id = dbus_notifications.notify("Dino", replace_id, "", conversation_display_name, body, actions, hash_table, -1); + uint32 notification_id = yield dbus_notifications.notify("Dino", replace_id, "", conversation_display_name, body, actions, hash_table, -1); content_notifications[conversation] = notification_id; add_action_listener(notification_id, "default", () => { @@ -134,7 +119,7 @@ public class Dino.Ui.FreeDesktopNotifier : NotificationProvider, Object { hash_table["desktop-entry"] = new Variant.string(Dino.Application.get_default().get_application_id()); string[] actions = new string[] {"default", "Open conversation", "reject", _("Reject"), "accept", _("Accept")}; try { - uint32 notification_id = dbus_notifications.notify("Dino", 0, "", summary, body, actions, hash_table, 0); + uint32 notification_id = yield dbus_notifications.notify("Dino", 0, "", summary, body, actions, hash_table, 0); call_notifications[call] = notification_id; add_action_listener(notification_id, "default", () => { @@ -157,7 +142,7 @@ public class Dino.Ui.FreeDesktopNotifier : NotificationProvider, Object { if (!call_notifications.has_key(call)) return; uint32 notification_id = call_notifications[call]; try { - dbus_notifications.close_notification(notification_id); + yield dbus_notifications.close_notification(notification_id); action_listeners.unset(notification_id); call_notifications.unset(call); } catch (Error e) { } @@ -172,7 +157,7 @@ public class Dino.Ui.FreeDesktopNotifier : NotificationProvider, Object { hash_table["desktop-entry"] = new Variant.string(Dino.Application.get_default().get_application_id()); string[] actions = new string[] {"default", "Open conversation", "accept", _("Accept"), "deny", _("Deny")}; try { - uint32 notification_id = dbus_notifications.notify("Dino", 0, "", summary, body, actions, hash_table, -1); + uint32 notification_id = yield dbus_notifications.notify("Dino", 0, "", summary, body, actions, hash_table, -1); if (!conversation_notifications.has_key(conversation)) { conversation_notifications[conversation] = new ArrayList(); @@ -210,7 +195,7 @@ public class Dino.Ui.FreeDesktopNotifier : NotificationProvider, Object { HashTable hash_table = new HashTable(null, null); hash_table["desktop-entry"] = new Variant.string(Dino.Application.get_default().get_application_id()); try { - dbus_notifications.notify("Dino", 0, "im.dino.Dino", summary, body, new string[]{}, hash_table, -1); + yield dbus_notifications.notify("Dino", 0, "im.dino.Dino", summary, body, new string[]{}, hash_table, -1); } catch (Error e) { warning("Failed showing connection error notification: %s", e.message); } @@ -232,7 +217,7 @@ public class Dino.Ui.FreeDesktopNotifier : NotificationProvider, Object { string[] actions = new string[] {"default", "", "reject", _("Reject"), "accept", _("Accept")}; try { - uint32 notification_id = dbus_notifications.notify("Dino", 0, "", summary, body, actions, hash_table, -1); + uint32 notification_id = yield dbus_notifications.notify("Dino", 0, "", summary, body, actions, hash_table, -1); Conversation group_conversation = stream_interactor.get_module(ConversationManager.IDENTITY).create_conversation(room_jid, account, Conversation.Type.GROUPCHAT); add_action_listener(notification_id, "default", () => { @@ -265,7 +250,7 @@ public class Dino.Ui.FreeDesktopNotifier : NotificationProvider, Object { string[] actions = new string[] {"deny", _("Deny"), "accept", _("Accept")}; try { - uint32 notification_id = dbus_notifications.notify("Dino", 0, "", summary, body, actions, hash_table, -1); + uint32 notification_id = yield dbus_notifications.notify("Dino", 0, "", summary, body, actions, hash_table, -1); add_action_listener(notification_id, "accept", () => { GLib.Application.get_default().activate_action("accept-voice-request", new Variant.int32(conversation.id)); @@ -282,7 +267,7 @@ public class Dino.Ui.FreeDesktopNotifier : NotificationProvider, Object { if (content_notifications != null) { foreach (uint32 id in content_notifications.values) { try { - dbus_notifications.close_notification(id); + yield dbus_notifications.close_notification(id); } catch (Error e) { } } content_notifications.clear(); @@ -292,7 +277,7 @@ public class Dino.Ui.FreeDesktopNotifier : NotificationProvider, Object { public async void retract_conversation_notifications(Conversation conversation) { if (content_notifications.has_key(conversation)) { try { - dbus_notifications.close_notification(content_notifications[conversation]); + yield dbus_notifications.close_notification(content_notifications[conversation]); } catch (Error e) { } } content_notifications.unset(conversation); -- cgit v1.2.3-70-g09d2