fix copy&paste typos
[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 Screens.TimerEdit import TimerSanityConflict
9 from Screens.EventView import EventViewSimple
10 from TimeDateInput import TimeDateInput
11 from enigma import eServiceReference
12 from RecordTimer import RecordTimerEntry, parseEvent
13 from TimerEntry import TimerEntry
14 from ServiceReference import ServiceReference
15 from time import localtime, time
16
17 mepg_config_initialized = False
18
19 class EPGSelection(Screen):
20         def __init__(self, session, service, zapFunc=None, eventid=None, bouquetChangeCB=None):
21                 Screen.__init__(self, session)
22                 self.bouquetChangeCB = bouquetChangeCB
23                 self.ask_time = -1 #now
24                 self["key_red"] = Button("")
25                 self.closeRecursive = False
26                 if isinstance(service, str) and eventid != None:
27                         self.type = EPG_TYPE_SIMILAR
28                         self["key_yellow"] = Button()
29                         self["key_blue"] = Button()
30                         self["key_red"] = Button()
31                         self.currentService=service
32                         self.eventid = eventid
33                         self.zapFunc = None
34                 elif isinstance(service, eServiceReference) or isinstance(service, str):
35                         self.type = EPG_TYPE_SINGLE
36                         self["key_yellow"] = Button()
37                         self["key_blue"] = Button()
38                         self.currentService=ServiceReference(service)
39                         self.zapFunc = None
40                         self.sort_type = 0
41                         self.setSortDescription()
42                 else:
43                         self.skinName = "EPGSelectionMulti"
44                         self.type = EPG_TYPE_MULTI
45                         self["key_yellow"] = Button(_("Prev"))
46                         self["key_blue"] = Button(_("Next"))
47                         self["now_button"] = Pixmap()
48                         self["next_button"] = Pixmap()
49                         self["more_button"] = Pixmap()
50                         self["now_button_sel"] = Pixmap()
51                         self["next_button_sel"] = Pixmap()
52                         self["more_button_sel"] = Pixmap()
53                         self["now_text"] = Label()
54                         self["next_text"] = Label()
55                         self["more_text"] = Label()
56                         self["date"] = Label()
57                         self.services = service
58                         self.zapFunc = zapFunc
59
60                 self["key_green"] = Button(_("Add timer"))
61                 self["list"] = EPGList(type = self.type, selChangedCB = self.onSelectionChanged, timer = self.session.nav.RecordTimer)
62
63                 self["actions"] = ActionMap(["EPGSelectActions", "OkCancelActions"],
64                         {
65                                 "cancel": self.closeScreen,
66                                 "ok": self.eventSelected,
67                                 "timerAdd": self.timerAdd,
68                                 "yellow": self.yellowButtonPressed,
69                                 "blue": self.blueButtonPressed,
70                                 "info": self.infoKeyPressed,
71                                 "red": self.zapTo,
72                                 "input_date_time": self.enterDateTime,
73                                 "nextBouquet": self.nextBouquet,
74                                 "prevBouquet": self.prevBouquet
75                         })
76                 self["actions"].csel = self
77
78                 self.onLayoutFinish.append(self.onCreate)
79
80         def nextBouquet(self):
81                 if self.bouquetChangeCB:
82                         self.bouquetChangeCB(1, self)
83
84         def prevBouquet(self):
85                 if self.bouquetChangeCB:
86                         self.bouquetChangeCB(-1, self)
87
88         def enterDateTime(self):
89                 if self.type == EPG_TYPE_MULTI:
90                         global mepg_config_initialized
91                         if not mepg_config_initialized:
92                                 config.misc.prev_mepg_time=ConfigClock(default = time())
93                                 mepg_config_initialized = True
94                         self.session.openWithCallback(self.onDateTimeInputClosed, TimeDateInput, config.misc.prev_mepg_time )
95
96         def onDateTimeInputClosed(self, ret):
97                 if len(ret) > 1:
98                         if ret[0]:
99                                 self.ask_time=ret[1]
100                                 self["list"].fillMultiEPG(self.services, ret[1])
101
102         def closeScreen(self):
103                 self.close(self.closeRecursive)
104
105         def infoKeyPressed(self):
106                 cur = self["list"].getCurrent()
107                 event = cur[0]
108                 service = cur[1]
109                 if event is not None:
110                         if self.type != EPG_TYPE_SIMILAR:
111                                 self.session.open(EventViewSimple, event, service, self.eventViewCallback, self.openSimilarList)
112                         else:
113                                 self.session.open(EventViewSimple, event, service, self.eventViewCallback)
114
115         def openSimilarList(self, eventid, refstr):
116                 self.session.open(EPGSelection, refstr, None, eventid)
117
118         def setServices(self, services):
119                 self.services = services
120                 self.onCreate()
121
122         #just used in multipeg
123         def onCreate(self):
124                 l = self["list"]
125                 l.recalcEntrySize()
126                 if self.type == EPG_TYPE_MULTI:
127                         l.fillMultiEPG(self.services, self.ask_time)
128                 elif self.type == EPG_TYPE_SINGLE:
129                         l.fillSingleEPG(self.currentService)
130                 else:
131                         l.fillSimilarList(self.currentService, self.eventid)
132
133         def eventViewCallback(self, setEvent, setService, val):
134                 l = self["list"]
135                 old = l.getCurrent()
136                 if val == -1:
137                         self.moveUp()
138                 elif val == +1:
139                         self.moveDown()
140                 cur = l.getCurrent()
141                 if self.type == EPG_TYPE_MULTI and cur[0] is None and cur[1].ref != old[1].ref:
142                         self.eventViewCallback(setEvent, setService, val)
143                 else:
144                         setService(cur[1])
145                         setEvent(cur[0])
146
147         def zapTo(self): # just used in multiepg
148                 if self.zapFunc and self["key_red"].getText() == "Zap":
149                         lst = self["list"]
150                         count = lst.getCurrentChangeCount()
151                         if count == 0:
152                                 self.closeRecursive = True
153                                 ref = lst.getCurrent()[1]
154                                 self.zapFunc(ref.ref)
155
156         def eventSelected(self):
157                 self.infoKeyPressed()
158
159         def yellowButtonPressed(self):
160                 if self.type == EPG_TYPE_MULTI:
161                         self["list"].updateMultiEPG(-1)
162                 elif self.type == EPG_TYPE_SINGLE:
163                         if self.sort_type == 0:
164                                 self.sort_type = 1
165                         else:
166                                 self.sort_type = 0
167                         self["list"].sortSingleEPG(self.sort_type)
168                         self.setSortDescription()
169
170         def setSortDescription(self):
171                 if self.sort_type == 1:
172                         # TRANSLATORS: This must fit into the header button in the EPG-List
173                         self["key_yellow"].setText(_("Sort Time"))
174                 else:
175                         # TRANSLATORS: This must fit into the header button in the EPG-List
176                         self["key_yellow"].setText(_("Sort A-Z"))
177
178         def blueButtonPressed(self):
179                 if self.type == EPG_TYPE_MULTI:
180                         self["list"].updateMultiEPG(1)
181
182         def timerAdd(self):
183                 cur = self["list"].getCurrent()
184                 event = cur[0]
185                 serviceref = cur[1]
186                 if event is None:
187                         return
188                 newEntry = RecordTimerEntry(serviceref, checkOldTimers = True, *parseEvent(event))
189                 self.session.openWithCallback(self.finishedAdd, TimerEntry, newEntry)
190
191         def finishedAdd(self, answer):
192                 print "finished add"
193                 if answer[0]:
194                         entry = answer[1]
195                         simulTimerList = self.session.nav.RecordTimer.record(entry)
196                         if simulTimerList is not None:
197                                 if (len(simulTimerList) == 2) and (simulTimerList[1].dontSave) and (simulTimerList[1].autoincrease):
198                                         simulTimerList[1].end = entry.begin - 30
199                                         self.session.nav.RecordTimer.timeChanged(simulTimerList[1])
200                                         self.session.nav.RecordTimer.record(entry)
201                                 else:
202                                         self.session.openWithCallback(self.finishSanityCorrection, TimerSanityConflict, simulTimerList)
203                 else:
204                         print "Timeredit aborted"               
205         
206         def finishSanityCorrection(self, answer):
207                 self.finishedAdd(answer)
208
209         def moveUp(self):
210                 self["list"].moveUp()
211
212         def moveDown(self):
213                 self["list"].moveDown()
214
215         def applyButtonState(self, state):
216                 if state == 0:
217                         self["now_button"].hide()
218                         self["now_button_sel"].hide()
219                         self["next_button"].hide()
220                         self["next_button_sel"].hide()
221                         self["more_button"].hide()
222                         self["more_button_sel"].hide()
223                         self["now_text"].hide()
224                         self["next_text"].hide()
225                         self["more_text"].hide()
226                         self["key_red"].setText("")
227                 else:
228                         if state == 1:
229                                 self["key_red"].setText("Zap")
230                                 self["now_button_sel"].show()
231                                 self["now_button"].hide()
232                         else:
233                                 self["now_button"].show()
234                                 self["now_button_sel"].hide()
235                                 self["key_red"].setText("")
236
237                         if state == 2:
238                                 self["next_button_sel"].show()
239                                 self["next_button"].hide()
240                         else:
241                                 self["next_button"].show()
242                                 self["next_button_sel"].hide()
243
244                         if state == 3:
245                                 self["more_button_sel"].show()
246                                 self["more_button"].hide()
247                         else:
248                                 self["more_button"].show()
249                                 self["more_button_sel"].hide()
250
251         def onSelectionChanged(self):
252                 if self.type == EPG_TYPE_MULTI:
253                         count = self["list"].getCurrentChangeCount()
254                         if self.ask_time != -1:
255                                 self.applyButtonState(0)
256                         elif count > 1:
257                                 self.applyButtonState(3)
258                         elif count > 0:
259                                 self.applyButtonState(2)
260                         else:
261                                 self.applyButtonState(1)
262                         days = [ _("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat"), _("Sun") ]
263                         datastr = ""
264                         event = self["list"].getCurrent()[0]
265                         if event is not None:
266                                 now = time()
267                                 beg = event.getBeginTime()
268                                 nowTime = localtime(now)
269                                 begTime = localtime(beg)
270                                 if nowTime[2] != begTime[2]:
271                                         datestr = '%s %d.%d.'%(days[begTime[6]], begTime[2], begTime[1])
272                                 else:
273                                         datestr = '%s %d.%d.'%(_("Today"), begTime[2], begTime[1])
274                         self["date"].setText(datestr)