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