fix file permissions of non-executable files
[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                 print "iNetwork.configuredNetworkAdapters",iNetwork.configuredNetworkAdapters
98                 print "len - iNetwork.configuredNetworkAdapters",len(iNetwork.configuredNetworkAdapters)
99                 if self.NextStep is not 'end':
100                         if len(self.Adapterlist) == 0:
101                                 #Reset Network to defaults if network broken
102                                 iNetwork.resetNetworkConfig('lan', self.checkInterfaceCB)
103                                 self.myref = self.session.openWithCallback(self.resetfinishedCB, MessageBox, _("Please wait while we prepare your network interfaces..."), type = MessageBox.TYPE_INFO, enable_input = False)
104                         if iface == 'eth0':
105                                 if iface in iNetwork.configuredNetworkAdapters and len(iNetwork.configuredNetworkAdapters) == 1:
106                                         if iNetwork.getAdapterAttribute(iface, 'up') is True:
107                                                 self.isInterfaceUp = True
108                                         else:
109                                                 self.isInterfaceUp = False
110                                         self.resetfinishedCB(False)
111                                 else:
112                                         iNetwork.resetNetworkConfig('lan',self.checkInterfaceCB)
113                                         self.myref = self.session.openWithCallback(self.resetfinishedCB, MessageBox, _("Please wait while we prepare your network interfaces..."), type = MessageBox.TYPE_INFO, enable_input = False)
114                         elif iface == 'wlan0':
115                                 if iface in iNetwork.configuredNetworkAdapters and len(iNetwork.configuredNetworkAdapters) == 1:
116                                         if iNetwork.getAdapterAttribute(iface, 'up') is True:
117                                                 self.isInterfaceUp = True
118                                         else:
119                                                 self.isInterfaceUp = False
120                                         self.resetfinishedCB(False)
121                                 else:
122                                         iNetwork.resetNetworkConfig('wlan',self.checkInterfaceCB)
123                                         self.myref = self.session.openWithCallback(self.resetfinishedCB, MessageBox, _("Please wait while we prepare your network interfaces..."), type = MessageBox.TYPE_INFO, enable_input = False)
124                         elif iface == 'ath0':
125                                 if iface in iNetwork.configuredNetworkAdapters and len(iNetwork.configuredNetworkAdapters) == 1:
126                                         if iNetwork.getAdapterAttribute(iface, 'up') is True:
127                                                 self.isInterfaceUp = True
128                                         else:
129                                                 self.isInterfaceUp = False
130                                         self.resetfinishedCB(False)
131                                 else:
132                                         iNetwork.resetNetworkConfig('wlan-mpci',self.checkInterfaceCB)
133                                         self.myref = self.session.openWithCallback(self.resetfinishedCB, MessageBox, _("Please wait while we prepare your network interfaces..."), type = MessageBox.TYPE_INFO, enable_input = False)
134                 else:
135                         self.resetfinishedCB(False)
136                         
137         def resetfinishedCB(self,data):
138                 if data is True:
139                         self.currStep = self.getStepWithID(self.NextStep)
140                         self.afterAsyncCode()
141                 else:
142                         self.currStep = self.getStepWithID(self.NextStep)
143                         self.afterAsyncCode()
144
145         def checkInterfaceCB(self,callback,iface):
146                 if callback is not None:
147                         if callback is True:
148                                 iNetwork.getInterfaces(self.getInterfacesDataAvail)
149                                 
150
151         def getInterfacesDataAvail(self, data):
152                 if data is True:
153                         if iNetwork.getAdapterAttribute(self.selectedInterface, 'up') is True:
154                                 self.isInterfaceUp = True
155                         else:
156                                 self.isInterfaceUp = False
157                         self.myref.close(True)
158
159         def AdapterSetupEnd(self, iface):
160                 if iNetwork.getAdapterAttribute(iface, "dhcp") is True:
161                         iNetwork.checkNetworkState(self.AdapterSetupEndFinished)
162                         self.AdapterRef = self.session.openWithCallback(self.AdapterSetupEndCB, MessageBox, _("Please wait while we test your network..."), type = MessageBox.TYPE_INFO, enable_input = False)
163
164                 else:
165                         self.currStep = self.getStepWithID("confdns")
166                         self.afterAsyncCode()
167
168         def AdapterSetupEndCB(self,data):
169                 if data is True:
170                         self.currStep = self.getStepWithID("checklanstatusend")
171                         self.afterAsyncCode()
172
173         def AdapterSetupEndFinished(self,data):
174                 if data <= 2:
175                         self.InterfaceState = True
176                 else:
177                         self.InterfaceState = False
178                 self.AdapterRef.close(True)
179                         
180         def checkNetwork(self):
181                 iNetwork.checkNetworkState(self.checkNetworkStateFinished)
182                 self.checkRef = self.session.openWithCallback(self.checkNetworkCB, MessageBox, _("Please wait while we test your network..."), type = MessageBox.TYPE_INFO, enable_input = False)
183
184         def checkNetworkCB(self,data):
185                 if data is True:
186                         self.currStep = self.getStepWithID("checklanstatusend")
187                         self.afterAsyncCode()
188
189         def checkNetworkStateFinished(self,data):
190                 if data <= 2:
191                         self.InterfaceState = True
192                 else:
193                         self.InterfaceState = False
194                 self.checkRef.close(True)
195         
196         def markDone(self):
197                 pass
198
199         def listModes(self):
200                 list = []
201                 self.WlanList = []
202                 try:
203                         from Plugins.SystemPlugins.WirelessLan.Wlan import Wlan
204                 except ImportError:
205                         list.append( ( _("No networks found"),_("unavailable") ) )
206                         self.WlanList.append(_("No networks found"))
207                         return list
208                 else:   
209                         self.w = Wlan(self.selectedInterface)
210                         aps = self.w.getNetworkList()
211                         if aps is not None:
212                                 print "[NetworkWizard.py] got Accespoints!"
213                                 for ap in aps:
214                                         a = aps[ap]
215                                         if a['active']:
216                                                 if a['essid'] != "":
217                                                         #a['essid'] = a['bssid']
218                                                         list.append( (a['essid'], a['essid']) )
219                                                         self.WlanList.append(a['essid'])        
220                         if "hidden..." not in list:
221                                 list.append( ( _("enter hidden network SSID"),_("hidden...") ) )
222                                 self.WlanList.append(_("hidden..."))    
223                         return list
224
225         def modeSelectionMade(self, index):
226                 self.modeSelect(index)
227                 
228         def modeSelectionMoved(self):
229                 self.modeSelect(self.selection)
230                 
231         def modeSelect(self, mode):
232                 self.ap = mode
233                 print "ModeSelected:", mode
234
235         def restartNetwork(self):
236                 iNetwork.restartNetwork()
237                 self.checkNetwork()
238         
239         def isWlanPluginInstalled(self):                
240                 try:
241                         from Plugins.SystemPlugins.WirelessLan.Wlan import Wlan
242                 except ImportError:
243                         self.WlanPluginInstalled = False
244                 else:
245                         self.WlanPluginInstalled = True
246