follow Console() changes
[enigma2.git] / lib / python / Screens / EpgSelection.py
1 from Screen import Screen
2 from Components.config import config, ConfigClock
3 from Components.Button import Button
4 from Components.Pixmap import Pixmap
5 from Components.Label import Label
6 from Components.EpgList import EPGList, EPG_TYPE_SINGLE, EPG_TYPE_SIMILAR, EPG_TYPE_MULTI
7 from Components.ActionMap import ActionMap
8 from Components.TimerSanityCheck import TimerSanityCheck
9 from Screens.TimerEdit import TimerSanityConflict
10 from Screens.EventView import EventViewSimple
11 from Screens.MessageBox import MessageBox
12 from TimeDateInput import TimeDateInput
13 from enigma import eServiceReference
14 from RecordTimer import RecordTimerEntry, parseEvent, AFTEREVENT
15 from TimerEntry import TimerEntry
16 from ServiceReference import ServiceReference
17 from time import localtime, time
18
19 mepg_config_initialized = False
20
21 class EPGSelection(Screen):
22         EMPTY = 0
23         ADD_TIMER = 1
24         REMOVE_TIMER = 2
25         
26         ZAP = 1
27
28         def __init__(self, session, service, zapFunc=None, eventid=None, bouquetChangeCB=None):
29                 Screen.__init__(self, session)
30                 self.bouquetChangeCB = bouquetChangeCB
31                 self.ask_time = -1 #now
32                 self["key_red"] = Button("")
33                 self.closeRecursive = False
34                 if isinstance(service, str) and eventid != None:
35                         self.type = EPG_TYPE_SIMILAR
36                         self["key_yellow"] = Button()
37                         self["key_blue"] = Button()
38                         self["key_red"] = Button()
39                         self.currentService=service
40                         self.eventid = eventid
41                         self.zapFunc = None
42                 elif isinstance(service, eServiceReference) or isinstance(service, str):
43                         self.type = EPG_TYPE_SINGLE
44                         self["key_yellow"] = Button()
45                         self["key_blue"] = Button()
46                         self.currentService=ServiceReference(service)
47                         self.zapFunc = None
48                         self.sort_type = 0
49                         self.setSortDescription()
50                 else:
51                         self.skinName = "EPGSelectionMulti"
52                         self.type = EPG_TYPE_MULTI
53                         self["key_yellow"] = Button(_("Prev"))
54                         self["key_blue"] = Button(_("Next"))
55                         self["now_button"] = Pixmap()
56                         self["next_button"] = Pixmap()
57                         self["more_button"] = Pixmap()
58                         self["now_button_sel"] = Pixmap()
59                         self["next_button_sel"] = Pixmap()
60                         self["more_button_sel"] = Pixmap()
61                         self["now_text"] = Label()
62                         self["next_text"] = Label()
63                         self["more_text"] = Label()
64                         self["date"] = Label()
65                         self.services = service
66                         self.zapFunc = zapFunc
67
68                 self["key_green"] = Button(_("Add timer"))
69                 self.key_green_choice = self.ADD_TIMER
70                 self.key_red_choice = self.EMPTY
71                 self["list"] = EPGList(type = self.type, selChangedCB = self.onSelectionChanged, timer = self.session.nav.RecordTimer)
72
73                 self["actions"] = ActionMap(["EPGSelectActions", "OkCancelActions"],
74                         {
75                                 "cancel": self.closeScreen,
76                                 "ok": self.eventSelected,
77                                 "timerAdd": self.timerAdd,
78                                 "yellow": self.yellowButtonPressed,
79                                 "blue": self.blueButtonPressed,
80                                 "info": self.infoKeyPressed,
81                                 "red": self.zapTo,
82                                 "input_date_time": self.enterDateTime,
83                                 "nextBouquet": self.nextBouquet,
84                                 "prevBouquet": self.prevBouquet
85                         })
86                 self["actions"].csel = self
87
88                 self.onLayoutFinish.append(self.onCreate)
89
90         def nextBouquet(self):
91                 if self.bouquetChangeCB:
92                         self.bouquetChangeCB(1, self)
93
94         def prevBouquet(self):
95                 if self.bouquetChangeCB:
96                         self.bouquetChangeCB(-1, self)
97
98         def enterDateTime(self):
99                 if self.type == EPG_TYPE_MULTI:
100                         global mepg_config_initialized
101                         if not mepg_config_initialized:
102                                 config.misc.prev_mepg_time=ConfigClock(default = time())
103                                 mepg_config_initialized = True
104                         self.session.openWithCallback(self.onDateTimeInputClosed, TimeDateInput, config.misc.prev_mepg_time )
105
106         def onDateTimeInputClosed(self, ret):
107                 if len(ret) > 1:
108                         if ret[0]:
109                                 self.ask_time=ret[1]
110                                 self["list"].fillMultiEPG(self.services, ret[1])
111
112         def closeScreen(self):
113                 self.close(self.closeRecursive)
114
115         def infoKeyPressed(self):
116                 cur = self["list"].getCurrent()
117                 event = cur[0]
118                 service = cur[1]
119                 if event is not None:
120                         if self.type != EPG_TYPE_SIMILAR:
121                                 self.session.open(EventViewSimple, event, service, self.eventViewCallback, self.openSimilarList)
122                         else:
123                                 self.session.open(EventViewSimple, event, service, self.eventViewCallback)
124
125         def openSimilarList(self, eventid, refstr):
126                 self.session.open(EPGSelection, refstr, None, eventid)
127
128         def setServices(self, services):
129                 self.services = services
130                 self.onCreate()
131
132         #just used in multipeg
133         def onCreate(self):
134                 l = self["list"]
135                 l.recalcEntrySize()
136                 if self.type == EPG_TYPE_MULTI:
137                         l.fillMultiEPG(self.services, self.ask_time)
138                         l.moveToService(self.session.nav.getCurrentlyPlayingServiceReference())
139                 elif self.type == EPG_TYPE_SINGLE:
140                         l.fillSingleEPG(self.currentService)
141                 else:
142                         l.fillSimilarList(self.currentService, self.eventid)
143
144         def eventViewCallback(self, setEvent, setService, val):
145                 l = self["list"]
146                 old = l.getCurrent()
147                 if val == -1:
148                         self.moveUp()
149                 elif val == +1:
150                         self.moveDown()
151                 cur = l.getCurrent()
152                 if self.type == EPG_TYPE_MULTI and cur[0] is None and cur[1].ref != old[1].ref:
153                         self.eventViewCallback(setEvent, setService, val)
154                 else:
155                         setService(cur[1])
156                         setEvent(cur[0])
157
158         def zapTo(self): # just used in multiepg
159                 if self.zapFunc and self.key_red_choice == self.ZAP:
160                         lst = self["list"]
161                         count = lst.getCurrentChangeCount()
162                         if count == 0:
163                                 self.closeRecursive = True
164                                 ref = lst.getCurrent()[1]
165                                 self.zapFunc(ref.ref)
166
167         def eventSelected(self):
168                 self.infoKeyPressed()
169
170         def yellowButtonPressed(self):
171                 if self.type == EPG_TYPE_MULTI:
172                         self["list"].updateMultiEPG(-1)
173                 elif self.type == EPG_TYPE_SINGLE:
174                         if self.sort_type == 0:
175                                 self.sort_type = 1
176                         else:
177                                 self.sort_type = 0
178                         self["list"].sortSingleEPG(self.sort_type)
179                         self.setSortDescription()
180
181         def setSortDescription(self):
182                 if self.sort_type == 1:
183                         # TRANSLATORS: This must fit into the header button in the EPG-List
184                         self["key_yellow"].setText(_("Sort Time"))
185                 else:
186                         # TRANSLATORS: This must fit into the header button in the EPG-List
187                         self["key_yellow"].setText(_("Sort A-Z"))
188
189         def blueButtonPressed(self):
190                 if self.type == EPG_TYPE_MULTI:
191                         self["list"].updateMultiEPG(1)
192
193         def removeTimer(self, timer):
194                 timer.afterEvent = AFTEREVENT.NONE
195                 self.session.nav.RecordTimer.removeEntry(timer)
196                 self["key_green"].setText(_("Add timer"))
197                 self.key_green_choice = self.ADD_TIMER
198
199         def timerAdd(self):
200                 cur = self["list"].getCurrent()
201                 event = cur[0]
202                 serviceref = cur[1]
203                 if event is None:
204                         return
205                 eventid = event.getEventId()
206                 refstr = serviceref.ref.toString()
207                 for timer in self.session.nav.RecordTimer.timer_list:
208                         if timer.eit == eventid and timer.service_ref.ref.toString() == refstr:
209                                 cb_func = lambda ret : not ret or self.removeTimer(timer)
210                                 self.session.openWithCallback(cb_func, MessageBox, _("Do you really want to delete %s?") % event.getEventName())
211                                 break
212                 else:
213                         newEntry = RecordTimerEntry(serviceref, checkOldTimers = True, *parseEvent(event))
214                         self.session.openWithCallback(self.finishedAdd, TimerEntry, newEntry)
215
216         def finishedAdd(self, answer):
217                 print "finished add"
218                 if answer[0]:
219                         entry = answer[1]
220                         simulTimerList = self.session.nav.RecordTimer.record(entry)
221                         if simulTimerList is not None:
222                                 for x in simulTimerList:
223                                         if x.setAutoincreaseEnd(entry):
224                                                 self.session.nav.RecordTimer.timeChanged(x)
225                                 simulTimerList = self.session.nav.RecordTimer.record(entry)
226                                 if simulTimerList is not None:
227                                         self.session.openWithCallback(self.finishSanityCorrection, TimerSanityConflict, simulTimerList)
228                         self["key_green"].setText(_("Remove timer"))
229                         self.key_green_choice = self.REMOVE_TIMER
230                 else:
231                         self["key_green"].setText(_("Add timer"))
232                         self.key_green_choice = self.ADD_TIMER
233                         print "Timeredit aborted"
234         
235         def finishSanityCorrection(self, answer):
236                 self.finishedAdd(answer)
237
238         def moveUp(self):
239                 self["list"].moveUp()
240
241         def moveDown(self):
242                 self["list"].moveDown()
243         
244         def applyButtonState(self, state):
245                 if state == 0:
246                         self["now_button"].hide()
247                         self["now_button_sel"].hide()
248                         self["next_button"].hide()
249                         self["next_button_sel"].hide()
250                         self["more_button"].hide()
251                         self["more_button_sel"].hide()
252                         self["now_text"].hide()
253                         self["next_text"].hide()
254                         self["more_text"].hide()
255                         self["key_red"].setText("")
256                 else:
257                         if state == 1:
258                                 self["now_button_sel"].show()
259                                 self["now_button"].hide()
260                         else:
261                                 self["now_button"].show()
262                                 self["now_button_sel"].hide()
263
264                         if state == 2:
265                                 self["next_button_sel"].show()
266                                 self["next_button"].hide()
267                         else:
268                                 self["next_button"].show()
269                                 self["next_button_sel"].hide()
270
271                         if state == 3:
272                                 self["more_button_sel"].show()
273                                 self["more_button"].hide()
274                         else:
275                                 self["more_button"].show()
276                                 self["more_button_sel"].hide()
277
278         def onSelectionChanged(self):
279                 cur = self["list"].getCurrent()
280                 if cur is None:
281                         if self.key_green_choice != self.EMPTY:
282                                 self["key_green"].setText("")
283                                 self.key_green_choice = self.EMPTY
284                         if self.key_red_choice != self.EMPTY:
285                                 self["key_red"].setText("")
286                                 self.key_red_choice = self.EMPTY
287                         return
288                 event = cur[0]
289                 if self.type == EPG_TYPE_MULTI:
290                         count = self["list"].getCurrentChangeCount()
291                         if self.ask_time != -1:
292                                 self.applyButtonState(0)
293                         elif count > 1:
294                                 self.applyButtonState(3)
295                         elif count > 0:
296                                 self.applyButtonState(2)
297                         else:
298                                 self.applyButtonState(1)
299                         days = [ _("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat"), _("Sun") ]
300                         datestr = ""
301                         if event is not None:
302                                 now = time()
303                                 beg = event.getBeginTime()
304                                 nowTime = localtime(now)
305                                 begTime = localtime(beg)
306                                 if nowTime[2] != begTime[2]:
307                                         datestr = '%s %d.%d.'%(days[begTime[6]], begTime[2], begTime[1])
308                                 else:
309                                         datestr = '%s %d.%d.'%(_("Today"), begTime[2], begTime[1])
310                         self["date"].setText(datestr)
311
312                 if cur[1] is None  or cur[1].getServiceName() == "":
313                         if self.key_green_choice != self.EMPTY:
314                                 self["key_green"].setText("")
315                                 self.key_green_choice = self.EMPTY
316                         if self.key_red_choice != self.EMPTY:
317                                 self["key_red"].setText("")
318                                 self.key_red_choice = self.EMPTY
319                         return
320                 elif self.key_red_choice != self.ZAP and  self.type == EPG_TYPE_MULTI:
321                                 self["key_red"].setText("Zap")
322                                 self.key_red_choice = self.ZAP
323
324                 if event is None:
325                         if self.key_green_choice != self.EMPTY:
326                                 self["key_green"].setText("")
327                                 self.key_green_choice = self.EMPTY
328                         return
329
330                 serviceref = cur[1]
331                 eventid = event.getEventId()
332                 refstr = serviceref.ref.toString()
333                 isRecordEvent = False
334                 for timer in self.session.nav.RecordTimer.timer_list:
335                         if timer.eit == eventid and timer.service_ref.ref.toString() == refstr:
336                                 isRecordEvent = True
337                                 break
338                 if isRecordEvent and self.key_green_choice != self.REMOVE_TIMER:
339                         self["key_green"].setText(_("Remove timer"))
340                         self.key_green_choice = self.REMOVE_TIMER
341                 elif not isRecordEvent and self.key_green_choice != self.ADD_TIMER:
342                         self["key_green"].setText(_("Add timer"))
343                         self.key_green_choice = self.ADD_TIMER