From ce719a8317fb67d9b78589c3abd68329a284f139 Mon Sep 17 00:00:00 2001 From: Yann Leboulanger Date: Tue, 22 Aug 2017 21:53:32 +0200 Subject: [PATCH] No need to have one sleepy instance per connection object --- gajim/common/app.py | 8 +++++++ gajim/common/connection.py | 10 ++++----- gajim/common/connection_handlers.py | 21 +++---------------- .../zeroconf/connection_handlers_zeroconf.py | 12 ----------- 4 files changed, 15 insertions(+), 36 deletions(-) diff --git a/gajim/common/app.py b/gajim/common/app.py index 6191cbfd0..710970a3e 100644 --- a/gajim/common/app.py +++ b/gajim/common/app.py @@ -244,6 +244,14 @@ try: except ImportError: HAVE_PYCURL = False +try: + from gajim.common import sleepy + if sleepy.SUPPORTED: + HAVE_IDLE = True +except Exception: + log.debug(_('Unable to load idle module')) + HAVE_IDLE = False + gajim_identity = {'type': 'pc', 'category': 'client', 'name': 'Gajim'} gajim_common_features = [nbxmpp.NS_BYTESTREAM, nbxmpp.NS_SI, nbxmpp.NS_FILE, diff --git a/gajim/common/connection.py b/gajim/common/connection.py index e7880ff28..98fe747a0 100644 --- a/gajim/common/connection.py +++ b/gajim/common/connection.py @@ -630,9 +630,8 @@ class CommonConnection: self.connected = app.SHOW_LIST.index(show) idle_time = None if auto: - global HAS_IDLE - if HAS_IDLE and app.config.get('autoaway'): - idle_sec = int(self.sleeper.getIdleSec()) + if app.HAVE_IDLE and app.config.get('autoaway'): + idle_sec = int(app.interface.sleeper.getIdleSec()) idle_time = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(time.time() - idle_sec)) app.nec.push_incoming_event(BeforeChangeShowEvent(None, @@ -2705,9 +2704,8 @@ class Connection(CommonConnection, ConnectionHandlers): p = self.add_sha(p, ptype != 'unavailable') self.add_lang(p) if auto: - global HAS_IDLE - if HAS_IDLE and app.config.get('autoaway'): - idle_sec = int(self.sleeper.getIdleSec()) + if app.HAVE_IDLE and app.config.get('autoaway'): + idle_sec = int(app.interface.sleeper.getIdleSec()) idle_time = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(time.time() - idle_sec)) idle = p.setTag('idle', namespace=nbxmpp.NS_IDLE) diff --git a/gajim/common/connection_handlers.py b/gajim/common/connection_handlers.py index be62e8caf..0ce8d31ea 100644 --- a/gajim/common/connection_handlers.py +++ b/gajim/common/connection_handlers.py @@ -47,6 +47,7 @@ from gajim.common import app from gajim.common import exceptions from gajim.common import dataforms from gajim.common import jingle_xtls +from gajim.common import sleepy from gajim.common.commands import ConnectionCommands from gajim.common.pubsub import ConnectionPubSub from gajim.common.protocol.caps import ConnectionCaps @@ -74,13 +75,6 @@ DELIMITER_ARRIVED = 'delimiter_arrived' PRIVACY_ARRIVED = 'privacy_arrived' BLOCKING_ARRIVED = 'blocking_arrived' PEP_CONFIG = 'pep_config' -HAS_IDLE = True -try: -# import idle - from gajim.common import sleepy -except Exception: - log.debug(_('Unable to load idle module')) - HAS_IDLE = False class ConnectionDisco: @@ -1345,7 +1339,6 @@ ConnectionVcard, ConnectionSocks5Bytestream, ConnectionDisco, ConnectionCommands, ConnectionPubSub, ConnectionPEP, ConnectionCaps, ConnectionHandlersBase, ConnectionJingle, ConnectionIBBytestream): def __init__(self): - global HAS_IDLE ConnectionArchive313.__init__(self) ConnectionVcard.__init__(self) ConnectionSocks5Bytestream.__init__(self) @@ -1382,13 +1375,6 @@ ConnectionHandlersBase, ConnectionJingle, ConnectionIBBytestream): self.privacy_default_list = None - try: - self.sleeper = sleepy.Sleepy() - HAS_IDLE = True - except Exception: - log.warning('Error while calling Sleepy()', exc_info=True) - HAS_IDLE = False - self.gmail_last_tid = None self.gmail_last_time = None @@ -1659,14 +1645,13 @@ ConnectionHandlersBase, ConnectionJingle, ConnectionIBBytestream): raise nbxmpp.NodeProcessed def _nec_last_request_received(self, obj): - global HAS_IDLE if obj.conn.name != self.name: return - if HAS_IDLE and app.config.get_per('accounts', self.name, + if app.HAVE_IDLE and app.config.get_per('accounts', self.name, 'send_idle_time'): iq_obj = obj.stanza.buildReply('result') qp = iq_obj.setQuery() - qp.attrs['seconds'] = int(self.sleeper.getIdleSec()) + qp.attrs['seconds'] = int(app.interface.sleeper.getIdleSec()) else: iq_obj = obj.stanza.buildReply('error') err = nbxmpp.ErrorNode(name=nbxmpp.NS_STANZAS + \ diff --git a/gajim/common/zeroconf/connection_handlers_zeroconf.py b/gajim/common/zeroconf/connection_handlers_zeroconf.py index 584c6fbfc..d0aeaf846 100644 --- a/gajim/common/zeroconf/connection_handlers_zeroconf.py +++ b/gajim/common/zeroconf/connection_handlers_zeroconf.py @@ -39,12 +39,6 @@ STATUS_LIST = ['offline', 'connecting', 'online', 'chat', 'away', 'xa', 'dnd', VCARD_PUBLISHED = 'vcard_published' VCARD_ARRIVED = 'vcard_arrived' AGENT_REMOVED = 'agent_removed' -HAS_IDLE = True -try: - import idle -except Exception: - log.debug(_('Unable to load idle module')) - HAS_IDLE = False from gajim.common import connection_handlers @@ -73,12 +67,6 @@ connection_handlers.ConnectionJingle): connection_handlers.ConnectionJingle.__init__(self) connection_handlers.ConnectionHandlersBase.__init__(self) - try: - idle.init() - except Exception: - global HAS_IDLE - HAS_IDLE = False - def _messageCB(self, ip, con, msg): """ Called when we receive a message