some fixes and cleanups
[enigma2-plugins.git] / wirelesslan / src / plugin.py
1 from enigma import eTimer
2 from Screens.Screen import Screen
3 from Components.ActionMap import ActionMap, NumberActionMap
4 from Components.Pixmap import Pixmap
5 from Components.Label import Label
6 from Components.MenuList import MenuList
7 from Components.config import config, getConfigListEntry, ConfigYesNo, NoSave, ConfigSubsection, ConfigText, ConfigSelection
8 from Components.ConfigList import ConfigListScreen
9 from Components.Network import Network, iNetwork
10 from Plugins.Plugin import PluginDescriptor
11 from os import system
12 from Wlan import Wlan, WlanList, wpaSupplicant
13
14 plugin_path = "/usr/lib/enigma2/python/Plugins/SystemPlugins/WirelessLan"
15 list = []
16 list.append(_("WEP"))
17 list.append(_("WPA"))
18 list.append(_("WPA2"))
19
20 config.plugins.wlan = ConfigSubsection()
21 config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False))
22
23 config.plugins.wlan.encryption = ConfigSubsection()
24 config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = False))
25 config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = _("WPA")))
26 config.plugins.wlan.encryption.psk = NoSave(ConfigText(default = "mysecurewlan", fixed_size = False))
27
28 class WlanSelectScreen(Screen):
29         skin = """
30         <screen position="185,238" size="350,100" title="Wireless Network Tools" >
31                 <widget name="menu" position="10,10" size="330,80" scrollbarMode="showOnDemand" />
32         </screen>
33         """
34         
35         
36         def __init__(self, session, iface):
37                 Screen.__init__(self, session)
38                 self.session = session
39                 
40                 self.skin = WlanSelectScreen.skin
41                 self.skin_path = plugin_path            
42                 
43                 self.iface = iface
44                 
45                 list = []
46                 list.append(_("Scan for Wireless Networks")) 
47                 list.append(_("Show WLAN Status"))
48                 list.append(_("Edit Wireless Settings"))
49                 
50                 
51                 self["menu"] = MenuList(list)
52                 
53                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "EPGSelectActions"],
54                 {
55                         "ok": self.ok,
56                         "back": self.exit,
57                 }, -1)
58
59
60         def ok(self):
61                 idx = self["menu"].getSelectedIndex()
62                 if idx is 0:
63                         self.session.open(WlanScan, self.iface)
64                         print "[plugin.py:Wireless] Starting WlanScan"
65                 elif idx is 1:
66                         self.session.open(WlanStatus, self.iface)
67                         print "[plugin.py:Wireless] Starting WlanStatus"
68
69                 elif idx is 2:
70                         self.session.open(WlanConfiguration, self.iface)
71                         print "[plugin.py:Wireless] Starting Manual Configuration"
72
73                 else:
74                         print "[plugin.py:Wireless] Unkown Menupoint"
75                                 
76         
77         def exit(self):
78                 self.close()
79
80
81
82 class WlanStatus(Screen):
83         #<screen position="185,188" size="350,223" title="Wireless Network Status" >
84         skin = """
85         <screen position="90,150" size="550,300" title="Wireless Network Status" >
86                 <widget name="LabelBSSID" position="10,10" size="150,25" valign="left" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
87                 <widget name="LabelESSID" position="10,38" size="150,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
88                 <widget name="LabelQuality" position="10,66" size="150,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
89                 <widget name="LabelSignal" position="10,94" size="150,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
90                 <widget name="LabelBitrate" position="10,122" size="150,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
91                 <widget name="LabelChannel" position="10,150" size="150,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
92                 
93                 <widget name="BSSID" position="320,10" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
94                 <widget name="ESSID" position="320,38" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
95                 <widget name="quality" position="320,66" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
96                 <widget name="signal" position="320,94" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
97                 <widget name="bitrate" position="320,122" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
98                 <widget name="channel" position="320,150" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
99                 
100                 <widget name="BottomBG" pixmap="skin_default/bottombar.png" position="5,210" size="540,120" zPosition="1" transparent="1" alphatest="on" />
101                 <widget name="IFtext" position="20,225" size="100,21" zPosition="10" font="Regular;19" transparent="1" />
102                 <widget name="IF" position="110,225" size="300,21" zPosition="10" font="Regular;19" transparent="1" />
103                 <widget name="Statustext" position="20,250" size="115,21" zPosition="10" font="Regular;19" transparent="1"/>
104                 <widget name="statuspic_inactive" pixmap="skin_default/buttons/button_green_off.png" position="120,250" zPosition="10" size="15,16" transparent="1" alphatest="on"/>
105                 <widget name="statuspic_active" pixmap="skin_default/buttons/button_green.png" position="120,250" zPosition="10" size="15,16" transparent="1" alphatest="on"/>
106                 <widget name="ButtonRedtext" position="430,225" size="120,21" zPosition="10" font="Regular;21" transparent="1" />
107                 <widget name="ButtonRed" pixmap="skin_default/buttons/button_red.png" position="410,225" zPosition="10" size="15,16" transparent="1" alphatest="on" />
108
109         </screen>
110         """
111         
112         
113         def __init__(self, session, iface):
114                 
115                 Screen.__init__(self, session)
116                 
117                 self.session = session
118                 self.iface = iface
119                 self.skin = WlanStatus.skin
120                 
121                 self.timer = eTimer()
122                 self.timer.timeout.get().append(self.resetList) 
123                 self.onShown.append(lambda: self.timer.start(5000))
124                                                     
125                 self["LabelBSSID"] = Label(_('Accesspoint:'))
126                 self["LabelESSID"] = Label(_('SSID:'))
127                 self["LabelQuality"] = Label(_('Link Quality:'))
128                 self["LabelSignal"] = Label(_('Signal Strength:'))
129                 self["LabelBitrate"] = Label(_('Bitrate:'))
130                 self["LabelChannel"] = Label(_('Channel:'))
131                         
132                 self["BSSID"] = Label()
133                 self["ESSID"] = Label()
134                 self["quality"] = Label()
135                 self["signal"] = Label()
136                 self["bitrate"] = Label()
137                 self["channel"] = Label()
138
139                 self["IFtext"] = Label()
140                 self["IF"] = Label()
141                 self["Statustext"] = Label()
142                 self["statuspic_active"] = Pixmap()
143                 self["statuspic_active"].hide()
144                 self["statuspic_inactive"] = Pixmap()
145                 self["statuspic_inactive"].hide()
146                 self["BottomBG"] = Pixmap()
147                 self["ButtonRed"] = Pixmap()
148                 self["ButtonRedtext"] = Label(_("Close"))
149
150                 self.resetList()
151                 self.updateStatusbar()
152                 
153                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "EPGSelectActions"],
154                 {
155                         "ok": self.exit,
156                         "back": self.exit,
157                 }, -1)
158                 
159         
160         def resetList(self):
161                 w = Wlan(self.iface)
162                 stats = w.getStatus()
163                 if stats['BSSID'] == "00:00:00:00:00:00":
164                         stats['BSSID'] = _("No Connection!")
165                 self["BSSID"].setText(stats['BSSID'])
166                 self["ESSID"].setText(stats['ESSID'])
167                 self["quality"].setText(stats['quality']+"%")
168                 self["signal"].setText(stats['signal']+"%")
169                 self["bitrate"].setText(stats['bitrate'])
170                 self["channel"].setText(stats['channel'])
171                 
172         
173         def exit(self):
174                 self.timer.stop()
175                 self.close()    
176
177         def updateStatusbar(self):
178                 self["IFtext"].setText(_("Network:"))
179                 self["IF"].setText(iNetwork.getFriendlyAdapterName(self.iface))
180                 self["Statustext"].setText(_("Link:"))
181                 w = Wlan(self.iface)
182                 stats = w.getStatus()
183                 if stats['BSSID'] == "00:00:00:00:00:00":
184                         self["statuspic_active"].hide()
185                         self["statuspic_inactive"].show()
186                 else:
187                         self["statuspic_active"].show()
188                         self["statuspic_inactive"].hide()
189
190
191 class WlanScan(Screen):
192         skin = """
193         <screen position="70,90" size="600,400" title="Choose a Wireless Network" >
194                 <widget name="info" position="10,10" size="580,30" font="Regular;24" transparent="1" foregroundColor="#FFFFFF" />
195                 <widget name="list" position="10,50" size="580,240" scrollbarMode="showOnDemand" />
196
197                 <ePixmap pixmap="skin_default/bottombar.png" position="30,310" size="540,120" zPosition="1" transparent="1" alphatest="on" />
198                 <ePixmap pixmap="skin_default/buttons/button_red.png" position="430,325" zPosition="10" size="15,16" transparent="1" alphatest="on" />
199                 <widget name="canceltext" position="450,325" size="120,21" zPosition="10" font="Regular;21" transparent="1" />
200                 <ePixmap pixmap="skin_default/buttons/button_green.png" position="50,325" zPosition="10" size="15,16" transparent="1" alphatest="on" />
201                 <widget name="selecttext" position="80,325" size="150,21" zPosition="10" font="Regular;21" transparent="1" />
202                 <ePixmap pixmap="skin_default/buttons/button_yellow.png" position="50,355" zPosition="10" size="15,16" transparent="1" alphatest="on" />
203                 <widget name="rescantext" position="80,355" size="150,21" zPosition="10" font="Regular;21" transparent="1" />
204         </screen>
205         """
206
207         
208         def __init__(self, session, iface):
209         
210                 Screen.__init__(self, session)
211                 self.session = session
212
213                 self.skin = WlanScan.skin
214                 self.skin_path = plugin_path 
215                 
216                 
217                 self["info"] = Label()
218                 
219                 self.list = []  
220                 self["list"] = WlanList(self.session, iface)
221                 
222                 self.setInfo()
223
224                 self["canceltext"] = Label(_("Close"))
225                 self["selecttext"] = Label(_("Connect"))
226                 self["rescantext"] = Label(_("Refresh"))
227                         
228                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "EPGSelectActions"],
229                 {
230                         "ok": self.select,
231                         "back": self.exit,
232                 }, -1)
233                 
234                 self["shortcuts"] = ActionMap(["ShortcutActions"],
235                 {
236                         "red": self.exit,
237                         "green": self.select,
238                         "yellow": self.rescan,
239                 })
240
241         
242         def select(self):
243                 cur = self["list"].getCurrent()
244                 #print "CURRENT",cur
245                 if cur[1] is not None:
246                         essid = cur[0]
247                         if essid == '':
248                                 essid = cur[1]
249                         encrypted = cur[2]
250                         self.close(essid,self["list"].getList())
251                 else:
252                         self.close(self["list"].getList())
253         
254         def WlanSetupClosed(self, *ret):
255                 if ret[0] == 2:
256                         self.close(None)
257         
258         def rescan(self):
259                 self["list"].reload()
260                 self.setInfo()
261         
262         def exit(self):
263                 self.close(None)
264
265         def setInfo(self):
266                 length = self["list"].getLength()
267                 
268                 if length == 0:
269                         length = "No" 
270                 self["info"].setText(str(length)+_(" Wireless Network(s) found!"))      
271
272
273         
274 class WlanConfiguration(ConfigListScreen, Screen):
275         skin = """
276                 <screen position="90,100" size="550,400" title="Wireless Network Configuration" >
277                         <widget name="interface" position="10,10" size="530,30" font="Regular;24" valign="center" />
278                         <widget name="config" position="10,60" size="530,150" scrollbarMode="showOnDemand" />
279                         
280                         <ePixmap name="BottomBG" pixmap="skin_default/bottombar.png" position="5,310" size="540,120" zPosition="1" transparent="1" alphatest="on" />
281                         <widget name="Adaptertext" position="20,325" size="100,21" zPosition="10" font="Regular;19" transparent="1" />
282                         <widget name="Adapter" position="110,325" size="300,21" zPosition="10" font="Regular;19" transparent="1" />
283                         <widget name="introduction" position="110,360" size="300,20" zPosition="10" font="Regular;21" halign="center" transparent="1" />
284                         <widget name="ButtonRedtext" position="430,325" size="80,21" zPosition="10" font="Regular;21" transparent="1" />
285                         <ePixmap name="ButtonRed" pixmap="skin_default/buttons/button_red.png" position="410,325" zPosition="10" size="15,16" transparent="1" alphatest="on" />
286                 </screen>
287         """
288         
289         def __init__(self, session, iface = "wlan0", essid = None, encrypted = False):          
290                 Screen.__init__(self, session)          
291                 self.skin = WlanConfiguration.skin
292                 print "ESSID,",essid
293                 self.iface = iface
294                 self.list = []
295                 self.ws = wpaSupplicant()
296                 
297                 self["introduction"] = Label(_("Press OK to activate the settings."))
298                 self["interface"] = Label(_("Interface: ")+self.iface)
299                 self["Adaptertext"] = Label(_("Network:"))
300                 self["Adapter"] = Label(iNetwork.getFriendlyAdapterName(self.iface))
301                 self["ButtonRedtext"] = Label(_("Close"))
302                 
303                 if essid is None:
304                         self.ws.loadConfig()
305                 
306                 else:
307                         config.plugins.wlan.essid.value = essid
308                         config.plugins.wlan.encryption.enabled.value = True
309                         
310
311                 self["actions"] = ActionMap(["SetupActions"],
312                 {
313                         "ok": self.ok,
314                         "cancel": self.cancel,
315                 }, -2)
316                 
317                 ConfigListScreen.__init__(self, self.list)
318                 self.createSetup()
319
320         
321         def createSetup(self):
322                 self.list = [ ]
323                                                 
324                 self.list.append(getConfigListEntry(_("Network SSID"), config.plugins.wlan.essid))
325                 #self.list.append(getConfigListEntry(_("Encryption"), config.plugins.wlan.encryption.enabled))
326                 self.encryptionEnabled = getConfigListEntry(_("Encryption"), config.plugins.wlan.encryption.enabled)
327                 self.list.append(self.encryptionEnabled)                
328                 if config.plugins.wlan.encryption.enabled.value:
329                         self.list.append(getConfigListEntry(_("Encryption Type"), config.plugins.wlan.encryption.type))
330                         self.list.append(getConfigListEntry(_("Encryption Key"), config.plugins.wlan.encryption.psk))
331                 
332                 self["config"].list = self.list
333                 self["config"].l.setList(self.list)
334
335         def keyLeft(self):
336                 ConfigListScreen.keyLeft(self)
337                 self.createSetup()
338
339         
340         def keyRight(self):
341                 ConfigListScreen.keyRight(self)
342                 self.createSetup()
343
344         
345         def ok(self):
346                 self.ws.writeConfig()
347                 iNetwork.deactivateNetworkConfig()
348                 iNetwork.activateNetworkConfig()
349                 self.close(2)
350
351         def cancel(self):
352                 self.close(1)
353
354
355
356 def EntryChosen(parms):
357         if parms[0]:
358                 session = parms[0]
359                 if parms[1] is not None:
360                         val = parms[1]
361                         essid = val[0]
362                         encrypted = val[2]
363                         iface = val[3]
364                         session.open(WlanConfiguration, iface, essid, encrypted)
365                 else:
366                         session.open(WlanConfiguration)
367
368
369 def WlanSelectScreenMain(session, iface):
370         session.open(WlanSelectScreen, iface)
371
372
373 def callFunction(iface):
374         
375         w = Wlan(iface)
376         i = w.getWirelessInterfaces()
377         if i:
378                 if iface in i:
379                         return WlanSelectScreenMain     
380         
381         return None
382
383
384 def configStrings(iface):
385         ret=system("dmesg | grep \"rt73: using permanent MAC addr\"")
386         ret2=system("dmesg | grep \"rt73: using net dev supplied MAC addr\"")
387         if ret == 0 or ret2 == 0:
388                 return "        pre-up /usr/sbin/wpa_supplicant -i"+iface+" -c/etc/wpa_supplicant.conf -B -Dralink\n    post-down killall -q wpa_supplicant"
389         else:
390                 return "        pre-up /usr/sbin/wpa_supplicant -i"+iface+" -c/etc/wpa_supplicant.conf -B\n     post-down killall -q wpa_supplicant"
391         
392
393 def Plugins(**kwargs):
394         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..."})
395