autoaway support

This commit is contained in:
Yann Leboulanger 2004-01-02 15:21:02 +00:00
parent bded2aeb2c
commit 1776cc4b45
3 changed files with 293 additions and 37 deletions

90
common/sleepy.py Normal file
View File

@ -0,0 +1,90 @@
#!/usr/bin/env python
"""A Quick class to tell if theres any activity on your machine"""
import time
from string import find, lower
STATE_UNKNOWN = "OS probably not supported"
STATE_SLEEPING = "Going to sleep"
STATE_ASLEEP = "asleep"
STATE_WOKEN = "waking up"
STATE_AWAKE = "awake"
NOT_SUPPORTED = 0
class Sleepy:
def __init__(self, interval = 60, devices = ['keyboard', 'mouse', 'ts'] ):
self.devices = devices
self.time_marker = time.time()
self.interval = self.interval_orig = interval
self.last_proc_vals = {}
for dev in self.devices: self.last_proc_vals[dev] = 0
self.state = STATE_AWAKE ## assume were awake to stake with
try:
self.proc_int_fh = open("/proc/interrupts",'r')
except:
NOT_SUPPORTED = 1
self.state = STATE_UNKNOWN
self.proc_int_fh.close()
def poll(self):
if NOT_SUPPORTED: return -1
now = time.time()
if (now - self.time_marker >= self.interval):
self.time_marker = time.time() ## reset marker
changed = 0 ## figure out if we have recieved interupts
for dev in self.devices: ## any of the selected devices
proc_val = self._read_proc(dev)
changed = changed or ( self.last_proc_vals[dev] != proc_val )
self.last_proc_vals[dev] = proc_val
if changed:
## we are awake :)
if self.state == STATE_ASLEEP or \
self.state == STATE_SLEEPING :
self.state = STATE_WOKEN
self.interval = self.interval_orig
else:
self.state = STATE_AWAKE
else:
## we are asleep
if self.state == STATE_AWAKE or \
self.state == STATE_WOKEN :
self.state = STATE_SLEEPING
## we increase the check time as catching activity
## is now more important
self.interval = 5
else:
self.state = STATE_ASLEEP
return 1
def getState(self):
return self.state
def setState(self,val):
self.state = val
def _read_proc(self, device = 'mouse'):
proc_int_fh = open("/proc/interrupts",'r')
info = proc_int_fh.readlines()
## ignore first line
for line in info[1:]:
cols = line.strip().split()
if (find(lower(cols[-1]),device) != -1):
proc_int_fh.close()
return int(cols[1])
proc_int_fh.close()
return 1
if __name__ == '__main__':
s = Sleepy(10)
while s.poll():
print "state is %s" % s.getState()
time.sleep(10)

View File

