TYPE_PIXMAP_ALPHATEST in eListboxMultiContent, fix alphatest
[enigma2.git] / lib / python / Components / Network.py
1 from config import *
2
3 import os
4 from socket import *
5
6 class Network:
7         def __init__(self):
8                 pass
9                 
10         def writeNetworkConfig(self):
11                 # fixme restarting and updating the network too often. possible fix: check current config and execute only if changed :/
12                 # fixme using interfaces.tmp instead of interfaces for now
13                 fp = file('/etc/network/interfaces', 'w')
14                 fp.write("auto eth0\n")
15                 if (config.network.dhcp.value == 1):
16                         fp.write("iface eth0 inet dhcp\n")
17                 else:
18                         fp.write("iface eth0 inet static\n")
19                         fp.write("      address %d.%d.%d.%d\n" % tuple(config.network.ip.value))
20                         fp.write("      netmask %d.%d.%d.%d\n" % tuple(config.network.netmask.value))
21                         fp.write("      gateway %d.%d.%d.%d\n" % tuple(config.network.gateway.value))
22                 fp.close()
23
24                 if config.network.dhcp.value == 0:
25                         fp = file('/etc/resolv.conf', 'w')
26                         fp.write("nameserver %d.%d.%d.%d\n" % tuple(config.network.dns.value))
27                         fp.close()              
28
29
30         def loadNetworkConfig(self):
31                 try:
32                         # parse the interfaces-file
33                         fp = file('/etc/network/interfaces', 'r')
34                         interfaces = fp.readlines()
35                         fp.close()
36                         
37                         ifaces = {}
38                         currif = ""
39                         for i in interfaces:
40                                 split = i.strip().split(' ')
41                                 if (split[0] == "iface"):
42                                         currif = split[1]
43                                         ifaces[currif] = {}
44                                         if (len(split) == 4 and split[3] == "dhcp"):
45                                                 ifaces[currif]["dhcp"] = "yes"
46                                         else:
47                                                 ifaces[currif]["dhcp"] = "no"
48                                 if (currif != ""):
49                                         if (split[0] == "address"):
50                                                 ifaces[currif]["address"] = map(int, split[1].split('.'))
51                                         if (split[0] == "netmask"):
52                                                 ifaces[currif]["netmask"] = map(int, split[1].split('.'))
53                                         if (split[0] == "gateway"):
54                                                 ifaces[currif]["gateway"] = map(int, split[1].split('.'))                                                                       
55                         
56                         # parse the resolv.conf-file
57                         fp = file('/etc/resolv.conf', 'r')
58                         resolv = fp.readlines()
59                         fp.close()
60                 except:
61                         pass
62                         
63                 try:
64                         for i in resolv:
65                                 split = i.strip().split(' ')
66                                 if (split[0] == "nameserver"):
67                                         config.network.nameserver.value = map(int, split[1].split('.'))
68                 except:
69                         pass
70                 
71                 try:
72                         # set this config
73                         if (ifaces.has_key("eth0")):
74                                 if (ifaces["eth0"]["dhcp"] == "yes"):
75                                         config.network.dhcp.value = 1
76                                 else:
77                                         config.network.dhcp.value = 0
78                                 if (ifaces["eth0"].has_key("address")): config.network.ip.value = ifaces["eth0"]["address"]
79                                 if (ifaces["eth0"].has_key("netmask")): config.network.netmask.value = ifaces["eth0"]["netmask"]
80                                 if (ifaces["eth0"].has_key("gateway")): config.network.gateway.value = ifaces["eth0"]["gateway"]
81                 except:
82                         pass
83
84         def activateNetworkConfig(self):
85                 import os
86                 os.system("/etc/init.d/networking restart")
87                 config.network.ip.value = self.getCurrentIP()
88                 
89         def setDHCP(self, useDHCP):
90                 if (useDHCP):
91                         print "Using DHCP"
92                         config.network.ip.enabled = False
93                         config.network.netmask.enabled = False
94                         config.network.gateway.enabled = False
95                         config.network.dns.enabled = False
96                 else:
97                         print "NOT using DHCP"
98                         config.network.ip.enabled = True
99                         config.network.netmask.enabled = True
100                         config.network.gateway.enabled = True
101                         config.network.dns.enabled = True
102                                         
103         def setIPNameserver(self, ip):
104                 return
105                 resolvconf = file('/etc/resolv.conf', 'w')
106                 resolvconf.write("nameserver %d.%d.%d.%d" % tuple(ip))
107                 resolvconf.close()
108                 
109         def setMACAddress(self, mac):
110                 #os.system("echo ifconfig eth0 ether %02x:%02x:%02x:%02x:%02x:%02x" % tuple(mac))
111                 pass
112                 
113         def setIPAddress(self, ip):
114                 pass
115                 #os.system("echo ifconfig eth0 %d.%d.%d.%d" % tuple(ip))
116                 #self.writeNetworkConfig()
117
118         def setGateway(self, ip):
119                 pass
120                 #os.system("echo route add default gw %d.%d.%d.%d" % tuple(ip))
121                 #self.writeNetworkConfig()
122                 
123         def setNetmask(self, ip):
124                 pass
125                 #os.system("echo ifconfig eth0 netmask %d.%d.%d.%d" % tuple(ip))                
126                 #self.writeNetworkConfig()              
127
128         def getCurrentIP(self):
129                 ipstr = [0,0,0,0]
130                 for x in os.popen("ifconfig eth0 | grep 'inet addr:'", "r").readline().split(' '):
131                         if x.split(':')[0] == "addr":
132                                 ipstr = x.split(':')[1].split('.')
133                 ip = []
134                 for x in ipstr:
135                         ip.append(int(x))
136                 print "[Network.py] got ip " + str(ip)
137                 return ip
138
139 iNetwork = Network()
140
141 def InitNetwork():
142         ip = iNetwork.getCurrentIP()
143
144                 
145         config.network = ConfigSubsection()
146         config.network.dhcp = configElement_nonSave("config.network.dhcp", configSelection, 1, (("no", _("no")), ("yes", _("yes"))))
147         config.network.ip = configElement_nonSave("config.network.ip", configSequence, ip, configsequencearg.get("IP"))
148         config.network.netmask = configElement_nonSave("config.network.netmask", configSequence, [255,255,255,0], configsequencearg.get("IP"))
149         config.network.gateway = configElement_nonSave("config.network.gateway", configSequence, [192,168,1,3], configsequencearg.get("IP"))
150         config.network.dns = configElement_nonSave("config.network.dns", configSequence, [192,168,1,3], configsequencearg.get("IP"))
151         config.network.mac = configElement_nonSave("config.network.mac", configSequence, [00,11,22,33,44,55], configsequencearg.get("MAC"))
152
153         iNetwork.loadNetworkConfig()
154         
155         #FIXME using this till other concept for this is implemented
156         #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes, sir", "you are my hero"))
157         #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes", "you are my hero"))
158
159
160         def writeNetworkConfig(configElement):
161                 iNetwork.writeNetworkConfig()
162                 
163         def setIPAddress(configElement):
164                 iNetwork.setIPAddress(configElement.value)
165
166         def setGateway(configElement):
167                 iNetwork.setGateway(configElement.value)
168
169         def setNetmask(configElement):
170                 iNetwork.setNetmask(configElement.value)                
171
172         def setDHCP(configElement):
173                 iNetwork.setDHCP(configElement.value)
174
175         def setIPNameserver(configElement):
176                 iNetwork.setIPNameserver(configElement.value)
177
178         def setMACAddress(configElement):
179                 iNetwork.setMACAddress(configElement.value)
180
181
182         # this will call the "setup-val" initial
183         config.network.dhcp.addNotifier(setDHCP)
184         config.network.ip.addNotifier(setIPAddress)
185         config.network.netmask.addNotifier(setNetmask)  
186         config.network.gateway.addNotifier(setGateway)
187         config.network.dns.addNotifier(setIPNameserver)
188         config.network.mac.addNotifier(setMACAddress)