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