config rewrite. some extensions still need to be updated.
[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.removeTimer,
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 removeTimer(self):
113                 list = self["timerlist"]
114                 cur = list.getCurrent()
115                 if cur:
116                         timer = cur[0]
117                         timer.afterEvent = AFTEREVENT.NONE
118                         self.session.nav.RecordTimer.removeEntry(timer)
119                         self.refill()
120         
121         def refill(self):
122                 self.fillTimerList()
123                 self["timerlist"].invalidate()
124         
125         def addCurrentTimer(self):
126                 event = None
127                 service = self.session.nav.getCurrentService()
128                 if service is not None:
129                         info = service.info()
130                         if info is not None:
131                                 event = info.getEvent(0)
132
133                 # FIXME only works if already playing a service
134                 serviceref = ServiceReference(self.session.nav.getCurrentlyPlayingServiceReference())
135                 
136                 if event is None:       
137                         data = (int(time()), int(time() + 60), "unknown event", "", None)
138                 else:
139                         data = parseEvent(event)
140
141                 self.addTimer(RecordTimerEntry(serviceref, checkOldTimers = True, *data))
142                 
143         def addTimer(self, timer):
144                 self.session.openWithCallback(self.finishedAdd, TimerEntry, timer)
145                 
146         def finishedEdit(self, answer):
147                 print "finished edit"
148                 
149                 if answer[0]:
150                         print "Edited timer"
151                         timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list, answer[1])
152                         if not timersanitycheck.check():
153                                 print "Sanity check failed"
154                         else:
155                                 print "Sanity check passed"
156                         self.session.nav.RecordTimer.timeChanged(answer[1])
157                         self.fillTimerList()
158                 else:
159                         print "Timeredit aborted"
160
161         def finishedAdd(self, answer):
162                 print "finished add"
163                 if answer[0]:
164                         timersanitycheck = TimerSanityCheck(self.session.nav.RecordTimer.timer_list, answer[1])
165                         if not timersanitycheck.check():
166                                 print "Sanity check failed"
167                                 self.session.openWithCallback(self.finishSanityCorrection, TimerSanityConflict, timersanitycheck.getSimulTimerList())
168                         else:
169                                 print "Sanity check passed"
170                                 entry = answer[1]
171                                 self.session.nav.RecordTimer.record(entry)
172                                 self.fillTimerList()
173                 else:
174                         print "Timeredit aborted"               
175
176         def finishSanityCorrection(self, answer):
177                 self.finishedAdd(answer)
178
179         def leave(self):
180                 self.session.nav.RecordTimer.saveTimer()
181                 self.session.nav.RecordTimer.on_state_change.remove(self.onStateChange)
182                 self.close()
183
184         def onStateChange(self, entry):
185                 self.refill()
186                 
187 class TimerSanityConflict(Screen):
188         def __init__(self, session, timer):
189                 Screen.__init__(self, session)
190                 self.timer = timer
191                 print "TimerSanityConflict", timer
192                         
193                 self["timer1"] = TimerList(self.getTimerList(timer[0]))
194                 if len(timer) > 1:
195                         self["timer2"] = TimerList(self.getTimerList(timer[1]))
196                 else:
197                         self["timer2"] = Button("No conflict")
198                 
199                 self.list = []
200                 count = 0
201                 for x in timer:
202                         if count != 0:
203                                 self.list.append((_("Conflicting timer") + " " + str(count), x))
204                         count += 1
205
206                 self["list"] = MenuList(self.list)
207                 
208                 self["key_red"] = Button("Edit")
209                 self["key_green"] = Button("Disable")
210                 self["key_yellow"] = Button("Edit")
211                 self["key_blue"] = Button("Disable")
212
213                 self["actions"] = ActionMap(["OkCancelActions", "DirectionActions", "ShortcutActions", "TimerEditActions"], 
214                         {
215                                 "ok": self.close,
216                                 #"cancel": self.leave,
217                                 "red": self.editTimer1,
218                                 "green": self.disableTimer1,
219 #                               "yellow": self.editTimer2,
220 #                               "blue": self.disableTimer2,
221                                 #"log": self.showLog,
222                                 #"left": self.left,
223                                 #"right": self.right,
224                                 "up": self.up,
225                                 "down": self.down
226                         }, -1)
227
228         def getTimerList(self, timer):
229                 return [TimerEntryComponent(timer, processed=False)]
230
231         def editTimer1(self):
232                 self.session.openWithCallback(self.finishedEdit, TimerEntry, self["timer1"].getCurrent()[0])
233
234         def disableTimer1(self):
235                 self.timer[0].disabled = True
236                 self.finishedEdit((True, self.timer[0]))
237
238         def finishedEdit(self, answer):
239                 self.close((True, self.timer[0]))
240
241         def up(self):
242                 self["list"].instance.moveSelection(self["list"].instance.moveUp)
243                 self["timer2"].l.setList(self.getTimerList(self["list"].getCurrent()[1]))
244                 
245         def down(self):
246                 self["list"].instance.moveSelection(self["list"].instance.moveDown)
247                 self["timer2"].l.setList(self.getTimerList(self["list"].getCurrent()[1]))
248                         
249