From 08a5088c16ae0bd69adc42ac6489adde3a9ad13f Mon Sep 17 00:00:00 2001 From: fiaxh Date: Fri, 2 Aug 2019 03:15:12 +0200 Subject: Rework encryption enabling logic + UI --- main/src/ui/chat_input/edit_history.vala | 2 +- main/src/ui/chat_input/encryption_button.vala | 19 ++- .../src/ui/chat_input/occupants_tab_completer.vala | 2 +- main/src/ui/chat_input/smiley_converter.vala | 4 +- main/src/ui/chat_input/view.vala | 136 +++++++------------- main/src/ui/chat_input_controller.vala | 143 +++++++++++++++++++++ main/src/ui/unified_window_controller.vala | 5 + 7 files changed, 208 insertions(+), 103 deletions(-) create mode 100644 main/src/ui/chat_input_controller.vala (limited to 'main/src/ui') diff --git a/main/src/ui/chat_input/edit_history.vala b/main/src/ui/chat_input/edit_history.vala index 82e6cbc5..1d179bb7 100644 --- a/main/src/ui/chat_input/edit_history.vala +++ b/main/src/ui/chat_input/edit_history.vala @@ -6,7 +6,7 @@ using Dino.Entities; namespace Dino.Ui.ChatInput { -class EditHistory { +public class EditHistory { private Conversation? conversation; private TextView text_input; diff --git a/main/src/ui/chat_input/encryption_button.vala b/main/src/ui/chat_input/encryption_button.vala index 0a092db0..d80fa18f 100644 --- a/main/src/ui/chat_input/encryption_button.vala +++ b/main/src/ui/chat_input/encryption_button.vala @@ -7,6 +7,8 @@ namespace Dino.Ui { public class EncryptionButton : MenuButton { + public signal void encryption_changed(Plugins.EncryptionListEntry? encryption_entry); + private Conversation? conversation; private RadioButton? button_unencrypted; private Map encryption_radios = new HashMap(); @@ -25,38 +27,45 @@ public class EncryptionButton : MenuButton { popover = builder.get_object("menu_encryption") as PopoverMenu; Box encryption_box = builder.get_object("encryption_box") as Box; button_unencrypted = builder.get_object("button_unencrypted") as RadioButton; - button_unencrypted.toggled.connect(encryption_changed); + button_unencrypted.toggled.connect(encryption_button_toggled); Application app = GLib.Application.get_default() as Application; foreach (var e in app.plugin_registry.encryption_list_entries) { RadioButton btn = new RadioButton.with_label(button_unencrypted.get_group(), e.name); encryption_radios[btn] = e; - btn.toggled.connect(encryption_changed); + btn.toggled.connect(encryption_button_toggled); btn.visible = true; encryption_box.pack_end(btn, false); } clicked.connect(update_encryption_menu_state); } - private void encryption_changed() { + private void encryption_button_toggled() { foreach (RadioButton e in encryption_radios.keys) { if (e.get_active()) { conversation.encryption = encryption_radios[e].encryption; + encryption_changed(encryption_radios[e]); update_encryption_menu_icon(); return; } } + + // Selected unencrypted conversation.encryption = Encryption.NONE; update_encryption_menu_icon(); + encryption_changed(null); } private void update_encryption_menu_state() { foreach (RadioButton e in encryption_radios.keys) { - e.set_sensitive(encryption_radios[e].can_encrypt(conversation)); - if (conversation.encryption == encryption_radios[e].encryption) e.set_active(true); + if (conversation.encryption == encryption_radios[e].encryption) { + e.set_active(true); + encryption_changed(encryption_radios[e]); + } } if (conversation.encryption == Encryption.NONE) { button_unencrypted.set_active(true); + encryption_changed(null); } } diff --git a/main/src/ui/chat_input/occupants_tab_completer.vala b/main/src/ui/chat_input/occupants_tab_completer.vala index 57d7e91d..87db8986 100644 --- a/main/src/ui/chat_input/occupants_tab_completer.vala +++ b/main/src/ui/chat_input/occupants_tab_completer.vala @@ -13,7 +13,7 @@ namespace Dino.Ui.ChatInput { * - At the start (with ",") and in the middle of a text * - Backwards tabbing */ -class OccupantsTabCompletor { +public class OccupantsTabCompletor { private StreamInteractor stream_interactor; private Conversation? conversation; diff --git a/main/src/ui/chat_input/smiley_converter.vala b/main/src/ui/chat_input/smiley_converter.vala index 6844222e..89512356 100644 --- a/main/src/ui/chat_input/smiley_converter.vala +++ b/main/src/ui/chat_input/smiley_converter.vala @@ -8,7 +8,6 @@ namespace Dino.Ui.ChatInput { class SmileyConverter { - private StreamInteractor stream_interactor; private TextView text_input; private static HashMap smiley_translations = new HashMap(); @@ -27,8 +26,7 @@ class SmileyConverter { smiley_translations[":/"] = "😕"; } - public SmileyConverter(StreamInteractor stream_interactor, TextView text_input) { - this.stream_interactor = stream_interactor; + public SmileyConverter(TextView text_input) { this.text_input = text_input; text_input.key_press_event.connect(on_text_input_key_press); diff --git a/main/src/ui/chat_input/view.vala b/main/src/ui/chat_input/view.vala index cefe3fb1..c40152a2 100644 --- a/main/src/ui/chat_input/view.vala +++ b/main/src/ui/chat_input/view.vala @@ -10,6 +10,8 @@ namespace Dino.Ui.ChatInput { [GtkTemplate (ui = "/im/dino/Dino/chat_input.ui")] public class View : Box { + public signal void send_text(string text); + public string text { owned get { return text_input.buffer.text; } set { text_input.buffer.text = value; } @@ -20,44 +22,36 @@ public class View : Box { private HashMap entry_cache = new HashMap(Conversation.hash_func, Conversation.equals_func); private int vscrollbar_min_height; - private OccupantsTabCompletor occupants_tab_completor; + public OccupantsTabCompletor occupants_tab_completor; private SmileyConverter smiley_converter; - private EditHistory edit_history; + public EditHistory edit_history; - [GtkChild] private Frame frame; - [GtkChild] private ScrolledWindow scrolled; + [GtkChild] public Frame frame; + [GtkChild] public ScrolledWindow scrolled; [GtkChild] public TextView text_input; - [GtkChild] private Box outer_box; - [GtkChild] private Button file_button; - [GtkChild] private Separator file_separator; - private EncryptionButton encryption_widget; + [GtkChild] public Box outer_box; + [GtkChild] public Button file_button; + [GtkChild] public Separator file_separator; + [GtkChild] public Label chat_input_status; + + public EncryptionButton encryption_widget; public View init(StreamInteractor stream_interactor) { this.stream_interactor = stream_interactor; occupants_tab_completor = new OccupantsTabCompletor(stream_interactor, text_input); - smiley_converter = new SmileyConverter(stream_interactor, text_input); + smiley_converter = new SmileyConverter(text_input); edit_history = new EditHistory(text_input, GLib.Application.get_default()); encryption_widget = new EncryptionButton(stream_interactor) { margin_top=3, valign=Align.START, visible=true }; file_button.clicked.connect(() => { PreviewFileChooserNative chooser = new PreviewFileChooserNative("Select file", get_toplevel() as Gtk.Window, FileChooserAction.OPEN, "Select", "Cancel"); - - // long max_file_size = stream_interactor.get_module(Manager.IDENTITY).get_max_file_size(conversation.account); - // if (max_file_size != -1) { - // FileFilter filter = new FileFilter(); - // filter.add_custom(FileFilterFlags.URI, (filter_info) => { - // File file = File.new_for_uri(filter_info.uri); - // FileInfo file_info = file.query_info("*", FileQueryInfoFlags.NONE); - // return file_info.get_size() <= max_file_size; - // }); - // chooser.set_filter(filter); - // } if (chooser.run() == Gtk.ResponseType.ACCEPT) { string uri = chooser.get_filename(); stream_interactor.get_module(FileManager.IDENTITY).send_file.begin(uri, conversation); } }); + file_button.get_style_context().add_class("dino-attach-button"); scrolled.get_vscrollbar().get_preferred_height(out vscrollbar_min_height, null); scrolled.vadjustment.notify["upper"].connect_after(on_upper_notify); @@ -66,18 +60,14 @@ public class View : Box { outer_box.add(encryption_widget); text_input.key_press_event.connect(on_text_input_key_press); - text_input.buffer.changed.connect(on_text_input_changed); Util.force_css(frame, "* { border-radius: 3px; }"); - stream_interactor.get_module(FileManager.IDENTITY).upload_available.connect(on_upload_available); return this; } public void initialize_for_conversation(Conversation conversation) { - occupants_tab_completor.initialize_for_conversation(conversation); - edit_history.initialize_for_conversation(conversation); - encryption_widget.set_conversation(conversation); + if (this.conversation != null) entry_cache[this.conversation] = text_input.buffer.text; this.conversation = conversation; @@ -86,74 +76,49 @@ public class View : Box { file_button.visible = upload_available; file_separator.visible = upload_available; - text_input.buffer.changed.disconnect(on_text_input_changed); text_input.buffer.text = ""; if (entry_cache.has_key(conversation)) { text_input.buffer.text = entry_cache[conversation]; } - text_input.buffer.changed.connect(on_text_input_changed); text_input.grab_focus(); } - private void send_text() { - string text = text_input.buffer.text; - text_input.buffer.text = ""; - if (text.has_prefix("/")) { - string[] token = text.split(" ", 2); - switch(token[0]) { - case "/me": - // Just send as is. - break; - case "/say": - if (token.length == 1) return; - text = token[1]; - break; - case "/kick": - stream_interactor.get_module(MucManager.IDENTITY).kick(conversation.account, conversation.counterpart, token[1]); - return; - case "/affiliate": - if (token.length > 1) { - string[] user_role = token[1].split(" ", 2); - if (user_role.length == 2) { - stream_interactor.get_module(MucManager.IDENTITY).change_affiliation(conversation.account, conversation.counterpart, user_role[0].strip(), user_role[1].strip()); - } - } - return; - case "/nick": - stream_interactor.get_module(MucManager.IDENTITY).change_nick(conversation.account, conversation.counterpart, token[1]); - return; - case "/ping": - Xmpp.XmppStream? stream = stream_interactor.get_stream(conversation.account); - stream.get_module(Xmpp.Xep.Ping.Module.IDENTITY).send_ping(stream, conversation.counterpart.with_resource(token[1]), null); - return; - case "/topic": - stream_interactor.get_module(MucManager.IDENTITY).change_subject(conversation.account, conversation.counterpart, token[1]); - 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; - } - } - break; - } + public void set_input_state(Plugins.InputFieldStatus.MessageType message_type) { + switch (message_type) { + case Plugins.InputFieldStatus.MessageType.NONE: + this.get_style_context().remove_class("dino-input-warning"); + this.get_style_context().remove_class("dino-input-error"); + break; + case Plugins.InputFieldStatus.MessageType.INFO: + this.get_style_context().remove_class("dino-input-warning"); + this.get_style_context().remove_class("dino-input-error"); + break; + case Plugins.InputFieldStatus.MessageType.WARNING: + this.get_style_context().add_class("dino-input-warning"); + this.get_style_context().remove_class("dino-input-error"); + break; + case Plugins.InputFieldStatus.MessageType.ERROR: + this.get_style_context().remove_class("dino-input-warning"); + this.get_style_context().add_class("dino-input-error"); + break; } - stream_interactor.get_module(MessageProcessor.IDENTITY).send_text(text, conversation); + } + + public void highlight_state_description() { + chat_input_status.get_style_context().add_class("input-status-highlight-once"); + Timeout.add_seconds(1, () => { + chat_input_status.get_style_context().remove_class("input-status-highlight-once"); + return false; + }); } private bool on_text_input_key_press(EventKey event) { if (event.keyval in new uint[]{Key.Return, Key.KP_Enter}) { if ((event.state & ModifierType.SHIFT_MASK) > 0) { text_input.buffer.insert_at_cursor("\n", 1); - } else if (text_input.buffer.text != ""){ - send_text(); + } else if (this.text != "") { + send_text(this.text); edit_history.reset_history(); } return true; @@ -167,21 +132,6 @@ public class View : Box { // 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 on_text_input_changed() { - if (text_input.buffer.text != "") { - stream_interactor.get_module(ChatInteraction.IDENTITY).on_message_entered(conversation); - } else { - stream_interactor.get_module(ChatInteraction.IDENTITY).on_message_cleared(conversation); - } - } - - private void on_upload_available(Account account) { - if (conversation != null && conversation.account.equals(account)) { - file_button.visible = true; - file_separator.visible = true; - } - } } } diff --git a/main/src/ui/chat_input_controller.vala b/main/src/ui/chat_input_controller.vala new file mode 100644 index 00000000..413b8bd5 --- /dev/null +++ b/main/src/ui/chat_input_controller.vala @@ -0,0 +1,143 @@ +using Gee; +using Gdk; +using Gtk; + +using Dino.Entities; + +namespace Dino.Ui { + +public class ChatInputController : Object { + + public new string? conversation_display_name { get; set; } + public string? conversation_topic { get; set; } + + private Conversation? conversation; + private ChatInput.View chat_input; + private Label status_description_label; + + private StreamInteractor stream_interactor; + private Plugins.InputFieldStatus input_field_status; + + public ChatInputController(ChatInput.View chat_input, StreamInteractor stream_interactor) { + this.chat_input = chat_input; + this.status_description_label = chat_input.chat_input_status; + this.stream_interactor = stream_interactor; + + reset_input_field_status(); + + chat_input.text_input.buffer.changed.connect(on_text_input_changed); + chat_input.send_text.connect(send_text); + + chat_input.encryption_widget.encryption_changed.connect(on_encryption_changed); + + stream_interactor.get_module(FileManager.IDENTITY).upload_available.connect(on_upload_available); + } + + + + public void set_conversation(Conversation conversation) { + this.conversation = conversation; + + reset_input_field_status(); + + chat_input.occupants_tab_completor.initialize_for_conversation(conversation); + chat_input.edit_history.initialize_for_conversation(conversation); + chat_input.encryption_widget.set_conversation(conversation); + } + + private void on_encryption_changed(Plugins.EncryptionListEntry? encryption_entry) { + reset_input_field_status(); + + if (encryption_entry == null) return; + + encryption_entry.encryption_activated(conversation, set_input_field_status); + } + + private void set_input_field_status(Plugins.InputFieldStatus? status) { + input_field_status = status; + + chat_input.set_input_state(status.message_type); + status_description_label.label = status.message; + + chat_input.file_button.sensitive = status.input_state == Plugins.InputFieldStatus.InputState.NORMAL; + } + + private void reset_input_field_status() { + set_input_field_status(new Plugins.InputFieldStatus("", Plugins.InputFieldStatus.MessageType.NONE, Plugins.InputFieldStatus.InputState.NORMAL)); + } + + private void on_upload_available(Account account) { + if (conversation != null && conversation.account.equals(account)) { + chat_input.file_button.visible = true; + chat_input.file_separator.visible = true; + } + } + + private void send_text() { + // Don't do anything if we're in a NO_SEND state. Don't clear the chat input, don't send. + if (input_field_status.input_state == Plugins.InputFieldStatus.InputState.NO_SEND) { + chat_input.highlight_state_description(); + return; + } + + string text = chat_input.text_input.buffer.text; + chat_input.text_input.buffer.text = ""; + if (chat_input.text.has_prefix("/")) { + string[] token = chat_input.text.split(" ", 2); + switch(token[0]) { + case "/me": + // Just send as is. + break; + case "/say": + if (token.length == 1) return; + text = token[1]; + break; + case "/kick": + stream_interactor.get_module(MucManager.IDENTITY).kick(conversation.account, conversation.counterpart, token[1]); + return; + case "/affiliate": + if (token.length > 1) { + string[] user_role = token[1].split(" ", 2); + if (user_role.length == 2) { + stream_interactor.get_module(MucManager.IDENTITY).change_affiliation(conversation.account, conversation.counterpart, user_role[0].strip(), user_role[1].strip()); + } + } + return; + case "/nick": + stream_interactor.get_module(MucManager.IDENTITY).change_nick(conversation.account, conversation.counterpart, token[1]); + return; + case "/ping": + Xmpp.XmppStream? stream = stream_interactor.get_stream(conversation.account); + stream.get_module(Xmpp.Xep.Ping.Module.IDENTITY).send_ping(stream, conversation.counterpart.with_resource(token[1]), null); + return; + case "/topic": + stream_interactor.get_module(MucManager.IDENTITY).change_subject(conversation.account, conversation.counterpart, token[1]); + 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; + } + } + break; + } + } + stream_interactor.get_module(MessageProcessor.IDENTITY).send_text(text, conversation); + } + + private void on_text_input_changed() { + if (chat_input.text_input.buffer.text != "") { + stream_interactor.get_module(ChatInteraction.IDENTITY).on_message_entered(conversation); + } else { + stream_interactor.get_module(ChatInteraction.IDENTITY).on_message_cleared(conversation); + } + } +} + +} diff --git a/main/src/ui/unified_window_controller.vala b/main/src/ui/unified_window_controller.vala index fa087dcc..dd5ed52d 100644 --- a/main/src/ui/unified_window_controller.vala +++ b/main/src/ui/unified_window_controller.vala @@ -19,6 +19,8 @@ public class UnifiedWindowController : Object { private SearchMenuEntry search_menu_entry = new SearchMenuEntry(); + private ChatInputController chat_input_controller; + public UnifiedWindowController(Application application, StreamInteractor stream_interactor, Database db) { this.app = application; this.stream_interactor = stream_interactor; @@ -50,6 +52,8 @@ public class UnifiedWindowController : Object { public void set_window(UnifiedWindow window) { this.window = window; + this.chat_input_controller = new ChatInputController(window.chat_input, stream_interactor); + this.bind_property("conversation-display-name", window, "title"); this.bind_property("conversation-topic", window, "subtitle"); search_menu_entry.search_button.bind_property("active", window.search_revealer, "reveal_child"); @@ -136,6 +140,7 @@ public class UnifiedWindowController : Object { if (do_reset_search) { reset_search_entry(); } + chat_input_controller.set_conversation(conversation); window.chat_input.initialize_for_conversation(conversation); if (default_initialize_conversation) { window.conversation_frame.initialize_for_conversation(conversation); -- cgit v1.2.3-70-g09d2