aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/conversation_content_view/subscription_notification.vala
blob: 7fdda0b3bcc65ea163751654709cb6ad0a8d1868 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using Gee;
using Gtk;

using Dino.Entities;

namespace Dino.Ui.ConversationSummary {

public class SubscriptionNotitication : Object {

    private StreamInteractor stream_interactor;
    private Conversation conversation;
    private ConversationView conversation_view;

    public SubscriptionNotitication(StreamInteractor stream_interactor) {
        this.stream_interactor = stream_interactor;

        stream_interactor.get_module(PresenceManager.IDENTITY).received_subscription_request.connect((jid, account) => {
            Conversation relevant_conversation = stream_interactor.get_module(ConversationManager.IDENTITY).create_conversation(jid, account, Conversation.Type.CHAT);
            stream_interactor.get_module(ConversationManager.IDENTITY).start_conversation(relevant_conversation);
            if (conversation != null && account.equals(conversation.account) && jid.equals(conversation.counterpart)) {
                show_pending_subscription_request();
            }
        });
    }

    public void init(Conversation conversation, ConversationView conversation_view) {
        this.conversation = conversation;
        this.conversation_view = conversation_view;

        if (conversation.type_ != Conversation.Type.CHAT) return;

        if (stream_interactor.get_module(PresenceManager.IDENTITY).exists_subscription_request(conversation.account, conversation.counterpart)) {
            show_pending_subscription_request();
        } else {
            var roster_item = stream_interactor.get_module(RosterManager.IDENTITY).get_roster_item(conversation.account, conversation.counterpart);
            if (roster_item == null ||
                    (roster_item.subscription == Xmpp.Roster.Item.SUBSCRIPTION_NONE || roster_item.subscription == Xmpp.Roster.Item.SUBSCRIPTION_FROM) &&
                    !roster_item.subscription_requested) {
                show_no_subscription(roster_item != null);
            }
        }
    }

    private void show_no_subscription(bool already_in_roster) {
        Box box = new Box(Orientation.HORIZONTAL, 5);
        Button accept_button = new Button.with_label(_("Send request"));
        GLib.Application app = GLib.Application.get_default();
        accept_button.clicked.connect(() => {
            if (!already_in_roster) {
                stream_interactor.get_module(RosterManager.IDENTITY).add_jid(conversation.account, conversation.counterpart, null);
            }
            stream_interactor.get_module(PresenceManager.IDENTITY).request_subscription(conversation.account, conversation.counterpart);
            app.activate_action("accept-subscription", conversation.id);
            ((Dino.Ui.Application) app).window.conversation_view.chat_input.chat_text_view.text_view.grab_focus();
            conversation_view.remove_notification(box);
        });
        box.append(new Label(_("You do not receive status updates from this contact yet.")) { margin_end=10 });
        box.append(accept_button);
        conversation_view.add_notification(box);
    }

    private void show_pending_subscription_request() {
        Box box = new Box(Orientation.HORIZONTAL, 5);
        Button accept_button = new Button.with_label(_("Accept"));
        Button deny_button = new Button.with_label(_("Deny"));
        GLib.Application app = GLib.Application.get_default();
        accept_button.clicked.connect(() => {
            app.activate_action("accept-subscription", conversation.id);
            ((Dino.Ui.Application) app).window.conversation_view.chat_input.chat_text_view.text_view.grab_focus();
            conversation_view.remove_notification(box);
        });
        deny_button.clicked.connect(() => {
            app.activate_action("deny-subscription", conversation.id);
            ((Dino.Ui.Application) app).window.conversation_view.chat_input.chat_text_view.text_view.grab_focus();
            conversation_view.remove_notification(box);
        });
        box.append(new Label(_("This contact would like to add you to their contact list")) { margin_end=10 });
        box.append(accept_button);
        box.append(deny_button);
        conversation_view.add_notification(box);
    }
}

}