2017-03-23 15:34:54 +00:00
|
|
|
using Gee;
|
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
using Dino.Entities;
|
|
|
|
using Xmpp;
|
|
|
|
|
|
|
|
namespace Dino.Ui {
|
2017-03-10 17:07:28 +00:00
|
|
|
|
2017-03-22 22:55:19 +00:00
|
|
|
public class Notifications : Object {
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-03-23 15:34:54 +00:00
|
|
|
public signal void conversation_selected(Conversation conversation);
|
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
private StreamInteractor stream_interactor;
|
2017-04-10 22:04:27 +00:00
|
|
|
private Gtk.Window window;
|
2017-09-19 20:41:33 +00:00
|
|
|
private HashMap<Conversation, Notification> notifications = new HashMap<Conversation, Notification>(Conversation.hash_func, Conversation.equals_func);
|
2017-12-14 01:01:55 +00:00
|
|
|
private Set<string>? active_conversation_ids = null;
|
|
|
|
private Set<string>? active_ids = new HashSet<string>();
|
2018-02-17 20:45:44 +00:00
|
|
|
private Canberra.Context sound_context;
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-04-10 22:04:27 +00:00
|
|
|
public Notifications(StreamInteractor stream_interactor, Gtk.Window window) {
|
2017-03-02 14:37:32 +00:00
|
|
|
this.stream_interactor = stream_interactor;
|
2017-04-10 22:04:27 +00:00
|
|
|
this.window = window;
|
2017-09-19 20:41:33 +00:00
|
|
|
|
2018-02-17 20:45:44 +00:00
|
|
|
Canberra.Context.create(out sound_context);
|
|
|
|
|
2017-12-14 01:01:55 +00:00
|
|
|
stream_interactor.get_module(ChatInteraction.IDENTITY).focused_in.connect((focused_conversation) => {
|
|
|
|
if (active_conversation_ids == null) {
|
2017-09-19 20:41:33 +00:00
|
|
|
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());
|
|
|
|
}
|
2017-12-14 01:01:55 +00:00
|
|
|
active_conversation_ids = new HashSet<string>();
|
2017-09-19 20:41:33 +00:00
|
|
|
} else {
|
2017-12-14 01:01:55 +00:00
|
|
|
foreach (string id in active_conversation_ids) {
|
2017-09-19 20:41:33 +00:00
|
|
|
GLib.Application.get_default().withdraw_notification(id);
|
|
|
|
}
|
2017-12-14 01:01:55 +00:00
|
|
|
active_conversation_ids.clear();
|
2017-09-19 20:41:33 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 01:01:55 +00:00
|
|
|
string subscription_id = focused_conversation.id.to_string() + "-subscription";
|
|
|
|
if (active_ids.contains(subscription_id)) {
|
|
|
|
GLib.Application.get_default().withdraw_notification(subscription_id);
|
2017-09-19 20:41:33 +00:00
|
|
|
}
|
|
|
|
});
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void start() {
|
2017-04-04 13:47:00 +00:00
|
|
|
stream_interactor.get_module(MessageProcessor.IDENTITY).message_received.connect(on_message_received);
|
2017-03-19 11:55:36 +00:00
|
|
|
stream_interactor.get_module(PresenceManager.IDENTITY).received_subscription_request.connect(on_received_subscription_request);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void on_message_received(Entities.Message message, Conversation conversation) {
|
2017-05-30 20:31:05 +00:00
|
|
|
if (!should_notify_message(message, conversation)) return;
|
|
|
|
|
2017-03-23 15:34:54 +00:00
|
|
|
if (!notifications.has_key(conversation)) {
|
2017-09-19 20:41:33 +00:00
|
|
|
notifications[conversation] = new Notification("");
|
|
|
|
notifications[conversation].set_default_action_and_target_value("app.open-conversation", new Variant.int32(conversation.id));
|
2017-03-23 15:34:54 +00:00
|
|
|
}
|
2017-03-19 11:55:36 +00:00
|
|
|
if (!stream_interactor.get_module(ChatInteraction.IDENTITY).is_active_focus()) {
|
2017-03-02 14:37:32 +00:00
|
|
|
string display_name = Util.get_conversation_display_name(stream_interactor, conversation);
|
2017-03-23 15:34:54 +00:00
|
|
|
string text = message.body;
|
2017-03-19 11:55:36 +00:00
|
|
|
if (stream_interactor.get_module(MucManager.IDENTITY).is_groupchat(conversation.counterpart, conversation.account)) {
|
2017-03-02 14:37:32 +00:00
|
|
|
string muc_occupant = Util.get_display_name(stream_interactor, message.from, conversation.account);
|
2017-09-19 20:41:33 +00:00
|
|
|
text = @"$muc_occupant: $text";
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
2017-09-19 20:41:33 +00:00
|
|
|
notifications[conversation].set_title(display_name);
|
|
|
|
notifications[conversation].set_body(text);
|
2017-10-29 14:15:28 +00:00
|
|
|
try {
|
|
|
|
notifications[conversation].set_icon(get_pixbuf_icon((new AvatarGenerator(40, 40)).draw_conversation(stream_interactor, conversation)));
|
|
|
|
} catch (Error e) { }
|
2017-09-19 20:41:33 +00:00
|
|
|
window.get_application().send_notification(conversation.id.to_string(), notifications[conversation]);
|
2017-12-14 01:01:55 +00:00
|
|
|
active_conversation_ids.add(conversation.id.to_string());
|
2017-09-05 21:53:18 +00:00
|
|
|
window.urgency_hint = true;
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
2018-02-17 20:45:44 +00:00
|
|
|
if (conversation.get_sound_setting(stream_interactor)) {
|
|
|
|
sound_context.play (0,
|
|
|
|
Canberra.PROP_EVENT_ID, "message-new-instant",
|
|
|
|
Canberra.PROP_EVENT_DESCRIPTION, "New Dino message");
|
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void on_received_subscription_request(Jid jid, Account account) {
|
2017-12-14 01:01:55 +00:00
|
|
|
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;
|
|
|
|
|
2017-09-19 20:41:33 +00:00
|
|
|
Notification notification = new Notification(_("Subscription request"));
|
|
|
|
notification.set_body(jid.bare_jid.to_string());
|
2017-10-29 14:15:28 +00:00
|
|
|
try {
|
|
|
|
notification.set_icon(get_pixbuf_icon((new AvatarGenerator(40, 40)).draw_jid(stream_interactor, jid, account)));
|
|
|
|
} catch (Error e) { }
|
2017-12-14 01:01:55 +00:00
|
|
|
notification.set_default_action_and_target_value("app.open-conversation", new Variant.int32(conversation.id));
|
2017-09-19 20:41:33 +00:00
|
|
|
notification.add_button_with_target_value(_("Accept"), "app.accept-subscription", conversation.id);
|
|
|
|
notification.add_button_with_target_value(_("Deny"), "app.deny-subscription", conversation.id);
|
2017-12-14 01:01:55 +00:00
|
|
|
window.get_application().send_notification(conversation.id.to_string() + "-subscription", notification);
|
|
|
|
active_ids.add(conversation.id.to_string() + "-subscription");
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
2017-05-30 20:31:05 +00:00
|
|
|
|
|
|
|
private bool should_notify_message(Entities.Message message, Conversation conversation) {
|
|
|
|
Conversation.NotifySetting notify = conversation.get_notification_setting(stream_interactor);
|
|
|
|
if (notify == Conversation.NotifySetting.OFF) return false;
|
2017-06-11 11:59:24 +00:00
|
|
|
Jid? nick = stream_interactor.get_module(MucManager.IDENTITY).get_own_jid(conversation.counterpart, conversation.account);
|
2017-10-21 23:24:57 +00:00
|
|
|
if (notify == Conversation.NotifySetting.HIGHLIGHT && nick != null) {
|
|
|
|
return Regex.match_simple("""\b""" + Regex.escape_string(nick.resourcepart) + """\b""", message.body, RegexCompileFlags.CASELESS);
|
|
|
|
}
|
2017-05-30 20:31:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-09-19 20:41:33 +00:00
|
|
|
|
2018-01-16 15:17:42 +00:00
|
|
|
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());
|
2017-09-19 20:41:33 +00:00
|
|
|
uint8[] buffer;
|
|
|
|
avatar.save_to_buffer(out buffer, "png");
|
|
|
|
return new BytesIcon(new Bytes(buffer));
|
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
2017-03-10 17:07:28 +00:00
|
|
|
|
2017-08-16 09:44:42 +00:00
|
|
|
}
|