Merge branch 'obi/master'
[enigma2.git] / lib / python / Components / TimerSanityCheck.py
index e7cc8b9b6f65e5f2c15db4a02a32bcfdf7e686cd..b472a19e130c305512a5a5eeee931d5a103d11c9 100644 (file)
@@ -1,13 +1,12 @@
-import string
 import NavigationInstance
-from time import localtime
-from Components.NimManager import nimmanager
+from time import localtime, mktime, gmtime
 from ServiceReference import ServiceReference
-from enigma import iServiceInformation, eServiceCenter
+from enigma import iServiceInformation, eServiceCenter, eServiceReference
 
 class TimerSanityCheck:
        def __init__(self, timerlist, newtimer=None):
                print "sanitycheck"
+               self.localtimediff = 25*3600 - mktime(gmtime(25*3600))
                self.timerlist = timerlist
                self.newtimer = newtimer
                self.simultimer = []
@@ -37,9 +36,15 @@ class TimerSanityCheck:
                                        return True
                                else:
                                        if timer.begin == self.newtimer.begin:
+                                               fl1 = timer.service_ref.ref.flags & eServiceReference.isGroup
+                                               fl2 = self.newtimer.service_ref.ref.flags & eServiceReference.isGroup
+                                               if fl1 != fl2:
+                                                       return False
+                                               if fl1: #is group
+                                                       return timer.service_ref.ref.getPath() == self.newtimer.service_ref.ref.getPath()
                                                getUnsignedDataRef1 = timer.service_ref.ref.getUnsignedData
                                                getUnsignedDataRef2 = self.newtimer.service_ref.ref.getUnsignedData
-                                               for x in range(1,5):
+                                               for x in (1, 2, 3, 4):
                                                        if getUnsignedDataRef1(x) != getUnsignedDataRef2(x):
                                                                break;
                                                else:
@@ -50,10 +55,11 @@ class TimerSanityCheck:
                #with special service for external plugins
                # Entries in eventlist
                # timeindex
+               # BeginEndFlag 1 for begin, -1 for end
                # index -1 for the new Timer, 0..n index of the existing timers
-               # BeginEndFlag -1 for begin, +1 for end
                # count of running timers
 
+               serviceHandler = eServiceCenter.getInstance()
                print "checkTimerlist"
 # create a list with all start and end times
 # split it into recurring and singleshot timers
@@ -71,13 +77,17 @@ class TimerSanityCheck:
                        rflags = ((rflags & 0x7F)>> 3)|((rflags & 0x07)<<4)
                        if rflags:
                                begin = self.newtimer.begin % 86400 # map to first day
+                               if (self.localtimediff > 0) and ((begin + self.localtimediff) > 86400):
+                                       rflags = ((rflags >> 1)& 0x3F)|((rflags << 6)& 0x40)
+                               elif (self.localtimediff < 0) and (begin < self.localtimediff):
+                                       rflags = ((rflags << 1)& 0x7E)|((rflags >> 6)& 0x01)
                                while rflags: # then arrange on the week
                                        if rflags & 1:
                                                self.rep_eventlist.append((begin, -1))
                                        begin += 86400
                                        rflags >>= 1
                        else:
-                               self.nrep_eventlist.extend([(self.newtimer.begin,-1,self.bflag),(self.newtimer.end,-1,self.eflag)])
+                               self.nrep_eventlist.extend([(self.newtimer.begin,self.bflag,-1),(self.newtimer.end,self.eflag,-1)])
 
 ##################################################################################
 # now process existing timers
@@ -88,13 +98,17 @@ class TimerSanityCheck:
                                        rflags = timer.repeated
                                        rflags = ((rflags & 0x7F)>> 3)|((rflags & 0x07)<<4)
                                        begin = timer.begin % 86400 # map all to first day
+                                       if (self.localtimediff > 0) and ((begin + self.localtimediff) > 86400):
+                                               rflags = ((rflags >> 1)& 0x3F)|((rflags << 6)& 0x40)
+                                       elif (self.localtimediff < 0) and (begin < self.localtimediff):
+                                               rflags = ((rflags << 1)& 0x7E)|((rflags >> 6)& 0x01)
                                        while rflags:
                                                if rflags & 1:
                                                        self.rep_eventlist.append((begin, idx))
                                                begin += 86400
                                                rflags >>= 1
                                else:
-                                       self.nrep_eventlist.extend([(timer.begin,idx,self.bflag),(timer.end,idx,self.eflag)])
+                                       self.nrep_eventlist.extend([(timer.begin,self.bflag,idx),(timer.end,self.eflag,idx)])
                        idx += 1
 
 ################################################################################
@@ -121,14 +135,13 @@ class TimerSanityCheck:
                                        new_event_end = new_event_begin + (event_end - event_begin)
                                        if event[1] == -1:
                                                if new_event_begin >= self.newtimer.begin: # is the soap already running?
-                                                       self.nrep_eventlist.extend([(new_event_begin, event[1], self.bflag),(new_event_end, event[1], self.eflag)])
+                                                       self.nrep_eventlist.extend([(new_event_begin, self.bflag, event[1]),(new_event_end, self.eflag, event[1])])
                                        else:
                                                if new_event_begin >= self.timerlist[event[1]].begin: # is the soap already running?
-                                                       self.nrep_eventlist.extend([(new_event_begin, event[1], self.bflag),(new_event_end, event[1], self.eflag)])
+                                                       self.nrep_eventlist.extend([(new_event_begin, self.bflag, event[1]),(new_event_end, self.eflag, event[1])])
                else:
                        offset_0 = 345600 # the Epoch begins on Thursday
