show similar broadcastings deferred in eventview
[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.timer = timer
49                 self.onSelChanged = [ ]
50                 if selChangedCB is not None:
51                         self.onSelChanged.append(selChangedCB)
52                 GUIComponent.__init__(self)
53                 self.type=type
54                 if type == EPG_TYPE_SINGLE and SINGLE_CPP > 0:
55                         self.l = eListboxEPGContent()
56                 else:
57                         self.l = eListboxPythonMultiContent()
58                         if type == EPG_TYPE_SINGLE:
59                                 self.l.setBuildFunc(self.buildSingleEntry)
60                         elif type == EPG_TYPE_MULTI:
61                                 self.l.setBuildFunc(self.buildMultiEntry)
62                         else:
63                                 assert(type == EPG_TYPE_SIMILAR)
64                                 self.l.setBuildFunc(self.buildSimilarEntry)
65                 self.epgcache = eEPGCache.getInstance()
66
67         def getEventFromId(self, service, eventid):
68                 event = None
69                 if self.epgcache is not None and eventid is not None:
70                         event = self.epgcache.lookupEventId(service.ref, eventid)
71                 return event
72
73         def getCurrentChangeCount(self):
74                 if self.type == EPG_TYPE_SINGLE:
75                         return 0
76                 return self.l.getCurrentSelection()[0]
77
78         def getCurrent(self):
79                 if self.type == EPG_TYPE_SINGLE:
80                         if SINGLE_CPP > 0:
81                                 evt = self.l.getCurrent()
82                         else:
83                                 cur = self.l.getCurrentSelection()
84                                 if cur is None:
85                                         return cur
86                                 else:
87                                         evt = self.getEventFromId(self.service, cur[0])
88                 elif self.type == EPG_TYPE_MULTI:
89                         tmp = self.l.getCurrentSelection()
90                         if tmp is None:
91                                 return ( None, None )
92                         eventid = tmp[2]
93                         service = ServiceReference(tmp[1])
94                         event = self.getEventFromId(service, eventid)
95                         evt = ( event, service )
96                 else:
97                         tmp = self.l.getCurrentSelection()
98                         if tmp is None:
99                                 return ( None, None )
100                         eventid = tmp[1]
101                         service = ServiceReference(tmp[0])
102                         event = self.getEventFromId(service, eventid)
103                         evt = ( event, service )
104                 return evt
105
106         def moveUp(self):
107                 self.instance.moveSelection(self.instance.moveUp)
108
109         def moveDown(self):
110                 self.instance.moveSelection(self.instance.moveDown)
111
112         def connectSelectionChanged(func):
113                 if not self.onSelChanged.count(func):
114                         self.onSelChanged.append(func)
115
116         def disconnectSelectionChanged(func):
117                 self.onSelChanged.remove(func)
118
119         def selectionChanged(self):
120                 for x in self.onSelChanged:
121                         if x is not None:
122                                 try:
123                                         x()
124                                 except:
125                                         pass
126
127         def GUIcreate(self, parent):
128                 self.instance = eListbox(parent)
129                 self.instance.setWrapAround(True)
130                 self.instance.selectionChanged.get().append(self.selectionChanged)
131                 self.instance.setContent(self.l)
132                 if SINGLE_CPP > 0:
133                         self.instance.setItemHeight(25)
134
135         def GUIdelete(self):
136                 self.instance = None
137
138         def recalcEntrySize(self):
139                 if SINGLE_CPP == 0:
140                         esize = self.l.getItemSize()
141                         self.l.setFont(0, gFont("Regular", 22))
142                         self.l.setFont(1, gFont("Regular", 16))
143                         width = esize.width()
144                         height = esize.height()
145                         if self.type == EPG_TYPE_SINGLE:
146                                 w = width/20*5
147                                 self.datetime_rect = Rect(0,0, w, height)
148                                 self.descr_rect = Rect(w, 0, width/20*15, height)
149                         elif self.type == EPG_TYPE_MULTI:
150                                 xpos = 0;
151                                 w = width/10*3;
152                                 self.service_rect = Rect(xpos, 0, w-10, height)
153                                 xpos += w;
154                                 w = width/10*2;
155                                 self.start_end_rect = Rect(xpos, 0, w-10, height)
156                                 self.progress_rect = Rect(xpos, 4, w-10, height-8)
157                                 xpos += w
158                                 w = width/10*5;
159                                 self.descr_rect = Rect(xpos, 0, width, height)
160                         else: # EPG_TYPE_SIMILAR
161                                 xpos = 0;
162                                 w = width/10*3;
163                                 self.datetime_rect = Rect(xpos, 0, w-10, height)
164                                 xpos += w
165                                 w = width/10*7;
166                                 self.service_rect = Rect(xpos, 0, w-10, height)
167
168         def buildSingleEntry(self, eventId, beginTime, duration, EventName):
169                 rec=(self.timer.isInTimer(eventId, beginTime, duration, self.service) > ((duration/10)*8)) 
170                 r1=self.datetime_rect
171                 r2=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_LEFT, "%02d.%02d, %02d:%02d"%(t[2],t[1],t[3],t[4])))
175                 if rec:
176                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r2.left(), r2.top(), 21, 21, loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, 'epgclock-fs8.png'))))
177                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r2.left() + 25, r2.top(), r2.width(), r2.height(), 0, RT_HALIGN_LEFT, EventName))
178                 else:
179                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r2.left(), r2.top(), r2.width(), r2.height(), 0, RT_HALIGN_LEFT, EventName))
180                 return res
181
182         def buildSimilarEntry(self, service, eventId, beginTime, service_name):
183                 r1=self.datetime_rect
184                 r2=self.service_rect
185                 res = [ None ]  # no private data needed
186                 t = localtime(beginTime)
187                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_LEFT, "%02d.%02d, %02d:%02d"%(t[2],t[1],t[3],t[4])))
188                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r2.left(), r2.top(), r2.width(), r2.height(), 0, RT_HALIGN_LEFT, service_name))
189                 return res
190
191         def buildMultiEntry(self, changecount, service, eventId, begTime, duration, EventName, nowTime, service_name):
192                 rec=begTime and (self.timer.isInTimer(eventId, begTime, duration, service) > ((duration/10)*8))
193                 sname = service_name
194                 r1=self.service_rect
195                 r2=self.progress_rect
196                 r3=self.descr_rect
197                 r4=self.start_end_rect
198                 res = [ None ] # no private data needed
199                 re = compile('\xc2\x86.*?\xc2\x87')
200                 list = re.findall(sname)
201                 if len(list):
202                         sname=''
203                         for substr in list:
204                                 sname+=substr[2:len(substr)-2]
205                         if len(sname) == 0:
206                                 sname = service_name;
207                 if rec:
208                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width()-21, r1.height(), 0, RT_HALIGN_LEFT, sname))
209                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r1.left()+r1.width()-16, r1.top(), 21, 21, loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, 'epgclock-fs8.png'))))
210                 else:
211                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_LEFT, sname))
212                 if begTime is not None:
213                         if nowTime < begTime:
214                                 begin = localtime(begTime)
215                                 end = localtime(begTime+duration)
216 #                               print "begin", begin
217 #                               print "end", end
218                                 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])));
219                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
220                         else:
221                                 percent = (nowTime - begTime) * 100 / duration
222                                 res.append((eListboxPythonMultiContent.TYPE_PROGRESS, r2.left(), r2.top(), r2.width(), r2.height(), percent));
223                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
224                 return res
225
226         def queryEPG(self, list, buildFunc=None):
227                 if self.epgcache is not None:
228                         if buildFunc is not None:
229                                 return self.epgcache.lookupEvent(list, buildFunc)
230                         else:
231                                 return self.epgcache.lookupEvent(list)
232                 return [ ]
233
234         def fillMultiEPG(self, services):
235                 t = time()
236                 test = [ '0RIBDTCN' ]
237                 for service in services:
238                         tuple = ( service.ref.toString(), 0 )
239                         test.append( tuple )
240                 self.list = self.queryEPG(test)
241                 self.l.setList(self.list)
242                 print time() - t
243                 self.selectionChanged()
244
245         def updateMultiEPG(self, direction):
246                 t = time()
247                 test = [ 'RIBDTCN' ]
248                 for x in self.list:
249                         service = x[1]
250                         begTime = x[3]
251                         duration = x[4]
252                         if begTime is None:
253                                 begTime = 0
254                         test.append((service, direction, begTime))
255                 tmp = self.queryEPG(test)
256                 cnt=0
257                 for x in tmp:
258                         changecount = self.list[cnt][0] + direction
259                         if changecount >= 0:
260                                 if x[2] is not None:
261                                         self.list[cnt]=(changecount, x[0], x[1], x[2], x[3], x[4], x[5], x[6])
262                         cnt+=1
263                 self.l.setList(self.list)
264                 print time() - t
265                 self.selectionChanged()
266
267         def fillSingleEPG(self, service):
268                 t = time()
269                 if SINGLE_CPP > 0:
270                         self.l.setRoot(service.ref)
271                 else:
272                         self.service = service
273                         test = [ 'IBDT', (service.ref.toString(), 0, -1, -1) ]
274                         self.l.setList(self.queryEPG(test))
275                 print time() - t
276                 self.selectionChanged()
277
278         def sort_func(self,x,y):
279                 if x[2] < y[2]:
280                         return -1
281                 elif x[2] == y[2]:
282                         return 0
283                 else:
284                         return 1
285
286         def fillSimilarList(self, refstr, event_id):
287                 t = time()
288          # search similar broadcastings
289                 if event_id is None:
290                         return
291                 l = self.epgcache.search(('RIBN', 1024, eEPGCache.SIMILAR_BROADCASTINGS_SEARCH, refstr, event_id))
292                 l.sort(self.sort_func)
293                 self.l.setList(l)
294                 self.selectionChanged()
295                 print time() - t