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