Merge branch 'bug_623_fix_advanced_diseqc_1.0' into experimental
[enigma2.git] / lib / python / Screens / EpgSelection.py
index 6740bfb6d6ea0da297f3834d5556708e9149c18a..f8edba11fe06c9e6b737f5afcd08f5e0b1393095 100644 (file)
@@ -5,11 +5,16 @@ from Components.Pixmap import Pixmap
 from Components.Label import Label
 from Components.EpgList import EPGList, EPG_TYPE_SINGLE, EPG_TYPE_SIMILAR, EPG_TYPE_MULTI
 from Components.ActionMap import ActionMap
+from Components.TimerSanityCheck import TimerSanityCheck
+from Components.UsageConfig import preferredTimerPath
+from Components.Sources.ServiceEvent import ServiceEvent
+from Components.Sources.Event import Event
 from Screens.TimerEdit import TimerSanityConflict
 from Screens.EventView import EventViewSimple
+from Screens.MessageBox import MessageBox
 from TimeDateInput import TimeDateInput
 from enigma import eServiceReference
-from RecordTimer import RecordTimerEntry, parseEvent
+from RecordTimer import RecordTimerEntry, parseEvent, AFTEREVENT
 from TimerEntry import TimerEntry
 from ServiceReference import ServiceReference
 from time import localtime, time
@@ -17,12 +22,22 @@ from time import localtime, time
 mepg_config_initialized = False
 
 class EPGSelection(Screen):
-       def __init__(self, session, service, zapFunc=None, eventid=None, bouquetChangeCB=None):
+       EMPTY = 0
+       ADD_TIMER = 1
+       REMOVE_TIMER = 2
+       
+       ZAP = 1
+
+       def __init__(self, session, service, zapFunc=None, eventid=None, bouquetChangeCB=None, serviceChangeCB=None):
                Screen.__init__(self, session)
                self.bouquetChangeCB = bouquetChangeCB
+               self.serviceChangeCB = serviceChangeCB
                self.ask_time = -1 #now
                self["key_red"] = Button("")
                self.closeRecursive = False
+               self.saved_title = None
+               self["Service"] = ServiceEvent()
+               self["Event"] = Event()
                if isinstance(service, str) and eventid != None:
                        self.type = EPG_TYPE_SIMILAR
                        self["key_yellow"] = Button()
@@ -58,7 +73,9 @@ class EPGSelection(Screen):
                        self.zapFunc = zapFunc
 
                self["key_green"] = Button(_("Add timer"))
-               self["list"] = EPGList(type = self.type, selChangedCB = self.onSelectionChanged, timer = self.session.nav.RecordTimer)
+               self.key_green_choice = self.ADD_TIMER
+               self.key_red_choice = self.EMPTY
+               self["list"] = EPGList(type = self.type, selChangedCB = self.onSelectionChanged, timer = session.nav.RecordTimer)
 
                self["actions"] = ActionMap(["EPGSelectActions", "OkCancelActions"],
                        {
@@ -70,11 +87,12 @@ class EPGSelection(Screen):
                                "info": self.infoKeyPressed,
                                "red": self.zapTo,
                                "input_date_time": self.enterDateTime,
-                               "nextBouquet": self.nextBouquet,
-                               "prevBouquet": self.prevBouquet
+                               "nextBouquet": self.nextBouquet, # just used in multi epg yet
+                               "prevBouquet": self.prevBouquet, # just used in multi epg yet
+                               "nextService": self.nextService, # just used in single epg yet
+                               "prevService": self.prevService, # just used in single epg yet
                        })
                self["actions"].csel = self
-
                self.onLayoutFinish.append(self.onCreate)
 
        def nextBouquet(self):
@@ -85,6 +103,14 @@ class EPGSelection(Screen):
                if self.bouquetChangeCB:
                        self.bouquetChangeCB(-1, self)
 
+       def nextService(self):
+               if self.serviceChangeCB:
+                       self.serviceChangeCB(1, self)
+
+       def prevService(self):
+               if self.serviceChangeCB:
+                       self.serviceChangeCB(-1, self)
+
        def enterDateTime(self):
                if self.type == EPG_TYPE_MULTI:
                        global mepg_config_initialized
