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