service: add sort of servicelist including all required layers
[enigma2.git] / lib / python / Components / NimManager.py
1 from config import config       #global config instance
2
3 from config import configElement
4 from config import ConfigSubsection
5 from config import ConfigSlider
6 from config import configSelection
7 from config import configSatlist
8
9 import xml.dom.minidom
10 from xml.dom import EMPTY_NAMESPACE
11 from skin import elementsWithTag
12 from Tools import XMLTools
13
14 from xml.sax import make_parser
15 from xml.sax.handler import ContentHandler
16
17 class boundFunction:
18         def __init__(self, fnc, *args):
19                 self.fnc = fnc
20                 self.args = args
21         def __call__(self, *args):
22                 self.fnc(*self.args + args)
23
24 class nimSlot:
25         def __init__(self, slotid, nimtype, name):
26                 self.slotid = slotid
27                 self.nimType = nimtype
28                 self.name = name
29
30 class NimManager:
31         class parseSats(ContentHandler):
32                 def __init__(self, satList, satellites):
33                         self.isPointsElement, self.isReboundsElement = 0, 0
34                         self.satList = satList
35                         self.satellites = satellites
36         
37                 def startElement(self, name, attrs):
38                         if (name == "sat"):
39                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
40                                 tpos = attrs.get('position',"")
41                                 tname = attrs.get('name',"")
42                                 self.satellites[tpos] = tname
43                                 self.satList.append( (tname, tpos) )
44
45         def readSatsfromFile(self):
46                 self.satellites = { }
47
48                 #FIXME: path ok???
49                 print "Reading satellites.xml"
50                 parser = make_parser()
51                 satHandler = self.parseSats(self.satList, self.satellites)
52                 parser.setContentHandler(satHandler)
53                 parser.parse('/etc/tuxbox/satellites.xml')
54     
55                 #satdom = xml.dom.minidom.parse('/etc/tuxbox/satellites.xml')
56
57
58                 #for entries in elementsWithTag(satdom.childNodes, "satellites"):
59                         #for x in elementsWithTag(entries.childNodes, "sat"):
60                                 ##print "found sat " + x.getAttribute('name') + " " + str(x.getAttribute('position'))
61                                 #tpos = x.getAttribute('position')
62                                 #tname = x.getAttribute('name')
63                                 ##tname.encode('utf8')
64                                 #self.satellites[tpos] = tname
65                                 #self.satList.append( (tname, tpos) )
66
67         def getNimType(self, slotID):
68                 #FIXME get it from /proc
69                 if slotID == 0:
70                         return self.nimType["DVB-S"]
71                 else:
72                         return self.nimType["empty/unknown"]
73
74         def getNimName(self, slotID):
75                 #FIXME get it from /proc
76                 return "Alps BSBE1"
77
78         def getNimSocketCount(self):
79                 #FIXME get it from /proc
80                 return 2
81
82         def getConfigPrefix(self, slotid):
83                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
84                         
85         def __init__(self):
86                 #use as enum
87                 self.nimType = {                "empty/unknown": -1,
88                                                                                                 "DVB-S": 0,
89                                                                                                 "DVB-C": 1,
90                                                                                                 "DVB-T": 2}
91                 self.satList = [ ]                                                                              
92                                                                                                 
93                 self.readSatsfromFile()                                                                         
94                                                                                                 
95                 self.nimCount = self.getNimSocketCount()
96                 
97                 self.nimslots = [ ]
98                 x = 0
99                 while x < self.nimCount:
100                         tType = self.getNimType(x)
101                         tName = self.getNimName(x)
102                         tNim = nimSlot(x, tType, tName)
103                         self.nimslots.append(tNim)
104                         x += 1
105                 
106                 InitNimManager(self)    #init config stuff
107
108         def nimList(self):
109                 list = [ ]
110                 for slot in self.nimslots:
111                         nimText = "Socket " + ("A", "B", "C", "D")[slot.slotid] + ": "
112                         if slot.nimType == -1:
113                                 nimText += "empty/unknown"
114                         else:
115                                 nimText += slot.name + " ("     
116                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
117                         list.append((nimText, slot))
118                 return list
119
120         #callbacks for c++ config
121         def nimConfigModeChanged(self, slotid, mode):
122                 print "nimConfigModeChanged set to " + str(mode)
123         def nimDiseqcModeChanged(self, slotid, mode):
124                 print "nimDiseqcModeChanged set to " + str(mode)
125         def nimPortAChanged(self, slotid, val):
126                 print "nimDiseqcA set to " + str(val)
127         def nimPortBChanged(self, slotid, val):
128                 print "nimDiseqcB set to " + str(val)
129         def nimPortCChanged(self, slotid, val):
130                 print "nimDiseqcC set to " + str(val)
131         def nimPortDChanged(self, slotid, val):
132                 print "nimDiseqcD set to " + str(val)
133
134
135 def InitNimManager(nimmgr):
136         config.Nims = [ConfigSubsection()] * nimmgr.nimCount
137
138         def nimConfigModeChanged(slotid, configElement):
139                 nimmgr.nimConfigModeChanged(slotid, configElement.value)
140         def nimDiseqcModeChanged(slotid, configElement):
141                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
142                 
143         def nimPortAChanged(slotid, configElement):
144                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
145         def nimPortBChanged(slotid, configElement):
146                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
147         def nimPortCChanged(slotid, configElement):
148                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
149         def nimPortDChanged(slotid, configElement):
150                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
151
152         for slot in nimmgr.nimslots:
153                 x = slot.slotid
154                 cname = nimmgr.getConfigPrefix(x)
155                 
156                 if slot.nimType == nimmgr.nimType["DVB-S"]:
157                         config.Nims[x].configMode = configElement(cname + "configMode",configSelection, 0, ("Simple", "Advanced"));
158                         config.Nims[x].diseqcMode = configElement(cname + "diseqcMode",configSelection, 2, ("Single", "Toneburst A/B", "DiSEqC A/B", "DiSEqC A/B/C/D"));
159                         config.Nims[x].diseqcA = configElement(cname + "diseqcA",configSatlist, 192, nimmgr.satList);
160                         config.Nims[x].diseqcB = configElement(cname + "diseqcB",configSatlist, 130, nimmgr.satList);
161                         config.Nims[x].diseqcC = configElement(cname + "diseqcC",configSatlist, 0, nimmgr.satList);
162                         config.Nims[x].diseqcD = configElement(cname + "diseqcD",configSatlist, 0, nimmgr.satList);
163                         
164                         #perhaps the instance of the slot is more useful?
165                         config.Nims[x].configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
166                         config.Nims[x].diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
167                         config.Nims[x].diseqcA.addNotifier(boundFunction(nimPortAChanged,x))
168                         config.Nims[x].diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
169                         config.Nims[x].diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
170                         config.Nims[x].diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
171                 else:
172                         print "pls add support for this frontend type!"         
173
174 nimmanager = NimManager()