d624a53c04135a1a8eb3b58f262fbb2ed876c628
[openblackhole/openblackhole-enigma2.git] / lib / python / Screens / ScanSetup.py
1 from Screen import Screen
2 from ServiceScan import ServiceScan
3 from Components.config import config, ConfigSubsection, ConfigSelection, \
4         ConfigYesNo, ConfigInteger, getConfigListEntry, ConfigSlider, ConfigEnableDisable
5 from Components.ActionMap import NumberActionMap, ActionMap
6 from Components.ConfigList import ConfigListScreen
7 from Components.NimManager import nimmanager, getConfigSatlist
8 from Components.Label import Label
9 from Tools.HardwareInfo import HardwareInfo
10 from Tools.Transponder import getChannelNumber, supportedChannels, channel2frequency
11 from Screens.InfoBar import InfoBar
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 = 7000000, fechigh = 6, feclow = 6,
19                 modulation = 2, transmission = 2, guard = 4,
20                 hierarchy = 4, system = 0, plpid = 0):
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         parm.system = system
33         parm.plpid = plpid
34         return parm
35
36 def getInitialTransponderList(tlist, pos):
37         list = nimmanager.getTransponders(pos)
38         for x in list:
39                 if x[0] == 0:           #SAT
40                         parm = eDVBFrontendParametersSatellite()
41                         parm.frequency = x[1]
42                         parm.symbol_rate = x[2]
43                         parm.polarisation = x[3]
44                         parm.fec = x[4]
45                         parm.inversion = x[7]
46                         parm.orbital_position = pos
47                         parm.system = x[5]
48                         parm.modulation = x[6]
49                         parm.rolloff = x[8]
50                         parm.pilot = x[9]
51                         tlist.append(parm)
52
53 def getInitialCableTransponderList(tlist, nim):
54         list = nimmanager.getTranspondersCable(nim)
55         for x in list:
56                 if x[0] == 1: #CABLE
57                         parm = eDVBFrontendParametersCable()
58                         parm.frequency = x[1]
59                         parm.symbol_rate = x[2]
60                         parm.modulation = x[3]
61                         parm.fec_inner = x[4]
62                         parm.inversion = x[5]
63                         parm.system = x[6]
64                         tlist.append(parm)
65
66 def getInitialTerrestrialTransponderList(tlist, region):
67         list = nimmanager.getTranspondersTerrestrial(region)
68
69         #self.transponders[self.parsedTer].append((2,freq,bw,const,crh,crl,guard,transm,hierarchy,inv))
70
71         #def buildTerTransponder(frequency, inversion = 2, bandwidth = 3, fechigh = 6, feclow = 6,
72                                 #modulation = 2, transmission = 2, guard = 4, hierarchy = 4):
73
74         for x in list:
75                 if x[0] == 2: #TERRESTRIAL
76                         parm = buildTerTransponder(x[1], x[9], x[2], x[4], x[5], x[3], x[7], x[6], x[8], x[10], x[11])
77                         tlist.append(parm)
78
79 cable_bands = {
80         "DVBC_BAND_EU_VHF_I" : 1 << 0,
81         "DVBC_BAND_EU_MID" : 1 << 1,
82         "DVBC_BAND_EU_VHF_III" : 1 << 2,
83         "DVBC_BAND_EU_SUPER" : 1 << 3,
84         "DVBC_BAND_EU_HYPER" : 1 << 4,
85         "DVBC_BAND_EU_UHF_IV" : 1 << 5,
86         "DVBC_BAND_EU_UHF_V" : 1 << 6,
87         "DVBC_BAND_US_LO" : 1 << 7,
88         "DVBC_BAND_US_MID" : 1 << 8,
89         "DVBC_BAND_US_HI" : 1 << 9,
90         "DVBC_BAND_US_SUPER" : 1 << 10,
91         "DVBC_BAND_US_HYPER" : 1 << 11,
92 }
93
94 class CableTransponderSearchSupport:
95 #       def setCableTransponderSearchResult(self, tlist):
96 #               pass
97
98 #       def cableTransponderSearchFinished(self):
99 #               pass
100
101         def tryGetRawFrontend(self, feid):
102                 res_mgr = eDVBResourceManager.getInstance()
103                 if res_mgr:
104                         raw_channel = res_mgr.allocateRawChannel(self.feid)
105                         if raw_channel:
106                                 frontend = raw_channel.getFrontend()
107                                 if frontend:
108                                         frontend.closeFrontend() # immediate close...
109                                         del frontend
110                                         del raw_channel
111                                         return True
112                 return False
113
114         def cableTransponderSearchSessionClosed(self, *val):
115                 print "cableTransponderSearchSessionClosed, val", val
116                 self.cable_search_container.appClosed.remove(self.cableTransponderSearchClosed)
117                 self.cable_search_container.dataAvail.remove(self.getCableTransponderData)
118                 if val and len(val):
119                         if val[0]:
120                                 self.setCableTransponderSearchResult(self.__tlist)
121                         else:
122                                 self.cable_search_container.sendCtrlC()
123                                 self.setCableTransponderSearchResult(None)
124                 self.cable_search_container = None
125                 self.cable_search_session = None
126                 self.__tlist = None
127                 self.cableTransponderSearchFinished()
128
129         def cableTransponderSearchClosed(self, retval):
130                 print "cableTransponderSearch finished", retval
131                 self.cable_search_session.close(True)
132
133         def getCableTransponderData(self, str):
134                 #prepend any remaining data from the previous call
135                 str = self.remainingdata + str
136                 #split in lines
137                 lines = str.split('\n')
138                 #'str' should end with '\n', so when splitting, the last line should be empty. If this is not the case, we received an incomplete line
139                 if len(lines[-1]):
140                         #remember this data for next time
141                         self.remainingdata = lines[-1]
142                         lines = lines[0:-1]
143                 else:
144                         self.remainingdata = ""
145
146                 for line in lines:
147                         data = line.split()
148                         if len(data):
149                                 if data[0] == 'OK':
150                                         print str
151                                         parm = eDVBFrontendParametersCable()
152                                         qam = { "QAM16" : parm.Modulation_QAM16,
153                                                 "QAM32" : parm.Modulation_QAM32,
154                                                 "QAM64" : parm.Modulation_QAM64,
155                                                 "QAM128" : parm.Modulation_QAM128,
156                                                 "QAM256" : parm.Modulation_QAM256 }
157                                         inv = { "INVERSION_OFF" : parm.Inversion_Off,
158                                                 "INVERSION_ON" : parm.Inversion_On,
159                                                 "INVERSION_AUTO" : parm.Inversion_Unknown }
160                                         fec = { "FEC_AUTO" : parm.FEC_Auto,
161                                                 "FEC_1_2" : parm.FEC_1_2,
162                                                 "FEC_2_3" : parm.FEC_2_3,
163                                                 "FEC_3_4" : parm.FEC_3_4,
164                                                 "FEC_5_6" : parm.FEC_5_6,
165                                                 "FEC_7_8" : parm.FEC_7_8,
166                                                 "FEC_8_9" : parm.FEC_8_9,
167                                                 "FEC_3_5" : parm.FEC_3_5,
168                                                 "FEC_4_5" : parm.FEC_4_5,
169                                                 "FEC_9_10" : parm.FEC_9_10,
170                                                 "FEC_NONE" : parm.FEC_None }
171                                         parm.frequency = int(data[1])
172                                         parm.symbol_rate = int(data[2])
173                                         parm.fec_inner = fec[data[3]]
174                                         parm.modulation = qam[data[4]]
175                                         parm.inversion = inv[data[5]]
176                                         self.__tlist.append(parm)
177                                 tmpstr = _("Try to find used transponders in cable network.. please wait...")
178                                 tmpstr += "\n\n"
179                                 tmpstr += data[1].isdigit() and "%s MHz " % (int(data[1]) / 1000.) or data[1]
180                                 tmpstr += data[0]
181                                 self.cable_search_session["text"].setText(tmpstr)
182
183         def startCableTransponderSearch(self, nim_idx):
184                 if not self.tryGetRawFrontend(nim_idx):
185                         self.session.nav.stopService()
186                         if not self.tryGetRawFrontend(nim_idx):
187                                 if self.session.pipshown:
188                                         self.session.infobar.showPiP()
189                                 if not self.tryGetRawFrontend(nim_idx):
190                                         self.cableTransponderSearchFinished()
191                                         return
192                 self.__tlist = [ ]
193                 self.remainingdata = ""
194                 self.cable_search_container = eConsoleAppContainer()
195                 self.cable_search_container.appClosed.append(self.cableTransponderSearchClosed)
196                 self.cable_search_container.dataAvail.append(self.getCableTransponderData)
197                 cableConfig = config.Nims[nim_idx].cable
198                 tunername = nimmanager.getNimName(nim_idx)
199                 try:
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                 except:
205                         # older API
206                         if nim_idx < 2:
207                                 if HardwareInfo().get_device_name() == "dm500hd":
208                                         bus = 2
209                                 else:
210                                         bus = nim_idx
211                         else:
212                                 if nim_idx == 2:
213                                         bus = 2 # DM8000 first nim is /dev/i2c/2
214                                 else:
215                                         bus = 4 # DM8000 second num is /dev/i2c/4
216
217                 if tunername == "CXD1981":
218                         cmd = "cxd1978 --init --scan --verbose --wakeup --inv 2 --bus %d" % bus
219                 elif tunername.startswith("Sundtek"):
220                         cmd = "mediaclient --blindscan %d" % nim_idx
221                 else:
222                         cmd = "tda1002x --init --scan --verbose --wakeup --inv 2 --bus %d" % bus
223
224                 if cableConfig.scan_type.value == "bands":
225                         cmd += " --scan-bands "
226                         bands = 0
227                         if cableConfig.scan_band_EU_VHF_I.value:
228                                 bands |= cable_bands["DVBC_BAND_EU_VHF_I"]
229                         if cableConfig.scan_band_EU_MID.value:
230                                 bands |= cable_bands["DVBC_BAND_EU_MID"]
231                         if cableConfig.scan_band_EU_VHF_III.value:
232                                 bands |= cable_bands["DVBC_BAND_EU_VHF_III"]
233                         if cableConfig.scan_band_EU_UHF_IV.value:
234                                 bands |= cable_bands["DVBC_BAND_EU_UHF_IV"]
235                         if cableConfig.scan_band_EU_UHF_V.value:
236                                 bands |= cable_bands["DVBC_BAND_EU_UHF_V"]
237                         if cableConfig.scan_band_EU_SUPER.value:
238                                 bands |= cable_bands["DVBC_BAND_EU_SUPER"]
239                         if cableConfig.scan_band_EU_HYPER.value:
240                                 bands |= cable_bands["DVBC_BAND_EU_HYPER"]
241                         if cableConfig.scan_band_US_LOW.value:
242                                 bands |= cable_bands["DVBC_BAND_US_LO"]
243                         if cableConfig.scan_band_US_MID.value:
244                                 bands |= cable_bands["DVBC_BAND_US_MID"]
245                         if cableConfig.scan_band_US_HIGH.value:
246                                 bands |= cable_bands["DVBC_BAND_US_HI"]
247                         if cableConfig.scan_band_US_SUPER.value:
248                                 bands |= cable_bands["DVBC_BAND_US_SUPER"]
249                         if cableConfig.scan_band_US_HYPER.value:
250                                 bands |= cable_bands["DVBC_BAND_US_HYPER"]
251                         cmd += str(bands)
252                 else:
253                         cmd += " --scan-stepsize "
254                         cmd += str(cableConfig.scan_frequency_steps.value)
255                 if cableConfig.scan_mod_qam16.value:
256                         cmd += " --mod 16"
257                 if cableConfig.scan_mod_qam32.value:
258                         cmd += " --mod 32"
259                 if cableConfig.scan_mod_qam64.value:
260                         cmd += " --mod 64"
261                 if cableConfig.scan_mod_qam128.value:
262                         cmd += " --mod 128"
263                 if cableConfig.scan_mod_qam256.value:
264                         cmd += " --mod 256"
265                 if cableConfig.scan_sr_6900.value:
266                         cmd += " --sr 6900000"
267                 if cableConfig.scan_sr_6875.value:
268                         cmd += " --sr 6875000"
269                 if cableConfig.scan_sr_ext1.value > 450:
270                         cmd += " --sr "
271                         cmd += str(cableConfig.scan_sr_ext1.value)
272                         cmd += "000"
273                 if cableConfig.scan_sr_ext2.value > 450:
274                         cmd += " --sr "
275                         cmd += str(cableConfig.scan_sr_ext2.value)
276                         cmd += "000"
277                 print "TDA1002x CMD is", cmd
278
279                 self.cable_search_container.execute(cmd)
280                 tmpstr = _("Try to find used transponders in cable network.. please wait...")
281                 tmpstr += "\n\n..."
282                 self.cable_search_session = self.session.openWithCallback(self.cableTransponderSearchSessionClosed, MessageBox, tmpstr, MessageBox.TYPE_INFO)
283
284 class ScanSetup(ConfigListScreen, Screen, CableTransponderSearchSupport):
285         def __init__(self, session):
286                 Screen.__init__(self, session)
287
288                 self.finished_cb = None
289                 self.updateSatList()
290                 self.service = session.nav.getCurrentService()
291                 self.feinfo = None
292                 self.networkid = 0
293                 frontendData = None
294                 if self.service is not None:
295                         self.feinfo = self.service.frontendInfo()
296                         frontendData = self.feinfo and self.feinfo.getAll(True)
297
298                 self.ter_channel_input = False
299                 self.ter_tnumber = None
300                 self.createConfig(frontendData)
301
302                 del self.feinfo
303                 del self.service
304
305                 self.session.postScanService = session.nav.getCurrentlyPlayingServiceOrGroup()
306
307                 self["actions"] = NumberActionMap(["SetupActions", "MenuActions"],
308                 {
309                         "ok": self.keyGo,
310                         "save": self.keyGo,
311                         "cancel": self.keyCancel,
312                         "menu": self.doCloseRecursive,
313                 }, -2)
314
315                 self.statusTimer = eTimer()
316                 self.statusTimer.callback.append(self.updateStatus)
317                 #self.statusTimer.start(5000, True)
318
319                 self.list = []
320                 ConfigListScreen.__init__(self, self.list)
321                 self["header"] = Label(_("Manual Scan"))
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.typeOfInputEntry = None
354                 self.systemEntry = None
355                 self.modulationEntry = None
356                 self.preDefSatList = None
357                 self.TerrestrialTransponders = None
358                 self.TerrestrialRegionEntry = None
359                 nim = nimmanager.nim_slots[index_to_scan]
360                 if nim.isCompatible("DVB-S"):
361                         self.typeOfScanEntry = getConfigListEntry(_("Type of scan"), self.scan_type)
362                         self.list.append(self.typeOfScanEntry)
363                 elif nim.isCompatible("DVB-C"):
364                         if config.Nims[index_to_scan].cable.scan_type.value != "provider": # only show predefined transponder if in provider mode
365                                 if self.scan_typecable.value == "predefined_transponder":
366                                         self.scan_typecable.value = self.cable_toggle[self.last_scan_typecable]
367                         self.last_scan_typecable = self.scan_typecable.value
368                         self.typeOfScanEntry = getConfigListEntry(_("Type of scan"), self.scan_typecable)
369                         self.list.append(self.typeOfScanEntry)
370                 elif nim.isCompatible("DVB-T"):
371                         self.typeOfScanEntry = getConfigListEntry(_("Type of scan"), self.scan_typeterrestrial)
372                         self.list.append(self.typeOfScanEntry)
373                         if self.scan_typeterrestrial.value == "single_transponder":
374                                 self.typeOfInputEntry = getConfigListEntry(_("Use frequency or channel"), self.scan_input_as)
375                                 if self.ter_channel_input:
376                                         self.list.append(self.typeOfInputEntry)
377                                 else:
378                                         self.scan_input_as.value = self.scan_input_as.choices[0]
379
380                 self.scan_networkScan.value = False
381                 if nim.isCompatible("DVB-S"):
382                         if self.scan_type.value == "single_transponder":
383                                 self.updateSatList()
384                                 if nim.isCompatible("DVB-S2"):
385                                         self.systemEntry = getConfigListEntry(_('System'), self.scan_sat.system)
386                                         self.list.append(self.systemEntry)
387                                 else:
388                                         # downgrade to dvb-s, in case a -s2 config was active
389                                         self.scan_sat.system.value = eDVBFrontendParametersSatellite.System_DVB_S
390                                 self.list.append(getConfigListEntry(_('Satellite'), self.scan_satselection[index_to_scan]))
391                                 self.list.append(getConfigListEntry(_('Frequency'), self.scan_sat.frequency))
392                                 self.list.append(getConfigListEntry(_('Inversion'), self.scan_sat.inversion))
393                                 self.list.append(getConfigListEntry(_('Symbol rate'), self.scan_sat.symbolrate))
394                                 self.list.append(getConfigListEntry(_('Polarization'), self.scan_sat.polarization))
395                                 if self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S:
396                                         self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec))
397                                 elif self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S2:
398                                         self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec_s2))
399                                         self.modulationEntry = getConfigListEntry(_('Modulation'), self.scan_sat.modulation)
400                                         self.list.append(self.modulationEntry)
401                                         self.list.append(getConfigListEntry(_('Roll-off'), self.scan_sat.rolloff))
402                                         self.list.append(getConfigListEntry(_('Pilot'), self.scan_sat.pilot))
403                         elif self.scan_type.value == "predefined_transponder" and self.satList[index_to_scan]:
404                                 self.updateSatList()
405                                 self.preDefSatList = getConfigListEntry(_('Satellite'), self.scan_satselection[index_to_scan])
406                                 self.list.append(self.preDefSatList)
407                                 sat = self.satList[index_to_scan][self.scan_satselection[index_to_scan].index]
408                                 self.predefinedTranspondersList(sat[0])
409                                 self.list.append(getConfigListEntry(_('Transponder'), self.preDefTransponders))
410                         elif self.scan_type.value == "single_satellite":
411                                 self.updateSatList()
412                                 print self.scan_satselection[index_to_scan]
413                                 self.list.append(getConfigListEntry(_("Satellite"), self.scan_satselection[index_to_scan]))
414                                 self.scan_networkScan.value = True
415                         elif "multisat" in self.scan_type.value:
416                                 tlist = []
417                                 SatList = nimmanager.getSatListForNim(index_to_scan)
418                                 for x in SatList:
419                                         if self.Satexists(tlist, x[0]) == 0:
420                                                 tlist.append(x[0])
421                                                 sat = ConfigEnableDisable(default = "_yes" in self.scan_type.value and True or False)
422                                                 configEntry = getConfigListEntry(nimmanager.getSatDescription(x[0]), sat)
423                                                 self.list.append(configEntry)
424                                                 self.multiscanlist.append((x[0], sat))
425                                 self.scan_networkScan.value = True
426                 elif nim.isCompatible("DVB-C"):
427                         if self.scan_typecable.value == "single_transponder":
428                                 self.list.append(getConfigListEntry(_("Frequency"), self.scan_cab.frequency))
429                                 self.list.append(getConfigListEntry(_("Inversion"), self.scan_cab.inversion))
430                                 self.list.append(getConfigListEntry(_("Symbol rate"), self.scan_cab.symbolrate))
431                                 self.list.append(getConfigListEntry(_("Modulation"), self.scan_cab.modulation))
432                                 self.list.append(getConfigListEntry(_("FEC"), self.scan_cab.fec))
433                         elif self.scan_typecable.value == "predefined_transponder":
434                                 self.predefinedCabTranspondersList()
435                                 self.list.append(getConfigListEntry(_('Transponder'), self.CableTransponders))
436                         if config.Nims[index_to_scan].cable.scan_networkid.value:
437                                 self.networkid = config.Nims[index_to_scan].cable.scan_networkid.value
438                                 self.scan_networkScan.value = True
439                 elif nim.isCompatible("DVB-T"):
440                         if self.scan_typeterrestrial.value == "single_transponder":
441                                 if nim.isCompatible("DVB-T2"):
442                                         self.systemEntry = getConfigListEntry(_('System'), self.scan_ter.system)
443                                         self.list.append(self.systemEntry)
444                                 else:
445                                         self.scan_ter.system.value = eDVBFrontendParametersTerrestrial.System_DVB_T
446                                 if self.ter_channel_input and self.scan_input_as.value == "channel":
447                                         channel = getChannelNumber(self.scan_ter.frequency.value*1000, self.ter_tnumber)
448                                         if channel:
449                                                 self.scan_ter.channel.value = int(channel.replace("+","").replace("-",""))
450                                         self.list.append(getConfigListEntry(_("Channel"), self.scan_ter.channel))
451                                 else:
452                                         prev_val = self.scan_ter.frequency.value
453                                         self.scan_ter.frequency.value = channel2frequency(self.scan_ter.channel.value, self.ter_tnumber)/1000
454                                         if self.scan_ter.frequency.value == 474000:
455                                                 self.scan_ter.frequency.value = prev_val
456                                         self.list.append(getConfigListEntry(_("Frequency"), self.scan_ter.frequency))
457                                 self.list.append(getConfigListEntry(_("Inversion"), self.scan_ter.inversion))
458                                 self.list.append(getConfigListEntry(_("Bandwidth"), self.scan_ter.bandwidth))
459                                 self.list.append(getConfigListEntry(_("Code rate HP"), self.scan_ter.fechigh))
460                                 self.list.append(getConfigListEntry(_("Code rate LP"), self.scan_ter.feclow))
461                                 self.list.append(getConfigListEntry(_("Modulation"), self.scan_ter.modulation))
462                                 self.list.append(getConfigListEntry(_("Transmission mode"), self.scan_ter.transmission))
463                                 self.list.append(getConfigListEntry(_("Guard interval"), self.scan_ter.guard))
464                                 self.list.append(getConfigListEntry(_("Hierarchy info"), self.scan_ter.hierarchy))
465                                 if self.scan_ter.system.value == eDVBFrontendParametersTerrestrial.System_DVB_T2:
466                                         self.list.append(getConfigListEntry(_('PLP ID'), self.scan_ter.plp_id))
467                         elif self.scan_typeterrestrial.value == "predefined_transponder":
468                                 self.TerrestrialRegion = self.terrestrial_nims_regions[index_to_scan]
469                                 self.TerrestrialRegionEntry = getConfigListEntry(_('Region'), self.TerrestrialRegion)
470                                 self.list.append(self.TerrestrialRegionEntry)
471                                 self.predefinedTerrTranspondersList()
472                                 self.list.append(getConfigListEntry(_('Transponder'), self.TerrestrialTransponders))
473                         elif self.scan_typeterrestrial.value == "complete":
474                                 self.TerrestrialRegion = self.terrestrial_nims_regions[index_to_scan]
475                                 self.TerrestrialRegionEntry = getConfigListEntry(_('Region'), self.TerrestrialRegion)
476                                 self.list.append(self.TerrestrialRegionEntry)
477                 self.list.append(getConfigListEntry(_("Network scan"), self.scan_networkScan))
478                 self.list.append(getConfigListEntry(_("Clear before scan"), self.scan_clearallservices))
479                 self.list.append(getConfigListEntry(_("Only free scan"), self.scan_onlyfree))
480                 self["config"].list = self.list
481                 self["config"].l.setList(self.list)
482
483         def Satexists(self, tlist, pos):
484                 for x in tlist:
485                         if x == pos:
486                                 return 1
487                 return 0
488
489         def newConfig(self):
490                 cur = self["config"].getCurrent()
491                 print "cur is", cur
492                 if cur == self.typeOfScanEntry or \
493                         cur == self.typeOfInputEntry or \
494                         cur == self.tunerEntry or \
495                         cur == self.systemEntry or \
496                         cur == self.preDefSatList or \
497                         cur == self.TerrestrialRegionEntry or \
498                         (self.modulationEntry and self.systemEntry[1].value == eDVBFrontendParametersSatellite.System_DVB_S2 and cur == self.modulationEntry):
499                         self.createSetup()
500
501         def createConfig(self, frontendData):
502                 defaultSat = {
503                         "orbpos": 192,
504                         "system": eDVBFrontendParametersSatellite.System_DVB_S,
505                         "frequency": 11836,
506                         "inversion": eDVBFrontendParametersSatellite.Inversion_Unknown,
507                         "symbolrate": 27500,
508                         "polarization": eDVBFrontendParametersSatellite.Polarisation_Horizontal,
509                         "fec": eDVBFrontendParametersSatellite.FEC_Auto,
510                         "fec_s2": eDVBFrontendParametersSatellite.FEC_9_10,
511                         "modulation": eDVBFrontendParametersSatellite.Modulation_QPSK }
512                 defaultCab = {
513                         "frequency": 466,
514                         "inversion": eDVBFrontendParametersCable.Inversion_Unknown,
515                         "modulation": eDVBFrontendParametersCable.Modulation_QAM64,
516                         "fec": eDVBFrontendParametersCable.FEC_Auto,
517                         "symbolrate": 6900,
518                         "system": eDVBFrontendParametersCable.System_DVB_C_ANNEX_A }
519                 defaultTer = {
520                         "frequency" : 474000,
521                         "inversion" : eDVBFrontendParametersTerrestrial.Inversion_Unknown,
522                         "bandwidth" : 8000000,
523                         "fechigh" : eDVBFrontendParametersTerrestrial.FEC_Auto,
524                         "feclow" : eDVBFrontendParametersTerrestrial.FEC_Auto,
525                         "modulation" : eDVBFrontendParametersTerrestrial.Modulation_Auto,
526                         "transmission_mode" : eDVBFrontendParametersTerrestrial.TransmissionMode_Auto,
527                         "guard_interval" : eDVBFrontendParametersTerrestrial.GuardInterval_Auto,
528                         "hierarchy": eDVBFrontendParametersTerrestrial.Hierarchy_Auto,
529                         "system": eDVBFrontendParametersTerrestrial.System_DVB_T,
530                         "plp_id": 0 }
531
532                 if frontendData is not None:
533                         ttype = frontendData.get("tuner_type", "UNKNOWN")
534                         if ttype == "DVB-S":
535                                 defaultSat["system"] = frontendData.get("system", eDVBFrontendParametersSatellite.System_DVB_S)
536                                 defaultSat["frequency"] = frontendData.get("frequency", 0) / 1000
537                                 defaultSat["inversion"] = frontendData.get("inversion", eDVBFrontendParametersSatellite.Inversion_Unknown)
538                                 defaultSat["symbolrate"] = frontendData.get("symbol_rate", 0) / 1000
539                                 defaultSat["polarization"] = frontendData.get("polarization", eDVBFrontendParametersSatellite.Polarisation_Horizontal)
540                                 if defaultSat["system"] == eDVBFrontendParametersSatellite.System_DVB_S2:
541                                         defaultSat["fec_s2"] = frontendData.get("fec_inner", eDVBFrontendParametersSatellite.FEC_Auto)
542                                         defaultSat["rolloff"] = frontendData.get("rolloff", eDVBFrontendParametersSatellite.RollOff_alpha_0_35)
543                                         defaultSat["pilot"] = frontendData.get("pilot", eDVBFrontendParametersSatellite.Pilot_Unknown)
544                                 else:
545                                         defaultSat["fec"] = frontendData.get("fec_inner", eDVBFrontendParametersSatellite.FEC_Auto)
546                                 defaultSat["modulation"] = frontendData.get("modulation", eDVBFrontendParametersSatellite.Modulation_QPSK)
547                                 defaultSat["orbpos"] = frontendData.get("orbital_position", 0)
548                         elif ttype == "DVB-C":
549                                 defaultCab["frequency"] = frontendData.get("frequency", 0) / 1000
550                                 defaultCab["symbolrate"] = frontendData.get("symbol_rate", 0) / 1000
551                                 defaultCab["inversion"] = frontendData.get("inversion", eDVBFrontendParametersCable.Inversion_Unknown)
552                                 defaultCab["fec"] = frontendData.get("fec_inner", eDVBFrontendParametersCable.FEC_Auto)
553                                 defaultCab["modulation"] = frontendData.get("modulation", eDVBFrontendParametersCable.Modulation_QAM16)
554                                 defaultCab["system"] = frontendData.get("system", eDVBFrontendParametersCable.System_DVB_C_ANNEX_A)
555                         elif ttype == "DVB-T":
556                                 defaultTer["frequency"] = frontendData.get("frequency", 47400000) / 1000
557                                 defaultTer["inversion"] = frontendData.get("inversion", eDVBFrontendParametersTerrestrial.Inversion_Unknown)
558                                 defaultTer["bandwidth"] = frontendData.get("bandwidth", 8000000)
559                                 defaultTer["fechigh"] = frontendData.get("code_rate_hp", eDVBFrontendParametersTerrestrial.FEC_Auto)
560                                 defaultTer["feclow"] = frontendData.get("code_rate_lp", eDVBFrontendParametersTerrestrial.FEC_Auto)
561                                 defaultTer["modulation"] = frontendData.get("constellation", eDVBFrontendParametersTerrestrial.Modulation_Auto)
562                                 defaultTer["transmission_mode"] = frontendData.get("transmission_mode", eDVBFrontendParametersTerrestrial.TransmissionMode_Auto)
563                                 defaultTer["guard_interval"] = frontendData.get("guard_interval", eDVBFrontendParametersTerrestrial.GuardInterval_Auto)
564                                 defaultTer["hierarchy"] = frontendData.get("hierarchy_information", eDVBFrontendParametersTerrestrial.Hierarchy_Auto)
565                                 defaultTer["system"] = frontendData.get("system", eDVBFrontendParametersTerrestrial.System_DVB_T)
566                                 defaultTer["plp_id"] = frontendData.get("plp_id", 0)
567
568                 self.scan_sat = ConfigSubsection()
569                 self.scan_cab = ConfigSubsection()
570                 self.scan_ter = ConfigSubsection()
571
572                 nim_list = []
573                 # collect all nims which are *not* set to "nothing"
574                 for n in nimmanager.nim_slots:
575                         if n.config_mode == "nothing":
576                                 continue
577                         if n.config_mode == "advanced" and len(nimmanager.getSatListForNim(n.slot)) < 1:
578                                 continue
579                         if n.config_mode in ("loopthrough", "satposdepends"):
580                                 root_id = nimmanager.sec.getRoot(n.slot_id, int(n.config.connectedTo.value))
581                                 if n.type == nimmanager.nim_slots[root_id].type: # check if connected from a DVB-S to DVB-S2 Nim or vice versa
582                                         continue
583                         nim_list.append((str(n.slot), n.friendly_full_description))
584
585                 self.scan_nims = ConfigSelection(choices = nim_list)
586                 if frontendData is not None and len(nim_list) > 0:
587                         self.scan_nims.value = str(frontendData.get("tuner_number", nim_list[0][0]))
588
589                 for slot in nimmanager.nim_slots:
590                         if slot.isCompatible("DVB-T"):
591                                 self.ter_tnumber = slot.slot
592                 if self.ter_tnumber is not None:
593                         self.ter_channel_input = supportedChannels(self.ter_tnumber)
594
595                 # status
596                 self.scan_snr = ConfigSlider()
597                 self.scan_snr.enabled = False
598                 self.scan_agc = ConfigSlider()
599                 self.scan_agc.enabled = False
600                 self.scan_ber = ConfigSlider()
601                 self.scan_ber.enabled = False
602
603                 # sat
604                 self.scan_sat.system = ConfigSelection(default = defaultSat["system"], choices = [
605                         (eDVBFrontendParametersSatellite.System_DVB_S, _("DVB-S")),
606                         (eDVBFrontendParametersSatellite.System_DVB_S2, _("DVB-S2"))])
607                 self.scan_sat.frequency = ConfigInteger(default = defaultSat["frequency"], limits = (1, 99999))
608                 self.scan_sat.inversion = ConfigSelection(default = defaultSat["inversion"], choices = [
609                         (eDVBFrontendParametersSatellite.Inversion_Off, _("Off")),
610                         (eDVBFrontendParametersSatellite.Inversion_On, _("On")),
611                         (eDVBFrontendParametersSatellite.Inversion_Unknown, _("Auto"))])
612                 self.scan_sat.symbolrate = ConfigInteger(default = defaultSat["symbolrate"], limits = (1, 99999))
613                 self.scan_sat.polarization = ConfigSelection(default = defaultSat["polarization"], choices = [
614                         (eDVBFrontendParametersSatellite.Polarisation_Horizontal, _("horizontal")),
615                         (eDVBFrontendParametersSatellite.Polarisation_Vertical, _("vertical")),
616                         (eDVBFrontendParametersSatellite.Polarisation_CircularLeft, _("circular left")),
617                         (eDVBFrontendParametersSatellite.Polarisation_CircularRight, _("circular right"))])
618                 self.scan_sat.fec = ConfigSelection(default = defaultSat["fec"], choices = [
619                         (eDVBFrontendParametersSatellite.FEC_Auto, _("Auto")),
620                         (eDVBFrontendParametersSatellite.FEC_1_2, "1/2"),
621                         (eDVBFrontendParametersSatellite.FEC_2_3, "2/3"),
622                         (eDVBFrontendParametersSatellite.FEC_3_4, "3/4"),
623                         (eDVBFrontendParametersSatellite.FEC_5_6, "5/6"),
624                         (eDVBFrontendParametersSatellite.FEC_7_8, "7/8"),
625                         (eDVBFrontendParametersSatellite.FEC_None, _("None"))])
626                 self.scan_sat.fec_s2 = ConfigSelection(default = defaultSat["fec_s2"], choices = [
627                         (eDVBFrontendParametersSatellite.FEC_1_2, "1/2"),
628                         (eDVBFrontendParametersSatellite.FEC_2_3, "2/3"),
629                         (eDVBFrontendParametersSatellite.FEC_3_4, "3/4"),
630                         (eDVBFrontendParametersSatellite.FEC_3_5, "3/5"),
631                         (eDVBFrontendParametersSatellite.FEC_4_5, "4/5"),
632                         (eDVBFrontendParametersSatellite.FEC_5_6, "5/6"),
633                         (eDVBFrontendParametersSatellite.FEC_7_8, "7/8"),
634                         (eDVBFrontendParametersSatellite.FEC_8_9, "8/9"),
635                         (eDVBFrontendParametersSatellite.FEC_9_10, "9/10")])
636                 self.scan_sat.modulation = ConfigSelection(default = defaultSat["modulation"], choices = [
637                         (eDVBFrontendParametersSatellite.Modulation_QPSK, "QPSK"),
638                         (eDVBFrontendParametersSatellite.Modulation_8PSK, "8PSK")])
639                 self.scan_sat.rolloff = ConfigSelection(default = defaultSat.get("rolloff", eDVBFrontendParametersSatellite.RollOff_alpha_0_35), choices = [
640                         (eDVBFrontendParametersSatellite.RollOff_alpha_0_35, "0.35"),
641                         (eDVBFrontendParametersSatellite.RollOff_alpha_0_25, "0.25"),
642                         (eDVBFrontendParametersSatellite.RollOff_alpha_0_20, "0.20"),
643                         (eDVBFrontendParametersSatellite.RollOff_auto, _("Auto"))])
644                 self.scan_sat.pilot = ConfigSelection(default = defaultSat.get("pilot", eDVBFrontendParametersSatellite.Pilot_Unknown), choices = [
645                         (eDVBFrontendParametersSatellite.Pilot_Off, _("Off")),
646                         (eDVBFrontendParametersSatellite.Pilot_On, _("On")),
647                         (eDVBFrontendParametersSatellite.Pilot_Unknown, _("Auto"))])
648
649                 # cable
650                 self.scan_cab.frequency = ConfigInteger(default = defaultCab["frequency"], limits = (50, 999))
651                 self.scan_cab.inversion = ConfigSelection(default = defaultCab["inversion"], choices = [
652                         (eDVBFrontendParametersCable.Inversion_Off, _("Off")),
653                         (eDVBFrontendParametersCable.Inversion_On, _("On")),
654                         (eDVBFrontendParametersCable.Inversion_Unknown, _("Auto"))])
655                 self.scan_cab.modulation = ConfigSelection(default = defaultCab["modulation"], choices = [
656                         (eDVBFrontendParametersCable.Modulation_QAM16, "16-QAM"),
657                         (eDVBFrontendParametersCable.Modulation_QAM32, "32-QAM"),
658                         (eDVBFrontendParametersCable.Modulation_QAM64, "64-QAM"),
659                         (eDVBFrontendParametersCable.Modulation_QAM128, "128-QAM"),
660                         (eDVBFrontendParametersCable.Modulation_QAM256, "256-QAM")])
661                 self.scan_cab.fec = ConfigSelection(default = defaultCab["fec"], choices = [
662                         (eDVBFrontendParametersCable.FEC_Auto, _("Auto")),
663                         (eDVBFrontendParametersCable.FEC_1_2, "1/2"),
664                         (eDVBFrontendParametersCable.FEC_2_3, "2/3"),
665                         (eDVBFrontendParametersCable.FEC_3_4, "3/4"),
666                         (eDVBFrontendParametersCable.FEC_5_6, "5/6"),
667                         (eDVBFrontendParametersCable.FEC_7_8, "7/8"),
668                         (eDVBFrontendParametersCable.FEC_8_9, "8/9"),
669                         (eDVBFrontendParametersCable.FEC_3_5, "3/5"),
670                         (eDVBFrontendParametersCable.FEC_4_5, "4/5"),
671                         (eDVBFrontendParametersCable.FEC_9_10, "9/10"),
672                         (eDVBFrontendParametersCable.FEC_None, _("None"))])
673                 self.scan_cab.symbolrate = ConfigInteger(default = defaultCab["symbolrate"], limits = (1, 99999))
674                 self.scan_cab.system = ConfigSelection(default = defaultCab["system"], choices = [
675                         (eDVBFrontendParametersCable.System_DVB_C_ANNEX_A, _("DVB-C")),
676                         (eDVBFrontendParametersCable.System_DVB_C_ANNEX_C, _("DVB-C ANNEX C"))])
677
678                 # terrestial
679                 self.scan_ter.frequency = ConfigInteger(default = defaultTer["frequency"], limits = (50000, 999000))
680                 self.scan_ter.channel = ConfigInteger(default = 21, limits = (1, 99))
681                 self.scan_ter.inversion = ConfigSelection(default = defaultTer["inversion"], choices = [
682                         (eDVBFrontendParametersTerrestrial.Inversion_Off, _("Off")),
683                         (eDVBFrontendParametersTerrestrial.Inversion_On, _("On")),
684                         (eDVBFrontendParametersTerrestrial.Inversion_Unknown, _("Auto"))])
685                 # WORKAROUND: we can't use BW-auto
686                 self.scan_ter.bandwidth = ConfigSelection(default = defaultTer["bandwidth"], choices = [
687                         (1712000, "1.712MHz"),
688                         (5000000, "5MHz"),
689                         (6000000, "6MHz"),
690                         (7000000, "7MHz"),
691                         (8000000, "8MHz"),
692                         (10000000,"10MHz")
693                         ])
694                 #, (eDVBFrontendParametersTerrestrial.Bandwidth_Auto, _("Auto"))))
695                 self.scan_ter.fechigh = ConfigSelection(default = defaultTer["fechigh"], choices = [
696                         (eDVBFrontendParametersTerrestrial.FEC_1_2, "1/2"),
697                         (eDVBFrontendParametersTerrestrial.FEC_2_3, "2/3"),
698                         (eDVBFrontendParametersTerrestrial.FEC_3_4, "3/4"),
699                         (eDVBFrontendParametersTerrestrial.FEC_5_6, "5/6"),
700                         (eDVBFrontendParametersTerrestrial.FEC_6_7, "6/7"),
701                         (eDVBFrontendParametersTerrestrial.FEC_7_8, "7/8"),
702                         (eDVBFrontendParametersTerrestrial.FEC_8_9, "8/9"),
703                         (eDVBFrontendParametersTerrestrial.FEC_Auto, _("Auto"))])
704                 self.scan_ter.feclow = ConfigSelection(default = defaultTer["feclow"], choices = [
705                         (eDVBFrontendParametersTerrestrial.FEC_1_2, "1/2"),
706                         (eDVBFrontendParametersTerrestrial.FEC_2_3, "2/3"),
707                         (eDVBFrontendParametersTerrestrial.FEC_3_4, "3/4"),
708                         (eDVBFrontendParametersTerrestrial.FEC_5_6, "5/6"),
709                         (eDVBFrontendParametersTerrestrial.FEC_6_7, "6/7"),
710                         (eDVBFrontendParametersTerrestrial.FEC_7_8, "7/8"),
711                         (eDVBFrontendParametersTerrestrial.FEC_8_9, "8/9"),
712                         (eDVBFrontendParametersTerrestrial.FEC_Auto, _("Auto"))])
713                 self.scan_ter.modulation = ConfigSelection(default = defaultTer["modulation"], choices = [
714                         (eDVBFrontendParametersTerrestrial.Modulation_QPSK, "QPSK"),
715                         (eDVBFrontendParametersTerrestrial.Modulation_QAM16, "QAM16"),
716                         (eDVBFrontendParametersTerrestrial.Modulation_QAM64, "QAM64"),
717                         (eDVBFrontendParametersTerrestrial.Modulation_QAM256, "QAM256"),
718                         (eDVBFrontendParametersTerrestrial.Modulation_Auto, _("Auto"))])
719                 self.scan_ter.transmission = ConfigSelection(default = defaultTer["transmission_mode"], choices = [
720                         (eDVBFrontendParametersTerrestrial.TransmissionMode_1k, "1K"),
721                         (eDVBFrontendParametersTerrestrial.TransmissionMode_2k, "2K"),
722                         (eDVBFrontendParametersTerrestrial.TransmissionMode_4k, "4K"),
723                         (eDVBFrontendParametersTerrestrial.TransmissionMode_8k, "8K"),
724                         (eDVBFrontendParametersTerrestrial.TransmissionMode_16k, "16K"),
725                         (eDVBFrontendParametersTerrestrial.TransmissionMode_32k, "32K"),
726                         (eDVBFrontendParametersTerrestrial.TransmissionMode_Auto, _("Auto"))])
727                 self.scan_ter.guard = ConfigSelection(default = defaultTer["guard_interval"], choices = [
728                         (eDVBFrontendParametersTerrestrial.GuardInterval_1_32, "1/32"),
729                         (eDVBFrontendParametersTerrestrial.GuardInterval_1_16, "1/16"),
730                         (eDVBFrontendParametersTerrestrial.GuardInterval_1_8, "1/8"),
731                         (eDVBFrontendParametersTerrestrial.GuardInterval_1_4, "1/4"),
732                         (eDVBFrontendParametersTerrestrial.GuardInterval_1_128, "1/128"),
733                         (eDVBFrontendParametersTerrestrial.GuardInterval_19_128, "19/128"),
734                         (eDVBFrontendParametersTerrestrial.GuardInterval_19_256, "19/256"),
735                         (eDVBFrontendParametersTerrestrial.GuardInterval_Auto, _("Auto"))])
736                 self.scan_ter.hierarchy = ConfigSelection(default = defaultTer["hierarchy"], choices = [
737                         (eDVBFrontendParametersTerrestrial.Hierarchy_None, _("None")),
738                         (eDVBFrontendParametersTerrestrial.Hierarchy_1, "1"),
739                         (eDVBFrontendParametersTerrestrial.Hierarchy_2, "2"),
740                         (eDVBFrontendParametersTerrestrial.Hierarchy_4, "4"),
741                         (eDVBFrontendParametersTerrestrial.Hierarchy_Auto, _("Auto"))])
742                 self.scan_ter.system = ConfigSelection(default = defaultTer["system"], choices = [
743                         (eDVBFrontendParametersTerrestrial.System_DVB_T, _("DVB-T")),
744                         (eDVBFrontendParametersTerrestrial.System_DVB_T2, _("DVB-T2"))])
745                 self.scan_ter.plp_id = ConfigInteger(default = defaultTer["plp_id"], limits = (0, 255))
746
747                 self.scan_scansat = {}
748                 for sat in nimmanager.satList:
749                         #print sat[1]
750                         self.scan_scansat[sat[0]] = ConfigYesNo(default = False)
751
752                 self.scan_satselection = []
753                 for slot in nimmanager.nim_slots:
754                         if slot.isCompatible("DVB-S"):
755                                 self.scan_satselection.append(getConfigSatlist(defaultSat["orbpos"], self.satList[slot.slot]))
756                         else:
757                                 self.scan_satselection.append(None)
758
759                 self.terrestrial_nims_regions = []
760                 for slot in nimmanager.nim_slots:
761                         if slot.isCompatible("DVB-T"):
762                                 self.terrestrial_nims_regions.append(self.getTerrestrialRegionsList(slot.slot))
763                         else:
764                                 self.terrestrial_nims_regions.append(None)
765
766                 if frontendData is not None and ttype == "DVB-S" and self.predefinedTranspondersList(defaultSat["orbpos"]) is not None:
767                         defaultSatSearchType = "predefined_transponder"
768                 else:
769                         defaultSatSearchType = "single_transponder"
770                 if frontendData is not None and ttype == "DVB-T" and self.predefinedTerrTranspondersList() is not None:
771                         defaultTerrSearchType = "predefined_transponder"
772                 else:
773                         defaultTerrSearchType = "single_transponder"
774
775                 if frontendData is not None and ttype == "DVB-C" and self.predefinedCabTranspondersList() is not None:
776                         defaultCabSearchType = "predefined_transponder"
777                 else:
778                         defaultCabSearchType = "single_transponder"
779
780                 self.scan_type = ConfigSelection(default = defaultSatSearchType, choices = [("single_transponder", _("User defined transponder")), ("predefined_transponder", _("Predefined transponder")), ("single_satellite", _("Single satellite")), ("multisat", _("Multisat")), ("multisat_yes", _("Multisat all select"))])
781                 self.scan_typecable = ConfigSelection(default = defaultCabSearchType, choices = [("single_transponder", _("User defined transponder")), ("predefined_transponder", _("Predefined transponder")), ("complete", _("Complete"))])
782                 self.last_scan_typecable = "single_transponder"
783                 self.cable_toggle = {"single_transponder":"complete", "complete":"single_transponder"}
784                 self.scan_typeterrestrial = ConfigSelection(default = defaultTerrSearchType, choices = [("single_transponder", _("User defined transponder")), ("predefined_transponder", _("Predefined transponder")), ("complete", _("Complete"))])
785                 self.scan_input_as = ConfigSelection(default = "channel", choices = [("frequency", _("Frequency")), ("channel", _("Channel"))])
786                 self.scan_clearallservices = ConfigSelection(default = "no", choices = [("no", _("no")), ("yes", _("yes")), ("yes_hold_feeds", _("yes (keep feeds)"))])
787                 self.scan_onlyfree = ConfigYesNo(default = False)
788                 self.scan_networkScan = ConfigYesNo(default = False)
789
790                 return True
791
792         def keyLeft(self):
793                 ConfigListScreen.keyLeft(self)
794                 self.newConfig()
795
796         def keyRight(self):
797                 ConfigListScreen.keyRight(self)
798                 self.newConfig()
799
800         def handleKeyFileCallback(self, answer):
801                 ConfigListScreen.handleKeyFileCallback(self, answer)
802                 self.newConfig()
803
804         def updateStatus(self):
805                 print "updatestatus"
806
807         def addSatTransponder(self, tlist, frequency, symbol_rate, polarisation, fec, inversion, orbital_position, system, modulation, rolloff, pilot):
808                 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)
809                 print "orbpos: " + str(orbital_position)
810                 parm = eDVBFrontendParametersSatellite()
811                 parm.modulation = modulation
812                 parm.system = system
813                 parm.frequency = frequency * 1000
814                 parm.symbol_rate = symbol_rate * 1000
815                 parm.polarisation = polarisation
816                 parm.fec = fec
817                 parm.inversion = inversion
818                 parm.orbital_position = orbital_position
819                 parm.rolloff = rolloff
820                 parm.pilot = pilot
821                 tlist.append(parm)
822
823         def addCabTransponder(self, tlist, frequency, symbol_rate, modulation, fec, inversion):
824                 print "Add Cab: frequ: " + str(frequency) + " symbol: " + str(symbol_rate) + " pol: " + str(modulation) + " fec: " + str(fec) + " inversion: " + str(inversion)
825                 parm = eDVBFrontendParametersCable()
826                 parm.frequency = frequency
827                 parm.symbol_rate = symbol_rate
828                 parm.modulation = modulation
829                 parm.fec_inner = fec
830                 parm.inversion = inversion
831                 tlist.append(parm)
832
833         def addTerTransponder(self, tlist, *args, **kwargs):
834                 tlist.append(buildTerTransponder(*args, **kwargs))
835
836         def keyGo(self):
837                 infoBarInstance = InfoBar.instance
838                 if infoBarInstance:
839                         infoBarInstance.checkTimeshiftRunning(self.keyGoCheckTimeshiftCallback)
840                 else:
841                         self.keyGoCheckTimeshiftCallback(True)
842
843         def keyGoCheckTimeshiftCallback(self, answer):
844                 if not answer or self.scan_nims.value == "":
845                         return
846                 tlist = []
847                 flags = None
848                 startScan = True
849                 removeAll = True
850                 index_to_scan = int(self.scan_nims.value)
851
852                 if self.scan_nims == [ ]:
853                         self.session.open(MessageBox, _("No tuner is enabled!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
854                         return
855                 nim = nimmanager.nim_slots[index_to_scan]
856                 print "nim", nim.slot
857                 if nim.isCompatible("DVB-S"):
858                         print "is compatible with DVB-S"
859                         if self.scan_type.value == "single_transponder":
860                                 # these lists are generated for each tuner, so this has work.
861                                 assert len(self.satList) > index_to_scan
862                                 assert len(self.scan_satselection) > index_to_scan
863
864                                 nimsats = self.satList[index_to_scan]
865                                 selsatidx = self.scan_satselection[index_to_scan].index
866
867                                 # however, the satList itself could be empty. in that case, "index" is 0 (for "None").
868                                 if len(nimsats):
869                                         orbpos = nimsats[selsatidx][0]
870                                         if self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S:
871                                                 fec = self.scan_sat.fec.value
872                                         else:
873                                                 fec = self.scan_sat.fec_s2.value
874                                         print "add sat transponder"
875                                         self.addSatTransponder(tlist, self.scan_sat.frequency.value,
876                                                                 self.scan_sat.symbolrate.value,
877                                                                 self.scan_sat.polarization.value,
878                                                                 fec,
879                                                                 self.scan_sat.inversion.value,
880                                                                 orbpos,
881                                                                 self.scan_sat.system.value,
882                                                                 self.scan_sat.modulation.value,
883                                                                 self.scan_sat.rolloff.value,
884                                                                 self.scan_sat.pilot.value)
885                                 removeAll = False
886                         elif self.scan_type.value == "predefined_transponder":
887                                 nimsats = self.satList[index_to_scan]
888                                 selsatidx = self.scan_satselection[index_to_scan].index
889                                 if len(nimsats):
890                                         orbpos = nimsats[selsatidx][0]
891                                         tps = nimmanager.getTransponders(orbpos)
892                                         if len(tps) and len(tps) > self.preDefTransponders.index:
893                                                 tp = tps[self.preDefTransponders.index]
894                                                 self.addSatTransponder(tlist, tp[1] / 1000, tp[2] / 1000, tp[3], tp[4], tp[7], orbpos, tp[5], tp[6], tp[8], tp[9])
895                                 removeAll = False
896                         elif self.scan_type.value == "single_satellite":
897                                 sat = self.satList[index_to_scan][self.scan_satselection[index_to_scan].index]
898                                 getInitialTransponderList(tlist, sat[0])
899                         elif "multisat" in self.scan_type.value:
900                                 SatList = nimmanager.getSatListForNim(index_to_scan)
901                                 for x in self.multiscanlist:
902                                         if x[1].value:
903                                                 print "   " + str(x[0])
904                                                 getInitialTransponderList(tlist, x[0])
905
906                 elif nim.isCompatible("DVB-C"):
907                         if self.scan_typecable.value == "single_transponder":
908                                 self.addCabTransponder(tlist, self.scan_cab.frequency.value*1000,
909                                                                                           self.scan_cab.symbolrate.value*1000,
910                                                                                           self.scan_cab.modulation.value,
911                                                                                           self.scan_cab.fec.value,
912                                                                                           self.scan_cab.inversion.value)
913                                 removeAll = False
914                         elif self.scan_typecable.value == "predefined_transponder":
915                                 tps = nimmanager.getTranspondersCable(index_to_scan)
916                                 if len(tps) and len(tps) > self.CableTransponders.index :
917                                         tp = tps[self.CableTransponders.index]
918                                         # 0 transponder type, 1 freq, 2 sym, 3 mod, 4 fec, 5 inv, 6 sys
919                                         self.addCabTransponder(tlist, tp[1], tp[2], tp[3], tp[4], tp[5])
920                                 removeAll = False
921                         elif self.scan_typecable.value == "complete":
922                                 if config.Nims[index_to_scan].cable.scan_type.value == "provider":
923                                         getInitialCableTransponderList(tlist, index_to_scan)
924                                 else:
925                                         startScan = False
926
927                 elif nim.isCompatible("DVB-T"):
928                         if self.scan_typeterrestrial.value == "single_transponder":
929                                 if self.scan_input_as.value == "channel":
930                                         frequency = channel2frequency(self.scan_ter.channel.value, self.ter_tnumber)
931                                 else:
932                                         frequency = self.scan_ter.frequency.value * 1000
933                                 self.addTerTransponder(tlist,
934                                                 frequency,
935                                                 inversion = self.scan_ter.inversion.value,
936                                                 bandwidth = self.scan_ter.bandwidth.value,
937                                                 fechigh = self.scan_ter.fechigh.value,
938                                                 feclow = self.scan_ter.feclow.value,
939                                                 modulation = self.scan_ter.modulation.value,
940                                                 transmission = self.scan_ter.transmission.value,
941                                                 guard = self.scan_ter.guard.value,
942                                                 hierarchy = self.scan_ter.hierarchy.value,
943                                                 system = self.scan_ter.system.value,
944                                                 plpid = self.scan_ter.plp_id.value)
945                                 removeAll = False
946                         elif self.scan_typeterrestrial.value == "predefined_transponder":
947                                 if self.TerrestrialTransponders is not None:
948                                         region = self.terrestrial_nims_regions[index_to_scan].value
949                                         tps = nimmanager.getTranspondersTerrestrial(region)
950                                         if len(tps) and len(tps) > self.TerrestrialTransponders.index :
951                                                 tp = tps[self.TerrestrialTransponders.index]
952                                                 tlist.append(buildTerTransponder(tp[1], tp[9], tp[2], tp[4], tp[5], tp[3], tp[7], tp[6], tp[8], tp[10], tp[11]))
953                                 removeAll = False
954                         elif self.scan_typeterrestrial.value == "complete":
955                                 getInitialTerrestrialTransponderList(tlist, self.TerrestrialRegion.value)
956
957                 flags = self.scan_networkScan.value and eComponentScan.scanNetworkSearch or 0
958
959                 tmp = self.scan_clearallservices.value
960                 if tmp == "yes":
961                         flags |= eComponentScan.scanRemoveServices
962                 elif tmp == "yes_hold_feeds":
963                         flags |= eComponentScan.scanRemoveServices
964                         flags |= eComponentScan.scanDontRemoveFeeds
965
966                 if tmp != "no" and not removeAll:
967                         flags |= eComponentScan.scanDontRemoveUnscanned
968
969                 if self.scan_onlyfree.value:
970                         flags |= eComponentScan.scanOnlyFree
971
972                 for x in self["config"].list:
973                         x[1].save()
974
975                 if startScan:
976                         self.startScan(tlist, flags, index_to_scan, self.networkid)
977                 else:
978                         self.flags = flags
979                         self.feid = index_to_scan
980                         self.tlist = []
981                         self.startCableTransponderSearch(self.feid)
982
983         def setCableTransponderSearchResult(self, tlist):
984                 self.tlist = tlist
985
986         def cableTransponderSearchFinished(self):
987                 if self.tlist is None:
988                         self.tlist = []
989                 else:
990                         self.startScan(self.tlist, self.flags, self.feid)
991
992         def predefinedTranspondersList(self, orbpos):
993                 default = None
994                 if orbpos is not None:
995                         list = []
996                         if self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S2:
997                                 fec = self.scan_sat.fec_s2.value
998                         else:
999                                 fec = self.scan_sat.fec.value
1000                         compare = [0, self.scan_sat.frequency.value*1000, self.scan_sat.symbolrate.value*1000, self.scan_sat.polarization.value, fec]
1001                         i = 0
1002                         tps = nimmanager.getTransponders(orbpos)
1003                         for tp in tps:
1004                                 if tp[0] == 0:
1005                                         if default is None and self.compareTransponders(tp, compare):
1006                                                 default = str(i)
1007                                         list.append((str(i), self.humanReadableTransponder(tp)))
1008                                         i += 1
1009                         self.preDefTransponders = ConfigSelection(choices = list, default = default)
1010                 return default
1011
1012         def humanReadableTransponder(self, tp):
1013                 if tp[3] in range (4) and tp[4] in range (11):
1014                         pol_list = ['H','V','L','R']
1015                         fec_list = ['Auto','1/2','2/3','3/4','5/6','7/8','8/9','3/5','4/5','9/10','None']
1016                         return str(tp[1] / 1000) + " " + pol_list[tp[3]] + " " + str(tp[2] / 1000) + " " + fec_list[tp[4]]
1017                 return _("Invalid transponder data")
1018
1019         def compareTransponders(self, tp, compare):
1020                 frequencyTolerance = 2000 #2 MHz
1021                 symbolRateTolerance = 10
1022                 return abs(tp[1] - compare[1]) <= frequencyTolerance and abs(tp[2] - compare[2]) <= symbolRateTolerance and tp[3] == compare[3] and (not tp[4] or tp[4] == compare[4])
1023
1024         def predefinedTerrTranspondersList(self):
1025                 default = None
1026                 list = []
1027                 compare = [2, self.scan_ter.frequency.value*1000]
1028                 i = 0
1029                 index_to_scan = int(self.scan_nims.value)
1030                 channels = supportedChannels(index_to_scan)
1031                 region = self.terrestrial_nims_regions[index_to_scan].value
1032                 tps = nimmanager.getTranspondersTerrestrial(region)
1033                 for tp in tps:
1034                         if tp[0] == 2: #TERRESTRIAL
1035                                 channel = ''
1036                                 if channels:
1037                                         channel = _(' (Channel %s)') % (getChannelNumber(tp[1], index_to_scan))
1038                                 if default is None and self.compareTerrTransponders(tp, compare):
1039                                         default = str(i)
1040                                 list.append((str(i), '%s MHz %s' % (str(tp[1] / 1000000), channel)))
1041                                 i += 1
1042                                 print "channel", channel
1043                 self.TerrestrialTransponders = ConfigSelection(choices = list, default = default)
1044                 return default
1045
1046         def compareTerrTransponders(self, tp, compare):
1047                 frequencyTolerance = 1000000 #1 MHz
1048                 return abs(tp[1] - compare[1]) <= frequencyTolerance
1049
1050         def getTerrestrialRegionsList(self, index_to_scan = None):
1051                 default = None
1052                 list = []
1053                 if index_to_scan is None:
1054                         index_to_scan = int(self.scan_nims.value)
1055                 defaultRegionForNIM = nimmanager.getTerrestrialDescription(index_to_scan)
1056                 for r in nimmanager.terrestrialsList:
1057                         if default is None and r[0] == defaultRegionForNIM:
1058                                 default = r[0]
1059                         list.append((r[0], r[0][:46]))
1060                 return ConfigSelection(choices = list, default = default)
1061
1062         def predefinedCabTranspondersList(self):
1063                 default = None
1064                 list = []
1065                 # 0 transponder type, 1 freq, 2 sym, 3 mod, 4 fec, 5 inv, 6 sys
1066                 compare = [1, self.scan_cab.frequency.value*1000, self.scan_cab.symbolrate.value*1000, self.scan_cab.modulation.value, self.scan_cab.fec.value, self.scan_cab.inversion.value, self.scan_cab.system.value]
1067                 i = 0
1068                 index_to_scan = int(self.scan_nims.value)
1069                 tps = nimmanager.getTranspondersCable(index_to_scan)
1070                 for tp in tps:
1071                         if tp[0] == 1: #CABLE
1072                                 if default is None and self.compareCabTransponders(tp, compare):
1073                                         default = str(i)
1074                                 list.append((str(i), self.humanReadableCabTransponder(tp)))
1075                                 i += 1
1076                 self.CableTransponders = ConfigSelection(choices = list, default = default)
1077                 return default
1078
1079         def humanReadableCabTransponder(self, tp):
1080                 if tp[3] in range (6) and (tp[4] in range (10) or tp[4] == 15):
1081                         mod_list = ['Auto', '16-QAM','32-QAM','64-QAM','128-QAM', '256-QAM']
1082                         fec_list = {0:"Auto", 1:'1/2', 2:'2/3', 3:'3/4', 4:'5/6', 5:'7/8', 6:'8/9', 7:'3/5', 8:'4/5', 9:'9/10', 15:'None'}
1083                         print str(tp[1]/1000) + " MHz " + fec_list[tp[4]] + " " + str(tp[2]/1000) + " " + mod_list[tp[3]]
1084                         return str(tp[1]/1000) + " MHz " + fec_list[tp[4]] + " " + str(tp[2]/1000) + " " + mod_list[tp[3]]
1085                 return _("Invalid transponder data")
1086
1087         def compareCabTransponders(self, tp, compare):
1088                 frequencyTolerance = 1000000 #1 MHz
1089                 symbolRateTolerance = 10
1090                 return abs(tp[1] - compare[1]) <= frequencyTolerance and abs(tp[2] - compare[2]) <= symbolRateTolerance and tp[3] == compare[3] and (not tp[4] or tp[4] == compare[4])
1091
1092         def startScan(self, tlist, flags, feid, networkid = 0):
1093                 if len(tlist):
1094                         # flags |= eComponentScan.scanSearchBAT
1095                         if self.finished_cb:
1096                                 self.session.openWithCallback(self.finished_cb, ServiceScan, [{"transponders": tlist, "feid": feid, "flags": flags, "networkid": networkid}])
1097                         else:
1098                                 self.session.openWithCallback(self.startScanCallback, ServiceScan, [{"transponders": tlist, "feid": feid, "flags": flags, "networkid": networkid}])
1099                 else:
1100                         if self.finished_cb:
1101                                 self.session.openWithCallback(self.finished_cb, MessageBox, _("Nothing to scan!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
1102                         else:
1103                                 self.session.open(MessageBox, _("Nothing to scan!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
1104
1105         def startScanCallback(self, answer):
1106                 if answer:
1107                         self.doCloseRecursive()
1108
1109         def keyCancel(self):
1110                 self.session.nav.playService(self.session.postScanService)
1111                 for x in self["config"].list:
1112                         x[1].cancel()
1113                 self.close()
1114
1115         def doCloseRecursive(self):
1116                 self.session.nav.playService(self.session.postScanService)
1117                 self.closeRecursive()
1118
1119 class ScanSimple(ConfigListScreen, Screen, CableTransponderSearchSupport):
1120         def getNetworksForNim(self, nim):
1121                 if nim.isCompatible("DVB-S"):
1122                         networks = nimmanager.getSatListForNim(nim.slot)
1123                 elif not nim.empty:
1124                         networks = [ nim.type ] # "DVB-C" or "DVB-T". TODO: seperate networks for different C/T tuners, if we want to support that.
1125                 else:
1126                         # empty tuners provide no networks.
1127                         networks = [ ]
1128                 return networks
1129
1130         def __init__(self, session):
1131                 Screen.__init__(self, session)
1132
1133                 self["actions"] = ActionMap(["SetupActions", "MenuActions"],
1134                 {
1135                         "ok": self.keyGo,
1136                         "save": self.keyGo,
1137                         "cancel": self.keyCancel,
1138                         "menu": self.doCloseRecursive,
1139                 }, -2)
1140
1141                 self.session.postScanService = session.nav.getCurrentlyPlayingServiceOrGroup()
1142
1143                 self.list = []
1144                 tlist = []
1145
1146                 known_networks = [ ]
1147                 nims_to_scan = [ ]
1148                 self.finished_cb = None
1149
1150                 for nim in nimmanager.nim_slots:
1151                         # collect networks provided by this tuner
1152
1153                         need_scan = False
1154                         networks = self.getNetworksForNim(nim)
1155
1156                         print "nim %d provides" % nim.slot, networks
1157                         print "known:", known_networks
1158
1159                         # we only need to scan on the first tuner which provides a network.
1160                         # this gives the first tuner for each network priority for scanning.
1161                         for x in networks:
1162                                 if x not in known_networks:
1163                                         need_scan = True
1164                                         print x, "not in ", known_networks
1165                                         known_networks.append(x)
1166
1167                         # don't offer to scan nims if nothing is connected
1168                         if not nimmanager.somethingConnected(nim.slot):
1169                                 need_scan = False
1170
1171                         if need_scan:
1172                                 nims_to_scan.append(nim)
1173
1174                 # we save the config elements to use them on keyGo
1175                 self.nim_enable = [ ]
1176
1177                 if len(nims_to_scan):
1178                         self.scan_networkScan = ConfigYesNo(default = True)
1179                         self.scan_clearallservices = ConfigSelection(default = "yes", choices = [("no", _("no")), ("yes", _("yes")), ("yes_hold_feeds", _("yes (keep feeds)"))])
1180                         self.list.append(getConfigListEntry(_("Network scan"), self.scan_networkScan))
1181                         self.list.append(getConfigListEntry(_("Clear before scan"), self.scan_clearallservices))
1182
1183                         for nim in nims_to_scan:
1184                                 nimconfig = ConfigYesNo(default = True)
1185                                 nimconfig.nim_index = nim.slot
1186                                 self.nim_enable.append(nimconfig)
1187                                 self.list.append(getConfigListEntry(_("Scan ") + nim.slot_name + " (" + nim.friendly_type + ")", nimconfig))
1188
1189                 ConfigListScreen.__init__(self, self.list)
1190                 self["header"] = Label(_("Automatic scan"))
1191                 self["footer"] = Label(_("Press OK to scan"))
1192
1193         def runAsync(self, finished_cb):
1194                 self.finished_cb = finished_cb
1195                 self.keyGo()
1196
1197         def keyGo(self):
1198                 InfoBarInstance = InfoBar.instance
1199                 if InfoBarInstance:
1200                         InfoBarInstance.checkTimeshiftRunning(self.keyGoCheckTimeshiftCallback)
1201                 else:
1202                         self.keyGoCheckTimeshiftCallback(True)
1203
1204         def keyGoCheckTimeshiftCallback(self, answer):
1205                 if answer:
1206                         self.scanList = []
1207                         self.known_networks = set()
1208                         self.nim_iter=0
1209                         self.buildTransponderList()
1210
1211         def buildTransponderList(self): # this method is called multiple times because of asynchronous stuff
1212                 APPEND_NOW = 0
1213                 SEARCH_CABLE_TRANSPONDERS = 1
1214                 action = APPEND_NOW
1215
1216                 n = self.nim_iter < len(self.nim_enable) and self.nim_enable[self.nim_iter] or None
1217                 self.nim_iter += 1
1218                 if n:
1219                         if n.value: # check if nim is enabled
1220                                 flags = 0
1221                                 nim = nimmanager.nim_slots[n.nim_index]
1222                                 networks = set(self.getNetworksForNim(nim))
1223                                 networkid = 0
1224
1225                                 # don't scan anything twice
1226                                 networks.discard(self.known_networks)
1227
1228                                 tlist = [ ]
1229                                 if nim.isCompatible("DVB-S"):
1230                                         # get initial transponders for each satellite to be scanned
1231                                         for sat in networks:
1232                                                 getInitialTransponderList(tlist, sat[0])
1233                                 elif nim.isCompatible("DVB-C"):
1234                                         if config.Nims[nim.slot].cable.scan_type.value == "provider":
1235                                                 getInitialCableTransponderList(tlist, nim.slot)
1236                                         else:
1237                                                 action = SEARCH_CABLE_TRANSPONDERS
1238                                                 networkid = config.Nims[nim.slot].cable.scan_networkid.value
1239                                 elif nim.isCompatible("DVB-T"):
1240                                         getInitialTerrestrialTransponderList(tlist, nimmanager.getTerrestrialDescription(nim.slot))
1241                                 else:
1242                                         assert False
1243
1244                                 flags = self.scan_networkScan.value and eComponentScan.scanNetworkSearch or 0
1245                                 tmp = self.scan_clearallservices.value
1246                                 if tmp == "yes":
1247                                         flags |= eComponentScan.scanRemoveServices
1248                                 elif tmp == "yes_hold_feeds":
1249                                         flags |= eComponentScan.scanRemoveServices
1250                                         flags |= eComponentScan.scanDontRemoveFeeds
1251
1252                                 if action == APPEND_NOW:
1253                                         self.scanList.append({"transponders": tlist, "feid": nim.slot, "flags": flags})
1254                                 elif action == SEARCH_CABLE_TRANSPONDERS:
1255                                         self.flags = flags
1256                                         self.feid = nim.slot
1257                                         self.networkid = networkid
1258                                         self.startCableTransponderSearch(nim.slot)
1259                                         return
1260                                 else:
1261                                         assert False
1262
1263                         self.buildTransponderList() # recursive call of this function !!!
1264                         return
1265                 # when we are here, then the recursion is finished and all enabled nims are checked
1266                 # so we now start the real transponder scan
1267                 self.startScan(self.scanList)
1268
1269         def startScan(self, scanList):
1270                 if len(scanList):
1271                         if self.finished_cb:
1272                                 self.session.openWithCallback(self.finished_cb, ServiceScan, scanList = scanList)
1273                         else:
1274                                 self.session.open(ServiceScan, scanList = scanList)
1275                 else:
1276                         if self.finished_cb:
1277                                 self.session.openWithCallback(self.finished_cb, MessageBox, _("Nothing to scan!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
1278                         else:
1279                                 self.session.open(MessageBox, _("Nothing to scan!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
1280
1281         def setCableTransponderSearchResult(self, tlist):
1282                 if tlist is not None:
1283                         self.scanList.append({"transponders": tlist, "feid": self.feid, "flags": self.flags})
1284
1285         def cableTransponderSearchFinished(self):
1286                 self.buildTransponderList()
1287
1288         def keyCancel(self):
1289                 self.session.nav.playService(self.session.postScanService)
1290                 self.close()
1291
1292         def doCloseRecursive(self):
1293                 self.session.nav.playService(self.session.postScanService)
1294                 self.closeRecursive()
1295
1296         def Satexists(self, tlist, pos):
1297                 for x in tlist:
1298                         if x == pos:
1299                                 return 1
1300                 return 0