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