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