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