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