2017-03-02 14:37:32 +00:00
|
|
|
using Gdk;
|
|
|
|
using Gee;
|
|
|
|
using Gtk;
|
|
|
|
|
|
|
|
using Dino.Entities;
|
|
|
|
using Xmpp;
|
|
|
|
|
2017-03-23 23:15:00 +00:00
|
|
|
namespace Dino.Ui.ChatInput {
|
2017-03-10 17:07:28 +00:00
|
|
|
|
2017-12-03 18:42:15 +00:00
|
|
|
[GtkTemplate (ui = "/im/dino/Dino/chat_input.ui")]
|
2017-03-23 23:15:00 +00:00
|
|
|
public class View : Box {
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-03-15 21:01:32 +00:00
|
|
|
[GtkChild] private ScrolledWindow scrolled;
|
2017-03-10 17:07:28 +00:00
|
|
|
[GtkChild] private TextView text_input;
|
2017-09-05 21:53:18 +00:00
|
|
|
[GtkChild] private Box box;
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-08-25 19:20:09 +00:00
|
|
|
public string text {
|
|
|
|
owned get { return text_input.buffer.text; }
|
|
|
|
set { text_input.buffer.text = value; }
|
|
|
|
}
|
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
private StreamInteractor stream_interactor;
|
2017-03-23 23:15:00 +00:00
|
|
|
private Conversation? conversation;
|
2017-03-02 14:37:32 +00:00
|
|
|
private HashMap<Conversation, string> entry_cache = new HashMap<Conversation, string>(Conversation.hash_func, Conversation.equals_func);
|
2017-03-15 21:01:32 +00:00
|
|
|
private int vscrollbar_min_height;
|
2017-03-23 23:15:00 +00:00
|
|
|
private OccupantsTabCompletor occupants_tab_completor;
|
|
|
|
private SmileyConverter smiley_converter;
|
2017-08-16 20:44:12 +00:00
|
|
|
private EditHistory edit_history;
|
2017-09-05 21:53:18 +00:00
|
|
|
private EncryptionButton encryption_widget = new EncryptionButton() { yalign=0, visible=true };
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-03-23 23:15:00 +00:00
|
|
|
public View(StreamInteractor stream_interactor) {
|
2017-03-02 14:37:32 +00:00
|
|
|
this.stream_interactor = stream_interactor;
|
2017-03-23 23:15:00 +00:00
|
|
|
occupants_tab_completor = new OccupantsTabCompletor(stream_interactor, text_input);
|
|
|
|
smiley_converter = new SmileyConverter(stream_interactor, text_input);
|
2017-08-16 20:44:12 +00:00
|
|
|
edit_history = new EditHistory(text_input, GLib.Application.get_default());
|
2017-03-23 23:15:00 +00:00
|
|
|
|
2017-09-05 21:53:18 +00:00
|
|
|
box.add(encryption_widget);
|
|
|
|
encryption_widget.get_style_context().add_class("dino-chatinput-button");
|
2017-03-15 21:01:32 +00:00
|
|
|
scrolled.get_vscrollbar().get_preferred_height(out vscrollbar_min_height, null);
|
|
|
|
scrolled.vadjustment.notify["upper"].connect_after(on_upper_notify);
|
2017-03-16 15:28:32 +00:00
|
|
|
text_input.key_press_event.connect(on_text_input_key_press);
|
|
|
|
text_input.buffer.changed.connect(on_text_input_changed);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void initialize_for_conversation(Conversation conversation) {
|
2017-03-23 23:15:00 +00:00
|
|
|
occupants_tab_completor.initialize_for_conversation(conversation);
|
2017-08-16 20:44:12 +00:00
|
|
|
edit_history.initialize_for_conversation(conversation);
|
2017-09-05 21:53:18 +00:00
|
|
|
encryption_widget.set_conversation(conversation);
|
2017-03-23 23:15:00 +00:00
|
|
|
|
2017-03-16 15:28:32 +00:00
|
|
|
if (this.conversation != null) entry_cache[this.conversation] = text_input.buffer.text;
|
2017-03-02 14:37:32 +00:00
|
|
|
this.conversation = conversation;
|
2017-03-16 15:28:32 +00:00
|
|
|
|
|
|
|
text_input.buffer.changed.disconnect(on_text_input_changed);
|
2017-03-02 14:37:32 +00:00
|
|
|
text_input.buffer.text = "";
|
|
|
|
if (entry_cache.has_key(conversation)) {
|
|
|
|
text_input.buffer.text = entry_cache[conversation];
|
|
|
|
}
|
2017-03-16 15:28:32 +00:00
|
|
|
text_input.buffer.changed.connect(on_text_input_changed);
|
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
text_input.grab_focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void send_text() {
|
|
|
|
string text = text_input.buffer.text;
|
2017-05-13 15:45:06 +00:00
|
|
|
text_input.buffer.text = "";
|
2017-03-02 14:37:32 +00:00
|
|
|
if (text.has_prefix("/")) {
|
|
|
|
string[] token = text.split(" ", 2);
|
|
|
|
switch(token[0]) {
|
|
|
|
case "/me":
|
2017-05-13 15:45:06 +00:00
|
|
|
// Just send as is.
|
|
|
|
break;
|
|
|
|
case "/say":
|
|
|
|
if (token.length == 1) return;
|
|
|
|
text = token[1];
|
2017-03-02 14:37:32 +00:00
|
|
|
break;
|
2017-05-13 15:45:06 +00:00
|
|
|
case "/kick":
|
|
|
|
stream_interactor.get_module(MucManager.IDENTITY).kick(conversation.account, conversation.counterpart, token[1]);
|
|
|
|
return;
|
2017-03-02 14:37:32 +00:00
|
|
|
case "/nick":
|
2017-03-19 11:55:36 +00:00
|
|
|
stream_interactor.get_module(MucManager.IDENTITY).change_nick(conversation.account, conversation.counterpart, token[1]);
|
2017-05-13 15:45:06 +00:00
|
|
|
return;
|
2017-05-24 15:28:39 +00:00
|
|
|
case "/ping":
|
|
|
|
Xmpp.Core.XmppStream? stream = stream_interactor.get_stream(conversation.account);
|
|
|
|
stream.get_module(Xmpp.Xep.Ping.Module.IDENTITY).send_ping(stream, conversation.counterpart.to_string() + "/" + token[1], null);
|
2017-05-13 15:45:06 +00:00
|
|
|
return;
|
2017-03-02 14:37:32 +00:00
|
|
|
case "/topic":
|
2017-03-19 11:55:36 +00:00
|
|
|
stream_interactor.get_module(MucManager.IDENTITY).change_subject(conversation.account, conversation.counterpart, token[1]);
|
2017-05-13 15:45:06 +00:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
if (token[0].has_prefix("//")) {
|
|
|
|
text = text.substring(1);
|
|
|
|
} else {
|
|
|
|
string cmd_name = token[0].substring(1);
|
|
|
|
Dino.Application app = GLib.Application.get_default() as Dino.Application;
|
|
|
|
if (app != null && app.plugin_registry.text_commands.has_key(cmd_name)) {
|
|
|
|
string? new_text = app.plugin_registry.text_commands[cmd_name].handle_command(token[1], conversation);
|
|
|
|
if (new_text == null) return;
|
|
|
|
text = (!)new_text;
|
|
|
|
}
|
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-05-13 15:45:06 +00:00
|
|
|
stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(text, conversation);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool on_text_input_key_press(EventKey event) {
|
2017-05-11 09:05:51 +00:00
|
|
|
if (event.keyval in new uint[]{Key.Return, Key.KP_Enter}) {
|
2017-03-18 22:44:05 +00:00
|
|
|
if ((event.state & ModifierType.SHIFT_MASK) > 0) {
|
2017-03-02 14:37:32 +00:00
|
|
|
text_input.buffer.insert_at_cursor("\n", 1);
|
|
|
|
} else if (text_input.buffer.text != ""){
|
|
|
|
send_text();
|
2017-08-16 20:44:12 +00:00
|
|
|
edit_history.reset_history();
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-15 21:01:32 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-16 15:28:32 +00:00
|
|
|
private void on_text_input_changed() {
|
2017-03-02 14:37:32 +00:00
|
|
|
if (text_input.buffer.text != "") {
|
2017-03-19 11:55:36 +00:00
|
|
|
stream_interactor.get_module(ChatInteraction.IDENTITY).on_message_entered(conversation);
|
2017-03-02 14:37:32 +00:00
|
|
|
} else {
|
2017-03-19 11:55:36 +00:00
|
|
|
stream_interactor.get_module(ChatInteraction.IDENTITY).on_message_cleared(conversation);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 17:07:28 +00:00
|
|
|
|
2017-08-16 20:44:12 +00:00
|
|
|
}
|