bugfix in autoaway + autoxa support
This commit is contained in:
		
							parent
							
								
									1776cc4b45
								
							
						
					
					
						commit
						dead744a92
					
				
					 3 changed files with 138 additions and 42 deletions
				
			
		| 
						 | 
				
			
			@ -6,20 +6,20 @@ from string import find, lower
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
STATE_UNKNOWN  = "OS probably not supported"
 | 
			
		||||
STATE_SLEEPING = "Going to sleep"
 | 
			
		||||
STATE_ASLEEP   = "asleep"
 | 
			
		||||
STATE_WOKEN    = "waking up"
 | 
			
		||||
STATE_XAWAY   = "extanted away"
 | 
			
		||||
STATE_AWAY   = "away"
 | 
			
		||||
STATE_AWAKE    = "awake"
 | 
			
		||||
 | 
			
		||||
NOT_SUPPORTED = 0
 | 
			
		||||
 | 
			
		||||
class Sleepy:
 | 
			
		||||
 | 
			
		||||
    def __init__(self, interval = 60, devices = ['keyboard', 'mouse', 'ts'] ):
 | 
			
		||||
    def __init__(self, interval1 = 60, interval2 = 120, devices = ['keyboard', 'mouse', 'ts'] ):
 | 
			
		||||
 | 
			
		||||
        self.devices       = devices
 | 
			
		||||
        self.time_marker   = time.time()
 | 
			
		||||
        self.interval = self.interval_orig = interval
 | 
			
		||||
        self.interval = self.interval_orig = interval1
 | 
			
		||||
	self.interval_orig2 = interval2
 | 
			
		||||
        self.last_proc_vals = {}
 | 
			
		||||
        for dev in self.devices: self.last_proc_vals[dev] = 0
 | 
			
		||||
        
 | 
			
		||||
| 
						 | 
				
			
			@ -35,34 +35,26 @@ class Sleepy:
 | 
			
		|||
    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:
 | 
			
		||||
        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 :)
 | 
			
		||||
	    self.time_marker = time.time() ## reset marker
 | 
			
		||||
            self.state = STATE_AWAKE
 | 
			
		||||
            self.interval = self.interval_orig
 | 
			
		||||
        else:
 | 
			
		||||
            if (now - self.time_marker >= self.interval):
 | 
			
		||||
                ## 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 
 | 
			
		||||
                if self.state == STATE_AWAKE:
 | 
			
		||||
                    self.state = STATE_AWAY
 | 
			
		||||
                    self.interval = self.interval_orig2 #second interval
 | 
			
		||||
                else:
 | 
			
		||||
                    self.state = STATE_ASLEEP
 | 
			
		||||
                    self.state = STATE_XAWAY
 | 
			
		||||
        return 1
 | 
			
		||||
 | 
			
		||||
    def getState(self):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,6 +2,7 @@
 | 
			
		|||
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">
 | 
			
		||||
 | 
			
		||||
<glade-interface>
 | 
			
		||||
<requires lib="gnome"/>
 | 
			
		||||
 | 
			
		||||
<widget class="GtkWindow" id="Gajim">
 | 
			
		||||
  <property name="visible">True</property>
 | 
			
		||||
| 
						 | 
				
			
			@ -428,7 +429,6 @@
 | 
			
		|||
	      <property name="visible">True</property>
 | 
			
		||||
	      <property name="can_focus">True</property>
 | 
			
		||||
	      <property name="relief">GTK_RELIEF_NORMAL</property>
 | 
			
		||||
	      <signal name="clicked" handler="on_clear_button_clicked" last_modification_time="Thu, 01 Jan 2004 06:12:56 GMT"/>
 | 
			
		||||
 | 
			
		||||
	      <child>
 | 
			
		||||
		<widget class="GtkAlignment" id="alignment5">
 | 
			
		||||
| 
						 | 
				
			
			@ -2976,7 +2976,7 @@ when NOT onlie</property>
 | 
			
		|||
 | 
			
		||||
	      <child>
 | 
			
		||||
		<widget class="GtkFrame" id="frame4">
 | 
			
		||||
		  <property name="width_request">250</property>
 | 
			
		||||
		  <property name="width_request">325</property>
 | 
			
		||||
		  <property name="height_request">142</property>
 | 
			
		||||
		  <property name="visible">True</property>
 | 
			
		||||
		  <property name="label_xalign">0</property>
 | 
			
		||||
