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