2c3b702075e09bb47944d54102c5a97ef8c08bc9
[openblackhole/openblackhole-enigma2.git] / lib / python / Components / ServiceList.py
1 from HTMLComponent import HTMLComponent
2 from GUIComponent import GUIComponent
3 from skin import parseColor, parseFont
4
5 from enigma import eListboxServiceContent, eListbox, eServiceCenter, eServiceReference, gFont, eRect
6 from Tools.LoadPixmap import LoadPixmap
7 from Tools.TextBoundary import getTextBoundarySize
8
9 from Tools.Directories import resolveFilename, SCOPE_CURRENT_SKIN
10
11 from Components.Renderer.Picon import getPiconName
12 from Components.config import config
13
14 def refreshServiceList(configElement = None):
15         from Screens.InfoBar import InfoBar
16         InfoBarInstance = InfoBar.instance
17         if InfoBarInstance is not None:
18                 servicelist = InfoBarInstance.servicelist
19                 if servicelist:
20                         servicelist.setMode()
21
22 class ServiceList(HTMLComponent, GUIComponent):
23         MODE_NORMAL = 0
24         MODE_FAVOURITES = 1
25
26         def __init__(self, serviceList):
27                 self.serviceList = serviceList
28                 GUIComponent.__init__(self)
29                 self.l = eListboxServiceContent()
30
31                 pic = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/folder.png"))
32                 pic and self.l.setPixmap(self.l.picFolder, pic)
33                 pic = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/marker.png"))
34                 pic and self.l.setPixmap(self.l.picMarker, pic)
35                 pic = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/ico_dvb_s-fs8.png"))
36                 pic and self.l.setPixmap(self.l.picDVB_S, pic)
37                 pic = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/ico_dvb_c-fs8.png"))
38                 pic and self.l.setPixmap(self.l.picDVB_C, pic)
39                 pic = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/ico_dvb_t-fs8.png"))
40                 pic and self.l.setPixmap(self.l.picDVB_T, pic)
41                 pic = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/ico_stream-fs8.png"))
42                 pic and self.l.setPixmap(self.l.picStream, pic)
43                 pic = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/ico_service_group-fs8.png"))
44                 pic and self.l.setPixmap(self.l.picServiceGroup, pic)
45                 pic = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/icon_crypt.png"))
46                 pic and self.l.setPixmap(self.l.picCrypto, pic)
47                 pic = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/record.png"))
48                 pic and self.l.setPixmap(self.l.picRecord, pic)
49
50                 self.root = None
51                 self.mode = self.MODE_NORMAL
52                 self.ItemHeight = 28
53                 self.ServiceNameFont = parseFont("Regular;22", ((1,1),(1,1)))
54                 self.ServiceInfoFont = parseFont("Regular;18", ((1,1),(1,1)))
55                 self.ServiceNumberFont = parseFont("Regular;20", ((1,1),(1,1)))
56                 self.progressBarWidth = 52
57                 self.progressPercentWidth = 0
58                 self.fieldMargins = 10
59
60                 self.onSelectionChanged = [ ]
61
62         def applySkin(self, desktop, parent):
63                 def foregroundColorMarked(value):
64                         self.l.setColor(eListboxServiceContent.markedForeground, parseColor(value))
65                 def foregroundColorMarkedSelected(value):
66                         self.l.setColor(eListboxServiceContent.markedForegroundSelected, parseColor(value))
67                 def backgroundColorMarked(value):
68                         self.l.setColor(eListboxServiceContent.markedBackground, parseColor(value))
69                 def backgroundColorMarkedSelected(value):
70                         self.l.setColor(eListboxServiceContent.markedBackgroundSelected, parseColor(value))
71                 def foregroundColorServiceNotAvail(value):
72                         self.l.setColor(eListboxServiceContent.serviceNotAvail, parseColor(value))
73                 def foregroundColorEvent(value):
74                         self.l.setColor(eListboxServiceContent.eventForeground, parseColor(value))
75                 def colorServiceDescription(value):
76                         self.l.setColor(eListboxServiceContent.eventForeground, parseColor(value))
77                 def foregroundColorEventSelected(value):
78                         self.l.setColor(eListboxServiceContent.eventForegroundSelected, parseColor(value))
79                 def colorServiceDescriptionSelected(value):
80                         self.l.setColor(eListboxServiceContent.eventForegroundSelected, parseColor(value))
81                 def foregroundColorEventborder(value):
82                         self.l.setColor(eListboxServiceContent.eventborderForeground, parseColor(value))
83                 def foregroundColorEventborderSelected(value):
84                         self.l.setColor(eListboxServiceContent.eventborderForegroundSelected, parseColor(value))
85                 def colorEventProgressbar(value):
86                         self.l.setColor(eListboxServiceContent.serviceEventProgressbarColor, parseColor(value))
87                 def colorEventProgressbarSelected(value):
88                         self.l.setColor(eListboxServiceContent.serviceEventProgressbarColorSelected, parseColor(value))
89                 def colorEventProgressbarBorder(value):
90                         self.l.setColor(eListboxServiceContent.serviceEventProgressbarBorderColor, parseColor(value))
91                 def colorEventProgressbarBorderSelected(value):
92                         self.l.setColor(eListboxServiceContent.serviceEventProgressbarBorderColorSelected, parseColor(value))
93                 def colorServiceRecorded(value):
94                         self.l.setColor(eListboxServiceContent.serviceRecorded, parseColor(value))
95                 def colorFallbackItem(value):
96                         self.l.setColor(eListboxServiceContent.serviceItemFallback, parseColor(value))
97                 def colorServiceSelectedFallback(value):
98                         self.l.setColor(eListboxServiceContent.serviceSelectedFallback, parseColor(value))
99                 def colorServiceDescriptionFallback(value):
100                         self.l.setColor(eListboxServiceContent.eventForegroundFallback, parseColor(value))
101                 def colorServiceDescriptionSelectedFallback(value):
102                         self.l.setColor(eListboxServiceContent.eventForegroundSelectedFallback, parseColor(value))
103                 def picServiceEventProgressbar(value):
104                         pic = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, value))
105                         pic and self.l.setPixmap(self.l.picServiceEventProgressbar, pic)
106                 def serviceItemHeight(value):
107                         self.ItemHeight = int(value)
108                 def serviceNameFont(value):
109                         self.ServiceNameFont = parseFont(value, ((1,1),(1,1)))
110                 def serviceInfoFont(value):
111                         self.ServiceInfoFont = parseFont(value, ((1,1),(1,1)))
112                 def serviceNumberFont(value):
113                         self.ServiceNumberFont = parseFont(value, ((1,1),(1,1)))
114                 def progressbarHeight(value):
115                         self.l.setProgressbarHeight(int(value))
116                 def progressbarBorderWidth(value):
117                         self.l.setProgressbarBorderWidth(int(value))
118                 def progressBarWidth(value):
119                         self.progressBarWidth = int(value)
120                 def progressPercentWidth(value):
121                         self.progressPercentWidth = int(value)
122                 def fieldMargins(value):
123                         self.fieldMargins = int(value)
124                 def nonplayableMargins(value):
125                         self.l.setNonplayableMargins(int(value))
126                 def itemsDistances(value):
127                         self.l.setItemsDistances(int(value))
128                 for (attrib, value) in list(self.skinAttributes):
129                         try:
130                                 locals().get(attrib)(value)
131                                 self.skinAttributes.remove((attrib, value))
132                         except:
133                                 pass
134                 return GUIComponent.applySkin(self, desktop, parent)
135
136         def connectSelChanged(self, fnc):
137                 if not fnc in self.onSelectionChanged:
138                         self.onSelectionChanged.append(fnc)
139
140         def disconnectSelChanged(self, fnc):
141                 if fnc in self.onSelectionChanged:
142                         self.onSelectionChanged.remove(fnc)
143
144         def selectionChanged(self):
145                 for x in self.onSelectionChanged:
146                         x()
147
148         def setCurrent(self, ref, adjust=True):
149                 if self.l.setCurrent(ref):
150                         return None
151                 from Components.ServiceEventTracker import InfoBarCount
152                 if adjust and config.usage.multibouquet.value and InfoBarCount == 1 and ref and ref.type != 8192:
153                         print "[servicelist] search for service in userbouquets"
154                         if self.serviceList:
155                                 revert_mode = config.servicelist.lastmode.value
156                                 revert_root = self.getRoot()
157                                 self.serviceList.setModeTv()
158                                 revert_tv_root = self.getRoot()
159                                 bouquets = self.serviceList.getBouquetList()
160                                 for bouquet in bouquets:
161                                         self.serviceList.enterUserbouquet(bouquet[1])
162                                         if self.l.setCurrent(ref):
163                                                 config.servicelist.lastmode.save()
164                                                 self.serviceList.saveChannel(ref)
165                                                 return True
166                                 self.serviceList.enterUserbouquet(revert_tv_root)       
167                                 self.serviceList.setModeRadio()
168                                 revert_radio_root = self.getRoot()
169                                 bouquets = self.serviceList.getBouquetList()
170                                 for bouquet in bouquets:
171                                         self.serviceList.enterUserbouquet(bouquet[1])
172                                         if self.l.setCurrent(ref):
173                                                 config.servicelist.lastmode.save()
174                                                 self.serviceList.saveChannel(ref)
175                                                 return True
176                                 self.serviceList.enterUserbouquet(revert_radio_root)            
177                                 print "[servicelist] service not found in any userbouquets"
178                                 if revert_mode == "tv":
179                                         self.serviceList.setModeTv()
180                                 elif revert_mode == "radio":
181                                         self.serviceList.setModeRadio()
182                                 self.serviceList.enterUserbouquet(revert_root)
183                 return False
184
185         def getCurrent(self):
186                 r = eServiceReference()
187                 self.l.getCurrent(r)
188                 return r
189
190         def atBegin(self):
191                 return self.instance.atBegin()
192
193         def atEnd(self):
194                 return self.instance.atEnd()
195
196         def moveUp(self):
197                 self.instance.moveSelection(self.instance.moveUp)
198
199         def moveDown(self):
200                 self.instance.moveSelection(self.instance.moveDown)
201
202         def moveToChar(self, char):
203                 # TODO fill with life
204                 print "Next char: "
205                 index = self.l.getNextBeginningWithChar(char)
206                 indexup = self.l.getNextBeginningWithChar(char.upper())
207                 if indexup != 0:
208                         if (index > indexup or index == 0):
209                                 index = indexup
210
211                 self.instance.moveSelectionTo(index)
212                 print "Moving to character " + str(char)
213
214         def moveToNextMarker(self):
215                 idx = self.l.getNextMarkerPos()
216                 self.instance.moveSelectionTo(idx)
217
218         def moveToPrevMarker(self):
219                 idx = self.l.getPrevMarkerPos()
220                 self.instance.moveSelectionTo(idx)
221
222         def moveToIndex(self, index):
223                 self.instance.moveSelectionTo(index)
224
225         def getCurrentIndex(self):
226                 return self.instance.getCurrentIndex()
227
228         GUI_WIDGET = eListbox
229
230         def postWidgetCreate(self, instance):
231                 instance.setWrapAround(True)
232                 instance.setContent(self.l)
233                 instance.selectionChanged.get().append(self.selectionChanged)
234                 self.setMode(self.mode)
235
236         def preWidgetRemove(self, instance):
237                 instance.setContent(None)
238                 instance.selectionChanged.get().remove(self.selectionChanged)
239
240         def getRoot(self):
241                 return self.root
242
243         def getRootServices(self):
244                 serviceHandler = eServiceCenter.getInstance()
245                 list = serviceHandler.list(self.root)
246                 dest = [ ]
247                 if list is not None:
248                         while 1:
249                                 s = list.getNext()
250                                 if s.valid():
251                                         dest.append(s.toString())
252                                 else:
253                                         break
254                 return dest
255
256         def setPlayableIgnoreService(self, ref):
257                 self.l.setIgnoreService(ref)
258
259         def setRoot(self, root, justSet=False):
260                 self.root = root
261                 self.l.setRoot(root, justSet)
262                 if not justSet:
263                         self.l.sort()
264                 self.selectionChanged()
265
266         def resetRoot(self):
267                 index = self.instance.getCurrentIndex()
268                 self.l.setRoot(self.root, False)
269                 self.l.sort()
270                 self.instance.moveSelectionTo(index)
271
272         def removeCurrent(self):
273                 self.l.removeCurrent()
274
275         def addService(self, service, beforeCurrent=False):
276                 self.l.addService(service, beforeCurrent)
277
278         def finishFill(self):
279                 self.l.FillFinished()
280                 self.l.sort()
281
282 # stuff for multiple marks (edit mode / later multiepg)
283         def clearMarks(self):
284                 self.l.initMarked()
285
286         def isMarked(self, ref):
287                 return self.l.isMarked(ref)
288
289         def addMarked(self, ref):
290                 self.l.addMarked(ref)
291
292         def removeMarked(self, ref):
293                 self.l.removeMarked(ref)
294
295         def getMarked(self):
296                 i = self.l
297                 i.markedQueryStart()
298                 ref = eServiceReference()
299                 marked = [ ]
300                 while i.markedQueryNext(ref) == 0:
301                         marked.append(ref.toString())
302                         ref = eServiceReference()
303                 return marked
304
305 #just for movemode.. only one marked entry..
306         def setCurrentMarked(self, state):
307                 self.l.setCurrentMarked(state)
308
309         def setMode(self, mode):
310                 self.mode = mode
311                 self.l.setItemHeight(self.ItemHeight)
312                 self.l.setVisualMode(eListboxServiceContent.visModeComplex)
313
314                 if config.usage.service_icon_enable.value:
315                         self.l.setGetPiconNameFunc(getPiconName)
316                 else:
317                         self.l.setGetPiconNameFunc(None)
318
319                 rowWidth = self.instance.size().width() - 30 #scrollbar is fixed 20 + 10 Extra marge
320
321                 if mode == self.MODE_NORMAL or not config.usage.show_channel_numbers_in_servicelist.value:
322                         channelNumberWidth = 0
323                         channelNumberSpace = 0
324                 else:
325                         channelNumberWidth = config.usage.alternative_number_mode.value and getTextBoundarySize(self.instance, self.ServiceNumberFont, self.instance.size(), "0000").width() or getTextBoundarySize(self.instance, self.ServiceNumberFont, self.instance.size(), "00000").width()
326                         channelNumberSpace = self.fieldMargins
327
328                 self.l.setElementPosition(self.l.celServiceNumber, eRect(0, 0, channelNumberWidth, self.ItemHeight))
329
330                 progressWidth = self.progressBarWidth
331                 if "perc" in config.usage.show_event_progress_in_servicelist.value:
332                         progressWidth = self.progressPercentWidth or self.progressBarWidth
333
334                 if "left" in config.usage.show_event_progress_in_servicelist.value:
335                         self.l.setElementPosition(self.l.celServiceEventProgressbar, eRect(channelNumberWidth+channelNumberSpace, 0, progressWidth , self.ItemHeight))
336                         self.l.setElementPosition(self.l.celServiceName, eRect(channelNumberWidth+channelNumberSpace + progressWidth + self.fieldMargins, 0, rowWidth - (channelNumberWidth+channelNumberSpace + progressWidth + self.fieldMargins), self.ItemHeight))
337                 elif "right" in config.usage.show_event_progress_in_servicelist.value:
338                         self.l.setElementPosition(self.l.celServiceEventProgressbar, eRect(rowWidth - progressWidth, 0, progressWidth, self.ItemHeight))
339                         self.l.setElementPosition(self.l.celServiceName, eRect(channelNumberWidth+channelNumberSpace, 0, rowWidth - (channelNumberWidth+channelNumberSpace + progressWidth + self.fieldMargins), self.ItemHeight))
340                 else:
341                         self.l.setElementPosition(self.l.celServiceEventProgressbar, eRect(0, 0, 0, 0))
342                         self.l.setElementPosition(self.l.celServiceName, eRect(channelNumberWidth+channelNumberSpace, 0, rowWidth - (channelNumberWidth+channelNumberSpace), self.ItemHeight))
343
344                 self.l.setElementFont(self.l.celServiceName, self.ServiceNameFont)
345                 self.l.setElementFont(self.l.celServiceNumber, self.ServiceNumberFont)
346                 self.l.setElementFont(self.l.celServiceInfo, self.ServiceInfoFont)
347                 if "perc" in config.usage.show_event_progress_in_servicelist.value:
348                         self.l.setElementFont(self.l.celServiceEventProgressbar, self.ServiceInfoFont)
349                 self.l.setHideNumberMarker(config.usage.hide_number_markers.value)
350                 self.l.setServiceTypeIconMode(int(config.usage.servicetype_icon_mode.value))
351                 self.l.setCryptoIconMode(int(config.usage.crypto_icon_mode.value))
352                 self.l.setRecordIndicatorMode(int(config.usage.record_indicator_mode.value))
353                 self.l.setColumnWidth(int(config.usage.servicelist_column.value))