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 notifications = new HashMap(Conversation.hash_func, Conversation.equals_func); private enum ClosedReason { // org.freedesktop.Notifications.NotificationClosed EXPIRED = 1, USER_DISMISSED = 2, CLOSE_NOTIFICATION = 3, UNDEFINED = 4 } public Notifications(StreamInteractor stream_interactor) { this.stream_interactor = stream_interactor; } public void start() { stream_interactor.get_module(MessageProcessor.IDENTITY).message_received.connect(on_message_received); stream_interactor.get_module(PresenceManager.IDENTITY).received_subscription_request.connect(on_received_subscription_request); } private void on_message_received(Entities.Message message, Conversation conversation) { if (!notifications.has_key(conversation)) { notifications[conversation] = new Notify.Notification("", null, null); notifications[conversation].set_hint("transient", true); notifications[conversation].closed.connect(() => { if (notifications[conversation].closed_reason == ClosedReason.USER_DISMISSED) { // USER_DISMISSED + transient = very probably clicked on conversation_selected(conversation); } }); } if (!stream_interactor.get_module(ChatInteraction.IDENTITY).is_active_focus()) { string display_name = Util.get_conversation_display_name(stream_interactor, conversation); string text = message.body; if (stream_interactor.get_module(MucManager.IDENTITY).is_groupchat(conversation.counterpart, conversation.account)) { string muc_occupant = Util.get_display_name(stream_interactor, message.from, conversation.account); text = @"$muc_occupant $text"; } notifications[conversation].update(display_name, text, null); notifications[conversation].set_image_from_pixbuf((new AvatarGenerator(40, 40)).draw_conversation(stream_interactor, conversation)); notifications[conversation].set_timeout(3); try { notifications[conversation].show(); } catch (Error error) { } } } private void on_received_subscription_request(Jid jid, Account account) { Notify.Notification notification = new Notify.Notification("Subscription request", jid.bare_jid.to_string(), null); notification.set_image_from_pixbuf((new AvatarGenerator(40, 40)).draw_jid(stream_interactor, jid, account)); notification.add_action("accept", "Accept", () => { stream_interactor.get_module(PresenceManager.IDENTITY).approve_subscription(account, jid); if (stream_interactor.get_module(RosterManager.IDENTITY).get_roster_item(account, jid) == null) { AddConversation.Chat.AddContactDialog dialog = new AddConversation.Chat.AddContactDialog(stream_interactor); dialog.jid = jid.bare_jid.to_string(); dialog.account = account; dialog.show(); } try { notification.close(); } catch (Error error) { } }); notification.add_action("deny", "Deny", () => { stream_interactor.get_module(PresenceManager.IDENTITY).deny_subscription(account, jid); try { notification.close(); } catch (Error error) { } }); try { notification.show(); } catch (Error error) { } } } }