Initialize and use dbus interfaces async

related #1155
This commit is contained in:
fiaxh 2022-02-01 17:38:15 +01:00
parent 1e63cb3bd9
commit b7ca33a37d
6 changed files with 92 additions and 76 deletions

View file

@ -5,14 +5,13 @@ public interface Login1Manager : Object {
public signal void PrepareForSleep(bool suspend);
}
public static Login1Manager? get_login1() {
Login1Manager? login1 = null;
public static async Login1Manager? get_login1() {
try {
login1 = Bus.get_proxy_sync(BusType.SYSTEM, "org.freedesktop.login1", "/org/freedesktop/login1");
return yield Bus.get_proxy(BusType.SYSTEM, "org.freedesktop.login1", "/org/freedesktop/login1");
} catch (IOError e) {
stderr.printf("%s\n", e.message);
}
return login1;
return null;
}
}

View file

@ -7,23 +7,22 @@ namespace Dino {
public signal void notification_closed (uint32 id, uint32 reason);
public abstract uint32 notify(string app_name, uint32 replaces_id, string app_icon, string summary,
public abstract async uint32 notify(string app_name, uint32 replaces_id, string app_icon, string summary,
string body, string[] actions, HashTable<string, Variant> hints, int32 expire_timeout) throws DBusError, IOError;
public abstract void get_capabilities(out string[] capabilities) throws Error;
public abstract async void get_capabilities(out string[] capabilities) throws Error;
public abstract void close_notification(uint id) throws DBusError, IOError;
public abstract async void close_notification(uint id) throws DBusError, IOError;
public abstract void get_server_information(out string name, out string vendor, out string version, out string spec_version) throws DBusError, IOError;
public abstract async void get_server_information(out string name, out string vendor, out string version, out string spec_version) throws DBusError, IOError;
}
public static DBusNotifications? get_notifications_dbus() {
DBusNotifications? upower = null;
public static async DBusNotifications? get_notifications_dbus() {
try {
upower = Bus.get_proxy_sync(BusType.SESSION, "org.freedesktop.Notifications", "/org/freedesktop/Notifications");
return yield Bus.get_proxy(BusType.SESSION, "org.freedesktop.Notifications", "/org/freedesktop/Notifications");
} catch (IOError e) {
warning("Couldn't get org.freedesktop.Notifications DBus instance: %s\n", e.message);
}
return upower;
return null;
}
}

View file

@ -104,10 +104,14 @@ public class ConnectionManager : Object {
network_monitor.network_changed.connect(on_network_changed);
network_monitor.notify["connectivity"].connect(on_network_changed);
}
login1 = get_login1();
get_login1.begin((_, res) => {
login1 = get_login1.end(res);
if (login1 != null) {
login1.PrepareForSleep.connect(on_prepare_for_sleep);
}
});
Timeout.add_seconds(60, () => {
foreach (Account account in connections.keys) {
if (connections[account].last_activity != null &&

View file

@ -12,7 +12,9 @@ public class NotificationEvents : StreamInteractionModule, Object {
public signal void notify_content_item(ContentItem content_item, Conversation conversation);
private StreamInteractor stream_interactor;
private NotificationProvider? notifier;
private Future<NotificationProvider> notifier;
private Promise<NotificationProvider> notifier_promise;
private bool notifier_outstanding = true;
public static void start(StreamInteractor stream_interactor) {
NotificationEvents m = new NotificationEvents(stream_interactor);
@ -22,24 +24,28 @@ public class NotificationEvents : StreamInteractionModule, Object {
public NotificationEvents(StreamInteractor stream_interactor) {
this.stream_interactor = stream_interactor;
stream_interactor.get_module(ContentItemStore.IDENTITY).new_item.connect(on_content_item_received);
stream_interactor.get_module(PresenceManager.IDENTITY).received_subscription_request.connect(on_received_subscription_request);
stream_interactor.get_module(ContentItemStore.IDENTITY).new_item.connect((item, conversation) => on_content_item_received.begin(item, conversation));
stream_interactor.get_module(PresenceManager.IDENTITY).received_subscription_request.connect((jid, account) => on_received_subscription_request.begin(jid, account));
stream_interactor.get_module(MucManager.IDENTITY).invite_received.connect(on_invite_received);
stream_interactor.get_module(MucManager.IDENTITY).voice_request_received.connect(on_voice_request_received);
stream_interactor.get_module(MucManager.IDENTITY).invite_received.connect((account, room_jid, from_jid, password, reason) => on_invite_received.begin(account, room_jid, from_jid, password, reason));
stream_interactor.get_module(MucManager.IDENTITY).voice_request_received.connect((account, room_jid, from_jid, nick) => on_voice_request_received.begin(account, room_jid, from_jid, nick));
stream_interactor.get_module(Calls.IDENTITY).call_incoming.connect(on_call_incoming);
stream_interactor.connection_manager.connection_error.connect((account, error) => notifier.notify_connection_error.begin(account, error));
stream_interactor.get_module(ChatInteraction.IDENTITY).focused_in.connect(on_focused_in);
stream_interactor.get_module(Calls.IDENTITY).call_incoming.connect((call, state, conversation, video) => on_call_incoming.begin(call, state, conversation, video));
stream_interactor.connection_manager.connection_error.connect((account, error) => on_connection_error(account, error));
stream_interactor.get_module(ChatInteraction.IDENTITY).focused_in.connect((conversation) => on_focused_in.begin(conversation));
notifier_promise = new Promise<NotificationProvider>();
notifier = notifier_promise.future;
}
public void register_notification_provider(NotificationProvider notification_provider) {
if (notifier == null || notifier.get_priority() < notification_provider.get_priority()) {
notifier = notification_provider;
public async void register_notification_provider(NotificationProvider notification_provider) {
if (notifier_outstanding || (yield notifier.wait_async()).get_priority() < notification_provider.get_priority()) {
notifier_outstanding = false;
notifier_promise.set_value(notification_provider);
}
}
private void on_content_item_received(ContentItem item, Conversation conversation) {
private async void on_content_item_received(ContentItem item, Conversation conversation) {
ContentItem last_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_latest(conversation);
if (item.id != last_item.id) return;
@ -71,7 +77,8 @@ public class NotificationEvents : StreamInteractionModule, Object {
notify_content_item(item, conversation);
if (notify != Conversation.NotifySetting.OFF) {
notifier.notify_message.begin(message, conversation, conversation_display_name, participant_display_name);
NotificationProvider notifier = yield notifier.wait_async();
yield notifier.notify_message(message, conversation, conversation_display_name, participant_display_name);
}
break;
case FileItem.TYPE:
@ -84,7 +91,8 @@ public class NotificationEvents : StreamInteractionModule, Object {
notify_content_item(item, conversation);
if (notify != Conversation.NotifySetting.OFF) {
notifier.notify_file.begin(file_transfer, conversation, is_image, conversation_display_name, participant_display_name);
NotificationProvider notifier = yield notifier.wait_async();
yield notifier.notify_file(file_transfer, conversation, is_image, conversation_display_name, participant_display_name);
}
break;
case CallItem.TYPE:
@ -93,23 +101,27 @@ public class NotificationEvents : StreamInteractionModule, Object {
}
}
private void on_voice_request_received(Account account, Jid room_jid, Jid from_jid, string nick) {
private async void on_voice_request_received(Account account, Jid room_jid, Jid from_jid, string nick) {
Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation(room_jid, account, Conversation.Type.GROUPCHAT);
if (conversation == null) return;
notifier.notify_voice_request.begin(conversation, from_jid);
NotificationProvider notifier = yield notifier.wait_async();
yield notifier.notify_voice_request(conversation, from_jid);
}
private void on_received_subscription_request(Jid jid, Account account) {
private async void on_received_subscription_request(Jid jid, Account account) {
Conversation conversation = stream_interactor.get_module(ConversationManager.IDENTITY).create_conversation(jid, account, Conversation.Type.CHAT);
if (stream_interactor.get_module(ChatInteraction.IDENTITY).is_active_focus(conversation)) return;
notifier.notify_subscription_request.begin(conversation);
NotificationProvider notifier = yield notifier.wait_async();
yield notifier.notify_subscription_request(conversation);
}
private void on_call_incoming(Call call, CallState call_state, Conversation conversation, bool video) {
private async void on_call_incoming(Call call, CallState call_state, Conversation conversation, bool video) {
string conversation_display_name = get_conversation_display_name(stream_interactor, conversation, null);
notifier.notify_call.begin(call, conversation, video, conversation_display_name);
NotificationProvider notifier = yield notifier.wait_async();
yield notifier.notify_call(call, conversation, video, conversation_display_name);
call.notify["state"].connect(() => {
if (call.state != Call.State.RINGING) {
notifier.retract_call_notification.begin(call, conversation);
@ -117,7 +129,7 @@ public class NotificationEvents : StreamInteractionModule, Object {
});
}
private void on_invite_received(Account account, Jid room_jid, Jid from_jid, string? password, string? reason) {
private async void on_invite_received(Account account, Jid room_jid, Jid from_jid, string? password, string? reason) {
string inviter_display_name;
if (room_jid.equals_bare(from_jid)) {
Conversation conversation = new Conversation(room_jid, account, Conversation.Type.GROUPCHAT);
@ -126,12 +138,19 @@ public class NotificationEvents : StreamInteractionModule, Object {
Conversation direct_conversation = new Conversation(from_jid, account, Conversation.Type.CHAT);
inviter_display_name = get_participant_display_name(stream_interactor, direct_conversation, from_jid);
}
notifier.notify_muc_invite.begin(account, room_jid, from_jid, inviter_display_name);
NotificationProvider notifier = yield notifier.wait_async();
yield notifier.notify_muc_invite(account, room_jid, from_jid, inviter_display_name);
}
private void on_focused_in(Conversation conversation) {
notifier.retract_content_item_notifications.begin();
notifier.retract_conversation_notifications.begin(conversation);
private async void on_connection_error(Account account, ConnectionManager.ConnectionError error) {
NotificationProvider notifier = yield notifier.wait_async();
yield notifier.notify_connection_error(account, error);
}
private async void on_focused_in(Conversation conversation) {
NotificationProvider notifier = yield notifier.wait_async();
yield notifier.retract_content_item_notifications();
yield notifier.retract_conversation_notifications(conversation);
}
}

View file

@ -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

View file

@ -17,16 +17,16 @@ public class Dino.Ui.FreeDesktopNotifier : NotificationProvider, Object {
private HashMap<uint32, HashMap<string, ListenerFuncWrapper>> action_listeners = new HashMap<uint32, HashMap<string, ListenerFuncWrapper>>();
private HashMap<Call, uint32> call_notifications = new HashMap<Call, uint32>(Call.hash_func, Call.equals_func);
private FreeDesktopNotifier(StreamInteractor stream_interactor) {
public FreeDesktopNotifier(StreamInteractor stream_interactor, DBusNotifications dbus_notifications) {
this.stream_interactor = stream_interactor;
}
private void set_dbus_notifications(DBusNotifications dbus_notifications) throws Error {
this.dbus_notifications = dbus_notifications;
init_dbus_notifications.begin();
}
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<uint32>();
@ -210,7 +195,7 @@ public class Dino.Ui.FreeDesktopNotifier : NotificationProvider, Object {
HashTable<string, Variant> hash_table = new HashTable<string, Variant>(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);