-support new zydas driver
[enigma2-plugins.git] / wirelesslan / src / plugin.py
1 # for localized messages
2 from __init__ import _
3
4 from enigma import eTimer
5 from Screens.Screen import Screen
6 from Components.ActionMap import ActionMap, NumberActionMap
7 from Components.Pixmap import Pixmap
8 from Components.Label import Label
9 from Components.MenuList import MenuList
10 from Components.config import config, getConfigListEntry, ConfigYesNo, NoSave, ConfigSubsection, ConfigText, ConfigSelection
11 from Components.ConfigList import ConfigListScreen
12 from Components.Network import Network, iNetwork
13 from Plugins.Plugin import PluginDescriptor
14 from os import system, path as os_path, listdir
15 from Wlan import Wlan, WlanList, wpaSupplicant
16
17 plugin_path = "/usr/lib/enigma2/python/Plugins/SystemPlugins/WirelessLan"
18
19 list = []
20 list.append(_("WEP"))
21 list.append(_("WPA"))
22 list.append(_("WPA2"))
23
24 config.plugins.wlan = ConfigSubsection()
25 config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False))
26
27 config.plugins.wlan.encryption = ConfigSubsection()
28 config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = False))
29 config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = _("WPA")))
30 config.plugins.wlan.encryption.psk = NoSave(ConfigText(default = "mysecurewlan", fixed_size = False))
31
32
33 class WlanStatus(Screen):
34         skin = """
35         <screen position="90,150" size="550,300" title="Wireless Network State" >
36                 <widget name="LabelBSSID" position="10,10" size="150,25" valign="left" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
37                 <widget name="LabelESSID" position="10,38" size="150,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
38                 <widget name="LabelQuality" position="10,66" size="150,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
39                 <widget name="LabelSignal" position="10,94" size="150,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
40                 <widget name="LabelBitrate" position="10,122" size="150,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
41                 <widget name="LabelChannel" position="10,150" size="150,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
42                 
43                 <widget name="BSSID" position="320,10" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
44                 <widget name="ESSID" position="320,38" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
45                 <widget name="quality" position="320,66" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
46                 <widget name="signal" position="320,94" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
47                 <widget name="bitrate" position="320,122" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
48                 <widget name="channel" position="320,150" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
49                 
50                 <widget name="BottomBG" pixmap="skin_default/bottombar.png" position="5,210" size="540,120" zPosition="1" transparent="1" alphatest="on" />
51                 <widget name="IFtext" position="20,225" size="100,21" zPosition="10" font="Regular;19" transparent="1" />
52                 <widget name="IF" position="110,225" size="300,21" zPosition="10" font="Regular;19" transparent="1" />
53                 <widget name="Statustext" position="20,250" size="115,21" zPosition="10" font="Regular;19" transparent="1"/>
54                 <widget name="statuspic_inactive" pixmap="skin_default/buttons/button_green_off.png" position="120,250" zPosition="10" size="15,16" transparent="1" alphatest="on"/>
55                 <widget name="statuspic_active" pixmap="skin_default/buttons/button_green.png" position="120,250" zPosition="10" size="15,16" transparent="1" alphatest="on"/>
56                 <widget name="ButtonRedtext" position="430,225" size="120,21" zPosition="10" font="Regular;21" transparent="1" />
57                 <widget name="ButtonRed" pixmap="skin_default/buttons/button_red.png" position="410,225" zPosition="10" size="15,16" transparent="1" alphatest="on" />
58
59         </screen>
60         """
61         
62         
63         def __init__(self, session, iface):
64                 
65                 Screen.__init__(self, session)
66                 
67                 self.session = session
68                 self.iface = iface
69                 self.skin = WlanStatus.skin
70                 
71                 self.timer = eTimer()
72                 self.timer.timeout.get().append(self.resetList) 
73                 self.onShown.append(lambda: self.timer.start(5000))
74                                                     
75                 self["LabelBSSID"] = Label(_('Accesspoint:'))
76                 self["LabelESSID"] = Label(_('SSID:'))
77                 self["LabelQuality"] = Label(_('Link Quality:'))
78                 self["LabelSignal"] = Label(_('Signal Strength:'))
79                 self["LabelBitrate"] = Label(_('Bitrate:'))
80                 self["LabelChannel"] = Label(_('Channel:'))
81                         
82                 self["BSSID"] = Label()
83                 self["ESSID"] = Label()
84                 self["quality"] = Label()
85                 self["signal"] = Label()
86                 self["bitrate"] = Label()
87                 self["channel"] = Label()
88
89                 self["IFtext"] = Label()
90                 self["IF"] = Label()
91                 self["Statustext"] = Label()
92                 self["statuspic_active"] = Pixmap()
93                 self["statuspic_active"].hide()
94                 self["statuspic_inactive"] = Pixmap()
95                 self["statuspic_inactive"].hide()
96                 self["BottomBG"] = Pixmap()
97                 self["ButtonRed"] = Pixmap()
98                 self["ButtonRedtext"] = Label(_("Close"))
99
100                 self.resetList()
101                 self.updateStatusbar()
102                 
103                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "EPGSelectActions", "ShortcutActions"],
104                 {
105                         "ok": self.exit,
106                         "back": self.exit,
107                         "red": self.exit,
108                 }, -1)
109                 self.onLayoutFinish.append(self.layoutFinished)
110                 
111         def layoutFinished(self):
112                 self.setTitle(_("Wireless Network State"))
113                 
114         def resetList(self):
115                 w = Wlan(self.iface)
116                 stats = w.getStatus()
117                 if stats['BSSID'] == "00:00:00:00:00:00":
118                         stats['BSSID'] = _("No Connection!")
119                 self["BSSID"].setText(stats['BSSID'])
120                 self["ESSID"].setText(stats['ESSID'])
121                 self["quality"].setText(stats['quality']+"%")
122                 self["signal"].setText(stats['signal']+"%")
123                 self["bitrate"].setText(stats['bitrate'])
124                 self["channel"].setText(stats['channel'])
125                 
126         
127         def exit(self):
128                 self.timer.stop()
129                 self.close()    
130
131         def updateStatusbar(self):
132                 self["IFtext"].setText(_("Network:"))
133                 self["IF"].setText(iNetwork.getFriendlyAdapterName(self.iface))
134                 self["Statustext"].setText(_("Link:"))
135                 w = Wlan(self.iface)
136                 stats = w.getStatus()
137                 if stats['BSSID'] == "00:00:00:00:00:00":
138                         self["statuspic_active"].hide()
139                         self["statuspic_inactive"].show()
140                 else:
141                         self["statuspic_active"].show()
142                         self["statuspic_inactive"].hide()
143
144
145 class WlanScan(Screen):
146         skin = """
147         <screen position="70,90" size="600,400" title="Choose a Wireless Network" >
148                 <widget name="info" position="10,10" size="580,30" font="Regular;24" transparent="1" foregroundColor="#FFFFFF" />
149                 <widget name="list" position="10,50" size="580,240" scrollbarMode="showOnDemand" />
150
151                 <ePixmap pixmap="skin_default/bottombar.png" position="30,310" size="540,120" zPosition="1" transparent="1" alphatest="on" />
152                 <ePixmap pixmap="skin_default/buttons/button_red.png" position="430,325" zPosition="10" size="15,16" transparent="1" alphatest="on" />
153                 <widget name="canceltext" position="450,325" size="120,21" zPosition="10" font="Regular;21" transparent="1" />
154                 <ePixmap pixmap="skin_default/buttons/button_green.png" position="50,325" zPosition="10" size="15,16" transparent="1" alphatest="on" />
155                 <widget name="selecttext" position="80,325" size="150,21" zPosition="10" font="Regular;21" transparent="1" />
156                 <ePixmap pixmap="skin_default/buttons/button_yellow.png" position="50,355" zPosition="10" size="15,16" transparent="1" alphatest="on" />
157                 <widget name="rescantext" position="80,355" size="150,21" zPosition="10" font="Regular;21" transparent="1" />
158         </screen>
159         """
160
161         
162         def __init__(self, session, iface):
163         
164                 Screen.__init__(self, session)
165                 self.session = session
166
167                 self.skin = WlanScan.skin
168                 self.skin_path = plugin_path 
169                 
170                 
171                 self["info"] = Label()
172                 
173                 self.list = []  
174                 self["list"] = WlanList(self.session, iface)
175                 
176                 self.setInfo()
177
178                 self["canceltext"] = Label(_("Close"))
179                 self["selecttext"] = Label(_("Connect"))
180                 self["rescantext"] = Label(_("Refresh"))
181                         
182                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "EPGSelectActions"],
183                 {
184                         "ok": self.select,
185                         "back": self.exit,
186                 }, -1)
187                 
188                 self["shortcuts"] = ActionMap(["ShortcutActions"],
189                 {
190                         "red": self.exit,
191                         "green": self.select,
192                         "yellow": self.rescan,
193                 })
194                 self.onLayoutFinish.append(self.layoutFinished)
195                 
196         def layoutFinished(self):
197                 self.setTitle(_("Choose a Wireless Network"))
198         
199         def select(self):
200                 cur = self["list"].getCurrent()
201                 #print "CURRENT",cur
202                 if cur[1] is not None:
203                         essid = cur[0]
204                         if essid == '':
205                                 essid = cur[1]
206                         encrypted = cur[2]
207                         self.close(essid,self["list"].getList())
208                 else:
209                         self.close(self["list"].getList())
210         
211         def WlanSetupClosed(self, *ret):
212                 if ret[0] == 2:
213                         self.close(None)
214         
215         def rescan(self):
216                 self["list"].reload()
217                 self.setInfo()
218         
219         def exit(self):
220                 self.close(None)
221
222         def setInfo(self):
223                 length = self["list"].getLength()
224                 
225                 if length == 0:
226                         length = "No" 
227                 self["info"].setText(str(length)+_(" Wireless Network(s) found!"))      
228
229
230 def WlanStatusScreenMain(session, iface):
231         session.open(WlanStatus, iface)
232
233
234 def callFunction(iface):
235         
236         w = Wlan(iface)
237         i = w.getWirelessInterfaces()
238         if i:
239                 if iface in i:
240                         return WlanStatusScreenMain     
241         
242         return None
243
244
245 def configStrings(iface):
246         driver = iNetwork.detectWlanModule()
247         print "WLAN-MODULE",driver
248         if driver == 'ralink':
249                 return "        pre-up /usr/sbin/wpa_supplicant -i"+iface+" -c/etc/wpa_supplicant.conf -B -Dralink\n    post-down killall -q wpa_supplicant"
250         if driver == 'madwifi':
251                 return "        pre-up /usr/sbin/wpa_supplicant -i"+iface+" -c/etc/wpa_supplicant.conf -B -Dmadwifi\n   post-down killall -q wpa_supplicant"
252         if driver == 'zydas':
253                 return "        pre-up /usr/sbin/wpa_supplicant -i"+iface+" -c/etc/wpa_supplicant.conf -B -Dzydas\n     post-down killall -q wpa_supplicant"
254
255 def Plugins(**kwargs):
256         return PluginDescriptor(name=_("Wireless LAN"), description=_("Connect to a Wireless Network"), where = PluginDescriptor.WHERE_NETWORKSETUP, fnc={"ifaceSupported": callFunction, "configStrings": configStrings, "menuEntryName": lambda x: "Wireless Network Configuartion..."})
257