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