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