summaryrefslogtreecommitdiff
path: root/noncore/net/networksetup/interfacesetupimp.cpp
Unidiff
Diffstat (limited to 'noncore/net/networksetup/interfacesetupimp.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/networksetup/interfacesetupimp.cpp147
1 files changed, 147 insertions, 0 deletions
diff --git a/noncore/net/networksetup/interfacesetupimp.cpp b/noncore/net/networksetup/interfacesetupimp.cpp
new file mode 100644
index 0000000..6a8449d
--- a/dev/null
+++ b/noncore/net/networksetup/interfacesetupimp.cpp
@@ -0,0 +1,147 @@
1#include "interfacesetupimp.h"
2#include "interface.h"
3#include "interfaces.h"
4
5#include <qcombobox.h>
6#include <qcheckbox.h>
7#include <qlineedit.h>
8#include <qspinbox.h>
9#include <qgroupbox.h>
10#include <qlabel.h>
11
12#include <qmessagebox.h>
13
14#include <assert.h>
15
16#define INTERFACE_FILE "/home/ben/interfaces"
17#define DNSSCRIPT "interfacednsscript"
18
19/**
20 * Constuctor. Set up the connection and load the first profile.
21 */
22InterfaceSetupImp::InterfaceSetupImp(QWidget* parent, const char* name, Interface *i, bool modal, WFlags fl) : InterfaceSetup(parent, name, modal, fl){
23 assert(i);
24 interface = i;
25 interfaces = new Interfaces(INTERFACE_FILE);
26 changeProfile(profileCombo->currentText());
27 bool error = false;
28 if(interfaces->getInterfaceMethod(error) == INTERFACES_LOOPBACK){
29 staticGroupBox->hide();
30 dhcpCheckBox->hide();
31 leaseTime->hide();
32 leaseHoursLabel->hide();
33 }
34 connect(profileCombo, SIGNAL(highlighted(const QString &)), this, SLOT(changeProfile(const QString &)));
35}
36
37/**
38 * Save the current settings, then write out the interfaces file and close.
39 */
40void InterfaceSetupImp::accept(){
41 if(!saveSettings())
42 return;
43 interfaces->write();
44 close(true);
45}
46
47/**
48 * Save the settings for the current Interface.
49 * @return bool true if successfull, false otherwise
50 */
51bool InterfaceSetupImp::saveSettings(){
52 // eh can't really do anything about it other then return. :-D
53 if(!interfaces->isInterfaceSet())
54 return true;
55
56 bool error = false;
57 // Loopback case
58 if(interfaces->getInterfaceMethod(error) == INTERFACES_LOOPBACK){
59 interfaces->setAuto(interface->getInterfaceName(), autoStart->isChecked());
60 return true;
61 }
62
63 if(!dhcpCheckBox->isChecked() && (ipAddressEdit->text().isEmpty() || subnetMaskEdit->text().isEmpty() || firstDNSLineEdit->text().isEmpty())){
64 QMessageBox::information(this, "Empy Fields.", "Please fill in address, subnet,\n gateway and the first dns entries.", "Ok");
65 return false;
66 }
67 interfaces->removeAllInterfaceOptions();
68
69 // DHCP
70 if(dhcpCheckBox->isChecked()){
71 interfaces->setInterfaceMethod(INTERFACES_METHOD_DHCP);
72 interfaces->setInterfaceOption("leasehours", QString("%1").arg(leaseTime->value()));
73 interfaces->setInterfaceOption("leasetime", QString("%1").arg(leaseTime->value()*60*60));
74 }
75 else{
76 interfaces->setInterfaceMethod("static");
77 interfaces->setInterfaceOption("address", ipAddressEdit->text());
78 interfaces->setInterfaceOption("netmask", subnetMaskEdit->text());
79 interfaces->setInterfaceOption("gateway", gatewayEdit->text());
80 QString dns = firstDNSLineEdit->text() + " " + secondDNSLineEdit->text();
81 interfaces->setInterfaceOption("up "DNSSCRIPT" add ", dns);
82 interfaces->setInterfaceOption("down "DNSSCRIPT" remove ", dns);
83 }
84
85 // IP Information
86 interfaces->setAuto(interface->getInterfaceName(), autoStart->isChecked());
87 return true;
88}
89
90/**
91 * The Profile has changed.
92 * @profile the new profile.
93 */
94void InterfaceSetupImp::changeProfile(const QString &profile){
95 QString newInterfaceName;
96 if(profile.lower() == "all")
97 newInterfaceName = interface->getInterfaceName();
98 else
99 newInterfaceName = interface->getInterfaceName() + "_" + profile;
100 if(newInterfaceName == currentInterfaceName)
101 return;
102 else{
103 saveSettings();
104 currentInterfaceName = newInterfaceName;
105 }
106 bool error = interfaces->setInterface(currentInterfaceName);
107
108 // See if we have to make a interface.
109 if(error){
110 qDebug("InterfaceSetupImp: Adding a new interface from profile change.");
111 interfaces->addInterface(currentInterfaceName, INTERFACES_FAMILY_INET, INTERFACES_METHOD_DHCP);
112 error = interfaces->setInterface(currentInterfaceName);
113 if(error){
114 qDebug("InterfaceSetupImp: Added interface, but still can't set.");
115 return;
116 }
117 }
118
119 //qDebug( currentInterfaceName.latin1() );
120 // We must have a valid interface to get this far so read some settings.
121
122 // DHCP
123 if(interfaces->getInterfaceMethod(error) == INTERFACES_METHOD_DHCP)
124 dhcpCheckBox->setChecked(true);
125 else
126 dhcpCheckBox->setChecked(false);
127 leaseTime->setValue(interfaces->getInterfaceOption("leasehours", error).toInt());
128 if(error)
129 leaseTime->setValue(interfaces->getInterfaceOption("leasetime", error).toInt()/60/60);
130 if(error)
131 leaseTime->setValue(24);
132
133 // IP Information
134 autoStart->setChecked(interfaces->isAuto(interface->getInterfaceName()));
135 QString dns = interfaces->getInterfaceOption("up interfacednsscript add", error);
136 if(dns.contains(" ")){
137 firstDNSLineEdit->setText(dns.mid(0, dns.find(" ")));
138 secondDNSLineEdit->setText(dns.mid(dns.find(" ")+1, dns.length()));
139 }
140 ipAddressEdit->setText(interfaces->getInterfaceOption("address", error));
141 subnetMaskEdit->setText(interfaces->getInterfaceOption("netmask", error));
142 gatewayEdit->setText(interfaces->getInterfaceOption("gateway", error));
143}
144
145
146// interfacesetup.cpp
147