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