| 
						 | 
				
			
			@ -3058,6 +3058,73 @@ when NOT onlie</property>
 | 
			
		|||
			  <property name="y_options"></property>
 | 
			
		||||
			</packing>
 | 
			
		||||
		      </child>
 | 
			
		||||
 | 
			
		||||
		      <child>
 | 
			
		||||
			<widget class="GtkCheckButton" id="chk_autoxa">
 | 
			
		||||
			  <property name="visible">True</property>
 | 
			
		||||
			  <property name="can_focus">True</property>
 | 
			
		||||
			  <property name="label" translatable="yes">Auto eXtanded 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">1</property>
 | 
			
		||||
			  <property name="bottom_attach">2</property>
 | 
			
		||||
			  <property name="x_options">fill</property>
 | 
			
		||||
			  <property name="y_options"></property>
 | 
			
		||||
			</packing>
 | 
			
		||||
		      </child>
 | 
			
		||||
 | 
			
		||||
		      <child>
 | 
			
		||||
			<widget class="GtkSpinButton" id="spin_autoxatime">
 | 
			
		||||
			  <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">20 1 100 1 10 10</property>
 | 
			
		||||
			</widget>
 | 
			
		||||
			<packing>
 | 
			
		||||
			  <property name="left_attach">1</property>
 | 
			
		||||
			  <property name="right_attach">2</property>
 | 
			
		||||
			  <property name="top_attach">1</property>
 | 
			
		||||
			  <property name="bottom_attach">2</property>
 | 
			
		||||
			  <property name="y_options"></property>
 | 
			
		||||
			</packing>
 | 
			
		||||
		      </child>
 | 
			
		||||
 | 
			
		||||
		      <child>
 | 
			
		||||
			<widget class="GtkLabel" id="label48">
 | 
			
		||||
			  <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">1</property>
 | 
			
		||||
			  <property name="bottom_attach">2</property>
 | 
			
		||||
			  <property name="x_options">fill</property>
 | 
			
		||||
			  <property name="y_options"></property>
 | 
			
		||||
			</packing>
 | 
			
		||||
		      </child>
 | 
			
		||||
		    </widget>
 | 
			
		||||
		  </child>
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -131,7 +131,20 @@ class prefs:
 | 
			
		|||
		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)
 | 
			
		||||
		#autoxa
 | 
			
		||||
		xa = self.chk_autoxa.get_active()
 | 
			
		||||
		if xa == True:
 | 
			
		||||
			self.r.cfgParser.set('GtkGui', 'autoxa', '1')
 | 
			
		||||
			self.r.plugin.autoxa = 1
 | 
			
		||||
		else:
 | 
			
		||||
			self.r.cfgParser.set('GtkGui', 'autoxa', '0')
 | 
			
		||||
			self.r.plugin.autoxa = 0
 | 
			
		||||
		axt = self.spin_autoxatime.get_value_as_int()
 | 
			
		||||
		self.r.plugin.autoxatime = axt
 | 
			
		||||
		self.r.cfgParser.set('GtkGui', 'autoxatime', axt)
 | 
			
		||||
		if self.r.plugin.sleeper:
 | 
			
		||||
			self.r.plugin.sleeper = common.sleepy.Sleepy(\
 | 
			
		||||
				self.r.plugin.autoawaytime*60, self.r.plugin.autoxatime*60)
 | 
			
		||||
		
 | 
			
		||||
		self.r.cfgParser.writeCfgFile()
 | 
			
		||||
		self.r.cfgParser.parseCfgFile()
 | 
			
		||||
| 
						 | 
				
			
			@ -153,6 +166,8 @@ class prefs:
 | 
			
		|||
		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")
 | 
			
		||||
		self.chk_autoxa = self.xml.get_widget("chk_autoxa")
 | 
			
		||||
		self.spin_autoxatime = self.xml.get_widget("spin_autoxatime")
 | 
			
		||||
 | 
			
		||||
		#Color for incomming messages
 | 
			
		||||
		colSt = self.r.cfgParser.GtkGui_inmsgcolor
 | 
			
		||||
