18ab2b79839c94d5f2e9cd1a662d5d406a8d9446
[vuplus_dvbapp] / lib / python / Screens / TimerEdit.py
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 RecordTimer import RecordTimerEntry, parseEvent, AFTEREVENT
8 from Screen import Screen
9 from Screens.ChoiceBox import ChoiceBox
10 from Screens.MessageBox import MessageBox
11 from ServiceReference import ServiceReference
12 from TimerEntry import TimerEntry, TimerLog
13 from Tools.BoundFunction import boundFunction
14 from time import time
15
16 class TimerEditList(Screen):
17         EMPTY = 0
18         ENABLE = 1
19         DISABLE = 2
20         CLEANUP = 3
21         DELETE = 4
22         
23         def __init__(self, session):
24                 Screen.__init__(self, session)
25                 
26                 list = [ ]
27                 self.list = list
28                 self.fillTimerList()
29                 
30                 print "EMPTY:",self.EMPTY
31                 print "ENABLE:",self.ENABLE
32                 print "DISABLE:",self.DISABLE
33                 print "CLEANUP:",self.CLEANUP
34                 print "DELETE:",self.DELETE
35
36                 self["timerlist"] = TimerList(list)
37                 
38                 self.key_red_choice = self.EMPTY
39                 self.key_yellow_choice = self.EMPTY
40                 self.key_blue_choice = self.EMPTY
41                 
42                 self["key_red"] = Button(" ")
43                 self["key_green"] = Button(_("Add"))
44                 self["key_yellow"] = Button(" ")
45                 self["key_blue"] = Button(" ")
46
47                 print "key_red_choice:",self.key_red_choice
48
49                 self["actions"] = ActionMap(["OkCancelActions", "DirectionActions", "ShortcutActions", "TimerEditActions"], 
50                         {
51                                 "ok": self.openEdit,
52                                 "cancel": self.leave,
53                                 "green": self.addCurrentTimer,
54                                 "log": self.showLog,
55                                 "left": self.left,
56                                 "right": self.right,
57                                 "up": self.up,
58                                 "down": self.down
59                         }, -1)
60                 self.session.nav.RecordTimer.on_state_change.append(self.onStateChange)
61                 self.onShown.append(self.updateState)
62
63         def up(self):
64                 self["timerlist"].instance.moveSelection(self["timerlist"].instance.moveUp)
65                 self.updateState()
66                 
67         def down(self):
68                 self["timerlist"].instance.moveSelection(self["timerlist"].instance.moveDown)
69                 self.updateState()
70
71         def left(self):
72                 self["timerlist"].instance.moveSelection(self["timerlist"].instance.pageUp)
73                 self.updateState()
74                 
75         def right(self):
76                 self["timerlist"].instance.moveSelection(self["timerlist"].instance.pageDown)
77                 self.updateState()
78                 
79         def toggleDisabledState(self):
80                 cur=self["timerlist"].getCurrent()
81                 if cur:
82                         t = cur
83                         if t.disabled:
84                                 print "try to ENABLE timer"
85                                 t.enable()
86                                 timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list, cur)
87                                 if not timersanitycheck.check():
88                                         t.disable()
89                                         print "Sanity check failed"
90                                         self.session.openWithCallback(self.finishedEdit, TimerSanityConflict, timersanitycheck.getSimulTimerList())
91                                 else:
92                                         print "Sanity check passed"
93                                         if timersanitycheck.doubleCheck():
94                                                 t.disable()
95                         else:
96                                 if t.isRunning():
97                                         if t.repeated:
98                                                 list = []
99                                                 list.append((_("Stop current event but not coming events"), "stoponlycurrent"))
100                                                 list.append((_("Stop current event and disable coming events"), "stopall"))
101                                                 list.append((_("Don't stop current event but disable coming events"), "stoponlycoming"))
102                                                 self.session.openWithCallback(boundFunction(self.runningEventCallback, t), ChoiceBox, title=_("Repeating event currently recording... What do you want to do?"), list = list)
103                                 else:
104                                         t.disable()
105                         self.session.nav.RecordTimer.timeChanged(t)
106                         self.refill()
107                         self.updateState()
108
109         def runningEventCallback(self, t, result):
110                 if result is not None:
111                         if result[1] == "stoponlycurrent" or result[1] == "stopall":
112                                 t.enable()
113                                 t.processRepeated(findRunningEvent = False)
114                                 self.session.nav.RecordTimer.doActivate(t)
115                         if result[1] == "stoponlycoming" or result[1] == "stopall":
116                                 t.disable()
117                         self.session.nav.RecordTimer.timeChanged(t)
118                         self.refill()
119                         self.updateState()
120
121         def removeAction(self, descr):
122                 actions = self["actions"].actions
123                 if descr in actions:
124                         del actions[descr]
125
126         def updateState(self):
127                 cur = self["timerlist"].getCurrent()
128                 if cur:
129                         if self.key_red_choice != self.DELETE:
130                                 self["actions"].actions.update({"red":self.removeTimerQuestion})
131                                 self["key_red"].setText(_("Delete"))
132                                 self.key_red_choice = self.DELETE
133                         
134                         if cur.disabled and (self.key_yellow_choice != self.ENABLE):
135                                 self["actions"].actions.update({"yellow":self.toggleDisabledState})
136                                 self["key_yellow"].setText(_("Enable"))
137                                 self.key_yellow_choice = self.ENABLE
138                         elif cur.isRunning() and not cur.repeated and (self.key_yellow_choice != self.EMPTY):
139                                 self.removeAction("yellow")
140                                 self["key_yellow"].setText(" ")
141                                 self.key_yellow_choice = self.EMPTY
142                         elif ((not cur.isRunning())or cur.repeated ) and (not cur.disabled) and (self.key_yellow_choice != self.DISABLE):
143                                 self["actions"].actions.update({"yellow":self.toggleDisabledState})
144                                 self["key_yellow"].setText(_("Disable"))
145                                 self.key_yellow_choice = self.DISABLE
146                 else:
147                         if self.key_red_choice != self.EMPTY:
148                                 self.removeAction("red")
149                                 self["key_red"].setText(" ")
150                                 self.key_red_choice = self.EMPTY
151                         if self.key_yellow_choice != self.EMPTY:
152                                 self.removeAction("yellow")
153                                 self["key_yellow"].setText(" ")
154                                 self.key_yellow_choice = self.EMPTY
155                 
156                 showCleanup = True
157                 for x in self.list:
158                         if (not x[0].disabled) and (x[1] == True):
159                                 break
160                 else:
161                         showCleanup = False
162                 
163                 if showCleanup and (self.key_blue_choice != self.CLEANUP):
164                         self["actions"].actions.update({"blue":self.cleanupQuestion})
165                         self["key_blue"].setText(_("Cleanup"))
166                         self.key_blue_choice = self.CLEANUP
167                 elif (not showCleanup) and (self.key_blue_choice != self.EMPTY):
168                         self.removeAction("blue")
169                         self["key_blue"].setText(" ")
170                         self.key_blue_choice = self.EMPTY
171
172         def fillTimerList(self):
173                 del self.list[:]
174                 
175                 for timer in self.session.nav.RecordTimer.timer_list:
176                         self.list.append((timer, False))
177                 
178                 for timer in self.session.nav.RecordTimer.processed_timers:
179                         self.list.append((timer, True))
180                 self.list.sort(cmp = lambda x, y: x[0].begin < y[0].begin)
181
182         def showLog(self):
183                 cur=self["timerlist"].getCurrent()
184                 if cur:
185                         self.session.openWithCallback(self.finishedEdit, TimerLog, cur)
186
187         def openEdit(self):
188                 cur=self["timerlist"].getCurrent()
189                 if cur:
190                         self.session.openWithCallback(self.finishedEdit, TimerEntry, cur)
191
192         def cleanupQuestion(self):
193                 self.session.openWithCallback(self.cleanupTimer, MessageBox, _("Really delete done timers?"))
194         
195         def cleanupTimer(self, delete):
196                 if delete:
197                         self.session.nav.RecordTimer.cleanup()
198                         self.refill()
199                         self.updateState()
200
201         def removeTimerQuestion(self):
202                 cur = self["timerlist"].getCurrent()
203                 if not cur:
204                         return
205
206                 self.session.openWithCallback(self.removeTimer, MessageBox, _("Do you really want to delete %s?") % (cur.name))
207
208         def removeTimer(self, result):
209                 if not result:
210                         return
211                 list = self["timerlist"]
212                 cur = list.getCurrent()
213                 if cur:
214                         timer = cur
215                         timer.afterEvent = AFTEREVENT.NONE
216                         self.session.nav.RecordTimer.removeEntry(timer)
217                         if not timer.dontSave:
218                                 for timer in self.session.nav.RecordTimer.timer_list:
219                                         if timer.dontSave and timer.autoincrease:
220                                                 timer.end = timer.begin + (3600 * 24 * 356 * 1)
221                                                 self.session.nav.RecordTimer.timeChanged(timer)
222                                                 timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list,timer)
223                                                 if not timersanitycheck.check():
224                                                         tsc_list = timersanitycheck.getSimulTimerList()
225                                                         if len(tsc_list) > 1:
226                                                                 timer.end = tsc_list[1].begin - 30
227                                                                 self.session.nav.RecordTimer.timeChanged(timer)
228
229                         self.refill()
230                         self.updateState()
231
232         
233         def refill(self):
234                 oldsize = len(self.list)
235                 self.fillTimerList()
236                 lst = self["timerlist"]
237                 newsize = len(self.list)
238                 if oldsize and oldsize != newsize:
239                         idx = lst.getCurrentIndex()
240                         lst.entryRemoved(idx)
241                 else:
242                         lst.invalidate()
243         
244         def addCurrentTimer(self):
245                 event = None
246                 service = self.session.nav.getCurrentService()
247                 if service is not None:
248                         info = service.info()
249                         if info is not None:
250                                 event = info.getEvent(0)
251
252                 # FIXME only works if already playing a service
253                 serviceref = ServiceReference(self.session.nav.getCurrentlyPlayingServiceReference())
254                 
255                 if event is None:       
256                         data = (int(time()), int(time() + 60), "", "", None)
257                 else:
258                         data = parseEvent(event, description = False)
259
260                 self.addTimer(RecordTimerEntry(serviceref, checkOldTimers = True, dirname = config.movielist.last_timer_videodir.value, *data))
261                 
262         def addTimer(self, timer):
263                 self.session.openWithCallback(self.finishedAdd, TimerEntry, timer)
264                 
265         def finishedEdit(self, answer):
266                 print "finished edit"
267                 
268                 if answer[0]:
269                         print "Edited timer"
270                         entry = answer[1]
271                         timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list, entry)
272                         if not timersanitycheck.check():
273                                 simulTimerList = timersanitycheck.getSimulTimerList()
274                                 if (len(simulTimerList) == 2) and (simulTimerList[1].dontSave) and (simulTimerList[1].autoincrease):
275                                         simulTimerList[1].end = entry.begin - 30
276                                         self.session.nav.RecordTimer.timeChanged(simulTimerList[1])
277                                         self.session.nav.RecordTimer.timeChanged(entry)
278                                 else:
279                                         print "Sanity check failed"
280                                         self.session.openWithCallback(self.finishedEdit, TimerSanityConflict, timersanitycheck.getSimulTimerList())
281                         else:
282                                 print "Sanity check passed"
283                                 if not timersanitycheck.doubleCheck():
284                                         self.session.nav.RecordTimer.timeChanged(entry)
285                         self.fillTimerList()
286                         self.updateState()
287                 else:
288                         print "Timeredit aborted"
289
290         def finishedAdd(self, answer):
291                 print "finished add"
292                 if answer[0]:
293                         entry = answer[1]
294                         simulTimerList = self.session.nav.RecordTimer.record(entry)
295                         if simulTimerList is not None:
296                                 if (len(simulTimerList) == 2) and (simulTimerList[1].dontSave) and (simulTimerList[1].autoincrease):
297                                         simulTimerList[1].end = entry.begin - 30
298                                         self.session.nav.RecordTimer.timeChanged(simulTimerList[1])
299                                         self.session.nav.RecordTimer.record(entry)
300                                 else:
301                                         self.session.openWithCallback(self.finishSanityCorrection, TimerSanityConflict, simulTimerList)
302                         self.fillTimerList()
303                         self.updateState()
304                 else:
305                         print "Timeredit aborted"
306
307         def finishSanityCorrection(self, answer):
308                 self.finishedAdd(answer)
309
310         def leave(self):
311                 self.session.nav.RecordTimer.on_state_change.remove(self.onStateChange)
312                 self.close()
313
314         def onStateChange(self, entry):
315                 self.refill()
316                 self.updateState()
317
318 class TimerSanityConflict(Screen):
319         EMPTY = 0
320         ENABLE = 1
321         DISABLE = 2
322         EDIT = 3
323         
324         def __init__(self, session, timer):
325                 Screen.__init__(self, session)
326                 self.timer = timer
327                 print "TimerSanityConflict"
328                         
329                 self["timer1"] = TimerList(self.getTimerList(timer[0]))
330                 self.list = []
331                 self.list2 = []
332                 count = 0
333                 for x in timer:
334                         if count != 0:
335                                 self.list.append((_("Conflicting timer") + " " + str(count), x))
336                                 self.list2.append((timer[count], False))
337                         count += 1
338
339                 self["list"] = MenuList(self.list)
340                 self["timer2"] = TimerList(self.list2)
341
342                 self["key_red"] = Button("Edit")
343                 self["key_green"] = Button(" ")
344                 self["key_yellow"] = Button(" ")
345                 self["key_blue"] = Button(" ")
346
347                 self.key_green_choice = self.EMPTY
348                 self.key_yellow_choice = self.EMPTY
349                 self.key_blue_choice = self.EMPTY
350
351                 self["actions"] = ActionMap(["OkCancelActions", "DirectionActions", "ShortcutActions", "TimerEditActions"], 
352                         {
353                                 "ok": self.leave_ok,
354                                 "cancel": self.leave_cancel,
355                                 "red": self.editTimer1,
356                                 "up": self.up,
357                                 "down": self.down
358                         }, -1)
359                 self.onShown.append(self.updateState)
360
361         def getTimerList(self, timer):
362                 return [(timer, False)]
363
364         def editTimer1(self):
365                 self.session.openWithCallback(self.finishedEdit, TimerEntry, self["timer1"].getCurrent())
366
367         def toggleTimer1(self):
368                 if self.timer[0].disabled:
369                         self.timer[0].disabled = False
370                 else:
371                         if not self.timer[0].isRunning():
372                                 self.timer[0].disabled = True
373                 self.finishedEdit((True, self.timer[0]))
374         
375         def editTimer2(self):
376                 self.session.openWithCallback(self.finishedEdit, TimerEntry, self["timer2"].getCurrent())
377
378         def toggleTimer2(self):
379                 x = self["list"].getSelectedIndex() + 1 # the first is the new timer so we do +1 here
380                 if self.timer[x].disabled:
381                         self.timer[x].disabled = False
382                 elif not self.timer[x].isRunning():
383                                 self.timer[x].disabled = True
384                 self.finishedEdit((True, self.timer[0]))
385         
386         def finishedEdit(self, answer):
387                 self.leave_ok()
388         
389         def leave_ok(self):
390                 self.close((True, self.timer[0]))
391         
392         def leave_cancel(self):
393                 self.close((False, self.timer[0]))
394
395         def up(self):
396                 self["list"].instance.moveSelection(self["list"].instance.moveUp)
397                 self["timer2"].moveToIndex(self["list"].getSelectedIndex())
398                 
399         def down(self):
400                 self["list"].instance.moveSelection(self["list"].instance.moveDown)
401                 self["timer2"].moveToIndex(self["list"].getSelectedIndex())
402
403         def removeAction(self, descr):
404                 actions = self["actions"].actions
405                 if descr in actions:
406                         del actions[descr]
407
408         def updateState(self):
409                 if self.timer[0] is not None:
410                         if self.timer[0].disabled and self.key_green_choice != self.ENABLE:
411                                 self["actions"].actions.update({"green":self.toggleTimer1})
412                                 self["key_green"].setText(_("Enable"))
413                                 self.key_green_choice = self.ENABLE
414                         elif self.timer[0].isRunning() and not timer[0].repeated and self.key_green_choice != self.EMPTY:
415                                 self.removeAction("green")
416                                 self["key_green"].setText(" ")
417                                 self.key_green_choice = self.EMPTY
418                         elif (not self.timer[0].isRunning() or self.timer[0].repeated ) and self.key_green_choice != self.DISABLE:
419                                 self["actions"].actions.update({"green":self.toggleTimer1})
420                                 self["key_green"].setText(_("Disable"))
421                                 self.key_green_choice = self.DISABLE
422                 
423                 if len(self.timer) > 1:
424                         x = self["list"].getSelectedIndex()
425                         if self.timer[x] is not None:
426                                 if self.key_yellow_choice == self.EMPTY:
427                                         self["actions"].actions.update({"yellow":self.editTimer2})
428                                         self["key_yellow"].setText(_("Edit"))
429                                         self.key_yellow_choice = self.EDIT
430                                 if self.timer[x].disabled and self.key_blue_choice != self.ENABLE:
431                                         self["actions"].actions.update({"blue":self.toggleTimer2})
432                                         self["key_blue"].setText(_("Enable"))
433                                         self.key_blue_choice = self.ENABLE
434                                 elif self.timer[x].isRunning() and not timer[x].repeated and self.key_blue_choice != self.EMPTY:
435                                         self.removeAction("blue")
436                                         self["key_blue"].setText(" ")
437                                         self.key_blue_choice = self.EMPTY
438                                 elif (not self.timer[x].isRunning() or self.timer[x].repeated ) and self.key_blue_choice != self.DISABLE:
439                                         self["actions"].actions.update({"blue":self.toggleTimer2})
440                                         self["key_blue"].setText(_("Disable"))
441                                         self.key_blue_choice = self.DISABLE
442                 else:
443 #FIXME.... this doesnt hide the buttons self.... just the text
444                         if self.key_yellow_choice != self.EMPTY:
445                                 self.removeAction("yellow")
446                                 self["key_yellow"].setText(" ")
447                                 self.key_yellow_choice = self.EMPTY
448                         if self.key_blue_choice != self.EMPTY:
449                                 self.removeAction("blue")
450                                 self["key_blue"].setText(" ")
451                                 self.key_blue_choice = self.EMPTY