From 781d241b93c1d7696cafa524f0c6e00d84579951 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Sat, 13 May 2017 17:45:06 +0200 Subject: add plugin api for text commands --- libdino/src/plugin/registry.vala | 9 +++++++++ main/src/ui/chat_input/view.vala | 34 +++++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/libdino/src/plugin/registry.vala b/libdino/src/plugin/registry.vala index 01d18d5f..6284269b 100644 --- a/libdino/src/plugin/registry.vala +++ b/libdino/src/plugin/registry.vala @@ -6,6 +6,7 @@ public class Registry { internal ArrayList encryption_list_entries = new ArrayList(); internal ArrayList account_settings_entries = new ArrayList(); internal ArrayList contact_details_entries = new ArrayList(); + internal Map text_commands = new HashMap(); internal Gee.Collection conversation_titlebar_entries = new Gee.TreeSet((a, b) => { if (a.order < b.order) { return -1; @@ -49,6 +50,14 @@ public class Registry { } } + public bool register_text_command(TextCommand cmd) { + lock(text_commands) { + if (text_commands.has_key(cmd.cmd)) return false; + text_commands[cmd.cmd] = cmd; + return true; + } + } + public bool register_contact_titlebar_entry(ConversationTitlebarEntry entry) { lock(conversation_titlebar_entries) { foreach(ConversationTitlebarEntry e in conversation_titlebar_entries) { diff --git a/main/src/ui/chat_input/view.vala b/main/src/ui/chat_input/view.vala index d8a31a22..a7c3099c 100644 --- a/main/src/ui/chat_input/view.vala +++ b/main/src/ui/chat_input/view.vala @@ -52,30 +52,46 @@ public class View : Box { 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 "/kick": - stream_interactor.get_module(MucManager.IDENTITY).kick(conversation.account, conversation.counterpart, token[1]); - break; case "/me": - stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(text, conversation); + // 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 "/nick": stream_interactor.get_module(MucManager.IDENTITY).change_nick(conversation.account, conversation.counterpart, token[1]); - break; + return; 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); - break; + 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; } - } else { - stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(text, conversation); } - text_input.buffer.text = ""; + stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(text, conversation); } private bool on_text_input_key_press(EventKey event) { -- cgit v1.2.3-70-g09d2