small fixes
[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         def __init__(self, session):
18                 Screen.__init__(self, session)
19                 
20                 list = [ ]
21                 self.list = list
22                 self.fillTimerList()
23
24                 self["timerlist"] = TimerList(list)
25                 
26                 self["key_red"] = Button(_("Delete"))
27                 self["key_green"] = Button(_("Add"))
28                 self["key_yellow"] = Button("")
29                 self["key_blue"] = Button(_("Cleanup"))
30
31                 self["actions"] = ActionMap(["OkCancelActions", "DirectionActions", "ShortcutActions", "TimerEditActions"], 
32                         {
33                                 "ok": self.openEdit,
34                                 "cancel": self.leave,
35                                 "red": self.removeTimerQuestion,
36                                 "green": self.addCurrentTimer,
37                                 "blue": self.cleanupQuestion,
38                                 "yellow": self.toggleDisabledState,
39                                 "log": self.showLog,
40                                 "left": self.left,
41                                 "right": self.right,
42                                 "up": self.up,
43                                 "down": self.down
44                         }, -1)
45                 self.session.nav.RecordTimer.on_state_change.append(self.onStateChange)
46                 self.onShown.append(self.updateState)
47
48         def up(self):
49                 self["timerlist"].instance.moveSelection(self["timerlist"].instance.moveUp)
50                 self.updateState()
51                 
52         def down(self):
53                 self["timerlist"].instance.moveSelection(self["timerlist"].instance.moveDown)
54                 self.updateState()
55
56         def left(self):
57                 self["timerlist"].instance.moveSelection(self["timerlist"].instance.pageUp)
58                 self.updateState()
59                 
60         def right(self):
61                 self["timerlist"].instance.moveSelection(self["timerlist"].instance.pageDown)
62                 self.updateState()
63                 
64         def toggleDisabledState(self):
65                 cur=self["timerlist"].getCurrent()
66                 if cur:
67                         t = cur
68                 
69                         if t.disabled:
70                                 t.enable()
71                                 self.session.nav.RecordTimer.timeChanged(t)
72
73                         else:
74                                 if t.isRunning() and t.repeated:
75                                         list = []
76                                         list.append((_("Stop current event but not coming events"), "stoponlycurrent"))
77                                         list.append((_("Stop current event and disable coming events"), "stopall"))
78                                         list.append((_("Don't stop current event but disable coming events"), "stoponlycoming"))
79                                         self.session.openWithCallback(boundFunction(self.runningEventCallback, t), ChoiceBox, title=_("Repeating event currently recording... What do you want to do?"), list = list)
80                                 else:
81                                         t.disable()
82                                         self.session.nav.RecordTimer.timeChanged(t)
83                         self.updateState()
84                         self.refill()
85
86         def runningEventCallback(self, t, result):
87                 if result is not None:
88                         if result[1] == "stoponlycurrent" or result[1] == "stopall":
89                                 t.enable()
90                                 t.processRepeated(findRunningEvent = False)
91                                 self.session.nav.RecordTimer.doActivate(t)
92                         if result[1] == "stoponlycoming" or result[1] == "stopall":
93                                 t.disable()
94                         self.session.nav.RecordTimer.timeChanged(t)
95
96                         self.updateState()
97                         self.refill()
98                 
99         def updateState(self):
100                 if len(self.list) > 0:
101                         if self["timerlist"].getCurrent().disabled:
102                                 self["key_yellow"].setText(_("Enable"))
103                         else:
104                                 self["key_yellow"].setText(_("Disable"))
105                         self["key_yellow"].instance.invalidate()
106
107         def fillTimerList(self):
108                 del self.list[:]
109                 
110                 for timer in self.session.nav.RecordTimer.timer_list:
111                         self.list.append((timer, False))
112                 
113                 for timer in self.session.nav.RecordTimer.processed_timers:
114                         self.list.append((timer, True))
115                 self.list.sort(cmp = lambda x, y: x[0].begin < y[0].begin)
116
117         def showLog(self):
118                 cur=self["timerlist"].getCurrent()
119                 if cur:
120                         self.session.openWithCallback(self.finishedEdit, TimerLog, cur)
121
122         def openEdit(self):
123                 cur=self["timerlist"].getCurrent()
124                 if cur:
125                         self.session.openWithCallback(self.finishedEdit, TimerEntry, cur)
126
127         def cleanupQuestion(self):
128                 self.session.openWithCallback(self.cleanupTimer, MessageBox, _("Really delete done timers?"))
129         
130         def cleanupTimer(self, delete):
131                 if delete:
132                         self.session.nav.RecordTimer.cleanup()
133                         self.refill()
134
135         def removeTimerQuestion(self):
136                 if not self["timerlist"].getCurrent():
137                         return
138                 self.session.openWithCallback(self.removeTimer, MessageBox, _("Really delete this timer?"))
139
140         def removeTimer(self, result):
141                 if not result:
142                         return
143                 list = self["timerlist"]
144                 cur = list.getCurrent()
145                 if cur:
146                         timer = cur
147                         timer.afterEvent = AFTEREVENT.NONE
148                         self.session.nav.RecordTimer.removeEntry(timer)
149                         self.refill()
150         
151         def refill(self):
152                 oldsize = len(self.list)
153                 self.fillTimerList()
154                 lst = self["timerlist"]
155                 newsize = len(self.list)
156                 if oldsize and oldsize != newsize:
157                         idx = lst.getCurrentIndex()
158                         lst.entryRemoved(idx)
159                 else:
160                         lst.invalidate()
161         
162         def addCurrentTimer(self):
163                 event = None
164                 service = self.session.nav.getCurrentService()
165                 if service is not None:
166                         info = service.info()
167                         if info is not None:
168                                 event = info.getEvent(0)
169
170                 # FIXME only works if already playing a service
171                 serviceref = ServiceReference(self.session.nav.getCurrentlyPlayingServiceReference())
172                 
173                 if event is None:       
174                         data = (int(time()), int(time() + 60), "", "", None)
175                 else:
176                         data = parseEvent(event, description = False)
177
178                 self.addTimer(RecordTimerEntry(serviceref, checkOldTimers = True, dirname = config.movielist.last_timer_videodir.value, *data))
179                 
180         def addTimer(self, timer):
181                 self.session.openWithCallback(self.finishedAdd, TimerEntry, timer)
182                 
183         def finishedEdit(self, answer):
184                 print "finished edit"
185                 
186                 if answer[0]:
187                         print "Edited timer"
188                         timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list, answer[1])
189                         if not timersanitycheck.check():
190                                 print "Sanity check failed"
191                         else:
192                                 print "Sanity check passed"
193                         self.session.nav.RecordTimer.timeChanged(answer[1])
194                         self.fillTimerList()
195                 else:
196                         print "Timeredit aborted"
197
198         def finishedAdd(self, answer):
199                 print "finished add"
200                 if answer[0]:
201                         timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list, answer[1])
202                         if not timersanitycheck.check():
203                                 print "Sanity check failed"
204                                 self.session.openWithCallback(self.finishSanityCorrection, TimerSanityConflict, timersanitycheck.getSimulTimerList())
205                         else:
206                                 print "Sanity check passed"
207                                 entry = answer[1]
208                                 self.session.nav.RecordTimer.record(entry)
209                                 self.fillTimerList()
210                 else:
211                         print "Timeredit aborted"               
212
213         def finishSanityCorrection(self, answer):
214                 self.finishedAdd(answer)
215
216         def leave(self):
217                 self.session.nav.RecordTimer.on_state_change.remove(self.onStateChange)
218                 self.close()
219
220         def onStateChange(self, entry):
221                 self.refill()
222
223 class TimerSanityConflict(Screen):
224         def __init__(self, session, timer):
225                 Screen.__init__(self, session)
226                 self.timer = timer
227                 print "TimerSanityConflict", timer
228                         
229                 self["timer1"] = TimerList(self.getTimerList(timer[0]))
230                 if len(timer) > 1:
231                         self["timer2"] = TimerList(self.getTimerList(timer[1]))
232                 else:
233                         self["timer2"] = TimerList([])
234                 
235                 self.list = []
236                 count = 0
237                 for x in timer:
238                         if count != 0:
239                                 self.list.append((_("Conflicting timer") + " " + str(count), x))
240                         count += 1
241
242                 self["list"] = MenuList(self.list)
243                 
244                 self["key_red"] = Button("Edit")
245                 self["key_green"] = Button("Disable")
246                 self["key_yellow"] = Button("Edit")
247                 self["key_blue"] = Button("Disable")
248
249                 self["actions"] = ActionMap(["OkCancelActions", "DirectionActions", "ShortcutActions", "TimerEditActions"], 
250                         {
251                                 "ok": self.close,
252                                 #"cancel": self.leave,
253                                 "red": self.editTimer1,
254                                 "green": self.disableTimer1,
255 #                               "yellow": self.editTimer2,
256 #                               "blue": self.disableTimer2,
257                                 #"log": self.showLog,
258                                 #"left": self.left,
259                                 #"right": self.right,
260                                 "up": self.up,
261                                 "down": self.down
262                         }, -1)
263
264         def getTimerList(self, timer):
265                 return [(timer, False)]
266
267         def editTimer1(self):
268                 self.session.openWithCallback(self.finishedEdit, TimerEntry, self["timer1"].getCurrent())
269
270         def disableTimer1(self):
271                 self.timer[0].disabled = True
272                 self.finishedEdit((True, self.timer[0]))
273
274         def finishedEdit(self, answer):
275                 self.close((True, self.timer[0]))
276
277         def up(self):
278                 self["list"].instance.moveSelection(self["list"].instance.moveUp)
279                 self["timer2"].l.setList(self.getTimerList(self["list"].getCurrent()))
280                 
281         def down(self):
282                 self["list"].instance.moveSelection(self["list"].instance.moveDown)
283                 self["timer2"].l.setList(self.getTimerList(self["list"].getCurrent()))