diff --git a/common/sleepy.py b/common/sleepy.py new file mode 100644 index 000000000..b715ea769 --- /dev/null +++ b/common/sleepy.py @@ -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) diff --git a/plugins/gtkgui/gtkgui.glade b/plugins/gtkgui/gtkgui.glade index 6111acccc..5b90f42f4 100644 --- a/plugins/gtkgui/gtkgui.glade +++ b/plugins/gtkgui/gtkgui.glade @@ -2971,18 +2971,120 @@ when NOT onlie - + True - Empty - False - False - GTK_JUSTIFY_LEFT - False - False - 0.5 - 0.5 - 0 - 0 + + + + 250 + 142 + True + 0 + 0.5 + GTK_SHADOW_ETCHED_IN + + + + True + 3 + 3 + False + 4 + 5 + + + + True + True + Auto Away After + True + GTK_RELIEF_NORMAL + False + False + True + + + 0 + 1 + 0 + 1 + fill + + + + + + + True + Minutes + False + False + GTK_JUSTIFY_LEFT + False + False + 0 + 0.5 + 0 + 0 + + + 2 + 3 + 0 + 1 + + + + + + + 50 + True + True + 1 + 0 + False + GTK_UPDATE_ALWAYS + False + False + 10 1 100 1 10 10 + + + 1 + 2 + 0 + 1 + fill + + + + + + + + + True + Auto Status + False + False + GTK_JUSTIFY_LEFT + False + False + 0.5 + 0.5 + 0 + 0 + + + label_item + + + + + 0 + 0 + + False @@ -2993,7 +3095,7 @@ when NOT onlie True - label37 + Presence False False GTK_JUSTIFY_LEFT diff --git a/plugins/gtkgui/gtkgui.py b/plugins/gtkgui/gtkgui.py index 61fe3534f..52b8fa9d4 100644 --- a/plugins/gtkgui/gtkgui.py +++ b/plugins/gtkgui/gtkgui.py @@ -22,13 +22,9 @@ import pygtk pygtk.require('2.0') import gtk from gtk import TRUE, FALSE -import gtk.glade -import gobject -import os -import string -import time -import Queue -import common.optparser +import gtk.glade,gobject +import os,string,time,Queue +import common.optparser,common.sleepy CONFPATH = "~/.gajimrc" Wbrowser = 0 Waccounts = 0 @@ -111,6 +107,11 @@ class prefs: self.r.cfgParser.set('GtkGui', 'statusmsgcolor', colSt) for j in self.r.tab_messages.keys(): 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 pp = self.chk_autopp.get_active() if pp == True: @@ -119,11 +120,18 @@ class prefs: else: self.r.cfgParser.set('GtkGui', 'autopopup', '0') self.r.autopopup = 0 - #IconStyle - ist = self.combo_iconstyle.entry.get_text() - self.r.cfgParser.set('GtkGui', 'iconstyle', ist) - self.r.iconstyle = ist - self.r.mkpixbufs() + #autoaway + aw = self.chk_autoaway.get_active() + if aw == True: + self.r.cfgParser.set('GtkGui', 'autoaway', '1') + 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.parseCfgFile() @@ -141,8 +149,10 @@ class prefs: self.da_in = self.xml.get_widget("drawing_in") self.da_out = self.xml.get_widget("drawing_out") 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.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 colSt = self.r.cfgParser.GtkGui_inmsgcolor @@ -168,13 +178,6 @@ class prefs: self.colorStatus = cmapStatus.alloc_color(colSt) 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 list_style = os.listdir('plugins/gtkgui/icons/') l = [] @@ -187,6 +190,25 @@ class prefs: if self.r.iconstyle in l: 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('on_but_col_clicked', self.on_color_button_clicked) self.xml.signal_connect('on_ok_clicked', self.on_ok) @@ -638,10 +660,6 @@ class roster: self.add_user(self.l_contact[j]['user']) 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(): #remove ressource from jid string ji = string.split(jid, '/')[0] @@ -775,8 +793,13 @@ class roster: # if (not self.showOffline) and widget.name == 'offline': # self.treestore.clear() if widget.name == 'offline': + self.connected = 0 + self.plugin.sleeper = None for j in self.l_contact.keys(): 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): window = prefs(self) @@ -795,6 +818,7 @@ class roster: def on_quit(self, widget): self.queueOUT.put(('QUIT','')) gtk.mainquit() +# sys.exit(0) def on_row_activated(self, widget, path, col=0): iter = self.treestore.get_iter(path) @@ -862,9 +886,13 @@ class roster: self.xml = gtk.glade.XML('plugins/gtkgui/gtkgui.glade', 'Gajim') self.window = self.xml.get_widget('Gajim') self.tree = self.xml.get_widget('treeview') - self.plug = plug + self.plugin = plug #(icon, name, jid, editable, background color, show_icon) 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 if not self.iconstyle: self.iconstyle = 'sun' @@ -1046,14 +1074,50 @@ class plugin: if (Waccounts != 0): Waccounts.init_accounts() 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): gtk.threads_init() gtk.threads_enter() self.queueIN = quIN 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) + gtk.timeout_add(1000, self.read_sleepy) + self.sleeper = None +# self.sleeper = common.sleepy.Sleepy(10) + self.sleeper_state = None gtk.main() +# while 1: +# while gtk.events_pending(): gtk.mainiteration() gtk.threads_leave() if __name__ == "__main__":