Fix crash when no WirelessLan Plugin installed
[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 Tools.Directories import resolveFilename, SCOPE_PLUGINS, SCOPE_SKIN_IMAGE
11 import time, os, re
12
13 config.misc.firstrun = ConfigBoolean(default = True)
14 list = []
15 list.append(_("WEP"))
16 list.append(_("WPA"))
17 list.append(_("WPA2"))
18
19 config.plugins.wlan = ConfigSubsection()
20 config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False))
21
22 config.plugins.wlan.encryption = ConfigSubsection()
23 config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = False))
24 config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = _("WPA")))
25 config.plugins.wlan.encryption.psk = NoSave(ConfigText(default = "mysecurewlan", fixed_size = False))
26
27 class NetworkWizard(WizardLanguage):
28         skin = """
29                 <screen position="0,0" size="720,576" title="Welcome..." flags="wfNoBorder" >
30                         <widget name="text" position="153,50" size="340,300" font="Regular;22" />
31                         <widget source="list" render="Listbox" position="53,310" size="440,220" scrollbarMode="showOnDemand" >
32                                 <convert type="StringList" />
33                         </widget>
34                         <widget name="config" position="53,310" zPosition="1" size="440,220" transparent="1" scrollbarMode="showOnDemand" />
35                         <widget name="wizard" pixmap="skin_default/wizard.png" position="40,50" zPosition="10" size="110,174" transparent="1" alphatest="on"/>
36                         <ePixmap pixmap="skin_default/buttons/button_red.png" position="40,225" zPosition="0" size="15,16" transparent="1" alphatest="on" />
37                         <widget name="languagetext" position="55,225" size="95,30" font="Regular;18" />
38                         <widget name="rc" pixmap="skin_default/rc.png" position="500,600" zPosition="10" size="154,475" transparent="1" alphatest="on"/>
39                         <widget name="arrowdown" pixmap="skin_default/arrowdown.png" position="0,0" zPosition="11" size="37,70" transparent="1" alphatest="on"/>
40                         <widget name="arrowup" pixmap="skin_default/arrowup.png" position="-100,-100" zPosition="11" size="37,70" transparent="1" alphatest="on"/>
41                         <widget name="arrowup2" pixmap="skin_default/arrowup.png" position="-100,-100" zPosition="11" size="37,70" transparent="1" alphatest="on"/>
42                         <widget name="arrowup3" pixmap="skin_default/arrowup.png" position="-100,-100" zPosition="11" size="37,70" transparent="1" alphatest="on"/>
43                 </screen>"""
44         def __init__(self, session):
45                 self.xmlfile = resolveFilename(SCOPE_PLUGINS, "SystemPlugins/NetworkWizard/networkwizard.xml")
46                 
47                 WizardLanguage.__init__(self, session, showSteps = False, showStepSlider = False)
48                 self.session = session
49                 self["wizard"] = Pixmap()
50                 self["rc"] = MovingPixmap()
51                 self["arrowdown"] = MovingPixmap()
52                 self["arrowup"] = MovingPixmap()
53                 self["arrowup2"] = MovingPixmap()
54                 self["arrowup3"] = MovingPixmap()
55                 
56                 self.InterfaceState = None
57                 self.isInterfaceUp = None
58                 self.InterfaceAvailable = None
59                 self.WlanPluginInstalled = None
60                 self.ap = None
61                 
62                 self.isInterfaceAvailable()
63                 self.isWlanPluginInstalled()
64
65         def checkInterface(self,iface):
66                 self.Adapterlist = iNetwork.getAdapterList()
67                 if len(self.Adapterlist) == 0:
68                         #Reset Network to defaults if network broken
69                         iNetwork.resetNetworkConfig('lan')
70                 if iface == 'eth0':
71                         iNetwork.resetNetworkConfig('lan')
72                         self.InterfaceUp('eth0')
73                 if iface == 'wlan0':
74                         iNetwork.resetNetworkConfig('wlan')
75                         self.InterfaceUp('wlan0')
76
77         def InterfaceUp(self,iface):
78                 iNetwork.getInterfaces()
79                 if iNetwork.getAdapterAttribute(iface, 'up') is True:
80                         self.isInterfaceUp = True
81                 else:
82                         self.isInterfaceUp = False
83                         
84         def markDone(self):
85                 pass
86
87         def listModes(self):
88                 list = []
89                 try:
90                         from Plugins.SystemPlugins.WirelessLan.Wlan import Wlan
91                 except ImportError:
92                         list.append( ( _("No Networks found"),_("unavailable") ) )
93                         return list
94                 else:   
95                         self.w = Wlan('wlan0')
96                         aps = self.w.getNetworkList()
97                         if aps is not None:
98                                 print "[Wlan.py] got Accespoints!"
99                                 for ap in aps:
100                                         a = aps[ap]
101                                         if a['active']:
102                                                 if a['essid'] == "":
103                                                         a['essid'] = a['bssid']
104                                                 list.append( (a['essid'], a['essid']) )         
105                         list.sort(key = lambda x: x[0])
106                         return list
107
108
109         def modeSelectionMade(self, index):
110                 print "modeSelectionMade:", index
111                 self.ap = index
112                 self.modeSelect(index)
113                 
114         def modeSelectionMoved(self):
115                 print "mode selection moved:", self.selection
116                 self.modeSelect(self.selection)
117                 
118         def modeSelect(self, mode):
119                 print "ModeSelected:", mode
120
121         def saveAccessPoint(self, mode):
122                 config.plugins.wlan.essid.value = str(mode)
123                 config.plugins.wlan.essid.save()
124                 config.plugins.wlan.encryption.enabled.value = False
125                 config.plugins.wlan.encryption.enabled.save()           
126
127         def checkNetwork(self):
128                 ret = iNetwork.checkNetworkState()
129                 if ret == True:
130                         self.InterfaceState = True
131                 else:
132                         self.InterfaceState = False
133
134         def restartNetwork(self):
135                 iNetwork.restartNetwork()
136                 self.checkNetwork()
137         
138         def isInterfaceAvailable(self):
139                 ret = iNetwork.checkforInterface('wlan0')
140                 if ret == True:
141                         self.InterfaceAvailable = True
142                 else:
143                         self.InterfaceAvailable = False
144                         
145         def isWlanPluginInstalled(self):                
146                 try:
147                         from Plugins.SystemPlugins.WirelessLan.Wlan import Wlan
148                 except ImportError:
149                         self.WlanPluginInstalled = False
150                 else:
151                         self.WlanPluginInstalled = True
152