add StaticText source, which can be used as a simple way to forward values between...
[enigma2.git] / lib / python / Components / EpgList.py
1 # -*- coding: utf-8 -*-
2 from HTMLComponent import *
3 from GUIComponent import *
4
5 from enigma import eEPGCache, eListbox, eListboxPythonMultiContent, gFont, loadPNG, \
6         RT_HALIGN_LEFT, RT_HALIGN_RIGHT, RT_HALIGN_CENTER, RT_VALIGN_CENTER
7
8 from re import *
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
55         def getEventFromId(self, service, eventid):
56                 event = None
57                 if self.epgcache is not None and eventid is not None:
58                         event = self.epgcache.lookupEventId(service.ref, eventid)
59                 return event
60
61         def getCurrentChangeCount(self):
62                 if self.type == EPG_TYPE_MULTI:
63                         return self.l.getCurrentSelection()[0]
64                 return 0
65
66         def getCurrent(self):
67                 idx=0
68                 if self.type == EPG_TYPE_MULTI:
69                         idx += 1
70                 tmp = self.l.getCurrentSelection()
71                 if tmp is None:
72                         return ( None, None )
73                 eventid = tmp[idx+1]
74                 service = ServiceReference(tmp[idx])
75                 event = self.getEventFromId(service, eventid)
76                 return ( event, service )
77
78         def moveUp(self):
79                 self.instance.moveSelection(self.instance.moveUp)
80
81         def moveDown(self):
82                 self.instance.moveSelection(self.instance.moveDown)
83
84         def connectSelectionChanged(func):
85                 if not self.onSelChanged.count(func):
86                         self.onSelChanged.append(func)
87
88         def disconnectSelectionChanged(func):
89                 self.onSelChanged.remove(func)
90
91         def selectionChanged(self):
92                 for x in self.onSelChanged:
93                         if x is not None:
94                                 try:
95                                         x()
96                                 except: # FIXME!!!
97                                         print "FIXME in EPGList.selectionChanged"
98                                         pass
99
100         GUI_WIDGET = eListbox
101         
102         def postWidgetCreate(self, instance):
103                 instance.setWrapAround(True)
104                 instance.selectionChanged.get().append(self.selectionChanged)
105                 instance.setContent(self.l)
106
107         def recalcEntrySize(self):
108                 esize = self.l.getItemSize()
109                 self.l.setFont(0, gFont("Regular", 22))
110                 self.l.setFont(1, gFont("Regular", 16))
111                 width = esize.width()
112                 height = esize.height()
113                 if self.type == EPG_TYPE_SINGLE:
114                         self.weekday_rect = Rect(0, 0, width/20*2-10, height)
115                         self.datetime_rect = Rect(width/20*2, 0, width/20*5-15, height)
116                         self.descr_rect = Rect(width/20*7, 0, width/20*13, height)
117                 elif self.type == EPG_TYPE_MULTI:
118                         xpos = 0;
119                         w = width/10*3;
120                         self.service_rect = Rect(xpos, 0, w-10, height)
121                         xpos += w;
122                         w = width/10*2;
123                         self.start_end_rect = Rect(xpos, 0, w-10, height)
124                         self.progress_rect = Rect(xpos, 4, w-10, height-8)
125                         xpos += w
126                         w = width/10*5;
127                         self.descr_rect = Rect(xpos, 0, width, height)
128                 else: # EPG_TYPE_SIMILAR
129                         self.weekday_rect = Rect(0, 0, width/20*2-10, height)
130                         self.datetime_rect = Rect(width/20*2, 0, width/20*5-15, height)
131                         self.service_rect = Rect(width/20*7, 0, width/20*13, height)
132
133         def buildSingleEntry(self, service, eventId, beginTime, duration, EventName):
134                 rec=(self.timer.isInTimer(eventId, beginTime, duration, service) > ((duration/10)*8)) 
135                 r1=self.weekday_rect
136                 r2=self.datetime_rect
137                 r3=self.descr_rect
138                 res = [ None ]  # no private data needed
139                 t = localtime(beginTime)
140                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]))
141                 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])))
142                 if rec:
143                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, 'epgclock-fs8.png'))))
144                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
145                 else:
146                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
147                 return res
148
149         def buildSimilarEntry(self, service, eventId, beginTime, service_name, duration):
150                 rec=(self.timer.isInTimer(eventId, beginTime, duration, service) > ((duration/10)*8)) 
151                 r1=self.weekday_rect
152                 r2=self.datetime_rect
153                 r3=self.service_rect
154                 res = [ None ]  # no private data needed
155                 t = localtime(beginTime)
156                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]))
157                 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])))
158                 if rec:
159                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, 'epgclock-fs8.png'))))
160                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name))
161                 else:
162                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name))
163                 return res
164
165         def buildMultiEntry(self, changecount, service, eventId, begTime, duration, EventName, nowTime, service_name):
166                 rec=begTime and (self.timer.isInTimer(eventId, begTime, duration, service) > ((duration/10)*8))
167                 sname = service_name
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                 re = compile('\xc2\x86.*?\xc2\x87')
174                 list = re.findall(sname)
175                 if len(list):
176                         sname=''
177                         for substr in list:
178                                 sname+=substr[2:len(substr)-2]
179                         if len(sname) == 0:
180                                 sname = service_name;
181                 if rec:
182                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width()-21, r1.height(), 0, RT_HALIGN_LEFT, sname))
183                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r1.left()+r1.width()-16, r1.top(), 21, 21, loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, 'epgclock-fs8.png'))))
184                 else:
185                         res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_LEFT, sname))
186                 if begTime is not None:
187                         if nowTime < begTime:
188                                 begin = localtime(begTime)
189                                 end = localtime(begTime+duration)
190 #                               print "begin", begin
191 #                               print "end", end
192                                 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])));
193                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
194                         else:
195                                 percent = (nowTime - begTime) * 100 / duration
196                                 res.append((eListboxPythonMultiContent.TYPE_PROGRESS, r2.left(), r2.top(), r2.width(), r2.height(), percent));
197                                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
198                 return res
199
200         def queryEPG(self, list, buildFunc=None):
201                 if self.epgcache is not None:
202                         if buildFunc is not None:
203                                 return self.epgcache.lookupEvent(list, buildFunc)
204                         else:
205                                 return self.epgcache.lookupEvent(list)
206                 return [ ]
207
208         def fillMultiEPG(self, services, stime=-1):
209                 t = time()
210                 test = [ '0RIBDTCN' ]
211                 for service in services:
212                         tuple = (service.ref.toString(), 0, stime)
213                         test.append( tuple )
214                 self.list = self.queryEPG(test)
215                 self.l.setList(self.list)
216                 print time() - t
217                 self.selectionChanged()
218
219         def updateMultiEPG(self, direction):
220                 t = time()
221                 test = [ 'RIBDTCN' ]
222                 for x in self.list:
223                         service = x[1]
224                         begTime = x[3]
225                         duration = x[4]
226                         if begTime is None:
227                                 begTime = 0
228                         test.append((service, direction, begTime))
229                 tmp = self.queryEPG(test)
230                 cnt=0
231                 for x in tmp:
232                         changecount = self.list[cnt][0] + direction
233                         if changecount >= 0:
234                                 if x[2] is not None:
235                                         self.list[cnt]=(changecount, x[0], x[1], x[2], x[3], x[4], x[5], x[6])
236                         cnt+=1
237                 self.l.setList(self.list)
238                 print time() - t
239                 self.selectionChanged()
240
241         def fillSingleEPG(self, service):
242                 t = time()
243                 test = [ 'RIBDT', (service.ref.toString(), 0, -1, -1) ]
244                 self.l.setList(self.queryEPG(test))
245                 print time() - t
246                 self.selectionChanged()
247
248         def sort_func(self,x,y):
249                 if x[2] < y[2]:
250                         return -1
251                 elif x[2] == y[2]:
252                         return 0
253                 else:
254                         return 1
255
256         def fillSimilarList(self, refstr, event_id):
257                 t = time()
258          # search similar broadcastings
259                 if event_id is None:
260                         return
261                 l = self.epgcache.search(('RIBND', 1024, eEPGCache.SIMILAR_BROADCASTINGS_SEARCH, refstr, event_id))
262                 if l and len(l):
263                         l.sort(self.sort_func)
264                 self.l.setList(l)
265                 self.selectionChanged()
266                 print time() - t