update to support Networksetup update ;-)
[enigma2-plugins.git] / networkwizard / src / NetworkWizard.py
1 # for localized messages
2 from __init__ import _
3
4 from Screens.Wizard import wizardManager, WizardSummary
5 from Components.Pixmap import Pixmap, MovingPixmap, MultiPixmap
6 from Components.config import config, ConfigBoolean, configfile, ConfigYesNo, NoSave, ConfigSubsection, ConfigText, getConfigListEntry, ConfigSelection
7 from Components.Network import iNetwork
8 from Components.Label import Label
9 from Components.MenuList import MenuList
10 from Components.PluginComponent import plugins
11 from Plugins.Plugin import PluginDescriptor
12 from Tools.Directories import resolveFilename, SCOPE_PLUGINS, SCOPE_SKIN_IMAGE
13 import time, os, re
14 from WizardPluginLanguage import WizardPluginLanguage
15 from Screens.Rc import Rc
16
17 config.misc.firstrun = ConfigBoolean(default = True)
18 list = []
19 list.append(_("WEP"))
20 list.append(_("WPA"))
21 list.append(_("WPA2"))
22
23 config.plugins.wlan = ConfigSubsection()
24 config.plugins.wlan.essidscan = NoSave(ConfigYesNo(default = True))
25 config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False))
26 config.plugins.wlan.encryption = ConfigSubsection()
27 config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = False))
28 config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = _("WPA")))
29 config.plugins.wlan.encryption.psk = NoSave(ConfigText(default = "mysecurewlan", fixed_size = False))
30
31 class NetworkWizard(WizardPluginLanguage, Rc):
32         skin = """
33                 <screen position="0,0" size="720,576" title="Welcome..." flags="wfNoBorder" >
34                         <widget name="text" position="153,50" size="340,300" font="Regular;22" />
35                         <widget source="list" render="Listbox" position="53,310" size="440,220" scrollbarMode="showOnDemand" >
36                                 <convert type="StringList" />
37                         </widget>
38                         <widget name="config" position="53,310" zPosition="1" size="440,220" transparent="1" scrollbarMode="showOnDemand" />
39                         <ePixmap pixmap="skin_default/buttons/button_red.png" position="40,225" zPosition="0" size="15,16" transparent="1" alphatest="on" />
40                         <widget name="languagetext" position="55,225" size="95,30" font="Regular;18" />
41                         <widget name="wizard" pixmap="skin_default/wizard.png" position="40,50" zPosition="10" size="110,174" alphatest="on" />
42                         <widget name="rc" pixmaps="skin_default/rc.png,skin_default/rcold.png" position="500,50" zPosition="10" size="154,500" alphatest="on" />
43                         <widget name="arrowdown" pixmap="skin_default/arrowdown.png" position="-100,-100" zPosition="11" size="37,70" alphatest="on" />
44                         <widget name="arrowdown2" pixmap="skin_default/arrowdown.png" position="-100,-100" zPosition="11" size="37,70" alphatest="on" />
45                         <widget name="arrowup" pixmap="skin_default/arrowup.png" position="-100,-100" zPosition="11" size="37,70" alphatest="on" />
46                         <widget name="arrowup2" pixmap="skin_default/arrowup.png" position="-100,-100" zPosition="11" size="37,70" alphatest="on" />
47                 </screen>"""
48         def __init__(self, session):
49                 self.xmlfile = resolveFilename(SCOPE_PLUGINS, "SystemPlugins/NetworkWizard/networkwizard.xml")
50                 WizardPluginLanguage.__init__(self, session, showSteps = False, showStepSlider = False)
51                 Rc.__init__(self)
52                 self.session = session
53                 self["wizard"] = Pixmap()
54                 
55                 self.InterfaceState = None
56                 self.isInterfaceUp = None
57                 self.WlanPluginInstalled = None
58                 self.ap = None
59                 self.selectedInterface = None
60                 self.NextStep = None
61                 
62                 self.isWlanPluginInstalled()
63
64         def listInterfaces(self):
65                 list = [(iNetwork.getFriendlyAdapterName(x),x) for x in iNetwork.getAdapterList()]
66                 list.append((_("Exit wizard and configure later manually"), "end"))
67                 return list
68
69         def InterfaceSelectionMade(self, index):
70                 self.selectedInterface = index
71                 self.InterfaceSelect(index)
72
73         def InterfaceSelect(self, index):
74                 if index == 'end':
75                         self.NextStep = 'end'
76                 elif index == 'eth0':
77                         self.NextStep = 'nwconfig'
78                 else:
79                         self.NextStep = 'scanwlan'
80
81         def InterfaceSelectionMoved(self):
82                 self.InterfaceSelect(self.selection)
83                 
84         def checkInterface(self,iface):
85                 self.Adapterlist = iNetwork.getAdapterList()
86                 if len(self.Adapterlist) == 0:
87                         #Reset Network to defaults if network broken
88                         iNetwork.resetNetworkConfig('lan')
89                 if iface == 'eth0':
90                         iNetwork.resetNetworkConfig('lan')
91                         self.InterfaceUp('eth0')
92                 if iface == 'wlan0':
93                         iNetwork.resetNetworkConfig('wlan')
94                         self.InterfaceUp('wlan0')
95                 if iface == 'ath0':
96                         iNetwork.resetNetworkConfig('wlan-mpci')
97                         self.InterfaceUp('ath0')
98         def InterfaceUp(self,iface):
99                 iNetwork.getInterfaces()
100                 if iNetwork.getAdapterAttribute(iface, 'up') is True:
101                         self.isInterfaceUp = True
102                 else:
103                         self.isInterfaceUp = False
104                         
105         def markDone(self):
106                 pass
107
108         def listModes(self):
109                 list = []
110                 try:
111                         from Plugins.SystemPlugins.WirelessLan.Wlan import Wlan
112                 except ImportError:
113                         list.append( ( _("No Networks found"),_("unavailable") ) )
114                         return list
115                 else:   
116                         self.w = Wlan(self.selectedInterface)
117                         aps = self.w.getNetworkList()
118                         if aps is not None:
119                                 print "[Wlan.py] got Accespoints!"
120                                 for ap in aps:
121                                         a = aps[ap]
122                                         if a['active']:
123                                                 if a['essid'] == "":
124                                                         a['essid'] = a['bssid']
125                                                 list.append( (a['essid'], a['essid']) )
126                         else:
127                                 list.append( ( _("No Networks found"),_("unavailable") ) )
128                         list.sort(key = lambda x: x[0])
129                         return list
130
131         def modeSelectionMade(self, index):
132                 self.ap = index
133                 self.modeSelect(index)
134                 
135         def modeSelectionMoved(self):
136                 self.modeSelect(self.selection)
137                 
138         def modeSelect(self, mode):
139                 print "ModeSelected:", mode
140
141         def saveAccessPoint(self, mode):
142                 from Plugins.SystemPlugins.WirelessLan.Wlan import wpaSupplicant,Wlan
143                 self.ws = wpaSupplicant()
144                 wsconfig = self.ws.loadConfig()
145                 config.plugins.wlan.essid.value = str(mode)
146                 config.plugins.wlan.essid.save()
147                 self.ws.writeConfig()
148
149         def checkNetwork(self):
150                 ret = iNetwork.checkNetworkState()
151                 if ret == True:
152                         self.InterfaceState = True
153                 else:
154                         self.InterfaceState = False
155
156         def restartNetwork(self):
157                 iNetwork.restartNetwork()
158                 self.checkNetwork()
159         
160
161         def isWlanPluginInstalled(self):                
162                 try:
163                         from Plugins.SystemPlugins.WirelessLan.Wlan import Wlan
164                 except ImportError:
165                         self.WlanPluginInstalled = False
166                 else:
167                         self.WlanPluginInstalled = True
168