2494ca0808b30c30f11d9679dd0e3b58b1bae81e
[enigma2.git] / lib / python / Components / EpgList.py
1 from HTMLComponent import HTMLComponent
2 from GUIComponent import GUIComponent
3
4 from enigma import eEPGCache, eListbox, eListboxPythonMultiContent, gFont, \
5         RT_HALIGN_LEFT, RT_HALIGN_RIGHT, RT_HALIGN_CENTER, RT_VALIGN_CENTER
6
7 from Tools.LoadPixmap import LoadPixmap
8
9 from time import localtime, time
10 from ServiceReference import ServiceReference
11 from Tools.Directories import resolveFilename, SCOPE_SKIN_IMAGE
12
13 EPG_TYPE_SINGLE = 0
14 EPG_TYPE_MULTI = 1
15 EPG_TYPE_SIMILAR = 2
16
17 class Rect:
18         def __init__(self, x, y, width, height):
19                 self.__left = x
20                 self.__top = y
21                 self.__width = width
22                 self.__height = height
23
24         def left(self):
25                 return self.__left
26
27         def top(self):
28                 return self.__top
29
30         def height(self):
31                 return self.__height
32
33         def width(self):
34                 return self.__width
35
36 class EPGList(HTMLComponent, GUIComponent):
37         def __init__(self, type=EPG_TYPE_SINGLE, selChangedCB=None, timer = None):
38                 self.days = (_("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat"), _("Sun"))
39                 self.timer = timer
40                 self.onSelChanged = [ ]
41                 if selChangedCB is not None:
42                         self.onSelChanged.append(selChangedCB)
43                 GUIComponent.__init__(self)
44                 self.type=type
45                 self.l = eListboxPythonMultiContent()
46                 self.l.setFont(0, gFont("Regular", 22))
47                 self.l.setFont(1, gFont("Regular", 16))
48                 if type == EPG_TYPE_SINGLE:
49                         self.l.setBuildFunc(self.buildSingleEntry)
50                 elif type == EPG_TYPE_MULTI:
51                         self.l.setBuildFunc(self.buildMultiEntry)
52                 else:
53                         assert(type == EPG_TYPE_SIMILAR)
54                         self.l.setBuildFunc(self.buildSimilarEntry)
55                 self.epgcache = eEPGCache.getInstance()
56                 self.clock_pixmap = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, 'skin_default/icons/epgclock.png'))
57                 self.clock_add_pixmap = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, 'skin_default/icons/epgclock_add.png'))
58                 self.clock_pre_pixmap = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, 'skin_default/icons/epgclock_pre.png'))
59                 self.clock_post_pixmap = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, 'skin_default/icons/epgclock_post.png'))
60                 self.clock_prepost_pixmap = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, 'skin_default/icons/epgclock_prepost.png'))
61
62         def getEventFromId(self, service, eventid):
63                 event = None
64                 if self.epgcache is not None and eventid is not None:
65                         event = self.epgcache.lookupEventId(service.ref, eventid)
66                 return event
67
68         def getCurrentChangeCount(self):
69                 if self.type == EPG_TYPE_MULTI and self.l.getCurrentSelection() is not None:
70                         return self.l.getCurrentSelection()[0]
71                 return 0
72
73         def getCurrent(self):
74                 idx=0
75                 if self.type == EPG_TYPE_MULTI:
76                         idx += 1
77                 tmp = self.l.getCurrentSelection()
78                 if tmp is None:
79                         return ( None, None )
80                 eventid = tmp[idx+1]
81                 service = ServiceReference(tmp[idx])
82                 event = self.getEventFromId(service, eventid)
83                 return ( event, service )
84
85         def moveUp(self):
86                 self.instance.moveSelection(self.instance.moveUp)
87
88         def moveDown(self):
89                 self.instance.moveSelection(self.instance.moveDown)
90
91         def connectSelectionChanged(func):
92                 if not self.onSelChanged.count(func):
93                         self.onSelChanged.append(func)
94
95         def disconnectSelectionChanged(func):
96                 self.onSelChanged.remove(func)
97
98         def selectionChanged(self):
99                 for x in self.onSelChanged:
100                         if x is not None:
101                                 x()
102 #                               try:
103 #                                       x()
104 #                               except: # FIXME!!!
105 #                                       print "FIXME in EPGList.selectionChanged"
106 #                                       pass
107
108         GUI_WIDGET = eListbox
109
110         def postWidgetCreate(self, instance):
111                 instance.setWrapAround(True)
112                 instance.selectionChanged.get().append(self.selectionChanged)
113                 instance.setContent(self.l)
114
115         def preWidgetRemove(self, instance):
116                 instance.selectionChanged.get().remove(self.selectionChanged)
117                 instance.setContent(None)
118
119         def recalcEntrySize(self):
120                 esize = self.l.getItemSize()
121                 width = esize.width()
122                 height = esize.height()
123                 if self.type == EPG_TYPE_SINGLE:
124                         self.weekday_rect = Rect(0, 0, width/20*2-10, height)
125                         self.datetime_rect = Rect(width/20*2, 0, width/20*5-15, height)
126                         self.descr_rect = Rect(width/20*7, 0, width/20*13, height)
127                 elif self.type == EPG_TYPE_MULTI:
128                         xpos = 0;
129                         w = width/10*3;
130                         self.service_rect = Rect(xpos, 0, w-10, height)
131                         xpos += w;
132                         w = width/10*2;
133                         self.start_end_rect = Rect(xpos, 0, w-10, height)
134                         self.progress_rect = Rect(xpos, 4, w-10, height-8)
135                         xpos += w
136                         w = width/10*5;
137                         self.descr_rect = Rect(xpos, 0, width, height)
138                 else: # EPG_TYPE_SIMILAR
139                         self.weekday_rect = Rect(0, 0, width/20*2-10, height)
140                         self.datetime_rect = Rect(width/20*2, 0, width/20*5-15, height)
141                         self.service_rect = Rect(width/20*7, 0, width/20*13, height)
142
143         def getClockPixmap(self, refstr, beginTime, duration, eventId):
144                 pre_clock = 1
145                 post_clock = 2
146                 clock_type = 0
147                 endTime = beginTime + duration
148                 for x in self.timer.timer_list:
149                         if x.service_ref.ref.toString() == refstr:
150                                 if x.eit == eventId:
151                                         return self.clock_pixmap
152                                 beg = x.begin
153                                 end = x.end
154                                 if beginTime > beg and beginTime < end and endTime > end:
155                                         clock_type |= pre_clock
156                                 elif beginTime < beg and endTime > beg and endTime < end:
157                                         clock_type |= post_clock
158                 if clock_type == 0:
159                         return self.clock_add_pixmap
160                 elif clock_type == pre_clock:
161                         return self.clock_pre_pixmap
162                 elif clock_type == post_clock:
163                         return self.clock_post_pixmap
164                 else:
165                         return self.clock_prepost_pixmap
166
167         def buildSingleEntry(self, service, eventId, beginTime, duration, EventName):
168                 rec=beginTime and (self.timer.isInTimer(eventId, beginTime, duration, service))
169                 r1=self.weekday_rect
170                 r2=self.datetime_rect
171                 r3=self.descr_rect
172                 t = localtime(beginTime)
173                 res = [
174                         None, # no private data needed
175                         (eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]),
176                         (eListboxPythonMultiContent.TYPE_TEXT, r2.left(), r2.top(), r2.width(), r1.height(), 0, RT_HALIGN_RIGHT, "%02d.%02d, %02d:%02d"%(t[2],t[1],t[3],t[4]))
177                 ]
178                 if rec:
179                         clock_pic = self.getClockPixmap(service, beginTime, duration, eventId)
180                         res.extend((
181                                 (eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, clock_pic),
182                                 (eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName)
183                         ))
184                 else:
185                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
186                 return res
187
188         def buildSimilarEntry(self, service, eventId, beginTime, service_name, duration):
189                 rec=beginTime and (self.timer.isInTimer(eventId, beginTime, duration, service))
190                 r1=self.weekday_rect
191                 r2=self.datetime_rect
192                 r3=self.service_rect
193                 t = localtime(beginTime)
194                 res = [
195                         None,  # no private data needed
196                         (eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]),
197                         (eListboxPythonMultiContent.TYPE_TEXT, r2.left(), r2.top(), r2.width(), r1.height(), 0, RT_HALIGN_RIGHT, "%02d.%02d, %02d:%02d"%(t[2],t[1],t[3],t[4]))
198                 ]
199                 if rec:
200                         clock_pic = self.getClockPixmap(service, beginTime, duration, eventId)
201                         res.extend((
202                                 (eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, clock_pic),
203                                 (eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name)
204                         ))
205                 else:
206                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name))
207                 return res
208
209         def buildMultiEntry(self, changecount, service, eventId, begTime, duration, EventName, nowTime, service_name):
210                 rec=begTime and (self.timer.isInTimer(eventId, begTime, duration, service))
211                 r1=self.service_rect
212                 r2=self.progress_rect
213                 r3=self.descr_rect
214                 r4=self.start_end_rect
215                 res = [ None ] # no private data needed
216                 if rec:
217                         clock_pic = self.getClockPixmap(service, begTime, duration, eventId)
218                         res.extend((
219                                 (eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width()-21, r1.height(), 0, RT_HALIGN_LEFT, service_name),
220                                 (eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r1.left()+r1.width()-16, r1.top(), 21, 21, clock_pic)
221                         ))
222                 else:
223                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_LEFT, service_name))
224                 if begTime is not None:
225                         if nowTime < begTime:
226                                 begin = localtime(begTime)
227                                 end = localtime(begTime+duration)
228 #                               print "begin", begin
229 #                               print "end", end
230                                 res.extend((
231                                         (eListboxPythonMultiContent.TYPE_TEXT, r4.left(), r4.top(), r4.width(), r4.height(), 1, RT_HALIGN_CENTER|RT_VALIGN_CENTER, "%02d.%02d - %02d.%02d"%(begin[3],begin[4],end[3],end[4])),
232                                         (eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName)
233                                 ))
234                         else:
235                                 percent = (nowTime - begTime) * 100 / duration
236                                 res.extend((
237                                         (eListboxPythonMultiContent.TYPE_PROGRESS, r2.left(), r2.top(), r2.width(), r2.height(), percent),
238                                         (eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName)
239                                 ))
240                 return res
241
242         def queryEPG(self, list, buildFunc=None):
243                 if self.epgcache is not None:
244                         if buildFunc is not None:
245                                 return self.epgcache.lookupEvent(list, buildFunc)
246                         else:
247                                 return self.epgcache.lookupEvent(list)
248                 return [ ]
249
250         def fillMultiEPG(self, services, stime=-1):
251                 #t = time()
252                 test = [ (service.ref.toString(), 0, stime) for service in services ]
253                 test.insert(0, 'X0RIBDTCn')
254                 self.list = self.queryEPG(test)
255                 self.l.setList(self.list)
256                 #print time() - t
257                 self.selectionChanged()
258
259         def updateMultiEPG(self, direction):
260                 #t = time()
261                 test = [ x[3] and (x[1], direction, x[3]) or (x[1], direction, 0) for x in self.list ]
262                 test.insert(0, 'XRIBDTCn')
263                 tmp = self.queryEPG(test)
264                 cnt=0
265                 for x in tmp:
266                         changecount = self.list[cnt][0] + direction
267                         if changecount >= 0:
268                                 if x[2] is not None:
269                                         self.list[cnt]=(changecount, x[0], x[1], x[2], x[3], x[4], x[5], x[6])
270                         cnt+=1
271                 self.l.setList(self.list)
272                 #print time() - t
273                 self.selectionChanged()
274
275         def fillSingleEPG(self, service):
276                 #t = time()
277                 test = [ 'RIBDT', (service.ref.toString(), 0, -1, -1) ]
278                 self.list = self.queryEPG(test)
279                 self.l.setList(self.list)
280                 #print time() - t
281                 self.selectionChanged()
282
283         def sortSingleEPG(self, type):
284                 list = self.list
285                 if list:
286                         event_id = self.getSelectedEventId()
287                         if type == 1:
288                                 list.sort(key=lambda x: (x[4] and x[4].lower(), x[2]))
289                         else:
290                                 assert(type == 0)
291                                 list.sort(key=lambda x: x[2])
292                         self.l.invalidate()
293                         self.moveToEventId(event_id)
294
295         def getSelectedEventId(self):
296                 x = self.l.getCurrentSelection()
297                 return x and x[1]
298
299         def moveToService(self,serviceref):
300                 index = 0
301                 refstr = serviceref.toString()
302                 for x in self.list:
303                         if x[1] == refstr:
304                                 self.instance.moveSelectionTo(index)
305                                 break
306                         index += 1
307                         
308         def moveToEventId(self, eventId):
309                 index = 0
310                 for x in self.list:
311                         if x[1] == eventId:
312                                 self.instance.moveSelectionTo(index)
313                                 break
314                         index += 1
315
316         def fillSimilarList(self, refstr, event_id):
317                 t = time()
318          # search similar broadcastings
319                 if event_id is None:
320                         return
321                 l = self.epgcache.search(('RIBND', 1024, eEPGCache.SIMILAR_BROADCASTINGS_SEARCH, refstr, event_id))
322                 if l and len(l):
323                         l.sort(key=lambda x: x[2])
324                 self.l.setList(l)
325                 self.selectionChanged()
326                 print time() - t