* start wpa_supplicant via /etc/network/interfaces
[enigma2-plugins.git] / wirelesslan / src / plugin.py
1 from Screens.Screen import Screen
2 from Screens.MessageBox import MessageBox
3
4 from Components.ActionMap import ActionMap, NumberActionMap
5 from Components.Pixmap import Pixmap
6 from Components.Label import Label
7 from Components.GUIComponent import *
8 from Components.MenuList import MenuList
9 from Components.MultiContent import MultiContentEntryText
10
11
12 from Components.config import config, getConfigListEntry
13 from Components.ConfigList import ConfigList, ConfigListScreen
14 from Components.Network import Network
15
16 from Plugins.Plugin import PluginDescriptor
17
18 from Wlan import Wlan, WlanList, wpaSupplicant
19
20 plugin_path = "/usr/lib/enigma2/python/Plugins/SystemPlugins/WirelessLan"
21
22 class WlanSelection(Screen):
23         skin = """
24         <screen position="70,138" size="610,300" title="Choose a Wireless Network" >
25                 <widget name="list" position="10,10" size="580,200" scrollbarMode="showOnDemand" />
26                 
27                 <widget name="cancel" position="10,255" size="140,40" pixmap="~/key-red.png" zPosition="1" transparent="1" alphatest="on" />
28                 <widget name="select" position="160,255" size="140,40" pixmap="~/key-green.png" zPosition="1" transparent="1" alphatest="on" />
29                 <widget name="rescan" position="310,255" size="140,40" pixmap="~/key-yellow.png" zPosition="1" transparent="1" alphatest="on" />
30                 <widget name="skip" position="460,255" size="140,40" pixmap="~/key-blue.png" zPosition="1" transparent="1" alphatest="on" />
31                 
32                 <widget name="canceltext" position="10,255" size="140,40" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />          
33                 <widget name="selecttext" position="160,255" size="140,40" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1"  foregroundColor="#FFFFFF" />
34                 <widget name="rescantext" position="310,255" size="140,40" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1"  foregroundColor="#FFFFFF" />
35                 <widget name="skiptext" position="460,255" size="140,40" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
36         </screen>
37         """
38         def __init__(self, session, iface):
39         
40                 self.skin = WlanSelection.skin
41                 self.session = session
42                 Screen.__init__(self, session)
43                 
44                 self.list = []
45                                 
46                 self["list"] = WlanList(self.session, iface)
47                 self.skin_path = plugin_path
48                 
49                 self["cancel"] = Pixmap()
50                 self["select"] = Pixmap()
51                 self["rescan"] = Pixmap()
52                 self["skip"] = Pixmap()
53                 
54                 
55                 self["canceltext"] = Label(_("Cancel"))
56                 self["selecttext"] = Label(_("Select"))
57                 self["rescantext"] = Label(_("Rescan"))
58                 self["skiptext"] = Label(_("Skip"))
59                         
60                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "EPGSelectActions"],
61                 {
62                         "ok": self.select,
63                         "back": self.exit,
64 #                       "up": self.up,
65 #                       "down": self.down,
66                 }, -1)
67                 
68                 self["shortcuts"] = ActionMap(["ShortcutActions"],
69                 {
70                         "red": self.exit,
71                         "green": self.select,
72                         "yellow": self.rescan,
73                         "blue": self.skip,
74                 })
75         
76         def select(self):
77                 cur = self["list"].getCurrent()
78                 if cur:
79                         ret = (self.session, cur)
80                 else:
81                         ret = (self.session, None)
82                 self.close(ret)
83         
84         def rescan(self):
85                 self["list"].reload()
86         
87         def skip(self):
88                 self.close( (self.session, None) )
89         
90         def exit(self):
91                 self.close( (None ,) )
92
93 class WlanConfiguration(ConfigListScreen, Screen):
94         skin = """
95                 <screen position="76,138" size="600,300" title="Wireless Network Configuration" >
96                         <widget name="interface" position="10,10" size="580,30" font="Regular;24" valign="center" />
97                         <widget name="config" position="10,60" size="580,150" scrollbarMode="showOnDemand" />
98                         <widget name="introduction" position="100,260" size="400,30" font="Regular;23" valign="center" halign="center" />       
99                 </screen>
100         """
101         
102         def __init__(self, session, essid = None, encrypted = False, iface = "wlan0"):
103                 
104                 Screen.__init__(self, session)          
105                 self.skin = WlanConfiguration.skin
106                 
107                 self.iface = iface
108                 self.list = []
109                 self.ws = wpaSupplicant()
110                 
111                 self["introduction"] = Label(_("Press OK to activate the settings."))
112                 self["interface"] = Label(_("Interface: ")+self.iface)
113                 
114                 if essid is None:
115                         self.ws.loadConfig()
116                 
117                 else:
118                         config.plugins.wlan.essid.value = essid
119                         config.plugins.wlan.encryption.enabled.value = True
120                         
121                 self["actions"] = ActionMap(["SetupActions"],
122                 {
123                         "ok": self.ok,
124                         "cancel": self.cancel,
125                 }, -2)
126                 
127                 ConfigListScreen.__init__(self, self.list)
128                 self.createSetup()
129         
130         def createSetup(self):
131
132                 self.list = [ ]
133                                                 
134                 self.list.append(getConfigListEntry(_("Network SSID"), config.plugins.wlan.essid))
135                 self.list.append(getConfigListEntry(_("Encryption"), config.plugins.wlan.encryption.enabled))
136                 
137                 if config.plugins.wlan.encryption.enabled.value:
138                         self.list.append(getConfigListEntry(_("Encryption Type"), config.plugins.wlan.encryption.type))
139                         self.list.append(getConfigListEntry(_("Encryption Key"), config.plugins.wlan.encryption.psk))
140                 
141                 self["config"].list = self.list
142                 self["config"].l.setList(self.list)
143         
144         def keyLeft(self):
145                 ConfigListScreen.keyLeft(self)
146                 self.createSetup()
147
148         def keyRight(self):
149                 ConfigListScreen.keyRight(self)
150                 self.createSetup()
151
152         def ok(self):
153                 self.ws.writeConfig()
154                 self.ws.restart(self.iface)
155                 self.close()
156
157         def cancel(self):
158                 self.close()
159
160 def EntryChosen(parms):
161         if parms[0]:
162                 session = parms[0]
163                 if parms[1] is not None:
164                         val = parms[1]
165                         essid = val[0]
166                         encrypted = val[2]
167                         iface = val[3]
168                         session.open(WlanConfiguration, essid, encrypted, iface)
169                 else:
170                         session.open(WlanConfiguration)
171
172 def WlanSelectionMain(session, iface):
173         session.openWithCallback(EntryChosen, WlanSelection, iface)
174
175 def WlanConfigurationMain(session, **kwargs):
176         session.open(WlanConfiguration)
177
178 def callFunction(iface):
179         w = Wlan()
180
181         if iface in w.getWirelessInterfaces():
182                 return WlanSelectionMain        
183         else:
184                 return None
185
186 def configStrings(iface):
187         return "pre-up /usr/sbin/wpa_supplicant -B -i"+iface+" -c/etc/wpa_supplicant.conf"
188         
189 def Plugins(**kwargs):
190         return PluginDescriptor(name=_("Wireless LAN"), description=_("Connect to a Wireless Network"), where = PluginDescriptor.WHERE_NETWORKSETUP, fnc={"ifaceSupported": callFunction, "configStrings": configStrings, "menuEntryName": "Wlan Configuartion Utility"})
191