fix remove last timer
[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.session.nav.RecordTimer.timeChanged(self["timerlist"].getCurrent()[0])
68                 self.updateState()
69                 self.refill()
70                 
71         def updateState(self):
72                 if len(self.list) > 0:
73                         if self["timerlist"].getCurrent()[0].disabled:
74                                 self["key_yellow"].setText(_("Enable"))
75                         else:
76                                 self["key_yellow"].setText(_("Disable"))
77                         self["key_yellow"].instance.invalidate()
78
79         def fillTimerList(self):
80                 del self.list[:]
81                 
82                 for timer in self.session.nav.RecordTimer.timer_list:
83                         self.list.append(TimerEntryComponent(timer, processed=False))
84                 
85                 for timer in self.session.nav.RecordTimer.processed_timers:
86                         self.list.append(TimerEntryComponent(timer, processed=True))
87                 self.list.sort(cmp = lambda x, y: x[0].begin < y[0].begin)
88
89         def showLog(self):
90                 self.session.openWithCallback(self.finishedEdit, TimerLog, self["timerlist"].getCurrent()[0])
91
92         def openEdit(self):
93                 self.session.openWithCallback(self.finishedEdit, TimerEntry, self["timerlist"].getCurrent()[0])
94                 #self.session.open(TimerEdit, self["timerlist"].getCurrent()[0])
95                 
96         def cleanupQuestion(self):
97                 self.session.openWithCallback(self.cleanupTimer, MessageBox, _("Really delete done timers?"))
98         
99         def cleanupTimer(self, delete):
100                 if delete:
101                         self.session.nav.RecordTimer.cleanup()
102                         self.refill()
103                 
104         def removeTimer(self):
105                 list = self["timerlist"]
106                 timer = list.getCurrent()[0]
107                 self.session.nav.RecordTimer.removeEntry(timer)
108                 self.refill()
109         
110         def refill(self):
111                 self.fillTimerList()
112                 self["timerlist"].invalidate()
113         
114         def addCurrentTimer(self):
115                 event = None
116                 service = self.session.nav.getCurrentService()
117                 if service is not None:
118                         info = service.info()
119                         if info is not None:
120                                 event = info.getEvent(0)
121
122                 # FIXME only works if already playing a service
123                 serviceref = ServiceReference(self.session.nav.getCurrentlyPlayingServiceReference())
124                 
125                 if event is None:       
126                         data = (int(time()), int(time() + 60), "unknown event", "", None)
127                 else:
128                         data = parseEvent(event)
129
130                 self.addTimer(RecordTimerEntry(serviceref, *data))
131                 
132         def addTimer(self, timer):
133                 self.session.openWithCallback(self.finishedAdd, TimerEntry, timer)
134                 
135         def finishedEdit(self, answer):
136                 print "finished edit"
137                 
138                 if answer[0]:
139                         print "Edited timer"
140                         timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list, answer[1])
141                         if not timersanitycheck.check():
142                                 print "Sanity check failed"
143                         else:
144                                 print "Sanity check passed"
145                         self.session.nav.RecordTimer.timeChanged(answer[1])
146                         self.fillTimerList()
147                 else:
148                         print "Timeredit aborted"
149
150         def finishedAdd(self, answer):
151                 print "finished add"
152                 if answer[0]:
153                         timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list, answer[1])
154                         if not timersanitycheck.check():
155                                 print "Sanity check failed"
156                                 self.session.openWithCallback(self.finishSanityCorrection, TimerSanityConflict, timersanitycheck.getSimulTimerList())
157                         else:
158                                 print "Sanity check passed"
159                                 entry = answer[1]
160                                 self.session.nav.RecordTimer.record(entry)
161                                 self.fillTimerList()
162                 else:
163                         print "Timeredit aborted"               
164
165         def finishSanityCorrection(self, answer):
166                 self.finishedAdd(answer)
167
168         def leave(self):
169                 self.session.nav.RecordTimer.saveTimer()
170                 self.session.nav.RecordTimer.on_state_change.remove(self.onStateChange)
171                 self.close()
172
173         def onStateChange(self, entry):
174                 self.refill()
175                 
176 class TimerSanityConflict(Screen):
177         def __init__(self, session, timer):
178                 Screen.__init__(self, session)
179                 self.timer = timer
180                 print "TimerSanityConflict", timer
181                         
182                 self["timer1"] = TimerList(self.getTimerList(timer[0]))
183                 if len(timer) > 1:
184                         self["timer2"] = TimerList(self.getTimerList(timer[1]))
185                 else:
186                         self["timer2"] = Button("No conflict")
187                 
188                 self.list = []
189                 count = 0
190                 for x in timer:
191                         if count != 0:
192                                 self.list.append((_("Conflicting timer") + " " + str(count), x))
193                         count += 1
194
195                 self["list"] = MenuList(self.list)
196                 
197                 self["key_red"] = Button("Edit")
198                 self["key_green"] = Button("Disable")
199                 self["key_yellow"] = Button("Edit")
200                 self["key_blue"] = Button("Disable")
201
202                 self["actions"] = ActionMap(["OkCancelActions", "DirectionActions", "ShortcutActions", "TimerEditActions"], 
203                         {
204                                 "ok": self.close,
205                                 #"cancel": self.leave,
206                                 "red": self.editTimer1,
207                                 "green": self.disableTimer1,
208 #                               "yellow": self.editTimer2,
209 #                               "blue": self.disableTimer2,
210                                 #"log": self.showLog,
211                                 #"left": self.left,
212                                 #"right": self.right,
213                                 "up": self.up,
214                                 "down": self.down
215                         }, -1)
216
217         def getTimerList(self, timer):
218                 return [TimerEntryComponent(timer, processed=False)]
219
220         def editTimer1(self):
221                 self.session.openWithCallback(self.finishedEdit, TimerEntry, self["timer1"].getCurrent()[0])
222
223         def disableTimer1(self):
224                 self.timer[0].disabled = True
225                 self.finishedEdit((True, self.timer[0]))
226
227         def finishedEdit(self, answer):
228                 self.close((True, self.timer[0]))
229
230         def up(self):
231                 self["list"].instance.moveSelection(self["list"].instance.moveUp)
232                 self["timer2"].l.setList(self.getTimerList(self["list"].getCurrent()[1]))
233                 
234         def down(self):
235                 self["list"].instance.moveSelection(self["list"].instance.moveDown)
236                 self["timer2"].l.setList(self.getTimerList(self["list"].getCurrent()[1]))
237                         
238