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