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