@@ -119,14 +145,25 @@ class EPGSelection(Screen):
                self.services = services
                self.onCreate()
 
+       def setService(self, service):
+               self.currentService = service
+               self.onCreate()
+
        #just used in multipeg
        def onCreate(self):
                l = self["list"]
                l.recalcEntrySize()
                if self.type == EPG_TYPE_MULTI:
                        l.fillMultiEPG(self.services, self.ask_time)
+                       l.moveToService(self.session.nav.getCurrentlyPlayingServiceReference())
                elif self.type == EPG_TYPE_SINGLE:
-                       l.fillSingleEPG(self.currentService)
+                       service = self.currentService
+                       self["Service"].newService(service.ref)
+                       if self.saved_title is None:
+                               self.saved_title = self.instance.getTitle()
+                       title = self.saved_title + ' - ' + service.getServiceName()
+                       self.instance.setTitle(title)
+                       l.fillSingleEPG(service)
                else:
                        l.fillSimilarList(self.currentService, self.eventid)
 
@@ -145,7 +182,7 @@ class EPGSelection(Screen):
                        setEvent(cur[0])
 
        def zapTo(self): # just used in multiepg
-               if self.zapFunc and self["key_red"].getText() == "Zap":
+               if self.zapFunc and self.key_red_choice == self.ZAP:
                        lst = self["list"]
                        count = lst.getCurrentChangeCount()
                        if count == 0:
@@ -179,14 +216,28 @@ class EPGSelection(Screen):
                if self.type == EPG_TYPE_MULTI:
                        self["list"].updateMultiEPG(1)
 
+       def removeTimer(self, timer):
+               timer.afterEvent = AFTEREVENT.NONE
+               self.session.nav.RecordTimer.removeEntry(timer)
+               self["key_green"].setText(_("Add timer"))
+               self.key_green_choice = self.ADD_TIMER
+
        def timerAdd(self):
                cur = self["list"].getCurrent()
                event = cur[0]
                serviceref = cur[1]
                if event is None:
                        return
-               newEntry = RecordTimerEntry(serviceref, checkOldTimers = True, *parseEvent(event))
-               self.session.openWithCallback(self.finishedAdd, TimerEntry, newEntry)
+               eventid = event.getEventId()
+               refstr = serviceref.ref.toString()
+               for timer in self.session.nav.RecordTimer.timer_list:
+                       if timer.eit == eventid and timer.service_ref.ref.toString() == refstr:
+                               cb_func = lambda ret : not ret or self.removeTimer(timer)
+                               self.session.openWithCallback(cb_func, MessageBox, _("Do you really want to delete %s?") % event.getEventName())
+                               break
+               else:
+                       newEntry = RecordTimerEntry(serviceref, checkOldTimers = True, dirname = preferredTimerPath(), *parseEvent(event))
+                       self.session.openWithCallback(self.finishedAdd, TimerEntry, newEntry)
 
        def finishedAdd(self, answer):
                print "finished add"
@@ -194,14 +245,18 @@ class EPGSelection(Screen):
                        entry = answer[1]
                        simulTimerList = self.session.nav.RecordTimer.record(entry)
                        if simulTimerList is not None:
-                               if (len(simulTimerList) == 2) and (simulTimerList[1].dontSave) and (simulTimerList[1].autoincrease):
-                                       simulTimerList[1].end = entry.begin - 30
-                                       self.session.nav.RecordTimer.timeChanged(simulTimerList[1])
-                                       self.session.nav.RecordTimer.record(entry)
-                               else:
+                               for x in simulTimerList:
+                                       if x.setAutoincreaseEnd(entry):
+                                               self.session.nav.RecordTimer.timeChanged(x)
+                               simulTimerList = self.session.nav.RecordTimer.record(entry)
+                               if simulTimerList is not None:
                                        self.session.openWithCallback(self.finishSanityCorrection, TimerSanityConflict, simulTimerList)
