display clock png in single epg list for fitting timers (we have a problem with the...
[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
13 RT_HALIGN_LEFT = 0
14 RT_HALIGN_RIGHT = 1
15 RT_HALIGN_CENTER = 2
16 RT_HALIGN_BLOCK = 4
17
18 RT_VALIGN_TOP = 0
19 RT_VALIGN_CENTER = 8
20 RT_VALIGN_BOTTOM = 16
21
22 RT_WRAP = 32
23
24 SINGLE_CPP = 0
25
26 class Rect:
27         def __init__(self, x, y, width, height):
28                 self.__left = x
29                 self.__top = y
30                 self.__width = width
31                 self.__height = height
32
33         def left(self):
34                 return self.__left
35
36         def top(self):
37                 return self.__top
38
39         def height(self):
40                 return self.__height
41
42         def width(self):
43                 return self.__width
44
45 class EPGList(HTMLComponent, GUIComponent):
46         def __init__(self, type=EPG_TYPE_SINGLE, selChangedCB=None, timer = None):
47                 self.timer = timer
48                 self.onSelChanged = [ ]
49                 if selChangedCB is not None:
50                         self.onSelChanged.append(selChangedCB)
51                 GUIComponent.__init__(self)
52                 self.type=type
53                 if type == EPG_TYPE_SINGLE and SINGLE_CPP > 0:
54                         self.l = eListboxEPGContent()
55                 else:
56                         self.l = eListboxPythonMultiContent()
57                 self.epgcache = eEPGCache.getInstance()
58
59         def getEventFromId(self, service, eventid):
60                 event = None
61                 if self.epgcache is not None and eventid is not None:
62                         event = self.epgcache.lookupEventId(service.ref, eventid)
63                 return event
64
65         def getCurrentChangeCount(self):
66                 if self.type == EPG_TYPE_SINGLE:
67                         return 0
68                 return self.l.getCurrentSelection()[0][0]
69
70         def getCurrent(self):
71                 if self.type == EPG_TYPE_SINGLE:
72                         if SINGLE_CPP > 0:
73                                 evt = self.l.getCurrent()
74                         else:
75                                 eventid = self.l.getCurrentSelection()[0]
76                                 evt = self.getEventFromId(self.service, eventid)
77                 else:
78                         tmp = self.l.getCurrentSelection()[0]
79                         eventid = tmp[1]
80                         service = ServiceReference(tmp[2])
81                         event = self.getEventFromId(service, eventid)
82                         evt = ( event, service )
83                 return evt
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                                 try:
102                                         x()
103                                 except:
104                                         pass
105
106         def GUIcreate(self, parent):
107                 self.instance = eListbox(parent)
108                 self.instance.setWrapAround(True)
109                 self.instance.selectionChanged.get().append(self.selectionChanged)
110                 self.instance.setContent(self.l)
111                 if SINGLE_CPP > 0:
112                         self.instance.setItemHeight(25)
113
114         def GUIdelete(self):
115                 self.instance = None
116
117         def recalcEntrySize(self):
118                 if SINGLE_CPP == 0:
119                         esize = self.l.getItemSize()
120                         self.l.setFont(0, gFont("Regular", 22))
121                         self.l.setFont(1, gFont("Regular", 16))
122                         width = esize.width()
123                         height = esize.height()
124                         if self.type == EPG_TYPE_SINGLE:
125                                 w = width/20*5
126                                 self.datetime_rect = Rect(0,0, w, height)
127                                 self.descr_rect = Rect(w, 0, width/20*15, height)
128                         else:
129                                 xpos = 0;
130                                 w = width/10*3;
131                                 self.service_rect = Rect(xpos, 0, w-10, height)
132                                 xpos += w;
133                                 w = width/10*2;
134                                 self.start_end_rect = Rect(xpos, 0, w-10, height)
135                                 self.progress_rect = Rect(xpos, 4, w-10, height-8)
136                                 xpos += w
137                                 w = width/10*5;
138                                 self.descr_rect = Rect(xpos, 0, width, height)
139
140         def buildSingleEntry(self, eventId, beginTime, duration, EventName, rec=False):
141                 r1=self.datetime_rect
142                 r2=self.descr_rect
143                 res = [ eventId ]
144                 t = localtime(beginTime)
145                 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])))
146                 if rec:
147                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r2.left(), r2.top(), 21, 21, loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, 'epgclock-fs8.png'))))
148                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r2.left() + 25, r2.top(), r2.width(), r2.height(), 0, RT_HALIGN_LEFT, EventName))
149                 else:
150                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r2.left(), r2.top(), r2.width(), r2.height(), 0, RT_HALIGN_LEFT, EventName))
151                 return res
152
153         def buildMultiEntry(self, changecount, service, eventId, begTime, duration, EventName, nowTime, service_name):
154                 sname = service_name
155                 r1=self.service_rect
156                 r2=self.progress_rect
157                 r3=self.descr_rect
158                 r4=self.start_end_rect
159                 res = [ (changecount, eventId, service, begTime, duration) ]
160                 re = compile('\xc2\x86.*?\xc2\x87')
161                 list = re.findall(sname)
162                 if len(list):
163                         sname=''
164                         for substr in list:
165                                 sname+=substr[2:len(substr)-2]
166                         if len(sname) == 0:
167                                 sname = service_name;
168                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_LEFT, sname))
169                 if begTime is not None:
170                         if nowTime < begTime:
171                                 begin = localtime(begTime)
172                                 end = localtime(begTime+duration)
173 #                               print "begin", begin
174 #                               print "end", end
175                                 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])));
176                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
177                         else:
178                                 percent = (nowTime - begTime) * 100 / duration
179                                 res.append((eListboxPythonMultiContent.TYPE_PROGRESS, r2.left(), r2.top(), r2.width(), r2.height(), percent));
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 queryEPG(self, list, buildFunc=None):
184                 if self.epgcache is not None:
185                         if buildFunc is not None:
186                                 return self.epgcache.lookupEvent(list, buildFunc)
187                         else:
188                                 return self.epgcache.lookupEvent(list)
189                 return [ ]
190
191         def fillMultiEPG(self, services):
192                 t = time()
193                 test = [ 'RIBDTCN' ]
194                 for service in services:
195                         tuple = ( service.ref.toString(), 0 )
196                         test.append( tuple )
197 #               self.list = self.queryEPG(test, self.buildMultiEntry)
198                 tmp = self.queryEPG(test)
199                 self.list = [ ]
200                 for x in tmp:
201                         self.list.append(self.buildMultiEntry(0, x[0], x[1], x[2], x[3], x[4], x[5], x[6]))
202                 self.l.setList(self.list)
203                 print time() - t
204                 self.selectionChanged()
205
206         def updateMultiEPG(self, direction):
207                 t = time()
208                 test = [ 'RIBDTCN' ]
209                 for x in self.list:
210                         data = x[0]
211                         service = data[2]
212                         begTime = data[3]
213                         duration = data[4]
214                         if begTime is None:
215                                 begTime = 0
216                         test.append((service, direction, begTime))
217 #               self.list = self.queryEPG(test, self.buildMultiEntry)
218                 tmp = self.queryEPG(test)
219                 cnt=0
220                 for x in tmp:
221                         changecount = self.list[cnt][0][0] + direction
222                         if changecount >= 0:
223                                 if x[2] is not None:
224                                         self.list[cnt]=self.buildMultiEntry(changecount, x[0], x[1], x[2], x[3], x[4], x[5], x[6])
225                         cnt+=1
226                 self.l.setList(self.list)
227                 print time() - t
228                 self.selectionChanged()
229
230         def fillSingleEPG(self, service):
231                 t = time()
232                 if SINGLE_CPP > 0:
233                         self.l.setRoot(service.ref)
234                 else:
235                         self.service = service
236                         test = [ 'IBDT', (service.ref.toString(), 0, -1, -1) ]
237 #                       self.list = self.queryEPG(test, self.buildSingleEntry)
238                         tmp = self.queryEPG(test)
239                         self.list = [ ]
240                         for x in tmp:
241                                 self.list.append(self.buildSingleEntry(x[0], x[1], x[2], x[3], (self.timer.isInTimer(eventid=x[0], begin=x[1], duration=x[2], service=service) > 0)))
242 #                               self.list.append(self.buildSingleEntry(refstr, x[0], x[1], x[2], x[3]))
243                         self.l.setList(self.list)
244                 print time() - t
245                 self.selectionChanged()