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