cleanup, coding style, and logic fixes in sleepy

This commit is contained in:
Nikos Kouremenos 2005-09-10 23:44:53 +00:00
parent 7a8ff6d6f5
commit e7345baf54
2 changed files with 20 additions and 18 deletions

View File

@ -1,8 +1,9 @@
## common/sleepy.py ## common/sleepy.py
## ##
## Gajim Team: ## Gajim Team:
## - Yann Le Boulanger <asterix@lagaule.org> ## - Yann Le Boulanger <asterix@lagaule.org>
## - Vincent Hanquez <tab@snarc.org> ## - Vincent Hanquez <tab@snarc.org>
## - Nikos Kouremenos <kourem@gmail.com>
## ##
## Copyright (C) 2003-2005 Gajim Team ## Copyright (C) 2003-2005 Gajim Team
## ##
@ -20,11 +21,11 @@ from common import gajim
STATE_UNKNOWN = 'OS probably not supported' STATE_UNKNOWN = 'OS probably not supported'
STATE_XAWAY = 'extanted away' STATE_XA = 'extanted away'
STATE_AWAY = 'away' STATE_AWAY = 'away'
STATE_AWAKE = 'awake' STATE_AWAKE = 'awake'
SUPPORTED = 1 SUPPORTED = True
try: try:
import common.idle as idle # when we launch gajim from sources import common.idle as idle # when we launch gajim from sources
except: except:
@ -32,32 +33,32 @@ except:
import idle # when Gajim is installed import idle # when Gajim is installed
except: except:
gajim.log.debug('Unable to load idle module') gajim.log.debug('Unable to load idle module')
SUPPORTED = 0 SUPPORTED = False
class Sleepy: class Sleepy:
def __init__(self, interval1 = 60, interval2 = 120): def __init__(self, away_interval = 60, xa_interval = 120):
self.away_interval = away_interval
self.interval1 = interval1 self.xa_interval = xa_interval
self.interval2 = interval2 self.state = STATE_AWAKE # assume we are awake
self.state = STATE_AWAKE ## assume were awake to stake with
try: try:
idle.init() idle.init()
except: except:
SUPPORTED = 0 SUPPORTED = False
self.state = STATE_UNKNOWN self.state = STATE_UNKNOWN
def poll(self): def poll(self):
if not SUPPORTED: return 0 if not SUPPORTED:
return False
idleTime = idle.getIdleSec() idleTime = idle.getIdleSec()
if idleTime > self.interval2: if idleTime > self.xa_interval:
self.state = STATE_XAWAY self.state = STATE_XA
elif idleTime > self.interval1: elif idleTime > self.away_interval:
self.state = STATE_AWAY self.state = STATE_AWAY
else: else:
self.state = STATE_AWAKE self.state = STATE_AWAKE
return 1 return True
def getState(self): def getState(self):
return self.state return self.state

View File

@ -823,7 +823,8 @@ class Interface:
def read_sleepy(self): def read_sleepy(self):
'''Check idle status and change that status if needed''' '''Check idle status and change that status if needed'''
if not self.sleeper.poll(): if not self.sleeper.poll():
return True # renew timeout (loop for ever) # idle detection is not supported in that OS
return False # stop looping in vain
state = self.sleeper.getState() state = self.sleeper.getState()
for account in gajim.connections: for account in gajim.connections:
if not gajim.sleeper_state.has_key(account) or \ if not gajim.sleeper_state.has_key(account) or \
@ -845,7 +846,7 @@ class Interface:
self.roster.send_status(account, 'away', self.roster.send_status(account, 'away',
gajim.config.get('autoaway_message'), True) gajim.config.get('autoaway_message'), True)
gajim.sleeper_state[account] = 'autoaway' gajim.sleeper_state[account] = 'autoaway'
elif state == common.sleepy.STATE_XAWAY and (\ elif state == common.sleepy.STATE_XA and (\
gajim.sleeper_state[account] == 'autoaway' or \ gajim.sleeper_state[account] == 'autoaway' or \
gajim.sleeper_state[account] == 'online') and \ gajim.sleeper_state[account] == 'online') and \
gajim.config.get('autoxa'): gajim.config.get('autoxa'):
@ -1165,7 +1166,7 @@ class Interface:
gtk.window_set_default_icon(pix) # set the icon to all newly opened windows gtk.window_set_default_icon(pix) # set the icon to all newly opened windows
self.roster.window.set_icon_from_file(path_to_file) # and to roster window self.roster.window.set_icon_from_file(path_to_file) # and to roster window
self.sleeper = common.sleepy.Sleepy( self.sleeper = common.sleepy.Sleepy(
gajim.config.get('autoawaytime') * 60, gajim.config.get('autoawaytime') * 60, # make minutes to seconds
gajim.config.get('autoxatime') * 60) gajim.config.get('autoxatime') * 60)
self.systray_enabled = False self.systray_enabled = False