042abccbb7758bfc4eda6deba850f254e7d2d0ee
[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                 self.clock_add_pixmap = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, 'skin_default/icons/epgclock_add.png'))
56                 self.clock_pre_pixmap = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, 'skin_default/icons/epgclock_pre.png'))
57                 self.clock_post_pixmap = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, 'skin_default/icons/epgclock_post.png'))
58
59         def getEventFromId(self, service, eventid):
60                 event = None
61                 if self.epgcache is not None and eventid is not None:
62                         event = self.epgcache.lookupEventId(service.ref, eventid)
63                 return event
64
65         def getCurrentChangeCount(self):
66                 if self.type == EPG_TYPE_MULTI:
67                         return self.l.getCurrentSelection()[0]
68                 return 0
69
70         def getCurrent(self):
71                 idx=0
72                 if self.type == EPG_TYPE_MULTI:
73                         idx += 1
74                 tmp = self.l.getCurrentSelection()
75                 if tmp is None:
76                         return ( None, None )
77                 eventid = tmp[idx+1]
78                 service = ServiceReference(tmp[idx])
79                 event = self.getEventFromId(service, eventid)
80                 return ( event, service )
81
82         def moveUp(self):
83                 self.instance.moveSelection(self.instance.moveUp)
84
85         def moveDown(self):
86                 self.instance.moveSelection(self.instance.moveDown)
87
88         def connectSelectionChanged(func):
89                 if not self.onSelChanged.count(func):
90                         self.onSelChanged.append(func)
91
92         def disconnectSelectionChanged(func):
93                 self.onSelChanged.remove(func)
94
95         def selectionChanged(self):
96                 for x in self.onSelChanged:
97                         if x is not None:
98                                 try:
99                                         x()
100                                 except: # FIXME!!!
101                                         print "FIXME in EPGList.selectionChanged"
102                                         pass
103
104         GUI_WIDGET = eListbox
105
106         def postWidgetCreate(self, instance):
107                 instance.setWrapAround(True)
108                 instance.selectionChanged.get().append(self.selectionChanged)
109                 instance.setContent(self.l)
110
111         def preWidgetRemove(self, instance):
112                 instance.selectionChanged.get().remove(self.selectionChanged)
113                 instance.setContent(None)
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 getClockPixmap(self, refstr, beginTime, duration, eventId):
142                 endTime = beginTime + duration
143                 for x in self.timer.timer_list:
144                         if x.service_ref.ref.toString() == refstr:
145                                 beg = x.begin
146                                 end = x.end
147                                 if x.eit == eventId:
148                                         return self.clock_pixmap
149                                 elif beginTime > beg and beginTime < end and endTime > end:
150                                         return self.clock_post_pixmap
151                                 elif beginTime < beg and endTime > beg and endTime < end:
152                                         return self.clock_pre_pixmap
153                 return self.clock_add_pixmap
154
155         def buildSingleEntry(self, service, eventId, beginTime, duration, EventName):
156                 rec=beginTime and (self.timer.isInTimer(eventId, beginTime, duration, service))
157                 r1=self.weekday_rect
158                 r2=self.datetime_rect
159                 r3=self.descr_rect
160                 res = [ None ]  # no private data needed
161                 t = localtime(beginTime)
162                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]))
163                 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])))
164                 if rec:
165                         clock_pic = self.getClockPixmap(service, beginTime, duration, eventId)
166                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, clock_pic))
167                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
168                 else:
169                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
170                 return res
171
172         def buildSimilarEntry(self, service, eventId, beginTime, service_name, duration):
173                 rec=beginTime and (self.timer.isInTimer(eventId, beginTime, duration, service))
174                 r1=self.weekday_rect
175                 r2=self.datetime_rect
176                 r3=self.service_rect
177                 res = [ None ]  # no private data needed
178                 t = localtime(beginTime)
179                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]))
180                 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])))
181                 if rec:
182                         clock_pic = self.getClockPixmap(service, beginTime, duration, eventId)
183                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, clock_pic))
184                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name))
185                 else:
186                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name))
187                 return res
188
189         def buildMultiEntry(self, changecount, service, eventId, begTime, duration, EventName, nowTime, service_name):
190                 rec=beginTime and (self.timer.isInTimer(eventId, begTime, duration, service))
191                 r1=self.service_rect
192                 r2=self.progress_rect
193                 r3=self.descr_rect
194                 r4=self.start_end_rect
195                 res = [ None ] # no private data needed
196                 if rec:
197                         clock_pic = self.getClockPixmap(service, begTime, duration, eventId)
198                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width()-21, r1.height(), 0, RT_HALIGN_LEFT, service_name))
199                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r1.left()+r1.width()-16, r1.top(), 21, 21, clock_pic))
200                 else:
201                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_LEFT, service_name))
202                 if begTime is not None:
203                         if nowTime < begTime:
204                                 begin = localtime(begTime)
205                                 end = localtime(begTime+duration)
206 #                               print "begin", begin
207 #                               print "end", end
208                                 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])));
209                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
210                         else:
211                                 percent = (nowTime - begTime) * 100 / duration
212                                 res.append((eListboxPythonMultiContent.TYPE_PROGRESS, r2.left(), r2.top(), r2.width(), r2.height(), percent));
213                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
214                 return res
215
216         def queryEPG(self, list, buildFunc=None):
217                 if self.epgcache is not None:
218                         if buildFunc is not None:
219                                 return self.epgcache.lookupEvent(list, buildFunc)
220                         else:
221                                 return self.epgcache.lookupEvent(list)
222                 return [ ]
223
224         def fillMultiEPG(self, services, stime=-1):
225                 t = time()
226                 test = [ (service.ref.toString(), 0, stime) for service in services ]
227                 test.insert(0, 'X0RIBDTCn')
228                 self.list = self.queryEPG(test)
229                 self.l.setList(self.list)
230                 print time() - t
231                 self.selectionChanged()
232
233         def updateMultiEPG(self, direction):
234                 t = time()
235                 test = [ x[3] and (x[1], direction, x[3]) or (x[1], direction, 0) for x in self.list ]
236                 test.insert(0, 'XRIBDTCn')
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.list = self.queryEPG(test)
253                 self.l.setList(self.list)
254                 print time() - t
255                 self.selectionChanged()
256
257         def sortSingleEPG(self, type):
258                 if len(self.list):
259                         if type == 1:
260                                 event_id = self.getSelectedEventId()
261                                 self.list.sort(key=lambda x: (x[4] and x[4].lower(), x[2]))
262                                 self.l.setList(self.list)
263                                 self.moveToEventId(event_id)
264                         else:
265                                 assert(type == 0)
266                                 event_id = self.getSelectedEventId()
267                                 self.list.sort(key=lambda x: x[2])
268                                 self.l.setList(self.list)
269                                 self.moveToEventId(event_id)
270
271         def getSelectedEventId(self):
272                 x = self.l.getCurrentSelection()
273                 return x and x[1]
274
275         def moveToEventId(self, eventId):
276                 index = 0
277                 for x in self.list:
278                         if x[1] == eventId:
279                                 self.instance.moveSelectionTo(index)
280                                 break
281                         index += 1
282
283         def fillSimilarList(self, refstr, event_id):
284                 t = time()
285          # search similar broadcastings
286                 if event_id is None:
287                         return
288                 l = self.epgcache.search(('RIBND', 1024, eEPGCache.SIMILAR_BROADCASTINGS_SEARCH, refstr, event_id))
289                 if l and len(l):
290                         l.sort(key=lambda x: x[2])
291                 self.l.setList(l)
292                 self.selectionChanged()
293                 print time() - t