59e2bd03b3b7b5c7954af8a56fafe3b6144eecbc
[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                         self.refill()
218                         self.updateState()
219
220         
221         def refill(self):
222                 oldsize = len(self.list)
223                 self.fillTimerList()
224                 lst = self["timerlist"]
225                 newsize = len(self.list)
226                 if oldsize and oldsize != newsize:
227                         idx = lst.getCurrentIndex()
228                         lst.entryRemoved(idx)
229                 else:
230                         lst.invalidate()
231         
232         def addCurrentTimer(self):
233                 event = None
234                 service = self.session.nav.getCurrentService()
235                 if service is not None:
236                         info = service.info()
237                         if info is not None:
238                                 event = info.getEvent(0)
239
240                 # FIXME only works if already playing a service
241                 serviceref = ServiceReference(self.session.nav.getCurrentlyPlayingServiceReference())
242                 
243                 if event is None:       
244                         data = (int(time()), int(time() + 60), "", "", None)
245                 else:
246                         data = parseEvent(event, description = False)
247
248                 self.addTimer(RecordTimerEntry(serviceref, checkOldTimers = True, dirname = config.movielist.last_timer_videodir.value, *data))
249                 
250         def addTimer(self, timer):
251                 self.session.openWithCallback(self.finishedAdd, TimerEntry, timer)
252                         
253                 
254         def finishedEdit(self, answer):
255                 print "finished edit"
256                 
257                 if answer[0]:
258                         print "Edited timer"
259                         entry = answer[1]
260                         timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list, entry)
261                         if not timersanitycheck.check():
262                                 simulTimerList = timersanitycheck.getSimulTimerList()
263                                 if (len(simulTimerList) == 2) and (simulTimerList[1].dontSave) and (simulTimerList[1].autoincrease):
264                                         simulTimerList[1].end = entry.begin - 30
265                                         self.session.nav.RecordTimer.timeChanged(simulTimerList[1])
266                                         self.session.nav.RecordTimer.timeChanged(entry)
267                                 else:
268                                         print "Sanity check failed"
269                                         self.session.openWithCallback(self.finishedEdit, TimerSanityConflict, timersanitycheck.getSimulTimerList())
270                         else:
271                                 print "Sanity check passed"
272                                 if not timersanitycheck.doubleCheck():
273                                         self.session.nav.RecordTimer.timeChanged(entry)
274                         self.fillTimerList()
275                         self.updateState()
276                 else:
277                         print "Timeredit aborted"
278
279         def finishedAdd(self, answer):
280                 print "finished add"
281                 if answer[0]:
282                         entry = answer[1]
283                         simulTimerList = self.session.nav.RecordTimer.record(entry)
284                         if simulTimerList is not None:
285                                 if (len(simulTimerList) == 2) and (simulTimerList[1].dontSave) and (simulTimerList[1].autoincrease):
286                                         simulTimerList[1].end = entry.begin - 30
287                                         self.session.nav.RecordTimer.timeChanged(simulTimerList[1])
288                                         self.session.nav.RecordTimer.record(entry)
289                                 else:
290                                         self.session.openWithCallback(self.finishSanityCorrection, TimerSanityConflict, simulTimerList)
291                         self.fillTimerList()
292                         self.updateState()
293                 else:
294                         print "Timeredit aborted"
295
296         def finishSanityCorrection(self, answer):
297                 self.finishedAdd(answer)
298
299         def leave(self):
300                 self.session.nav.RecordTimer.on_state_change.remove(self.onStateChange)
301                 self.close()
302
303         def onStateChange(self, entry):
304                 self.refill()
305                 self.updateState()
306
307 class TimerSanityConflict(Screen):
308         EMPTY = 0
309         ENABLE = 1
310         DISABLE = 2
311         EDIT = 3
312         
313         def __init__(self, session, timer):
314                 Screen.__init__(self, session)
315                 self.timer = timer
316                 print "TimerSanityConflict"
317                         
318                 self["timer1"] = TimerList(self.getTimerList(timer[0]))
319                 self.list = []
320                 self.list2 = []
321                 count = 0
322                 for x in timer:
323                         if count != 0:
324                                 self.list.append((_("Conflicting timer") + " " + str(count), x))
325                                 self.list2.append((timer[count], False))
326                         count += 1
327                 if count == 1:
328                         self.list.append((_("Channel not in services list")))
329
330                 self["list"] = MenuList(self.list)
331                 self["timer2"] = TimerList(self.list2)
332
333                 self["key_red"] = Button("Edit")
334                 self["key_green"] = Button(" ")
335                 self["key_yellow"] = Button(" ")
336                 self["key_blue"] = Button(" ")
337
338                 self.key_green_choice = self.EMPTY
339                 self.key_yellow_choice = self.EMPTY
340                 self.key_blue_choice = self.EMPTY
341
342                 self["actions"] = ActionMap(["OkCancelActions", "DirectionActions", "ShortcutActions", "TimerEditActions"], 
343                         {
344                                 "ok": self.leave_ok,
345                                 "cancel": self.leave_cancel,
346                                 "red": self.editTimer1,
347                                 "up": self.up,
348                                 "down": self.down
349                         }, -1)
350                 self.onShown.append(self.updateState)
351
352         def getTimerList(self, timer):
353                 return [(timer, False)]
354
355         def editTimer1(self):
356                 self.session.openWithCallback(self.finishedEdit, TimerEntry, self["timer1"].getCurrent())
357
358         def toggleTimer1(self):
359                 if self.timer[0].disabled:
360                         self.timer[0].disabled = False
361                 else:
362                         if not self.timer[0].isRunning():
363                                 self.timer[0].disabled = True
364                 self.finishedEdit((True, self.timer[0]))
365         
366         def editTimer2(self):
367                 self.session.openWithCallback(self.finishedEdit, TimerEntry, self["timer2"].getCurrent())
368
369         def toggleTimer2(self):
370                 x = self["list"].getSelectedIndex() + 1 # the first is the new timer so we do +1 here
371                 if self.timer[x].disabled:
372                         self.timer[x].disabled = False
373                 elif not self.timer[x].isRunning():
374                                 self.timer[x].disabled = True
375                 self.finishedEdit((True, self.timer[0]))
376         
377         def finishedEdit(self, answer):
378                 self.leave_ok()
379         
380         def leave_ok(self):
381                 self.close((True, self.timer[0]))
382         
383         def leave_cancel(self):
384                 self.close((False, self.timer[0]))
385
386         def up(self):
387                 self["list"].instance.moveSelection(self["list"].instance.moveUp)
388                 self["timer2"].moveToIndex(self["list"].getSelectedIndex())
389                 
390         def down(self):
391                 self["list"].instance.moveSelection(self["list"].instance.moveDown)
392                 self["timer2"].moveToIndex(self["list"].getSelectedIndex())
393
394         def removeAction(self, descr):
395                 actions = self["actions"].actions
396                 if descr in actions:
397                         del actions[descr]
398
399         def updateState(self):
400                 if self.timer[0] is not None:
401                         if self.timer[0].disabled and self.key_green_choice != self.ENABLE:
402                                 self["actions"].actions.update({"green":self.toggleTimer1})
403                                 self["key_green"].setText(_("Enable"))
404                                 self.key_green_choice = self.ENABLE
405                         elif self.timer[0].isRunning() and not timer[0].repeated and self.key_green_choice != self.EMPTY:
406                                 self.removeAction("green")
407                                 self["key_green"].setText(" ")
408                                 self.key_green_choice = self.EMPTY
409                         elif (not self.timer[0].isRunning() or self.timer[0].repeated ) and self.key_green_choice != self.DISABLE:
410                                 self["actions"].actions.update({"green":self.toggleTimer1})
411                                 self["key_green"].setText(_("Disable"))
412                                 self.key_green_choice = self.DISABLE
413                 
414                 if len(self.timer) > 1:
415                         x = self["list"].getSelectedIndex()
416                         if self.timer[x] is not None:
417                                 if self.key_yellow_choice == self.EMPTY:
418                                         self["actions"].actions.update({"yellow":self.editTimer2})
419                                         self["key_yellow"].setText(_("Edit"))
420                                         self.key_yellow_choice = self.EDIT
421                                 if self.timer[x].disabled and self.key_blue_choice != self.ENABLE:
422                                         self["actions"].actions.update({"blue":self.toggleTimer2})
423                                         self["key_blue"].setText(_("Enable"))
424                                         self.key_blue_choice = self.ENABLE
425                                 elif self.timer[x].isRunning() and not timer[x].repeated and self.key_blue_choice != self.EMPTY:
426                                         self.removeAction("blue")
427                                         self["key_blue"].setText(" ")
428                                         self.key_blue_choice = self.EMPTY
429                                 elif (not self.timer[x].isRunning() or self.timer[x].repeated ) and self.key_blue_choice != self.DISABLE:
430                                         self["actions"].actions.update({"blue":self.toggleTimer2})
431                                         self["key_blue"].setText(_("Disable"))
432                                         self.key_blue_choice = self.DISABLE
433                 else:
434 #FIXME.... this doesnt hide the buttons self.... just the text
435                         if self.key_yellow_choice != self.EMPTY:
436                                 self.removeAction("yellow")
437                                 self["key_yellow"].setText(" ")
438                                 self.key_yellow_choice = self.EMPTY
439                         if self.key_blue_choice != self.EMPTY:
440                                 self.removeAction("blue")
441                                 self["key_blue"].setText(" ")
442                                 self.key_blue_choice = self.EMPTY