384009454c2de7b38e788bf75e4c9a6c5b7229a6
[enigma2.git] / lib / python / Components / EpgList.py
1 from HTMLComponent import *
2 from GUIComponent import *
3
4 from enigma import eEPGCache, eListbox, eListboxPythonMultiContent, gFont, loadPNG, \
5         RT_HALIGN_LEFT, RT_HALIGN_RIGHT, RT_HALIGN_CENTER, RT_VALIGN_CENTER
6
7 from re import *
8 from time import localtime, time
9 from ServiceReference import ServiceReference
10 from Tools.Directories import resolveFilename, SCOPE_SKIN_IMAGE
11
12 EPG_TYPE_SINGLE = 0
13 EPG_TYPE_MULTI = 1
14 EPG_TYPE_SIMILAR = 2
15
16 class Rect:
17         def __init__(self, x, y, width, height):
18                 self.__left = x
19                 self.__top = y
20                 self.__width = width
21                 self.__height = height
22
23         def left(self):
24                 return self.__left
25
26         def top(self):
27                 return self.__top
28
29         def height(self):
30                 return self.__height
31
32         def width(self):
33                 return self.__width
34
35 class EPGList(HTMLComponent, GUIComponent):
36         def __init__(self, type=EPG_TYPE_SINGLE, selChangedCB=None, timer = None):
37                 self.days = [ _("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat"), _("Sun") ]
38                 self.timer = timer
39                 self.onSelChanged = [ ]
40                 if selChangedCB is not None:
41                         self.onSelChanged.append(selChangedCB)
42                 GUIComponent.__init__(self)
43                 self.type=type
44                 self.l = eListboxPythonMultiContent()
45                 if type == EPG_TYPE_SINGLE:
46                         self.l.setBuildFunc(self.buildSingleEntry)
47                 elif type == EPG_TYPE_MULTI:
48                         self.l.setBuildFunc(self.buildMultiEntry)
49                 else:
50                         assert(type == EPG_TYPE_SIMILAR)
51                         self.l.setBuildFunc(self.buildSimilarEntry)
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 getCurrentChangeCount(self):
61                 if self.type == EPG_TYPE_MULTI:
62                         return self.l.getCurrentSelection()[0]
63                 return 0
64
65         def getCurrent(self):
66                 idx=0
67                 if self.type == EPG_TYPE_MULTI:
68                         idx += 1
69                 tmp = self.l.getCurrentSelection()
70                 if tmp is None:
71                         return ( None, None )
72                 eventid = tmp[idx+1]
73                 service = ServiceReference(tmp[idx])
74                 event = self.getEventFromId(service, eventid)
75                 return ( event, service )
76
77         def moveUp(self):
78                 self.instance.moveSelection(self.instance.moveUp)
79
80         def moveDown(self):
81                 self.instance.moveSelection(self.instance.moveDown)
82
83         def connectSelectionChanged(func):
84                 if not self.onSelChanged.count(func):
85                         self.onSelChanged.append(func)
86
87         def disconnectSelectionChanged(func):
88                 self.onSelChanged.remove(func)
89
90         def selectionChanged(self):
91                 for x in self.onSelChanged:
92                         if x is not None:
93                                 try:
94                                         x()
95                                 except: # FIXME!!!
96                                         print "FIXME in EPGList.selectionChanged"
97                                         pass
98
99         GUI_WIDGET = eListbox
100         
101         def postWidgetCreate(self, instance):
102                 instance.setWrapAround(True)
103                 instance.selectionChanged.get().append(self.selectionChanged)
104                 instance.setContent(self.l)
105
106         def recalcEntrySize(self):
107                 esize = self.l.getItemSize()
108                 self.l.setFont(0, gFont("Regular", 22))
109                 self.l.setFont(1, gFont("Regular", 16))
110                 width = esize.width()
111                 height = esize.height()
112                 if self.type == EPG_TYPE_SINGLE:
113                         self.weekday_rect = Rect(0, 0, width/20*2-10, height)
114                         self.datetime_rect = Rect(width/20*2, 0, width/20*5-15, height)
115                         self.descr_rect = Rect(width/20*7, 0, width/20*13, height)
116                 elif self.type == EPG_TYPE_MULTI:
117                         xpos = 0;
118                         w = width/10*3;
119                         self.service_rect = Rect(xpos, 0, w-10, height)
120                         xpos += w;
121                         w = width/10*2;
122                         self.start_end_rect = Rect(xpos, 0, w-10, height)
123                         self.progress_rect = Rect(xpos, 4, w-10, height-8)
124                         xpos += w
125                         w = width/10*5;
126                         self.descr_rect = Rect(xpos, 0, width, height)
127                 else: # EPG_TYPE_SIMILAR
128                         self.weekday_rect = Rect(0, 0, width/20*2-10, height)
129                         self.datetime_rect = Rect(width/20*2, 0, width/20*5-15, height)
130                         self.service_rect = Rect(width/20*7, 0, width/20*13, height)
131
132         def buildSingleEntry(self, service, eventId, beginTime, duration, EventName):
133                 rec=(self.timer.isInTimer(eventId, beginTime, duration, service) > ((duration/10)*8)) 
134                 r1=self.weekday_rect
135                 r2=self.datetime_rect
136                 r3=self.descr_rect
137                 res = [ None ]  # no private data needed
138                 t = localtime(beginTime)
139                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]))
140                 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])))
141                 if rec:
142                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, 'epgclock-fs8.png'))))
143                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
144                 else:
145                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
146                 return res
147
148         def buildSimilarEntry(self, service, eventId, beginTime, service_name, duration):
149                 rec=(self.timer.isInTimer(eventId, beginTime, duration, service) > ((duration/10)*8)) 
150                 r1=self.weekday_rect
151                 r2=self.datetime_rect
152                 r3=self.service_rect
153                 res = [ None ]  # no private data needed
154                 t = localtime(beginTime)
155                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]))
156                 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])))
157                 if rec:
158                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, 'epgclock-fs8.png'))))
159                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name))
160                 else:
161                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name))
162                 return res
163
164         def buildMultiEntry(self, changecount, service, eventId, begTime, duration, EventName, nowTime, service_name):
165                 rec=begTime and (self.timer.isInTimer(eventId, begTime, duration, service) > ((duration/10)*8))
166                 r1=self.service_rect
167                 r2=self.progress_rect
168                 r3=self.descr_rect
169                 r4=self.start_end_rect
170                 res = [ None ] # no private data needed
171                 if rec:
172                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width()-21, r1.height(), 0, RT_HALIGN_LEFT, service_name))
173                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r1.left()+r1.width()-16, r1.top(), 21, 21, loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, 'epgclock-fs8.png'))))
174                 else:
175                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_LEFT, service_name))
176                 if begTime is not None:
177                         if nowTime < begTime:
178                                 begin = localtime(begTime)
179                                 end = localtime(begTime+duration)
180 #                               print "begin", begin
181 #                               print "end", end
182                                 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])));
183                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
184                         else:
185                                 percent = (nowTime - begTime) * 100 / duration
186                                 res.append((eListboxPythonMultiContent.TYPE_PROGRESS, r2.left(), r2.top(), r2.width(), r2.height(), percent));
187                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
188                 return res
189
190         def queryEPG(self, list, buildFunc=None):
191                 if self.epgcache is not None:
192                         if buildFunc is not None:
193                                 return self.epgcache.lookupEvent(list, buildFunc)
194                         else:
195                                 return self.epgcache.lookupEvent(list)
196                 return [ ]
197
198         def fillMultiEPG(self, services, stime=-1):
199                 t = time()
200                 test = [ '0RIBDTCn' ]
201                 for service in services:
202                         tuple = (service.ref.toString(), 0, stime)
203                         test.append( tuple )
204                 self.list = self.queryEPG(test)
205                 self.l.setList(self.list)
206                 print time() - t
207                 self.selectionChanged()
208
209         def updateMultiEPG(self, direction):
210                 t = time()
211                 test = [ 'RIBDTCn' ]
212                 for x in self.list:
213                         service = x[1]
214                         begTime = x[3]
215                         duration = x[4]
216                         if begTime is None:
217                                 begTime = 0
218                         test.append((service, direction, begTime))
219                 tmp = self.queryEPG(test)
220                 cnt=0
221                 for x in tmp:
222                         changecount = self.list[cnt][0] + direction
223                         if changecount >= 0:
224                                 if x[2] is not None:
225                                         self.list[cnt]=(changecount, x[0], x[1], x[2], x[3], x[4], x[5], x[6])
226                         cnt+=1
227                 self.l.setList(self.list)
228                 print time() - t
229                 self.selectionChanged()
230
231         def fillSingleEPG(self, service):
232                 t = time()
233                 test = [ 'RIBDT', (service.ref.toString(), 0, -1, -1) ]
234                 self.l.setList(self.queryEPG(test))
235                 print time() - t
236                 self.selectionChanged()
237
238         def sort_func(self,x,y):
239                 if x[2] < y[2]:
240                         return -1
241                 elif x[2] == y[2]:
242                         return 0
243                 else:
244                         return 1
245
246         def fillSimilarList(self, refstr, event_id):
247                 t = time()
248          # search similar broadcastings
249                 if event_id is None:
250                         return
251                 l = self.epgcache.search(('RIBND', 1024, eEPGCache.SIMILAR_BROADCASTINGS_SEARCH, refstr, event_id))
252                 if l and len(l):
253                         l.sort(self.sort_func)
254                 self.l.setList(l)
255                 self.selectionChanged()
256                 print time() - t