1 from Components.ActionMap import ActionMap
2 from Components.Button import Button
3 from Components.config import config
4 from Components.MenuList import MenuList
5 from Components.TimerList import TimerList
6 from Components.TimerSanityCheck import TimerSanityCheck
7 from Components.UsageConfig import preferredTimerPath
8 from RecordTimer import RecordTimerEntry, parseEvent, AFTEREVENT
9 from Screen import Screen
10 from Screens.ChoiceBox import ChoiceBox
11 from Screens.MessageBox import MessageBox
12 from ServiceReference import ServiceReference
13 from TimerEntry import TimerEntry, TimerLog
14 from Tools.BoundFunction import boundFunction
16 from timer import TimerEntry as RealTimerEntry
18 class TimerEditList(Screen):
25 def __init__(self, session):
26 Screen.__init__(self, session)
32 print "EMPTY:",self.EMPTY
33 print "ENABLE:",self.ENABLE
34 print "DISABLE:",self.DISABLE
35 print "CLEANUP:",self.CLEANUP
36 print "DELETE:",self.DELETE
38 self["timerlist"] = TimerList(list)
40 self.key_red_choice = self.EMPTY
41 self.key_yellow_choice = self.EMPTY
42 self.key_blue_choice = self.EMPTY
44 self["key_red"] = Button(" ")
45 self["key_green"] = Button(_("Add"))
46 self["key_yellow"] = Button(" ")
47 self["key_blue"] = Button(" ")
49 print "key_red_choice:",self.key_red_choice
51 self["actions"] = ActionMap(["OkCancelActions", "DirectionActions", "ShortcutActions", "TimerEditActions"],
55 "green": self.addCurrentTimer,
62 self.session.nav.RecordTimer.on_state_change.append(self.onStateChange)
63 self.onShown.append(self.updateState)
66 self["timerlist"].instance.moveSelection(self["timerlist"].instance.moveUp)
70 self["timerlist"].instance.moveSelection(self["timerlist"].instance.moveDown)
74 self["timerlist"].instance.moveSelection(self["timerlist"].instance.pageUp)
78 self["timerlist"].instance.moveSelection(self["timerlist"].instance.pageDown)
81 def toggleDisabledState(self):
82 cur=self["timerlist"].getCurrent()
86 print "try to ENABLE timer"
88 timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list, cur)
89 if not timersanitycheck.check():
91 print "Sanity check failed"
92 self.session.openWithCallback(self.finishedEdit, TimerSanityConflict, timersanitycheck.getSimulTimerList())
94 print "Sanity check passed"
95 if timersanitycheck.doubleCheck():
101 (_("Stop current event but not coming events"), "stoponlycurrent"),
102 (_("Stop current event and disable coming events"), "stopall"),
103 (_("Don't stop current event but disable coming events"), "stoponlycoming")
105 self.session.openWithCallback(boundFunction(self.runningEventCallback, t), ChoiceBox, title=_("Repeating event currently recording... What do you want to do?"), list = list)
108 self.session.nav.RecordTimer.timeChanged(t)
112 def runningEventCallback(self, t, result):
113 if result is not None:
114 if result[1] == "stoponlycurrent" or result[1] == "stopall":
116 t.processRepeated(findRunningEvent = False)
117 self.session.nav.RecordTimer.doActivate(t)
118 if result[1] == "stoponlycoming" or result[1] == "stopall":
120 self.session.nav.RecordTimer.timeChanged(t)
124 def removeAction(self, descr):
125 actions = self["actions"].actions
129 def updateState(self):
130 cur = self["timerlist"].getCurrent()
132 if self.key_red_choice != self.DELETE:
133 self["actions"].actions.update({"red":self.removeTimerQuestion})
134 self["key_red"].setText(_("Delete"))
135 self.key_red_choice = self.DELETE
137 if cur.disabled and (self.key_yellow_choice != self.ENABLE):
138 self["actions"].actions.update({"yellow":self.toggleDisabledState})
139 self["key_yellow"].setText(_("Enable"))
140 self.key_yellow_choice = self.ENABLE
141 elif cur.isRunning() and not cur.repeated and (self.key_yellow_choice != self.EMPTY):
142 self.removeAction("yellow")
143 self["key_yellow"].setText(" ")
144 self.key_yellow_choice = self.EMPTY
145 elif ((not cur.isRunning())or cur.repeated ) and (not cur.disabled) and (self.key_yellow_choice != self.DISABLE):
146 self["actions"].actions.update({"yellow":self.toggleDisabledState})
147 self["key_yellow"].setText(_("Disable"))
148 self.key_yellow_choice = self.DISABLE
150 if self.key_red_choice != self.EMPTY:
151 self.removeAction("red")
152 self["key_red"].setText(" ")
153 self.key_red_choice = self.EMPTY
154 if self.key_yellow_choice != self.EMPTY:
155 self.removeAction("yellow")
156 self["key_yellow"].setText(" ")
157 self.key_yellow_choice = self.EMPTY
161 if (not x[0].disabled) and (x[1] == True):
166 if showCleanup and (self.key_blue_choice != self.CLEANUP):
167 self["actions"].actions.update({"blue":self.cleanupQuestion})
168 self["key_blue"].setText(_("Cleanup"))
169 self.key_blue_choice = self.CLEANUP
170 elif (not showCleanup) and (self.key_blue_choice != self.EMPTY):
171 self.removeAction("blue")
172 self["key_blue"].setText(" ")
173 self.key_blue_choice = self.EMPTY
175 def fillTimerList(self):
176 #helper function to move finished timers to end of list
177 def eol_compare(x, y):
178 if x[0].state != y[0].state and x[0].state == RealTimerEntry.StateEnded or y[0].state == RealTimerEntry.StateEnded:
179 return cmp(x[0].state, y[0].state)
180 return cmp(x[0].begin, x[1].begin)
184 list.extend([(timer, False) for timer in self.session.nav.RecordTimer.timer_list])
185 list.extend([(timer, True) for timer in self.session.nav.RecordTimer.processed_timers])
186 if config.usage.timerlist_finished_timer_position.index: #end of list
187 list.sort(cmp = eol_compare)
189 list.sort(key = lambda x: x[0].begin)
192 cur=self["timerlist"].getCurrent()
194 self.session.openWithCallback(self.finishedEdit, TimerLog, cur)
197 cur=self["timerlist"].getCurrent()
199 self.session.openWithCallback(self.finishedEdit, TimerEntry, cur)
201 def cleanupQuestion(self):
202 self.session.openWithCallback(self.cleanupTimer, MessageBox, _("Really delete done timers?"))
204 def cleanupTimer(self, delete):
206 self.session.nav.RecordTimer.cleanup()
210 def removeTimerQuestion(self):
211 cur = self["timerlist"].getCurrent()
215 self.session.openWithCallback(self.removeTimer, MessageBox, _("Do you really want to delete %s?") % (cur.name))
217 def removeTimer(self, result):
220 list = self["timerlist"]
221 cur = list.getCurrent()
224 timer.afterEvent = AFTEREVENT.NONE
225 self.session.nav.RecordTimer.removeEntry(timer)
231 oldsize = len(self.list)
233 lst = self["timerlist"]
234 newsize = len(self.list)
235 if oldsize and oldsize != newsize:
236 idx = lst.getCurrentIndex()
237 lst.entryRemoved(idx)
241 def addCurrentTimer(self):
243 service = self.session.nav.getCurrentService()
244 if service is not None:
245 info = service.info()
247 event = info.getEvent(0)
249 # FIXME only works if already playing a service
250 serviceref = ServiceReference(self.session.nav.getCurrentlyPlayingServiceReference())
253 data = (int(time()), int(time() + 60), "", "", None)
255 data = parseEvent(event, description = False)
257 self.addTimer(RecordTimerEntry(serviceref, checkOldTimers = True, dirname = preferredTimerPath(), *data))
259 def addTimer(self, timer):
260 self.session.openWithCallback(self.finishedAdd, TimerEntry, timer)
263 def finishedEdit(self, answer):
264 print "finished edit"
269 timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list, entry)
271 if not timersanitycheck.check():
272 simulTimerList = timersanitycheck.getSimulTimerList()
273 if simulTimerList is not None:
274 for x in simulTimerList:
275 if x.setAutoincreaseEnd(entry):
276 self.session.nav.RecordTimer.timeChanged(x)
277 if not timersanitycheck.check():
278 simulTimerList = timersanitycheck.getSimulTimerList()
279 if simulTimerList is not None:
280 self.session.openWithCallback(self.finishedEdit, TimerSanityConflict, timersanitycheck.getSimulTimerList())
286 print "Sanity check passed"
287 self.session.nav.RecordTimer.timeChanged(entry)
292 print "Timeredit aborted"
294 def finishedAdd(self, answer):
298 simulTimerList = self.session.nav.RecordTimer.record(entry)
299 if simulTimerList is not None:
300 for x in simulTimerList:
301 if x.setAutoincreaseEnd(entry):
302 self.session.nav.RecordTimer.timeChanged(x)
303 simulTimerList = self.session.nav.RecordTimer.record(entry)
304 if simulTimerList is not None:
305 self.session.openWithCallback(self.finishSanityCorrection, TimerSanityConflict, simulTimerList)
309 print "Timeredit aborted"
311 def finishSanityCorrection(self, answer):
312 self.finishedAdd(answer)
315 self.session.nav.RecordTimer.on_state_change.remove(self.onStateChange)
318 def onStateChange(self, entry):
322 class TimerSanityConflict(Screen):
328 def __init__(self, session, timer):
329 Screen.__init__(self, session)
331 print "TimerSanityConflict"
333 self["timer1"] = TimerList(self.getTimerList(timer[0]))
339 self.list.append((_("Conflicting timer") + " " + str(count), x))
340 self.list2.append((timer[count], False))
343 self.list.append((_("Channel not in services list")))
345 self["list"] = MenuList(self.list)
346 self["timer2"] = TimerList(self.list2)
348 self["key_red"] = Button("Edit")
349 self["key_green"] = Button(" ")
350 self["key_yellow"] = Button(" ")
351 self["key_blue"] = Button(" ")
353 self.key_green_choice = self.EMPTY
354 self.key_yellow_choice = self.EMPTY
355 self.key_blue_choice = self.EMPTY
357 self["actions"] = ActionMap(["OkCancelActions", "DirectionActions", "ShortcutActions", "TimerEditActions"],
360 "cancel": self.leave_cancel,
361 "red": self.editTimer1,
365 self.onShown.append(self.updateState)
367 def getTimerList(self, timer):
368 return [(timer, False)]
370 def editTimer1(self):
371 self.session.openWithCallback(self.finishedEdit, TimerEntry, self["timer1"].getCurrent())
373 def toggleTimer1(self):
374 if self.timer[0].disabled:
375 self.timer[0].disabled = False
377 if not self.timer[0].isRunning():
378 self.timer[0].disabled = True
379 self.finishedEdit((True, self.timer[0]))
381 def editTimer2(self):
382 self.session.openWithCallback(self.finishedEdit, TimerEntry, self["timer2"].getCurrent())
384 def toggleTimer2(self):
385 x = self["list"].getSelectedIndex() + 1 # the first is the new timer so we do +1 here
386 if self.timer[x].disabled:
387 self.timer[x].disabled = False
388 elif not self.timer[x].isRunning():
389 self.timer[x].disabled = True
390 self.finishedEdit((True, self.timer[0]))
392 def finishedEdit(self, answer):
396 self.close((True, self.timer[0]))
398 def leave_cancel(self):
399 self.close((False, self.timer[0]))
402 self["list"].instance.moveSelection(self["list"].instance.moveUp)
403 self["timer2"].moveToIndex(self["list"].getSelectedIndex())
406 self["list"].instance.moveSelection(self["list"].instance.moveDown)
407 self["timer2"].moveToIndex(self["list"].getSelectedIndex())
409 def removeAction(self, descr):
410 actions = self["actions"].actions
414 def updateState(self):
415 if self.timer[0] is not None:
416 if self.timer[0].disabled and self.key_green_choice != self.ENABLE:
417 self["actions"].actions.update({"green":self.toggleTimer1})
418 self["key_green"].setText(_("Enable"))
419 self.key_green_choice = self.ENABLE
420 elif self.timer[0].isRunning() and not self.timer[0].repeated and self.key_green_choice != self.EMPTY:
421 self.removeAction("green")
422 self["key_green"].setText(" ")
423 self.key_green_choice = self.EMPTY
424 elif (not self.timer[0].isRunning() or self.timer[0].repeated ) and self.key_green_choice != self.DISABLE:
425 self["actions"].actions.update({"green":self.toggleTimer1})
426 self["key_green"].setText(_("Disable"))
427 self.key_green_choice = self.DISABLE
429 if len(self.timer) > 1:
430 x = self["list"].getSelectedIndex()
431 if self.timer[x] is not None:
432 if self.key_yellow_choice == self.EMPTY:
433 self["actions"].actions.update({"yellow":self.editTimer2})
434 self["key_yellow"].setText(_("Edit"))
435 self.key_yellow_choice = self.EDIT
436 if self.timer[x].disabled and self.key_blue_choice != self.ENABLE:
437 self["actions"].actions.update({"blue":self.toggleTimer2})
438 self["key_blue"].setText(_("Enable"))
439 self.key_blue_choice = self.ENABLE
440 elif self.timer[x].isRunning() and not self.timer[x].repeated and self.key_blue_choice != self.EMPTY:
441 self.removeAction("blue")
442 self["key_blue"].setText(" ")
443 self.key_blue_choice = self.EMPTY
444 elif (not self.timer[x].isRunning() or self.timer[x].repeated ) and self.key_blue_choice != self.DISABLE:
445 self["actions"].actions.update({"blue":self.toggleTimer2})
446 self["key_blue"].setText(_("Disable"))
447 self.key_blue_choice = self.DISABLE
449 #FIXME.... this doesnt hide the buttons self.... just the text
450 if self.key_yellow_choice != self.EMPTY:
451 self.removeAction("yellow")
452 self["key_yellow"].setText(" ")
453 self.key_yellow_choice = self.EMPTY
454 if self.key_blue_choice != self.EMPTY:
455 self.removeAction("blue")
456 self["key_blue"].setText(" ")
457 self.key_blue_choice = self.EMPTY