more complete implementation of national option character subsets
[enigma2.git] / timer.py
index c03eb1e..e298f66 100644 (file)
--- a/timer.py
+++ b/timer.py
@@ -1,5 +1,6 @@
 import bisect
 import time
+import calendar
 from enigma import *
 
 class TimerEntry:
@@ -31,11 +32,17 @@ class TimerEntry:
        # update self.begin and self.end according to the self.repeated-flags
        def processRepeated(self):
                print "ProcessRepeated"
-               print time.strftime("%c", time.localtime(self.begin))
-               print time.strftime("%c", time.localtime(self.end))
                if (self.repeated != 0):
                        now = int(time.time()) + 1
-                       
+
+                       #to avoid problems with daylight saving, we need to calculate with localtime, in struct_time representation
+                       localbegin = time.localtime(self.begin)
+                       localend = time.localtime(self.end)
+                       localnow = time.localtime(now)
+
+                       print time.strftime("%c", localbegin)
+                       print time.strftime("%c", localend)
+
                        day = []
                        flags = self.repeated
                        for x in range(0, 7):
@@ -46,18 +53,23 @@ class TimerEntry:
                                        day.append(1)
                                flags = flags >> 1
 
-                       print time.strftime("%c", time.localtime(now))
+                       print time.strftime("%c", localnow)
+                       while ((day[localbegin.tm_wday] != 0) or ((day[localbegin.tm_wday] == 0) and localend < localnow)):
+                               print time.strftime("%c", localbegin)
+                               print time.strftime("%c", localend)
+                               #add one day to the struct_time, we have to convert using gmt functions, because the daylight saving flag might change after we add our 86400 seconds
+                               localbegin = time.gmtime(calendar.timegm(localbegin) + 86400)
+                               localend = time.gmtime(calendar.timegm(localend) + 86400)
+
+                       #we now have a struct_time representation of begin and end in localtime, but we have to calculate back to (gmt) seconds since epoch
+                       self.begin = int(time.mktime(localbegin))
+                       self.end = int(time.mktime(localend)) + 1
+
+                       print "ProcessRepeated result"
                        print time.strftime("%c", time.localtime(self.begin))
                        print time.strftime("%c", time.localtime(self.end))
-                       print str(time.localtime(self.begin).tm_wday)
-                       while ((day[time.localtime(self.begin).tm_wday] != 0) or ((day[time.localtime(self.begin).tm_wday] == 0) and self.end < now)):
-                               print time.strftime("%c", time.localtime(self.begin))
-                               print time.strftime("%c", time.localtime(self.end))
-                               self.begin += 86400
-                               self.end += 86400
-                       
+
                        self.timeChanged()
-                       
 
        def __lt__(self, o):
                return self.getNextActivation() < o.getNextActivation()
@@ -81,11 +93,19 @@ class TimerEntry:
                # set begin to now.
                if self.begin > self.end:
                        self.begin = self.end
+
+               self.cancelled = True
        
        # must be overridden!
        def getNextActivation():
                pass
 
+       def disable(self):
+               self.disabled = True
+       
+       def enable(self):
+               self.disabled = False
+
 class Timer:
        # the time between "polls". We do this because
        # we want to account for time jumps etc.
@@ -113,6 +133,11 @@ class Timer:
                for f in self.on_state_change:
                        f(entry)
                        
+       def getNextRecordingTime(self):
+               if len(self.timer_list) > 0:
+                       return self.timer_list[0].begin
+               return -1
+                       
        def cleanup(self):
                self.processed_timers = [entry for entry in self.processed_timers if entry.disabled]
        
@@ -124,6 +149,9 @@ class Timer:
                # right into the processedTimers.
                if entry.shouldSkip() or entry.state == TimerEntry.StateEnded or (entry.state == TimerEntry.StateWaiting and entry.disabled):
                        print "already passed, skipping"
+                       print "shouldSkip:", entry.shouldSkip()
+                       print "state == ended", entry.state == TimerEntry.StateEnded
+                       print "waiting && disabled:", (entry.state == TimerEntry.StateWaiting and entry.disabled)
                        bisect.insort(self.processed_timers, entry)
                        entry.state = TimerEntry.StateEnded
                else:
@@ -162,9 +190,16 @@ class Timer:
                self.setNextActivation(min)
        
        def timeChanged(self, timer):
+               print "time changed"
                timer.timeChanged()
-               self.timer_list.remove(timer)
+               if timer.state == TimerEntry.StateEnded:
+                       self.processed_timers.remove(timer)
+               else:
+                       self.timer_list.remove(timer)
 
+               # give the timer a chance to re-enqueue
+               if timer.state == TimerEntry.StateEnded:
+                       timer.state = TimerEntry.StateWaiting
                self.addTimerEntry(timer)
        
        def doActivate(self, w):
@@ -173,8 +208,7 @@ class Timer:
                # when activating a timer which has already passed,
                # simply abort the timer. don't run trough all the stages.
                if w.shouldSkip():
-                       w.abort()
-                       bisect.insort(self.processed_timers, w)
+                       w.state = TimerEntry.StateEnded
                else:
                        # when active returns true, this means "accepted".
                        # otherwise, the current state is kept.