67c49cc5b03098a9c7d4b291aba2e0d2cdf01e46
[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                                                 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 updateState(self):
122                 cur = self["timerlist"].getCurrent()
123                 if cur:
124                         if self.key_red_choice != self.DELETE:
125                                 self["actions"].actions.update({"red":self.removeTimerQuestion})
126                                 self["key_red"].setText(_("Delete"))
127                                 self["key_red"].instance.invalidate()
128                                 self.key_red_choice = self.DELETE
129                         
130                         if cur.disabled and (self.key_yellow_choice != self.ENABLE):
131                                 self["actions"].actions.update({"yellow":self.toggleDisabledState})
132                                 self["key_yellow"].setText(_("Enable"))
133                                 self["key_yellow"].instance.invalidate()
134                                 self.key_yellow_choice = self.ENABLE
135                         elif cur.isRunning() and not cur.repeated and (self.key_yellow_choice != self.EMPTY):
136                                 del self["actions"].actions["yellow"]
137                                 self["key_yellow"].setText(" ")
138                                 self["key_yellow"].instance.invalidate()
139                                 self.key_yellow_choice = self.EMPTY
140                         elif ((not cur.isRunning())or cur.repeated ) and (not cur.disabled) and (self.key_yellow_choice != self.DISABLE):
141                                 self["actions"].actions.update({"yellow":self.toggleDisabledState})
142                                 self["key_yellow"].setText(_("Disable"))
143                                 self["key_yellow"].instance.invalidate()
144                                 self.key_yellow_choice = self.DISABLE
145                 else:
146                         if self.key_red_choice != self.EMPTY:
147                                 del self["actions"].actions["red"]
148                                 self["key_red"].setText(" ")
149                                 self["key_red"].instance.invalidate()
150                                 self.key_red_choice = self.EMPTY
151                         if self.key_yellow_choice != self.EMPTY:
152                                 del self["actions"].actions["yellow"]
153                                 self["key_yellow"].setText(" ")
154                                 self["key_yellow"].instance.invalidate()
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"].instance.invalidate()
168                         self.key_blue_choice = self.CLEANUP
169                 elif (not showCleanup) and (self.key_blue_choice != self.EMPTY):
170                         del self["actions"].actions["blue"]
171                         self["key_blue"].setText(" ")
172                         self["key_blue"].instance.invalidate()
173                         self.key_blue_choice = self.EMPTY
174
175
176         def fillTimerList(self):
177                 del self.list[:]
178                 
179                 for timer in self.session.nav.RecordTimer.timer_list:
180                         self.list.append((timer, False))
181                 
182                 for timer in self.session.nav.RecordTimer.processed_timers:
183                         self.list.append((timer, True))
184                 self.list.sort(cmp = lambda x, y: x[0].begin < y[0].begin)
185
186         def showLog(self):
187                 cur=self["timerlist"].getCurrent()
188                 if cur:
189                         self.session.openWithCallback(self.finishedEdit, TimerLog, cur)
190
191         def openEdit(self):
192                 cur=self["timerlist"].getCurrent()
193                 if cur:
194                         self.session.openWithCallback(self.finishedEdit, TimerEntry, cur)
195
196         def cleanupQuestion(self):
197                 self.session.openWithCallback(self.cleanupTimer, MessageBox, _("Really delete done timers?"))
198         
199         def cleanupTimer(self, delete):
200                 if delete:
201                         self.session.nav.RecordTimer.cleanup()
202                         self.refill()
203                         self.updateState()
204
205         def removeTimerQuestion(self):
206                 if not self["timerlist"].getCurrent():
207                         return
208                 self.session.openWithCallback(self.removeTimer, MessageBox, _("Really delete this timer?"))
209
210         def removeTimer(self, result):
211                 if not result:
212                         return
213                 list = self["timerlist"]
214                 cur = list.getCurrent()
215                 if cur:
216                         timer = cur
217                         timer.afterEvent = AFTEREVENT.NONE
218                         self.session.nav.RecordTimer.removeEntry(timer)
219                         if not timer.dontSave:
220                                 for timer in self.session.nav.RecordTimer.timer_list:
221                                         if timer.dontSave and timer.autoincrease:
222                                                 timer.end = timer.begin + (3600 * 24 * 356 * 1)
223                                                 self.session.nav.RecordTimer.timeChanged(timer)
224                                                 timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list,timer)
225                                                 if not timersanitycheck.check():
226                                                         tsc_list = timersanitycheck.getSimulTimerList()
227                                                         if len(tsc_list) > 1:
228                                                                 timer.end = tsc_list[1].begin - 30
229                                                                 self.session.nav.RecordTimer.timeChanged(timer)
230
231                         self.refill()
232                         self.updateState()
233
234         
235         def refill(self):
236                 oldsize = len(self.list)
237                 self.fillTimerList()
238                 lst = self["timerlist"]
239                 newsize = len(self.list)
240                 if oldsize and oldsize != newsize:
241                         idx = lst.getCurrentIndex()
242                         lst.entryRemoved(idx)
243                 else:
244                         lst.invalidate()
245         
246         def addCurrentTimer(self):
247                 event = None
248                 service = self.session.nav.getCurrentService()
249                 if service is not None:
250                         info = service.info()
251                         if info is not None:
252                                 event = info.getEvent(0)
253
254                 # FIXME only works if already playing a service
255                 serviceref = ServiceReference(self.session.nav.getCurrentlyPlayingServiceReference())
256                 
257                 if event is None:       
258                         data = (int(time()), int(time() + 60), "", "", None)
259                 else:
260                         data = parseEvent(event, description = False)
261
262                 self.addTimer(RecordTimerEntry(serviceref, checkOldTimers = True, dirname = config.movielist.last_timer_videodir.value, *data))
263                 
264         def addTimer(self, timer):
265                 self.session.openWithCallback(self.finishedAdd, TimerEntry, timer)
266                 
267         def finishedEdit(self, answer):
268                 print "finished edit"
269                 
270                 if answer[0]:
271                         print "Edited timer"
272                         entry = answer[1]
273                         timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list, entry)
274                         if not timersanitycheck.check():
275                                 simulTimerList = timersanitycheck.getSimulTimerList()
276                                 if (len(simulTimerList) == 2) and (simulTimerList[1].dontSave) and (simulTimerList[1].autoincrease):
277                                         simulTimerList[1].end = entry.begin - 30
278                                         self.session.nav.RecordTimer.timeChanged(simulTimerList[1])
279                                         self.session.nav.RecordTimer.timeChanged(entry)
280                                 else:
281                                         print "Sanity check failed"
282                                         self.session.openWithCallback(self.finishedEdit, TimerSanityConflict, timersanitycheck.getSimulTimerList())
283                         else:
284                                 print "Sanity check passed"
285                                 if not timersanitycheck.doubleCheck():
286                                         self.session.nav.RecordTimer.timeChanged(entry)
287                         self.fillTimerList()
288                         self.updateState()
289                 else:
290                         print "Timeredit aborted"
291
292         def finishedAdd(self, answer):
293                 print "finished add"
294                 if answer[0]:
295                         entry = answer[1]
296                         simulTimerList = self.session.nav.RecordTimer.record(entry)
297                         if simulTimerList is not None:
298                                 if (len(simulTimerList) == 2) and (simulTimerList[1].dontSave) and (simulTimerList[1].autoincrease):
299                                         simulTimerList[1].end = entry.begin - 30
300                                         self.session.nav.RecordTimer.timeChanged(simulTimerList[1])
301                                         self.session.nav.RecordTimer.record(entry)
302                                 else:
303                                         self.session.openWithCallback(self.finishSanityCorrection, TimerSanityConflict, simulTimerList)
304                         self.fillTimerList()
305                         self.updateState()
306                 else:
307                         print "Timeredit aborted"
308
309         def finishSanityCorrection(self, answer):
310                 self.finishedAdd(answer)
311
312         def leave(self):
313                 self.session.nav.RecordTimer.on_state_change.remove(self.onStateChange)
314                 self.close()
315
316         def onStateChange(self, entry):
317                 self.refill()
318                 self.updateState()
319
320 class TimerSanityConflict(Screen):
321         EMPTY = 0
322         ENABLE = 1
323         DISABLE = 2
324         
325         def __init__(self, session, timer):
326                 Screen.__init__(self, session)
327                 self.timer = timer
328                 print "TimerSanityConflict"
329                         
330                 self["timer1"] = TimerList(self.getTimerList(timer[0]))
331                 self.list = []
332                 self.list2 = []
333                 count = 0
334                 for x in timer:
335                         if count != 0:
336                                 self.list.append((_("Conflicting timer") + " " + str(count), x))
337                                 self.list2.append((timer[count], False))
338                         count += 1
339
340                 self["list"] = MenuList(self.list)
341                 self["timer2"] = TimerList(self.list2)
342
343                 self["key_red"] = Button("Edit")
344                 self["key_green"] = Button(" ")
345                 self["key_yellow"] = Button("Edit")
346                 self["key_blue"] = Button(" ")
347
348                 self.key_green_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 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"].instance.invalidate()
409                                 self.key_green_choice = self.ENABLE
410                         elif self.timer[0].isRunning() and not timer[0].repeated and self.key_green_choice != self.EMPTY:
411                                 del self["actions"].actions["green"]
412                                 self["key_green"].setText(" ")
413                                 self["key_green"].instance.invalidate()
414                                 self.key_green_choice = self.EMPTY
415                         elif (not self.timer[0].isRunning() or self.timer[0].repeated ) and self.key_green_choice != self.DISABLE:
416                                 self["actions"].actions.update({"green":self.toggleTimer1})
417                                 self["key_green"].setText(_("Disable"))
418                                 self["key_green"].instance.invalidate()
419                                 self.key_green_choice = self.DISABLE
420                 if len(self.timer) > 1:
421                         x = self["list"].getSelectedIndex()
422                         if self.timer[x] is not None:
423                                 if self.timer[x].disabled and self.key_blue_choice != self.ENABLE:
424                                         self["actions"].actions.update({"blue":self.toggleTimer2})
425                                         self["key_blue"].setText(_("Enable"))
426                                         self["key_blue"].instance.invalidate()
427                                         self.key_blue_choice = self.ENABLE
428                                 elif self.timer[x].isRunning() and not timer[x].repeated and self.key_blue_choice != self.EMPTY:
429                                         del self["actions"].actions["blue"]
430                                         self["key_blue"].setText(" ")
431                                         self["key_blue"].instance.invalidate()
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"].instance.invalidate()
437                                         self.key_blue_choice = self.DISABLE
438                 else:
439 #FIXME.... this doesnt hide the buttons self.... just the text
440                         del self["actions"].actions["yellow"]
441                         self["key_yellow"].setText(" ")
442                         self["key_yellow"].instance.invalidate()
443                         self.key_yellow_choice = self.EMPTY
444                         del self["actions"].actions["blue"]
445                         self["key_blue"].setText(" ")
446                         self["key_blue"].instance.invalidate()
447                         self.key_blue_choice = self.EMPTY