Better idle support. Idle module is now optional
This commit is contained in:
		
							parent
							
								
									0f00e7ad8a
								
							
						
					
					
						commit
						3241f32e11
					
				
					 3 changed files with 41 additions and 44 deletions
				
			
		|  | @ -2,7 +2,6 @@ | |||
| """A Quick class to tell if theres any activity on your machine""" | ||||
| 
 | ||||
| import time | ||||
| import idle | ||||
| from string import find, lower | ||||
| 
 | ||||
| 
 | ||||
|  | @ -11,42 +10,45 @@ STATE_XAWAY   = "extanted away" | |||
| STATE_AWAY   = "away" | ||||
| STATE_AWAKE    = "awake" | ||||
| 
 | ||||
| NOT_SUPPORTED = 0 | ||||
| SUPPORTED = 1 | ||||
| try: | ||||
| 	import idle | ||||
| except: | ||||
| 	SUPPORTED = 0 | ||||
| 
 | ||||
| class Sleepy: | ||||
| 
 | ||||
|     def __init__(self, interval1 = 60, interval2 = 120, devices = ['keyboard', 'mouse', 'ts'] ): | ||||
| 	def __init__(self, interval1 = 60, interval2 = 120): | ||||
| 
 | ||||
|         self.interval1 = interval1 | ||||
| 	self.interval2 = interval2 | ||||
|         self.state         = STATE_AWAKE ## assume were awake to stake with | ||||
|         try: | ||||
|             idle.init() | ||||
|         except: | ||||
|             NOT_SUPPORTED = 1 | ||||
|             self.state = STATE_UNKNOWN | ||||
| 		self.interval1 = interval1 | ||||
| 		self.interval2 = interval2 | ||||
| 		self.state         = STATE_AWAKE ## assume were awake to stake with | ||||
| 		try: | ||||
| 			idle.init() | ||||
| 		except: | ||||
| 			SUPPORTED = 0 | ||||
| 			self.state = STATE_UNKNOWN | ||||
| 
 | ||||
|     def poll(self): | ||||
|         if NOT_SUPPORTED: return -1 | ||||
|         now = time.time() | ||||
| 	def poll(self): | ||||
| 		if not SUPPORTED: return 0 | ||||
| 
 | ||||
| 	idleTime = idle.getIdleSec() | ||||
| 	if idleTime > self.interval2: | ||||
| 		self.state = STATE_XAWAY | ||||
| 	elif idleTime > self.interval1: | ||||
| 		self.state = STATE_AWAY | ||||
| 	else: | ||||
| 		self.state = STATE_AWAKE | ||||
|         return 1 | ||||
| 		idleTime = idle.getIdleSec() | ||||
| 		if idleTime > self.interval2: | ||||
| 			self.state = STATE_XAWAY | ||||
| 		elif idleTime > self.interval1: | ||||
| 			self.state = STATE_AWAY | ||||
| 		else: | ||||
| 			self.state = STATE_AWAKE | ||||
| 		return 1 | ||||
| 
 | ||||
|     def getState(self): | ||||
|         return self.state | ||||
| 	def getState(self): | ||||
| 		return self.state | ||||
| 
 | ||||
|     def setState(self,val): | ||||
|         self.state = val | ||||
| 	def setState(self,val): | ||||
| 		self.state = val | ||||
|              | ||||
| if __name__ == '__main__': | ||||
|     s = Sleepy(10) | ||||
|     while s.poll(): | ||||
|         print "state is %s" % s.getState()  | ||||
|         time.sleep(5) | ||||
| 	s = Sleepy(10) | ||||
| 	while s.poll(): | ||||
| 		print "state is %s" % s.getState()  | ||||
| 		time.sleep(5) | ||||
|  |  | |||
|  | @ -235,10 +235,9 @@ class preference_Window: | |||
| 			self.plugin.config['autoxa'] = 0 | ||||
| 		axt = self.spin_autoxatime.get_value_as_int() | ||||
| 		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.config['autoawaytime']*60, \ | ||||
| 				self.plugin.config['autoxatime']*60) | ||||
| 		self.plugin.sleeper = common.sleepy.Sleepy(\ | ||||
| 			self.plugin.config['autoawaytime']*60, \ | ||||
| 			self.plugin.config['autoxatime']*60) | ||||
| 		#trayicon | ||||
| 		if self.chk_trayicon.get_active(): | ||||
| 			self.plugin.config['trayicon'] = 1 | ||||
|  |  | |||
|  | @ -1364,7 +1364,8 @@ class roster_Window: | |||
| 						passphrase = '' | ||||
| 				self.plugin.send('PASSPHRASE', account, passphrase) | ||||
| 		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 | ||||
| 		else: | ||||
| 			self.plugin.sleeper_state[account] = 0 | ||||
|  | @ -1425,16 +1426,10 @@ class roster_Window: | |||
| 			model.set_value(accountIter, 0, self.pixbufs[status]) | ||||
| 		statuss = ['offline', 'online', 'away', 'xa', 'dnd', 'invisible'] | ||||
| 		if status == 'offline': | ||||
| 			self.plugin.sleeper = None | ||||
| 			for jid in self.contacts[account]: | ||||
| 				luser = self.contacts[account][jid] | ||||
| 				for user in luser: | ||||
| 					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.set_optionmenu() | ||||
| 
 | ||||
|  | @ -2343,9 +2338,8 @@ class plugin: | |||
| 	 | ||||
| 	def read_sleepy(self):	 | ||||
| 		"""Check if we are idle""" | ||||
| 		if not self.sleeper: | ||||
| 		if not self.sleeper.poll(): | ||||
| 			return 1 | ||||
| 		self.sleeper.poll() | ||||
| 		state = self.sleeper.getState() | ||||
| 		for account in self.accounts.keys(): | ||||
| 			if not self.sleeper_state[account]: | ||||
|  | @ -2439,7 +2433,9 @@ class plugin: | |||
| 		self.roster = roster_Window(self) | ||||
| 		gtk.timeout_add(100, self.read_queue) | ||||
| 		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']: | ||||
| 			try: | ||||
| 				global trayicon | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue