* show number of found Networks in Scan Window
[enigma2-plugins.git] / wirelesslan / src / plugin.py
1 from Screens.Screen import Screen
2 from Screens.MessageBox import MessageBox
3
4 from Components.ActionMap import ActionMap, NumberActionMap
5 from Components.Pixmap import Pixmap
6 from Components.Label import Label
7 from Components.GUIComponent import *
8 from Components.MenuList import MenuList
9 from Components.MultiContent import MultiContentEntryText
10
11
12 from Components.config import config, getConfigListEntry
13 from Components.ConfigList import ConfigList, ConfigListScreen
14 from Components.Network import Network
15
16 from Plugins.Plugin import PluginDescriptor
17
18 from Wlan import Wlan, WlanList, wpaSupplicant
19
20 plugin_path = "/usr/lib/enigma2/python/Plugins/SystemPlugins/WirelessLan"
21
22 class WlanSelection(Screen):
23         skin = """
24         <screen position="70,138" size="610,300" title="Choose a Wireless Network" >
25                 <widget name="info" position="10,10" size="580,30" font="Regular;24" transparent="1" foregroundColor="#FFFFFF" />
26                 <widget name="list" position="10,60" size="580,200" scrollbarMode="showOnDemand" />
27                 
28                 <widget name="cancel" position="10,255" size="140,40" pixmap="~/key-red.png" zPosition="1" transparent="1" alphatest="on" />
29                 <widget name="select" position="160,255" size="140,40" pixmap="~/key-green.png" zPosition="1" transparent="1" alphatest="on" />
30                 <widget name="rescan" position="310,255" size="140,40" pixmap="~/key-yellow.png" zPosition="1" transparent="1" alphatest="on" />
31                 <widget name="skip" position="460,255" size="140,40" pixmap="~/key-blue.png" zPosition="1" transparent="1" alphatest="on" />
32                 
33                 <widget name="canceltext" position="10,255" size="140,40" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />          
34                 <widget name="selecttext" position="160,255" size="140,40" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1"  foregroundColor="#FFFFFF" />
35                 <widget name="rescantext" position="310,255" size="140,40" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1"  foregroundColor="#FFFFFF" />
36                 <widget name="skiptext" position="460,255" size="140,40" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" />
37         </screen>
38         """
39         def __init__(self, session, iface):
40         
41                 Screen.__init__(self, session)
42                 self.session = session
43                 
44                 self.skin = WlanSelection.skin
45                 self.skin_path = plugin_path 
46                 
47                 
48                 self["info"] = Label()
49                 
50                 self.list = []  
51                 self["list"] = WlanList(self.session, iface)
52                 self["cancel"] = Pixmap()
53                 self["select"] = Pixmap()
54                 self["rescan"] = Pixmap()
55                 self["skip"] = Pixmap()
56                 
57                 self.setInfo()
58                 
59                 self["canceltext"] = Label(_("Cancel"))
60                 self["selecttext"] = Label(_("Select"))
61                 self["rescantext"] = Label(_("Rescan"))
62                 self["skiptext"] = Label(_("Skip"))
63                         
64                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "EPGSelectActions"],
65                 {
66                         "ok": self.select,
67                         "back": self.exit,
68 #                       "up": self.up,
69 #                       "down": self.down,
70                 }, -1)
71                 
72                 self["shortcuts"] = ActionMap(["ShortcutActions"],
73                 {
74                         "red": self.exit,
75                         "green": self.select,
76                         "yellow": self.rescan,
77                         "blue": self.skip,
78                 })
79         
80         def select(self):
81                 cur = self["list"].getCurrent()
82                 if cur:
83                         ret = (self.session, cur)
84                 else:
85                         ret = (self.session, None)
86                 self.close(ret)
87         
88         def rescan(self):
89                 self["list"].reload()
90                 self.setInfo()
91         
92         def skip(self):
93                 self.close( (self.session, None) )
94         
95         def exit(self):
96                 self.close( (None ,) )
97         
98         def setInfo(self):
99                 length = self["list"].getLength()
100                 
101                 if length == 0:
102                         length = "No" 
103                 self["info"].setText(str(length)+_(" Wireless Network(s) found!"))      
104         
105 class WlanConfiguration(ConfigListScreen, Screen):
106         skin = """
107                 <screen position="76,138" size="600,300" title="Wireless Network Configuration" >
108                         <widget name="interface" position="10,10" size="580,30" font="Regular;24" valign="center" />
109                         <widget name="config" position="10,60" size="580,150" scrollbarMode="showOnDemand" />
110                         <widget name="introduction" position="100,260" size="400,30" font="Regular;23" valign="center" halign="center" />       
111                 </screen>
112         """
113         
114         def __init__(self, session, essid = None, encrypted = False, iface = "wlan0"):
115                 
116                 Screen.__init__(self, session)          
117                 self.skin = WlanConfiguration.skin
118                 
119                 self.iface = iface
120                 self.list = []
121                 self.ws = wpaSupplicant()
122                 
123                 self["introduction"] = Label(_("Press OK to activate the settings."))
124                 self["interface"] = Label(_("Interface: ")+self.iface)
125                 
126                 if essid is None:
127                         self.ws.loadConfig()
128                 
129                 else:
130                         config.plugins.wlan.essid.value = essid
131                         config.plugins.wlan.encryption.enabled.value = True
132                         
133                 self["actions"] = ActionMap(["SetupActions"],
134                 {
135                         "ok": self.ok,
136                         "cancel": self.cancel,
137                 }, -2)
138                 
139                 ConfigListScreen.__init__(self, self.list)
140                 self.createSetup()
141         
142         def createSetup(self):
143
144                 self.list = [ ]
145                                                 
146                 self.list.append(getConfigListEntry(_("Network SSID"), config.plugins.wlan.essid))
147                 self.list.append(getConfigListEntry(_("Encryption"), config.plugins.wlan.encryption.enabled))
148                 
149                 if config.plugins.wlan.encryption.enabled.value:
150                         self.list.append(getConfigListEntry(_("Encryption Type"), config.plugins.wlan.encryption.type))
151                         self.list.append(getConfigListEntry(_("Encryption Key"), config.plugins.wlan.encryption.psk))
152                 
153                 self["config"].list = self.list
154                 self["config"].l.setList(self.list)
155         
156         def keyLeft(self):
157                 ConfigListScreen.keyLeft(self)
158                 self.createSetup()
159
160         def keyRight(self):
161                 ConfigListScreen.keyRight(self)
162                 self.createSetup()
163
164         def ok(self):
165                 self.ws.writeConfig()
166                 self.ws.restart(self.iface)
167                 self.close()
168
169         def cancel(self):
170                 self.close()
171
172 def EntryChosen(parms):
173         if parms[0]:
174                 session = parms[0]
175                 if parms[1] is not None:
176                         val = parms[1]
177                         essid = val[0]
178                         encrypted = val[2]
179                         iface = val[3]
180                         session.open(WlanConfiguration, essid, encrypted, iface)
181                 else:
182                         session.open(WlanConfiguration)
183
184 def WlanSelectionMain(session, iface):
185         session.openWithCallback(EntryChosen, WlanSelection, iface)
186
187 def WlanConfigurationMain(session, **kwargs):
188         session.open(WlanConfiguration)
189
190 def callFunction(iface):
191         w = Wlan()
192
193         if iface in w.getWirelessInterfaces():
194                 return WlanSelectionMain        
195         else:
196                 return None
197
198 def configStrings(iface):
199         return "pre-up /usr/sbin/wpa_supplicant -B -i"+iface+" -c/etc/wpa_supplicant.conf"
200         
201 def Plugins(**kwargs):
202         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..."})
203