Implementing error type=modify handling for XEP-0077

This commit is contained in:
Alexander Bogdanov 2019-04-16 18:33:32 +02:00 committed by Philipp Hörist
parent 9e0fb54173
commit ff9afe194e
2 changed files with 63 additions and 25 deletions

View File

@ -92,7 +92,10 @@ class Register:
error = stanza.getErrorMsg() error = stanza.getErrorMsg()
log.info('Error: %s', error) log.info('Error: %s', error)
if error_cb() is not None: if error_cb() is not None:
error_cb()(error) form = is_form = None
if stanza.getTagAttr('error', 'type') == 'modify':
form, is_form = self._get_register_form(stanza)
error_cb()(error, form, is_form)
return return
self._con.get_module('Presence').subscribe(agent, auto_auth=True) self._con.get_module('Presence').subscribe(agent, auto_auth=True)
@ -116,8 +119,7 @@ class Register:
iq, self._register_info_response, {'success_cb': weak_success_cb, iq, self._register_info_response, {'success_cb': weak_success_cb,
'error_cb': weak_error_cb}) 'error_cb': weak_error_cb})
@staticmethod def _register_info_response(self, _con, stanza, success_cb, error_cb):
def _register_info_response(_con, stanza, success_cb, error_cb):
if not nbxmpp.isResultNode(stanza): if not nbxmpp.isResultNode(stanza):
error = stanza.getErrorMsg() error = stanza.getErrorMsg()
log.info('Error: %s', error) log.info('Error: %s', error)
@ -125,18 +127,28 @@ class Register:
error_cb()(error) error_cb()(error)
else: else:
log.info('Register form received') log.info('Register form received')
form = stanza.getQuery().getTag('x', namespace=nbxmpp.NS_DATA)
is_form = form is not None
if not is_form:
form = {}
for field in stanza.getQueryPayload():
if not isinstance(field, nbxmpp.Node):
continue
form[field.getName()] = field.getData()
if success_cb() is not None: if success_cb() is not None:
form, is_form = self._get_register_form(stanza)
success_cb()(form, is_form) success_cb()(form, is_form)
@staticmethod
def _get_register_form(stanza):
query = stanza.getTag('query')
if not query:
return None, False
form = query.getTag('x', namespace=nbxmpp.NS_DATA)
is_form = form is not None
if not is_form:
form = {}
for field in query.getPayload():
if not isinstance(field, nbxmpp.Node):
continue
form[field.getName()] = field.getData()
return form, is_form
def get_instance(*args, **kwargs): def get_instance(*args, **kwargs):
return Register(*args, **kwargs), 'Register' return Register(*args, **kwargs), 'Register'

View File

@ -75,6 +75,14 @@ class ServiceRegistration(Gtk.Assistant):
sidebar = main_box.get_children()[0] sidebar = main_box.get_children()[0]
main_box.remove(sidebar) main_box.remove(sidebar)
def _build_dataform(self, form, is_form):
if not is_form:
from gajim import config
return config.FakeDataForm(form)
dataform = dataforms.extend_form(node=form)
return DataFormWidget(dataform)
def _on_page_change(self, assistant, page): def _on_page_change(self, assistant, page):
if self.get_current_page() == Page.REQUEST: if self.get_current_page() == Page.REQUEST:
self._con.get_module('Register').get_register_form( self._con.get_module('Register').get_register_form(
@ -86,23 +94,22 @@ class ServiceRegistration(Gtk.Assistant):
def _on_get_success(self, form, is_form): def _on_get_success(self, form, is_form):
log.info('Show Form page') log.info('Show Form page')
self._is_form = is_form self._is_form = is_form
if is_form: self._data_form_widget = self._build_dataform(form, is_form)
dataform = dataforms.extend_form(node=form)
self._data_form_widget = DataFormWidget(dataform)
else:
from gajim import config
self._data_form_widget = config.FakeDataForm(form)
page = self.get_nth_page(Page.FORM) self.get_nth_page(Page.FORM).set_form(self._data_form_widget)
page.pack_start(self._data_form_widget, True, True, 0)
self._data_form_widget.show_all()
self.set_current_page(Page.FORM) self.set_current_page(Page.FORM)
def _on_error(self, error_text): def _on_error(self, error_text, form=None, is_form=False):
log.info('Show Error page') if form is not None:
page = self.get_nth_page(Page.ERROR) log.info('Show Form page')
page.set_text(error_text) self._is_form = is_form
self.set_current_page(Page.ERROR) self._data_form_widget = self._build_dataform(form, is_form)
self.get_nth_page(Page.FORM).set_form(self._data_form_widget, error_text=error_text)
self.set_current_page(Page.FORM)
else:
log.info('Show Error page')
self.get_nth_page(Page.ERROR).set_text(error_text)
self.set_current_page(Page.ERROR)
def _on_cancel(self, widget): def _on_cancel(self, widget):
self.destroy() self.destroy()
@ -159,6 +166,25 @@ class FormPage(Gtk.Box):
def __init__(self): def __init__(self):
super().__init__(orientation=Gtk.Orientation.VERTICAL) super().__init__(orientation=Gtk.Orientation.VERTICAL)
self._form = None
self._label = Gtk.Label()
self._label.set_no_show_all(True)
self._label.get_style_context().add_class('error-color')
self.pack_end(self._label, False, False, 0)
def set_form(self, form, error_text=None):
if self._form is not None:
self.remove(self._form)
self._form.destroy()
self._label.hide()
self._form = form
if error_text is not None:
self._label.set_text(error_text)
self._label.show()
self.pack_start(form, True, True, 0)
self._form.show_all()
class SuccessfulPage(Gtk.Box): class SuccessfulPage(Gtk.Box):