allow language references through indexes
[enigma2.git] / lib / python / Components / ServiceList.py
1 from HTMLComponent import HTMLComponent
2 from GUIComponent import GUIComponent
3 from skin import parseColor
4
5 from enigma import eListboxServiceContent, eListbox, eServiceCenter, eServiceReference, gFont, eRect
6 from Tools.LoadPixmap import LoadPixmap
7
8 from string import upper
9
10 from Tools.Directories import resolveFilename, SCOPE_SKIN_IMAGE
11
12 class ServiceList(HTMLComponent, GUIComponent):
13         MODE_NORMAL = 0
14         MODE_FAVOURITES = 1
15
16         def __init__(self):
17                 GUIComponent.__init__(self)
18                 self.l = eListboxServiceContent()
19
20                 pic = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "folder.png"))
21                 if pic:
22                         self.l.setPixmap(self.l.picFolder, pic)
23
24                 pic = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "marker-fs8.png"))
25                 if pic:
26                         self.l.setPixmap(self.l.picMarker, pic)
27
28                 pic = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "ico_dvb_s-fs8.png"))
29                 if pic:
30                         self.l.setPixmap(self.l.picDVB_S, pic)
31
32                 pic = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "ico_dvb_c-fs8.png"))
33                 if pic:
34                         self.l.setPixmap(self.l.picDVB_C, pic)
35
36                 pic = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "ico_dvb_t-fs8.png"))
37                 if pic:
38                         self.l.setPixmap(self.l.picDVB_T, pic)
39
40                 pic = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "ico_service_group-fs8.png"))
41                 if pic:
42                         self.l.setPixmap(self.l.picServiceGroup, pic)
43
44                 self.root = None
45                 self.mode = self.MODE_NORMAL
46                 self.onSelectionChanged = [ ]
47
48         def applySkin(self, desktop):
49                 attribs = [ ]
50                 if self.skinAttributes is not None:
51                         attribs = [ ]
52                         for (attrib, value) in self.skinAttributes:
53                                 if attrib == "foregroundColorMarked":
54                                         self.l.setColor(eListboxServiceContent.markedForeground, parseColor(value))
55                                 elif attrib == "foregroundColorMarkedSelected":
56                                         self.l.setColor(eListboxServiceContent.markedForegroundSelected, parseColor(value))
57                                 elif attrib == "backgroundColorMarked":
58                                         self.l.setColor(eListboxServiceContent.markedBackground, parseColor(value))
59                                 elif attrib == "backgroundColorMarkedSelected":
60                                         self.l.setColor(eListboxServiceContent.markedBackgroundSelected, parseColor(value))
61                                 elif attrib == "foregroundColorServiceNotAvail":
62                                         self.l.setColor(eListboxServiceContent.serviceNotAvail, parseColor(value))
63                                 else:
64                                         attribs.append((attrib, value))
65                 self.skinAttributes = attribs
66                 return GUIComponent.applySkin(self, desktop)
67
68         def connectSelChanged(self, fnc):
69                 if not fnc in self.onSelectionChanged:
70                         self.onSelectionChanged.append(fnc)
71
72         def disconnectSelChanged(self, fnc):
73                 if fnc in self.onSelectionChanged:
74                         self.onSelectionChanged.remove(fnc)
75
76         def selectionChanged(self):
77                 for x in self.onSelectionChanged:
78                         x()
79
80         def setCurrent(self, ref):
81                 self.l.setCurrent(ref)
82
83         def getCurrent(self):
84                 r = eServiceReference()
85                 self.l.getCurrent(r)
86                 return r
87
88         def atBegin(self):
89                 return self.instance.atBegin()
90
91         def atEnd(self):
92                 return self.instance.atEnd()
93
94         def moveUp(self):
95                 self.instance.moveSelection(self.instance.moveUp)
96
97         def moveDown(self):
98                 self.instance.moveSelection(self.instance.moveDown)
99
100         def moveToChar(self, char):
101                 # TODO fill with life
102                 print "Next char: "
103                 index = self.l.getNextBeginningWithChar(char)
104                 indexup = self.l.getNextBeginningWithChar(upper(char))
105                 if indexup != 0:
106                         if (index > indexup or index == 0):
107                                 index = indexup
108
109                 self.instance.moveSelectionTo(index)
110                 print "Moving to character " + str(char)
111
112         def moveToNextMarker(self):
113                 idx = self.l.getNextMarkerPos()
114                 self.instance.moveSelectionTo(idx)
115
116         def moveToPrevMarker(self):
117                 idx = self.l.getPrevMarkerPos()
118                 self.instance.moveSelectionTo(idx)
119
120         def moveToIndex(self, index):
121                 self.instance.moveSelectionTo(index)
122
123         def getCurrentIndex(self):
124                 return self.instance.getCurrentIndex()
125
126         GUI_WIDGET = eListbox
127         
128         def postWidgetCreate(self, instance):
129                 instance.setWrapAround(True)
130                 instance.setContent(self.l)
131                 instance.selectionChanged.get().append(self.selectionChanged)
132                 self.setMode(self.mode)
133
134         def preWidgetRemove(self, instance):
135                 instance.setContent(None)
136                 instance.selectionChanged.get().remove(self.selectionChanged)
137
138         def getRoot(self):
139                 return self.root
140
141         def getRootServices(self):
142                 serviceHandler = eServiceCenter.getInstance()
143                 list = serviceHandler.list(self.root)
144                 dest = [ ]
145                 if list is not None:
146                         while 1:
147                                 s = list.getNext()
148                                 if s.valid():
149                                         dest.append(s.toString())
150                                 else:
151                                         break
152                 return dest
153
154         def setNumberOffset(self, offset):
155                 self.l.setNumberOffset(offset)
156
157         def setPlayableIgnoreService(self, ref):
158                 self.l.setIgnoreService(ref)
159
160         def setRoot(self, root, justSet=False):
161                 self.root = root
162                 self.l.setRoot(root, justSet)
163                 if not justSet:
164                         self.l.sort()
165                 self.selectionChanged()
166
167         def removeCurrent(self):
168                 self.l.removeCurrent()
169
170         def addService(self, service, beforeCurrent=False):
171                 self.l.addService(service, beforeCurrent)
172
173         def finishFill(self):
174                 self.l.FillFinished()
175                 self.l.sort()
176
177 # stuff for multiple marks (edit mode / later multiepg)
178         def clearMarks(self):
179                 self.l.initMarked()
180
181         def isMarked(self, ref):
182                 return self.l.isMarked(ref)
183
184         def addMarked(self, ref):
185                 self.l.addMarked(ref)
186
187         def removeMarked(self, ref):
188                 self.l.removeMarked(ref)
189
190         def getMarked(self):
191                 i = self.l
192                 i.markedQueryStart()
193                 ref = eServiceReference()
194                 marked = [ ]
195                 while i.markedQueryNext(ref) == 0:
196                         marked.append(ref.toString())
197                         ref = eServiceReference()
198                 return marked
199
200 #just for movemode.. only one marked entry..
201         def setCurrentMarked(self, state):
202                 self.l.setCurrentMarked(state)
203
204         def setMode(self, mode):
205                 self.mode = mode
206
207                 if mode == self.MODE_NORMAL:
208                         self.l.setItemHeight(28)
209                         self.l.setVisualMode(eListboxServiceContent.visModeComplex)
210                         self.l.setElementFont(self.l.celServiceName, gFont("Regular", 22))
211                         self.l.setElementPosition(self.l.celServiceName, eRect(0, 0, self.instance.size().width(), 28))
212                         self.l.setElementFont(self.l.celServiceInfo, gFont("Regular", 18))
213                 else:
214                         self.l.setItemHeight(28)
215                         self.l.setVisualMode(eListboxServiceContent.visModeComplex)
216                         self.l.setElementFont(self.l.celServiceNumber, gFont("Regular", 20))
217                         self.l.setElementPosition(self.l.celServiceNumber, eRect(0, 0, 50, 28))
218                         self.l.setElementFont(self.l.celServiceName, gFont("Regular", 22))
219                         self.l.setElementPosition(self.l.celServiceName, eRect(60, 0, self.instance.size().width()-60, 28))
220                         self.l.setElementFont(self.l.celServiceInfo, gFont("Regular", 18))