+                       self["key_green"].setText(_("Remove timer"))
+                       self.key_green_choice = self.REMOVE_TIMER
                else:
-                       print "Timeredit aborted"               
+                       self["key_green"].setText(_("Add timer"))
+                       self.key_green_choice = self.ADD_TIMER
+                       print "Timeredit aborted"
        
        def finishSanityCorrection(self, answer):
                self.finishedAdd(answer)
@@ -211,7 +266,7 @@ class EPGSelection(Screen):
 
        def moveDown(self):
                self["list"].moveDown()
-
+       
        def applyButtonState(self, state):
                if state == 0:
                        self["now_button"].hide()
@@ -226,13 +281,11 @@ class EPGSelection(Screen):
                        self["key_red"].setText("")
                else:
                        if state == 1:
-                               self["key_red"].setText("Zap")
                                self["now_button_sel"].show()
                                self["now_button"].hide()
                        else:
                                self["now_button"].show()
                                self["now_button_sel"].hide()
-                               self["key_red"].setText("")
 
                        if state == 2:
                                self["next_button_sel"].show()
@@ -249,6 +302,17 @@ class EPGSelection(Screen):
                                self["more_button_sel"].hide()
 
        def onSelectionChanged(self):
+               cur = self["list"].getCurrent()
+               if cur is None:
+                       if self.key_green_choice != self.EMPTY:
+                               self["key_green"].setText("")
+                               self.key_green_choice = self.EMPTY
+                       if self.key_red_choice != self.EMPTY:
+                               self["key_red"].setText("")
+                               self.key_red_choice = self.EMPTY
+                       return
+               event = cur[0]
+               self["Event"].newEvent(event)
                if self.type == EPG_TYPE_MULTI:
                        count = self["list"].getCurrentChangeCount()
                        if self.ask_time != -1:
@@ -260,8 +324,7 @@ class EPGSelection(Screen):
                        else:
                                self.applyButtonState(1)
                        days = [ _("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat"), _("Sun") ]
-                       datastr = ""
-                       event = self["list"].getCurrent()[0]
+                       datestr = ""
                        if event is not None:
                                now = time()
                                beg = event.getBeginTime()
@@ -272,3 +335,40 @@ class EPGSelection(Screen):
                                else:
                                        datestr = '%s %d.%d.'%(_("Today"), begTime[2], begTime[1])
                        self["date"].setText(datestr)
+                       if cur[1] is None:
+                               self["Service"].newService(None)
+                       else:
+                               self["Service"].newService(cur[1].ref)
+
+               if cur[1] is None or cur[1].getServiceName() == "":
+                       if self.key_green_choice != self.EMPTY:
+                               self["key_green"].setText("")
+                               self.key_green_choice = self.EMPTY
+                       if self.key_red_choice != self.EMPTY:
+                               self["key_red"].setText("")
+                               self.key_red_choice = self.EMPTY
+                       return
+               elif self.key_red_choice != self.ZAP and  self.type == EPG_TYPE_MULTI:
+                               self["key_red"].setText("Zap")
+                               self.key_red_choice = self.ZAP
+
+               if event is None:
+                       if self.key_green_choice != self.EMPTY:
+                               self["key_green"].setText("")
+                               self.key_green_choice = self.EMPTY
+                       return
+
+               serviceref = cur[1]
+               eventid = event.getEventId()
+               refstr = serviceref.ref.toString()
+               isRecordEvent = False
+               for timer in self.session.nav.RecordTimer.timer_list:
+                       if timer.eit == eventid and timer.service_ref.ref.toString() == refstr:
+                               isRecordEvent = True
+                               break
+               if isRecordEvent and self.key_green_choice != self.REMOVE_TIMER:
+                       self["key_green"].setText(_("Remove timer"))
+                       self.key_green_choice = self.REMOVE_TIMER
+               elif not isRecordEvent and self.key_green_choice != self.ADD_TIMER:
+                       self["key_green"].setText(_("Add timer"))
+                       self.key_green_choice = self.ADD_TIMER