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