b728879275ccd50d07fa0955edd0b9d020c5e8f0
[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):
46                 GUIComponent.__init__(self)
47                 self.type=type
48                 if type == EPG_TYPE_SINGLE and SINGLE_CPP > 0:
49                         self.l = eListboxEPGContent()
50                 else:
51                         self.l = eListboxPythonMultiContent()
52                 self.epgcache = eEPGCache.getInstance()
53
54         def getEventFromId(self, service, eventid):
55                 event = None
56                 if self.epgcache is not None and eventid is not None:
57                         event = self.epgcache.lookupEventId(service.ref, eventid)
58                 return event
59
60         def getCurrent(self):
61                 if self.type == EPG_TYPE_SINGLE:
62                         if SINGLE_CPP > 0:
63                                 evt = self.l.getCurrent()
64                         else:
65                                 eventid = self.l.getCurrentSelection()[0]
66                                 evt = self.getEventFromId(self.service, eventid)
67                 else:
68                         tmp = self.l.getCurrentSelection()[0]
69                         eventid = tmp[0]
70                         service = ServiceReference(tmp[1])
71                         event = self.getEventFromId(service, eventid)
72                         evt = ( event, service )
73                 return evt
74
75         def moveUp(self):
76                 self.instance.moveSelection(self.instance.moveUp)
77
78         def moveDown(self):
79                 self.instance.moveSelection(self.instance.moveDown)
80
81         def GUIcreate(self, parent):
82                 self.instance = eListbox(parent)
83                 self.instance.setContent(self.l)
84                 if SINGLE_CPP > 0:
85                         self.instance.setItemHeight(25)
86
87         def GUIdelete(self):
88                 self.instance = None
89
90         def recalcEntrySize(self):
91                 t = time()
92                 esize = self.l.getItemSize()
93                 self.l.setFont(0, gFont("Regular", 22))
94                 self.l.setFont(1, gFont("Regular", 16))
95                 width = esize.width()
96                 height = esize.height()
97                 if self.type == EPG_TYPE_SINGLE:
98                         w = width/20*5
99                         self.datetime_rect = Rect(0,0, w, height)
100                         self.descr_rect = Rect(w, 0, width/20*15, height)
101                 else:
102                         xpos = 0;
103                         w = width/10*3;
104                         self.service_rect = Rect(xpos, 0, w-10, height)
105                         xpos += w;
106                         w = width/10*2;
107                         self.start_end_rect = Rect(xpos, 0, w-10, height)
108                         self.progress_rect = Rect(xpos, 4, w-10, height-8)
109                         xpos += w
110                         w = width/10*5;
111                         self.descr_rect = Rect(xpos, 0, width, height)
112                 print "recalcEntrySize", time() - t
113
114         def buildSingleEntry(self, eventId, beginTime, duration, EventName):
115                 r1=self.datetime_rect
116                 r2=self.descr_rect
117                 res = [ eventId ]
118                 t = localtime(beginTime)
119                 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])))
120                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r2.left(), r2.top(), r2.width(), r2.height(), 0, RT_HALIGN_LEFT, EventName))
121                 return res
122
123         def buildMultiEntry(self, service, eventId, begTime, duration, EventName, nowTime, service_name):
124                 sname = service_name
125                 r1=self.service_rect
126                 r2=self.progress_rect
127                 r3=self.descr_rect
128                 r4=self.start_end_rect
129                 res = [ (eventId, service, begTime, duration) ]
130                 re = compile('\xc2\x86.*?\xc2\x87')
131                 list = re.findall(sname)
132                 if len(list):
133                         sname=''
134                         for substr in list:
135                                 sname+=substr[2:len(substr)-2]
136                         if len(sname) == 0:
137                                 sname = service_name;
138                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_LEFT, sname))
139                 if begTime is not None:
140                         if nowTime < begTime:
141                                 begin = localtime(begTime)
142                                 end = localtime(begTime+duration)
143 #                               print "begin", begin
144 #                               print "end", end
145                                 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])));
146                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
147                         else:
148                                 percent = (nowTime - begTime) * 100 / duration
149                                 res.append((eListboxPythonMultiContent.TYPE_PROGRESS, r2.left(), r2.top(), r2.width(), r2.height(), percent));
150                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
151                 return res
152
153         def queryEPG(self, list, buildFunc=None):
154                 if self.epgcache is not None:
155                         if buildFunc is not None:
156                                 return self.epgcache.lookupEvent(list, buildFunc)
157                         else:
158                                 return self.epgcache.lookupEvent(list)
159                 return [ ]
160
161         def fillMultiEPG(self, services):
162                 t = time()
163                 test = [ 'RIBDTCN' ]
164                 for service in services:
165                         tuple = ( service.ref.toString(), 0 )
166                         test.append( tuple )
167 #               self.list = self.queryEPG(test, self.buildMultiEntry)
168                 tmp = self.queryEPG(test)
169                 self.list = [ ]
170                 for x in tmp:
171                         self.list.append(self.buildMultiEntry(x[0], x[1], x[2], x[3], x[4], x[5], x[6]))
172                 self.l.setList(self.list)
173                 print time() - t
174
175         def updateMultiEPG(self, direction):
176                 t = time()
177                 test = [ 'RIBDTCN' ]
178                 for x in self.list:
179                         data = x[0]
180                         service = data[1]
181                         begTime = data[2]
182                         duration = data[3]
183                         new_stime = 0
184                         if begTime is not None:
185                                 if direction > 0:
186                                         new_stime = begTime+duration+120
187                                 else:
188                                         new_stime = begTime-120
189                         test.append((service, 0, new_stime))
190                 self.list = self.queryEPG(test, self.buildMultiEntry)
191 #               tmp = self.queryEPG(test)
192 #               self.list = [ ]
193 #               for x in tmp:
194 #                       self.list.append(self.buildMultiEntry(x[0], x[1], x[2], x[3], x[4], x[5], x[6]))
195                 self.l.setList(self.list)
196                 print time() - t
197
198         def fillSingleEPG(self, service):
199                 t = time()
200                 if SINGLE_CPP > 0:
201                         self.l.setRoot(service.ref)
202                 else:
203                         self.service = service
204                         test = [ 'IBDT', (service.ref.toString(), 0, -1, -1) ]
205 #                       self.list = self.queryEPG(test, self.buildSingleEntry)
206                         tmp = self.queryEPG(test)
207                         self.list = [ ]
208                         for x in tmp:
209                                 self.list.append(self.buildSingleEntry(x[0], x[1], x[2], x[3]))
210 #                               self.list.append(self.buildSingleEntry(refstr, x[0], x[1], x[2], x[3]))
211                         self.l.setList(self.list)
212                 print time() - t