Merge branch 'master' into dvd_scale_test
[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, 'skin_default/icons/epgclock.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 preWidgetRemove(self, instance):
109                 instance.selectionChanged.get().remove(self.selectionChanged)
110                 instance.setContent(None)
111
112         def recalcEntrySize(self):
113                 esize = self.l.getItemSize()
114                 self.l.setFont(0, gFont("Regular", 22))
115                 self.l.setFont(1, gFont("Regular", 16))
116                 width = esize.width()
117                 height = esize.height()
118                 if self.type == EPG_TYPE_SINGLE:
119                         self.weekday_rect = Rect(0, 0, width/20*2-10, height)
120                         self.datetime_rect = Rect(width/20*2, 0, width/20*5-15, height)
121                         self.descr_rect = Rect(width/20*7, 0, width/20*13, height)
122                 elif self.type == EPG_TYPE_MULTI:
123                         xpos = 0;
124                         w = width/10*3;
125                         self.service_rect = Rect(xpos, 0, w-10, height)
126                         xpos += w;
127                         w = width/10*2;
128                         self.start_end_rect = Rect(xpos, 0, w-10, height)
129                         self.progress_rect = Rect(xpos, 4, w-10, height-8)
130                         xpos += w
131                         w = width/10*5;
132                         self.descr_rect = Rect(xpos, 0, width, height)
133                 else: # EPG_TYPE_SIMILAR
134                         self.weekday_rect = Rect(0, 0, width/20*2-10, height)
135                         self.datetime_rect = Rect(width/20*2, 0, width/20*5-15, height)
136                         self.service_rect = Rect(width/20*7, 0, width/20*13, height)
137
138         def buildSingleEntry(self, service, eventId, beginTime, duration, EventName):
139                 rec=beginTime and (self.timer.isInTimer(eventId, beginTime, duration, service) > ((duration/10)*8)) 
140                 r1=self.weekday_rect
141                 r2=self.datetime_rect
142                 r3=self.descr_rect
143                 res = [ None ]  # no private data needed
144                 t = localtime(beginTime)
145                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]))
146                 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])))
147                 if rec:
148                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, self.clock_pixmap))
149                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
150                 else:
151                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
152                 return res
153
154         def buildSimilarEntry(self, service, eventId, beginTime, service_name, duration):
155                 rec=beginTime and (self.timer.isInTimer(eventId, beginTime, duration, service) > ((duration/10)*8)) 
156                 r1=self.weekday_rect
157                 r2=self.datetime_rect
158                 r3=self.service_rect
159                 res = [ None ]  # no private data needed
160                 t = localtime(beginTime)
161                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]))
162                 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])))
163                 if rec:
164                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, self.clock_pixmap))
165                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name))
166                 else:
167                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name))
168                 return res
169
170         def buildMultiEntry(self, changecount, service, eventId, begTime, duration, EventName, nowTime, service_name):
171                 rec=begTime and (self.timer.isInTimer(eventId, begTime, duration, service) > ((duration/10)*8))
172                 r1=self.service_rect
173                 r2=self.progress_rect
174                 r3=self.descr_rect
175                 r4=self.start_end_rect
176                 res = [ None ] # no private data needed
177                 if rec:
178                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width()-21, r1.height(), 0, RT_HALIGN_LEFT, service_name))
179                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r1.left()+r1.width()-16, r1.top(), 21, 21, self.clock_pixmap))
180                 else:
181                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_LEFT, service_name))
182                 if begTime is not None:
183                         if nowTime < begTime:
184                                 begin = localtime(begTime)
185                                 end = localtime(begTime+duration)
186 #                               print "begin", begin
187 #                               print "end", end
188                                 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])));
189                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
190                         else:
191                                 percent = (nowTime - begTime) * 100 / duration
192                                 res.append((eListboxPythonMultiContent.TYPE_PROGRESS, r2.left(), r2.top(), r2.width(), r2.height(), percent));
193                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
194                 return res
195
196         def queryEPG(self, list, buildFunc=None):
197                 if self.epgcache is not None:
198                         if buildFunc is not None:
199                                 return self.epgcache.lookupEvent(list, buildFunc)
200                         else:
201                                 return self.epgcache.lookupEvent(list)
202                 return [ ]
203
204         def fillMultiEPG(self, services, stime=-1):
205                 t = time()
206                 test = [ (service.ref.toString(), 0, stime) for service in services ]
207                 test.insert(0, 'X0RIBDTCn')
208                 self.list = self.queryEPG(test)
209                 self.l.setList(self.list)
210                 print time() - t
211                 self.selectionChanged()
212
213         def updateMultiEPG(self, direction):
214                 t = time()
215                 test = [ x[3] and (x[1], direction, x[3]) or (x[1], direction, 0) for x in self.list ]
216                 test.insert(0, 'XRIBDTCn')
217                 tmp = self.queryEPG(test)
218                 cnt=0
219                 for x in tmp:
220                         changecount = self.list[cnt][0] + direction
221                         if changecount >= 0:
222                                 if x[2] is not None:
223                                         self.list[cnt]=(changecount, x[0], x[1], x[2], x[3], x[4], x[5], x[6])
224                         cnt+=1
225                 self.l.setList(self.list)
226                 print time() - t
227                 self.selectionChanged()
228
229         def fillSingleEPG(self, service):
230                 t = time()
231                 test = [ 'RIBDT', (service.ref.toString(), 0, -1, -1) ]
232                 self.list = self.queryEPG(test)
233                 self.l.setList(self.list)
234                 print time() - t
235                 self.selectionChanged()
236
237         def sortSingleEPG(self, type):
238                 if len(self.list):
239                         if type == 1:
240                                 event_id = self.getSelectedEventId()
241                                 self.list.sort(key=lambda x: (x[4] and x[4].lower(), x[2]))
242                                 self.l.setList(self.list)
243                                 self.moveToEventId(event_id)
244                         else:
245                                 assert(type == 0)
246                                 event_id = self.getSelectedEventId()
247                                 self.list.sort(key=lambda x: x[2])
248                                 self.l.setList(self.list)
249                                 self.moveToEventId(event_id)
250
251         def getSelectedEventId(self):
252                 x = self.l.getCurrentSelection()
253                 return x and x[1]
254
255         def moveToEventId(self, eventId):
256                 index = 0
257                 for x in self.list:
258                         if x[1] == eventId:
259                                 self.instance.moveSelectionTo(index)
260                                 break
261                         index += 1
262
263         def fillSimilarList(self, refstr, event_id):
264                 t = time()
265          # search similar broadcastings
266                 if event_id is None:
267                         return
268                 l = self.epgcache.search(('RIBND', 1024, eEPGCache.SIMILAR_BROADCASTINGS_SEARCH, refstr, event_id))
269                 if l and len(l):
270                         l.sort(key=lambda x: x[2])
271                 self.l.setList(l)
272                 self.selectionChanged()
273                 print time() - t