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