fix resolve key for ConfigSubDict / ConfigSublist
[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 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, timer = None):
46                 self.days = [ _("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat"), _("Sun") ]
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                 self.l = eListboxPythonMultiContent()
54                 if type == EPG_TYPE_SINGLE:
55                         self.l.setBuildFunc(self.buildSingleEntry)
56                 elif type == EPG_TYPE_MULTI:
57                         self.l.setBuildFunc(self.buildMultiEntry)
58                 else:
59                         assert(type == EPG_TYPE_SIMILAR)
60                         self.l.setBuildFunc(self.buildSimilarEntry)
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_MULTI:
71                         return self.l.getCurrentSelection()[0]
72                 return 0
73
74         def getCurrent(self):
75                 idx=0
76                 if self.type == EPG_TYPE_MULTI:
77                         idx += 1
78                 tmp = self.l.getCurrentSelection()
79                 if tmp is None:
80                         return ( None, None )
81                 eventid = tmp[idx+1]
82                 service = ServiceReference(tmp[idx])
83                 event = self.getEventFromId(service, eventid)
84                 return ( event, service )
85
86         def moveUp(self):
87                 self.instance.moveSelection(self.instance.moveUp)
88
89         def moveDown(self):
90                 self.instance.moveSelection(self.instance.moveDown)
91
92         def connectSelectionChanged(func):
93                 if not self.onSelChanged.count(func):
94                         self.onSelChanged.append(func)
95
96         def disconnectSelectionChanged(func):
97                 self.onSelChanged.remove(func)
98
99         def selectionChanged(self):
100                 for x in self.onSelChanged:
101                         if x is not None:
102                                 try:
103                                         x()
104                                 except: # FIXME!!!
105                                         print "FIXME in EPGList.selectionChanged"
106                                         pass
107
108         GUI_WIDGET = eListbox
109         
110         def postWidgetCreate(self, instance):
111                 instance.setWrapAround(True)
112                 instance.selectionChanged.get().append(self.selectionChanged)
113                 instance.setContent(self.l)
114
115         def recalcEntrySize(self):
116                 esize = self.l.getItemSize()
117                 self.l.setFont(0, gFont("Regular", 22))
118                 self.l.setFont(1, gFont("Regular", 16))
119                 width = esize.width()
120                 height = esize.height()
121                 if self.type == EPG_TYPE_SINGLE:
122                         self.weekday_rect = Rect(0, 0, width/20*2-10, height)
123                         self.datetime_rect = Rect(width/20*2, 0, width/20*5-15, height)
124                         self.descr_rect = Rect(width/20*7, 0, width/20*13, height)
125                 elif self.type == EPG_TYPE_MULTI:
126                         xpos = 0;
127                         w = width/10*3;
128                         self.service_rect = Rect(xpos, 0, w-10, height)
129                         xpos += w;
130                         w = width/10*2;
131                         self.start_end_rect = Rect(xpos, 0, w-10, height)
132                         self.progress_rect = Rect(xpos, 4, w-10, height-8)
133                         xpos += w
134                         w = width/10*5;
135                         self.descr_rect = Rect(xpos, 0, width, height)
136                 else: # EPG_TYPE_SIMILAR
137                         self.weekday_rect = Rect(0, 0, width/20*2-10, height)
138                         self.datetime_rect = Rect(width/20*2, 0, width/20*5-15, height)
139                         self.service_rect = Rect(width/20*7, 0, width/20*13, height)
140
141         def buildSingleEntry(self, service, eventId, beginTime, duration, EventName):
142                 rec=(self.timer.isInTimer(eventId, beginTime, duration, service) > ((duration/10)*8)) 
143                 r1=self.weekday_rect
144                 r2=self.datetime_rect
145                 r3=self.descr_rect
146                 res = [ None ]  # no private data needed
147                 t = localtime(beginTime)
148                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]))
149                 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])))
150                 if rec:
151                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, 'epgclock-fs8.png'))))
152                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
153                 else:
154                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
155                 return res
156
157         def buildSimilarEntry(self, service, eventId, beginTime, service_name, duration):
158                 rec=(self.timer.isInTimer(eventId, beginTime, duration, service) > ((duration/10)*8)) 
159                 r1=self.weekday_rect
160                 r2=self.datetime_rect
161                 r3=self.service_rect
162                 res = [ None ]  # no private data needed
163                 t = localtime(beginTime)
164                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]))
165                 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])))
166                 if rec:
167                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, 'epgclock-fs8.png'))))
168                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name))
169                 else:
170                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name))
171                 return res
172
173         def buildMultiEntry(self, changecount, service, eventId, begTime, duration, EventName, nowTime, service_name):
174                 rec=begTime and (self.timer.isInTimer(eventId, begTime, duration, service) > ((duration/10)*8))
175                 sname = service_name
176                 r1=self.service_rect
177                 r2=self.progress_rect
178                 r3=self.descr_rect
179                 r4=self.start_end_rect
180                 res = [ None ] # no private data needed
181                 re = compile('\xc2\x86.*?\xc2\x87')
182                 list = re.findall(sname)
183                 if len(list):
184                         sname=''
185                         for substr in list:
186                                 sname+=substr[2:len(substr)-2]
187                         if len(sname) == 0:
188                                 sname = service_name;
189                 if rec:
190                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width()-21, r1.height(), 0, RT_HALIGN_LEFT, sname))
191                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r1.left()+r1.width()-16, r1.top(), 21, 21, loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, 'epgclock-fs8.png'))))
192                 else:
193                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_LEFT, sname))
194                 if begTime is not None:
195                         if nowTime < begTime:
196                                 begin = localtime(begTime)
197                                 end = localtime(begTime+duration)
198 #                               print "begin", begin
199 #                               print "end", end
200                                 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])));
201                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
202                         else:
203                                 percent = (nowTime - begTime) * 100 / duration
204                                 res.append((eListboxPythonMultiContent.TYPE_PROGRESS, r2.left(), r2.top(), r2.width(), r2.height(), percent));
205                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
206                 return res
207
208         def queryEPG(self, list, buildFunc=None):
209                 if self.epgcache is not None:
210                         if buildFunc is not None:
211                                 return self.epgcache.lookupEvent(list, buildFunc)
212                         else:
213                                 return self.epgcache.lookupEvent(list)
214                 return [ ]
215
216         def fillMultiEPG(self, services, stime=-1):
217                 t = time()
218                 test = [ '0RIBDTCN' ]
219                 for service in services:
220                         tuple = (service.ref.toString(), 0, stime)
221                         test.append( tuple )
222                 self.list = self.queryEPG(test)
223                 self.l.setList(self.list)
224                 print time() - t
225                 self.selectionChanged()
226
227         def updateMultiEPG(self, direction):
228                 t = time()
229                 test = [ 'RIBDTCN' ]
230                 for x in self.list:
231                         service = x[1]
232                         begTime = x[3]
233                         duration = x[4]
234                         if begTime is None:
235                                 begTime = 0
236                         test.append((service, direction, begTime))
237                 tmp = self.queryEPG(test)
238                 cnt=0
239                 for x in tmp:
240                         changecount = self.list[cnt][0] + direction
241                         if changecount >= 0:
242                                 if x[2] is not None:
243                                         self.list[cnt]=(changecount, x[0], x[1], x[2], x[3], x[4], x[5], x[6])
244                         cnt+=1
245                 self.l.setList(self.list)
246                 print time() - t
247                 self.selectionChanged()
248
249         def fillSingleEPG(self, service):
250                 t = time()
251                 test = [ 'RIBDT', (service.ref.toString(), 0, -1, -1) ]
252                 self.l.setList(self.queryEPG(test))
253                 print time() - t
254                 self.selectionChanged()
255
256         def sort_func(self,x,y):
257                 if x[2] < y[2]:
258                         return -1
259                 elif x[2] == y[2]:
260                         return 0
261                 else:
262                         return 1
263
264         def fillSimilarList(self, refstr, event_id):
265                 t = time()
266          # search similar broadcastings
267                 if event_id is None:
268                         return
269                 l = self.epgcache.search(('RIBND', 1024, eEPGCache.SIMILAR_BROADCASTINGS_SEARCH, refstr, event_id))
270                 if l and len(l):
271                         l.sort(self.sort_func)
272                 self.l.setList(l)
273                 self.selectionChanged()
274                 print time() - t