Philipp Hörist pushed to branch master at gajim / gajim
Commits:
-
94b6b0c2
by lovetox at 2021-02-13T21:53:40+01:00
3 changed files:
Changes:
| ... | ... | @@ -435,6 +435,7 @@ def _finish_connect(self): |
| 435 | 435 |
|
| 436 | 436 |
self.get_module('Bookmarks').request_bookmarks()
|
| 437 | 437 |
self.get_module('SoftwareVersion').set_enabled(True)
|
| 438 |
+ self.get_module('LastActivity').set_enabled(True)
|
|
| 438 | 439 |
self.get_module('Annotations').request_annotations()
|
| 439 | 440 |
self.get_module('Blocking').get_blocking_list()
|
| 440 | 441 |
|
| ... | ... | @@ -14,45 +14,33 @@ |
| 14 | 14 |
|
| 15 | 15 |
# XEP-0012: Last Activity
|
| 16 | 16 |
|
| 17 |
-import nbxmpp
|
|
| 18 |
-from nbxmpp.namespaces import Namespace
|
|
| 19 |
-from nbxmpp.structs import StanzaHandler
|
|
| 20 |
- |
|
| 21 | 17 |
from gajim.common import app
|
| 22 | 18 |
from gajim.common import idle
|
| 23 | 19 |
from gajim.common.modules.base import BaseModule
|
| 24 | 20 |
|
| 25 | 21 |
|
| 26 | 22 |
class LastActivity(BaseModule):
|
| 23 |
+ |
|
| 24 |
+ _nbxmpp_extends = 'LastActivity'
|
|
| 25 |
+ _nbxmpp_methods = [
|
|
| 26 |
+ 'request_last_activity',
|
|
| 27 |
+ 'set_idle_func',
|
|
| 28 |
+ 'disable',
|
|
| 29 |
+ ]
|
|
| 30 |
+ |
|
| 27 | 31 |
def __init__(self, con):
|
| 28 | 32 |
BaseModule.__init__(self, con)
|
| 29 | 33 |
|
| 30 |
- self.handlers = [
|
|
| 31 |
- StanzaHandler(name='iq',
|
|
| 32 |
- typ='get',
|
|
| 33 |
- callback=self._answer_request,
|
|
| 34 |
- ns=Namespace.LAST),
|
|
| 35 |
- ]
|
|
| 36 |
- |
|
| 37 |
- def _answer_request(self, _con, stanza, properties):
|
|
| 38 |
- self._log.info('Request from %s', properties.jid)
|
|
| 39 |
- |
|
| 40 |
- allow_send = app.settings.get_account_setting(self._account,
|
|
| 41 |
- 'send_idle_time')
|
|
| 42 |
- if app.is_installed('IDLE') and allow_send:
|
|
| 43 |
- iq = stanza.buildReply('result')
|
|
| 44 |
- query = iq.setQuery()
|
|
| 45 |
- seconds = idle.Monitor.get_idle_sec()
|
|
| 46 |
- query.attrs['seconds'] = seconds
|
|
| 47 |
- self._log.info('Respond with seconds: %s', seconds)
|
|
| 48 |
- else:
|
|
| 49 |
- iq = stanza.buildReply('error')
|
|
| 50 |
- err = nbxmpp.ErrorNode(nbxmpp.ERR_SERVICE_UNAVAILABLE)
|
|
| 51 |
- iq.addChild(node=err)
|
|
| 52 |
- |
|
| 53 |
- self._con.connection.send(iq)
|
|
| 34 |
+ def set_enabled(self, enabled):
|
|
| 35 |
+ if enabled and app.is_installed('IDLE'):
|
|
| 36 |
+ if not app.settings.get_account_setting(self._account,
|
|
| 37 |
+ 'send_idle_time'):
|
|
| 38 |
+ return
|
|
| 54 | 39 |
|
| 55 |
- raise nbxmpp.NodeProcessed
|
|
| 40 |
+ self._nbxmpp('LastActivity').set_idle_func(
|
|
| 41 |
+ idle.Monitor.get_idle_sec)
|
|
| 42 |
+ else:
|
|
| 43 |
+ self._nbxmpp('LastActivity').disable()
|
|
| 56 | 44 |
|
| 57 | 45 |
|
| 58 | 46 |
def get_instance(*args, **kwargs):
|
| ... | ... | @@ -16,8 +16,8 @@ |
| 16 | 16 |
from collections import namedtuple
|
| 17 | 17 |
from datetime import timedelta
|
| 18 | 18 |
|
| 19 |
-import nbxmpp
|
|
| 20 | 19 |
from nbxmpp.errors import StanzaError
|
| 20 |
+from nbxmpp.errors import MalformedStanzaError
|
|
| 21 | 21 |
from nbxmpp.namespaces import Namespace
|
| 22 | 22 |
from gi.repository import Gtk
|
| 23 | 23 |
from gi.repository import Gdk
|
| ... | ... | @@ -67,7 +67,9 @@ def __init__(self, account): |
| 67 | 67 |
con = app.connections[account]
|
| 68 | 68 |
con.get_module('SoftwareVersion').request_software_version(
|
| 69 | 69 |
self.hostname, callback=self._software_version_received)
|
| 70 |
- self.request_last_activity()
|
|
| 70 |
+ |
|
| 71 |
+ con.get_module('LastActivity').request_last_activity(
|
|
| 72 |
+ self.hostname, callback=self._on_last_activity)
|
|
| 71 | 73 |
|
| 72 | 74 |
server_info = con.get_module('Discovery').server_info
|
| 73 | 75 |
self._add_contact_addresses(server_info.dataforms)
|
| ... | ... | @@ -129,13 +131,6 @@ def _on_cert_button_clicked(self, _button): |
| 129 | 131 |
transient_for=self,
|
| 130 | 132 |
cert=self.cert)
|
| 131 | 133 |
|
| 132 |
- def request_last_activity(self):
|
|
| 133 |
- if not app.account_is_connected(self.account):
|
|
| 134 |
- return
|
|
| 135 |
- con = app.connections[self.account]
|
|
| 136 |
- iq = nbxmpp.Iq(to=self.hostname, typ='get', queryNS=Namespace.LAST)
|
|
| 137 |
- con.connection.SendAndCallForResponse(iq, self._on_last_activity)
|
|
| 138 |
- |
|
| 139 | 134 |
def _add_contact_addresses(self, dataforms):
|
| 140 | 135 |
fields = {
|
| 141 | 136 |
'admin-addresses': _('Admin'),
|
| ... | ... | @@ -209,28 +204,20 @@ def _on_activate_link(self, label, *args): |
| 209 | 204 |
open_uri(label.get_text(), account=self.account)
|
| 210 | 205 |
return Gdk.EVENT_STOP
|
| 211 | 206 |
|
| 212 |
- def _on_last_activity(self, _nbxmpp_client, stanza):
|
|
| 213 |
- if self._destroyed:
|
|
| 214 |
- # Window got closed in the meantime
|
|
| 215 |
- return
|
|
| 216 |
- if not nbxmpp.isResultNode(stanza):
|
|
| 217 |
- log.warning('Received malformed result: %s', stanza)
|
|
| 218 |
- return
|
|
| 219 |
- if stanza.getQueryNS() != Namespace.LAST:
|
|
| 220 |
- log.warning('Wrong namespace on result: %s', stanza)
|
|
| 221 |
- return
|
|
| 207 |
+ def _on_last_activity(self, task):
|
|
| 222 | 208 |
try:
|
| 223 |
- seconds = int(stanza.getQuery().getAttr('seconds'))
|
|
| 224 |
- except (ValueError, TypeError, AttributeError):
|
|
| 225 |
- log.exception('Received malformed last activity result')
|
|
| 226 |
- else:
|
|
| 227 |
- delta = timedelta(seconds=seconds)
|
|
| 228 |
- hours = 0
|
|
| 229 |
- if seconds >= 3600:
|
|
| 230 |
- hours = delta.seconds // 3600
|
|
| 231 |
- uptime = _('%(days)s days, %(hours)s hours') % {
|
|
| 232 |
- 'days': delta.days, 'hours': hours}
|
|
| 233 |
- self._ui.server_uptime.set_text(uptime)
|
|
| 209 |
+ result = task.finish()
|
|
| 210 |
+ except (StanzaError, MalformedStanzaError) as error:
|
|
| 211 |
+ log.warning(error)
|
|
| 212 |
+ return
|
|
| 213 |
+ |
|
| 214 |
+ delta = timedelta(seconds=result.seconds)
|
|
| 215 |
+ hours = 0
|
|
| 216 |
+ if result.seconds >= 3600:
|
|
| 217 |
+ hours = delta.seconds // 3600
|
|
| 218 |
+ uptime = _('%(days)s days, %(hours)s hours') % {
|
|
| 219 |
+ 'days': delta.days, 'hours': hours}
|
|
| 220 |
+ self._ui.server_uptime.set_text(uptime)
|
|
| 234 | 221 |
|
| 235 | 222 |
def _software_version_received(self, task):
|
| 236 | 223 |
try:
|