add ability to enable/disable "change to next/prev bouquet when quickzap is used...
[enigma2.git] / lib / python / Screens / TimerEdit.py
1 from Screen import Screen
2 from Components.TimerList import TimerList, TimerEntryComponent
3 from Components.ConfigList import ConfigList
4 from Components.MenuList import MenuList
5 from Components.ActionMap import ActionMap
6 from Components.TimeInput import TimeInput
7 from Components.Label import Label
8 from Components.Button import Button
9 from Screens.MessageBox import MessageBox
10 from TimerEntry import TimerEntry, TimerLog
11 from RecordTimer import RecordTimerEntry, parseEvent
12 from time import *
13 from ServiceReference import ServiceReference
14 from Components.config import *
15 from Components.TimerSanityCheck import TimerSanityCheck
16
17 class TimerEditList(Screen):
18         def __init__(self, session):
19                 Screen.__init__(self, session)
20                 
21                 list = [ ]
22                 self.list = list
23                 self.fillTimerList()
24
25                 self["timerlist"] = TimerList(list)
26                 
27                 self["key_red"] = Button(_("Delete"))
28                 self["key_green"] = Button(_("Add"))
29                 self["key_yellow"] = Button("")
30                 self["key_blue"] = Button(_("Cleanup"))
31
32                 self["actions"] = ActionMap(["OkCancelActions", "DirectionActions", "ShortcutActions", "TimerEditActions"], 
33                         {
34                                 "ok": self.openEdit,
35                                 "cancel": self.leave,
36                                 "red": self.removeTimer,
37                                 "green": self.addCurrentTimer,
38                                 "blue": self.cleanupQuestion,
39                                 "yellow": self.toggleDisabledState,
40                                 "log": self.showLog,
41                                 "left": self.left,
42                                 "right": self.right,
43                                 "up": self.up,
44                                 "down": self.down
45                         }, -1)
46                 self.session.nav.RecordTimer.on_state_change.append(self.onStateChange)
47                 self.onShown.append(self.updateState)
48
49         def up(self):
50                 self["timerlist"].instance.moveSelection(self["timerlist"].instance.moveUp)
51                 self.updateState()
52                 
53         def down(self):
54                 self["timerlist"].instance.moveSelection(self["timerlist"].instance.moveDown)
55                 self.updateState()
56
57         def left(self):
58                 self["timerlist"].instance.moveSelection(self["timerlist"].instance.pageUp)
59                 self.updateState()
60                 
61         def right(self):
62                 self["timerlist"].instance.moveSelection(self["timerlist"].instance.pageDown)
63                 self.updateState()
64                 
65         def toggleDisabledState(self):
66                 self["timerlist"].getCurrent()[0].disabled = not self["timerlist"].getCurrent()[0].disabled
67                 self.updateState()
68                 self.refill()
69                 
70         def updateState(self):
71                 if len(self.list) > 0:
72                         if self["timerlist"].getCurrent()[0].disabled:
73                                 self["key_yellow"].setText(_("Enable"))
74                         else:
75                                 self["key_yellow"].setText(_("Disable"))
76                         self["key_yellow"].instance.invalidate()
77
78         def fillTimerList(self):
79                 del self.list[:]
80                 
81                 for timer in self.session.nav.RecordTimer.timer_list:
82                         self.list.append(TimerEntryComponent(timer, processed=False))
83                 
84                 for timer in self.session.nav.RecordTimer.processed_timers:
85                         self.list.append(TimerEntryComponent(timer, processed=True))
86
87         def showLog(self):
88                 self.session.openWithCallback(self.finishedEdit, TimerLog, self["timerlist"].getCurrent()[0])
89
90         def openEdit(self):
91                 self.session.openWithCallback(self.finishedEdit, TimerEntry, self["timerlist"].getCurrent()[0])
92                 #self.session.open(TimerEdit, self["timerlist"].getCurrent()[0])
93                 
94         def cleanupQuestion(self):
95                 self.session.openWithCallback(self.cleanupTimer, MessageBox, _("Really delete done timers?"))
96         
97         def cleanupTimer(self, delete):
98                 if delete:
99                         self.session.nav.RecordTimer.cleanup()
100                         self.refill()
101                 
102         def removeTimer(self):
103                 list = self["timerlist"]
104                 timer = list.getCurrent()[0]
105                 currentIndex = list.getCurrentIndex()
106                 self.session.nav.RecordTimer.removeEntry(timer)
107                 list.moveDown()
108                 if list.getCurrentIndex() == currentIndex:
109                         currentIndex -= 1
110                 self.refill()
111                 list.moveToIndex(currentIndex)
112         
113         def refill(self):
114                 self.fillTimerList()
115                 self["timerlist"].invalidate()
116         
117         def addCurrentTimer(self):
118                 event = None
119                 service = self.session.nav.getCurrentService()
120                 if service is not None:
121                         info = service.info()
122                         if info is not None:
123                                 event = info.getEvent(0)
124
125                 # FIXME only works if already playing a service
126                 serviceref = ServiceReference(self.session.nav.getCurrentlyPlayingServiceReference())
127                 
128                 if event is None:       
129                         data = (int(time()), int(time() + 60), "unknown event", "", None)
130                 else:
131                         data = parseEvent(event)
132
133                 self.addTimer(RecordTimerEntry(serviceref, *data))
134                 
135         def addTimer(self, timer):
136                 self.session.openWithCallback(self.finishedAdd, TimerEntry, timer)
137                 
138         def finishedEdit(self, answer):
139                 print "finished edit"
140                 
141                 if answer[0]:
142                         print "Edited timer"
143                         timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list, answer[1])
144                         if not timersanitycheck.check():
145                                 print "Sanity check failed"
146                         else:
147                                 print "Sanity check passed"
148                         self.session.nav.RecordTimer.timeChanged(answer[1])
149                         self.fillTimerList()
150                 else:
151                         print "Timeredit aborted"
152
153         def finishedAdd(self, answer):
154                 print "finished add"
155                 if answer[0]:
156                         timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list, answer[1])
157                         if not timersanitycheck.check():
158                                 print "Sanity check failed"
159                                 self.session.openWithCallback(self.finishSanityCorrection, TimerSanityConflict, timersanitycheck.getSimulTimerList())
160                         else:
161                                 print "Sanity check passed"
162                                 entry = answer[1]
163                                 self.session.nav.RecordTimer.record(entry)
164                                 self.fillTimerList()
165                 else:
166                         print "Timeredit aborted"               
167
168         def finishSanityCorrection(self, answer):
169                 self.finishedAdd(answer)
170
171         def leave(self):
172                 self.session.nav.RecordTimer.saveTimer()
173                 self.session.nav.RecordTimer.on_state_change.remove(self.onStateChange)
174                 self.close()
175
176         def onStateChange(self, entry):
177                 self.refill()
178                 
179 class TimerSanityConflict(Screen):
180         def __init__(self, session, timer):
181                 Screen.__init__(self, session)
182                 self.timer = timer
183                 print timer
184                         
185                 self["timer1"] = TimerList(self.getTimerList(timer[0]))
186                 if len(timer) > 1:
187                         self["timer2"] = TimerList(self.getTimerList(timer[1]))
188                 else:
189                         self["timer2"] = Button("No conflict")
190                 
191                 self.list = []
192                 count = 0
193                 for x in timer:
194                         if count != 0:
195                                 self.list.append((_("Conflicting timer") + " " + str(count), x))
196                         count += 1
197
198                 self["list"] = MenuList(self.list)
199                 
200                 self["key_red"] = Button("Edit")
201                 self["key_green"] = Button("Disable")
202                 self["key_yellow"] = Button("Edit")
203                 self["key_blue"] = Button("Disable")
204
205                 self["actions"] = ActionMap(["OkCancelActions", "DirectionActions", "ShortcutActions", "TimerEditActions"], 
206                         {
207                                 "ok": self.close,
208                                 #"cancel": self.leave,
209                                 "red": self.editTimer1,
210                                 "green": self.disableTimer1,
211 #                               "yellow": self.editTimer2,
212 #                               "blue": self.disableTimer2,
213                                 #"log": self.showLog,
214                                 #"left": self.left,
215                                 #"right": self.right,
216                                 "up": self.up,
217                                 "down": self.down
218                         }, -1)
219
220         def getTimerList(self, timer):
221                 return [TimerEntryComponent(timer, processed=False)]
222
223         def editTimer1(self):
224                 self.session.openWithCallback(self.finishedEdit, TimerEntry, self["timer1"].getCurrent()[0])
225
226         def disableTimer1(self):
227                 self.timer[0].disabled = True
228                 self.finishedEdit((True, self.timer[0]))
229
230         def finishedEdit(self, answer):
231                 self.close((True, self.timer[0]))
232
233         def up(self):
234                 self["list"].instance.moveSelection(self["list"].instance.moveUp)
235                 self["timer2"].l.setList(self.getTimerList(self["list"].getCurrent()[1]))
236                 
237         def down(self):
238                 self["list"].instance.moveSelection(self["list"].instance.moveDown)
239                 self["timer2"].l.setList(self.getTimerList(self["list"].getCurrent()[1]))
240                         
241