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