Refactor Notifications, add freedesktop backend

fixes #707
This commit is contained in:
fiaxh 2020-11-20 15:21:34 +01:00
parent 07917f1d84
commit 11d9855a39
10 changed files with 665 additions and 283 deletions

View file

@ -11,6 +11,7 @@ SOURCES
src/application.vala
src/dbus/login1.vala
src/dbus/notifications.vala
src/dbus/upower.vala
src/entity/account.vala
@ -49,6 +50,7 @@ SOURCES
src/service/stream_interactor.vala
src/service/util.vala
src/util/display_name.vala
src/util/util.vala
src/util/weak_map.vala
CUSTOM_VAPIS

View file

@ -0,0 +1,29 @@
namespace Dino {
[DBus (name = "org.freedesktop.Notifications")]
public interface DBusNotifications : GLib.Object {
public signal void action_invoked(uint32 key, string action_key);
public signal void notification_closed (uint32 id, uint32 reason);
public abstract 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 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 static DBusNotifications? get_notifications_dbus() {
DBusNotifications? upower = null;
try {
upower = Bus.get_proxy_sync(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;
}
}

View file

@ -16,6 +16,7 @@ public class NotificationEvents : StreamInteractionModule, Object {
public signal void notify_voice_request(Account account, Jid room_jid, Jid from_jid, string nick);
private StreamInteractor stream_interactor;
private NotificationProvider? notifier;
public static void start(StreamInteractor stream_interactor) {
NotificationEvents m = new NotificationEvents(stream_interactor);
@ -27,55 +28,102 @@ public class NotificationEvents : StreamInteractionModule, Object {
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(MucManager.IDENTITY).invite_received.connect((account, room_jid, from_jid, password, reason) => notify_muc_invite(account, room_jid, from_jid, password, reason));
stream_interactor.get_module(MucManager.IDENTITY).voice_request_received.connect((account, room_jid, from_jid, nick) => notify_voice_request(account, room_jid, from_jid, nick));
stream_interactor.connection_manager.connection_error.connect((account, error) => notify_connection_error(account, error));
stream_interactor.get_module(MucManager.IDENTITY).invite_received.connect(on_invite_received);
stream_interactor.get_module(MucManager.IDENTITY).voice_request_received.connect((account, room_jid, from_jid, 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);
});
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((conversation) => {
notifier.retract_content_item_notifications.begin();
notifier.retract_conversation_notifications.begin(conversation);
});
}
public void register_notification_provider(NotificationProvider notification_provider) {
if (notifier == null || notifier.get_priority() < notification_provider.get_priority()) {
notifier = notification_provider;
}
}
private 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 && last_item.id != conversation.read_up_to_item) return;
if (!should_notify(item, conversation)) return;
if (item.id != last_item.id) return;
if (item.id == conversation.read_up_to_item) return;
if (stream_interactor.get_module(ChatInteraction.IDENTITY).is_active_focus()) return;
notify_content_item(item, conversation);
Conversation.NotifySetting notify = conversation.get_notification_setting(stream_interactor);
if (notify == Conversation.NotifySetting.OFF) return;
string conversation_display_name = get_conversation_display_name(stream_interactor, conversation, null);
string? participant_display_name = null;
if (conversation.type_ == Conversation.Type.GROUPCHAT) {
participant_display_name = get_participant_display_name(stream_interactor, conversation, item.jid);
}
private bool should_notify(ContentItem content_item, Conversation conversation) {
Conversation.NotifySetting notify = conversation.get_notification_setting(stream_interactor);
if (notify == Conversation.NotifySetting.OFF) return false;
switch (content_item.type_) {
switch (item.type_) {
case MessageItem.TYPE:
Message message = ((MessageItem) content_item).message;
if (message.direction == Message.DIRECTION_SENT) return false;
Message message = ((MessageItem) item).message;
if (message.direction == Message.DIRECTION_SENT) return;
if (notify == Conversation.NotifySetting.HIGHLIGHT) {
Jid? nick = stream_interactor.get_module(MucManager.IDENTITY).get_own_jid(conversation.counterpart, conversation.account);
if (nick == null) return;
bool highlight = Regex.match_simple("\\b" + Regex.escape_string(nick.resourcepart) + "\\b", message.body, RegexCompileFlags.CASELESS);
if (!highlight) return;
}
notifier.notify_message.begin(message, conversation, conversation_display_name, participant_display_name);
break;
case FileItem.TYPE:
FileTransfer file_transfer = ((FileItem) content_item).file_transfer;
FileTransfer file_transfer = ((FileItem) item).file_transfer;
bool is_image = file_transfer.mime_type != null && file_transfer.mime_type.has_prefix("image");
// Don't notify on file transfers in a groupchat set to "mention only"
if (notify == Conversation.NotifySetting.HIGHLIGHT) return false;
if (file_transfer.direction == FileTransfer.DIRECTION_SENT) return false;
if (notify == Conversation.NotifySetting.HIGHLIGHT) return;
if (file_transfer.direction == FileTransfer.DIRECTION_SENT) return;
notifier.notify_file.begin(file_transfer, conversation, is_image, conversation_display_name, participant_display_name);
break;
}
if (content_item.type_ == MessageItem.TYPE && notify == Conversation.NotifySetting.HIGHLIGHT) {
Jid? nick = stream_interactor.get_module(MucManager.IDENTITY).get_own_jid(conversation.counterpart, conversation.account);
if (nick == null) return false;
Entities.Message message = ((MessageItem) content_item).message;
return Regex.match_simple("\\b" + Regex.escape_string(nick.resourcepart) + "\\b", message.body, RegexCompileFlags.CASELESS);
}
return true;
}
private 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;
notify_subscription_request(conversation);
notifier.notify_subscription_request.begin(conversation);
}
private 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);
inviter_display_name = get_participant_display_name(stream_interactor, conversation, from_jid);
} else {
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);
}
}
public interface NotificationProvider : Object {
public abstract double get_priority();
public abstract async void notify_message(Message message, Conversation conversation, string conversation_display_name, string? participant_display_name);
public abstract async void notify_file(FileTransfer file_transfer, Conversation conversation, bool is_image, string conversation_display_name, string? participant_display_name);
public abstract async void notify_subscription_request(Conversation conversation);
public abstract async void notify_connection_error(Account account, ConnectionManager.ConnectionError error);
public abstract async void notify_muc_invite(Account account, Jid room_jid, Jid from_jid, string inviter_display_name);
public abstract async void notify_voice_request(Conversation conversation, Jid from_jid);
public abstract async void retract_content_item_notifications();
public abstract async void retract_conversation_notifications(Conversation conversation);
}
}

View file

@ -0,0 +1,97 @@
using Gee;
using Dino.Entities;
using Xmpp;
namespace Dino {
public static string get_conversation_display_name(StreamInteractor stream_interactor, Conversation conversation, string? muc_pm_format) {
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 (muc_pm_format ?? "%s / %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();
}
public static string get_participant_display_name(StreamInteractor stream_interactor, Conversation conversation, Jid participant, string? self_word = null) {
if (self_word != null) {
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 self_word;
}
}
if (conversation.type_ == Conversation.Type.CHAT) {
return get_real_display_name(stream_interactor, conversation.account, participant, self_word) ?? 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();
}
private static string? get_real_display_name(StreamInteractor stream_interactor, Account account, Jid jid, string? self_word = null) {
if (jid.equals_bare(account.bare_jid)) {
if (self_word != null || account.alias == null || account.alias.length == 0) {
return self_word;
}
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;
}
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();
}
private static string get_occupant_display_name(StreamInteractor stream_interactor, Conversation conversation, Jid jid, string? self_word = null, 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, self_word);
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();
}
}

View file

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

View file

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

View file

@ -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));
}
}
}

View file

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

View file

@ -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));
}
}
}

View file

@ -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();
}