| 
						 | 
				
			
			@ -209,6 +224,18 @@ class prefs:
 | 
			
		|||
		ti = string.atoi(st)
 | 
			
		||||
		self.spin_autoawaytime.set_value(ti)
 | 
			
		||||
 | 
			
		||||
		#Autoxa
 | 
			
		||||
		st = self.r.cfgParser.GtkGui_autoxa
 | 
			
		||||
		if not st:
 | 
			
		||||
			st = '1'
 | 
			
		||||
		xa = string.atoi(st)
 | 
			
		||||
		self.chk_autoxa.set_active(xa)
 | 
			
		||||
		st = self.r.cfgParser.GtkGui_autoxatime
 | 
			
		||||
		if not st:
 | 
			
		||||
			st = '20'
 | 
			
		||||
		ti = string.atoi(st)
 | 
			
		||||
		self.spin_autoxatime.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)
 | 
			
		||||
| 
						 | 
				
			
			@ -799,7 +826,8 @@ class roster:
 | 
			
		|||
				self.chg_status(j, 'offline', 'Disconnected')
 | 
			
		||||
		else:
 | 
			
		||||
			self.connected = 1
 | 
			
		||||
			self.plugin.sleeper = common.sleepy.Sleepy(self.plugin.autoawaytime*60)
 | 
			
		||||
			self.plugin.sleeper = common.sleepy.Sleepy(\
 | 
			
		||||
				self.plugin.autoawaytime*60, self.plugin.autoxatime*60)
 | 
			
		||||
 | 
			
		||||
	def on_prefs(self, widget):
 | 
			
		||||
		window = prefs(self)
 | 
			
		||||
| 
						 | 
				
			
			@ -1076,24 +1104,26 @@ class plugin:
 | 
			
		|||
		return 1
 | 
			
		||||
	
 | 
			
		||||
	def read_sleepy(self):
 | 
			
		||||
		if self.sleeper:
 | 
			
		||||
			state_pres = None
 | 
			
		||||
		if self.sleeper and (self.autoaway or self.autoxa) and \
 | 
			
		||||
			(self.r.optionmenu.get_history()==0 or \
 | 
			
		||||
			self.sleeper_state!=common.sleepy.STATE_AWAKE):
 | 
			
		||||
			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:
 | 
			
		||||
				if state == common.sleepy.STATE_AWAKE:
 | 
			
		||||
					#on repasse online
 | 
			
		||||
					self.r.optionmenu.set_history(0)
 | 
			
		||||
					self.r.queueOUT.put(('STATUS',('online', accounts[0])))
 | 
			
		||||
				if state == common.sleepy.STATE_SLEEPING:
 | 
			
		||||
				if state == common.sleepy.STATE_AWAY and self.autoaway:
 | 
			
		||||
					#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)
 | 
			
		||||
				if state == common.sleepy.STATE_XAWAY and self.autoxa:
 | 
			
		||||
					#on passe away
 | 
			
		||||
					self.r.optionmenu.set_history(2)
 | 
			
		||||
					self.r.queueOUT.put(('STATUS',('xa', accounts[0])))
 | 
			
		||||
			self.sleeper_state = state
 | 
			
		||||
		return 1
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1110,10 +1140,17 @@ class plugin:
 | 
			
		|||
		if not st:
 | 
			
		||||
			st = '10'
 | 
			
		||||
		self.autoawaytime = string.atoi(st)
 | 
			
		||||
		st = self.r.cfgParser.GtkGui_autoxa
 | 
			
		||||
		if not st:
 | 
			
		||||
			st = '1'
 | 
			
		||||
		self.autoxa = string.atoi(st)
 | 
			
		||||
		st = self.r.cfgParser.GtkGui_autoxatime
 | 
			
		||||
		if not st:
 | 
			
		||||
			st = '20'
 | 
			
		||||
		self.autoxatime = 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:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue