c0896f4107ff1953fddaba589e37dfed672dd0f7
[enigma2.git] / lib / python / Screens / TimerEdit.py
1 from Screen import Screen
2 from Components.TimerList import TimerList, TimerEntryComponent
3 from Components.ActionMap import ActionMap
4 from Components.TimeInput import TimeInput
5 from Components.Label import Label
6 from Components.Button import Button
7 from TimerEntry import TimerEntry
8 from RecordTimer import RecordTimerEntry, parseEvent
9 from time import *
10 from ServiceReference import ServiceReference
11 from Components.config import *
12
13 class TimerEditList(Screen):
14         def __init__(self, session):
15                 Screen.__init__(self, session)
16                 
17                 list = [ ]
18                 self.list = list
19                 self.fillTimerList()
20
21                 self["timerlist"] = TimerList(list)
22                 
23                 self["key_red"] = Button(_("Delete"))
24                 self["key_green"] = Button(_("Add"))
25                 self["key_yellow"] = Button("")
26                 self["key_blue"] = Button("")
27
28                 self["actions"] = ActionMap(["OkCancelActions", "ShortcutActions"], 
29                         {
30                                 "ok": self.openEdit,
31                                 "cancel": self.leave,
32                                 "red": self.removeTimer,
33                                 "green": self.addCurrentTimer
34                         })
35                 self.session.nav.RecordTimer.on_state_change.append(self.onStateChange)
36
37         def fillTimerList(self):
38                 del self.list[:]
39                 
40                 for timer in self.session.nav.RecordTimer.timer_list:
41                         self.list.append(TimerEntryComponent(timer, processed=False))
42                 
43                 for timer in self.session.nav.RecordTimer.processed_timers:
44                         self.list.append(TimerEntryComponent(timer, processed=True))
45
46         def openEdit(self):
47                 self.session.openWithCallback(self.finishedEdit, TimerEntry, self["timerlist"].getCurrent()[0])
48                 #self.session.open(TimerEdit, self["timerlist"].getCurrent()[0])
49                 
50         def removeTimer(self):
51                 list = self["timerlist"]
52                 currentIndex = list.getCurrentIndex()
53                 list.moveDown()
54                 if list.getCurrentIndex() == currentIndex:
55                         currentIndex -= 1
56                         list.moveToIndex(currentIndex)
57                 self.session.nav.RecordTimer.removeEntry(list.getCurrent()[0])
58                 self.refill()
59         
60         def refill(self):
61                 self.fillTimerList()
62                 self["timerlist"].invalidate()
63         
64         def addCurrentTimer(self):
65                 event = None
66                 service = self.session.nav.getCurrentService()
67                 if service is not None:
68                         info = service.info()
69                         if info is not None:
70                                 event = info.getEvent(0)
71
72                 # FIXME only works if already playing a service
73                 serviceref = ServiceReference(self.session.nav.getCurrentlyPlayingServiceReference())
74                 
75                 if event is None:       
76                         data = (int(time()), int(time() + 60), "unknown event", "", None)
77                 else:
78                         data = parseEvent(event)
79
80                 self.addTimer(RecordTimerEntry(serviceref, *data))
81                 
82         def addTimer(self, timer):
83                 self.session.openWithCallback(self.finishedAdd, TimerEntry, timer)
84                 
85         def finishedEdit(self, answer):
86                 print "finished edit"
87                 if answer[0]:
88                         print "Edited timer"
89                         self.session.nav.RecordTimer.timeChanged(answer[1])
90                         self.fillTimerList()
91                 else:
92                         print "Timeredit aborted"
93
94         def finishedAdd(self, answer):
95                 print "finished add"
96                 if answer[0]:
97                         entry = answer[1]
98                         self.session.nav.RecordTimer.record(entry)
99                         self.fillTimerList()
100                 else:
101                         print "Timeredit aborted"               
102
103         def leave(self):
104                 self.session.nav.RecordTimer.saveTimer()
105                 self.session.nav.RecordTimer.on_state_change.remove(self.onStateChange)
106                 self.close()
107
108         def onStateChange(self, entry):
109                 self.refill()