more robust code
[enigma2.git] / lib / python / Screens / ScanSetup.py
1 from Screen import Screen
2 from Screens.DefaultWizard import DefaultWizard
3 from ServiceScan import ServiceScan
4 from Components.config import config, ConfigSubsection, ConfigSelection, \
5         ConfigYesNo, ConfigInteger, getConfigListEntry, ConfigSlider, ConfigEnableDisable
6 from Components.ActionMap import NumberActionMap, ActionMap
7 from Components.ConfigList import ConfigListScreen
8 from Components.NimManager import nimmanager, getConfigSatlist
9 from Components.Label import Label
10 from Tools.Directories import resolveFilename, SCOPE_DEFAULTPARTITIONMOUNTDIR, SCOPE_DEFAULTDIR, SCOPE_DEFAULTPARTITION
11 from Tools.HardwareInfo import HardwareInfo
12 from Screens.MessageBox import MessageBox
13 from enigma import eTimer, eDVBFrontendParametersSatellite, eComponentScan, \
14         eDVBSatelliteEquipmentControl, eDVBFrontendParametersTerrestrial, \
15         eDVBFrontendParametersCable, eConsoleAppContainer, eDVBResourceManager
16
17 def buildTerTransponder(frequency, 
18                 inversion=2, bandwidth = 3, fechigh = 6, feclow = 6,
19                 modulation = 2, transmission = 2, guard = 4,
20                 hierarchy = 4):
21 #       print "freq", frequency, "inv", inversion, "bw", bandwidth, "fech", fechigh, "fecl", feclow, "mod", modulation, "tm", transmission, "guard", guard, "hierarchy", hierarchy
22         parm = eDVBFrontendParametersTerrestrial()
23         parm.frequency = frequency
24         parm.inversion = inversion
25         parm.bandwidth = bandwidth
26         parm.code_rate_HP = fechigh
27         parm.code_rate_LP = feclow
28         parm.modulation = modulation
29         parm.transmission_mode = transmission
30         parm.guard_interval = guard
31         parm.hierarchy = hierarchy
32         return parm
33
34 def getInitialTransponderList(tlist, pos):
35         list = nimmanager.getTransponders(pos)
36         for x in list:
37                 if x[0] == 0:           #SAT
38                         parm = eDVBFrontendParametersSatellite()
39                         parm.frequency = x[1]
40                         parm.symbol_rate = x[2]
41                         parm.polarisation = x[3]
42                         parm.fec = x[4]
43                         parm.inversion = x[7]
44                         parm.orbital_position = pos
45                         parm.system = x[5]
46                         parm.modulation = x[6]
47                         parm.rolloff = x[8]
48                         parm.pilot = x[9]
49                         tlist.append(parm)
50
51 def getInitialCableTransponderList(tlist, nim):
52         list = nimmanager.getTranspondersCable(nim)
53         for x in list:
54                 if x[0] == 1: #CABLE
55                         parm = eDVBFrontendParametersCable()
56                         parm.frequency = x[1]
57                         parm.symbol_rate = x[2]
58                         parm.modulation = x[3]
59                         parm.fec_inner = x[4]
60                         parm.inversion = parm.Inversion_Unknown
61                         #print "frequency:", x[1]
62                         #print "symbol_rate:", x[2]
63                         #print "modulation:", x[3]
64                         #print "fec_inner:", x[4]
65                         #print "inversion:", 2
66                         tlist.append(parm)
67
68 def getInitialTerrestrialTransponderList(tlist, region):
69         list = nimmanager.getTranspondersTerrestrial(region)
70
71         #self.transponders[self.parsedTer].append((2,freq,bw,const,crh,crl,guard,transm,hierarchy,inv))
72
73         #def buildTerTransponder(frequency, inversion = 2, bandwidth = 3, fechigh = 6, feclow = 6,
74                                 #modulation = 2, transmission = 2, guard = 4, hierarchy = 4):
75
76         for x in list:
77                 if x[0] == 2: #TERRESTRIAL
78                         parm = buildTerTransponder(x[1], x[9], x[2], x[4], x[5], x[3], x[7], x[6], x[8])
79                         tlist.append(parm)
80
81 cable_bands = {
82         "DVBC_BAND_EU_VHF_I" : 1 << 0,
83         "DVBC_BAND_EU_MID" : 1 << 1,
84         "DVBC_BAND_EU_VHF_III" : 1 << 2,
85         "DVBC_BAND_EU_SUPER" : 1 << 3,
86         "DVBC_BAND_EU_HYPER" : 1 << 4,
87         "DVBC_BAND_EU_UHF_IV" : 1 << 5,
88         "DVBC_BAND_EU_UHF_V" : 1 << 6,
89         "DVBC_BAND_US_LO" : 1 << 7,
90         "DVBC_BAND_US_MID" : 1 << 8,
91         "DVBC_BAND_US_HI" : 1 << 9,
92         "DVBC_BAND_US_SUPER" : 1 << 10,
93         "DVBC_BAND_US_HYPER" : 1 << 11,
94 }
95
96 class CableTransponderSearchSupport:
97 #       def setCableTransponderSearchResult(self, tlist):
98 #               pass
99
100 #       def cableTransponderSearchFinished(self):
101 #               pass
102
103         def tryGetRawFrontend(self, feid):
104                 res_mgr = eDVBResourceManager.getInstance()
105                 if res_mgr:
106                         raw_channel = res_mgr.allocateRawChannel(self.feid)
107                         if raw_channel:
108                                 frontend = raw_channel.getFrontend()
109                                 if frontend:
110                                         frontend.closeFrontend() # immediate close... 
111                                         del frontend
112                                         del raw_channel
113                                         return True
114                 return False
115
116         def cableTransponderSearchSessionClosed(self, *val):
117                 print "cableTransponderSearchSessionClosed, val", val
118                 self.resetTimeout()
119                 self.cable_search_container.appClosed.remove(self.cableTransponderSearchClosed)
120                 self.cable_search_container.dataAvail.remove(self.getCableTransponderData)
121                 if val and len(val):
122                         if val[0]:
123                                 self.setCableTransponderSearchResult(self.__tlist)
124                         else:
125                                 self.cable_search_container.sendCtrlC()
126                                 self.setCableTransponderSearchResult(None)
127                 self.cable_search_container = None
128                 self.cable_search_session = None
129                 self.__tlist = None
130                 self.cableTransponderSearchFinished()
131
132         def cableTransponderSearchClosed(self, retval):
133                 print "cableTransponderSearch finished", retval
134                 self.cable_search_session.close(True)
135
136         def getCableTransponderData(self, str):
137                 data = str.split()
138                 if len(data):
139                         if data[0] == 'OK':
140                                 print str
141                                 parm = eDVBFrontendParametersCable()
142                                 qam = { "QAM16" : parm.Modulation_QAM16,
143                                         "QAM32" : parm.Modulation_QAM32,
144                                         "QAM64" : parm.Modulation_QAM64,
145                                         "QAM128" : parm.Modulation_QAM128,
146                                         "QAM256" : parm.Modulation_QAM256 }
147                                 inv = { "INVERSION_OFF" : parm.Inversion_Off,
148                                         "INVERSION_ON" : parm.Inversion_On,
149                                         "INVERSION_AUTO" : parm.Inversion_Unknown }
150                                 fec = { "FEC_AUTO" : parm.FEC_Auto,
151                                         "FEC_1_2" : parm.FEC_1_2,
152                                         "FEC_2_3" : parm.FEC_2_3,
153                                         "FEC_3_4" : parm.FEC_3_4,
154                                         "FEC_5_6": parm.FEC_5_6,
155                                         "FEC_7_8" : parm.FEC_7_8,
156                                         "FEC_8_9" : parm.FEC_8_9,
157                                         "FEC_NONE" : parm.FEC_None }
158                                 parm.frequency = int(data[1])
159                                 parm.symbol_rate = int(data[2])
160                                 parm.fec_inner = fec[data[3]]
161                                 parm.modulation = qam[data[4]]
162                                 parm.inversion = inv[data[5]]
163                                 self.__tlist.append(parm)
164                         tmpstr = _("Try to find used Transponders in cable network.. please wait...")
165                         tmpstr += "\n\n"
166                         tmpstr += data[1]
167                         tmpstr += " kHz "
168                         tmpstr += data[0]
169                         self.cable_search_session["text"].setText(tmpstr)
170
171         def setTimeout(self):
172                 try:
173                         self.oldtimeoutvalue = open("/sys/module/dvb_core/parameters/dvb_shutdown_timeout", "r").readline()
174                         open("/sys/module/dvb_core/parameters/dvb_shutdown_timeout", "w").write("0")
175                 except:
176                         print "[info] no /sys/module/dvb_core/parameters/dvb_shutdown_timeout available"                
177                 
178         def resetTimeout(self):
179                 try:
180                         open("/sys/module/dvb_core/parameters/dvb_shutdown_timeout", "w").write(self.oldtimeoutvalue)
181                 except:
182                         print "[info] no /sys/module/dvb_core/parameters/dvb_shutdown_timeout available"
183
184         def startCableTransponderSearch(self, nim_idx):
185                 if not self.tryGetRawFrontend(nim_idx):
186                         self.session.nav.stopService()
187                         if not self.tryGetRawFrontend(nim_idx):
188                                 if self.session.pipshown: # try to disable pip
189                                         self.session.pipshown = False
190                                         del self.session.pip
191                                 if not self.tryGetRawFrontend(nim_idx):
192                                         self.cableTransponderSearchFinished()
193                                         return
194                 self.__tlist = [ ]
195                 self.cable_search_container = eConsoleAppContainer()
196                 self.cable_search_container.appClosed.append(self.cableTransponderSearchClosed)
197                 self.cable_search_container.dataAvail.append(self.getCableTransponderData)
198                 cableConfig = config.Nims[nim_idx].cable
199                 tunername = nimmanager.getNimName(nim_idx)
200                 bus = nimmanager.getI2CDevice(nim_idx)
201                 if bus is None:
202                         print "ERROR: could not get I2C device for nim", nim_idx, "for cable transponder search"
203                         bus = 2
204
205                 if tunername == "CXD1981":
206                         cmd = "cxd1978 --init --scan --verbose --wakeup --inv 2 --bus %d" % bus
207                 else:
208                         cmd = "tda1002x --init --scan --verbose --wakeup --inv 2 --bus %d" % bus
209                 
210                 if cableConfig.scan_type.value == "bands":
211                         cmd += " --scan-bands "
212                         bands = 0
213                         if cableConfig.scan_band_EU_VHF_I.value:
214                                 bands |= cable_bands["DVBC_BAND_EU_VHF_I"]
215                         if cableConfig.scan_band_EU_MID.value:
216                                 bands |= cable_bands["DVBC_BAND_EU_MID"]
217                         if cableConfig.scan_band_EU_VHF_III.value:
218                                 bands |= cable_bands["DVBC_BAND_EU_VHF_III"]
219                         if cableConfig.scan_band_EU_UHF_IV.value:
220                                 bands |= cable_bands["DVBC_BAND_EU_UHF_IV"]
221                         if cableConfig.scan_band_EU_UHF_V.value:
222                                 bands |= cable_bands["DVBC_BAND_EU_UHF_V"]
223                         if cableConfig.scan_band_EU_SUPER.value:
224                                 bands |= cable_bands["DVBC_BAND_EU_SUPER"]
225                         if cableConfig.scan_band_EU_HYPER.value:
226                                 bands |= cable_bands["DVBC_BAND_EU_HYPER"]
227                         if cableConfig.scan_band_US_LOW.value:
228                                 bands |= cable_bands["DVBC_BAND_US_LO"]
229                         if cableConfig.scan_band_US_MID.value:
230                                 bands |= cable_bands["DVBC_BAND_US_MID"]
231                         if cableConfig.scan_band_US_HIGH.value:
232                                 bands |= cable_bands["DVBC_BAND_US_HI"]
233                         if cableConfig.scan_band_US_SUPER.value:
234                                 bands |= cable_bands["DVBC_BAND_US_SUPER"]
235                         if cableConfig.scan_band_US_HYPER.value:
236                                 bands |= cable_bands["DVBC_BAND_US_HYPER"]
237                         cmd += str(bands)
238                 else:
239                         cmd += " --scan-stepsize "
240                         cmd += str(cableConfig.scan_frequency_steps.value)
241                 if cableConfig.scan_mod_qam16.value:
242                         cmd += " --mod 16"
243                 if cableConfig.scan_mod_qam32.value:
244                         cmd += " --mod 32"
245                 if cableConfig.scan_mod_qam64.value:
246                         cmd += " --mod 64"
247                 if cableConfig.scan_mod_qam128.value:
248                         cmd += " --mod 128"
249                 if cableConfig.scan_mod_qam256.value:
250                         cmd += " --mod 256"
251                 if cableConfig.scan_sr_6900.value:
252                         cmd += " --sr 6900000"
253                 if cableConfig.scan_sr_6875.value:
254                         cmd += " --sr 6875000"
255                 if cableConfig.scan_sr_ext1.value > 450:
256                         cmd += " --sr "
257                         cmd += str(cableConfig.scan_sr_ext1.value)
258                         cmd += "000"
259                 if cableConfig.scan_sr_ext2.value > 450:
260                         cmd += " --sr "
261                         cmd += str(cableConfig.scan_sr_ext2.value)
262                         cmd += "000"
263                 print "TDA1002x CMD is", cmd
264
265                 self.cable_search_container.execute(cmd)
266                 tmpstr = _("Try to find used transponders in cable network.. please wait...")
267                 tmpstr += "\n\n..."
268                 self.setTimeout()
269                 self.cable_search_session = self.session.openWithCallback(self.cableTransponderSearchSessionClosed, MessageBox, tmpstr, MessageBox.TYPE_INFO)
270
271 class DefaultSatLists(DefaultWizard):
272         def __init__(self, session, silent = True, showSteps = False):
273                 self.xmlfile = "defaultsatlists.xml"
274                 DefaultWizard.__init__(self, session, silent, showSteps, neededTag = "services")
275                 print "configuredSats:", nimmanager.getConfiguredSats()
276
277         def setDirectory(self):
278                 self.directory = []
279                 self.directory.append(resolveFilename(SCOPE_DEFAULTDIR))
280                 import os
281                 os.system("mount %s %s" % (resolveFilename(SCOPE_DEFAULTPARTITION), resolveFilename(SCOPE_DEFAULTPARTITIONMOUNTDIR)))
282                 self.directory.append(resolveFilename(SCOPE_DEFAULTPARTITIONMOUNTDIR))
283                                 
284         def statusCallback(self, status, progress):
285                 print "statusCallback:", status, progress
286                 from Components.DreamInfoHandler import DreamInfoHandler
287                 if status == DreamInfoHandler.STATUS_DONE:
288                         self["text"].setText(_("The installation of the default services lists is finished.") + "\n\n" + _("Please press OK to continue."))
289                         self.markDone()
290                         self.disableKeys = False        
291
292 class ScanSetup(ConfigListScreen, Screen, CableTransponderSearchSupport):
293         def __init__(self, session):
294                 Screen.__init__(self, session)
295
296                 self.finished_cb = None
297                 self.updateSatList()
298                 self.service = session.nav.getCurrentService()
299                 self.feinfo = None
300                 frontendData = None
301                 if self.service is not None:
302                         self.feinfo = self.service.frontendInfo()
303                         frontendData = self.feinfo and self.feinfo.getAll(True)
304                 
305                 self.createConfig(frontendData)
306
307                 del self.feinfo
308                 del self.service
309
310                 self["actions"] = NumberActionMap(["SetupActions"],
311                 {
312                         "ok": self.keyGo,
313                         "cancel": self.keyCancel,
314                 }, -2)
315
316                 self.statusTimer = eTimer()
317                 self.statusTimer.callback.append(self.updateStatus)
318                 #self.statusTimer.start(5000, True)
319
320                 self.list = []
321                 ConfigListScreen.__init__(self, self.list)
322                 if not self.scan_nims.value == "":
323                         self.createSetup()
324                         self["introduction"] = Label(_("Press OK to start the scan"))
325                 else:
326                         self["introduction"] = Label(_("Nothing to scan!\nPlease setup your tuner settings before you start a service scan."))
327
328         def runAsync(self, finished_cb):
329                 self.finished_cb = finished_cb
330                 self.keyGo()
331
332         def updateSatList(self):
333                 self.satList = []
334                 for slot in nimmanager.nim_slots:
335                         if slot.isCompatible("DVB-S"):
336                                 self.satList.append(nimmanager.getSatListForNim(slot.slot))
337                         else:
338                                 self.satList.append(None)
339
340         def createSetup(self):
341                 self.list = []
342                 self.multiscanlist = []
343                 index_to_scan = int(self.scan_nims.value)
344                 print "ID: ", index_to_scan
345
346                 self.tunerEntry = getConfigListEntry(_("Tuner"), self.scan_nims)
347                 self.list.append(self.tunerEntry)
348                 
349                 if self.scan_nims == [ ]:
350                         return
351                 
352                 self.typeOfScanEntry = None
353                 self.systemEntry = None
354                 self.modulationEntry = None
355                 nim = nimmanager.nim_slots[index_to_scan]
356                 if nim.isCompatible("DVB-S"):
357                         self.typeOfScanEntry = getConfigListEntry(_("Type of scan"), self.scan_type)
358                         self.list.append(self.typeOfScanEntry)
359                 elif nim.isCompatible("DVB-C"):
360                         self.typeOfScanEntry = getConfigListEntry(_("Type of scan"), self.scan_typecable)
361                         self.list.append(self.typeOfScanEntry)
362                 elif nim.isCompatible("DVB-T"):
363                         self.typeOfScanEntry = getConfigListEntry(_("Type of scan"), self.scan_typeterrestrial)
364                         self.list.append(self.typeOfScanEntry)
365
366                 self.scan_networkScan.value = False
367                 if nim.isCompatible("DVB-S"):
368                         if self.scan_type.value == "single_transponder":
369                                 self.updateSatList()
370                                 if nim.isCompatible("DVB-S2"):
371                                         self.systemEntry = getConfigListEntry(_('System'), self.scan_sat.system)
372                                         self.list.append(self.systemEntry)
373                                 else:
374                                         # downgrade to dvb-s, in case a -s2 config was active
375                                         self.scan_sat.system.value = eDVBFrontendParametersSatellite.System_DVB_S
376                                 self.list.append(getConfigListEntry(_('Satellite'), self.scan_satselection[index_to_scan]))
377                                 self.list.append(getConfigListEntry(_('Frequency'), self.scan_sat.frequency))
378                                 self.list.append(getConfigListEntry(_('Inversion'), self.scan_sat.inversion))
379                                 self.list.append(getConfigListEntry(_('Symbol Rate'), self.scan_sat.symbolrate))
380                                 self.list.append(getConfigListEntry(_("Polarity"), self.scan_sat.polarization))
381                                 if self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S:
382                                         self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec))
383                                 elif self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S2:
384                                         self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec_s2))
385                                         self.modulationEntry = getConfigListEntry(_('Modulation'), self.scan_sat.modulation)
386                                         self.list.append(self.modulationEntry)
387                                         self.list.append(getConfigListEntry(_('Rolloff'), self.scan_sat.rolloff))
388                                         self.list.append(getConfigListEntry(_('Pilot'), self.scan_sat.pilot))
389                         elif self.scan_type.value == "single_satellite":
390                                 self.updateSatList()
391                                 print self.scan_satselection[index_to_scan]
392                                 self.list.append(getConfigListEntry(_("Satellite"), self.scan_satselection[index_to_scan]))
393                                 self.scan_networkScan.value = True
394                         elif self.scan_type.value.find("multisat") != -1:
395                                 tlist = []
396                                 SatList = nimmanager.getSatListForNim(index_to_scan)
397                                 for x in SatList:
398                                         if self.Satexists(tlist, x[0]) == 0:
399                                                 tlist.append(x[0])
400                                                 sat = ConfigEnableDisable(default = self.scan_type.value.find("_yes") != -1 and True or False)
401                                                 configEntry = getConfigListEntry(nimmanager.getSatDescription(x[0]), sat)
402                                                 self.list.append(configEntry)
403                                                 self.multiscanlist.append((x[0], sat))
404                                 self.scan_networkScan.value = True
405                 elif nim.isCompatible("DVB-C"):
406                         if self.scan_typecable.value == "single_transponder":
407                                 self.list.append(getConfigListEntry(_("Frequency"), self.scan_cab.frequency))
408                                 self.list.append(getConfigListEntry(_("Inversion"), self.scan_cab.inversion))
409                                 self.list.append(getConfigListEntry(_("Symbol Rate"), self.scan_cab.symbolrate))
410                                 self.list.append(getConfigListEntry(_("Modulation"), self.scan_cab.modulation))
411                                 self.list.append(getConfigListEntry(_("FEC"), self.scan_cab.fec))
412                 elif nim.isCompatible("DVB-T"):
413                         if self.scan_typeterrestrial.value == "single_transponder":
414                                 self.list.append(getConfigListEntry(_("Frequency"), self.scan_ter.frequency))
415                                 self.list.append(getConfigListEntry(_("Inversion"), self.scan_ter.inversion))
416                                 self.list.append(getConfigListEntry(_("Bandwidth"), self.scan_ter.bandwidth))
417                                 self.list.append(getConfigListEntry(_("Code rate high"), self.scan_ter.fechigh))
418                                 self.list.append(getConfigListEntry(_("Code rate low"), self.scan_ter.feclow))
419                                 self.list.append(getConfigListEntry(_("Modulation"), self.scan_ter.modulation))
420                                 self.list.append(getConfigListEntry(_("Transmission mode"), self.scan_ter.transmission))
421                                 self.list.append(getConfigListEntry(_("Guard interval mode"), self.scan_ter.guard))
422                                 self.list.append(getConfigListEntry(_("Hierarchy mode"), self.scan_ter.hierarchy))
423                 self.list.append(getConfigListEntry(_("Network scan"), self.scan_networkScan))
424                 self.list.append(getConfigListEntry(_("Clear before scan"), self.scan_clearallservices))
425                 self.list.append(getConfigListEntry(_("Only Free scan"), self.scan_onlyfree))
426                 self["config"].list = self.list
427                 self["config"].l.setList(self.list)
428
429         def Satexists(self, tlist, pos):
430                 for x in tlist:
431                         if x == pos:
432                                 return 1
433                 return 0
434
435         def newConfig(self):
436                 cur = self["config"].getCurrent()
437                 print "cur is", cur
438                 if cur == self.typeOfScanEntry or \
439                         cur == self.tunerEntry or \
440                         cur == self.systemEntry or \
441                         (self.modulationEntry and self.systemEntry[1].value == eDVBFrontendParametersSatellite.System_DVB_S2 and cur == self.modulationEntry):
442                         self.createSetup()
443
444         def createConfig(self, frontendData):
445                                                            #("Type", frontendData["system"], TYPE_TEXT),
446                                            #("Modulation", frontendData["modulation"], TYPE_TEXT),
447                                            #("Orbital position", frontendData["orbital_position"], TYPE_VALUE_DEC),
448                                            #("Frequency", frontendData["frequency"], TYPE_VALUE_DEC),
449                                            #("Symbolrate", frontendData["symbol_rate"], TYPE_VALUE_DEC),
450                                            #("Polarization", frontendData["polarization"], TYPE_TEXT),
451                                            #("Inversion", frontendData["inversion"], TYPE_TEXT),
452                                            #("FEC inner", frontendData["fec_inner"], TYPE_TEXT),
453                                                 #)
454                 #elif frontendData["tuner_type"] == "DVB-C":
455                         #return ( ("NIM", ['A', 'B', 'C', 'D'][frontendData["tuner_number"]], TYPE_TEXT),
456                                            #("Type", frontendData["tuner_type"], TYPE_TEXT),
457                                            #("Frequency", frontendData["frequency"], TYPE_VALUE_DEC),
458                                            #("Symbolrate", frontendData["symbol_rate"], TYPE_VALUE_DEC),
459                                            #("Modulation", frontendData["modulation"], TYPE_TEXT),
460                                            #("Inversion", frontendData["inversion"], TYPE_TEXT),
461                         #                  ("FEC inner", frontendData["fec_inner"], TYPE_TEXT),
462                                                 #)
463                 #elif frontendData["tuner_type"] == "DVB-T":
464                         #return ( ("NIM", ['A', 'B', 'C', 'D'][frontendData["tuner_number"]], TYPE_TEXT),
465                                            #("Type", frontendData["tuner_type"], TYPE_TEXT),
466                                            #("Frequency", frontendData["frequency"], TYPE_VALUE_DEC),
467                                            #("Inversion", frontendData["inversion"], TYPE_TEXT),
468                                            #("Bandwidth", frontendData["bandwidth"], TYPE_VALUE_DEC),
469                                            #("CodeRateLP", frontendData["code_rate_lp"], TYPE_TEXT),
470                                            #("CodeRateHP", frontendData["code_rate_hp"], TYPE_TEXT),
471                                            #("Constellation", frontendData["constellation"], TYPE_TEXT),
472                                            #("Transmission Mode", frontendData["transmission_mode"], TYPE_TEXT),
473                                            #("Guard Interval", frontendData["guard_interval"], TYPE_TEXT),
474                                            #("Hierarchy Inform.", frontendData["hierarchy_information"], TYPE_TEXT),
475                         defaultSat = {
476                                 "orbpos": 192,
477                                 "system": eDVBFrontendParametersSatellite.System_DVB_S,
478                                 "frequency": 11836,
479                                 "inversion": eDVBFrontendParametersSatellite.Inversion_Unknown,
480                                 "symbolrate": 27500,
481                                 "polarization": eDVBFrontendParametersSatellite.Polarisation_Horizontal,
482                                 "fec": eDVBFrontendParametersSatellite.FEC_Auto,
483                                 "fec_s2": eDVBFrontendParametersSatellite.FEC_9_10,
484                                 "modulation": eDVBFrontendParametersSatellite.Modulation_QPSK }
485                         defaultCab = {
486                                 "frequency": 466,
487                                 "inversion": eDVBFrontendParametersCable.Inversion_Unknown,
488                                 "modulation": eDVBFrontendParametersCable.Modulation_QAM64,
489                                 "fec": eDVBFrontendParametersCable.FEC_Auto,
490                                 "symbolrate": 6900 }
491                         defaultTer = {
492                                 "frequency" : 466000,
493                                 "inversion" : eDVBFrontendParametersTerrestrial.Inversion_Unknown,
494                                 "bandwidth" : eDVBFrontendParametersTerrestrial.Bandwidth_7MHz,
495                                 "fechigh" : eDVBFrontendParametersTerrestrial.FEC_Auto,
496                                 "feclow" : eDVBFrontendParametersTerrestrial.FEC_Auto,
497                                 "modulation" : eDVBFrontendParametersTerrestrial.Modulation_Auto,
498                                 "transmission_mode" : eDVBFrontendParametersTerrestrial.TransmissionMode_Auto,
499                                 "guard_interval" : eDVBFrontendParametersTerrestrial.GuardInterval_Auto,
500                                 "hierarchy": eDVBFrontendParametersTerrestrial.Hierarchy_Auto }
501
502                         if frontendData is not None:
503                                 ttype = frontendData.get("tuner_type", "UNKNOWN")
504                                 if ttype == "DVB-S":
505                                         defaultSat["system"] = frontendData.get("system", eDVBFrontendParametersSatellite.System_DVB_S)
506                                         defaultSat["frequency"] = frontendData.get("frequency", 0) / 1000
507                                         defaultSat["inversion"] = frontendData.get("inversion", eDVBFrontendParametersSatellite.Inversion_Unknown)
508                                         defaultSat["symbolrate"] = frontendData.get("symbol_rate", 0) / 1000
509                                         defaultSat["polarization"] = frontendData.get("polarization", eDVBFrontendParametersSatellite.Polarisation_Horizontal)
510                                         if defaultSat["system"] == eDVBFrontendParametersSatellite.System_DVB_S2:
511                                                 defaultSat["fec_s2"] = frontendData.get("fec_inner", eDVBFrontendParametersSatellite.FEC_Auto)
512                                                 defaultSat["rolloff"] = frontendData.get("rolloff", eDVBFrontendParametersSatellite.RollOff_alpha_0_35)
513                                                 defaultSat["pilot"] = frontendData.get("pilot", eDVBFrontendParametersSatellite.Pilot_Unknown)
514                                         else:
515                                                 defaultSat["fec"] = frontendData.get("fec_inner", eDVBFrontendParametersSatellite.FEC_Auto)
516                                         defaultSat["modulation"] = frontendData.get("modulation", eDVBFrontendParametersSatellite.Modulation_QPSK)
517                                         defaultSat["orbpos"] = frontendData.get("orbital_position", 0)
518                                 elif ttype == "DVB-C":
519                                         defaultCab["frequency"] = frontendData.get("frequency", 0) / 1000
520                                         defaultCab["symbolrate"] = frontendData.get("symbol_rate", 0) / 1000
521                                         defaultCab["inversion"] = frontendData.get("inversion", eDVBFrontendParametersCable.Inversion_Unknown)
522                                         defaultCab["fec"] = frontendData.get("fec_inner", eDVBFrontendParametersCable.FEC_Auto)
523                                         defaultCab["modulation"] = frontendData.get("modulation", eDVBFrontendParametersCable.Modulation_QAM16)
524                                 elif ttype == "DVB-T":
525                                         defaultTer["frequency"] = frontendData.get("frequency", 0)
526                                         defaultTer["inversion"] = frontendData.get("inversion", eDVBFrontendParametersTerrestrial.Inversion_Unknown)
527                                         defaultTer["bandwidth"] = frontendData.get("bandwidth", eDVBFrontendParametersTerrestrial.Bandwidth_7MHz)
528                                         defaultTer["fechigh"] = frontendData.get("code_rate_hp", eDVBFrontendParametersTerrestrial.FEC_Auto)
529                                         defaultTer["feclow"] = frontendData.get("code_rate_lp", eDVBFrontendParametersTerrestrial.FEC_Auto)
530                                         defaultTer["modulation"] = frontendData.get("constellation", eDVBFrontendParametersTerrestrial.Modulation_Auto)
531                                         defaultTer["transmission_mode"] = frontendData.get("transmission_mode", eDVBFrontendParametersTerrestrial.TransmissionMode_Auto)
532                                         defaultTer["guard_interval"] = frontendData.get("guard_interval", eDVBFrontendParametersTerrestrial.GuardInterval_Auto)
533                                         defaultTer["hierarchy"] = frontendData.get("hierarchy_information", eDVBFrontendParametersTerrestrial.Hierarchy_Auto)
534
535                         self.scan_sat = ConfigSubsection()
536                         self.scan_cab = ConfigSubsection()
537                         self.scan_ter = ConfigSubsection()
538
539                         self.scan_type = ConfigSelection(default = "single_transponder", choices = [("single_transponder", _("Single transponder")), ("single_satellite", _("Single satellite")), ("multisat", _("Multisat")), ("multisat_yes", _("Multisat"))])
540                         self.scan_typecable = ConfigSelection(default = "single_transponder", choices = [("single_transponder", _("Single transponder")), ("complete", _("Complete"))])
541                         self.scan_typeterrestrial = ConfigSelection(default = "single_transponder", choices = [("single_transponder", _("Single transponder")), ("complete", _("Complete"))])
542                         self.scan_clearallservices = ConfigSelection(default = "no", choices = [("no", _("no")), ("yes", _("yes")), ("yes_hold_feeds", _("yes (keep feeds)"))])
543                         self.scan_onlyfree = ConfigYesNo(default = False)
544                         self.scan_networkScan = ConfigYesNo(default = False)
545
546                         nim_list = []
547                         # collect all nims which are *not* set to "nothing"
548                         for n in nimmanager.nim_slots:
549                                 if n.config_mode == "nothing":
550                                         continue
551                                 if n.config_mode == "advanced" and len(nimmanager.getSatListForNim(n.slot)) < 1:
552                                         continue
553                                 if n.config_mode in ("loopthrough", "satposdepends"):
554                                         root_id = nimmanager.sec.getRoot(n.slot_id, int(n.config.connectedTo.value))
555                                         if n.type == nimmanager.nim_slots[root_id].type: # check if connected from a DVB-S to DVB-S2 Nim or vice versa
556                                                 continue
557                                 nim_list.append((str(n.slot), n.friendly_full_description))
558
559                         self.scan_nims = ConfigSelection(choices = nim_list)
560
561                         # status
562                         self.scan_snr = ConfigSlider()
563                         self.scan_snr.enabled = False
564                         self.scan_agc = ConfigSlider()
565                         self.scan_agc.enabled = False
566                         self.scan_ber = ConfigSlider()
567                         self.scan_ber.enabled = False
568
569                         # sat
570                         self.scan_sat.system = ConfigSelection(default = defaultSat["system"], choices = [
571                                 (eDVBFrontendParametersSatellite.System_DVB_S, _("DVB-S")),
572                                 (eDVBFrontendParametersSatellite.System_DVB_S2, _("DVB-S2"))])
573                         self.scan_sat.frequency = ConfigInteger(default = defaultSat["frequency"], limits = (1, 99999))
574                         self.scan_sat.inversion = ConfigSelection(default = defaultSat["inversion"], choices = [
575                                 (eDVBFrontendParametersSatellite.Inversion_Off, _("off")),
576                                 (eDVBFrontendParametersSatellite.Inversion_On, _("on")),
577                                 (eDVBFrontendParametersSatellite.Inversion_Unknown, _("Auto"))])
578                         self.scan_sat.symbolrate = ConfigInteger(default = defaultSat["symbolrate"], limits = (1, 99999))
579                         self.scan_sat.polarization = ConfigSelection(default = defaultSat["polarization"], choices = [
580                                 (eDVBFrontendParametersSatellite.Polarisation_Horizontal, _("horizontal")),
581                                 (eDVBFrontendParametersSatellite.Polarisation_Vertical, _("vertical")),
582                                 (eDVBFrontendParametersSatellite.Polarisation_CircularLeft, _("circular left")),
583                                 (eDVBFrontendParametersSatellite.Polarisation_CircularRight, _("circular right"))])
584                         self.scan_sat.fec = ConfigSelection(default = defaultSat["fec"], choices = [
585                                 (eDVBFrontendParametersSatellite.FEC_Auto, _("Auto")),
586                                 (eDVBFrontendParametersSatellite.FEC_1_2, "1/2"),
587                                 (eDVBFrontendParametersSatellite.FEC_2_3, "2/3"),
588                                 (eDVBFrontendParametersSatellite.FEC_3_4, "3/4"),
589                                 (eDVBFrontendParametersSatellite.FEC_5_6, "5/6"),
590                                 (eDVBFrontendParametersSatellite.FEC_7_8, "7/8"),
591                                 (eDVBFrontendParametersSatellite.FEC_None, _("None"))])
592                         self.scan_sat.fec_s2 = ConfigSelection(default = defaultSat["fec_s2"], choices = [
593                                 (eDVBFrontendParametersSatellite.FEC_1_2, "1/2"),
594                                 (eDVBFrontendParametersSatellite.FEC_2_3, "2/3"),
595                                 (eDVBFrontendParametersSatellite.FEC_3_4, "3/4"),
596                                 (eDVBFrontendParametersSatellite.FEC_3_5, "3/5"),
597                                 (eDVBFrontendParametersSatellite.FEC_4_5, "4/5"),
598                                 (eDVBFrontendParametersSatellite.FEC_5_6, "5/6"),
599                                 (eDVBFrontendParametersSatellite.FEC_7_8, "7/8"),
600                                 (eDVBFrontendParametersSatellite.FEC_8_9, "8/9"),
601                                 (eDVBFrontendParametersSatellite.FEC_9_10, "9/10")])
602                         self.scan_sat.modulation = ConfigSelection(default = defaultSat["modulation"], choices = [
603                                 (eDVBFrontendParametersSatellite.Modulation_QPSK, "QPSK"),
604                                 (eDVBFrontendParametersSatellite.Modulation_8PSK, "8PSK")])
605                         self.scan_sat.rolloff = ConfigSelection(default = defaultSat.get("rolloff", eDVBFrontendParametersSatellite.RollOff_alpha_0_35), choices = [
606                                 (eDVBFrontendParametersSatellite.RollOff_alpha_0_35, "0.35"),
607                                 (eDVBFrontendParametersSatellite.RollOff_alpha_0_25, "0.25"),
608                                 (eDVBFrontendParametersSatellite.RollOff_alpha_0_20, "0.20")])
609                         self.scan_sat.pilot = ConfigSelection(default = defaultSat.get("pilot", eDVBFrontendParametersSatellite.Pilot_Unknown), choices = [
610                                 (eDVBFrontendParametersSatellite.Pilot_Off, _("off")),
611                                 (eDVBFrontendParametersSatellite.Pilot_On, _("on")),
612                                 (eDVBFrontendParametersSatellite.Pilot_Unknown, _("Auto"))])
613
614                         # cable
615                         self.scan_cab.frequency = ConfigInteger(default = defaultCab["frequency"], limits = (50, 999))
616                         self.scan_cab.inversion = ConfigSelection(default = defaultCab["inversion"], choices = [
617                                 (eDVBFrontendParametersCable.Inversion_Off, _("off")),
618                                 (eDVBFrontendParametersCable.Inversion_On, _("on")),
619                                 (eDVBFrontendParametersCable.Inversion_Unknown, _("Auto"))])
620                         self.scan_cab.modulation = ConfigSelection(default = defaultCab["modulation"], choices = [
621                                 (eDVBFrontendParametersCable.Modulation_QAM16, "16-QAM"),
622                                 (eDVBFrontendParametersCable.Modulation_QAM32, "32-QAM"),
623                                 (eDVBFrontendParametersCable.Modulation_QAM64, "64-QAM"),
624                                 (eDVBFrontendParametersCable.Modulation_QAM128, "128-QAM"),
625                                 (eDVBFrontendParametersCable.Modulation_QAM256, "256-QAM")])
626                         self.scan_cab.fec = ConfigSelection(default = defaultCab["fec"], choices = [
627                                 (eDVBFrontendParametersCable.FEC_Auto, _("Auto")),
628                                 (eDVBFrontendParametersCable.FEC_1_2, "1/2"),
629                                 (eDVBFrontendParametersCable.FEC_2_3, "2/3"),
630                                 (eDVBFrontendParametersCable.FEC_3_4, "3/4"),
631                                 (eDVBFrontendParametersCable.FEC_5_6, "5/6"),
632                                 (eDVBFrontendParametersCable.FEC_7_8, "7/8"),
633                                 (eDVBFrontendParametersCable.FEC_8_9, "8/9"),
634                                 (eDVBFrontendParametersCable.FEC_None, _("None"))])
635                         self.scan_cab.symbolrate = ConfigInteger(default = defaultCab["symbolrate"], limits = (1, 99999))
636
637                         # terrestial
638                         self.scan_ter.frequency = ConfigInteger(default = 466000, limits = (50000, 999000))
639                         self.scan_ter.inversion = ConfigSelection(default = defaultTer["inversion"], choices = [
640                                 (eDVBFrontendParametersTerrestrial.Inversion_Off, _("off")),
641                                 (eDVBFrontendParametersTerrestrial.Inversion_On, _("on")),
642                                 (eDVBFrontendParametersTerrestrial.Inversion_Unknown, _("Auto"))])
643                         # WORKAROUND: we can't use BW-auto
644                         self.scan_ter.bandwidth = ConfigSelection(default = defaultTer["bandwidth"], choices = [
645                                 (eDVBFrontendParametersTerrestrial.Bandwidth_8MHz, "8MHz"),
646                                 (eDVBFrontendParametersTerrestrial.Bandwidth_7MHz, "7MHz"),
647                                 (eDVBFrontendParametersTerrestrial.Bandwidth_6MHz, "6MHz")])
648                         #, (eDVBFrontendParametersTerrestrial.Bandwidth_Auto, _("Auto"))))
649                         self.scan_ter.fechigh = ConfigSelection(default = defaultTer["fechigh"], choices = [
650                                 (eDVBFrontendParametersTerrestrial.FEC_1_2, "1/2"),
651                                 (eDVBFrontendParametersTerrestrial.FEC_2_3, "2/3"),
652                                 (eDVBFrontendParametersTerrestrial.FEC_3_4, "3/4"),
653                                 (eDVBFrontendParametersTerrestrial.FEC_5_6, "5/6"),
654                                 (eDVBFrontendParametersTerrestrial.FEC_7_8, "7/8"),
655                                 (eDVBFrontendParametersTerrestrial.FEC_Auto, _("Auto"))])
656                         self.scan_ter.feclow = ConfigSelection(default = defaultTer["feclow"], choices = [
657                                 (eDVBFrontendParametersTerrestrial.FEC_1_2, "1/2"),
658                                 (eDVBFrontendParametersTerrestrial.FEC_2_3, "2/3"),
659                                 (eDVBFrontendParametersTerrestrial.FEC_3_4, "3/4"),
660                                 (eDVBFrontendParametersTerrestrial.FEC_5_6, "5/6"),
661                                 (eDVBFrontendParametersTerrestrial.FEC_7_8, "7/8"),
662                                 (eDVBFrontendParametersTerrestrial.FEC_Auto, _("Auto"))])
663                         self.scan_ter.modulation = ConfigSelection(default = defaultTer["modulation"], choices = [
664                                 (eDVBFrontendParametersTerrestrial.Modulation_QPSK, "QPSK"),
665                                 (eDVBFrontendParametersTerrestrial.Modulation_QAM16, "QAM16"),
666                                 (eDVBFrontendParametersTerrestrial.Modulation_QAM64, "QAM64"),
667                                 (eDVBFrontendParametersTerrestrial.Modulation_Auto, _("Auto"))])
668                         self.scan_ter.transmission = ConfigSelection(default = defaultTer["transmission_mode"], choices = [
669                                 (eDVBFrontendParametersTerrestrial.TransmissionMode_2k, "2K"),
670                                 (eDVBFrontendParametersTerrestrial.TransmissionMode_8k, "8K"),
671                                 (eDVBFrontendParametersTerrestrial.TransmissionMode_Auto, _("Auto"))])
672                         self.scan_ter.guard = ConfigSelection(default = defaultTer["guard_interval"], choices = [
673                                 (eDVBFrontendParametersTerrestrial.GuardInterval_1_32, "1/32"),
674                                 (eDVBFrontendParametersTerrestrial.GuardInterval_1_16, "1/16"),
675                                 (eDVBFrontendParametersTerrestrial.GuardInterval_1_8, "1/8"),
676                                 (eDVBFrontendParametersTerrestrial.GuardInterval_1_4, "1/4"),
677                                 (eDVBFrontendParametersTerrestrial.GuardInterval_Auto, _("Auto"))])
678                         self.scan_ter.hierarchy = ConfigSelection(default = defaultTer["hierarchy"], choices = [
679                                 (eDVBFrontendParametersTerrestrial.Hierarchy_None, _("None")),
680                                 (eDVBFrontendParametersTerrestrial.Hierarchy_1, "1"),
681                                 (eDVBFrontendParametersTerrestrial.Hierarchy_2, "2"),
682                                 (eDVBFrontendParametersTerrestrial.Hierarchy_4, "4"),
683                                 (eDVBFrontendParametersTerrestrial.Hierarchy_Auto, _("Auto"))])
684
685                         self.scan_scansat = {}
686                         for sat in nimmanager.satList:
687                                 #print sat[1]
688                                 self.scan_scansat[sat[0]] = ConfigYesNo(default = False)
689
690                         self.scan_satselection = []
691                         for slot in nimmanager.nim_slots:
692                                 if slot.isCompatible("DVB-S"):
693                                         self.scan_satselection.append(getConfigSatlist(defaultSat["orbpos"], self.satList[slot.slot]))
694                                 else:
695                                         self.scan_satselection.append(None)
696
697                         return True
698
699         def keyLeft(self):
700                 ConfigListScreen.keyLeft(self)
701                 self.newConfig()
702
703         def keyRight(self):
704                 ConfigListScreen.keyRight(self)
705                 self.newConfig()
706
707         def updateStatus(self):
708                 print "updatestatus"
709
710         def addSatTransponder(self, tlist, frequency, symbol_rate, polarisation, fec, inversion, orbital_position, system, modulation, rolloff, pilot):
711                 print "Add Sat: frequ: " + str(frequency) + " symbol: " + str(symbol_rate) + " pol: " + str(polarisation) + " fec: " + str(fec) + " inversion: " + str(inversion) + " modulation: " + str(modulation) + " system: " + str(system) + " rolloff" + str(rolloff) + " pilot" + str(pilot)
712                 print "orbpos: " + str(orbital_position)
713                 parm = eDVBFrontendParametersSatellite()
714                 parm.modulation = modulation
715                 parm.system = system
716                 parm.frequency = frequency * 1000
717                 parm.symbol_rate = symbol_rate * 1000
718                 parm.polarisation = polarisation
719                 parm.fec = fec
720                 parm.inversion = inversion
721                 parm.orbital_position = orbital_position
722                 parm.rolloff = rolloff
723                 parm.pilot = pilot
724                 tlist.append(parm)
725
726         def addCabTransponder(self, tlist, frequency, symbol_rate, modulation, fec, inversion):
727                 print "Add Cab: frequ: " + str(frequency) + " symbol: " + str(symbol_rate) + " pol: " + str(modulation) + " fec: " + str(fec) + " inversion: " + str(inversion)
728                 parm = eDVBFrontendParametersCable()
729                 parm.frequency = frequency * 1000
730                 parm.symbol_rate = symbol_rate * 1000
731                 parm.modulation = modulation
732                 parm.fec = fec
733                 parm.inversion = inversion
734                 tlist.append(parm)
735
736         def addTerTransponder(self, tlist, *args, **kwargs):
737                 tlist.append(buildTerTransponder(*args, **kwargs))
738
739         def keyGo(self):
740                 if self.scan_nims.value == "":
741                         return
742                 tlist = []
743                 flags = None
744                 startScan = True
745                 removeAll = True
746                 index_to_scan = int(self.scan_nims.value)
747                 
748                 if self.scan_nims == [ ]:
749                         self.session.open(MessageBox, _("No tuner is enabled!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
750                         return
751
752                 nim = nimmanager.nim_slots[index_to_scan]
753                 print "nim", nim.slot
754                 if nim.isCompatible("DVB-S"):
755                         print "is compatible with DVB-S"
756                         if self.scan_type.value == "single_transponder":
757                                 # these lists are generated for each tuner, so this has work.
758                                 assert len(self.satList) > index_to_scan
759                                 assert len(self.scan_satselection) > index_to_scan
760                                 
761                                 nimsats = self.satList[index_to_scan]
762                                 selsatidx = self.scan_satselection[index_to_scan].index
763
764                                 # however, the satList itself could be empty. in that case, "index" is 0 (for "None").
765                                 if len(nimsats):
766                                         orbpos = nimsats[selsatidx][0]
767                                         if self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S:
768                                                 fec = self.scan_sat.fec.value
769                                         else:
770                                                 fec = self.scan_sat.fec_s2.value
771                                         print "add sat transponder"
772                                         self.addSatTransponder(tlist, self.scan_sat.frequency.value,
773                                                                 self.scan_sat.symbolrate.value,
774                                                                 self.scan_sat.polarization.value,
775                                                                 fec,
776                                                                 self.scan_sat.inversion.value,
777                                                                 orbpos,
778                                                                 self.scan_sat.system.value,
779                                                                 self.scan_sat.modulation.value,
780                                                                 self.scan_sat.rolloff.value,
781                                                                 self.scan_sat.pilot.value)
782                                 removeAll = False
783                         elif self.scan_type.value == "single_satellite":
784                                 sat = self.satList[index_to_scan][self.scan_satselection[index_to_scan].index]
785                                 getInitialTransponderList(tlist, sat[0])
786                         elif self.scan_type.value.find("multisat") != -1:
787                                 SatList = nimmanager.getSatListForNim(index_to_scan)
788                                 for x in self.multiscanlist:
789                                         if x[1].value:
790                                                 print "   " + str(x[0])
791                                                 getInitialTransponderList(tlist, x[0])
792
793                 elif nim.isCompatible("DVB-C"):
794                         if self.scan_typecable.value == "single_transponder":
795                                 self.addCabTransponder(tlist, self.scan_cab.frequency.value,
796                                                                                           self.scan_cab.symbolrate.value,
797                                                                                           self.scan_cab.modulation.value,
798                                                                                           self.scan_cab.fec.value,
799                                                                                           self.scan_cab.inversion.value)
800                                 removeAll = False
801                         elif self.scan_typecable.value == "complete":
802                                 if config.Nims[index_to_scan].cable.scan_type.value == "provider":
803                                         getInitialCableTransponderList(tlist, index_to_scan)
804                                 else:
805                                         startScan = False
806
807                 elif nim.isCompatible("DVB-T"):
808                         if self.scan_typeterrestrial.value == "single_transponder":
809                                 self.addTerTransponder(tlist,
810                                                 self.scan_ter.frequency.value * 1000,
811                                                 inversion = self.scan_ter.inversion.value,
812                                                 bandwidth = self.scan_ter.bandwidth.value,
813                                                 fechigh = self.scan_ter.fechigh.value,
814                                                 feclow = self.scan_ter.feclow.value,
815                                                 modulation = self.scan_ter.modulation.value,
816                                                 transmission = self.scan_ter.transmission.value,
817                                                 guard = self.scan_ter.guard.value,
818                                                 hierarchy = self.scan_ter.hierarchy.value)
819                                 removeAll = False
820                         elif self.scan_typeterrestrial.value == "complete":
821                                 getInitialTerrestrialTransponderList(tlist, nimmanager.getTerrestrialDescription(index_to_scan))
822
823                 flags = self.scan_networkScan.value and eComponentScan.scanNetworkSearch or 0
824
825                 tmp = self.scan_clearallservices.value
826                 if tmp == "yes":
827                         flags |= eComponentScan.scanRemoveServices
828                 elif tmp == "yes_hold_feeds":
829                         flags |= eComponentScan.scanRemoveServices
830                         flags |= eComponentScan.scanDontRemoveFeeds
831
832                 if tmp != "no" and not removeAll:
833                         flags |= eComponentScan.scanDontRemoveUnscanned
834
835                 if self.scan_onlyfree.value:
836                         flags |= eComponentScan.scanOnlyFree
837
838                 for x in self["config"].list:
839                         x[1].save()
840
841                 if startScan:
842                         self.startScan(tlist, flags, index_to_scan)
843                 else:
844                         self.flags = flags
845                         self.feid = index_to_scan
846                         self.tlist = []
847                         self.startCableTransponderSearch(self.feid)
848
849         def setCableTransponderSearchResult(self, tlist):
850                 self.tlist = tlist
851
852         def cableTransponderSearchFinished(self):
853                 if self.tlist is None:
854                         self.tlist = []
855                 else:
856                         self.startScan(self.tlist, self.flags, self.feid)
857
858         def startScan(self, tlist, flags, feid):
859                 if len(tlist):
860                         # flags |= eComponentScan.scanSearchBAT
861                         if self.finished_cb:
862                                 self.session.openWithCallback(self.finished_cb, ServiceScan, [{"transponders": tlist, "feid": feid, "flags": flags}])
863                         else:
864                                 self.session.open(ServiceScan, [{"transponders": tlist, "feid": feid, "flags": flags}])
865                 else:
866                         if self.finished_cb:
867                                 self.session.openWithCallback(self.finished_cb, MessageBox, _("Nothing to scan!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
868                         else:
869                                 self.session.open(MessageBox, _("Nothing to scan!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
870
871         def keyCancel(self):
872                 for x in self["config"].list:
873                         x[1].cancel()
874                 self.close()
875
876 class ScanSimple(ConfigListScreen, Screen, CableTransponderSearchSupport):
877         def getNetworksForNim(self, nim):
878                 if nim.isCompatible("DVB-S"):
879                         networks = nimmanager.getSatListForNim(nim.slot)
880                 elif not nim.empty:
881                         networks = [ nim.type ] # "DVB-C" or "DVB-T". TODO: seperate networks for different C/T tuners, if we want to support that.
882                 else:
883                         # empty tuners provide no networks.
884                         networks = [ ]
885                 return networks
886
887         def __init__(self, session):
888                 Screen.__init__(self, session)
889
890                 self["actions"] = ActionMap(["SetupActions"],
891                 {
892                         "ok": self.keyGo,
893                         "cancel": self.keyCancel,
894                 }, -2)
895
896                 self.list = []
897                 tlist = []
898
899                 known_networks = [ ]
900                 nims_to_scan = [ ]
901                 self.finished_cb = None
902
903                 for nim in nimmanager.nim_slots:
904                         # collect networks provided by this tuner
905
906                         need_scan = False
907                         networks = self.getNetworksForNim(nim)
908                         
909                         print "nim %d provides" % nim.slot, networks
910                         print "known:", known_networks
911
912                         # we only need to scan on the first tuner which provides a network.
913                         # this gives the first tuner for each network priority for scanning.
914                         for x in networks:
915                                 if x not in known_networks:
916                                         need_scan = True
917                                         print x, "not in ", known_networks
918                                         known_networks.append(x)
919                                         
920                         # don't offer to scan nims if nothing is connected
921                         if not nimmanager.somethingConnected(nim.slot):
922                                 need_scan = False                               
923
924                         if need_scan:
925                                 nims_to_scan.append(nim)
926
927                 # we save the config elements to use them on keyGo
928                 self.nim_enable = [ ]
929
930                 if len(nims_to_scan):
931                         self.scan_clearallservices = ConfigSelection(default = "yes", choices = [("no", _("no")), ("yes", _("yes")), ("yes_hold_feeds", _("yes (keep feeds)"))])
932                         self.list.append(getConfigListEntry(_("Clear before scan"), self.scan_clearallservices))
933
934                         for nim in nims_to_scan:
935                                 nimconfig = ConfigYesNo(default = True)
936                                 nimconfig.nim_index = nim.slot
937                                 self.nim_enable.append(nimconfig)
938                                 self.list.append(getConfigListEntry(_("Scan ") + nim.slot_name + " (" + nim.friendly_type + ")", nimconfig))
939
940                 ConfigListScreen.__init__(self, self.list)
941                 self["header"] = Label(_("Automatic Scan"))
942                 self["footer"] = Label(_("Press OK to scan"))
943
944         def runAsync(self, finished_cb):
945                 self.finished_cb = finished_cb
946                 self.keyGo()
947
948         def keyGo(self):
949                 self.scanList = []
950                 self.known_networks = set()
951                 self.nim_iter=0
952                 self.buildTransponderList()
953
954         def buildTransponderList(self): # this method is called multiple times because of asynchronous stuff
955                 APPEND_NOW = 0
956                 SEARCH_CABLE_TRANSPONDERS = 1
957                 action = APPEND_NOW
958
959                 n = self.nim_iter < len(self.nim_enable) and self.nim_enable[self.nim_iter] or None
960                 self.nim_iter += 1
961                 if n:
962                         if n.value: # check if nim is enabled
963                                 flags = 0
964                                 nim = nimmanager.nim_slots[n.nim_index]
965                                 networks = set(self.getNetworksForNim(nim))
966
967                                 # don't scan anything twice
968                                 networks.discard(self.known_networks)
969
970                                 tlist = [ ]
971                                 if nim.isCompatible("DVB-S"):
972                                         # get initial transponders for each satellite to be scanned
973                                         for sat in networks:
974                                                 getInitialTransponderList(tlist, sat[0])
975                                 elif nim.isCompatible("DVB-C"):
976                                         if config.Nims[nim.slot].cable.scan_type.value == "provider":
977                                                 getInitialCableTransponderList(tlist, nim.slot)
978                                         else:
979                                                 action = SEARCH_CABLE_TRANSPONDERS
980                                 elif nim.isCompatible("DVB-T"):
981                                         getInitialTerrestrialTransponderList(tlist, nimmanager.getTerrestrialDescription(nim.slot))
982                                 else:
983                                         assert False
984
985                                 flags |= eComponentScan.scanNetworkSearch #FIXMEEE.. use flags from cables / satellites / terrestrial.xml
986                                 tmp = self.scan_clearallservices.value
987                                 if tmp == "yes":
988                                         flags |= eComponentScan.scanRemoveServices
989                                 elif tmp == "yes_hold_feeds":
990                                         flags |= eComponentScan.scanRemoveServices
991                                         flags |= eComponentScan.scanDontRemoveFeeds
992
993                                 if action == APPEND_NOW:
994                                         self.scanList.append({"transponders": tlist, "feid": nim.slot, "flags": flags})
995                                 elif action == SEARCH_CABLE_TRANSPONDERS:
996                                         self.flags = flags
997                                         self.feid = nim.slot
998                                         self.startCableTransponderSearch(nim.slot)
999                                         return
1000                                 else:
1001                                         assert False
1002
1003                         self.buildTransponderList() # recursive call of this function !!!
1004                         return
1005                 # when we are here, then the recursion is finished and all enabled nims are checked
1006                 # so we now start the real transponder scan
1007                 self.startScan(self.scanList)
1008
1009         def startScan(self, scanList):
1010                 if len(scanList):
1011                         if self.finished_cb:
1012                                 self.session.openWithCallback(self.finished_cb, ServiceScan, scanList = scanList)
1013                         else:
1014                                 self.session.open(ServiceScan, scanList = scanList)
1015                 else:
1016                         if self.finished_cb:
1017                                 self.session.openWithCallback(self.finished_cb, MessageBox, _("Nothing to scan!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
1018                         else:
1019                                 self.session.open(MessageBox, _("Nothing to scan!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
1020
1021         def setCableTransponderSearchResult(self, tlist):
1022                 if tlist is not None:
1023                         self.scanList.append({"transponders": tlist, "feid": self.feid, "flags": self.flags})
1024
1025         def cableTransponderSearchFinished(self):
1026                 self.buildTransponderList()
1027
1028         def keyCancel(self):
1029                 self.close()
1030
1031         def Satexists(self, tlist, pos):
1032                 for x in tlist:
1033                         if x == pos:
1034                                 return 1
1035                 return 0
1036