intial commit of autotimer, epgrefresh and werbezapper to enigma2-plugins
[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
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[3] > begin[0] or (time[3] == begin[0] and time[4] >= 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[3] < end[0] or (time[3] == end[0] and time[4] <= 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[3] < begin[0] or (time[3] == begin[0] and time[4] <= 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[3] > end[0] or (time[3] == end[0] and time[4] >= 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.prepare_time = 0
45                 self.function = tocall
46                 self.nocheck = nocheck
47
48         def getNextActivation(self):
49                 return self.begin
50
51         def activate(self):
52                 if self.state == self.StateRunning:
53                         print "[EPGRefresh] Timer activated"
54                         # Just execute function and signalize success if told to
55                         if self.nocheck:
56                                 print "[EPGRefresh] Timer is not checking but running function immediately"
57                                 self.function()
58                                 return True
59
60                         # Check if in timespan
61                         if checkTimespan(config.plugins.epgrefresh.begin.value, config.plugins.epgrefresh.end.value):
62                                 print "[EPGRefresh] In Timespan, will check if we're in Standby and have no Recordings running next"
63                                 # Do we realy want to check nav?
64                                 from NavigationInstance import instance
65                                 if config.plugins.epgrefresh.force.value or (Screens.Standby.inStandby and instance is not None and not instance.RecordTimer.isRecording()):
66                                         self.function()
67                                         return True
68                                 else:
69                                         print "[EPGRefresh] Box still in use, rescheduling"     
70
71                                         # Recheck later
72                                         self.begin = time() + config.plugins.epgrefresh.delay_standby.value*60
73                                         return False
74                         else:
75                                 print "[EPGRefresh] Not in timespan, rescheduling"
76
77                                 # Recheck later
78                                 self.begin = config.plugins.epgrefresh.delay_standby.value*60
79                                 return False
80
81                 return True
82
83         def shouldSkip(self):
84                 return False
85
86 class EPGRefreshTimer(timer.Timer):
87         def __init__(self):
88                 timer.Timer.__init__(self)
89
90         def remove(self, entry):
91                 print "[EPGRefresh] Timer removed " + str(entry)
92
93                 # avoid re-enqueuing
94                 entry.repeated = False
95
96                 # abort timer.
97                 # this sets the end time to current time, so timer will be stopped.
98                 entry.abort()
99
100                 if entry.state != entry.StateEnded:
101                         self.timeChanged(entry)
102
103                 print "state: ", entry.state
104                 print "in processed: ", entry in self.processed_timers
105                 print "in running: ", entry in self.timer_list
106                 # now the timer should be in the processed_timers list. remove it from there.
107                 self.processed_timers.remove(entry)
108
109         def setRefreshTimer(self, tocall):
110                 # Add refresh Timer
111                 begin = [x for x in localtime()]
112                 begin[3] = config.plugins.epgrefresh.begin.value[0]
113                 begin[4] = config.plugins.epgrefresh.begin.value[1]
114                 begin = mktime(begin)
115
116                 # Also call function when begin lies in the past (timer won't do so)
117                 if begin < time():
118                         tocall()
119
120                 refreshTimer = EPGRefreshTimerEntry(begin, tocall, nocheck = True)
121
122                 for x in range(0,7):
123                         refreshTimer.setRepeated(x)
124
125                 # We can be sure that whenever this function is called the timer list
126                 # was wiped, so just add a new timer
127                 self.addTimerEntry(refreshTimer)
128
129         def add(self, entry):
130                 entry.timeChanged()
131                 print "[EPGRefresh] Timer added " + str(entry)
132                 self.addTimerEntry(entry)
133
134         def clear(self):
135                 self.timer_list = []
136
137         def isActive(self):
138                 return len(self.timer_list) > 0
139
140 epgrefreshtimer = EPGRefreshTimer()