aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/chat_input.vala
blob: 209869a812fe81e3cfb1317aae2dcc20aa2a0098 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using Gdk;
using Gee;
using Gtk;

using Dino.Entities;
using Xmpp;

namespace Dino.Ui {

[GtkTemplate (ui = "/org/dino-im/chat_input.ui")]
public class ChatInput : Box {

    [GtkChild] private ScrolledWindow scrolled;
    [GtkChild] private TextView text_input;

    private Conversation? conversation;
    private StreamInteractor stream_interactor;
    private HashMap<Conversation, string> entry_cache = new HashMap<Conversation, string>(Conversation.hash_func, Conversation.equals_func);
    private static HashMap<string, string> smiley_translations = new HashMap<string, string>();
    private int vscrollbar_min_height;

    static construct {
        smiley_translations[":)"] = "🙂";
        smiley_translations[":D"] = "😀";
        smiley_translations[";)"] = "😉";
        smiley_translations["O:)"] = "😇";
        smiley_translations["]:>"] = "😈";
        smiley_translations[":o"] = "😮";
        smiley_translations[":P"] = "😛";
        smiley_translations[";P"] = "😜";
        smiley_translations[":("] = "🙁";
        smiley_translations[":'("] = "😢";
        smiley_translations[":/"] = "😕";
        smiley_translations["-.-"] = "😑";
    }

    public ChatInput(StreamInteractor stream_interactor) {
        this.stream_interactor = stream_interactor;
        scrolled.get_vscrollbar().get_preferred_height(out vscrollbar_min_height, null);
        scrolled.vadjustment.notify["upper"].connect_after(on_upper_notify);
    }

    public void initialize_for_conversation(Conversation conversation) {
        if (this.conversation != null) {
            if (text_input.buffer.text != "") {
                entry_cache[this.conversation] = text_input.buffer.text;
            } else {
                entry_cache.unset(this.conversation);
            }
        }
        this.conversation = conversation;
        text_input.buffer.text = "";
        if (entry_cache.has_key(conversation)) {
            text_input.buffer.text = entry_cache[conversation];
        }
        text_input.key_press_event.connect(on_text_input_key_press);
        text_input.key_release_event.connect(on_text_input_key_release);
        text_input.grab_focus();
    }

    private void send_text() {
        string text = text_input.buffer.text;
        if (text.has_prefix("/")) {
            string[] token = text.split(" ", 2);
            switch(token[0]) {
                case "/kick":
                    MucManager.get_instance(stream_interactor).kick(conversation.account, conversation.counterpart, token[1]);
                    break;
                case "/me":
                    MessageManager.get_instance(stream_interactor).send_message(text, conversation);
                    break;
                case "/nick":
                    MucManager.get_instance(stream_interactor).change_nick(conversation.account, conversation.counterpart, token[1]);
                    break;
                case "/topic":
                    MucManager.get_instance(stream_interactor).change_subject(conversation.account, conversation.counterpart, token[1]);
                    break;
            }
        } else {
            MessageManager.get_instance(stream_interactor).send_message(text, conversation);
        }
        text_input.buffer.text = "";
    }

    private bool on_text_input_key_press(EventKey event) {
        if (event.keyval == Key.space || event.keyval == Key.Return) {
            check_convert_smiley();
        }
        if (event.keyval == Key.Return) {
            if (event.state == ModifierType.SHIFT_MASK) {
                text_input.buffer.insert_at_cursor("\n", 1);
            } else if (text_input.buffer.text != ""){
                send_text();
            }
            return true;
        }
        return false;
    }

    private void on_upper_notify() {
        scrolled.vadjustment.value = scrolled.vadjustment.upper - scrolled.vadjustment.page_size;

        // hack for vscrollbar not requiring space and making textview higher //TODO doesn't resize immediately
        scrolled.get_vscrollbar().visible = (scrolled.vadjustment.upper > scrolled.max_content_height - 2 * vscrollbar_min_height);
    }

    private void check_convert_smiley() {
        if (Dino.Settings.instance().convert_utf8_smileys) {
            foreach (string smiley in smiley_translations.keys) {
                if (text_input.buffer.text.has_suffix(smiley)) {
                    if (text_input.buffer.text.length == smiley.length ||
                            text_input.buffer.text[text_input.buffer.text.length - smiley.length - 1] == ' ') {
                        text_input.buffer.text = text_input.buffer.text.substring(0, text_input.buffer.text.length - smiley.length) + smiley_translations[smiley];
                    }
                }
            }
        }
    }

    private bool on_text_input_key_release(EventKey event) {
        if (text_input.buffer.text != "") {
            ChatInteraction.get_instance(stream_interactor).on_message_entered(conversation);
        } else {
            ChatInteraction.get_instance(stream_interactor).on_message_cleared(conversation);
        }
        return false;
    }
}

}