fix warnings
[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 postWidgetCreate(self, instance):
132                 instance.setWrapAround(True)
133                 instance.selectionChanged.get().append(self.selectionChanged)
134                 instance.setContent(self.l)
135                 if SINGLE_CPP > 0:
136                         instance.setItemHeight(25)
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                                 self.weekday_rect = Rect(0, 0, width/20*2-10, height)
147                                 self.datetime_rect = Rect(width/20*2, 0, width/20*5-15, height)
148                                 self.descr_rect = Rect(width/20*7, 0, width/20*13, 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                                 self.weekday_rect = Rect(0, 0, width/20*2-10, height)
162                                 self.datetime_rect = Rect(width/20*2, 0, width/20*5-15, height)
163                                 self.service_rect = Rect(width/20*7, 0, width/20*13, height)
164
165         def buildSingleEntry(self, eventId, beginTime, duration, EventName):
166                 rec=(self.timer.isInTimer(eventId, beginTime, duration, self.service) > ((duration/10)*8)) 
167                 r1=self.weekday_rect
168                 r2=self.datetime_rect
169                 r3=self.descr_rect
170                 res = [ None ]  # no private data needed
171                 t = localtime(beginTime)
172                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]))
173                 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])))
174                 if rec:
175                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, 'epgclock-fs8.png'))))
176                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
177                 else:
178                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
179                 return res
180
181         def buildSimilarEntry(self, service, eventId, beginTime, service_name, duration):
182                 rec=(self.timer.isInTimer(eventId, beginTime, duration, service) > ((duration/10)*8)) 
183                 r1=self.weekday_rect
184                 r2=self.datetime_rect
185                 r3=self.service_rect
186                 res = [ None ]  # no private data needed
187                 t = localtime(beginTime)
188                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]))
189                 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])))
190                 if rec:
191                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, 'epgclock-fs8.png'))))
192                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name))
193                 else:
194                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name))
195                 return res
196
197         def buildMultiEntry(self, changecount, service, eventId, begTime, duration, EventName, nowTime, service_name):
198                 rec=begTime and (self.timer.isInTimer(eventId, begTime, duration, service) > ((duration/10)*8))
199                 sname = service_name
200                 r1=self.service_rect
201                 r2=self.progress_rect
202                 r3=self.descr_rect
203                 r4=self.start_end_rect
204                 res = [ None ] # no private data needed
205                 re = compile('\xc2\x86.*?\xc2\x87')
206                 list = re.findall(sname)
207                 if len(list):
208                         sname=''
209                         for substr in list:
210                                 sname+=substr[2:len(substr)-2]
211                         if len(sname) == 0:
212                                 sname = service_name;
213                 if rec:
214                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width()-21, r1.height(), 0, RT_HALIGN_LEFT, sname))
215                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r1.left()+r1.width()-16, r1.top(), 21, 21, loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, 'epgclock-fs8.png'))))
216                 else:
217                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_LEFT, sname))
218                 if begTime is not None:
219                         if nowTime < begTime:
220                                 begin = localtime(begTime)
221                                 end = localtime(begTime+duration)
222 #                               print "begin", begin
223 #                               print "end", end
224                                 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])));
225                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
226                         else:
227                                 percent = (nowTime - begTime) * 100 / duration
228                                 res.append((eListboxPythonMultiContent.TYPE_PROGRESS, r2.left(), r2.top(), r2.width(), r2.height(), percent));
229                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
230                 return res
231
232         def queryEPG(self, list, buildFunc=None):
233                 if self.epgcache is not None:
234                         if buildFunc is not None:
235                                 return self.epgcache.lookupEvent(list, buildFunc)
236                         else:
237                                 return self.epgcache.lookupEvent(list)
238                 return [ ]
239
240         def fillMultiEPG(self, services, stime=-1):
241                 t = time()
242                 test = [ '0RIBDTCN' ]
243                 for service in services:
244                         tuple = (service.ref.toString(), 0, stime)
245                         test.append( tuple )
246                 self.list = self.queryEPG(test)
247                 self.l.setList(self.list)
248                 print time() - t
249                 self.selectionChanged()
250
251         def updateMultiEPG(self, direction):
252                 t = time()
253                 test = [ 'RIBDTCN' ]
254                 for x in self.list:
255                         service = x[1]
256                         begTime = x[3]
257                         duration = x[4]
258                         if begTime is None:
259                                 begTime = 0
260                         test.append((service, direction, begTime))
261                 tmp = self.queryEPG(test)
262                 cnt=0
263                 for x in tmp:
264                         changecount = self.list[cnt][0] + direction
265                         if changecount >= 0:
266                                 if x[2] is not None:
267                                         self.list[cnt]=(changecount, x[0], x[1], x[2], x[3], x[4], x[5], x[6])
268                         cnt+=1
269                 self.l.setList(self.list)
270                 print time() - t
271                 self.selectionChanged()
272
273         def fillSingleEPG(self, service):
274                 t = time()
275                 if SINGLE_CPP > 0:
276                         self.l.setRoot(service.ref)
277                 else:
278                         self.service = service
279                         test = [ 'IBDT', (service.ref.toString(), 0, -1, -1) ]
280                         self.l.setList(self.queryEPG(test))
281                 print time() - t
282                 self.selectionChanged()
283
284         def sort_func(self,x,y):
285                 if x[2] < y[2]:
286                         return -1
287                 elif x[2] == y[2]:
288                         return 0
289                 else:
290                         return 1
291
292         def fillSimilarList(self, refstr, event_id):
293                 t = time()
294          # search similar broadcastings
295                 if event_id is None:
296                         return
297                 l = self.epgcache.search(('RIBND', 1024, eEPGCache.SIMILAR_BROADCASTINGS_SEARCH, refstr, event_id))
298                 if l and len(l):
299                         l.sort(self.sort_func)
300                 self.l.setList(l)
301                 self.selectionChanged()
302                 print time() - t