Add NetworkWizard to configure LAN/WLAN on Enigma2 first-startup
[enigma2-plugins.git] / networkwizard / src / NetworkWizard.py
1 from Screens.Wizard import wizardManager, WizardSummary
2 from Screens.WizardLanguage import WizardLanguage
3 from Components.Pixmap import Pixmap, MovingPixmap
4 from Components.config import config, ConfigBoolean, configfile, ConfigYesNo, NoSave, ConfigSubsection, ConfigText, getConfigListEntry, ConfigSelection
5 from Components.Network import iNetwork
6 from Components.Label import Label
7 from Components.MenuList import MenuList
8 from Components.PluginComponent import plugins
9 from Plugins.Plugin import PluginDescriptor
10 from Plugins.SystemPlugins.WirelessLan.plugin import *
11 from Plugins.SystemPlugins.WirelessLan.Wlan import Wlan, WlanList, wpaSupplicant
12 from Tools.Directories import resolveFilename, SCOPE_PLUGINS, SCOPE_SKIN_IMAGE
13 import time, os, re
14
15 config.misc.firstrun = ConfigBoolean(default = True)
16 list = []
17 list.append(_("WEP"))
18 list.append(_("WPA"))
19 list.append(_("WPA2"))
20
21 config.plugins.wlan = ConfigSubsection()
22 config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False))
23
24 config.plugins.wlan.encryption = ConfigSubsection()
25 config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = False))
26 config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = _("WPA")))
27 config.plugins.wlan.encryption.psk = NoSave(ConfigText(default = "mysecurewlan", fixed_size = False))
28
29 class NetworkWizard(WizardLanguage):
30         skin = """
31                 <screen position="0,0" size="720,576" title="Welcome..." flags="wfNoBorder" >
32                         <widget name="text" position="153,50" size="340,300" font="Regular;22" />
33                         <widget source="list" render="Listbox" position="53,310" size="440,220" scrollbarMode="showOnDemand" >
34                                 <convert type="StringList" />
35                         </widget>
36                         <widget name="config" position="53,310" zPosition="1" size="440,220" transparent="1" scrollbarMode="showOnDemand" />
37                         <widget name="wizard" pixmap="skin_default/wizard.png" position="40,50" zPosition="10" size="110,174" transparent="1" alphatest="on"/>
38                         <ePixmap pixmap="skin_default/buttons/button_red.png" position="40,225" zPosition="0" size="15,16" transparent="1" alphatest="on" />
39                         <widget name="languagetext" position="55,225" size="95,30" font="Regular;18" />
40                         <widget name="rc" pixmap="skin_default/rc.png" position="500,600" zPosition="10" size="154,475" transparent="1" alphatest="on"/>
41                         <widget name="arrowdown" pixmap="skin_default/arrowdown.png" position="0,0" zPosition="11" size="37,70" transparent="1" alphatest="on"/>
42                         <widget name="arrowup" pixmap="skin_default/arrowup.png" position="-100,-100" zPosition="11" size="37,70" transparent="1" alphatest="on"/>
43                         <widget name="arrowup2" pixmap="skin_default/arrowup.png" position="-100,-100" zPosition="11" size="37,70" transparent="1" alphatest="on"/>
44                 </screen>"""
45         def __init__(self, session):
46                 self.xmlfile = resolveFilename(SCOPE_PLUGINS, "SystemPlugins/NetworkWizard/networkwizard.xml")
47                 
48                 WizardLanguage.__init__(self, session, showSteps = False, showStepSlider = False)
49                 self.session = session
50                 self["wizard"] = Pixmap()
51                 self["rc"] = MovingPixmap()
52                 self["arrowdown"] = MovingPixmap()
53                 self["arrowup"] = MovingPixmap()
54                 self["arrowup2"] = MovingPixmap()
55                 
56                 self.InterfaceState = None
57                 self.isInterfaceUp = None
58                 self.ap = None
59
60         def checkInterface(self,iface):
61                 self.Adapterlist = iNetwork.getAdapterList()
62                 if len(self.Adapterlist) == 0:
63                         #Reset Network to defaults if network broken
64                         iNetwork.resetNetworkConfig('lan')
65                 if iface == 'eth0':
66                         iNetwork.resetNetworkConfig('lan')
67                         self.InterfaceUp('eth0')
68                 if iface == 'wlan0':
69                         iNetwork.resetNetworkConfig('wlan')
70                         self.InterfaceUp('wlan0')
71
72         def InterfaceUp(self,iface):
73                 iNetwork.getInterfaces()
74                 if iNetwork.getAdapterAttribute(iface, 'up') is True:
75                         self.isInterfaceUp = True
76                 else:
77                         self.isInterfaceUp = False
78                         
79         def markDone(self):
80                 pass
81
82         def listModes(self):
83                 self.w = Wlan('wlan0')
84                 aps = self.w.getNetworkList()
85                 list = []
86                 if aps is not None:
87                         print "[Wlan.py] got Accespoints!"
88                         for ap in aps:
89                                 a = aps[ap]
90                                 if a['active']:
91                                         if a['essid'] == "":
92                                                 a['essid'] = a['bssid']
93                                         list.append( (a['essid'], a['essid']) )         
94                 list.sort(key = lambda x: x[0])
95                 return list
96         
97
98         def modeSelectionMade(self, index):
99                 print "modeSelectionMade:", index
100                 self.ap = index
101                 self.modeSelect(index)
102                 
103         def modeSelectionMoved(self):
104                 print "mode selection moved:", self.selection
105                 self.modeSelect(self.selection)
106                 
107         def modeSelect(self, mode):
108                 print "ModeSelected:", mode
109
110         def saveAccessPoint(self, mode):
111                 config.plugins.wlan.essid.value = str(mode)
112                 config.plugins.wlan.essid.save()
113                 config.plugins.wlan.encryption.enabled.value = False
114                 config.plugins.wlan.encryption.enabled.save()           
115
116         def checkNetwork(self):
117                 ret = iNetwork.checkNetworkState()
118                 if ret == True:
119                         self.InterfaceState = True
120                 else:
121                         self.InterfaceState = False
122
123         def restartNetwork(self):
124                 iNetwork.restartNetwork()
125                 self.checkNetwork()
126