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