-                       weeks = 2 # test two weeks to take care of Sunday-Monday transitions
-                       for cnt in range(weeks):
+                       for cnt in (0, 1): # test two weeks to take care of Sunday-Monday transitions
                                for event in self.rep_eventlist:
                                        if event[1] == -1: # -1 is the identifier of the changed timer
                                                event_begin = self.newtimer.begin
@@ -138,58 +151,69 @@ class TimerSanityCheck:
                                                event_end = self.timerlist[event[1]].end
                                        new_event_begin = event[0] + offset_0 + (cnt * 604800)
                                        new_event_end = new_event_begin + (event_end - event_begin)
-                                       self.nrep_eventlist.extend([(new_event_begin, event[1], self.bflag),(new_event_end, event[1], self.eflag)])
-
-               def sort_func(x, y):
-                       ret = cmp(x[0], y[0])
-                       if not ret:
-                               ret = cmp(y[2], x[2])
-                               if not ret:
-                                       return cmp(x[1], y[1])
-                       return ret
+                                       self.nrep_eventlist.extend([(new_event_begin, self.bflag, event[1]),(new_event_end, self.eflag, event[1])])
+
 ################################################################################
 # order list chronological
-               self.nrep_eventlist.sort(sort_func)
+               self.nrep_eventlist.sort()
 
 ##################################################################################
 # detect overlapping timers and overlapping times
                fakeRecList = []
                ConflictTimer = None
                ConflictTunerType = None
-               ConflictSlot = None
                newTimerTunerType = None
-               newTimerTunerSlot = None
                cnt = 0
                idx = 0
                overlaplist = []
                for event in self.nrep_eventlist:
-                       cnt -= event[2]
-                       if event[1] == -1: # new timer
+                       cnt += event[1]
+                       if event[2] == -1: # new timer
                                timer = self.newtimer
                        else:
-                               timer = self.timerlist[event[1]]
-                       if event[2] == self.bflag:
-                               fakeRecService = NavigationInstance.instance.recordService(timer.service_ref)
-                               fakeRecResult = fakeRecService.start(True)
-                               feinfo = fakeRecService.frontendInfo().getFrontendData()
-                               tunerType = feinfo.get("tuner_type")
-                               tunerSlot = feinfo.get("tuner_number")
-                               if event[1] == -1: # new timer
+                               timer = self.timerlist[event[2]]
+                       if event[1] == self.bflag:
+                               tunerType = [ ]
+                               fakeRecService = NavigationInstance.instance.recordService(timer.service_ref, True)
+                               if fakeRecService:
+                                       fakeRecResult = fakeRecService.start(True)
+                               else:
+                                       fakeRecResult = -1
+                               if not fakeRecResult: # tune okay
+                                       feinfo = fakeRecService.frontendInfo().getFrontendData()
+                                       tunerType.append(feinfo.get("tuner_type"))
+                               else: # tune failed.. so we must go another way to get service type (DVB-S, DVB-T, DVB-C)
+
+                                       def getServiceType(ref): # helper function to get a service type of a service reference
+                                               serviceInfo = serviceHandler.info(ref)
+                                               serviceInfo = serviceInfo and serviceInfo.getInfoObject(ref, iServiceInformation.sTransponderData)
+                                               return serviceInfo and serviceInfo["tuner_type"] or ""
+
+                                       ref = timer.service_ref.ref
+                                       if ref.flags & eServiceReference.isGroup: # service group ?
+                                               serviceList = serviceHandler.list(ref) # get all alternative services
+                                               if serviceList:
+                                                       for ref in serviceList.getContent("R"): # iterate over all group service references
+                                                               type = getServiceType(ref)
+                                                               if not type in tunerType: # just add single time
+                                                                       tunerType.append(type) 
+                                       else:
+                                               tunerType.append(getServiceType(ref))
+
+                               if event[2] == -1: # new timer
                                        newTimerTunerType = tunerType
-                                       newTimerTunerSlot = tunerSlot
-                               overlaplist.append((fakeRecResult, timer, tunerType, tunerSlot))
+                               overlaplist.append((fakeRecResult, timer, tunerType))
                                fakeRecList.append((timer, fakeRecService))
                                if fakeRecResult:
                                        if ConflictTimer is None: # just take care of the first conflict
                                                ConflictTimer = timer
                                                ConflictTunerType = tunerType
-                                               ConflictTunerSlot = tunerSlot
-                       elif event[2] == self.eflag:
+                       elif event[1] == self.eflag:
                                for fakeRec in fakeRecList:
-                                       if timer == fakeRec[0]:
+                                       if timer == fakeRec[0] and fakeRec[1]:
                                                NavigationInstance.instance.stopRecordService(fakeRec[1])
                                                fakeRecList.remove(fakeRec)
-                               del fakeRec
+                               fakeRec = None
                                for entry in overlaplist:
                                        if entry[1] == timer:
                                                overlaplist.remove(entry)
@@ -218,7 +242,6 @@ class TimerSanityCheck:
                                                if nt and kt:
                                                        ConflictTimer = self.newtimer
                                                        ConflictTunerType = newTimerTunerType
-                                                       ConflictSlot = newTimerTunerSlot
                                                        break
 
                self.simultimer = [ ConflictTimer ]
@@ -230,8 +253,11 @@ class TimerSanityCheck:
                                else:
                                        continue
                                for entry in event[4]:
-                                       if not self.simultimer.count(entry[1]) and (entry[2] == ConflictTunerType or entry[3] == ConflictTunerSlot):
-                                               self.simultimer.append(entry[1])
+                                       if not entry[1] in self.simultimer:
+                                               for x in entry[2]:
+                                                       if x in ConflictTunerType:
+                                                               self.simultimer.append(entry[1])
+                                                               break
 
                if len(self.simultimer) < 2:
                        print "Bug: unknown Conflict!"