Commit 9971a49d authored by Philipp Hörist's avatar Philipp Hörist

PubSub: Use nbxmpp methods

parent 9dc44a6f
Pipeline #6791 failed with stages
in 3 minutes and 47 seconds
......@@ -22,10 +22,8 @@
import nbxmpp
from nbxmpp.namespaces import Namespace
from nbxmpp.modules import dataforms
from gajim.common import app
from gajim.common.nec import NetworkIncomingEvent
from gajim.common.modules.base import BaseModule
......@@ -34,6 +32,9 @@ class PubSub(BaseModule):
_nbxmpp_extends = 'PubSub'
_nbxmpp_methods = [
'publish',
'delete',
'set_node_configuration',
'get_node_configuration',
]
def __init__(self, con):
......@@ -77,93 +78,6 @@ def send_pb_unsubscribe(self, jid, node, cb, **kwargs):
self._con.connection.SendAndCallForResponse(query, cb, kwargs)
def send_pb_delete(self, jid, node, on_ok=None, on_fail=None):
"""
Delete node
"""
if not app.account_is_available(self._account):
return
query = nbxmpp.Iq('set', to=jid)
pubsub = query.addChild('pubsub', namespace=Namespace.PUBSUB_OWNER)
pubsub.addChild('delete', {'node': node})
def response(_nbxmpp_client, resp, jid, node):
if resp.getType() == 'result' and on_ok:
on_ok(jid, node)
elif on_fail:
msg = resp.getErrorMsg()
on_fail(jid, node, msg)
self._con.connection.SendAndCallForResponse(
query, response, {'jid': jid, 'node': node})
def send_pb_configure(self, jid, node, form, cb=None, **kwargs):
if not app.account_is_available(self._account):
return
if cb is None:
cb = self._default_callback
query = nbxmpp.Iq('set', to=jid)
pubsub = query.addChild('pubsub', namespace=Namespace.PUBSUB_OWNER)
configure = pubsub.addChild('configure', {'node': node})
configure.addChild(node=form)
self._log.info('Send node config for %s', node)
self._con.connection.SendAndCallForResponse(query, cb, kwargs)
def request_pb_configuration(self, jid, node):
if not app.account_is_available(self._account):
return
query = nbxmpp.Iq('get', to=jid)
pubsub = query.addChild('pubsub', namespace=Namespace.PUBSUB_OWNER)
pubsub.addChild('configure', {'node': node})
self._log.info('Request node config for %s', node)
self._con.connection.SendAndCallForResponse(
query, self._received_pb_configuration, {'node': node})
def _received_pb_configuration(self, _nbxmpp_client, stanza, node):
if not nbxmpp.isResultNode(stanza):
self._log.warning('Error: %s', stanza.getError())
return
pubsub = stanza.getTag('pubsub', namespace=Namespace.PUBSUB_OWNER)
if pubsub is None:
self._log.warning('Malformed PubSub configure '
'stanza (no pubsub node): %s', stanza)
return
configure = pubsub.getTag('configure')
if configure is None:
self._log.warning('Malformed PubSub configure '
'stanza (no configure node): %s', stanza)
return
if configure.getAttr('node') != node:
self._log.warning('Malformed PubSub configure '
'stanza (wrong node): %s', stanza)
return
form = configure.getTag('x', namespace=Namespace.DATA)
if form is None:
self._log.warning('Malformed PubSub configure '
'stanza (no form): %s', stanza)
return
app.nec.push_incoming_event(PubSubConfigReceivedEvent(
None, conn=self._con, node=node,
form=dataforms.extend_form(node=form)))
def _default_callback(self, _con, stanza, *args, **kwargs):
if not nbxmpp.isResultNode(stanza):
self._log.warning('Error: %s', stanza.getError())
class PubSubConfigReceivedEvent(NetworkIncomingEvent):
name = 'pubsub-config-received'
def get_instance(*args, **kwargs):
return PubSub(*args, **kwargs), 'PubSub'
......@@ -12,12 +12,13 @@
# You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
import logging
from gi.repository import Gdk
from gi.repository import Gtk
from nbxmpp.errors import StanzaError
from gajim.common import app
from gajim.common import ged
from gajim.common.i18n import _
from gajim.common.helpers import to_user_string
......@@ -25,13 +26,14 @@
from gajim.gtk.dialogs import WarningDialog
from gajim.gtk.dataform import DataFormDialog
from gajim.gtk.util import get_builder
from gajim.gtk.util import EventHelper
class PEPConfig(Gtk.ApplicationWindow, EventHelper):
log = logging.getLogger('gajim.gtk.pep')
class PEPConfig(Gtk.ApplicationWindow):
def __init__(self, account):
Gtk.ApplicationWindow.__init__(self)
EventHelper.__init__(self)
self.set_application(app.app)
self.set_position(Gtk.WindowPosition.CENTER)
self.set_show_menubar(False)
......@@ -51,10 +53,6 @@ def __init__(self, account):
self._ui.services_treeview.get_selection().connect(
'changed', self._on_services_selection_changed)
self.register_events([
('pubsub-config-received', ged.GUI1, self._nec_pep_config_received),
])
self.show_all()
self.connect('key-press-event', self._on_key_press_event)
self._ui.connect_signals(self)
......@@ -97,9 +95,18 @@ def _items_received(self, task):
if item.jid == jid and item.node is not None:
self.treestore.append([item.node])
def _node_removed(self, jid, node):
if jid != app.get_jid_from_account(self.account):
def _on_node_delete(self, task):
node = task.get_user_data()
try:
task.finish()
except StanzaError as error:
WarningDialog(
_('PEP node was not removed'),
_('PEP node %(node)s was not removed:\n%(message)s') % {
'node': node, 'message': error})
return
model = self._ui.services_treeview.get_model()
iter_ = model.get_iter_first()
while iter_:
......@@ -108,25 +115,17 @@ def _node_removed(self, jid, node):
break
iter_ = model.iter_next(iter_)
def _node_not_removed(self, jid, node, msg):
if jid != app.get_jid_from_account(self.account):
return
WarningDialog(
_('PEP node was not removed'),
_('PEP node %(node)s was not removed:\n%(message)s') % {
'node': node, 'message': msg})
def _on_delete_button_clicked(self, _widget):
selection = self._ui.services_treeview.get_selection()
if not selection:
return
model, iter_ = selection.get_selected()
node = model[iter_][0]
our_jid = app.get_jid_from_account(self.account)
con = app.connections[self.account]
con.get_module('PubSub').send_pb_delete(our_jid, node,
on_ok=self._node_removed,
on_fail=self._node_not_removed)
con.get_module('PubSub').delete(node,
callback=self._on_node_delete,
user_data=node)
def _on_configure_button_clicked(self, _widget):
selection = self._ui.services_treeview.get_selection()
......@@ -134,18 +133,25 @@ def _on_configure_button_clicked(self, _widget):
return
model, iter_ = selection.get_selected()
node = model[iter_][0]
our_jid = app.get_jid_from_account(self.account)
con = app.connections[self.account]
con.get_module('PubSub').request_pb_configuration(our_jid, node)
con.get_module('PubSub').get_node_configuration(
node,
callback=self._nec_pep_config_received)
def _on_config_submit(self, form, node):
our_jid = app.get_jid_from_account(self.account)
con = app.connections[self.account]
con.get_module('PubSub').send_pb_configure(our_jid, node, form)
con.get_module('PubSub').set_node_configuration(node, form)
def _nec_pep_config_received(self, task):
try:
result = task.finish()
except Exception:
log.exception('Failed to retrive config')
return
def _nec_pep_config_received(self, obj):
DataFormDialog(_('Configure %s') % obj.node,
DataFormDialog(_('Configure %s') % result.node,
self,
obj.form,
obj.node,
result.form,
result.node,
self._on_config_submit)
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