pluginsort: initial checkin
[enigma2-plugins.git] / epgrefresh / src / EPGRefreshTimer.py
1 # To check if in Standby
2 import Screens.Standby
3
4 # Base Class
5 import timer
6
7 # To see if in Timespan and to determine begin of timespan
8 from time import localtime, mktime, time, strftime
9
10 # Config
11 from Components.config import config
12
13 def checkTimespan(begin, end):
14         # Get current time
15         time = localtime()
16
17         # Check if we span a day
18         if begin[0] > end[0] or (begin[0] == end[0] and begin[1] >= end[1]):
19                 # Check if begin of event is later than our timespan starts
20                 if time.tm_hour > begin[0] or (time.tm_hour == begin[0] and time.tm_min >= begin[1]):
21                         # If so, event is in our timespan
22                         return True
23                 # Check if begin of event is earlier than our timespan end
24                 if time.tm_hour < end[0] or (time.tm_hour == end[0] and time.tm_min <= end[1]):
25                         # If so, event is in our timespan
26                         return True
27                 return False
28         else:
29                 # Check if event begins earlier than our timespan starts
30                 if time.tm_hour < begin[0] or (time.tm_hour == begin[0] and time.tm_min < begin[1]):
31                         # Its out of our timespan then
32                         return False
33                 # Check if event begins later than our timespan ends
34                 if time.tm_hour > end[0] or (time.tm_hour == end[0] and time.tm_min > end[1]):
35                         # Its out of our timespan then
36                         return False
37                 return True
38
39 class EPGRefreshTimerEntry(timer.TimerEntry):
40         """TimerEntry ..."""
41         def __init__(self, begin, tocall, nocheck = False):
42                 timer.TimerEntry.__init__(self, int(begin), int(begin))
43
44                 self.function = tocall
45                 self.nocheck = nocheck
46                 if nocheck:
47                         self.state = self.StatePrepared
48
49         def getNextActivation(self):
50                 # We delay our activation so we won't rush into reprocessing a repeating one
51                 return self.begin+1
52
53         def activate(self):
54                 if self.state == self.StateWaiting:
55                         # Check if in timespan
56                         if checkTimespan(config.plugins.epgrefresh.begin.value, config.plugins.epgrefresh.end.value):
57                                 print "[EPGRefresh] In Timespan, will check if we're in Standby and have no Recordings running next"
58                                 # Do we realy want to check nav?
59                                 from NavigationInstance import instance
60                                 if config.plugins.epgrefresh.force.value or (Screens.Standby.inStandby and instance is not None and not instance.RecordTimer.isRecording()):
61                                         return True
62                                 else:
63                                         print "[EPGRefresh] Box still in use, rescheduling"
64
65                                         # Recheck later
66                                         self.begin = time() + config.plugins.epgrefresh.delay_standby.value*60
67                                         return False
68                         else:
69                                 print "[EPGRefresh] Not in timespan, ending timer"
70                                 self.state = self.StateEnded
71                                 return False
72                 elif self.state == self.StateRunning:
73                         self.function()
74
75                 return True
76
77         def resetState(self):
78                 self.state = self.StateWaiting
79                 self.cancelled = False
80                 self.timeChanged()
81
82         def timeChanged(self):
83                 if self.nocheck and self.state < self.StateRunning:
84                         self.state = self.StatePrepared
85
86         def shouldSkip(self):
87                 return False
88
89         def __repr__(self):
90                 return ''.join((
91                                 "<EPGRefreshTimerEntry (",
92                                 ', '.join((
93                                         strftime("%c", localtime(self.begin)),
94                                         str(self.repeated),
95                                         str(self.function)
96                                 )),
97                                 ")>"
98                         ))
99
100 class EPGRefreshTimer(timer.Timer):
101         def __init__(self):
102                 timer.Timer.__init__(self)
103
104         def remove(self, entry):
105                 print "[EPGRefresh] Timer removed " + str(entry)
106
107                 # avoid re-enqueuing
108                 entry.repeated = False
109
110                 # abort timer.
111                 # this sets the end time to current time, so timer will be stopped.
112                 entry.abort()
113
114                 if entry.state != entry.StateEnded:
115                         self.timeChanged(entry)
116
117                 print "state: ", entry.state
118                 print "in processed: ", entry in self.processed_timers
119                 print "in running: ", entry in self.timer_list
120                 # now the timer should be in the processed_timers list. remove it from there.
121                 self.processed_timers.remove(entry)
122
123         def setRefreshTimer(self, tocall):
124                 # Add refresh Timer
125                 now = localtime()
126                 # XXX: basic workaround if the clock is not yet set
127                 year = 2011
128                 if now.tm_year > 2011:
129                         year = now.tm_year
130                 begin = mktime(
131                         (year, now.tm_mon, now.tm_mday,
132                         config.plugins.epgrefresh.begin.value[0],
133                         config.plugins.epgrefresh.begin.value[1],
134                         0, now.tm_wday, now.tm_yday, now.tm_isdst)
135                 )
136
137                 # If the last scan was finished before our timespan begins/began and
138                 # timespan began in the past fire the timer once (timer wouldn't do so
139                 # by itself)
140                 if config.plugins.epgrefresh.lastscan.value < begin and begin < time():
141                         tocall()
142
143                 refreshTimer = EPGRefreshTimerEntry(begin, tocall, nocheck = True)
144
145                 i = 0
146                 while i < 7:
147                         refreshTimer.setRepeated(i)
148                         i += 1
149
150                 # We can be sure that whenever this function is called the timer list
151                 # was wiped, so just add a new timer
152                 self.addTimerEntry(refreshTimer)
153
154         def add(self, entry):
155                 entry.timeChanged()
156                 print "[EPGRefresh] Timer added " + str(entry)
157                 self.addTimerEntry(entry)
158
159         def clear(self):
160                 self.timer_list = []
161
162         def isActive(self):
163                 return len(self.timer_list) > 0
164
165 epgrefreshtimer = EPGRefreshTimer()