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