needed update for avahi stuff
[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
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, "skin_default/icons/folder.png"))
21                 if pic:
22                         self.l.setPixmap(self.l.picFolder, pic)
23
24                 pic = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/icons/marker.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.ItemHeight = 28
47                 self.ServiceNameFont = parseFont("Regular;22", ((1,1),(1,1)))
48                 self.ServiceInfoFont = parseFont("Regular;18", ((1,1),(1,1)))
49                 self.ServiceNumberFont = parseFont("Regular;20", ((1,1),(1,1)))
50                 self.onSelectionChanged = [ ]
51
52         def applySkin(self, desktop, parent):
53                 attribs = [ ]
54                 if self.skinAttributes is not None:
55                         attribs = [ ]
56                         for (attrib, value) in self.skinAttributes:
57                                 if attrib == "foregroundColorMarked":
58                                         self.l.setColor(eListboxServiceContent.markedForeground, parseColor(value))
59                                 elif attrib == "foregroundColorMarkedSelected":
60                                         self.l.setColor(eListboxServiceContent.markedForegroundSelected, parseColor(value))
61                                 elif attrib == "backgroundColorMarked":
62                                         self.l.setColor(eListboxServiceContent.markedBackground, parseColor(value))
63                                 elif attrib == "backgroundColorMarkedSelected":
64                                         self.l.setColor(eListboxServiceContent.markedBackgroundSelected, parseColor(value))
65                                 elif attrib == "foregroundColorServiceNotAvail":
66                                         self.l.setColor(eListboxServiceContent.serviceNotAvail, parseColor(value))
67                                 elif attrib == "serviceItemHeight":
68                                         self.ItemHeight = int(value)
69                                 elif attrib == "serviceNameFont":
70                                         self.ServiceNameFont = parseFont(value, ((1,1),(1,1)))
71                                 elif attrib == "serviceInfoFont":
72                                         self.ServiceInfoFont = parseFont(value, ((1,1),(1,1)))
73                                 elif attrib == "serviceNumberFont":
74                                         self.ServiceNumberFont = parseFont(value, ((1,1),(1,1)))
75                                 else:
76                                         attribs.append((attrib, value))
77                 self.skinAttributes = attribs
78                 return GUIComponent.applySkin(self, desktop, parent)
79
80         def connectSelChanged(self, fnc):
81                 if not fnc in self.onSelectionChanged:
82                         self.onSelectionChanged.append(fnc)
83
84         def disconnectSelChanged(self, fnc):
85                 if fnc in self.onSelectionChanged:
86                         self.onSelectionChanged.remove(fnc)
87
88         def selectionChanged(self):
89                 for x in self.onSelectionChanged:
90                         x()
91
92         def setCurrent(self, ref):
93                 self.l.setCurrent(ref)
94
95         def getCurrent(self):
96                 r = eServiceReference()
97                 self.l.getCurrent(r)
98                 return r
99
100         def atBegin(self):
101                 return self.instance.atBegin()
102
103         def atEnd(self):
104                 return self.instance.atEnd()
105
106         def moveUp(self):
107                 self.instance.moveSelection(self.instance.moveUp)
108
109         def moveDown(self):
110                 self.instance.moveSelection(self.instance.moveDown)
111
112         def moveToChar(self, char):
113                 # TODO fill with life
114                 print "Next char: "
115                 index = self.l.getNextBeginningWithChar(char)
116                 indexup = self.l.getNextBeginningWithChar(upper(char))
117                 if indexup != 0:
118                         if (index > indexup or index == 0):
119                                 index = indexup
120
121                 self.instance.moveSelectionTo(index)
122                 print "Moving to character " + str(char)
123
124         def moveToNextMarker(self):
125                 idx = self.l.getNextMarkerPos()
126                 self.instance.moveSelectionTo(idx)
127
128         def moveToPrevMarker(self):
129                 idx = self.l.getPrevMarkerPos()
130                 self.instance.moveSelectionTo(idx)
131
132         def moveToIndex(self, index):
133                 self.instance.moveSelectionTo(index)
134
135         def getCurrentIndex(self):
136                 return self.instance.getCurrentIndex()
137
138         GUI_WIDGET = eListbox
139         
140         def postWidgetCreate(self, instance):
141                 instance.setWrapAround(True)
142                 instance.setContent(self.l)
143                 instance.selectionChanged.get().append(self.selectionChanged)
144                 self.setMode(self.mode)
145
146         def preWidgetRemove(self, instance):
147                 instance.setContent(None)
148                 instance.selectionChanged.get().remove(self.selectionChanged)
149
150         def getRoot(self):
151                 return self.root
152
153         def getRootServices(self):
154                 serviceHandler = eServiceCenter.getInstance()
155                 list = serviceHandler.list(self.root)
156                 dest = [ ]
157                 if list is not None:
158                         while 1:
159                                 s = list.getNext()
160                                 if s.valid():
161                                         dest.append(s.toString())
162                                 else:
163                                         break
164                 return dest
165
166         def setNumberOffset(self, offset):
167                 self.l.setNumberOffset(offset)
168
169         def setPlayableIgnoreService(self, ref):
170                 self.l.setIgnoreService(ref)
171
172         def setRoot(self, root, justSet=False):
173                 self.root = root
174                 self.l.setRoot(root, justSet)
175                 if not justSet:
176                         self.l.sort()
177                 self.selectionChanged()
178
179         def removeCurrent(self):
180                 self.l.removeCurrent()
181
182         def addService(self, service, beforeCurrent=False):
183                 self.l.addService(service, beforeCurrent)
184
185         def finishFill(self):
186                 self.l.FillFinished()
187                 self.l.sort()
188
189 # stuff for multiple marks (edit mode / later multiepg)
190         def clearMarks(self):
191                 self.l.initMarked()
192
193         def isMarked(self, ref):
194                 return self.l.isMarked(ref)
195
196         def addMarked(self, ref):
197                 self.l.addMarked(ref)
198
199         def removeMarked(self, ref):
200                 self.l.removeMarked(ref)
201
202         def getMarked(self):
203                 i = self.l
204                 i.markedQueryStart()
205                 ref = eServiceReference()
206                 marked = [ ]
207                 while i.markedQueryNext(ref) == 0:
208                         marked.append(ref.toString())
209                         ref = eServiceReference()
210                 return marked
211
212 #just for movemode.. only one marked entry..
213         def setCurrentMarked(self, state):
214                 self.l.setCurrentMarked(state)
215
216         def setMode(self, mode):
217                 self.mode = mode
218                 if mode == self.MODE_NORMAL:
219                         self.l.setItemHeight(self.ItemHeight)
220                         self.l.setVisualMode(eListboxServiceContent.visModeComplex)
221                         self.l.setElementFont(self.l.celServiceName, self.ServiceNameFont)
222                         self.l.setElementPosition(self.l.celServiceName, eRect(0, 0, self.instance.size().width(), self.ItemHeight))
223                         self.l.setElementFont(self.l.celServiceInfo, self.ServiceInfoFont)
224                 else:
225                         self.l.setItemHeight(self.ItemHeight)
226                         self.l.setVisualMode(eListboxServiceContent.visModeComplex)
227                         self.l.setElementFont(self.l.celServiceNumber, self.ServiceNumberFont)
228                         self.l.setElementPosition(self.l.celServiceNumber, eRect(0, 0, 50, self.ItemHeight))
229                         self.l.setElementFont(self.l.celServiceName, self.ServiceNameFont)
230                         self.l.setElementPosition(self.l.celServiceName, eRect(60, 0, self.instance.size().width()-60, self.ItemHeight))
231                         self.l.setElementFont(self.l.celServiceInfo, self.ServiceInfoFont)