fix return of actionmap
[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                         if type == EPG_TYPE_SINGLE:
58                                 self.l.setBuildFunc(self.buildSingleEntry)
59                         else:
60                                 self.l.setBuildFunc(self.buildMultiEntry)
61                 self.epgcache = eEPGCache.getInstance()
62
63         def getEventFromId(self, service, eventid):
64                 event = None
65                 if self.epgcache is not None and eventid is not None:
66                         event = self.epgcache.lookupEventId(service.ref, eventid)
67                 return event
68
69         def getCurrentChangeCount(self):
70                 if self.type == EPG_TYPE_SINGLE:
71                         return 0
72                 return self.l.getCurrentSelection()[0]
73
74         def getCurrent(self):
75                 if self.type == EPG_TYPE_SINGLE:
76                         if SINGLE_CPP > 0:
77                                 evt = self.l.getCurrent()
78                         else:
79                                 eventid = self.l.getCurrentSelection()[0]
80                                 evt = self.getEventFromId(self.service, eventid)
81                 else:
82                         tmp = self.l.getCurrentSelection()
83                         eventid = tmp[2]
84                         service = ServiceReference(tmp[1])
85                         event = self.getEventFromId(service, eventid)
86                         evt = ( event, service )
87                 return evt
88
89         def moveUp(self):
90                 self.instance.moveSelection(self.instance.moveUp)
91
92         def moveDown(self):
93                 self.instance.moveSelection(self.instance.moveDown)
94
95         def connectSelectionChanged(func):
96                 if not self.onSelChanged.count(func):
97                         self.onSelChanged.append(func)
98
99         def disconnectSelectionChanged(func):
100                 self.onSelChanged.remove(func)
101
102         def selectionChanged(self):
103                 for x in self.onSelChanged:
104                         if x is not None:
105                                 try:
106                                         x()
107                                 except:
108                                         pass
109
110         def GUIcreate(self, parent):
111                 self.instance = eListbox(parent)
112                 self.instance.setWrapAround(True)
113                 self.instance.selectionChanged.get().append(self.selectionChanged)
114                 self.instance.setContent(self.l)
115                 if SINGLE_CPP > 0:
116                         self.instance.setItemHeight(25)
117
118         def GUIdelete(self):
119                 self.instance = None
120
121         def recalcEntrySize(self):
122                 if SINGLE_CPP == 0:
123                         esize = self.l.getItemSize()
124                         self.l.setFont(0, gFont("Regular", 22))
125                         self.l.setFont(1, gFont("Regular", 16))
126                         width = esize.width()
127                         height = esize.height()
128                         if self.type == EPG_TYPE_SINGLE:
129                                 w = width/20*5
130                                 self.datetime_rect = Rect(0,0, w, height)
131                                 self.descr_rect = Rect(w, 0, width/20*15, height)
132                         else:
133                                 xpos = 0;
134                                 w = width/10*3;
135                                 self.service_rect = Rect(xpos, 0, w-10, height)
136                                 xpos += w;
137                                 w = width/10*2;
138                                 self.start_end_rect = Rect(xpos, 0, w-10, height)
139                                 self.progress_rect = Rect(xpos, 4, w-10, height-8)
140                                 xpos += w
141                                 w = width/10*5;
142                                 self.descr_rect = Rect(xpos, 0, width, height)
143
144         def buildSingleEntry(self, eventId, beginTime, duration, EventName):
145                 rec=(self.timer.isInTimer(eventid=eventId, begin=beginTime, duration=duration, service=self.service) > 0)
146                 r1=self.datetime_rect
147                 r2=self.descr_rect
148                 res = [ None ]  # no private data needed
149                 t = localtime(beginTime)
150                 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])))
151                 if rec:
152                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r2.left(), r2.top(), 21, 21, loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, 'epgclock-fs8.png'))))
153                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r2.left() + 25, r2.top(), r2.width(), r2.height(), 0, RT_HALIGN_LEFT, EventName))
154                 else:
155                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r2.left(), r2.top(), r2.width(), r2.height(), 0, RT_HALIGN_LEFT, EventName))
156                 return res
157
158         def buildMultiEntry(self, changecount, service, eventId, begTime, duration, EventName, nowTime, service_name):
159                 sname = service_name
160                 r1=self.service_rect
161                 r2=self.progress_rect
162                 r3=self.descr_rect
163                 r4=self.start_end_rect
164                 res = [ None ] # no private data needed
165                 re = compile('\xc2\x86.*?\xc2\x87')
166                 list = re.findall(sname)
167                 if len(list):
168                         sname=''
169                         for substr in list:
170                                 sname+=substr[2:len(substr)-2]
171                         if len(sname) == 0:
172                                 sname = service_name;
173                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_LEFT, sname))
174                 if begTime is not None:
175                         if nowTime < begTime:
176                                 begin = localtime(begTime)
177                                 end = localtime(begTime+duration)
178 #                               print "begin", begin
179 #                               print "end", end
180                                 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])));
181                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
182                         else:
183                                 percent = (nowTime - begTime) * 100 / duration
184                                 res.append((eListboxPythonMultiContent.TYPE_PROGRESS, r2.left(), r2.top(), r2.width(), r2.height(), percent));
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 queryEPG(self, list, buildFunc=None):
189                 if self.epgcache is not None:
190                         if buildFunc is not None:
191                                 return self.epgcache.lookupEvent(list, buildFunc)
192                         else:
193                                 return self.epgcache.lookupEvent(list)
194                 return [ ]
195
196         def fillMultiEPG(self, services):
197                 t = time()
198                 test = [ '0RIBDTCN' ]
199                 for service in services:
200                         tuple = ( service.ref.toString(), 0 )
201                         test.append( tuple )
202                 self.list = self.queryEPG(test)
203                 self.l.setList(self.list)
204                 print time() - t
205                 self.selectionChanged()
206
207         def updateMultiEPG(self, direction):
208                 t = time()
209                 test = [ 'RIBDTCN' ]
210                 for x in self.list:
211                         service = x[1]
212                         begTime = x[3]
213                         duration = x[4]
214                         if begTime is None:
215                                 begTime = 0
216                         test.append((service, direction, begTime))
217                 tmp = self.queryEPG(test)
218                 cnt=0
219                 for x in tmp:
220                         changecount = self.list[cnt][0] + direction
221                         if changecount >= 0:
222                                 if x[2] is not None:
223                                         self.list[cnt]=(changecount, x[0], x[1], x[2], x[3], x[4], x[5], x[6])
224                         cnt+=1
225                 self.l.setList(self.list)
226                 print time() - t
227                 self.selectionChanged()
228
229         def fillSingleEPG(self, service):
230                 t = time()
231                 if SINGLE_CPP > 0:
232                         self.l.setRoot(service.ref)
233                 else:
234                         self.service = service
235                         test = [ 'IBDT', (service.ref.toString(), 0, -1, -1) ]
236                         self.l.setList(self.queryEPG(test))
237                 print time() - t
238                 self.selectionChanged()