@ -2971,18 +2971,120 @@ when NOT onlie</property>
</child> </child>
<child> <child>
<widget class="GtkLabel" id="label40"> <widget class="GtkFixed" id="fixed3">
<property name="visible">True</property> <property name="visible">True</property>
<property name="label" translatable="yes">Empty</property>
<property name="use_underline">False</property> <child>
<property name="use_markup">False</property> <widget class="GtkFrame" id="frame4">
<property name="justify">GTK_JUSTIFY_LEFT</property> <property name="width_request">250</property>
<property name="wrap">False</property> <property name="height_request">142</property>
<property name="selectable">False</property> <property name="visible">True</property>
<property name="xalign">0.5</property> <property name="label_xalign">0</property>
<property name="yalign">0.5</property> <property name="label_yalign">0.5</property>
<property name="xpad">0</property> <property name="shadow_type">GTK_SHADOW_ETCHED_IN</property>
<property name="ypad">0</property>
<child>
<widget class="GtkTable" id="table3">
<property name="visible">True</property>
<property name="n_rows">3</property>
<property name="n_columns">3</property>
<property name="homogeneous">False</property>
<property name="row_spacing">4</property>
<property name="column_spacing">5</property>
<child>
<widget class="GtkCheckButton" id="chk_autoaway">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="label" translatable="yes">Auto Away After</property>
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="active">False</property>
<property name="inconsistent">False</property>
<property name="draw_indicator">True</property>
</widget>
<packing>
<property name="left_attach">0</property>
<property name="right_attach">1</property>
<property name="top_attach">0</property>
<property name="bottom_attach">1</property>
<property name="x_options">fill</property>
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkLabel" id="label47">
<property name="visible">True</property>
<property name="label" translatable="yes">Minutes</property>
<property name="use_underline">False</property>
<property name="use_markup">False</property>
<property name="justify">GTK_JUSTIFY_LEFT</property>
<property name="wrap">False</property>
<property name="selectable">False</property>
<property name="xalign">0</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
</widget>
<packing>
<property name="left_attach">2</property>
<property name="right_attach">3</property>
<property name="top_attach">0</property>
<property name="bottom_attach">1</property>
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkSpinButton" id="spin_autoawaytime">
<property name="width_request">50</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="climb_rate">1</property>
<property name="digits">0</property>
<property name="numeric">False</property>
<property name="update_policy">GTK_UPDATE_ALWAYS</property>
<property name="snap_to_ticks">False</property>
<property name="wrap">False</property>
<property name="adjustment">10 1 100 1 10 10</property>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">0</property>
<property name="bottom_attach">1</property>
<property name="x_options">fill</property>
<property name="y_options"></property>
</packing>
</child>
</widget>
</child>
<child>
<widget class="GtkLabel" id="label46">
<property name="visible">True</property>
<property name="label" translatable="yes">Auto Status</property>
<property name="use_underline">False</property>
<property name="use_markup">False</property>
<property name="justify">GTK_JUSTIFY_LEFT</property>
<property name="wrap">False</property>
<property name="selectable">False</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
</widget>
<packing>
<property name="type">label_item</property>
</packing>
</child>
</widget>
<packing>
<property name="x">0</property>
<property name="y">0</property>
</packing>
</child>
</widget> </widget>
<packing> <packing>
<property name="tab_expand">False</property> <property name="tab_expand">False</property>
@ -2993,7 +3095,7 @@ when NOT onlie</property>
<child> <child>
<widget class="GtkLabel" id="label37"> <widget class="GtkLabel" id="label37">
<property name="visible">True</property> <property name="visible">True</property>
<property name="label" translatable="yes">label37</property> <property name="label" translatable="yes">Presence</property>
<property name="use_underline">False</property> <property name="use_underline">False</property>
<property name="use_markup">False</property> <property name="use_markup">False</property>
<property name="justify">GTK_JUSTIFY_LEFT</property> <property name="justify">GTK_JUSTIFY_LEFT</property>

View File

