unix linebreaks
[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                 self["skiptext"] = Label(_("Skip"))
184                         
185                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "EPGSelectActions"],
186                 {
187                         "ok": self.select,
188                         "back": self.exit,
189                 }, -1)
190                 
191                 self["shortcuts"] = ActionMap(["ShortcutActions"],
192                 {
193                         "red": self.exit,
194                         "green": self.select,
195                         "yellow": self.rescan,
196                 })
197
198         
199         def select(self):
200                 cur = self["list"].getCurrent()
201                 if cur:
202                         ret = (self.session, cur)
203                 else:
204                         ret = (self.session, None)
205                 self.close(ret)
206
207         
208         def rescan(self):
209                 self["list"].reload()
210                 self.setInfo()
211
212         
213         def exit(self):
214                 self.close( (None ,) )
215
216         
217         def setInfo(self):
218                 length = self["list"].getLength()
219                 
220                 if length == 0:
221                         length = "No" 
222                 self["info"].setText(str(length)+_(" Wireless Network(s) found!"))      
223
224
225         
226 class WlanConfiguration(ConfigListScreen, Screen):
227         skin = """
228                 <screen position="76,138" size="600,300" title="Wireless Network Configuration" >
229                         <widget name="interface" position="10,10" size="580,30" font="Regular;24" valign="center" />
230                         <widget name="config" position="10,60" size="580,150" scrollbarMode="showOnDemand" />
231                         <widget name="introduction" position="100,260" size="400,30" font="Regular;23" valign="center" halign="center" />       
232                 </screen>
233         """
234         
235         def __init__(self, session, iface = "wlan0", essid = None, encrypted = False):          
236                 Screen.__init__(self, session)          
237                 self.skin = WlanConfiguration.skin
238                 
239                 self.iface = iface
240                 self.list = []
241                 self.ws = wpaSupplicant()
242                 
243                 self["introduction"] = Label(_("Press OK to activate the settings."))
244                 self["interface"] = Label(_("Interface: ")+self.iface)
245                 
246                 if essid is None:
247                         self.ws.loadConfig()
248                 
249                 else:
250                         config.plugins.wlan.essid.value = essid
251                         config.plugins.wlan.encryption.enabled.value = True
252                         
253                 self["actions"] = ActionMap(["SetupActions"],
254                 {
255                         "ok": self.ok,
256                         "cancel": self.cancel,
257                 }, -2)
258                 
259                 ConfigListScreen.__init__(self, self.list)
260                 self.createSetup()
261
262         
263         def createSetup(self):
264                 self.list = [ ]
265                                                 
266                 self.list.append(getConfigListEntry(_("Network SSID"), config.plugins.wlan.essid))
267                 self.list.append(getConfigListEntry(_("Encryption"), config.plugins.wlan.encryption.enabled))
268                 
269                 if config.plugins.wlan.encryption.enabled.value:
270                         self.list.append(getConfigListEntry(_("Encryption Type"), config.plugins.wlan.encryption.type))
271                         self.list.append(getConfigListEntry(_("Encryption Key"), config.plugins.wlan.encryption.psk))
272                 
273                 self["config"].list = self.list
274                 self["config"].l.setList(self.list)
275         
276         
277         def keyLeft(self):
278                 ConfigListScreen.keyLeft(self)
279                 self.createSetup()
280
281         
282         def keyRight(self):
283                 ConfigListScreen.keyRight(self)
284                 self.createSetup()
285
286         
287         def ok(self):
288                 self.ws.writeConfig()
289                 self.ws.restart(self.iface)
290                 self.close()
291
292         def cancel(self):
293                 self.close()
294
295
296
297 def EntryChosen(parms):
298         if parms[0]:
299                 session = parms[0]
300                 if parms[1] is not None:
301                         val = parms[1]
302                         essid = val[0]
303                         encrypted = val[2]
304                         iface = val[3]
305                         session.open(WlanConfiguration, iface, essid, encrypted)
306                 else:
307                         session.open(WlanConfiguration)
308
309
310 def WlanSelectScreenMain(session, iface):
311         session.open(WlanSelectScreen, iface)
312
313
314 def callFunction(iface):
315         
316         w = Wlan(iface)
317         i = w.getWirelessInterfaces()
318         if i:
319                 if iface in i:
320                         return WlanSelectScreenMain     
321         
322         return None
323
324
325 def configStrings(iface):
326         return "\tpre-up /usr/sbin/wpa_supplicant -i"+iface+" -c/etc/wpa_supplicant.conf -B"
327         
328
329 def Plugins(**kwargs):
330         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..."})
331