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