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