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