whitespace fixes
[enigma2.git] / lib / python / Screens / TimerEdit.py
1 from Screen import Screen
2 from Components.TimerList import TimerList, TimerEntryComponent
3 from Components.MenuList import MenuList
4 from Components.ActionMap import ActionMap
5 from Components.Label import Label
6 from Components.Button import Button
7 from Screens.MessageBox import MessageBox
8 from TimerEntry import TimerEntry, TimerLog
9 from RecordTimer import RecordTimerEntry, parseEvent, AFTEREVENT
10 from time import *
11 from ServiceReference import ServiceReference
12 from Components.TimerSanityCheck import TimerSanityCheck
13
14 class TimerEditList(Screen):
15         def __init__(self, session):
16                 Screen.__init__(self, session)
17                 
18                 list = [ ]
19                 self.list = list
20                 self.fillTimerList()
21
22                 self["timerlist"] = TimerList(list)
23                 
24                 self["key_red"] = Button(_("Delete"))
25                 self["key_green"] = Button(_("Add"))
26                 self["key_yellow"] = Button("")
27                 self["key_blue"] = Button(_("Cleanup"))
28
29                 self["actions"] = ActionMap(["OkCancelActions", "DirectionActions", "ShortcutActions", "TimerEditActions"], 
30                         {
31                                 "ok": self.openEdit,
32                                 "cancel": self.leave,
33                                 "red": self.removeTimerQuestion,
34                                 "green": self.addCurrentTimer,
35                                 "blue": self.cleanupQuestion,
36                                 "yellow": self.toggleDisabledState,
37                                 "log": self.showLog,
38                                 "left": self.left,
39                                 "right": self.right,
40                                 "up": self.up,
41                                 "down": self.down
42                         }, -1)
43                 self.session.nav.RecordTimer.on_state_change.append(self.onStateChange)
44                 self.onShown.append(self.updateState)
45
46         def up(self):
47                 self["timerlist"].instance.moveSelection(self["timerlist"].instance.moveUp)
48                 self.updateState()
49                 
50         def down(self):
51                 self["timerlist"].instance.moveSelection(self["timerlist"].instance.moveDown)
52                 self.updateState()
53
54         def left(self):
55                 self["timerlist"].instance.moveSelection(self["timerlist"].instance.pageUp)
56                 self.updateState()
57                 
58         def right(self):
59                 self["timerlist"].instance.moveSelection(self["timerlist"].instance.pageDown)
60                 self.updateState()
61                 
62         def toggleDisabledState(self):
63                 cur=self["timerlist"].getCurrent()
64                 if cur:
65                         t = cur[0]
66                 
67                         if t.disabled:
68                                 t.enable()
69                         else:
70                                 t.disable()
71
72                         self.session.nav.RecordTimer.timeChanged(t)
73                         self.updateState()
74                         self.refill()
75                 
76         def updateState(self):
77                 if len(self.list) > 0:
78                         if self["timerlist"].getCurrent()[0].disabled:
79                                 self["key_yellow"].setText(_("Enable"))
80                         else:
81                                 self["key_yellow"].setText(_("Disable"))
82                         self["key_yellow"].instance.invalidate()
83
84         def fillTimerList(self):
85                 del self.list[:]
86                 
87                 for timer in self.session.nav.RecordTimer.timer_list:
88                         self.list.append(TimerEntryComponent(timer, processed=False))
89                 
90                 for timer in self.session.nav.RecordTimer.processed_timers:
91                         self.list.append(TimerEntryComponent(timer, processed=True))
92                 self.list.sort(cmp = lambda x, y: x[0].begin < y[0].begin)
93
94         def showLog(self):
95                 cur=self["timerlist"].getCurrent()
96                 if cur:
97                         self.session.openWithCallback(self.finishedEdit, TimerLog, cur[0])
98
99         def openEdit(self):
100                 cur=self["timerlist"].getCurrent()
101                 if cur:
102                         self.session.openWithCallback(self.finishedEdit, TimerEntry, cur[0])
103
104         def cleanupQuestion(self):
105                 self.session.openWithCallback(self.cleanupTimer, MessageBox, _("Really delete done timers?"))
106         
107         def cleanupTimer(self, delete):
108                 if delete:
109                         self.session.nav.RecordTimer.cleanup()
110                         self.refill()
111         
112         def removeTimerQuestion(self):
113                 self.session.openWithCallback(self.removeTimer, MessageBox, _("Really delete this timer?"))
114                 
115         def removeTimer(self, result):
116                 if not result:
117                         return
118                 list = self["timerlist"]
119                 cur = list.getCurrent()
120                 if cur:
121                         timer = cur[0]
122                         timer.afterEvent = AFTEREVENT.NONE
123                         self.session.nav.RecordTimer.removeEntry(timer)
124                         self.refill()
125         
126         def refill(self):
127                 self.fillTimerList()
128                 self["timerlist"].invalidate()
129         
130         def addCurrentTimer(self):
131                 event = None
132                 service = self.session.nav.getCurrentService()
133                 if service is not None:
134                         info = service.info()
135                         if info is not None:
136                                 event = info.getEvent(0)
137
138                 # FIXME only works if already playing a service
139                 serviceref = ServiceReference(self.session.nav.getCurrentlyPlayingServiceReference())
140                 
141                 if event is None:       
142                         data = (int(time()), int(time() + 60), "", "", None)
143                 else:
144                         data = parseEvent(event, description = False)
145
146                 self.addTimer(RecordTimerEntry(serviceref, checkOldTimers = True, *data))
147                 
148         def addTimer(self, timer):
149                 self.session.openWithCallback(self.finishedAdd, TimerEntry, timer)
150                 
151         def finishedEdit(self, answer):
152                 print "finished edit"
153                 
154                 if answer[0]:
155                         print "Edited timer"
156                         timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list, answer[1])
157                         if not timersanitycheck.check():
158                                 print "Sanity check failed"
159                         else:
160                                 print "Sanity check passed"
161                         self.session.nav.RecordTimer.timeChanged(answer[1])
162                         self.fillTimerList()
163                 else:
164                         print "Timeredit aborted"
165
166         def finishedAdd(self, answer):
167                 print "finished add"
168                 if answer[0]:
169                         timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list, answer[1])
170                         if not timersanitycheck.check():
171                                 print "Sanity check failed"
172                                 self.session.openWithCallback(self.finishSanityCorrection, TimerSanityConflict, timersanitycheck.getSimulTimerList())
173                         else:
174                                 print "Sanity check passed"
175                                 entry = answer[1]
176                                 self.session.nav.RecordTimer.record(entry)
177                                 self.fillTimerList()
178                 else:
179                         print "Timeredit aborted"               
180
181         def finishSanityCorrection(self, answer):
182                 self.finishedAdd(answer)
183
184         def leave(self):
185                 self.session.nav.RecordTimer.saveTimer()
186                 self.session.nav.RecordTimer.on_state_change.remove(self.onStateChange)
187                 self.close()
188
189         def onStateChange(self, entry):
190                 self.refill()
191                 
192 class TimerSanityConflict(Screen):
193         def __init__(self, session, timer):
194                 Screen.__init__(self, session)
195                 self.timer = timer
196                 print "TimerSanityConflict", timer
197                         
198                 self["timer1"] = TimerList(self.getTimerList(timer[0]))
199                 if len(timer) > 1:
200                         self["timer2"] = TimerList(self.getTimerList(timer[1]))
201                 else:
202                         self["timer2"] = Button("No conflict")
203                 
204                 self.list = []
205                 count = 0
206                 for x in timer:
207                         if count != 0:
208                                 self.list.append((_("Conflicting timer") + " " + str(count), x))
209                         count += 1
210
211                 self["list"] = MenuList(self.list)
212                 
213                 self["key_red"] = Button("Edit")
214                 self["key_green"] = Button("Disable")
215                 self["key_yellow"] = Button("Edit")
216                 self["key_blue"] = Button("Disable")
217
218                 self["actions"] = ActionMap(["OkCancelActions", "DirectionActions", "ShortcutActions", "TimerEditActions"], 
219                         {
220                                 "ok": self.close,
221                                 #"cancel": self.leave,
222                                 "red": self.editTimer1,
223                                 "green": self.disableTimer1,
224 #                               "yellow": self.editTimer2,
225 #                               "blue": self.disableTimer2,
226                                 #"log": self.showLog,
227                                 #"left": self.left,
228                                 #"right": self.right,
229                                 "up": self.up,
230                                 "down": self.down
231                         }, -1)
232
233         def getTimerList(self, timer):
234                 return [TimerEntryComponent(timer, processed=False)]
235
236         def editTimer1(self):
237                 self.session.openWithCallback(self.finishedEdit, TimerEntry, self["timer1"].getCurrent()[0])
238
239         def disableTimer1(self):
240                 self.timer[0].disabled = True
241                 self.finishedEdit((True, self.timer[0]))
242
243         def finishedEdit(self, answer):
244                 self.close((True, self.timer[0]))
245
246         def up(self):
247                 self["list"].instance.moveSelection(self["list"].instance.moveUp)
248                 self["timer2"].l.setList(self.getTimerList(self["list"].getCurrent()[1]))
249                 
250         def down(self):
251                 self["list"].instance.moveSelection(self["list"].instance.moveDown)
252                 self["timer2"].l.setList(self.getTimerList(self["list"].getCurrent()[1]))
253                         
254