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