Commit 51096a04 authored by Philipp Hörist's avatar Philipp Hörist

[form_handler] Add Form Handler plugin

parent ca331879
from form_handler.plugin import FormHandlerPlugin
\ No newline at end of file
# Copyright (C) 2018 Philipp Hörist <philipp AT hoerist.com>
#
# 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 <http://www.gnu.org/licenses/>.
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()
# Copyright (C) 2018 Philipp Hörist <philipp AT hoerist.com>
#
# 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 <http://www.gnu.org/licenses/>.
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)
[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 <philipp@hoerist.com>
homepage: https://dev.gajim.org/gajim/gajim-plugins/wikis/formhandler
min_gajim_version: 1.1.91
max_gajim_version: 1.2.90
# Copyright (C) 2018 Philipp Hörist <philipp AT hoerist.com>
#
# 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 <http://www.gnu.org/licenses/>.
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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment