Screens/NetworkSetup.py, SystemPlugins/WirelessLan,Networkwizard: dont use "hidden...
[enigma2.git] / lib / python / Plugins / SystemPlugins / NetworkWizard / NetworkWizard.py
1 from Screens.Wizard import wizardManager, WizardSummary
2 from Screens.WizardLanguage import WizardLanguage
3 from Screens.Rc import Rc
4 from Screens.MessageBox import MessageBox
5 from Components.Pixmap import Pixmap, MovingPixmap, MultiPixmap
6 from Components.Sources.Boolean import Boolean
7 from Components.Network import iNetwork
8 from Tools.Directories import resolveFilename, SCOPE_PLUGINS, SCOPE_SKIN_IMAGE
9 from enigma import eTimer
10 from os import system
11
12 class NetworkWizard(WizardLanguage, Rc):
13         skin = """
14                 <screen position="0,0" size="720,576" title="Welcome..." flags="wfNoBorder" >
15                         <widget name="text" position="153,40" size="340,300" font="Regular;22" />
16                         <widget source="list" render="Listbox" position="53,340" size="440,180" scrollbarMode="showOnDemand" >
17                                 <convert type="StringList" />
18                         </widget>
19                         <widget name="config" position="53,340" zPosition="1" size="440,180" transparent="1" scrollbarMode="showOnDemand" />
20                         <ePixmap pixmap="skin_default/buttons/button_red.png" position="40,225" zPosition="0" size="15,16" transparent="1" alphatest="on" />
21                         <widget name="languagetext" position="55,225" size="95,30" font="Regular;18" />
22                         <widget name="wizard" pixmap="skin_default/wizard.png" position="40,50" zPosition="10" size="110,174" alphatest="on" />
23                         <widget name="rc" pixmaps="skin_default/rc.png,skin_default/rcold.png" position="500,50" zPosition="10" size="154,500" alphatest="on" />
24                         <widget name="arrowdown" pixmap="skin_default/arrowdown.png" position="-100,-100" zPosition="11" size="37,70" alphatest="on" />
25                         <widget name="arrowdown2" pixmap="skin_default/arrowdown.png" position="-100,-100" zPosition="11" size="37,70" alphatest="on" />
26                         <widget name="arrowup" pixmap="skin_default/arrowup.png" position="-100,-100" zPosition="11" size="37,70" alphatest="on" />
27                         <widget name="arrowup2" pixmap="skin_default/arrowup.png" position="-100,-100" zPosition="11" size="37,70" alphatest="on" />
28                         <widget source="VKeyIcon" render="Pixmap" pixmap="skin_default/buttons/key_text.png" position="40,260" zPosition="0" size="35,25" transparent="1" alphatest="on" >
29                                 <convert type="ConditionalShowHide" />
30                         </widget>
31                         <widget name="HelpWindow" pixmap="skin_default/buttons/key_text.png" position="125,170" zPosition="1" size="1,1" transparent="1" alphatest="on" />      
32                 </screen>"""
33         def __init__(self, session, interface = None):
34                 self.xmlfile = resolveFilename(SCOPE_PLUGINS, "SystemPlugins/NetworkWizard/networkwizard.xml")
35                 WizardLanguage.__init__(self, session, showSteps = False, showStepSlider = False)
36                 Rc.__init__(self)
37                 self.session = session
38                 self["wizard"] = Pixmap()
39                 self["HelpWindow"] = Pixmap()
40                 self["HelpWindow"].hide()
41                 self["VKeyIcon"] = Boolean(False)
42
43                 self.InstalledInterfaceCount = None
44                 self.Adapterlist = None
45                 self.InterfaceState = None
46                 self.isInterfaceUp = None
47                 self.WlanPluginInstalled = False
48                 self.ap = None
49                 self.w = None
50                 if interface is not None:
51                         self.selectedInterface = interface
52                 else:
53                         self.selectedInterface = None
54                 self.NextStep = None
55                 self.resetRef = None
56                 self.checkRef = None
57                 self.AdapterRef = None
58                 self.APList = None
59                 self.newAPlist = None
60                 self.WlanList = None
61                 self.oldlist = None
62                 
63                 self.originalInterfaceState = {}
64                 self.originalInterfaceStateChanged = False
65                 self.Text = None
66                 self.rescanTimer = eTimer()
67                 self.rescanTimer.callback.append(self.rescanTimerFired)
68                 self.getInstalledInterfaceCount()
69                 self.isWlanPluginInstalled()
70                 
71         def exitWizardQuestion(self, ret = False):
72                 if (ret):
73                         self.markDone()
74                         self.close()
75                 
76         def markDone(self):
77                 self.stopScan()
78                 del self.rescanTimer
79                 self.checkOldInterfaceState()
80                 pass
81
82         def stopScan(self):
83                 self.rescanTimer.stop()
84                 if self.w is not None:
85                         from Plugins.SystemPlugins.WirelessLan.Wlan import iWlan
86                         iWlan.stopGetNetworkList()
87                         self.w = None
88
89         def getInstalledInterfaceCount(self):
90                 self.originalInterfaceState = {}
91                 self.stopScan()
92                 self.Adapterlist = iNetwork.getAdapterList()
93                 self.InstalledInterfaceCount = len(self.Adapterlist)
94                 if self.Adapterlist is not None:
95                         if self.InstalledInterfaceCount == 1 and self.selectedInterface is None:
96                                         self.selectedInterface = self.Adapterlist[0]
97                 for interface in iNetwork.getAdapterList():
98                         self.originalInterfaceState[interface] = {}
99                         self.originalInterfaceState[interface]["up"] = iNetwork.getAdapterAttribute(interface, 'up')
100
101         def selectInterface(self):
102                 self.InterfaceState = None
103                 if self.selectedInterface is None:
104                         if self.InstalledInterfaceCount <= 1:
105                                 if not iNetwork.isWirelessInterface(self.selectedInterface):
106                                         self.NextStep = 'nwconfig'
107                                 else:
108                                         self.NextStep = 'scanwlan'
109                                 self.checkInterface(self.selectedInterface)
110                         else:
111                                 self.NextStep = 'selectinterface'
112                                 self.currStep = self.getStepWithID(self.NextStep)
113                                 self.afterAsyncCode()
114                 else:
115                         if not iNetwork.isWirelessInterface(self.selectedInterface):
116                                 self.NextStep = 'nwconfig'
117                         else:
118                                 self.NextStep = 'scanwlan'
119                         self.checkInterface(self.selectedInterface)
120
121         def checkOldInterfaceState(self):
122                 # disable up interface if it was originally down and config is unchanged.
123                 if self.originalInterfaceStateChanged is False:
124                         for interface in self.originalInterfaceState.keys():
125                                 if self.originalInterfaceState[interface]["up"] is False:
126                                         if iNetwork.checkforInterface(interface) is True:
127                                                 system("ifconfig " + interface + " down")
128
129         def listInterfaces(self):
130                 self.stopScan()
131                 self.checkOldInterfaceState()
132                 list = [(iNetwork.getFriendlyAdapterName(x),x) for x in iNetwork.getAdapterList()]
133                 list.append((_("Exit network wizard"), "end"))
134                 return list
135
136         def InterfaceSelectionMade(self, index):
137                 self.selectedInterface = index
138                 self.InterfaceSelect(index)
139
140         def InterfaceSelect(self, index):
141                 if index == 'end':
142                         self.NextStep = 'end'
143                 elif index == 'eth0':
144                         self.NextStep = 'nwconfig'
145                 else:
146                         self.NextStep = 'scanwlan'
147
148         def InterfaceSelectionMoved(self):
149                 self.InterfaceSelect(self.selection)
150                 
151         def checkInterface(self,iface):
152                 self.stopScan()
153                 if self.Adapterlist is None:
154                         self.Adapterlist = iNetwork.getAdapterList()
155                 if self.NextStep is not 'end':
156                         if len(self.Adapterlist) == 0:
157                                 #Reset Network to defaults if network broken
158                                 iNetwork.resetNetworkConfig('lan', self.resetNetworkConfigCB)
159                                 self.resetRef = self.session.openWithCallback(self.resetNetworkConfigFinished, MessageBox, _("Please wait while we prepare your network interfaces..."), type = MessageBox.TYPE_INFO, enable_input = False)
160                         if iface in iNetwork.getInstalledAdapters():
161                                 if iface in iNetwork.configuredNetworkAdapters and len(iNetwork.configuredNetworkAdapters) == 1:
162                                         if iNetwork.getAdapterAttribute(iface, 'up') is True:
163                                                 self.isInterfaceUp = True
164                                         else:
165                                                 self.isInterfaceUp = False
166                                         self.currStep = self.getStepWithID(self.NextStep)
167                                         self.afterAsyncCode()
168                                 else:
169                                         self.isInterfaceUp = iNetwork.checkforInterface(iface)
170                                         self.currStep = self.getStepWithID(self.NextStep)
171                                         self.afterAsyncCode()
172                 else:
173                         self.resetNetworkConfigFinished(False)
174                         
175         def resetNetworkConfigFinished(self,data):
176                 if data is True:
177                         self.currStep = self.getStepWithID(self.NextStep)
178                         self.afterAsyncCode()
179                 else:
180                         self.currStep = self.getStepWithID(self.NextStep)
181                         self.afterAsyncCode()
182
183         def resetNetworkConfigCB(self,callback,iface):
184                 if callback is not None:
185                         if callback is True:
186                                 iNetwork.getInterfaces(self.getInterfacesFinished)
187                                 
188         def getInterfacesFinished(self, data):
189                 if data is True:
190                         if iNetwork.getAdapterAttribute(self.selectedInterface, 'up') is True:
191                                 self.isInterfaceUp = True
192                         else:
193                                 self.isInterfaceUp = False
194                         self.resetRef.close(True)
195                 else:
196                         print "we should never come here!"
197
198         def AdapterSetupEnd(self, iface):
199                 self.originalInterfaceStateChanged = True
200                 if iNetwork.getAdapterAttribute(iface, "dhcp") is True:
201                         iNetwork.checkNetworkState(self.AdapterSetupEndFinished)
202                         self.AdapterRef = self.session.openWithCallback(self.AdapterSetupEndCB, MessageBox, _("Please wait while we test your network..."), type = MessageBox.TYPE_INFO, enable_input = False)
203                 else:
204                         self.currStep = self.getStepWithID("confdns")
205                         self.afterAsyncCode()
206
207         def AdapterSetupEndCB(self,data):
208                 if data is True:
209                         if iNetwork.isWirelessInterface(self.selectedInterface):
210                                 if self.WlanPluginInstalled == True:
211                                         from Plugins.SystemPlugins.WirelessLan.Wlan import iStatus
212                                         iStatus.getDataForInterface(self.selectedInterface,self.checkWlanStateCB)
213                                 else:
214                                         self.currStep = self.getStepWithID("checklanstatusend")
215                                         self.afterAsyncCode()                                   
216                         else:
217                                 self.currStep = self.getStepWithID("checklanstatusend")
218                                 self.afterAsyncCode()
219
220         def AdapterSetupEndFinished(self,data):
221                 if data <= 2:
222                         self.InterfaceState = True
223                 else:
224                         self.InterfaceState = False
225                 self.AdapterRef.close(True)
226                         
227         def checkWlanStateCB(self,data,status):
228                 if data is not None:
229                         if data is True:
230                                 if status is not None:
231                                         text1 = _("Your Dreambox is now ready to use.\n\nYour internet connection is working now.\n\n")
232                                         text2 = _('Accesspoint:') + "\t" + str(status[self.selectedInterface]["acesspoint"]) + "\n"
233                                         text3 = _('SSID:') + "\t" + str(status[self.selectedInterface]["essid"]) + "\n"
234                                         text4 = _('Link Quality:') + "\t" + str(status[self.selectedInterface]["quality"])+ "\n"
235                                         text5 = _('Signal Strength:') + "\t" + str(status[self.selectedInterface]["signal"]) + "\n"
236                                         text6 = _('Bitrate:') + "\t" + str(status[self.selectedInterface]["bitrate"]) + "\n"
237                                         text7 = _('Encryption:') + " " + str(status[self.selectedInterface]["encryption"]) + "\n"
238                                         text8 = _("Please press OK to continue.")
239                                         infotext = text1 + text2 + text3 + text4 + text5 + text7 +"\n" + text8
240                                         self.currStep = self.getStepWithID("checkWlanstatusend")
241                                         self.Text = infotext
242                                         if str(status[self.selectedInterface]["acesspoint"]) == "Not-Associated":
243                                                 self.InterfaceState = False
244                                         self.afterAsyncCode()
245
246         def checkNetwork(self):
247                 iNetwork.checkNetworkState(self.checkNetworkStateCB)
248                 self.checkRef = self.session.openWithCallback(self.checkNetworkCB, MessageBox, _("Please wait while we test your network..."), type = MessageBox.TYPE_INFO, enable_input = False)
249
250         def checkNetworkCB(self,data):
251                 if data is True:
252                         if iNetwork.isWirelessInterface(self.selectedInterface):
253                                 if self.WlanPluginInstalled == True:
254                                         from Plugins.SystemPlugins.WirelessLan.Wlan import iStatus
255                                         iStatus.getDataForInterface(self.selectedInterface,self.checkWlanStateCB)
256                                 else:
257                                         self.currStep = self.getStepWithID("checklanstatusend")
258                                         self.afterAsyncCode()                                   
259                         else:
260                                 self.currStep = self.getStepWithID("checklanstatusend")
261                                 self.afterAsyncCode()
262
263         def checkNetworkStateCB(self,data):
264                 if data <= 2:
265                         self.InterfaceState = True
266                 else:
267                         self.InterfaceState = False
268                 self.checkRef.close(True)
269         
270         def rescanTimerFired(self):
271                 self.rescanTimer.stop()
272                 self.updateAPList()
273
274         def updateAPList(self):
275                 self.oldlist = self.APList
276                 self.newAPlist = []
277                 newList = []
278                 newListIndex = None
279                 currentListEntry = None
280                 newList = self.listAccessPoints()
281
282                 for oldentry in self.oldlist:
283                         if oldentry not in newList:
284                                 newList.append(oldentry)
285                                 
286                 for newentry in newList:
287                         if newentry[1] == "<hidden>":
288                                 continue
289                         self.newAPlist.append(newentry)
290                 
291                 if len(self.newAPlist):
292                         if "<hidden>" not in self.newAPlist:
293                                 self.newAPlist.append(( _("enter hidden network SSID"), "<hidden>" ))
294
295                         if (self.wizard[self.currStep].has_key("dynamiclist")):
296                                 currentListEntry = self["list"].getCurrent()
297                                 idx = 0
298                                 for entry in self.newAPlist:
299                                         if entry == currentListEntry:
300                                                 newListIndex = idx
301                                         idx +=1
302                                 self.wizard[self.currStep]["evaluatedlist"] = self.newAPlist
303                                 self['list'].setList(self.newAPlist)
304                                 self["list"].setIndex(newListIndex)
305                                 self["list"].updateList(self.newAPlist)
306
307         def listAccessPoints(self):
308                 self.APList = []
309                 if self.WlanPluginInstalled is False:
310                         self.APList.append( ( _("No networks found"),_("unavailable") ) )
311                         return self.APList
312                 else:
313                         try:
314                                 from Plugins.SystemPlugins.WirelessLan.Wlan import iWlan
315                                 iWlan.setInterface(self.selectedInterface)
316                                 self.w = iWlan.getInterface()
317                                 aps = iWlan.getNetworkList()
318                         except ValueError:
319                                 self.APList = []
320                                 self.APList.append( ( _("No networks found"),_("unavailable") ) )
321                                 return self.APList
322
323                         if aps is not None:
324                                 print "[NetworkWizard.py] got Accespoints!"
325                                 tmplist = []
326                                 complist = []
327                                 for ap in aps:
328                                         a = aps[ap]
329                                         if a['active']:
330                                                 tmplist.append( (a['bssid'], a['essid']) )
331                                                 complist.append( (a['bssid'], a['essid']) )
332                                 
333                                 for entry in tmplist:
334                                         if entry[1] == "":
335                                                 for compentry in complist:
336                                                         if compentry[0] == entry[0]:
337                                                                 complist.remove(compentry)
338                                 for entry in complist:
339                                         self.APList.append( (entry[1], entry[1]) )
340
341                         if "<hidden>" not in self.APList:
342                                 self.APList.append(( _("enter hidden network SSID"), "<hidden>" ))
343                         
344                         self.rescanTimer.start(5000)
345                         return self.APList
346
347         def AccessPointsSelectionMade(self, index):
348                 self.ap = index
349                 self.WlanList = []
350                 if (self.wizard[self.currStep].has_key("dynamiclist")):
351                         for entry in self['list'].list:
352                                 if entry[1] == "<hidden>":
353                                         continue
354                                 self.WlanList.append( (entry[1], entry[0]) )
355                 self.AccessPointsSelect(index)
356
357         def AccessPointsSelect(self, index):
358                 self.NextStep = 'wlanconfig'
359
360         def AccessPointsSelectionMoved(self):
361                 self.AccessPointsSelect(self.selection)
362
363         def checkWlanSelection(self):
364                 self.stopScan()
365                 self.currStep = self.getStepWithID(self.NextStep)
366
367         def isWlanPluginInstalled(self):
368                 try:
369                         from Plugins.SystemPlugins.WirelessLan.Wlan import iWlan
370                 except ImportError:
371                         self.WlanPluginInstalled = False
372                 else:
373                         self.WlanPluginInstalled = True
374
375         def listChoices(self):
376                 self.stopScan()
377                 list = []
378                 if self.WlanPluginInstalled == True:
379                         list.append((_("Configure your wireless LAN again"), "scanwlan"))
380                 list.append((_("Configure your internal LAN"), "nwconfig"))
381                 list.append((_("Exit network wizard"), "end"))          
382                 return list
383
384         def ChoicesSelectionMade(self, index):
385                 self.ChoicesSelect(index)
386
387         def ChoicesSelect(self, index):
388                 if index == 'end':
389                         self.NextStep = 'end'
390                 elif index == 'nwconfig':
391                         self.selectedInterface = "eth0"
392                         self.NextStep = 'nwconfig'
393                 else:
394                         self.NextStep = 'scanwlan'
395
396         def ChoicesSelectionMoved(self):
397                 pass