cleanup the timerlist by pressing blue in the timer list
[enigma2.git] / lib / python / Screens / TimerEdit.py
index 173b1ba20b336fd071a757594861c93748f21012..99939bd8e1563bc5fdccb569242d1965001df85c 100644 (file)
 from Screen import Screen
-from Components.TimerList import TimerList, TimerEntry
+from Components.TimerList import TimerList, TimerEntryComponent
 from Components.ActionMap import ActionMap
 from Components.TimeInput import TimeInput
 from Components.Label import Label
 from Components.Button import Button
-from Components.TextInput import TextInput
+from Screens.MessageBox import MessageBox
+from TimerEntry import TimerEntry, TimerLog
+from RecordTimer import RecordTimerEntry, parseEvent
+from time import *
+from ServiceReference import ServiceReference
+from Components.config import *
 
-class TimerEdit(Screen):
-       def __init__(self, session, entry):
+class TimerEditList(Screen):
+       def __init__(self, session):
                Screen.__init__(self, session)
+               
+               list = [ ]
+               self.list = list
+               self.fillTimerList()
 
-               self["actions"] = ActionMap(["OkCancelActions"], 
-                       {
-                               "ok": self.apply,
-                               "cancel": self.close
-                       })
+               self["timerlist"] = TimerList(list)
                
-               self["shortcuts"] = ActionMap(["ShortcutActions"],
+               self["key_red"] = Button(_("Delete"))
+               self["key_green"] = Button(_("Add"))
+               self["key_yellow"] = Button("")
+               self["key_blue"] = Button(_("Cleanup"))
+
+               self["actions"] = ActionMap(["OkCancelActions", "ShortcutActions", "TimerEditActions"], 
                        {
-                               "red": self.beginFocus,
-                               "yellow": self.endFocus,
-                               "green": self.descFocus
+                               "ok": self.openEdit,
+                               "cancel": self.leave,
+                               "red": self.removeTimer,
+                               "green": self.addCurrentTimer,
+                               "blue": self.cleanupQuestion,
+                               "log": self.showLog
                        })
+               self.session.nav.RecordTimer.on_state_change.append(self.onStateChange)
+
+       def fillTimerList(self):
+               del self.list[:]
                
-               self.entry = entry
-               # begin, end, description, service
-               self["begin"] = TimeInput()
-               self["end"] = TimeInput()
-               
-               self["lbegin"] = Label("Begin")
-               self["lend"] = Label("End")
+               for timer in self.session.nav.RecordTimer.timer_list:
+                       self.list.append(TimerEntryComponent(timer, processed=False))
                
-               self["description"] = TextInput()
-               self["apply"] = Button("Apply")
-               self["service"] = Button()
+               for timer in self.session.nav.RecordTimer.processed_timers:
+                       self.list.append(TimerEntryComponent(timer, processed=True))
+
+       def showLog(self):
+               self.session.openWithCallback(self.finishedEdit, TimerLog, self["timerlist"].getCurrent()[0])
+
+       def openEdit(self):
+               self.session.openWithCallback(self.finishedEdit, TimerEntry, self["timerlist"].getCurrent()[0])
+               #self.session.open(TimerEdit, self["timerlist"].getCurrent()[0])
                
-               self["description"].setText(entry.description);
-       
-       def beginFocus(self):
-               self.setFocus(self["begin"])
+       def cleanupQuestion(self):
+               self.session.openWithCallback(self.cleanupTimer, MessageBox, _("Really delete done timers?"))
        
-       def endFocus(self):
-               self.setFocus(self["end"])
-       
-       def descFocus(self):
-               self.setFocus(self["description"])
+       def cleanupTimer(self, delete):
+               if delete:
+                       self.session.nav.RecordTimer.cleanup()
+                       self.refill()
+               
+       def removeTimer(self):
+               list = self["timerlist"]
+               currentIndex = list.getCurrentIndex()
+               list.moveDown()
+               if list.getCurrentIndex() == currentIndex:
+                       currentIndex -= 1
+                       list.moveToIndex(currentIndex)
+               self.session.nav.RecordTimer.removeEntry(list.getCurrent()[0])
+               self.refill()
        
-       def apply(self):
-               print "applied!"
+       def refill(self):
+               self.fillTimerList()
+               self["timerlist"].invalidate()
        
-class TimerEditList(Screen):
-       def __init__(self, session):
-               Screen.__init__(self, session)
+       def addCurrentTimer(self):
+               event = None
+               service = self.session.nav.getCurrentService()
+               if service is not None:
+                       info = service.info()
+                       if info is not None:
+                               event = info.getEvent(0)
+
+               # FIXME only works if already playing a service
+               serviceref = ServiceReference(self.session.nav.getCurrentlyPlayingServiceReference())
                
-               list = [ ]
-               for timer in session.nav.RecordTimer.timer_list:
-                       list.append(TimerEntry(timer, 0))
+               if event is None:       
+                       data = (int(time()), int(time() + 60), "unknown event", "", None)
+               else:
+                       data = parseEvent(event)
+
+               self.addTimer(RecordTimerEntry(serviceref, *data))
                
-               for timer in session.nav.RecordTimer.processed_timers:
-                       list.append(TimerEntry(timer, 1))
+       def addTimer(self, timer):
+               self.session.openWithCallback(self.finishedAdd, TimerEntry, timer)
                
-               self["timerlist"] = TimerList(list)
+       def finishedEdit(self, answer):
+               print "finished edit"
+               if answer[0]:
+                       print "Edited timer"
+                       self.session.nav.RecordTimer.timeChanged(answer[1])
+                       self.fillTimerList()
+               else:
+                       print "Timeredit aborted"
 
-               self["actions"] = ActionMap(["OkCancelActions"], 
-                       {
-#                              "ok": self.openEdit,
-                               "cancel": self.close
-                       })
+       def finishedAdd(self, answer):
+               print "finished add"
+               if answer[0]:
+                       entry = answer[1]
+                       self.session.nav.RecordTimer.record(entry)
+                       self.fillTimerList()
+               else:
+                       print "Timeredit aborted"               
 
-       def openEdit(self):
-               self.session.open(TimerEdit, self["timerlist"].getCurrent()[0])
+       def leave(self):
+               self.session.nav.RecordTimer.saveTimer()
+               self.session.nav.RecordTimer.on_state_change.remove(self.onStateChange)
+               self.close()
+
+       def onStateChange(self, entry):
+               self.refill()