call saveAll from within def run... so now save connectedTo configEntries
[enigma2.git] / lib / python / Screens / Satconfig.py
1 from enigma import eDVBDB
2 from Screen import Screen
3 from Components.SystemInfo import SystemInfo
4 from Components.ActionMap import ActionMap
5 from Components.ConfigList import ConfigListScreen
6 from Components.MenuList import MenuList
7 from Components.NimManager import nimmanager
8 from Components.config import getConfigListEntry, config, ConfigNothing, ConfigSelection, updateConfigElement
9 from Screens.MessageBox import MessageBox
10
11 from time import mktime, localtime
12 from datetime import datetime
13
14 class NimSetup(Screen, ConfigListScreen):
15         def createSimpleSetup(self, list, mode):
16                 if mode == "single":
17                         list.append(getConfigListEntry(_("Satellite"), self.nimConfig.diseqcA))
18                 else:
19                         list.append(getConfigListEntry(_("Port A"), self.nimConfig.diseqcA))
20
21                 if mode in ["toneburst_a_b", "diseqc_a_b", "diseqc_a_b_c_d"]:
22                         list.append(getConfigListEntry(_("Port B"), self.nimConfig.diseqcB))
23                         if mode == "diseqc_a_b_c_d":
24                                 list.append(getConfigListEntry(_("Port C"), self.nimConfig.diseqcC))
25                                 list.append(getConfigListEntry(_("Port D"), self.nimConfig.diseqcD))
26
27         def createPositionerSetup(self, list):
28                 nim = self.nimConfig
29                 list.append(getConfigListEntry(_("Longitude"), nim.longitude))
30                 list.append(getConfigListEntry(" ", nim.longitudeOrientation))
31                 list.append(getConfigListEntry(_("Latitude"), nim.latitude))
32                 list.append(getConfigListEntry(" ", nim.latitudeOrientation))
33                 if SystemInfo["CanMeasureFrontendInputPower"]:
34                         self.advancedPowerMeasurement = getConfigListEntry(_("Use Power Measurement"), nim.powerMeasurement)
35                         list.append(self.advancedPowerMeasurement)
36                         if nim.powerMeasurement.value:
37                                 list.append(getConfigListEntry(_("Power threshold in mA"), nim.powerThreshold))
38                                 self.turningSpeed = getConfigListEntry(_("Rotor turning speed"), nim.turningSpeed)
39                                 list.append(self.turningSpeed)
40                                 if nim.turningSpeed.value == "fast epoch":
41                                         self.turnFastEpochBegin = getConfigListEntry(_("Begin time"), nim.fastTurningBegin)
42                                         self.turnFastEpochEnd = getConfigListEntry(_("End time"), nim.fastTurningEnd)
43                                         list.append(self.turnFastEpochBegin)
44                                         list.append(self.turnFastEpochEnd)
45                 else:
46                         if nim.powerMeasurement.value:
47                                 nim.powerMeasurement.value = False
48                                 nim.powerMeasurement.save()
49                 
50         def createConfigMode(self):
51                 choices = { "nothing": _("nothing connected"),
52                                         "simple": _("simple"),
53                                         "advanced": _("advanced")}
54                 #if len(nimmanager.getNimListOfType(nimmanager.getNimType(self.slotid), exception = x)) > 0:
55                 #       choices["equal"] = _("equal to")
56                 #       choices["satposdepends"] = _("second cable of motorized LNB")
57                 if len(nimmanager.canEqualTo(self.slotid)) > 0:
58                         choices["equal"] = _("equal to")
59                 if len(nimmanager.canDependOn(self.slotid)) > 0:
60                         choices["satposdepends"] = _("second cable of motorized LNB")
61                 if len(nimmanager.canConnectTo(self.slotid)) > 0:
62                         choices["loopthrough"] = _("loopthrough to")
63                 self.nimConfig.configMode.setChoices(choices)
64                                                         
65         def createSetup(self):
66                 print "Creating setup"
67                 self.list = [ ]
68
69                 self.configMode = None
70                 self.diseqcModeEntry = None
71                 self.advancedSatsEntry = None
72                 self.advancedLnbsEntry = None
73                 self.advancedDiseqcMode = None
74                 self.advancedUsalsEntry = None
75                 self.advancedLof = None
76                 self.advancedPowerMeasurement = None
77                 self.turningSpeed = None
78                 self.turnFastEpochBegin = None
79                 self.turnFastEpochEnd = None
80                 self.uncommittedDiseqcCommand = None
81                 self.cableScanType = None
82                 self.have_advanced = False
83
84                 if self.nim.isCompatible("DVB-S"):
85                         self.configMode = getConfigListEntry(_("Configuration Mode"), self.nimConfig.configMode)
86                         self.list.append(self.configMode)
87
88                         if self.nimConfig.configMode.value == "simple":                 #simple setup
89                                 self.diseqcModeEntry = getConfigListEntry(_("DiSEqC Mode"), self.nimConfig.diseqcMode)
90                                 self.list.append(self.diseqcModeEntry)
91                                 if self.nimConfig.diseqcMode.value in ["single", "toneburst_a_b", "diseqc_a_b", "diseqc_a_b_c_d"]:
92                                         self.createSimpleSetup(self.list, self.nimConfig.diseqcMode.value)
93                                 if self.nimConfig.diseqcMode.value == "positioner":
94                                         self.createPositionerSetup(self.list)
95                         elif self.nimConfig.configMode.value == "equal":
96                                 choices = []
97                                 nimlist = nimmanager.canEqualTo(self.nim.slot)
98                                 for id in nimlist:
99                                         #choices.append((str(id), str(chr(65 + id))))
100                                         choices.append((str(id), nimmanager.getNimDescription(id)))
101                                 self.nimConfig.connectedTo.setChoices(choices)
102                                 #self.nimConfig.connectedTo = updateConfigElement(self.nimConfig.connectedTo, ConfigSelection(choices = choices))
103                                 self.list.append(getConfigListEntry(_("Tuner"), self.nimConfig.connectedTo))
104                         elif self.nimConfig.configMode.value == "satposdepends":
105                                 choices = []
106                                 nimlist = nimmanager.canDependOn(self.nim.slot)
107                                 for id in nimlist:
108                                         #choices.append((str(id), str(chr(65 + id))))
109                                         choices.append((str(id), nimmanager.getNimDescription(id)))
110                                 self.nimConfig.connectedTo.setChoices(choices)
111                                 #self.nimConfig.connectedTo = updateConfigElement(self.nimConfig.connectedTo, ConfigSelection(choices = choices))
112                                 self.list.append(getConfigListEntry(_("Tuner"), self.nimConfig.connectedTo))
113                         elif self.nimConfig.configMode.value == "loopthrough":
114                                 choices = []
115                                 print "connectable to:", nimmanager.canConnectTo(self.slotid)
116                                 connectable = nimmanager.canConnectTo(self.slotid)
117                                 for id in connectable:
118                                         choices.append((str(id), nimmanager.getNimDescription(id)))
119                                 self.nimConfig.connectedTo.setChoices(choices)
120                                 #self.nimConfig.connectedTo = updateConfigElement(self.nimConfig.connectedTo, ConfigSelection(choices = choices))
121                                 self.list.append(getConfigListEntry(_("Connected to"), self.nimConfig.connectedTo))
122                         elif self.nimConfig.configMode.value == "nothing":
123                                 pass
124                         elif self.nimConfig.configMode.value == "advanced": # advanced
125                                 # SATs
126                                 self.advancedSatsEntry = getConfigListEntry(_("Satellite"), self.nimConfig.advanced.sats)
127                                 self.list.append(self.advancedSatsEntry)
128                                 cur_orb_pos = self.nimConfig.advanced.sats.orbital_position
129                                 satlist = self.nimConfig.advanced.sat.keys()
130                                 if cur_orb_pos is not None:
131                                         if cur_orb_pos not in satlist:
132                                                 cur_orb_pos = satlist[0]
133                                         currSat = self.nimConfig.advanced.sat[cur_orb_pos]
134                                         self.fillListWithAdvancedSatEntrys(currSat)
135                                 self.have_advanced = True
136                 elif self.nim.isCompatible("DVB-C"):
137                         self.configMode = getConfigListEntry(_("Configuration Mode"), self.nimConfig.configMode)
138                         self.list.append(self.configMode)
139                         if self.nimConfig.configMode.value == "enabled":
140                                 self.cableScanType=getConfigListEntry(_("Used service scan type"), self.nimConfig.cable.scan_type)
141                                 self.list.append(self.cableScanType)
142                                 if self.nimConfig.cable.scan_type.value == "provider":
143                                         self.list.append(getConfigListEntry(_("Provider to scan"), self.nimConfig.cable.scan_provider))
144                                 else:
145                                         if self.nimConfig.cable.scan_type.value == "bands":
146                                                 self.list.append(getConfigListEntry(_("Scan band EU VHF I"), self.nimConfig.cable.scan_band_EU_VHF_I))
147                                                 self.list.append(getConfigListEntry(_("Scan band EU MID"), self.nimConfig.cable.scan_band_EU_MID))
148                                                 self.list.append(getConfigListEntry(_("Scan band EU VHF III"), self.nimConfig.cable.scan_band_EU_VHF_III))
149                                                 self.list.append(getConfigListEntry(_("Scan band EU UHF IV"), self.nimConfig.cable.scan_band_EU_UHF_IV))
150                                                 self.list.append(getConfigListEntry(_("Scan band EU UHF V"), self.nimConfig.cable.scan_band_EU_UHF_V))
151                                                 self.list.append(getConfigListEntry(_("Scan band EU SUPER"), self.nimConfig.cable.scan_band_EU_SUPER))
152                                                 self.list.append(getConfigListEntry(_("Scan band EU HYPER"), self.nimConfig.cable.scan_band_EU_HYPER))
153                                                 self.list.append(getConfigListEntry(_("Scan band US LOW"), self.nimConfig.cable.scan_band_US_LOW))
154                                                 self.list.append(getConfigListEntry(_("Scan band US MID"), self.nimConfig.cable.scan_band_US_MID))
155                                                 self.list.append(getConfigListEntry(_("Scan band US HIGH"), self.nimConfig.cable.scan_band_US_HIGH))
156                                                 self.list.append(getConfigListEntry(_("Scan band US SUPER"), self.nimConfig.cable.scan_band_US_SUPER))
157                                                 self.list.append(getConfigListEntry(_("Scan band US HYPER"), self.nimConfig.cable.scan_band_US_HYPER))
158                                         elif self.nimConfig.cable.scan_type.value == "steps":
159                                                 self.list.append(getConfigListEntry(_("Frequency scan step size(khz)"), self.nimConfig.cable.scan_frequency_steps))
160                                         self.list.append(getConfigListEntry(_("Scan QAM16"), self.nimConfig.cable.scan_mod_qam16))
161                                         self.list.append(getConfigListEntry(_("Scan QAM32"), self.nimConfig.cable.scan_mod_qam32))
162                                         self.list.append(getConfigListEntry(_("Scan QAM64"), self.nimConfig.cable.scan_mod_qam64))
163                                         self.list.append(getConfigListEntry(_("Scan QAM128"), self.nimConfig.cable.scan_mod_qam128))
164                                         self.list.append(getConfigListEntry(_("Scan QAM256"), self.nimConfig.cable.scan_mod_qam256))
165                                         self.list.append(getConfigListEntry(_("Scan SR6900"), self.nimConfig.cable.scan_sr_6900))
166                                         self.list.append(getConfigListEntry(_("Scan SR6875"), self.nimConfig.cable.scan_sr_6875))
167                                         self.list.append(getConfigListEntry(_("Scan additional SR"), self.nimConfig.cable.scan_sr_ext1))
168                                         self.list.append(getConfigListEntry(_("Scan additional SR"), self.nimConfig.cable.scan_sr_ext2))
169                         self.have_advanced = False
170                 elif self.nim.isCompatible("DVB-T"):
171                         self.configMode = getConfigListEntry(_("Configuration Mode"), self.nimConfig.configMode)
172                         self.list.append(self.configMode)
173                         self.have_advanced = False
174                         if self.nimConfig.configMode.value == "enabled":
175                                 self.list.append(getConfigListEntry(_("Terrestrial provider"), self.nimConfig.terrestrial))
176                                 self.list.append(getConfigListEntry(_("Enable 5V for active antenna"), self.nimConfig.terrestrial_5V))
177                 else:
178                         self.have_advanced = False
179                 self["config"].list = self.list
180                 self["config"].l.setList(self.list)
181
182         def newConfig(self):
183                 checkList = (self.configMode, self.diseqcModeEntry, self.advancedSatsEntry, \
184                         self.advancedLnbsEntry, self.advancedDiseqcMode, self.advancedUsalsEntry, \
185                         self.advancedLof, self.advancedPowerMeasurement, self.turningSpeed, \
186                         self.uncommittedDiseqcCommand, self.cableScanType)
187                 for x in checkList:
188                         if self["config"].getCurrent() == x:
189                                 self.createSetup()
190
191         def run(self):
192                 if self.have_advanced and self.nim.config_mode == "advanced":
193                         self.fillAdvancedList()
194                 for x in self.list:
195                         if x in [self.turnFastEpochBegin, self.turnFastEpochEnd]:
196                                 # workaround for storing only hour*3600+min*60 value in configfile
197                                 # not really needed.. just for cosmetics..
198                                 tm = localtime(x[1].value)
199                                 dt = datetime(1970, 1, 1, tm.tm_hour, tm.tm_min)
200                                 x[1].value = int(mktime(dt.timetuple()))
201                         x[1].save()
202                 nimmanager.sec.update()
203                 self.saveAll()
204
205         def fillListWithAdvancedSatEntrys(self, Sat):
206                 currLnb = self.nimConfig.advanced.lnb[int(Sat.lnb.value)]
207                 
208                 if isinstance(currLnb, ConfigNothing):
209                         currLnb = None
210
211                 self.list.append(getConfigListEntry(_("Voltage mode"), Sat.voltage))
212                 self.list.append(getConfigListEntry(_("Tone mode"), Sat.tonemode))
213                 if currLnb and currLnb.diseqcMode.value == "1_2":
214                         self.advancedUsalsEntry = getConfigListEntry(_("Use usals for this sat"), Sat.usals)
215                         self.list.append(self.advancedUsalsEntry)
216                         if not Sat.usals.value:
217                                 self.list.append(getConfigListEntry(_("Stored position"), Sat.rotorposition))
218
219                 # LNBs
220                 self.advancedLnbsEntry = getConfigListEntry(_("LNB"), Sat.lnb)
221                 self.list.append(self.advancedLnbsEntry)
222                 if currLnb:
223                         self.advancedDiseqcMode = getConfigListEntry(_("DiSEqC mode"), currLnb.diseqcMode)
224                         self.list.append(self.advancedDiseqcMode)
225                         if currLnb.diseqcMode.value != "none":
226                                 self.list.append(getConfigListEntry(_("Toneburst"), currLnb.toneburst))
227                                 self.list.append(getConfigListEntry(_("Committed DiSEqC command"), currLnb.commitedDiseqcCommand))
228                                 self.list.append(getConfigListEntry(_("Fast DiSEqC"), currLnb.fastDiseqc))
229                                 self.list.append(getConfigListEntry(_("Sequence repeat"), currLnb.sequenceRepeat))
230                                 if currLnb.diseqcMode.value == "1_0":
231                                         self.list.append(getConfigListEntry(_("Command order"), currLnb.commandOrder1_0))
232                                 else:
233                                         if currLnb.uncommittedDiseqcCommand.index:
234                                                 if currLnb.commandOrder.value == "ct":
235                                                         currLnb.commandOrder.value = "cut"
236                                                 elif currLnb.commandOrder.value == "tc":
237                                                         currLnb.commandOrder.value = "tcu"
238                                         else:
239                                                 if currLnb.commandOrder.index & 1:
240                                                         currLnb.commandOrder.value = "tc"
241                                                 else:
242                                                         currLnb.commandOrder.value = "ct"
243                                         self.list.append(getConfigListEntry(_("Command order"), currLnb.commandOrder))
244                                         self.uncommittedDiseqcCommand = getConfigListEntry(_("Uncommitted DiSEqC command"), currLnb.uncommittedDiseqcCommand)
245                                         self.list.append(self.uncommittedDiseqcCommand)
246                                         self.list.append(getConfigListEntry(_("DiSEqC repeats"), currLnb.diseqcRepeats))
247                                 if currLnb.diseqcMode.value == "1_2":
248                                         self.list.append(getConfigListEntry(_("Longitude"), currLnb.longitude))
249                                         self.list.append(getConfigListEntry(" ", currLnb.longitudeOrientation))
250                                         self.list.append(getConfigListEntry(_("Latitude"), currLnb.latitude))
251                                         self.list.append(getConfigListEntry(" ", currLnb.latitudeOrientation))
252                                         if SystemInfo["CanMeasureFrontendInputPower"]:
253                                                 self.advancedPowerMeasurement = getConfigListEntry(_("Use Power Measurement"), currLnb.powerMeasurement)
254                                                 self.list.append(self.advancedPowerMeasurement)
255                                                 if currLnb.powerMeasurement.value:
256                                                         self.list.append(getConfigListEntry(_("Power threshold in mA"), currLnb.powerThreshold))
257                                                         self.turningSpeed = getConfigListEntry(_("Rotor turning speed"), currLnb.turningSpeed)
258                                                         self.list.append(self.turningSpeed)
259                                                         if currLnb.turningSpeed.value == "fast epoch":
260                                                                 self.turnFastEpochBegin = getConfigListEntry(_("Begin time"), currLnb.fastTurningBegin)
261                                                                 self.turnFastEpochEnd = getConfigListEntry(_("End time"), currLnb.fastTurningEnd)
262                                                                 self.list.append(self.turnFastEpochBegin)
263                                                                 self.list.append(self.turnFastEpochEnd)
264                                         else:
265                                                 if currLnb.powerMeasurement.value:
266                                                         currLnb.powerMeasurement.value = False
267                                                         currLnb.powerMeasurement.save()
268                         self.advancedLof = getConfigListEntry(_("LOF"), currLnb.lof)
269                         self.list.append(self.advancedLof)
270                         if currLnb.lof.value == "user_defined":
271                                 self.list.append(getConfigListEntry(_("LOF/L"), currLnb.lofl))
272                                 self.list.append(getConfigListEntry(_("LOF/H"), currLnb.lofh))
273                                 self.list.append(getConfigListEntry(_("Threshold"), currLnb.threshold))
274 #                       self.list.append(getConfigListEntry(_("12V Output"), currLnb.output_12v))
275                         self.list.append(getConfigListEntry(_("Increased voltage"), currLnb.increased_voltage))
276
277         def fillAdvancedList(self):
278                 self.list = [ ]
279                 self.configMode = getConfigListEntry(_("Configuration Mode"), self.nimConfig.configMode)
280                 self.list.append(self.configMode)
281                 self.advancedSatsEntry = getConfigListEntry(_("Satellite"), self.nimConfig.advanced.sats)
282                 self.list.append(self.advancedSatsEntry)
283                 for x in nimmanager.satList:
284                         Sat = self.nimConfig.advanced.sat[x[0]]
285                         self.fillListWithAdvancedSatEntrys(Sat)
286                 self["config"].list = self.list
287
288         def keySave(self):
289                 old_configured_sats = nimmanager.getConfiguredSats()
290                 self.run()
291                 new_configured_sats = nimmanager.getConfiguredSats()
292                 self.unconfed_sats = old_configured_sats - new_configured_sats
293                 self.satpos_to_remove = None
294                 self.deleteConfirmed(False)
295
296         def deleteConfirmed(self, confirmed):
297                 if confirmed:
298                         eDVBDB.getInstance().removeServices(-1, -1, -1, self.satpos_to_remove)
299
300                 if self.satpos_to_remove is not None:
301                         self.unconfed_sats.remove(self.satpos_to_remove)
302
303                 self.satpos_to_remove = None
304                 for orbpos in self.unconfed_sats:
305                         self.satpos_to_remove = orbpos
306                         orbpos = self.satpos_to_remove
307                         try:
308                                 # why we need this cast?
309                                 sat_name = str(nimmanager.getSatDescription(orbpos))
310                         except:
311                                 if orbpos > 1800: # west
312                                         orbpos = 3600 - orbpos
313                                         h = _("W")
314                                 else:
315                                         h = _("E")
316                                 sat_name = ("%d.%d" + h) % (orbpos / 10, orbpos % 10)
317                         self.session.openWithCallback(self.deleteConfirmed, MessageBox, _("Delete no more configured satellite\n%s?") %(sat_name))
318                         break
319                 if not self.satpos_to_remove:
320                         self.close()
321
322         def __init__(self, session, slotid):
323                 Screen.__init__(self, session)
324                 self.list = [ ]
325
326                 ConfigListScreen.__init__(self, self.list)
327
328                 self["actions"] = ActionMap(["SetupActions"],
329                 {
330                         "ok": self.keySave,
331                         "cancel": self.keyCancel,
332                 }, -2)
333
334                 self.slotid = slotid
335                 self.nim = nimmanager.nim_slots[slotid]
336                 self.nimConfig = self.nim.config
337                 self.createConfigMode()
338                 self.createSetup()
339
340         def keyLeft(self):
341                 ConfigListScreen.keyLeft(self)
342                 self.newConfig()
343
344         def keyRight(self):
345                 ConfigListScreen.keyRight(self)
346                 self.newConfig()
347                 
348         def saveAll(self):
349                 if self.nim.isCompatible("DVB-S"):
350                         # reset connectedTo to all choices to properly store the default value
351                         choices = []
352                         nimlist = nimmanager.getNimListOfType("DVB-S", self.slotid)
353                         for id in nimlist:
354                                 choices.append((str(id), nimmanager.getNimDescription(id)))
355                         self.nimConfig.connectedTo.setChoices(choices)
356                 for x in self["config"].list:
357                         x[1].save()
358                         
359         def cancelConfirm(self, result):
360                 if not result:
361                         return
362
363                 for x in self["config"].list:
364                         x[1].cancel()
365                 # we need to call saveAll to reset the connectedTo choices
366                 self.saveAll()
367                 self.close()
368                         
369 class NimSelection(Screen):
370         def __init__(self, session):
371                 Screen.__init__(self, session)
372                 
373                 menu = [ ]
374                 for x in nimmanager.nim_slots:
375                         menu.append((x.friendly_full_description, x))
376
377                 self["nimlist"] = MenuList(menu)
378
379                 self["actions"] = ActionMap(["OkCancelActions"],
380                 {
381                         "ok": self.okbuttonClick ,
382                         "cancel": self.close
383                 }, -2)
384
385         def okbuttonClick(self):
386                 nim = self["nimlist"].getCurrent()
387                 nim = nim and nim[1]
388                 if nim is not None and not nim.empty:
389                         self.session.open(NimSetup, nim.slot)