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