Better idle support. Idle module is now optional

This commit is contained in:
Yann Leboulanger 2004-10-20 22:53:15 +00:00
parent 0f00e7ad8a
commit 3241f32e11
3 changed files with 41 additions and 44 deletions

View file

@ -2,7 +2,6 @@
"""A Quick class to tell if theres any activity on your machine""" """A Quick class to tell if theres any activity on your machine"""
import time import time
import idle
from string import find, lower from string import find, lower
@ -11,11 +10,15 @@ STATE_XAWAY = "extanted away"
STATE_AWAY = "away" STATE_AWAY = "away"
STATE_AWAKE = "awake" STATE_AWAKE = "awake"
NOT_SUPPORTED = 0 SUPPORTED = 1
try:
import idle
except:
SUPPORTED = 0
class Sleepy: class Sleepy:
def __init__(self, interval1 = 60, interval2 = 120, devices = ['keyboard', 'mouse', 'ts'] ): def __init__(self, interval1 = 60, interval2 = 120):
self.interval1 = interval1 self.interval1 = interval1
self.interval2 = interval2 self.interval2 = interval2
@ -23,12 +26,11 @@ class Sleepy:
try: try:
idle.init() idle.init()
except: except:
NOT_SUPPORTED = 1 SUPPORTED = 0
self.state = STATE_UNKNOWN self.state = STATE_UNKNOWN
def poll(self): def poll(self):
if NOT_SUPPORTED: return -1 if not SUPPORTED: return 0
now = time.time()
idleTime = idle.getIdleSec() idleTime = idle.getIdleSec()
if idleTime > self.interval2: if idleTime > self.interval2:

View file

@ -235,7 +235,6 @@ class preference_Window:
self.plugin.config['autoxa'] = 0 self.plugin.config['autoxa'] = 0
axt = self.spin_autoxatime.get_value_as_int() axt = self.spin_autoxatime.get_value_as_int()
self.plugin.config['autoxatime'] = axt self.plugin.config['autoxatime'] = axt
if self.chk_autoaway.get_active() or self.chk_autoxa.get_active():
self.plugin.sleeper = common.sleepy.Sleepy(\ self.plugin.sleeper = common.sleepy.Sleepy(\
self.plugin.config['autoawaytime']*60, \ self.plugin.config['autoawaytime']*60, \
self.plugin.config['autoxatime']*60) self.plugin.config['autoxatime']*60)

View file

@ -1364,7 +1364,8 @@ class roster_Window:
passphrase = '' passphrase = ''
self.plugin.send('PASSPHRASE', account, passphrase) self.plugin.send('PASSPHRASE', account, passphrase)
self.plugin.send('STATUS', account, (status, txt)) self.plugin.send('STATUS', account, (status, txt))
if status == 'online': if status == 'online' and self.plugin.sleeper.getState() != \
common.sleepy.STATE_UNKNOWN:
self.plugin.sleeper_state[account] = 1 self.plugin.sleeper_state[account] = 1
else: else:
self.plugin.sleeper_state[account] = 0 self.plugin.sleeper_state[account] = 0
@ -1425,16 +1426,10 @@ class roster_Window:
model.set_value(accountIter, 0, self.pixbufs[status]) model.set_value(accountIter, 0, self.pixbufs[status])
statuss = ['offline', 'online', 'away', 'xa', 'dnd', 'invisible'] statuss = ['offline', 'online', 'away', 'xa', 'dnd', 'invisible']
if status == 'offline': if status == 'offline':
self.plugin.sleeper = None
for jid in self.contacts[account]: for jid in self.contacts[account]:
luser = self.contacts[account][jid] luser = self.contacts[account][jid]
for user in luser: for user in luser:
self.chg_user_status(user, 'offline', 'Disconnected', account) self.chg_user_status(user, 'offline', 'Disconnected', account)
elif self.plugin.connected[account] == 0:
if (self.plugin.config['autoaway'] or self.plugin.config['autoxa']):
self.plugin.sleeper = common.sleepy.Sleepy(\
self.plugin.config['autoawaytime']*60, \
self.plugin.config['autoxatime']*60)
self.plugin.connected[account] = statuss.index(status) self.plugin.connected[account] = statuss.index(status)
self.set_optionmenu() self.set_optionmenu()
@ -2343,9 +2338,8 @@ class plugin:
def read_sleepy(self): def read_sleepy(self):
"""Check if we are idle""" """Check if we are idle"""
if not self.sleeper: if not self.sleeper.poll():
return 1 return 1
self.sleeper.poll()
state = self.sleeper.getState() state = self.sleeper.getState()
for account in self.accounts.keys(): for account in self.accounts.keys():
if not self.sleeper_state[account]: if not self.sleeper_state[account]:
@ -2439,7 +2433,9 @@ class plugin:
self.roster = roster_Window(self) self.roster = roster_Window(self)
gtk.timeout_add(100, self.read_queue) gtk.timeout_add(100, self.read_queue)
gtk.timeout_add(1000, self.read_sleepy) gtk.timeout_add(1000, self.read_sleepy)
self.sleeper = None self.sleeper = common.sleepy.Sleepy( \
self.config['autoawaytime']*60, \
self.config['autoxatime']*60)
if self.config['trayicon']: if self.config['trayicon']:
try: try:
global trayicon global trayicon