Refactor FakeDataFormWidget

- Add test
- Support oob redirect url
This commit is contained in:
Philipp Hörist 2019-04-19 19:36:32 +02:00
parent 9f4adbdd1c
commit 8bfe90c5fe
7 changed files with 132 additions and 101 deletions

View File

@ -134,9 +134,14 @@ class Register(BaseModule):
is_form = form is not None
if not is_form:
form = {}
oob = stanza.getQuery().getTag('x', namespace=nbxmpp.NS_X_OOB)
if oob is not None:
form['redirect-url'] = oob.getTagData('url')
for field in stanza.getQueryPayload():
if not isinstance(field, nbxmpp.Node):
continue
if field.getName() == 'x':
continue
form[field.getName()] = field.getData()
return form, is_form

View File

@ -1,83 +0,0 @@
# Copyright (C) 2003-2005 Vincent Hanquez <tab AT snarc.org>
# Copyright (C) 2003-2014 Yann Leboulanger <asterix AT lagaule.org>
# Copyright (C) 2005 Alex Podaras <bigpod AT gmail.com>
# Stéphan Kochen <stephan AT kochen.nl>
# Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com>
# Nikos Kouremenos <kourem AT gmail.com>
# Copyright (C) 2006 Junglecow J <junglecow AT gmail.com>
# Copyright (C) 2006-2007 Travis Shirk <travis AT pobox.com>
# Stefan Bethge <stefan AT lanpartei.de>
# Copyright (C) 2006-2008 Jean-Marie Traissard <jim AT lapin.org>
# Copyright (C) 2007 James Newton <redshodan AT gmail.com>
# Julien Pivotto <roidelapluie AT gmail.com>
# Copyright (C) 2007-2008 Stephan Erb <steve-e AT h3c.de>
# Copyright (C) 2008 Jonathan Schleifer <js-gajim AT webkeks.org>
#
# This file is part of Gajim.
#
# Gajim 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.
#
# Gajim 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 Gajim. If not, see <http://www.gnu.org/licenses/>.
from gi.repository import Gtk
from gi.repository import GObject
class FakeDataForm(Gtk.Table):
"""
Class for forms that are in XML format <entry1>value1</entry1> infos in a
table {entry1: value1}
"""
def __init__(self, infos, selectable=False):
GObject.GObject.__init__(self)
self.infos = infos
self.selectable = selectable
self.entries = {}
self._draw_table()
def _draw_table(self):
"""
Draw the table
"""
nbrow = 0
if 'instructions' in self.infos:
nbrow = 1
self.resize(rows=nbrow, columns=2)
label = Gtk.Label(label=self.infos['instructions'])
if self.selectable:
label.set_selectable(True)
self.attach(label, 0, 2, 0, 1, 0, 0, 0, 0)
for name in self.infos.keys():
if name in ('key', 'instructions', 'x', 'registered'):
continue
if not name:
continue
nbrow = nbrow + 1
self.resize(rows=nbrow, columns=2)
label = Gtk.Label(label=name.capitalize() + ':')
self.attach(label, 0, 1, nbrow - 1, nbrow, 0, 0, 0, 0)
entry = Gtk.Entry()
entry.set_activates_default(True)
if self.infos[name]:
entry.set_text(self.infos[name])
if name == 'password':
entry.set_visibility(False)
self.attach(entry, 1, 2, nbrow - 1, nbrow, 0, 0, 0, 0)
self.entries[name] = entry
if nbrow == 1:
entry.grab_focus()
def get_infos(self):
for name in self.entries:
self.infos[name] = self.entries[name].get_text()
return self.infos

View File

@ -29,11 +29,10 @@ from gajim.common.modules import dataforms
from gajim import dataforms_widget
from gajim import gui_menu_builder
from gajim.config import FakeDataForm
from gajim.gtk.util import get_builder
from gajim.gtk.dialogs import ErrorDialog
from gajim.gtk.proxies import ManageProxies
from gajim.gtk.dataform import FakeDataFormWidget
class AccountCreationWizard:
@ -325,7 +324,7 @@ class AccountCreationWizard:
if self.is_form:
form = self.data_form_widget.data_form
else:
form = self.data_form_widget.get_infos()
form = self.data_form_widget.get_submit_form()
app.connections[self.account].send_new_account_infos(
form, self.is_form)
self.xml.get_object('form_vbox').remove(self.data_form_widget)
@ -381,7 +380,7 @@ class AccountCreationWizard:
self.data_form_widget.set_data_form(dataform)
empty_config = False
else:
self.data_form_widget = FakeDataForm(obj.config, selectable=True)
self.data_form_widget = FakeDataFormWidget(obj.config)
for field in obj.config:
if field in ('key', 'instructions', 'x', 'registered'):
continue

View File

