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