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