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