follow async changes(needs enigma2 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 Screens.MessageBox import MessageBox
6 from Components.Pixmap import Pixmap, MovingPixmap, MultiPixmap
7 from Components.config import config, ConfigBoolean, configfile, ConfigYesNo, NoSave, ConfigSubsection, ConfigText, getConfigListEntry, ConfigSelection, ConfigPassword
8 from Components.Network import iNetwork
9 from Components.Label import Label
10 from Components.MenuList import MenuList
11 from Components.PluginComponent import plugins
12 from Plugins.Plugin import PluginDescriptor
13 from Tools.Directories import resolveFilename, SCOPE_PLUGINS, SCOPE_SKIN_IMAGE
14 import time, os, re
15 from WizardPluginLanguage import WizardPluginLanguage
16 from Screens.Rc import Rc
17
18 config.misc.firstrun = ConfigBoolean(default = True)
19 list = []
20 list.append("WEP")
21 list.append("WPA")
22 list.append("WPA2")
23 list.append("WPA/WPA2")
24
25 weplist = []
26 weplist.append("ASCII")
27 weplist.append("HEX")
28
29 config.plugins.wlan = ConfigSubsection()
30 config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False))
31 config.plugins.wlan.hiddenessid = NoSave(ConfigText(default = "home", fixed_size = False))
32
33 config.plugins.wlan.encryption = ConfigSubsection()
34 config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = False))
35 config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = "WPA/WPA2" ))
36 config.plugins.wlan.encryption.wepkeytype = NoSave(ConfigSelection(weplist, default = "ASCII"))
37 config.plugins.wlan.encryption.psk = NoSave(ConfigPassword(default = "mysecurewlan", fixed_size = False))
38
39 class NetworkWizard(WizardPluginLanguage, Rc):
40         skin = """
41                 <screen position="0,0" size="720,576" title="Welcome..." flags="wfNoBorder" >
42                         <widget name="text" position="153,40" size="340,330" font="Regular;22" />
43                         <widget source="list" render="Listbox" position="53,340" size="440,180" scrollbarMode="showOnDemand" >
44                                 <convert type="StringList" />
45                         </widget>
46                         <widget name="config" position="53,340" zPosition="1" size="440,180" transparent="1" scrollbarMode="showOnDemand" />
47                         <ePixmap pixmap="skin_default/buttons/button_red.png" position="40,225" zPosition="0" size="15,16" transparent="1" alphatest="on" />
48                         <widget name="languagetext" position="55,225" size="95,30" font="Regular;18" />
49                         <widget name="wizard" pixmap="skin_default/wizard.png" position="40,50" zPosition="10" size="110,174" alphatest="on" />
50                         <widget name="rc" pixmaps="skin_default/rc.png,skin_default/rcold.png" position="500,50" zPosition="10" size="154,500" alphatest="on" />
51                         <widget name="arrowdown" pixmap="skin_default/arrowdown.png" position="-100,-100" zPosition="11" size="37,70" alphatest="on" />
52                         <widget name="arrowdown2" pixmap="skin_default/arrowdown.png" position="-100,-100" zPosition="11" size="37,70" alphatest="on" />
53                         <widget name="arrowup" pixmap="skin_default/arrowup.png" position="-100,-100" zPosition="11" size="37,70" alphatest="on" />
54                         <widget name="arrowup2" pixmap="skin_default/arrowup.png" position="-100,-100" zPosition="11" size="37,70" alphatest="on" />
55                 </screen>"""
56         def __init__(self, session):
57                 self.xmlfile = resolveFilename(SCOPE_PLUGINS, "SystemPlugins/NetworkWizard/networkwizard.xml")
58                 WizardPluginLanguage.__init__(self, session, showSteps = False, showStepSlider = False)
59                 Rc.__init__(self)
60                 self.session = session
61                 self["wizard"] = Pixmap()
62                 
63                 self.InterfaceState = None
64                 self.isInterfaceUp = None
65                 self.WlanPluginInstalled = None
66                 self.ap = None
67                 self.selectedInterface = None
68                 self.NextStep = None
69                 self.myref = None
70                 self.checkRef = None
71                 self.AdapterRef = None
72                 self.WlanList = None
73                 self.isWlanPluginInstalled()
74
75         def listInterfaces(self):
76                 list = [(iNetwork.getFriendlyAdapterName(x),x) for x in iNetwork.getAdapterList()]
77                 list.append((_("Exit network wizard"), "end"))
78                 return list
79
80         def InterfaceSelectionMade(self, index):
81                 self.selectedInterface = index
82                 self.InterfaceSelect(index)
83
84         def InterfaceSelect(self, index):
85                 if index == 'end':
86                         self.NextStep = 'end'
87                 elif index == 'eth0':
88                         self.NextStep = 'nwconfig'
89                 else:
90                         self.NextStep = 'scanwlan'
91
92         def InterfaceSelectionMoved(self):
93                 self.InterfaceSelect(self.selection)
94                 
95         def checkInterface(self,iface):
96                 self.Adapterlist = iNetwork.getAdapterList()
97                 if self.NextStep is not 'end':
98                         if len(self.Adapterlist) == 0:
99                                 #Reset Network to defaults if network broken
100                                 iNetwork.resetNetworkConfig('lan', self.checkInterfaceCB)
101                                 self.myref = self.session.openWithCallback(self.resetfinishedCB, MessageBox, _("Please wait while we prepare your network interfaces..."), type = MessageBox.TYPE_INFO, enable_input = False)
102                         if iface == 'eth0':
103                                 iNetwork.resetNetworkConfig('lan',self.checkInterfaceCB)
104                                 self.myref = self.session.openWithCallback(self.resetfinishedCB, MessageBox, _("Please wait while we prepare your network interfaces..."), type = MessageBox.TYPE_INFO, enable_input = False)
105                         elif iface == 'wlan0':
106                                 iNetwork.resetNetworkConfig('wlan',self.checkInterfaceCB)
107                                 self.myref = self.session.openWithCallback(self.resetfinishedCB, MessageBox, _("Please wait while we prepare your network interfaces..."), type = MessageBox.TYPE_INFO, enable_input = False)
108                         elif iface == 'ath0':
109                                 iNetwork.resetNetworkConfig('wlan-mpci',self.checkInterfaceCB)
110                                 self.myref = self.session.openWithCallback(self.resetfinishedCB, MessageBox, _("Please wait while we prepare your network interfaces..."), type = MessageBox.TYPE_INFO, enable_input = False)
111                 else:
112                         self.resetfinishedCB(False)
113                         
114         def resetfinishedCB(self,data):
115                 if data is True:
116                         self.currStep = self.getStepWithID(self.NextStep)
117                         self.afterAsyncCode()
118                 else:
119                         self.currStep = self.getStepWithID(self.NextStep)
120                         self.afterAsyncCode()
121
122         def checkInterfaceCB(self,callback,iface):
123                 if callback is not None:
124                         if callback is True:
125                                 iNetwork.getInterfaces(self.getInterfacesDataAvail)
126                                 
127
128         def getInterfacesDataAvail(self, data):
129                 if data is True:
130                         if iNetwork.getAdapterAttribute(self.selectedInterface, 'up') is True:
131                                 self.isInterfaceUp = True
132                         else:
133                                 self.isInterfaceUp = False
134                         self.myref.close(True)
135
136         def AdapterSetupEnd(self, iface):
137                 if iNetwork.getAdapterAttribute(iface, "dhcp") is True:
138                         iNetwork.checkNetworkState(self.AdapterSetupEndFinished)
139                         self.AdapterRef = self.session.openWithCallback(self.AdapterSetupEndCB, MessageBox, _("Please wait while we test your network..."), type = MessageBox.TYPE_INFO, enable_input = False)
140
141                 else:
142                         self.currStep = self.getStepWithID("confdns")
143                         self.afterAsyncCode()
144
145         def AdapterSetupEndCB(self,data):
146                 if data is True:
147                         self.currStep = self.getStepWithID("checklanstatusend")
148                         self.afterAsyncCode()
149
150         def AdapterSetupEndFinished(self,data):
151                 if data <= 2:
152                         self.InterfaceState = True
153                 else:
154                         self.InterfaceState = False
155                 self.AdapterRef.close(True)
156                         
157         def checkNetwork(self):
158                 iNetwork.checkNetworkState(self.checkNetworkStateFinished)
159                 self.checkRef = self.session.openWithCallback(self.checkNetworkCB, MessageBox, _("Please wait while we test your network..."), type = MessageBox.TYPE_INFO, enable_input = False)
160
161         def checkNetworkCB(self,data):
162                 if data is True:
163                         self.currStep = self.getStepWithID("checklanstatusend")
164                         self.afterAsyncCode()
165
166         def checkNetworkStateFinished(self,data):
167                 if data <= 2:
168                         self.InterfaceState = True
169                 else:
170                         self.InterfaceState = False
171                 self.checkRef.close(True)
172         
173         def markDone(self):
174                 pass
175
176         def listModes(self):
177                 list = []
178                 self.WlanList = []
179                 try:
180                         from Plugins.SystemPlugins.WirelessLan.Wlan import Wlan
181                 except ImportError:
182                         list.append( ( _("No networks found"),_("unavailable") ) )
183                         self.WlanList.append(_("No networks found"))
184                         return list
185                 else:   
186                         self.w = Wlan(self.selectedInterface)
187                         aps = self.w.getNetworkList()
188                         if aps is not None:
189                                 print "[NetworkWizard.py] got Accespoints!"
190                                 for ap in aps:
191                                         a = aps[ap]
192                                         if a['active']:
193                                                 if a['essid'] == "":
194                                                         a['essid'] = a['bssid']
195                                                 list.append( (a['essid'], a['essid']) )
196                                                 self.WlanList.append(a['essid'])        
197                         if "hidden..." not in list:
198                                 list.append( ( _("enter hidden network SSID"),_("hidden...") ) )
199                                 self.WlanList.append(_("hidden..."))    
200                         return list
201
202         def modeSelectionMade(self, index):
203                 self.modeSelect(index)
204                 
205         def modeSelectionMoved(self):
206                 self.modeSelect(self.selection)
207                 
208         def modeSelect(self, mode):
209                 self.ap = mode
210                 print "ModeSelected:", mode
211
212         def restartNetwork(self):
213                 iNetwork.restartNetwork()
214                 self.checkNetwork()
215         
216         def isWlanPluginInstalled(self):                
217                 try:
218                         from Plugins.SystemPlugins.WirelessLan.Wlan import Wlan
219                 except ImportError:
220                         self.WlanPluginInstalled = False
221                 else:
222                         self.WlanPluginInstalled = True
223