root/branches/gajim_0.11.1/src/common/sleepy.py

Revision 7829, 3.0 kB (checked in by asterix, 23 months ago)

merge diff from trunc to 0.11 branch

  • Property svn:eol-style set to LF
  • Property svn:keywords set to LastChangedDate LastChangedRevision LastChangedBy HeadURL Id
Line 
1## common/sleepy.py
2##
3## Contributors for this file:
4##        - Yann Le Boulanger <asterix@lagaule.org>
5##        - Nikos Kouremenos <kourem@gmail.com>
6##
7## Copyright (C) 2003-2004 Yann Le Boulanger <asterix@lagaule.org>
8##                                               Vincent Hanquez <tab@snarc.org>
9## Copyright (C) 2005-2006 Yann Le Boulanger <asterix@lagaule.org>
10##                                      Nikos Kouremenos <kourem@gmail.com>
11##
12## This program is free software; you can redistribute it and/or modify
13## it under the terms of the GNU General Public License as published
14## by the Free Software Foundation; version 2 only.
15##
16## This program is distributed in the hope that it will be useful,
17## but WITHOUT ANY WARRANTY; without even the implied warranty of
18## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19## GNU General Public License for more details.
20##
21
22from common import gajim
23import os
24
25
26STATE_UNKNOWN  = 'OS probably not supported'
27STATE_XA   = 'extended away'
28STATE_AWAY   = 'away'
29STATE_AWAKE     = 'awake'
30
31SUPPORTED = True
32try:
33                if os.name == 'nt':
34                        import ctypes
35
36                        GetTickCount = ctypes.windll.kernel32.GetTickCount
37                        GetLastInputInfo = ctypes.windll.user32.GetLastInputInfo
38
39                        class LASTINPUTINFO(ctypes.Structure):
40                                _fields_ = [('cbSize', ctypes.c_uint), ('dwTime', ctypes.c_uint)]
41
42                        lastInputInfo = LASTINPUTINFO()
43                        lastInputInfo.cbSize = ctypes.sizeof(lastInputInfo)
44
45                else: # unix
46                        import idle
47except:
48        gajim.log.debug('Unable to load idle module')
49        SUPPORTED = False
50
51class SleepyWindows:
52        def __init__(self, away_interval = 60, xa_interval = 120):
53                self.away_interval = away_interval
54                self.xa_interval = xa_interval
55                self.state = STATE_AWAKE # assume we are awake
56
57        def getIdleSec(self):
58                GetLastInputInfo(ctypes.byref(lastInputInfo))
59                idleDelta = float(GetTickCount() - lastInputInfo.dwTime) / 1000
60                return idleDelta
61
62        def poll(self):
63                '''checks to see if we should change state'''
64                if not SUPPORTED:
65                        return False
66
67                idleTime = self.getIdleSec()
68
69                # xa is stronger than away so check for xa first
70                if idleTime > self.xa_interval:
71                        self.state = STATE_XA
72                elif idleTime > self.away_interval:
73                        self.state = STATE_AWAY
74                else:
75                        self.state = STATE_AWAKE
76                return True
77
78        def getState(self):
79                return self.state
80
81        def setState(self, val):
82                self.state = val
83
84class SleepyUnix:
85        def __init__(self, away_interval = 60, xa_interval = 120):
86                self.away_interval = away_interval
87                self.xa_interval = xa_interval
88                self.state = STATE_AWAKE # assume we are awake
89                try:
90                        idle.init()
91                except:
92                        SUPPORTED = False
93                        self.state = STATE_UNKNOWN
94
95        def getIdleSec(self):
96                return idle.getIdleSec()
97
98        def poll(self):
99                '''checks to see if we should change state'''
100                if not SUPPORTED:
101                        return False
102
103                idleTime = self.getIdleSec()
104
105                # xa is stronger than away so check for xa first
106                if idleTime > self.xa_interval:
107                        self.state = STATE_XA
108                elif idleTime > self.away_interval:
109                        self.state = STATE_AWAY
110                else:
111                        self.state = STATE_AWAKE
112                return True
113
114        def getState(self):
115                return self.state
116
117        def setState(self, val):
118                self.state = val
119
120if os.name == 'nt':
121        Sleepy = SleepyWindows
122else:
123        Sleepy = SleepyUnix
Note: See TracBrowser for help on using the browser.