@ -21,6 +21,7 @@ from gajim.gtkgui_helpers import scale_pixbuf_from_data
from gajim.common import app
from gajim.common.i18n import _
from gajim.common.helpers import launch_browser_mailer
from gajim.common.modules.dataforms import extend_form
from gajim.gtk.util import MultiLineLabel
@ -554,6 +555,76 @@ class ImageMediaField():
form_grid.attach(self._image, 1, row_number, 1, 1)
class FakeDataFormWidget(Gtk.ScrolledWindow):
def __init__(self, fields):
Gtk.ScrolledWindow.__init__(self)
self.set_hexpand(True)
self.set_vexpand(True)
self.set_overlay_scrolling(False)
self.get_style_context().add_class('data-form-widget')
self._grid = Gtk.Grid()
self._grid.set_column_spacing(12)
self._grid.set_row_spacing(12)
self._grid.set_halign(Gtk.Align.CENTER)
self._fields = fields
self._entries = {}
self._row_count = 0
instructions = fields.pop('instructions', None)
if instructions is not None:
label = Gtk.Label(label=instructions)
label.set_justify(Gtk.Justification.CENTER)
label.set_max_width_chars(40)
label.set_line_wrap(True)
label.set_line_wrap_mode(Pango.WrapMode.WORD)
self._grid.attach(label, 0, self._row_count, 2, 1)
self._row_count += 1
redirect_url = fields.pop('redirect-url', None)
if not fields and redirect_url is not None:
# Server wants to redirect registration
button = Gtk.Button(label='Register')
button.set_halign(Gtk.Align.CENTER)
button.get_style_context().add_class('suggested-action')
button.connect('clicked',
lambda *args: launch_browser_mailer('url',
redirect_url))
self._grid.attach(button, 0, self._row_count, 2, 1)
else:
self._add_fields()
self.add(self._grid)
self.show_all()
def _add_fields(self):
for name, value in self._fields.items():
if name in ('key', 'x', 'registered'):
continue
label = Gtk.Label(label=name.capitalize())
label.set_xalign(1)
self._grid.attach(label, 0, self._row_count, 1, 1)
self._row_count += 1
entry = Gtk.Entry()
entry.set_text(value)
if name == 'password':
entry.set_input_purpose(Gtk.InputPurpose.PASSWORD)
entry.set_visibility(False)
self._entries[name] = entry
self._grid.attach_next_to(entry, label,
Gtk.PositionType.RIGHT, 1, 1)
def get_submit_form(self):
fields = {}
for name, entry in self._entries.items():
fields[name] = entry.get_text()
return fields
class DataFormDialog(Gtk.Dialog):
def __init__(self, title, transient_for, form, node, submit_callback):
Gtk.Dialog.__init__(self,

View File

@ -25,11 +25,11 @@ from gajim.common.modules import dataforms
from gajim.common.i18n import _
from gajim import vcard
from gajim import config
from gajim import dataforms_widget
from gajim.gtk.util import get_builder
from gajim.gtk.add_contact import AddNewContactWindow
from gajim.gtk.dataform import FakeDataFormWidget
class SearchWindow:
@ -92,7 +92,7 @@ class SearchWindow:
con.get_module('Search').send_search_form(
self.jid, self.data_form_widget.data_form.get_purged(), True)
else:
infos = self.data_form_widget.get_infos()
infos = self.data_form_widget.get_submit_form()
if 'instructions' in infos:
del infos['instructions']
con.get_module('Search').send_search_form(self.jid, infos, False)
@ -146,7 +146,7 @@ class SearchWindow:
'%s - Search - Gajim' % self.data_form_widget.title)
else:
self.is_form = False
self.data_form_widget = config.FakeDataForm(obj.data)
self.data_form_widget = FakeDataFormWidget(obj.data)
self.data_form_widget.show_all()
self._ui.search_vbox.pack_start(self.data_form_widget, True, True, 0)

View File

@ -22,6 +22,7 @@ from gajim.common.modules import dataforms
from gajim.common.i18n import _
from gajim.gtk.dataform import DataFormWidget
from gajim.gtk.dataform import FakeDataFormWidget
log = logging.getLogger('gajim.gtk.registration')
@ -77,8 +78,7 @@ class ServiceRegistration(Gtk.Assistant):
def _build_dataform(self, form, is_form):
if not is_form:
from gajim import config
return config.FakeDataForm(form)
return FakeDataFormWidget(form)
dataform = dataforms.extend_form(node=form)
@ -122,15 +122,7 @@ class ServiceRegistration(Gtk.Assistant):
def _register(self):
log.info('Show Sending page')
if self._is_form:
form = self._data_form_widget.get_submit_form()
else:
form = self._data_form_widget.get_infos()
if 'instructions' in form:
del form['instructions']
if 'registered' in form:
del form['registered']
form = self._data_form_widget.get_submit_form()
self._con.get_module('Register').register_agent(
self._agent,
form,

47
test/gtk/fake_dataform.py Normal file
View File

@ -0,0 +1,47 @@
from gi.repository import Gtk
from gajim.gtk.dataform import FakeDataFormWidget
from gajim.common.const import CSSPriority
from test.gtk import util
util.load_style('gajim.css', CSSPriority.APPLICATION)
fake_form = {
'instructions': 'This is the a long long long long long long test instruction',
'username': '',
'nick': '',
'password': '',
'name': '',
'first': '',
'last': '',
'email': '',
'address': '',
'city': '',
'state': '',
'zip': '',
'phone': '',
'url': '',
'date': '',
'misc': '',
'text': '',
'key': '',
}
fake_form2 = {
'instructions': 'To register, visit https://jabber.at/account/register/',
'redirect-url': 'https://jabber.at/account/register/'
}
class DataFormWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Data Form Test")
self.set_default_size(600, 600)
self._widget = FakeDataFormWidget(fake_form2)
self.add(self._widget)
self.show()
win = DataFormWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()