aboutsummaryrefslogtreecommitdiff
path: root/client/src/ui/chat_input.vala
diff options
context:
space:
mode:
authorfiaxh <git@mx.ax.lt>2017-03-02 15:37:32 +0100
committerfiaxh <git@mx.ax.lt>2017-03-02 15:37:32 +0100
commit56bc45ce4d07a7a9a415e9dc8ad2f7c3f3c9e48d (patch)
tree0bd0c2c80cb81179c26282fb3fbe8fd22983f40b /client/src/ui/chat_input.vala
downloaddino-56bc45ce4d07a7a9a415e9dc8ad2f7c3f3c9e48d.tar.gz
dino-56bc45ce4d07a7a9a415e9dc8ad2f7c3f3c9e48d.zip
Initial commit
Diffstat (limited to 'client/src/ui/chat_input.vala')
-rw-r--r--client/src/ui/chat_input.vala123
1 files changed, 123 insertions, 0 deletions
diff --git a/client/src/ui/chat_input.vala b/client/src/ui/chat_input.vala
new file mode 100644
index 00000000..d2f9c562
--- /dev/null
+++ b/client/src/ui/chat_input.vala
@@ -0,0 +1,123 @@
+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 : Grid {
+
+ [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>();
+
+ 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;
+ }
+
+ 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 "/ping": // TODO remove this
+ Xep.Ping.Module.get_module(stream_interactor.get_stream(conversation.account))
+ .send_ping(stream_interactor.get_stream(conversation.account), @"$(conversation.counterpart.bare_jid)/$(token[1])");
+ Xep.Ping.Module.get_module(stream_interactor.get_stream(conversation.account)).get_id();
+ 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 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;
+ }
+}
+} \ No newline at end of file