From 51096a0453252498be1f10e8a780a1c3ddd71e20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Thu, 29 Nov 2018 23:28:24 +0100 Subject: [PATCH] [form_handler] Add Form Handler plugin --- form_handler/__init__.py | 1 + form_handler/gtk/form.py | 63 +++++++++++++++++++++++++ form_handler/gtk/util.py | 29 ++++++++++++ form_handler/manifest.ini | 9 ++++ form_handler/plugin.py | 97 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 199 insertions(+) create mode 100644 form_handler/__init__.py create mode 100644 form_handler/gtk/form.py create mode 100644 form_handler/gtk/util.py create mode 100644 form_handler/manifest.ini create mode 100644 form_handler/plugin.py diff --git a/form_handler/__init__.py b/form_handler/__init__.py new file mode 100644 index 0000000..8876e6e --- /dev/null +++ b/form_handler/__init__.py @@ -0,0 +1 @@ +from form_handler.plugin import FormHandlerPlugin \ No newline at end of file diff --git a/form_handler/gtk/form.py b/form_handler/gtk/form.py new file mode 100644 index 0000000..0cddc25 --- /dev/null +++ b/form_handler/gtk/form.py @@ -0,0 +1,63 @@ +# Copyright (C) 2018 Philipp Hörist +# +# This file is part of Form Handler. +# +# Form Handler is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published +# by the Free Software Foundation; version 3 only. +# +# Form Handler is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Form Handler. If not, see . + +import nbxmpp +from gi.repository import Gtk + +from gajim.common import app +from gajim.common.modules.dataforms import extend_form +from gajim.common.connection_handlers_events import MessageOutgoingEvent + +from gajim.gtk.dataform import DataFormWidget + +from form_handler.gtk.util import find_control + + +class FormDialog(Gtk.ApplicationWindow): + def __init__(self, data): + transient = app.app.get_active_window() + Gtk.ApplicationWindow.__init__(self, title="Data Form Test") + self.set_transient_for(transient) + self.set_default_size(600, 400) + + self._account = data['account'] + self._jid = data['jid'] + + self._form_widget = DataFormWidget( + extend_form(node=nbxmpp.Node(node=data['form']))) + box = Gtk.Box(orientation='vertical', spacing=12) + box.add(self._form_widget) + + button = Gtk.Button(label=data['submit-text']) + button.connect('clicked', self._on_send_clicked) + button.set_halign(Gtk.Align.END) + box.add(button) + + self.add(box) + self.show_all() + + def _on_send_clicked(self, _button): + form = self._form_widget.get_submit_form() + app.nec.push_outgoing_event(MessageOutgoingEvent(None, + account=self._account, + jid=self._jid, + form_node=form, + is_loggable=False)) + control = find_control(self._account, self._jid) + if control is None: + return + control.print_conversation('Successful sent form', 'info') + self.destroy() diff --git a/form_handler/gtk/util.py b/form_handler/gtk/util.py new file mode 100644 index 0000000..d4e77c2 --- /dev/null +++ b/form_handler/gtk/util.py @@ -0,0 +1,29 @@ +# Copyright (C) 2018 Philipp Hörist +# +# This file is part of Form Handler. +# +# Form Handler is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published +# by the Free Software Foundation; version 3 only. +# +# Form Handler is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Form Handler. If not, see . + +from gi.repository import Gtk + +from gajim.common import app + + +def get_button(label, data, callback): + button = Gtk.Button(label=label) + button.connect('clicked', callback, data) + return button + + +def find_control(account, jid): + return app.interface.msg_win_mgr.get_control(jid, account) diff --git a/form_handler/manifest.ini b/form_handler/manifest.ini new file mode 100644 index 0000000..2d0ac1a --- /dev/null +++ b/form_handler/manifest.ini @@ -0,0 +1,9 @@ +[info] +name: Form Handler +short_name: form_handler +version: 1.2.1 +description: Lets the user diplay and answer forms attached to messages +authors = Philipp Hoerist +homepage: https://dev.gajim.org/gajim/gajim-plugins/wikis/formhandler +min_gajim_version: 1.1.91 +max_gajim_version: 1.2.90 diff --git a/form_handler/plugin.py b/form_handler/plugin.py new file mode 100644 index 0000000..bf8d785 --- /dev/null +++ b/form_handler/plugin.py @@ -0,0 +1,97 @@ +# Copyright (C) 2018 Philipp Hörist +# +# This file is part of Form Handler. +# +# Form Handler is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published +# by the Free Software Foundation; version 3 only. +# +# Form Handler is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Form Handler. If not, see . + +import logging + +import nbxmpp + +from gajim.common import ged +from gajim.common.modules.dataforms import extend_form + +from gajim.plugins import GajimPlugin +from gajim.plugins.plugins_i18n import _ + +from form_handler.gtk.util import get_button +from form_handler.gtk.form import FormDialog + +log = logging.getLogger('gajim.plugin_system.form_handler') + + +class FormHandlerPlugin(GajimPlugin): + def init(self): + + self.config_dialog = None + + self.events_handlers = { + 'decrypted-message-received': (ged.CORE, + self._on_message_received), + } + + self.gui_extension_points = { + 'print_real_text': (self._print_text, None), + } + + def _on_message_received(self, event): + form = event.stanza.getTag('x', namespace=nbxmpp.NS_DATA) + if form is None: + return + + if form.getAttr('type') != 'form': + return + + data = self._parse_form(form) + + data['form'] = str(form) + data['jid'] = event.jid + + event.additional_data['form_handler'] = data + + @staticmethod + def _parse_form(form): + dataform = extend_form(node=form) + result = {} + try: + result['submit-text'] = dataform['submit-button-text'].value + except KeyError: + result['submit-text'] = _('Submit') + + try: + result['open-text'] = dataform['open-button-text'].value + except KeyError: + result['open-text'] = _('Open') + + return result + + def _print_text(self, tv, _real_text, _text_tags, _graphics, + iter_, additional_data): + if 'form_handler' not in additional_data: + return + + data = additional_data['form_handler'] + data['account'] = tv.account + + button = get_button(data['open-text'], data, self._show_form) + + buffer_ = tv.tv.get_buffer() + anchor = buffer_.create_child_anchor(iter_) + anchor.plaintext = '' + + button.show_all() + tv.tv.add_child_at_anchor(button, anchor) + + @staticmethod + def _show_form(_button, data): + FormDialog(data)