* Add License
[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.GUIComponent import *
9 from Components.MenuList import MenuList
10 from Components.MultiContent import MultiContentEntryText
11 from Components.config import config, getConfigListEntry
12 from Components.ConfigList import ConfigList, ConfigListScreen
13 from Network import Network
14
15 from Plugins.Plugin import PluginDescriptor
16
17 from Wlan import WlanList, InitNetwork, wpaSupplicant
18
19 class WlanSelection(Screen):
20         skin = """
21         <screen position="76,138" size="600,300" title="Choose a Wireless Network" >
22                 <widget name="list" position="10,10" size="580,200" scrollbarMode="showOnDemand" />
23         </screen>
24         """
25                 #<widget name="Explanation" position="10,340" size="580,100" /> 
26         def __init__(self, session, args = None):
27         
28                 self.skin = WlanSelection.skin
29                 self.session = session
30                 Screen.__init__(self, session)
31                 
32                 self.list = []
33                                 
34                 self["list"] = WlanList(None)
35                 
36                 self["actions"] = NumberActionMap(["WizardActions", "InputActions", "EPGSelectActions"],
37                 {
38                         "ok": self.ok,
39                         "back": self.back,
40                         "up": self.up,
41                         "down": self.down,
42                 }, -1)
43
44         def up(self):
45                 print "up"
46         
47         def down(self):
48                 print "down"
49         
50         def ok(self):
51                 cur = self["list"].getCurrent()
52                 if cur:
53                         ret = (self.session, cur)
54                 else:
55                         ret = (self.session, None)
56                 self.close(ret)
57         
58         def back(self):
59                 self.close( (self.session, None) )
60
61 class WlanConfiguration(ConfigListScreen, Screen):
62         skin = """
63                 <screen position="76,138" size="600,300" title="Wireless Network Configuration" >
64                         <widget name="config" position="10,10" size="580,200" scrollbarMode="showOnDemand" />   
65                 </screen>
66         """
67         
68         def __init__(self, session, essid = None, encrypted = False, iface = "wlan1"):
69                 
70                 Screen.__init__(self, session)          
71
72                 self.ws = wpaSupplicant()
73                 
74                 if essid is None:
75                         self.ws.loadConfig()
76                 
77                 else:
78                         config.plugins.wlan.enabled.value = True
79                         config.plugins.wlan.interface.value = iface
80                         config.plugins.wlan.essid.value = essid
81                         config.plugins.wlan.encryption.enabled.value = True
82                         
83                 self["actions"] = ActionMap(["SetupActions"],
84                 {
85                         "ok": self.ok,
86                         "cancel": self.cancel,
87                 }, -2)
88                 
89                 self.skin = WlanConfiguration.skin
90                 
91                 self.list = []
92                 self.iface = iface
93                 
94                 ConfigListScreen.__init__(self, self.list)
95                 self.createSetup()
96         
97         def createSetup(self):
98                 #InitNetwork()
99                 n = Network(self.iface)
100                 n.loadNetworkConfig()
101         
102                 self.list = [ ]
103                 
104                 self.list.append(getConfigListEntry(_("Wireless Network Adapter"), config.plugins.wlan.enabled))
105                 if config.plugins.wlan.enabled.value:
106                         
107                         self.list.append(getConfigListEntry(_("Interface"), config.plugins.wlan.interface))                     
108                         self.list.append(getConfigListEntry(_("Network SSID"), config.plugins.wlan.essid))
109                         self.list.append(getConfigListEntry(_("Encryption"), config.plugins.wlan.encryption.enabled))
110                         if config.plugins.wlan.encryption.enabled.value:
111                                 self.list.append(getConfigListEntry(_("Encryption Type"), config.plugins.wlan.encryption.type))
112                                 self.list.append(getConfigListEntry(_("Encryption Key"), config.plugins.wlan.encryption.psk))
113                         
114                         self.dhcpEntry = getConfigListEntry(_("Use DHCP"), config.network.dhcp)
115                         self.list.append(self.dhcpEntry)
116                         
117                         if not config.network.dhcp.value:
118                                 self.list.append(getConfigListEntry(_("IP Address"), config.network.ip))
119                                 self.list.append(getConfigListEntry(_("Netmask"), config.network.netmask))
120                                 self.list.append(getConfigListEntry(_("Nameserver"), config.network.dns))
121                                 self.list.append(getConfigListEntry(_("Gateway"), config.network.gateway))
122                 
123                 self["config"].list = self.list
124                 self["config"].l.setList(self.list)
125         
126         def newConfig(self):
127                 print self["config"].getCurrent()
128                 if self["config"].getCurrent() == self.dhcpEntry:
129                         self.createSetup()
130
131         def keyLeft(self):
132                 ConfigListScreen.keyLeft(self)
133                 self.createSetup()
134
135         def keyRight(self):
136                 ConfigListScreen.keyRight(self)
137                 self.createSetup()
138
139         def ok(self):
140                 self.ws.writeConfig()
141                 self.ws.restart(self.iface)
142                 n = Network(self.iface)
143                 n.writeNetworkConfig()
144                 n.activateNetworkConfig()
145                 self.close()
146
147         def cancel(self):
148                 self.close()
149
150 def EntryChoosed(parms):
151         if parms[0]:
152                 session = parms[0]
153                 if parms[1] is not None:
154                         val = parms[1]
155                         essid = val[0]
156                         encrypted = val[2]
157                         iface = val[3]
158                         session.open(WlanConfiguration, essid, encrypted, iface)
159                 else:
160                         session.open(WlanConfiguration)
161
162 def WlanSelectionMain(session, **kwargs):
163         session.openWithCallback(EntryChoosed, WlanSelection)
164
165 def WlanConfigurationMain(session, **kwargs):
166         session.open(WlanConfiguration)
167         
168 def Plugins(**kwargs):
169         return PluginDescriptor(name=_("Wireless LAN"), description=_("Connect to a Wireless Network"), where = PluginDescriptor.WHERE_PLUGINMENU, fnc=WlanSelectionMain)
170