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