Commit c5df74c5 authored by Philipp Hörist's avatar Philipp Hörist

Refactor idle module

- Get rid of sleepy.py, handle everything in idle.py
- Introduce one Monitor class that handles everything
parent 2e5d966f
......@@ -276,8 +276,8 @@ def detect_dependencies():
# IDLE
try:
from gajim.common import sleepy
if sleepy.SUPPORTED:
from gajim.common import idle
if idle.Monitor.is_available():
_dependencies['IDLE'] = True
except Exception:
pass
......
......@@ -61,6 +61,7 @@ from gajim.common import app
from gajim.common import gpg
from gajim.common import passwords
from gajim.common import i18n
from gajim.common import idle
from gajim.common.connection_handlers import *
from gajim.common.contacts import GC_Contact
from gajim.gtkgui_helpers import get_action
......@@ -596,7 +597,7 @@ class CommonConnection:
idle_time = None
if auto:
if app.is_installed('IDLE') and app.config.get('autoaway'):
idle_sec = int(app.interface.sleeper.getIdleSec())
idle_sec = idle.Monitor.get_idle_sec()
idle_time = time.strftime('%Y-%m-%dT%H:%M:%SZ',
time.gmtime(time.time() - idle_sec))
app.nec.push_incoming_event(BeforeChangeShowEvent(None,
......@@ -2025,8 +2026,8 @@ class Connection(CommonConnection, ConnectionHandlers):
if signed:
p.setTag(nbxmpp.NS_SIGNED + ' x').setData(signed)
if idle_time:
idle = p.setTag('idle', namespace=nbxmpp.NS_IDLE)
idle.setAttr('since', idle_time)
idle_node = p.setTag('idle', namespace=nbxmpp.NS_IDLE)
idle_node.setAttr('since', idle_time)
if self.connection:
self.connection.send(p)
self.priority = priority
......@@ -2761,11 +2762,11 @@ class Connection(CommonConnection, ConnectionHandlers):
self.add_lang(p)
if auto:
if app.is_installed('IDLE') and app.config.get('autoaway'):
idle_sec = int(app.interface.sleeper.getIdleSec())
idle_sec = idle.Monitor.get_idle_sec()
idle_time = time.strftime('%Y-%m-%dT%H:%M:%SZ',
time.gmtime(time.time() - idle_sec))
idle = p.setTag('idle', namespace=nbxmpp.NS_IDLE)
idle.setAttr('since', idle_time)
idle_node = p.setTag('idle', namespace=nbxmpp.NS_IDLE)
idle_node.setAttr('since', idle_time)
# send instantly so when we go offline, status is sent to gc before we
# disconnect from jabber server
self.connection.send(p)
......
......@@ -48,6 +48,7 @@ from gajim.common import app
from gajim.common import dataforms
from gajim.common import jingle_xtls
from gajim.common import configpaths
from gajim.common import idle
from gajim.common.caps_cache import muc_caps_cache
from gajim.common.commands import ConnectionCommands
from gajim.common.pubsub import ConnectionPubSub
......@@ -1730,7 +1731,7 @@ ConnectionHTTPUpload):
'send_idle_time'):
iq_obj = obj.stanza.buildReply('result')
qp = iq_obj.setQuery()
qp.attrs['seconds'] = int(app.interface.sleeper.getIdleSec())
qp.attrs['seconds'] = idle.Monitor.get_idle_sec()
else:
iq_obj = obj.stanza.buildReply('error')
err = nbxmpp.ErrorNode(name=nbxmpp.NS_STANZAS + \
......
......@@ -106,6 +106,12 @@ class JIDConstant(IntEnum):
NORMAL_TYPE = 0
ROOM_TYPE = 1
@unique
class IdleState(IntEnum):
UNKNOWN = 0
XA = 1
AWAY = 2
AWAKE = 3
SSLError = {
2: _("Unable to get issuer certificate"),
......
This diff is collapsed.
# -*- coding:utf-8 -*-
## src/common/sleepy.py
##
## Copyright (C) 2003-2014 Yann Leboulanger <asterix AT lagaule.org>
## Copyright (C) 2005-2006 Nikos Kouremenos <kourem AT gmail.com>
## Copyright (C) 2007 Jean-Marie Traissard <jim AT lapin.org>
## Copyright (C) 2008 Mateusz Biliński <mateusz AT bilinski.it>
##
## 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 gajim.common import app
import os
import logging
log = logging.getLogger('gajim.c.sleepy')
STATE_UNKNOWN = 'OS probably not supported'
STATE_XA = 'extended away'
STATE_AWAY = 'away'
STATE_AWAKE = 'awake'
SUPPORTED = True
try:
if os.name == 'nt':
import ctypes
GetTickCount = ctypes.windll.kernel32.GetTickCount
GetLastInputInfo = ctypes.windll.user32.GetLastInputInfo
class LASTINPUTINFO(ctypes.Structure):
_fields_ = [('cbSize', ctypes.c_uint), ('dwTime', ctypes.c_uint)]
lastInputInfo = LASTINPUTINFO()
lastInputInfo.cbSize = ctypes.sizeof(lastInputInfo)
# one or more of these may not be supported before XP.
OpenInputDesktop = ctypes.windll.user32.OpenInputDesktop
CloseDesktop = ctypes.windll.user32.CloseDesktop
SystemParametersInfo = ctypes.windll.user32.SystemParametersInfoW
else: # unix
from gajim.common import idle
except Exception:
log.warning('Unable to load idle module')
SUPPORTED = False
class SleepyWindows:
def __init__(self, away_interval = 60, xa_interval = 120):
self.away_interval = away_interval
self.xa_interval = xa_interval
self.state = STATE_AWAKE # assume we are awake
def getIdleSec(self):
GetLastInputInfo(ctypes.byref(lastInputInfo))
idleDelta = float(GetTickCount() - lastInputInfo.dwTime) / 1000
return idleDelta
def poll(self):
"""
Check to see if we should change state
"""
if not SUPPORTED:
return False
# screen saver, in windows >= XP
saver_runing = ctypes.c_int(0)
# 0x72 is SPI_GETSCREENSAVERRUNNING
if SystemParametersInfo(0x72, 0, ctypes.byref(saver_runing), 0) and \
saver_runing.value:
self.state = STATE_XA
return True
desk = OpenInputDesktop(0, False, 0)
if not desk:
# Screen locked
self.state = STATE_XA
return True
CloseDesktop(desk)
idleTime = self.getIdleSec()
# xa is stronger than away so check for xa first
if idleTime > self.xa_interval:
self.state = STATE_XA
elif idleTime > self.away_interval:
self.state = STATE_AWAY
else:
self.state = STATE_AWAKE
return True
def getState(self):
return self.state
def setState(self, val):
self.state = val
class SleepyUnix:
def __init__(self, away_interval = 60, xa_interval = 120):
global SUPPORTED
self.away_interval = away_interval
self.xa_interval = xa_interval
self.state = STATE_AWAKE # assume we are awake
def getIdleSec(self):
return idle.getIdleSec()
def poll(self):
"""
Check to see if we should change state
"""
if not SUPPORTED:
return False
idleTime = self.getIdleSec()
# xa is stronger than away so check for xa first
if idleTime > self.xa_interval:
self.state = STATE_XA
elif idleTime > self.away_interval:
self.state = STATE_AWAY
else:
self.state = STATE_AWAKE
return True
def getState(self):
return self.state
def setState(self, val):
self.state = val
if os.name == 'nt':
Sleepy = SleepyWindows
else:
Sleepy = SleepyUnix
......@@ -40,7 +40,7 @@ from gi.repository import GObject
from gi.repository import GLib
from gajim.common import config as c_config
from gajim.common import sleepy
from gajim.common import idle
from gajim.common.i18n import Q_
from gajim import gtkgui_helpers
......@@ -353,7 +353,7 @@ class PreferencesWindow:
self.auto_xa_message_entry.set_text(st)
self.auto_xa_message_entry.set_sensitive(app.config.get('autoxa'))
if not sleepy.SUPPORTED:
if not idle.Monitor.is_available():
self.xml.get_object('autoaway_table').set_sensitive(False)
# ask_status when online / offline
......@@ -925,9 +925,8 @@ class PreferencesWindow:
def on_auto_away_time_spinbutton_value_changed(self, widget):
aat = widget.get_value_as_int()
app.config.set('autoawaytime', aat)
app.interface.sleeper = sleepy.Sleepy(
app.config.get('autoawaytime') * 60,
app.config.get('autoxatime') * 60)
idle.Monitor.set_interval(app.config.get('autoawaytime') * 60,
app.config.get('autoxatime') * 60)
def on_auto_away_message_entry_changed(self, widget):
app.config.set('autoaway_message', widget.get_text())
......@@ -939,9 +938,8 @@ class PreferencesWindow:
def on_auto_xa_time_spinbutton_value_changed(self, widget):
axt = widget.get_value_as_int()
app.config.set('autoxatime', axt)
app.interface.sleeper = sleepy.Sleepy(
app.config.get('autoawaytime') * 60,
app.config.get('autoxatime') * 60)
idle.Monitor.set_interval(app.config.get('autoawaytime') * 60,
app.config.get('autoxatime') * 60)
def on_auto_xa_message_entry_changed(self, widget):
app.config.set('autoxa_message', widget.get_text())
......
......@@ -163,8 +163,8 @@ class FeaturesWindow:
return app.is_installed('GSPELL')
def idle_available(self):
from gajim.common import sleepy
return sleepy.SUPPORTED
from gajim.common import idle
return idle.Monitor.is_available()
def pycrypto_available(self):
return app.is_installed('PYCRYPTO')
......@@ -181,4 +181,3 @@ class FeaturesWindow:
def gupnp_igd_available(self):
return app.is_installed('UPNP')
......@@ -79,7 +79,7 @@ from gajim.filetransfers_window import FileTransfersWindow
from gajim.atom_window import AtomWindow
from gajim.session import ChatControlSession
from gajim.common import sleepy
from gajim.common import idle
from nbxmpp import idlequeue
from nbxmpp import Hashes2
......@@ -1114,13 +1114,13 @@ class Interface:
app.logger.insert_jid(obj.conn.get_own_jid().getStripped())
account = obj.conn.name
app.block_signed_in_notifications[account] = True
state = self.sleeper.getState()
connected = obj.conn.connected
if state != sleepy.STATE_UNKNOWN and connected in (2, 3):
if idle.Monitor.is_unknown() and connected in (2, 3):
# we go online or free for chat, so we activate auto status
app.sleeper_state[account] = 'online'
elif not ((state == sleepy.STATE_AWAY and connected == 4) or \
(state == sleepy.STATE_XA and connected == 5)):
elif not ((idle.Monitor.is_away() and connected == 4) or \
(idle.Monitor.is_xa() and connected == 5)):
# If we are autoaway/xa and come back after a disconnection, do
# nothing
# Else disable autoaway
......@@ -2239,15 +2239,15 @@ class Interface:
"""
Check idle status and change that status if needed
"""
if not self.sleeper.poll():
if not idle.Monitor.poll():
# idle detection is not supported in that OS
return False # stop looping in vain
state = self.sleeper.getState()
for account in app.connections:
if account not in app.sleeper_state or \
not app.sleeper_state[account]:
continue
if state == sleepy.STATE_AWAKE:
if idle.Monitor.is_awake():
if app.sleeper_state[account] in ('autoaway', 'autoxa'):
# we go online
self.roster.send_status(account, 'online',
......@@ -2261,7 +2261,7 @@ class Interface:
app.status_before_autoaway[account])
app.status_before_autoaway[account] = ''
app.sleeper_state[account] = 'off'
elif state == sleepy.STATE_AWAY and app.config.get('autoaway'):
elif idle.Monitor.is_away() and app.config.get('autoaway'):
if app.sleeper_state[account] == 'online':
# we save out online status
app.status_before_autoaway[account] = \
......@@ -2288,7 +2288,7 @@ class Interface:
self.roster.send_status(account, app.SHOW_LIST[connected],
app.status_before_autoaway[account], auto=True)
app.sleeper_state[account] = 'idle'
elif state == sleepy.STATE_XA and \
elif idle.Monitor.is_xa() and \
app.sleeper_state[account] in ('online', 'autoaway',
'autoaway-forced') and app.config.get('autoxa'):
# we go extended away [we pass True to auto param]
......@@ -2913,9 +2913,8 @@ class Interface:
self.show_vcard_when_connect = []
self.sleeper = sleepy.Sleepy(
app.config.get('autoawaytime') * 60, # make minutes to seconds
app.config.get('autoxatime') * 60)
idle.Monitor.set_interval(app.config.get('autoawaytime') * 60,
app.config.get('autoxatime') * 60)
gtkgui_helpers.make_jabber_state_images()
......
......@@ -46,7 +46,6 @@ import locale
from enum import IntEnum, unique
from gajim.common import sleepy
from gajim import history_window
from gajim import dialogs
from gajim import vcard
......@@ -62,6 +61,7 @@ from gajim.common.const import AvatarSize
from gajim.common import app
from gajim.common import helpers
from gajim.common import idle
from gajim.common.exceptions import GajimGeneralException
from gajim.common import i18n
if app.is_installed('GEOCLUE'):
......@@ -2125,8 +2125,7 @@ class RosterWindow:
def send_status_continue(self, account, status, txt, auto, to):
if app.account_is_connected(account) and not to:
if status == 'online' and app.interface.sleeper.getState() != \
sleepy.STATE_UNKNOWN:
if status == 'online' and not idle.Monitor.is_unknown():
app.sleeper_state[account] = 'online'
elif app.sleeper_state[account] not in ('autoaway', 'autoxa') or \
status == 'offline':
......
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