also check devidex2 here
[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                 self.clock_prepost_pixmap = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, 'skin_default/icons/epgclock_prepost.png'))
59
60         def getEventFromId(self, service, eventid):
61                 event = None
62                 if self.epgcache is not None and eventid is not None:
63                         event = self.epgcache.lookupEventId(service.ref, eventid)
64                 return event
65
66         def getCurrentChangeCount(self):
67                 if self.type == EPG_TYPE_MULTI and self.l.getCurrentSelection() is not None:
68                         return self.l.getCurrentSelection()[0]
69                 return 0
70
71         def getCurrent(self):
72                 idx=0
73                 if self.type == EPG_TYPE_MULTI:
74                         idx += 1
75                 tmp = self.l.getCurrentSelection()
76                 if tmp is None:
77                         return ( None, None )
78                 eventid = tmp[idx+1]
79                 service = ServiceReference(tmp[idx])
80                 event = self.getEventFromId(service, eventid)
81                 return ( event, service )
82
83         def moveUp(self):
84                 self.instance.moveSelection(self.instance.moveUp)
85
86         def moveDown(self):
87                 self.instance.moveSelection(self.instance.moveDown)
88
89         def connectSelectionChanged(func):
90                 if not self.onSelChanged.count(func):
91                         self.onSelChanged.append(func)
92
93         def disconnectSelectionChanged(func):
94                 self.onSelChanged.remove(func)
95
96         def selectionChanged(self):
97                 for x in self.onSelChanged:
98                         if x is not None:
99                                 x()
100 #                               try:
101 #                                       x()
102 #                               except: # FIXME!!!
103 #                                       print "FIXME in EPGList.selectionChanged"
104 #                                       pass
105
106         GUI_WIDGET = eListbox
107
108         def postWidgetCreate(self, instance):
109                 instance.setWrapAround(True)
110                 instance.selectionChanged.get().append(self.selectionChanged)
111                 instance.setContent(self.l)
112
113         def preWidgetRemove(self, instance):
114                 instance.selectionChanged.get().remove(self.selectionChanged)
115                 instance.setContent(None)
116
117         def recalcEntrySize(self):
118                 esize = self.l.getItemSize()
119                 self.l.setFont(0, gFont("Regular", 22))
120                 self.l.setFont(1, gFont("Regular", 16))
121                 width = esize.width()
122                 height = esize.height()
123                 if self.type == EPG_TYPE_SINGLE:
124                         self.weekday_rect = Rect(0, 0, width/20*2-10, height)
125                         self.datetime_rect = Rect(width/20*2, 0, width/20*5-15, height)
126                         self.descr_rect = Rect(width/20*7, 0, width/20*13, height)
127                 elif self.type == EPG_TYPE_MULTI:
128                         xpos = 0;
129                         w = width/10*3;
130                         self.service_rect = Rect(xpos, 0, w-10, height)
131                         xpos += w;
132                         w = width/10*2;
133                         self.start_end_rect = Rect(xpos, 0, w-10, height)
134                         self.progress_rect = Rect(xpos, 4, w-10, height-8)
135                         xpos += w
136                         w = width/10*5;
137                         self.descr_rect = Rect(xpos, 0, width, height)
138                 else: # EPG_TYPE_SIMILAR
139                         self.weekday_rect = Rect(0, 0, width/20*2-10, height)
140                         self.datetime_rect = Rect(width/20*2, 0, width/20*5-15, height)
141                         self.service_rect = Rect(width/20*7, 0, width/20*13, height)
142
143         def getClockPixmap(self, refstr, beginTime, duration, eventId):
144                 pre_clock = 1
145                 post_clock = 2
146                 clock_type = 0
147                 endTime = beginTime + duration
148                 for x in self.timer.timer_list:
149                         if x.service_ref.ref.toString() == refstr:
150                                 if x.eit == eventId:
151                                         return self.clock_pixmap
152                                 beg = x.begin
153                                 end = x.end
154                                 if beginTime > beg and beginTime < end and endTime > end:
155                                         clock_type |= pre_clock
156                                 elif beginTime < beg and endTime > beg and endTime < end:
157                                         clock_type |= post_clock
158                 if clock_type == 0:
159                         return self.clock_add_pixmap
160                 elif clock_type == pre_clock:
161                         return self.clock_pre_pixmap
162                 elif clock_type == post_clock:
163                         return self.clock_post_pixmap
164                 else:
165                         return self.clock_prepost_pixmap
166
167         def buildSingleEntry(self, service, eventId, beginTime, duration, EventName):
168                 rec=beginTime and (self.timer.isInTimer(eventId, beginTime, duration, service))
169                 r1=self.weekday_rect
170                 r2=self.datetime_rect
171                 r3=self.descr_rect
172                 res = [ None ]  # no private data needed
173                 t = localtime(beginTime)
174                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]))
175                 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])))
176                 if rec:
177                         clock_pic = self.getClockPixmap(service, beginTime, duration, eventId)
178                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, clock_pic))
179                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
180                 else:
181                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
182                 return res
183
184         def buildSimilarEntry(self, service, eventId, beginTime, service_name, duration):
185                 rec=beginTime and (self.timer.isInTimer(eventId, beginTime, duration, service))
186                 r1=self.weekday_rect
187                 r2=self.datetime_rect
188                 r3=self.service_rect
189                 res = [ None ]  # no private data needed
190                 t = localtime(beginTime)
191                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]))
192                 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])))
193                 if rec:
194                         clock_pic = self.getClockPixmap(service, beginTime, duration, eventId)
195                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, clock_pic))
196                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name))
197                 else:
198                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name))
199                 return res
200
201         def buildMultiEntry(self, changecount, service, eventId, begTime, duration, EventName, nowTime, service_name):
202                 rec=begTime and (self.timer.isInTimer(eventId, begTime, duration, service))
203                 r1=self.service_rect
204                 r2=self.progress_rect
205                 r3=self.descr_rect
206                 r4=self.start_end_rect
207                 res = [ None ] # no private data needed
208                 if rec:
209                         clock_pic = self.getClockPixmap(service, begTime, duration, eventId)
210                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width()-21, r1.height(), 0, RT_HALIGN_LEFT, service_name))
211                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r1.left()+r1.width()-16, r1.top(), 21, 21, clock_pic))
212                 else:
213                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_LEFT, service_name))
214                 if begTime is not None:
215                         if nowTime < begTime:
216                                 begin = localtime(begTime)
217                                 end = localtime(begTime+duration)
218 #                               print "begin", begin
219 #                               print "end", end
220                                 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])));
221                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
222                         else:
223                                 percent = (nowTime - begTime) * 100 / duration
224                                 res.append((eListboxPythonMultiContent.TYPE_PROGRESS, r2.left(), r2.top(), r2.width(), r2.height(), percent));
225                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
226                 return res
227
228         def queryEPG(self, list, buildFunc=None):
229                 if self.epgcache is not None:
230                         if buildFunc is not None:
231                                 return self.epgcache.lookupEvent(list, buildFunc)
232                         else:
233                                 return self.epgcache.lookupEvent(list)
234                 return [ ]
235
236         def fillMultiEPG(self, services, stime=-1):
237                 t = time()
238                 test = [ (service.ref.toString(), 0, stime) for service in services ]
239                 test.insert(0, 'X0RIBDTCn')
240                 self.list = self.queryEPG(test)
241                 self.l.setList(self.list)
242                 print time() - t
243                 self.selectionChanged()
244
245         def updateMultiEPG(self, direction):
246                 t = time()
247                 test = [ x[3] and (x[1], direction, x[3]) or (x[1], direction, 0) for x in self.list ]
248                 test.insert(0, 'XRIBDTCn')
249                 tmp = self.queryEPG(test)
250                 cnt=0
251                 for x in tmp:
252                         changecount = self.list[cnt][0] + direction
253                         if changecount >= 0:
254                                 if x[2] is not None:
255                                         self.list[cnt]=(changecount, x[0], x[1], x[2], x[3], x[4], x[5], x[6])
256                         cnt+=1
257                 self.l.setList(self.list)
258                 print time() - t
259                 self.selectionChanged()
260
261         def fillSingleEPG(self, service):
262                 t = time()
263                 test = [ 'RIBDT', (service.ref.toString(), 0, -1, -1) ]
264                 self.list = self.queryEPG(test)
265                 self.l.setList(self.list)
266                 print time() - t
267                 self.selectionChanged()
268
269         def sortSingleEPG(self, type):
270                 if len(self.list):
271                         if type == 1:
272                                 event_id = self.getSelectedEventId()
273                                 self.list.sort(key=lambda x: (x[4] and x[4].lower(), x[2]))
274                                 self.l.setList(self.list)
275                                 self.moveToEventId(event_id)
276                         else:
277                                 assert(type == 0)
278                                 event_id = self.getSelectedEventId()
279                                 self.list.sort(key=lambda x: x[2])
280                                 self.l.setList(self.list)
281                                 self.moveToEventId(event_id)
282
283         def getSelectedEventId(self):
284                 x = self.l.getCurrentSelection()
285                 return x and x[1]
286
287         def moveToService(self,serviceref):
288                 for x in range(len(self.list)):
289                         if self.list[x][1] == serviceref.toString():
290                                 self.instance.moveSelectionTo(x)
291                                 break
292                         
293         def moveToEventId(self, eventId):
294                 index = 0
295                 for x in self.list:
296                         if x[1] == eventId:
297                                 self.instance.moveSelectionTo(index)
298                                 break
299                         index += 1
300
301         def fillSimilarList(self, refstr, event_id):
302                 t = time()
303          # search similar broadcastings
304                 if event_id is None:
305                         return
306                 l = self.epgcache.search(('RIBND', 1024, eEPGCache.SIMILAR_BROADCASTINGS_SEARCH, refstr, event_id))
307                 if l and len(l):
308                         l.sort(key=lambda x: x[2])
309                 self.l.setList(l)
310                 self.selectionChanged()
311                 print time() - t