WirelessLan Plugin: - replace labels and buttons through StaticText for better skinab...
[enigma2-plugins.git] / wirelesslan / src / plugin.py
1 # for localized messages
2 from __init__ import _
3
4 from enigma import eTimer
5 from Screens.Screen import Screen
6 from Components.ActionMap import ActionMap, NumberActionMap
7 from Components.Pixmap import Pixmap,MultiPixmap
8 from Components.Label import Label
9 from Components.Sources.StaticText import StaticText
10 from Components.MenuList import MenuList
11 from Components.config import config, getConfigListEntry, ConfigYesNo, NoSave, ConfigSubsection, ConfigText, ConfigSelection, ConfigPassword
12 from Components.ConfigList import ConfigListScreen
13 from Components.Network import Network, iNetwork
14 from Components.Console import Console
15 from Plugins.Plugin import PluginDescriptor
16 from os import system, path as os_path, listdir
17 from Wlan import Wlan, WlanList, wpaSupplicant
18 from Wlan import Status, iStatus
19
20 plugin_path = "/usr/lib/enigma2/python/Plugins/SystemPlugins/WirelessLan"
21
22 list = []
23 list.append("WEP")
24 list.append("WPA")
25 list.append("WPA2")
26 list.append("WPA/WPA2")
27
28 weplist = []
29 weplist.append("ASCII")
30 weplist.append("HEX")
31
32 config.plugins.wlan = ConfigSubsection()
33 config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False))
34 config.plugins.wlan.hiddenessid = NoSave(ConfigText(default = "home", fixed_size = False))
35
36 config.plugins.wlan.encryption = ConfigSubsection()
37 config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = False))
38 config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = "WPA/WPA2" ))
39 config.plugins.wlan.encryption.wepkeytype = NoSave(ConfigSelection(weplist, default = "ASCII"))
40 config.plugins.wlan.encryption.psk = NoSave(ConfigPassword(default = "mysecurewlan", fixed_size = False))
41
42
43 class WlanStatus(Screen):
44         skin = """
45         <screen position="center,center" size="560,400" title="Wireless Network State" >
46                 <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
47                 <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
48
49                 <widget source="LabelBSSID" render="Label" position="10,60" size="250,25" valign="left" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
50                 <widget source="LabelESSID" render="Label" position="10,100" size="250,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
51                 <widget source="LabelQuality" render="Label" position="10,140" size="250,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
52                 <widget source="LabelSignal" render="Label" position="10,180" size="250,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
53                 <widget source="LabelBitrate" render="Label" position="10,220" size="250,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
54                 <widget source="LabelEnc" render="Label" position="10,260" size="250,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
55                 
56                 <widget source="BSSID" render="Label" position="320,60" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
57                 <widget source="ESSID" render="Label" position="320,100" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
58                 <widget source="quality" render="Label" position="320,140" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
59                 <widget source="signal" render="Label" position="320,180" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
60                 <widget source="bitrate" render="Label" position="320,220" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
61                 <widget source="enc" render="Label" position="320,260" size="180,25" valign="center" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
62
63                 <ePixmap pixmap="skin_default/div-h.png" position="0,350" zPosition="1" size="560,2" />         
64                 <widget source="IFtext" render="Label" position="10,355" size="120,21" zPosition="10" font="Regular;20" halign="left" backgroundColor="#25062748" transparent="1" />
65                 <widget source="IF" render="Label" position="120,355" size="400,21" zPosition="10" font="Regular;20" halign="left" backgroundColor="#25062748" transparent="1" />
66                 <widget source="Statustext" render="Label" position="10,375" size="115,21" zPosition="10" font="Regular;20" halign="left" backgroundColor="#25062748" transparent="1"/>
67                 <widget name="statuspic" pixmaps="skin_default/buttons/button_green.png,skin_default/buttons/button_green_off.png" position="130,380" zPosition="10" size="15,16" transparent="1" alphatest="on"/>
68         </screen>
69         """
70         
71         def __init__(self, session, iface):
72                 Screen.__init__(self, session)
73                 self.session = session
74                 self.iface = iface
75                 self.skin = WlanStatus.skin
76                                     
77                 self["LabelBSSID"] = StaticText(_('Accesspoint:'))
78                 self["LabelESSID"] = StaticText(_('SSID:'))
79                 self["LabelQuality"] = StaticText(_('Link Quality:'))
80                 self["LabelSignal"] = StaticText(_('Signal Strength:'))
81                 self["LabelBitrate"] = StaticText(_('Bitrate:'))
82                 self["LabelEnc"] = StaticText(_('Encryption:'))
83                         
84                 self["BSSID"] = StaticText()
85                 self["ESSID"] = StaticText()
86                 self["quality"] = StaticText()
87                 self["signal"] = StaticText()
88                 self["bitrate"] = StaticText()
89                 self["enc"] = StaticText()
90
91                 self["IFtext"] = StaticText()
92                 self["IF"] = StaticText()
93                 self["Statustext"] = StaticText()
94                 self["statuspic"] = MultiPixmap()
95                 self["statuspic"].hide()
96                 self["key_red"] = StaticText(_("Close"))
97
98                 self.resetList()
99                 self.updateStatusbar()
100                 
101                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "EPGSelectActions", "ShortcutActions"],
102                 {
103                         "ok": self.exit,
104                         "back": self.exit,
105                         "red": self.exit,
106                 }, -1)
107                 self.timer = eTimer()
108                 self.timer.timeout.get().append(self.resetList) 
109                 self.onShown.append(lambda: self.timer.start(5000))
110                 self.onLayoutFinish.append(self.layoutFinished)
111                 self.onClose.append(self.cleanup)
112
113         def cleanup(self):
114                 iStatus.stopWlanConsole()
115                 
116         def layoutFinished(self):
117                 self.setTitle(_("Wireless Network State"))
118                 
119         def resetList(self):
120                 print "self.iface im resetlist",self.iface
121                 iStatus.getDataForInterface(self.iface,self.getInfoCB)
122                 
123         def getInfoCB(self,data,status):
124                 if data is not None:
125                         if data is True:
126                                 if status is not None:
127                                         self["BSSID"].setText(status[self.iface]["acesspoint"])
128                                         self["ESSID"].setText(status[self.iface]["essid"])
129                                         self["quality"].setText(status[self.iface]["quality"]+"%")
130                                         self["signal"].setText(status[self.iface]["signal"])
131                                         self["bitrate"].setText(status[self.iface]["bitrate"])
132                                         self["enc"].setText(status[self.iface]["encryption"])
133                                         self.updateStatusLink(status)
134
135         def exit(self):
136                 self.timer.stop()
137                 self.close()    
138
139         def updateStatusbar(self):
140                 print "self.iface im updateStatusbar",self.iface
141                 self["BSSID"].setText(_("Please wait..."))
142                 self["ESSID"].setText(_("Please wait..."))
143                 self["quality"].setText(_("Please wait..."))
144                 self["signal"].setText(_("Please wait..."))
145                 self["bitrate"].setText(_("Please wait..."))
146                 self["enc"].setText(_("Please wait..."))
147                 self["IFtext"].setText(_("Network:"))
148                 self["IF"].setText(iNetwork.getFriendlyAdapterName(self.iface))
149                 self["Statustext"].setText(_("Link:"))
150
151         def updateStatusLink(self,status):
152                 if status is not None:
153                         if status[self.iface]["acesspoint"] == "No Connection" or status[self.iface]["acesspoint"] == "Not-Associated" or status[self.iface]["acesspoint"] == False:
154                                 self["statuspic"].setPixmapNum(1)
155                         else:
156                                 self["statuspic"].setPixmapNum(0)
157                         self["statuspic"].show()                
158
159 class WlanScan(Screen):
160         skin = """
161         <screen position="center,center" size="560,400" title="Choose a Wireless Network" >
162                 <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
163                 <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
164                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
165                 <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
166                 <widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
167                 <widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" transparent="1" />
168                 <widget name="list" position="5,50" size="550,280" scrollbarMode="showOnDemand" />
169                 <ePixmap pixmap="skin_default/div-h.png" position="0,340" zPosition="1" size="560,2" />         
170                 <widget source="info" render="Label" position="0,350" size="560,50" font="Regular;24" halign="center" valign="center" backgroundColor="#25062748" transparent="1" />
171         </screen>
172         """
173
174         def __init__(self, session, iface):
175                 Screen.__init__(self, session)
176                 self.session = session
177                 self.iface = iface
178                 self.skin = WlanScan.skin
179                 self.skin_path = plugin_path
180                 self.oldInterfaceState = iNetwork.getAdapterAttribute(self.iface, "up")
181                 
182                 self["info"] = StaticText()
183                 
184                 self.list = []  
185                 self["list"] = WlanList(self.session, self.iface)
186                 
187                 self.setInfo()
188
189                 self["key_red"] = StaticText(_("Close"))
190                 self["key_green"] = StaticText(_("Connect"))
191                 self["key_yellow"] = StaticText(_("Refresh"))
192                         
193                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "EPGSelectActions"],
194                 {
195                         "ok": self.select,
196                         "back": self.cancel,
197                 }, -1)
198                 
199                 self["shortcuts"] = ActionMap(["ShortcutActions"],
200                 {
201                         "red": self.cancel,
202                         "green": self.select,
203                         "yellow": self.rescan,
204                 })
205                 self.onLayoutFinish.append(self.layoutFinished)
206                 
207         def layoutFinished(self):
208                 self.setTitle(_("Choose a wireless network"))
209         
210         def select(self):
211                 cur = self["list"].getCurrent()
212                 #print "CURRENT",cur
213                 if cur is not None:
214                         if cur[1] is not None:
215                                 essid = cur[0]
216                                 if essid == '':
217                                         essid = cur[1]
218                                 encrypted = cur[2]
219                                 self.close(essid,self["list"].getList())
220                         else:
221                                 self.close(None,None)
222                 else:
223                         self.close(None,None)
224         
225         def WlanSetupClosed(self, *ret):
226                 if ret[0] == 2:
227                         self.close(None)
228         
229         def rescan(self):
230                 self["list"].reload()
231                 self.setInfo()
232         
233         def cancel(self):
234                 if self.oldInterfaceState is False:
235                         iNetwork.deactivateInterface(self.iface,self.deactivateInterfaceCB)
236                 else:
237                         self.close(None)
238
239         def deactivateInterfaceCB(self,data):
240                 if data is not None:
241                         if data is True:
242                                 iNetwork.getInterfaces(self.cancelCB)
243         
244         def cancelCB(self,data):                        
245                 if data is not None:
246                         if data is True:
247                                 self.close(None)
248
249         def setInfo(self):
250                 length = self["list"].getLength()
251                 
252                 if length == 0:
253                         self["info"].setText(_("No wireless networks found! Please refresh."))
254                 elif length == 1:
255                         self["info"].setText(_("1 wireless network found!"))
256                 else:
257                         self["info"].setText(str(length)+_(" wireless networks found!"))        
258
259
260 def WlanStatusScreenMain(session, iface):
261         session.open(WlanStatus, iface)
262
263
264 def callFunction(iface):
265         
266         w = Wlan(iface)
267         i = w.getWirelessInterfaces()
268         if i:
269                 if iface in i:
270                         return WlanStatusScreenMain
271         
272         return None
273
274
275 def configStrings(iface):
276         driver = iNetwork.detectWlanModule()
277         print "WLAN-MODULE",driver
278         if driver == 'ralink':
279                 return "        pre-up /usr/sbin/wpa_supplicant -i"+iface+" -c/etc/wpa_supplicant.conf -B -Dralink\n    post-down wpa_cli terminate"
280         if driver == 'madwifi':
281                 if config.plugins.wlan.essid.value == "hidden...":
282                         return "        pre-up iwconfig "+iface+" essid "+config.plugins.wlan.hiddenessid.value+"\n     pre-up /usr/sbin/wpa_supplicant -i"+iface+" -c/etc/wpa_supplicant.conf -B -dd -Dmadwifi\n       post-down wpa_cli terminate"
283                 return "        pre-up iwconfig "+iface+" essid "+config.plugins.wlan.essid.value+"\n   pre-up /usr/sbin/wpa_supplicant -i"+iface+" -c/etc/wpa_supplicant.conf -B -dd -Dmadwifi\n       post-down wpa_cli terminate"
284         if driver == 'zydas':
285                 return "        pre-up /usr/sbin/wpa_supplicant -i"+iface+" -c/etc/wpa_supplicant.conf -B -dd -Dzydas\n post-down wpa_cli terminate"
286
287 def Plugins(**kwargs):
288         return PluginDescriptor(name=_("Wireless LAN"), description=_("Connect to a Wireless Network"), where = PluginDescriptor.WHERE_NETWORKSETUP, fnc={"ifaceSupported": callFunction, "configStrings": configStrings, "WlanPluginEntry": lambda x: "Wireless Network Configuartion..."})
289