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