home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June / CHIP 2006-06.2.iso / program / freeware / Democracy-0.8.2.exe / xulrunner / python / idlenotifier.py < prev    next >
Encoding:
Python Source  |  2006-04-10  |  3.0 KB  |  74 lines

  1. """This module provides IdleNotifier objects which run in the background and 
  2.    notify delegate objects about the system idle state. The low-level platform
  3.    specific code which checks the actual idle state must be provided by the
  4.    frontend.
  5.    
  6.    An IdleNotifier object is controlled by two parameters: the idle threshold
  7.    and the periodicity. The periodicity value specifies the frequency at which
  8.    the system's idle state will be checked. When the system has been idling for 
  9.    more than idleThreshold seconds, the delegate's systemHasBeenIdlingSince 
  10.    method is called. When the system is active and is known to have been 
  11.    previously idling, the delegate's systemIsActiveAgain method is called.
  12. """
  13.  
  14. import threading
  15. import time
  16.  
  17. import idletime
  18.  
  19. DEFAULT_PERIODICITY = 5             # Check every X seconds
  20. DEFAULT_IDLE_THRESHOLD = 60 * 5     # Notify after X seconds of idling
  21.  
  22.  
  23. class IdleNotifier (threading.Thread):
  24.     
  25.     def __init__(self, delegate, idleThreshold=DEFAULT_IDLE_THRESHOLD, periodicity=DEFAULT_PERIODICITY):
  26.         """Initialize the IdleNotifier object, the passed delegate will be
  27.            called when the system idle state changes.
  28.            - idleThreshold specifies the idle time (in seconds) after which the
  29.            delegate is called.
  30.            - periodicity specifies that that the idle state should be checked
  31.            every X seconds.
  32.         """
  33.         threading.Thread.__init__(self)
  34.         self.setName("IdleNotifier")
  35.         self.setDaemon(True)
  36.         self.stopEvent = threading.Event()
  37.         self.idling = False
  38.         self.wasIdling = False
  39.         self.delegate = delegate
  40.         self.idleThreshold = idleThreshold
  41.         self.periodicity = periodicity
  42.                         
  43.     def run(self):
  44.         print "DTV: idle notifier thread running"
  45.         while not self.stopEvent.isSet():
  46.             seconds = idletime.get()
  47.             if self.idling:
  48.                 self._whenIdling(seconds)
  49.             else:
  50.                 self._whenNotIdling(seconds)
  51.             self.stopEvent.wait(self.periodicity)
  52.         print "DTV: idle notifier thread stopped"
  53.  
  54.     def join(self):
  55.         self.stopEvent.set()
  56.         threading.Thread.join(self)
  57.     
  58.     def _whenNotIdling(self, seconds):
  59.         if seconds >= self.idleThreshold:
  60.             print "DTV: system has been idling since %d seconds" % seconds
  61.             self.idling = True
  62.             self.wasIdling = True
  63.             if self.delegate is not None and hasattr(self.delegate, 'systemHasBeenIdlingSince'):
  64.                 self.delegate.systemHasBeenIdlingSince(seconds)
  65.  
  66.     def _whenIdling(self, seconds):
  67.         if seconds < self.idleThreshold:
  68.             self.idling = False
  69.             if self.wasIdling:
  70.                 print "DTV: system is active again since %d seconds" % seconds
  71.                 if self.delegate is not None and hasattr(self.delegate, 'systemIsActiveAgain'):
  72.                     self.delegate.systemIsActiveAgain()
  73.                     self.wasIdling = False
  74.