@ -22,13 +22,9 @@ import pygtk
pygtk.require('2.0') pygtk.require('2.0')
import gtk import gtk
from gtk import TRUE, FALSE from gtk import TRUE, FALSE
import gtk.glade import gtk.glade,gobject
import gobject import os,string,time,Queue
import os import common.optparser,common.sleepy
import string
import time
import Queue
import common.optparser
CONFPATH = "~/.gajimrc" CONFPATH = "~/.gajimrc"
Wbrowser = 0 Wbrowser = 0
Waccounts = 0 Waccounts = 0
@ -111,6 +107,11 @@ class prefs:
self.r.cfgParser.set('GtkGui', 'statusmsgcolor', colSt) self.r.cfgParser.set('GtkGui', 'statusmsgcolor', colSt)
for j in self.r.tab_messages.keys(): for j in self.r.tab_messages.keys():
self.r.tab_messages[j].tagStatus.set_property("foreground", colSt) self.r.tab_messages[j].tagStatus.set_property("foreground", colSt)
#IconStyle
ist = self.combo_iconstyle.entry.get_text()
self.r.cfgParser.set('GtkGui', 'iconstyle', ist)
self.r.iconstyle = ist
self.r.mkpixbufs()
#autopopup #autopopup
pp = self.chk_autopp.get_active() pp = self.chk_autopp.get_active()
if pp == True: if pp == True:
@ -119,11 +120,18 @@ class prefs:
else: else:
self.r.cfgParser.set('GtkGui', 'autopopup', '0') self.r.cfgParser.set('GtkGui', 'autopopup', '0')
self.r.autopopup = 0 self.r.autopopup = 0
#IconStyle #autoaway
ist = self.combo_iconstyle.entry.get_text() aw = self.chk_autoaway.get_active()
self.r.cfgParser.set('GtkGui', 'iconstyle', ist) if aw == True:
self.r.iconstyle = ist self.r.cfgParser.set('GtkGui', 'autoaway', '1')
self.r.mkpixbufs() self.r.plugin.autoaway = 1
else:
self.r.cfgParser.set('GtkGui', 'autoaway', '0')
self.r.plugin.autoaway = 0
aat = self.spin_autoawaytime.get_value_as_int()
self.r.plugin.autoawaytime = aat
self.r.cfgParser.set('GtkGui', 'autoawaytime', aat)
self.r.plugin.sleeper = common.sleepy.Sleepy(self.r.plugin.autoawaytime*60)
self.r.cfgParser.writeCfgFile() self.r.cfgParser.writeCfgFile()
self.r.cfgParser.parseCfgFile() self.r.cfgParser.parseCfgFile()
@ -141,8 +149,10 @@ class prefs:
self.da_in = self.xml.get_widget("drawing_in") self.da_in = self.xml.get_widget("drawing_in")
self.da_out = self.xml.get_widget("drawing_out") self.da_out = self.xml.get_widget("drawing_out")
self.da_status = self.xml.get_widget("drawing_status") self.da_status = self.xml.get_widget("drawing_status")
self.chk_autopp = self.xml.get_widget("chk_autopopup")
self.combo_iconstyle = self.xml.get_widget("combo_iconstyle") self.combo_iconstyle = self.xml.get_widget("combo_iconstyle")
self.chk_autopp = self.xml.get_widget("chk_autopopup")
self.chk_autoaway = self.xml.get_widget("chk_autoaway")
self.spin_autoawaytime = self.xml.get_widget("spin_autoawaytime")
#Color for incomming messages #Color for incomming messages
colSt = self.r.cfgParser.GtkGui_inmsgcolor colSt = self.r.cfgParser.GtkGui_inmsgcolor
@ -168,13 +178,6 @@ class prefs:
self.colorStatus = cmapStatus.alloc_color(colSt) self.colorStatus = cmapStatus.alloc_color(colSt)
self.da_status.window.set_background(self.colorStatus) self.da_status.window.set_background(self.colorStatus)
#Autopopup
st = self.r.cfgParser.GtkGui_autopopup
if not st:
st = '0'
pp = string.atoi(st)
self.chk_autopp.set_active(pp)
#iconStyle #iconStyle
list_style = os.listdir('plugins/gtkgui/icons/') list_style = os.listdir('plugins/gtkgui/icons/')
l = [] l = []
@ -187,6 +190,25 @@ class prefs:
if self.r.iconstyle in l: if self.r.iconstyle in l:
self.combo_iconstyle.entry.set_text(self.r.iconstyle) self.combo_iconstyle.entry.set_text(self.r.iconstyle)
#Autopopup
st = self.r.cfgParser.GtkGui_autopopup
if not st:
st = '0'
pp = string.atoi(st)
self.chk_autopp.set_active(pp)
#Autoaway
st = self.r.cfgParser.GtkGui_autoaway
if not st:
st = '1'
aw = string.atoi(st)
self.chk_autoaway.set_active(aw)
st = self.r.cfgParser.GtkGui_autoawaytime
if not st:
st = '10'
ti = string.atoi(st)
self.spin_autoawaytime.set_value(ti)
self.xml.signal_connect('gtk_widget_destroy', self.delete_event) self.xml.signal_connect('gtk_widget_destroy', self.delete_event)
self.xml.signal_connect('on_but_col_clicked', self.on_color_button_clicked) self.xml.signal_connect('on_but_col_clicked', self.on_color_button_clicked)
self.xml.signal_connect('on_ok_clicked', self.on_ok) self.xml.signal_connect('on_ok_clicked', self.on_ok)
@ -638,10 +660,6 @@ class roster:
self.add_user(self.l_contact[j]['user']) self.add_user(self.l_contact[j]['user'])
def mklists(self, tab): def mklists(self, tab):
""" l_contact = {jid:{'user':_, 'iter':[iter1, ...]] """
self.l_contact = {}
""" l_group = {name:iter} """
self.l_group = {}
for jid in tab.keys(): for jid in tab.keys():
#remove ressource from jid string #remove ressource from jid string
ji = string.split(jid, '/')[0] ji = string.split(jid, '/')[0]
@ -775,8 +793,13 @@ class roster:
# if (not self.showOffline) and widget.name == 'offline': # if (not self.showOffline) and widget.name == 'offline':
# self.treestore.clear() # self.treestore.clear()
if widget.name == 'offline': if widget.name == 'offline':
self.connected = 0
self.plugin.sleeper = None
for j in self.l_contact.keys(): for j in self.l_contact.keys():
self.chg_status(j, 'offline', 'Disconnected') self.chg_status(j, 'offline', 'Disconnected')
else:
self.connected = 1
self.plugin.sleeper = common.sleepy.Sleepy(self.plugin.autoawaytime*60)
def on_prefs(self, widget): def on_prefs(self, widget):
window = prefs(self) window = prefs(self)
@ -795,6 +818,7 @@ class roster:
def on_quit(self, widget): def on_quit(self, widget):
self.queueOUT.put(('QUIT','')) self.queueOUT.put(('QUIT',''))
gtk.mainquit() gtk.mainquit()
# sys.exit(0)
def on_row_activated(self, widget, path, col=0): def on_row_activated(self, widget, path, col=0):
iter = self.treestore.get_iter(path) iter = self.treestore.get_iter(path)
@ -862,9 +886,13 @@ class roster:
self.xml = gtk.glade.XML('plugins/gtkgui/gtkgui.glade', 'Gajim') self.xml = gtk.glade.XML('plugins/gtkgui/gtkgui.glade', 'Gajim')
self.window = self.xml.get_widget('Gajim') self.window = self.xml.get_widget('Gajim')
self.tree = self.xml.get_widget('treeview') self.tree = self.xml.get_widget('treeview')
self.plug = plug self.plugin = plug
#(icon, name, jid, editable, background color, show_icon) #(icon, name, jid, editable, background color, show_icon)
self.treestore = gtk.TreeStore(gtk.gdk.Pixbuf, str, str, gobject.TYPE_BOOLEAN, str, gobject.TYPE_BOOLEAN) self.treestore = gtk.TreeStore(gtk.gdk.Pixbuf, str, str, gobject.TYPE_BOOLEAN, str, gobject.TYPE_BOOLEAN)
#l_contact = {jid:{'user':_, 'iter':[iter1, ...]]
self.l_contact = {}
#l_group = {name:iter}
self.l_group = {}
self.iconstyle = self.cfgParser.GtkGui_iconstyle self.iconstyle = self.cfgParser.GtkGui_iconstyle
if not self.iconstyle: if not self.iconstyle:
self.iconstyle = 'sun' self.iconstyle = 'sun'
@ -1046,14 +1074,50 @@ class plugin:
if (Waccounts != 0): if (Waccounts != 0):
Waccounts.init_accounts() Waccounts.init_accounts()
return 1 return 1
def read_sleepy(self):
if self.sleeper:
state_pres = None
self.sleeper.poll()
state = self.sleeper.getState()
if state != self.sleeper_state:
accountsStr = self.r.cfgParser.Profile_accounts
accounts = string.split(accountsStr, ' ')
if state == common.sleepy.STATE_WOKEN:
#on repasse online
self.r.optionmenu.set_history(0)
self.r.queueOUT.put(('STATUS',('online', accounts[0])))
if state == common.sleepy.STATE_SLEEPING:
#on passe away
self.r.optionmenu.set_history(1)
self.r.queueOUT.put(('STATUS',('away', accounts[0])))
if state_pres:
pass
#self.send(state_pres)
self.sleeper_state = state
return 1
def __init__(self, quIN, quOUT): def __init__(self, quIN, quOUT):
gtk.threads_init() gtk.threads_init()
gtk.threads_enter() gtk.threads_enter()
self.queueIN = quIN self.queueIN = quIN
self.r = roster(quOUT, self) self.r = roster(quOUT, self)
st = self.r.cfgParser.GtkGui_autoaway
if not st:
st = '1'
self.autoaway = string.atoi(st)
st = self.r.cfgParser.GtkGui_autoawaytime
if not st:
st = '10'
self.autoawaytime = string.atoi(st)
self.time = gtk.timeout_add(200, self.read_queue) self.time = gtk.timeout_add(200, self.read_queue)
gtk.timeout_add(1000, self.read_sleepy)
self.sleeper = None
# self.sleeper = common.sleepy.Sleepy(10)
self.sleeper_state = None
gtk.main() gtk.main()
# while 1:
# while gtk.events_pending(): gtk.mainiteration()
gtk.threads_leave() gtk.threads_leave()
if __name__ == "__main__": if __name__ == "__main__":