diff options
author | fiaxh <git@lightrise.org> | 2020-11-20 15:21:34 +0100 |
---|---|---|
committer | fiaxh <git@lightrise.org> | 2020-11-20 15:21:34 +0100 |
commit | 11d9855a3994bc24ff67f5c2c4933c1d9559f6c5 (patch) | |
tree | 29407df7d6cfb36f82580de5f39f0bbf9c28d372 /main | |
parent | 07917f1d841f449157aa3aaa2507b0547dd274e7 (diff) | |
download | dino-11d9855a3994bc24ff67f5c2c4933c1d9559f6c5.tar.gz dino-11d9855a3994bc24ff67f5c2c4933c1d9559f6c5.zip |
Refactor Notifications, add freedesktop backend
fixes #707
Diffstat (limited to 'main')
-rw-r--r-- | main/CMakeLists.txt | 3 | ||||
-rw-r--r-- | main/src/ui/application.vala | 12 | ||||
-rw-r--r-- | main/src/ui/notifications.vala | 174 | ||||
-rw-r--r-- | main/src/ui/notifier_freedesktop.vala | 268 | ||||
-rw-r--r-- | main/src/ui/notifier_gnotifications.vala | 169 | ||||
-rw-r--r-- | main/src/ui/util/helper.vala | 94 |
6 files changed, 463 insertions, 257 deletions
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index c70539f6..5975da8b 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -107,7 +107,8 @@ SOURCES src/ui/conversation_view_controller.vala src/ui/file_send_overlay.vala src/ui/global_search.vala - src/ui/notifications.vala + src/ui/notifier_freedesktop.vala + src/ui/notifier_gnotifications.vala src/ui/settings_dialog.vala src/ui/main_window.vala src/ui/main_window_controller.vala diff --git a/main/src/ui/application.vala b/main/src/ui/application.vala index d2b82969..17abddbc 100644 --- a/main/src/ui/application.vala +++ b/main/src/ui/application.vala @@ -5,7 +5,6 @@ using Dino.Ui; using Xmpp; public class Dino.Ui.Application : Gtk.Application, Dino.Application { - private Notifications notifications; private MainWindow window; public MainWindowController controller; @@ -29,8 +28,11 @@ public class Dino.Ui.Application : Gtk.Application, Dino.Application { create_actions(); startup.connect(() => { - notifications = new Notifications(stream_interactor); - notifications.start(); + stream_interactor.get_module(NotificationEvents.IDENTITY).register_notification_provider(new GNotificationsNotifier(stream_interactor)); + FreeDesktopNotifier free_desktop_notifier = FreeDesktopNotifier.try_create(stream_interactor); + if (free_desktop_notifier != null) { + stream_interactor.get_module(NotificationEvents.IDENTITY).register_notification_provider(free_desktop_notifier); + } }); activate.connect(() => { @@ -40,8 +42,6 @@ public class Dino.Ui.Application : Gtk.Application, Dino.Application { window = new MainWindow(this, stream_interactor, db, config); controller.set_window(window); if ((get_flags() & ApplicationFlags.IS_SERVICE) == ApplicationFlags.IS_SERVICE) window.delete_event.connect(window.hide_on_delete); - - notifications.conversation_selected.connect((conversation) => controller.select_conversation(conversation)); } window.present(); }); @@ -99,7 +99,7 @@ public class Dino.Ui.Application : Gtk.Application, Dino.Application { open_conversation_action.activate.connect((variant) => { Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation_by_id(variant.get_int32()); if (conversation != null) controller.select_conversation(conversation); - window.present(); + Util.present_window(window); }); add_action(open_conversation_action); diff --git a/main/src/ui/notifications.vala b/main/src/ui/notifications.vala deleted file mode 100644 index 134c757a..00000000 --- a/main/src/ui/notifications.vala +++ /dev/null @@ -1,174 +0,0 @@ -using Gee; - -using Dino.Entities; -using Xmpp; - -namespace Dino.Ui { - -public class Notifications : Object { - - public signal void conversation_selected(Conversation conversation); - - private StreamInteractor stream_interactor; - private HashMap<Conversation, Notification> notifications = new HashMap<Conversation, Notification>(Conversation.hash_func, Conversation.equals_func); - private Set<string>? active_conversation_ids = null; - private Set<string>? active_ids = new HashSet<string>(); - - public Notifications(StreamInteractor stream_interactor) { - this.stream_interactor = stream_interactor; - - stream_interactor.get_module(ChatInteraction.IDENTITY).focused_in.connect((focused_conversation) => { - if (active_conversation_ids == null) { - Gee.List<Conversation> conversations = stream_interactor.get_module(ConversationManager.IDENTITY).get_active_conversations(); - foreach (Conversation conversation in conversations) { - GLib.Application.get_default().withdraw_notification(conversation.id.to_string()); - } - active_conversation_ids = new HashSet<string>(); - } else { - foreach (string id in active_conversation_ids) { - GLib.Application.get_default().withdraw_notification(id); - } - active_conversation_ids.clear(); - } - - string subscription_id = focused_conversation.id.to_string() + "-subscription"; - if (active_ids.contains(subscription_id)) { - GLib.Application.get_default().withdraw_notification(subscription_id); - } - }); - } - - public void start() { - stream_interactor.get_module(NotificationEvents.IDENTITY).notify_content_item.connect((content_item, conversation) => notify_content_item.begin(content_item, conversation)); - stream_interactor.get_module(NotificationEvents.IDENTITY).notify_subscription_request.connect(notify_subscription_request); - stream_interactor.get_module(NotificationEvents.IDENTITY).notify_connection_error.connect(notify_connection_error); - stream_interactor.get_module(NotificationEvents.IDENTITY).notify_muc_invite.connect(on_invite_received); - stream_interactor.get_module(NotificationEvents.IDENTITY).notify_voice_request.connect(on_voice_request_received); - } - - private async void notify_content_item(ContentItem content_item, Conversation conversation) { - if (!notifications.has_key(conversation)) { - notifications[conversation] = new Notification(""); - notifications[conversation].set_default_action_and_target_value("app.open-conversation", new Variant.int32(conversation.id)); - } - string display_name = Util.get_conversation_display_name(stream_interactor, conversation); - string text = ""; - switch (content_item.type_) { - case MessageItem.TYPE: - Message message = ((MessageItem) content_item).message; - text = message.body; - break; - case FileItem.TYPE: - FileTransfer transfer = ((FileItem) content_item).file_transfer; - - bool file_is_image = transfer.mime_type != null && transfer.mime_type.has_prefix("image"); - if (transfer.direction == Message.DIRECTION_SENT) { - text = file_is_image ? _("Image sent") : _("File sent"); - } else { - text = file_is_image ? _("Image received") : _("File received"); - } - break; - } - if (stream_interactor.get_module(MucManager.IDENTITY).is_groupchat(conversation.counterpart, conversation.account)) { - string muc_occupant = Util.get_participant_display_name(stream_interactor, conversation, content_item.jid); - text = @"$muc_occupant: $text"; - } - notifications[conversation].set_title(display_name); - notifications[conversation].set_body(text); - try { - Cairo.ImageSurface conversation_avatar = (yield Util.get_conversation_avatar_drawer(stream_interactor, conversation)).size(40, 40).draw_image_surface(); - notifications[conversation].set_icon(get_pixbuf_icon(conversation_avatar)); - } catch (Error e) { } - GLib.Application.get_default().send_notification(conversation.id.to_string(), notifications[conversation]); - if (active_conversation_ids != null) { - active_conversation_ids.add(conversation.id.to_string()); - } - - // Don't set urgency hint in GNOME, produces "Window is active" notification - var desktop_env = Environment.get_variable("XDG_CURRENT_DESKTOP"); - if (desktop_env == null || !desktop_env.down().contains("gnome")) { - var app = (GLib.Application.get_default() as Application); - if (app.active_window != null) { - app.active_window.urgency_hint = true; - } - } - } - - private async void notify_subscription_request(Conversation conversation) { - Notification notification = new Notification(_("Subscription request")); - notification.set_body(conversation.counterpart.to_string()); - try { - Cairo.ImageSurface jid_avatar = (yield Util.get_conversation_avatar_drawer(stream_interactor, conversation)).size(40, 40).draw_image_surface(); - notification.set_icon(get_pixbuf_icon(jid_avatar)); - } catch (Error e) { } - notification.set_default_action_and_target_value("app.open-conversation", new Variant.int32(conversation.id)); - notification.add_button_with_target_value(_("Accept"), "app.accept-subscription", conversation.id); - notification.add_button_with_target_value(_("Deny"), "app.deny-subscription", conversation.id); - GLib.Application.get_default().send_notification(conversation.id.to_string() + "-subscription", notification); - active_ids.add(conversation.id.to_string() + "-subscription"); - } - - private void notify_connection_error(Account account, ConnectionManager.ConnectionError error) { - Notification notification = new Notification(_("Could not connect to %s").printf(account.bare_jid.domainpart)); - switch (error.source) { - case ConnectionManager.ConnectionError.Source.SASL: - notification.set_body(_("Wrong password")); - break; - case ConnectionManager.ConnectionError.Source.TLS: - notification.set_body(_("Invalid TLS certificate")); - break; - } - GLib.Application.get_default().send_notification(account.id.to_string() + "-connection-error", notification); - } - - private async void on_invite_received(Account account, Jid room_jid, Jid from_jid, string? password, string? reason) { - Conversation direct_conversation = new Conversation(from_jid, account, Conversation.Type.CHAT); - string display_name = Util.get_participant_display_name(stream_interactor, direct_conversation, from_jid); - string display_room = room_jid.bare_jid.to_string(); - Notification notification = new Notification(_("Invitation to %s").printf(display_room)); - string body = _("%s invited you to %s").printf(display_name, display_room); - notification.set_body(body); - - try { - Cairo.ImageSurface jid_avatar = (yield Util.get_conversation_avatar_drawer(stream_interactor, direct_conversation)).size(40, 40).draw_image_surface(); - notification.set_icon(get_pixbuf_icon(jid_avatar)); - } catch (Error e) { } - - Conversation group_conversation = stream_interactor.get_module(ConversationManager.IDENTITY).create_conversation(room_jid, account, Conversation.Type.GROUPCHAT); - notification.set_default_action_and_target_value("app.open-muc-join", new Variant.int32(group_conversation.id)); - notification.add_button_with_target_value(_("Deny"), "app.deny-invite", group_conversation.id); - notification.add_button_with_target_value(_("Accept"), "app.open-muc-join", group_conversation.id); - GLib.Application.get_default().send_notification(null, notification); - } - - 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; - - string display_name = Util.get_participant_display_name(stream_interactor, conversation, from_jid); - string display_room = Util.get_conversation_display_name(stream_interactor, conversation); - Notification notification = new Notification(_("Permission request")); - string body = _("%s requests the permission to write in %s").printf(display_name, display_room); - notification.set_body(body); - - try { - Cairo.ImageSurface jid_avatar = (yield Util.get_conversation_avatar_drawer(stream_interactor, conversation)).size(40, 40).draw_image_surface(); - notification.set_icon(get_pixbuf_icon(jid_avatar)); - } catch (Error e) { } - - Variant variant = new Variant.tuple(new Variant[] {new Variant.int32(conversation.id), new Variant.string(nick)}); - notification.set_default_action_and_target_value("app.accept-voice-request", variant); - notification.add_button_with_target_value(_("Deny"), "app.deny-voice-request", conversation.id); - notification.add_button_with_target_value(_("Accept"), "app.accept-voice-request", variant); - GLib.Application.get_default().send_notification(null, notification); - } - - private Icon get_pixbuf_icon(Cairo.ImageSurface surface) throws Error { - Gdk.Pixbuf avatar = Gdk.pixbuf_get_from_surface(surface, 0, 0, surface.get_width(), surface.get_height()); - uint8[] buffer; - avatar.save_to_buffer(out buffer, "png"); - return new BytesIcon(new Bytes(buffer)); - } -} - -} diff --git a/main/src/ui/notifier_freedesktop.vala b/main/src/ui/notifier_freedesktop.vala new file mode 100644 index 00000000..e660ee4e --- /dev/null +++ b/main/src/ui/notifier_freedesktop.vala @@ -0,0 +1,268 @@ +using Gee; + +using Dino.Entities; +using Xmpp; + +public class Dino.Ui.FreeDesktopNotifier : NotificationProvider, Object { + + public signal void conversation_selected(Conversation conversation); + + private StreamInteractor stream_interactor; + private DBusNotifications dbus_notifications; + private bool supports_body_markup = false; + + private HashMap<Conversation, uint32> content_notifications = new HashMap<Conversation, uint32>(Conversation.hash_func, Conversation.equals_func); + private HashMap<Conversation, Gee.List<uint32>> conversation_notifications = new HashMap<Conversation, Gee.List<uint32>>(Conversation.hash_func, Conversation.equals_func); + private HashMap<uint32, HashMap<string, ListenerFuncWrapper>> action_listeners = new HashMap<uint32, HashMap<string, ListenerFuncWrapper>>(); + + private FreeDesktopNotifier(StreamInteractor stream_interactor, DBusNotifications dbus_notifications) { + this.stream_interactor = stream_interactor; + this.dbus_notifications = dbus_notifications; + + try { + string[] caps; + dbus_notifications.get_capabilities(out caps); + foreach (string cap in caps) { + switch (cap) { + case "body-markup": + supports_body_markup = true; + break; + } + } + + dbus_notifications.action_invoked.connect((id, action) => { + if (action_listeners.has_key(id) && action_listeners[id].has_key(action)) { + action_listeners[id][action].func(); + } + }); + + dbus_notifications.notification_closed.connect((id) => { + action_listeners.unset(id); + }); + } catch (Error e) { + warning("Failed accessing fdo notification server: %s", e.message); + } + } + + public static FreeDesktopNotifier? try_create(StreamInteractor stream_interactor) { + DBusNotifications? dbus_notifications = get_notifications_dbus(); + if (dbus_notifications == null) return null; + + FreeDesktopNotifier notifier = new FreeDesktopNotifier(stream_interactor, dbus_notifications); + notifier.dbus_notifications = dbus_notifications; + + return notifier; + } + + public double get_priority() { + return 1; + } + + public async void notify_message(Message message, Conversation conversation, string conversation_display_name, string? participant_display_name) { + yield notify_content_item(conversation, conversation_display_name, participant_display_name, message.body); + } + + public async void notify_file(FileTransfer file_transfer, Conversation conversation, bool is_image, string conversation_display_name, string? participant_display_name) { + string text = ""; + if (file_transfer.direction == Message.DIRECTION_SENT) { + text = is_image ? _("Image sent") : _("File sent"); + } else { + text = is_image ? _("Image received") : _("File received"); + } + + if (supports_body_markup) { + text = "<i>" + text + "</i>"; + } + + yield notify_content_item(conversation, conversation_display_name, participant_display_name, text); + } + + private async void notify_content_item(Conversation conversation, string conversation_display_name, string? participant_display_name, string body_) { + string body = body_; + if (participant_display_name != null) { + if (supports_body_markup) { + body = @"<b>$(Markup.escape_text(participant_display_name)):</b> $(Markup.escape_text(body))"; + } else { + body = @"$participant_display_name: $body"; + } + } + + uint32 replace_id = content_notifications.has_key(conversation) ? content_notifications[conversation] : 0; + HashTable<string, Variant> hash_table = new HashTable<string, Variant>(null, null); + hash_table["image-data"] = yield get_conversation_icon(conversation); + string[] actions = new string[] {"default", "Open conversation"}; + try { + uint32 notification_id = dbus_notifications.notify("Dino", replace_id, "", conversation_display_name, body, actions, hash_table, 0); + content_notifications[conversation] = notification_id; + + add_action_listener(notification_id, "default", () => { + GLib.Application.get_default().activate_action("open-conversation", new Variant.int32(conversation.id)); + }); + } catch (Error e) { + warning("Failed showing content item notification: %s", e.message); + } + + // Don't set urgency hint in GNOME, produces "Window is active" notification + var desktop_env = Environment.get_variable("XDG_CURRENT_DESKTOP"); + if (desktop_env == null || !desktop_env.down().contains("gnome")) { + var app = (GLib.Application.get_default() as Application); + if (app.active_window != null) { + app.active_window.urgency_hint = true; + } + } + } + + public async void notify_subscription_request(Conversation conversation) { + string summary = _("Subscription request"); + string body = Markup.escape_text(conversation.counterpart.to_string()); + + HashTable<string, Variant> hash_table = new HashTable<string, Variant>(null, null); + hash_table["image-data"] = yield get_conversation_icon(conversation); + 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, 0); + + if (!conversation_notifications.has_key(conversation)) { + conversation_notifications[conversation] = new ArrayList<uint32>(); + } + conversation_notifications[conversation].add(notification_id); + + add_action_listener(notification_id, "default", () => { + GLib.Application.get_default().activate_action("open-conversation", new Variant.int32(conversation.id)); + }); + add_action_listener(notification_id, "accept", () => { + GLib.Application.get_default().activate_action("accept-subscription", new Variant.int32(conversation.id)); + }); + add_action_listener(notification_id, "deny", () => { + GLib.Application.get_default().activate_action("deny-subscription", new Variant.int32(conversation.id)); + }); + } catch (Error e) { + warning("Failed showing subscription request notification: %s", e.message); + } + } + + public async void notify_connection_error(Account account, ConnectionManager.ConnectionError error) { + string summary = _("Could not connect to %s").printf(account.bare_jid.domainpart); + string body = ""; + switch (error.source) { + case ConnectionManager.ConnectionError.Source.SASL: + body = _("Wrong password"); + break; + case ConnectionManager.ConnectionError.Source.TLS: + body = _("Invalid TLS certificate"); + break; + } + + HashTable<string, Variant> hash_table = new HashTable<string, Variant>(null, null); + try { + dbus_notifications.notify("Dino", 0, "im.dino.Dino", summary, body, new string[]{}, hash_table, 0); + } catch (Error e) { + warning("Failed showing connection error notification: %s", e.message); + } + } + + public async void notify_muc_invite(Account account, Jid room_jid, Jid from_jid, string inviter_display_name) { + Conversation direct_conversation = new Conversation(from_jid, account, Conversation.Type.CHAT); + + string display_room = room_jid.bare_jid.to_string(); + string summary = _("Invitation to %s").printf(display_room); + string body = _("%s invited you to %s").printf(inviter_display_name, display_room); + if (supports_body_markup) { + body = Markup.escape_text(body); + } + + HashTable<string, Variant> hash_table = new HashTable<string, Variant>(null, null); + hash_table["image-data"] = yield get_conversation_icon(direct_conversation); + string[] actions = new string[] {"default", "", "reject", _("Reject"), "accept", _("Accept")}; + + try { + uint32 notification_id = dbus_notifications.notify("Dino", 0, "", summary, body, actions, hash_table, 0); + + Conversation group_conversation = stream_interactor.get_module(ConversationManager.IDENTITY).create_conversation(room_jid, account, Conversation.Type.GROUPCHAT); + add_action_listener(notification_id, "default", () => { + GLib.Application.get_default().activate_action("open-muc-join", new Variant.int32(group_conversation.id)); + }); + add_action_listener(notification_id, "accept", () => { + GLib.Application.get_default().activate_action("deny-invite", new Variant.int32(group_conversation.id)); + }); + add_action_listener(notification_id, "deny", () => { + GLib.Application.get_default().activate_action("open-muc-join", new Variant.int32(group_conversation.id)); + }); + } catch (Error e) { + warning("Failed showing muc invite notification: %s", e.message); + } + } + + public async void notify_voice_request(Conversation conversation, Jid from_jid) { + + string display_name = Util.get_participant_display_name(stream_interactor, conversation, from_jid); + string display_room = Util.get_conversation_display_name(stream_interactor, conversation); + string summary = _("Permission request"); + string body = _("%s requests the permission to write in %s").printf(display_name, display_room); + if (supports_body_markup) { + Markup.escape_text(body); + } + + HashTable<string, Variant> hash_table = new HashTable<string, Variant>(null, null); + hash_table["image-data"] = yield get_conversation_icon(conversation); + string[] actions = new string[] {"deny", _("Deny"), "accept", _("Accept")}; + + try { + uint32 notification_id = dbus_notifications.notify("Dino", 0, "", summary, body, actions, hash_table, 0); + + add_action_listener(notification_id, "accept", () => { + GLib.Application.get_default().activate_action("deny-invite", new Variant.int32(conversation.id)); + }); + add_action_listener(notification_id, "deny", () => { + GLib.Application.get_default().activate_action("open-muc-join", new Variant.int32(conversation.id)); + }); + } catch (Error e) { + warning("Failed showing voice request notification: %s", e.message); + } + } + + public async void retract_content_item_notifications() { + if (content_notifications != null) { + foreach (uint32 id in content_notifications.values) { + try { + dbus_notifications.close_notification(id); + } catch (Error e) { } + } + content_notifications.clear(); + } + } + + public async void retract_conversation_notifications(Conversation conversation) { + if (content_notifications.has_key(conversation)) { + try { + dbus_notifications.close_notification(content_notifications[conversation]); + } catch (Error e) { } + } + content_notifications.unset(conversation); + } + + private async Variant get_conversation_icon(Conversation conversation) { + AvatarDrawer drawer = yield Util.get_conversation_avatar_drawer(stream_interactor, conversation); + Cairo.ImageSurface surface = drawer.size(40, 40).draw_image_surface(); + Gdk.Pixbuf avatar = Gdk.pixbuf_get_from_surface(surface, 0, 0, surface.get_width(), surface.get_height()); + var bytes = avatar.pixel_bytes; + var image_bytes = Variant.new_from_data<Bytes>(new VariantType("ay"), bytes.get_data(), true, bytes); + return new Variant("(iiibii@ay)", avatar.width, avatar.height, avatar.rowstride, avatar.has_alpha, avatar.bits_per_sample, avatar.n_channels, image_bytes); + } + + private void add_action_listener(uint32 id, string name, owned ListenerFunc func) { + if (!action_listeners.has_key(id)) { + action_listeners[id] = new HashMap<string, ListenerFuncWrapper>(); + } + action_listeners[id][name] = new ListenerFuncWrapper((owned) func); + } + + delegate void ListenerFunc(); + class ListenerFuncWrapper { + public ListenerFunc func; + + public ListenerFuncWrapper(owned ListenerFunc func) { + this.func = (owned) func; + } + } +}
\ No newline at end of file diff --git a/main/src/ui/notifier_gnotifications.vala b/main/src/ui/notifier_gnotifications.vala new file mode 100644 index 00000000..a7aab753 --- /dev/null +++ b/main/src/ui/notifier_gnotifications.vala @@ -0,0 +1,169 @@ +using Gee; + +using Dino.Entities; +using Xmpp; + +namespace Dino.Ui { + + public class GNotificationsNotifier : NotificationProvider, Object { + + public signal void conversation_selected(Conversation conversation); + + private StreamInteractor stream_interactor; + private HashMap<Conversation, Notification> notifications = new HashMap<Conversation, Notification>(Conversation.hash_func, Conversation.equals_func); + private Set<string>? active_conversation_ids = null; + private Set<string>? active_ids = new HashSet<string>(); + + public GNotificationsNotifier(StreamInteractor stream_interactor) { + this.stream_interactor = stream_interactor; + } + + public double get_priority() { + return 0; + } + + public async void notify_message(Message message, Conversation conversation, string conversation_display_name, string? participant_display_name) { + string text = message.body; + if (participant_display_name != null) { + text = @"$participant_display_name: $text"; + } + yield notify_content_item(conversation, conversation_display_name, text); + } + + public async void notify_file(FileTransfer file_transfer, Conversation conversation, bool is_image, string conversation_display_name, string? participant_display_name) { + string text = ""; + if (file_transfer.direction == Message.DIRECTION_SENT) { + text = is_image ? _("Image sent") : _("File sent"); + } else { + text = is_image ? _("Image received") : _("File received"); + } + + if (participant_display_name != null) { + text = @"$participant_display_name: $text"; + } + + yield notify_content_item(conversation, conversation_display_name, text); + } + + private async void notify_content_item(Conversation conversation, string title, string body) { + if (!notifications.has_key(conversation)) { + notifications[conversation] = new Notification(""); + notifications[conversation].set_default_action_and_target_value("app.open-conversation", new Variant.int32(conversation.id)); + } + Notification notification = notifications[conversation]; + + notification.set_title(title); + notification.set_body(body); + try { + notification.set_icon(yield get_conversation_icon(conversation)); + } catch (Error e) { } + + GLib.Application.get_default().send_notification(conversation.id.to_string(), notifications[conversation]); + + if (active_conversation_ids != null) { + active_conversation_ids.add(conversation.id.to_string()); + } + + // Don't set urgency hint in GNOME, produces "Window is active" notification + var desktop_env = Environment.get_variable("XDG_CURRENT_DESKTOP"); + if (desktop_env == null || !desktop_env.down().contains("gnome")) { + var app = (GLib.Application.get_default() as Application); + if (app.active_window != null) { + app.active_window.urgency_hint = true; + } + } + } + + public async void notify_subscription_request(Conversation conversation) { + Notification notification = new Notification(_("Subscription request")); + notification.set_body(conversation.counterpart.to_string()); + try { + notification.set_icon(yield get_conversation_icon(conversation)); + } catch (Error e) { } + notification.set_default_action_and_target_value("app.open-conversation", new Variant.int32(conversation.id)); + notification.add_button_with_target_value(_("Accept"), "app.accept-subscription", conversation.id); + notification.add_button_with_target_value(_("Deny"), "app.deny-subscription", conversation.id); + GLib.Application.get_default().send_notification(conversation.id.to_string() + "-subscription", notification); + active_ids.add(conversation.id.to_string() + "-subscription"); + } + + public async void notify_connection_error(Account account, ConnectionManager.ConnectionError error) { + Notification notification = new Notification(_("Could not connect to %s").printf(account.bare_jid.domainpart)); + switch (error.source) { + case ConnectionManager.ConnectionError.Source.SASL: + notification.set_body("Wrong password"); + break; + case ConnectionManager.ConnectionError.Source.TLS: + notification.set_body("Invalid TLS certificate"); + break; + } + GLib.Application.get_default().send_notification(account.id.to_string() + "-connection-error", notification); + } + + public async void notify_muc_invite(Account account, Jid room_jid, Jid from_jid, string inviter_display_name) { + Conversation direct_conversation = new Conversation(from_jid, account, Conversation.Type.CHAT); + + string display_room = room_jid.bare_jid.to_string(); + Notification notification = new Notification(_("Invitation to %s").printf(display_room)); + string body = _("%s invited you to %s").printf(inviter_display_name, display_room); + notification.set_body(body); + + try { + notification.set_icon(yield get_conversation_icon(direct_conversation)); + } catch (Error e) { } + + Conversation group_conversation = stream_interactor.get_module(ConversationManager.IDENTITY).create_conversation(room_jid, account, Conversation.Type.GROUPCHAT); + notification.set_default_action_and_target_value("app.open-muc-join", new Variant.int32(group_conversation.id)); + notification.add_button_with_target_value(_("Deny"), "app.deny-invite", group_conversation.id); + notification.add_button_with_target_value(_("Accept"), "app.open-muc-join", group_conversation.id); + GLib.Application.get_default().send_notification(null, notification); + } + + public async void notify_voice_request(Conversation conversation, Jid from_jid) { + string display_name = Util.get_participant_display_name(stream_interactor, conversation, from_jid); + string display_room = Util.get_conversation_display_name(stream_interactor, conversation); + Notification notification = new Notification(_("Permission request")); + string body = _("%s requests the permission to write in %s").printf(display_name, display_room); + notification.set_body(body); + + try { + notification.set_icon(yield get_conversation_icon(conversation)); + } catch (Error e) { } + + notification.add_button_with_target_value(_("Deny"), "app.deny-voice-request", conversation.id); + notification.add_button_with_target_value(_("Accept"), "app.accept-voice-request", conversation.id); + GLib.Application.get_default().send_notification(null, notification); + } + + public async void retract_content_item_notifications() { + if (active_conversation_ids == null) { + Gee.List<Conversation> conversations = stream_interactor.get_module(ConversationManager.IDENTITY).get_active_conversations(); + foreach (Conversation conversation in conversations) { + GLib.Application.get_default().withdraw_notification(conversation.id.to_string()); + } + active_conversation_ids = new HashSet<string>(); + } else { + foreach (string id in active_conversation_ids) { + GLib.Application.get_default().withdraw_notification(id); + } + active_conversation_ids.clear(); + } + } + + public async void retract_conversation_notifications(Conversation conversation) { + string subscription_id = conversation.id.to_string() + "-subscription"; + if (active_ids.contains(subscription_id)) { + GLib.Application.get_default().withdraw_notification(subscription_id); + } + } + + private async Icon get_conversation_icon(Conversation conversation) throws Error { + AvatarDrawer drawer = yield Util.get_conversation_avatar_drawer(stream_interactor, conversation); + Cairo.ImageSurface surface = drawer.size(40, 40).draw_image_surface(); + Gdk.Pixbuf avatar = Gdk.pixbuf_get_from_surface(surface, 0, 0, surface.get_width(), surface.get_height()); + uint8[] buffer; + avatar.save_to_buffer(out buffer, "png"); + return new BytesIcon(new Bytes(buffer)); + } + } +} diff --git a/main/src/ui/util/helper.vala b/main/src/ui/util/helper.vala index 5d6d7bf5..b147e5d7 100644 --- a/main/src/ui/util/helper.vala +++ b/main/src/ui/util/helper.vala @@ -115,94 +115,23 @@ public static async AvatarDrawer get_conversation_participants_avatar_drawer(Str } public static string get_conversation_display_name(StreamInteractor stream_interactor, Conversation conversation) { - if (conversation.type_ == Conversation.Type.CHAT) { - string? display_name = get_real_display_name(stream_interactor, conversation.account, conversation.counterpart); - if (display_name != null) return display_name; - return conversation.counterpart.to_string(); - } - if (conversation.type_ == Conversation.Type.GROUPCHAT) { - return get_groupchat_display_name(stream_interactor, conversation.account, conversation.counterpart); - } - if (conversation.type_ == Conversation.Type.GROUPCHAT_PM) { - return _("%s from %s").printf(get_occupant_display_name(stream_interactor, conversation, conversation.counterpart), get_groupchat_display_name(stream_interactor, conversation.account, conversation.counterpart.bare_jid)); - } - return conversation.counterpart.to_string(); + return Dino.get_conversation_display_name(stream_interactor, conversation, _("%s from %s")); } public static string get_participant_display_name(StreamInteractor stream_interactor, Conversation conversation, Jid participant, bool me_is_me = false) { - if (me_is_me) { - if (conversation.account.bare_jid.equals_bare(participant) || - (conversation.type_ == Conversation.Type.GROUPCHAT || conversation.type_ == Conversation.Type.GROUPCHAT_PM) && - conversation.nickname != null && participant.equals_bare(conversation.counterpart) && conversation.nickname == participant.resourcepart) { - return _("Me"); - } - } - if (conversation.type_ == Conversation.Type.CHAT) { - return get_real_display_name(stream_interactor, conversation.account, participant, me_is_me) ?? participant.bare_jid.to_string(); - } - if ((conversation.type_ == Conversation.Type.GROUPCHAT || conversation.type_ == Conversation.Type.GROUPCHAT_PM) && conversation.counterpart.equals_bare(participant)) { - return get_occupant_display_name(stream_interactor, conversation, participant); - } - return participant.bare_jid.to_string(); + return Dino.get_participant_display_name(stream_interactor, conversation, participant, me_is_me ? _("Me") : null); } private static string? get_real_display_name(StreamInteractor stream_interactor, Account account, Jid jid, bool me_is_me = false) { - if (jid.equals_bare(account.bare_jid)) { - if (me_is_me || account.alias == null || account.alias.length == 0) { - return _("Me"); - } - return account.alias; - } - Roster.Item roster_item = stream_interactor.get_module(RosterManager.IDENTITY).get_roster_item(account, jid); - if (roster_item != null && roster_item.name != null && roster_item.name != "") { - return roster_item.name; - } - return null; + return Dino.get_real_display_name(stream_interactor, account, jid, me_is_me ? _("Me") : null); } private static string get_groupchat_display_name(StreamInteractor stream_interactor, Account account, Jid jid) { - MucManager muc_manager = stream_interactor.get_module(MucManager.IDENTITY); - string? room_name = muc_manager.get_room_name(account, jid); - if (room_name != null && room_name != jid.localpart) { - return room_name; - } - if (muc_manager.is_private_room(account, jid)) { - Gee.List<Jid>? other_occupants = muc_manager.get_other_offline_members(jid, account); - if (other_occupants != null && other_occupants.size > 0) { - var builder = new StringBuilder (); - foreach(Jid occupant in other_occupants) { - if (builder.len != 0) { - builder.append(", "); - } - builder.append((get_real_display_name(stream_interactor, account, occupant) ?? occupant.localpart ?? occupant.domainpart).split(" ")[0]); - } - return builder.str; - } - } - return jid.to_string(); + return Dino.get_groupchat_display_name(stream_interactor, account, jid); } private static string get_occupant_display_name(StreamInteractor stream_interactor, Conversation conversation, Jid jid, bool me_is_me = false, bool muc_real_name = false) { - if (muc_real_name) { - MucManager muc_manager = stream_interactor.get_module(MucManager.IDENTITY); - if (muc_manager.is_private_room(conversation.account, jid.bare_jid)) { - Jid? real_jid = muc_manager.get_real_jid(jid, conversation.account); - if (real_jid != null) { - string? display_name = get_real_display_name(stream_interactor, conversation.account, real_jid, me_is_me); - if (display_name != null) return display_name; - } - } - } - - // If it's us (jid=our real full JID), display our nick - if (conversation.type_ == Conversation.Type.GROUPCHAT_PM && conversation.account.bare_jid.equals_bare(jid)) { - var muc_conv = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation(conversation.counterpart.bare_jid, conversation.account, Conversation.Type.GROUPCHAT); - if (muc_conv != null && muc_conv.nickname != null) { - return muc_conv.nickname; - } - } - - return jid.resourcepart ?? jid.to_string(); + return Dino.get_occupant_display_name(stream_interactor, conversation, jid, me_is_me ? _("Me") : null); } public static void image_set_from_scaled_pixbuf(Image image, Gdk.Pixbuf pixbuf, int scale = 0, int width = 0, int height = 0) { @@ -440,6 +369,19 @@ public string summarize_whitespaces_to_space(string s) { } } +public void present_window(Window window) { +#if GDK3_WITH_X11 + Gdk.X11.Window x11window = window.get_window() as Gdk.X11.Window; + if (x11window != null) { + window.present_with_time(Gdk.X11.get_server_time(x11window)); + } else { + window.present(); + } +#else + window.present(); +#endif +} + public bool use_csd() { return ((Application) GLib.Application.get_default()).use_csd(); } |