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