-rw-r--r-- | noncore/net/networksetup/interfaces.cpp | 42 | ||||
-rw-r--r-- | noncore/net/networksetup/interfaces.h | 1 | ||||
-rw-r--r-- | noncore/net/networksetup/interfacesetupimp.cpp | 21 | ||||
-rw-r--r-- | noncore/net/networksetup/mainwindowimp.cpp | 12 | ||||
-rw-r--r-- | noncore/settings/networksettings/interfaces.cpp | 42 | ||||
-rw-r--r-- | noncore/settings/networksettings/interfaces.h | 1 | ||||
-rw-r--r-- | noncore/settings/networksettings/interfacesetupimp.cpp | 21 | ||||
-rw-r--r-- | noncore/settings/networksettings/mainwindowimp.cpp | 12 |
8 files changed, 128 insertions, 24 deletions
diff --git a/noncore/net/networksetup/interfaces.cpp b/noncore/net/networksetup/interfaces.cpp index eef42df..0927258 100644 --- a/noncore/net/networksetup/interfaces.cpp +++ b/noncore/net/networksetup/interfaces.cpp @@ -122,96 +122,122 @@ bool Interfaces::setAuto(QString interface, bool setAuto){ else{ qDebug(QString("Interfaces: Can't set interface %1 auto to false sense it is already false.").arg(interface).latin1()); } } return true; } /** * Set the current interface to interface. This needs to be done before you * can call getFamily(), getMethod, and get/setOption(). * @param interface the name of the interface to set. All whitespace is * removed from the interface name. * @return bool true if it is successfull. */ bool Interfaces::setInterface(QString interface){ interface = interface.simplifyWhiteSpace(); interface = interface.replace(QRegExp(" "), ""); return setStanza(IFACE, interface, currentIface); } /** * A quick helper funtion to see if the current interface is set. * @return bool true if set, false otherwise. */ bool Interfaces::isInterfaceSet(){ return (currentIface != interfaces.end()); } /** * Add a new interface of with the settings - family and method * @param interface the name of the interface to set. All whitespace is * removed from the interface name. * @param family the family of this interface inet or inet, ipx or inet6 * Must of one of the families defined in interfaces.h * @param method for the family. see interfaces man page for family methods. * @return true if successfull. */ bool Interfaces::addInterface(QString interface, QString family, QString method){ if(acceptedFamily.contains(family)==0) return false; interface = interface.simplifyWhiteSpace(); interface = interface.replace(QRegExp(" "), ""); interfaces.append(""); interfaces.append(QString(IFACE " %1 %2 %3").arg(interface).arg(family).arg(method)); return true; } /** + * Copies interface with name interface to name newInterface + * @param newInterface name of the new interface. + * @return bool true if successfull + */ +bool Interfaces::copyInterface(QString interface, QString newInterface){ + if(!setInterface(interface)) return false; + + QStringList::Iterator it = currentIface; + it++; + + bool error; + addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error)); + if(!setInterface(newInterface)) return false; + QStringList::Iterator newIface = currentIface; + newIface++; + + for ( it; it != interfaces.end(); ++it ){ + if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO))) + break; + newIface = interfaces.insert(newIface, *it); + } + + return true; +} + +/** * Remove the currently selected interface and all of its options. * @return bool if successfull or not. */ bool Interfaces::removeInterface(){ if(currentIface == interfaces.end()) return false; (*currentIface) = ""; return removeAllInterfaceOptions(); } /** * Gets the hardware name of the interface that is currently selected. * @return QString name of the hardware interface (eth0, usb2, wlan1...). * @param error set to true if any error occurs, false otherwise. */ QString Interfaces::getInterfaceName(bool &error){ if(currentIface == interfaces.end()){ error = true; return QString(); } QString line = (*currentIface); line = line.mid(QString(IFACE).length() +1, line.length()); line = line.simplifyWhiteSpace(); int findSpace = line.find(" "); if( findSpace < 0){ error = true; return QString(); } error = false; return line.mid(0, findSpace); } /** * Gets the family name of the interface that is currently selected. * @return QString name of the family (inet, inet6, ipx). * @param error set to true if any error occurs, false otherwise. */ QString Interfaces::getInterfaceFamily(bool &error){ QString name = getInterfaceName(error); if(error){ error = true; return QString(); } QString line = (*currentIface); line = line.mid(QString(IFACE).length() +1, line.length()); line = line.mid(name.length()+1, line.length()); line = line.simplifyWhiteSpace(); int findSpace = line.find(" "); @@ -356,104 +382,118 @@ void Interfaces::addMapping(QString option){ * @param value value to go with map * @return bool true if it is successfull. */ bool Interfaces::setMap(QString map, QString value){ return setOption(currentMapping, map, value); } /** * Get a map value within a mapping. * @param map map to get value of * @param bool true if it is successfull. * @return value that goes to the map */ QString Interfaces::getMap(QString map, bool &error){ return getOption(currentMapping, map, error); } /** * Sets a script value of the current mapping to argument. * @param argument the script name. * @return true if successfull. */ bool Interfaces::setScript(QString argument){ return setOption(currentMapping, "script", argument); } /** * @param error true if could not retrieve the current script argument. * @return QString the argument of the script for the current mapping. */ QString Interfaces::getScript(bool &error){ return getOption(currentMapping, "script", error); } /** * Helper function used to parse through the QStringList and put pointers in * the correct place. * @param stanza The stanza (auto, iface, mapping) to look for. * @param option string that must be in the stanza's main line. * @param interator interator to place at location of stanza if successfull. * @return bool true if the stanza is found. */ bool Interfaces::setStanza(QString stanza, QString option, QStringList::Iterator &iterator){ bool found = false; iterator = interfaces.end(); for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { QString line = (*it).simplifyWhiteSpace(); if(line.contains(stanza) && line.contains(option)){ + uint point = line.find(option); + bool valid = true; + if(point > 0){ + // There are more chars in the line. check +1 + if(line.at(point-1) != ' ') + valid = false; + } + point += option.length(); + if(point < line.length()-1){ + // There are more chars in the line. check -1 + if(line.at(point) != ' ') + valid = false; + } + if(valid){ if(found == true){ qDebug(QString("Interfaces: Found multiple stanza's for search: %1 %2").arg(stanza).arg(option).latin1()); } - qDebug("Found"); found = true; iterator = it; } } + } return found; } /** * Sets a value of an option in a stanza * @param start the start of the stanza * @param option the option to use when setting value. * @return bool true if successfull, false otherwise. */ bool Interfaces::setOption(QStringList::Iterator start, QString option, QString value){ if(start == interfaces.end()) return false; bool found = false; for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ if(!found && value != ""){ // Got to the end of the stanza without finding it, so append it. interfaces.insert(--it, QString("\t%1 %2").arg(option).arg(value)); } found = true; break; } if((*it).contains(option) && it != start){ // Found it in stanza so replace it. if(found) qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); found = true; if(value == "") (*it) = ""; else (*it) = QString("\t%1 %2").arg(option).arg(value); } } if(!found){ QStringList::Iterator p = start; interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value)); found = true; } return found; } /** * Removes all options in a stanza * @param start the start of the stanza * @return bool true if successfull, false otherwise. */ bool Interfaces::removeAllOptions(QStringList::Iterator start){ diff --git a/noncore/net/networksetup/interfaces.h b/noncore/net/networksetup/interfaces.h index e617c17..7cf04f0 100644 --- a/noncore/net/networksetup/interfaces.h +++ b/noncore/net/networksetup/interfaces.h @@ -1,71 +1,72 @@ #ifndef INTERFACES_H #define INTERFACES_H #include <qstring.h> #include <qstringlist.h> #define INTERFACES_LOOPBACK "loopback" #define INTERFACES_FAMILY_INET "inet" #define INTERFACES_FAMILY_IPX "ipx" #define INTERFACES_FAMILY_INET6 "inet6" #define INTERFACES_METHOD_DHCP "dhcp" #define INTERFACES_METHOD_STATIC "static" #define INTERFACES_METHOD_PPP "ppp" /** * This class provides a clean frontend for parsing the network interfaces file. * It provides helper functions to minipulate the options within the file. * See the interfaces man page for the syntax rules. */ class Interfaces { public: Interfaces(QString useInterfacesFile = "/etc/network/interfaces"); QStringList getInterfaceList(); bool isAuto(QString interface); bool setAuto(QString interface, bool setAuto); bool removeInterface(); bool addInterface(QString interface, QString family, QString method); + bool copyInterface(QString oldInterface, QString newInterface); bool setInterface(QString interface); bool isInterfaceSet(); QString getInterfaceName(bool &error); bool setInterfaceName(QString newName); QString getInterfaceFamily(bool &error); bool setInterfaceFamily(QString newName); QString getInterfaceMethod(bool &error); bool setInterfaceMethod(QString newName); QString getInterfaceOption(QString option, bool &error); bool setInterfaceOption(QString option, QString value); bool removeAllInterfaceOptions(); bool setMapping(QString interface); void addMapping(QString options); bool setMap(QString map, QString value); QString getMap(QString map, bool &error); bool setScript(QString); QString getScript(bool &error); bool write(); private: bool setStanza(QString stanza, QString option,QStringList::Iterator &iterator); bool setOption(QStringList::Iterator start, QString option, QString value); QString getOption(QStringList::Iterator start, QString option, bool &error); bool removeAllOptions(QStringList::Iterator start); QString interfacesFile; QStringList interfaces; QStringList::Iterator currentIface; QStringList::Iterator currentMapping; QStringList acceptedFamily; }; #endif // interfaces diff --git a/noncore/net/networksetup/interfacesetupimp.cpp b/noncore/net/networksetup/interfacesetupimp.cpp index c16d821..bdbdfde 100644 --- a/noncore/net/networksetup/interfacesetupimp.cpp +++ b/noncore/net/networksetup/interfacesetupimp.cpp @@ -44,100 +44,105 @@ void InterfaceSetupImp::accept(){ /** * Save the settings for the current Interface. * @return bool true if successfull, false otherwise */ bool InterfaceSetupImp::saveSettings(){ // eh can't really do anything about it other then return. :-D if(!interfaces->isInterfaceSet()) return true; bool error = false; // Loopback case if(interfaces->getInterfaceMethod(error) == INTERFACES_LOOPBACK){ interfaces->setAuto(interface->getInterfaceName(), autoStart->isChecked()); return true; } if(!dhcpCheckBox->isChecked() && (ipAddressEdit->text().isEmpty() || subnetMaskEdit->text().isEmpty() || firstDNSLineEdit->text().isEmpty())){ QMessageBox::information(this, "Empy Fields.", "Please fill in address, subnet,\n gateway and the first dns entries.", "Ok"); return false; } interfaces->removeAllInterfaceOptions(); // DHCP if(dhcpCheckBox->isChecked()){ interfaces->setInterfaceMethod(INTERFACES_METHOD_DHCP); interfaces->setInterfaceOption("leasehours", QString("%1").arg(leaseTime->value())); interfaces->setInterfaceOption("leasetime", QString("%1").arg(leaseTime->value()*60*60)); } else{ interfaces->setInterfaceMethod("static"); interfaces->setInterfaceOption("address", ipAddressEdit->text()); interfaces->setInterfaceOption("netmask", subnetMaskEdit->text()); interfaces->setInterfaceOption("gateway", gatewayEdit->text()); QString dns = firstDNSLineEdit->text() + " " + secondDNSLineEdit->text(); interfaces->setInterfaceOption("up "DNSSCRIPT" add ", dns); interfaces->setInterfaceOption("down "DNSSCRIPT" remove ", dns); } // IP Information interfaces->setAuto(interface->getInterfaceName(), autoStart->isChecked()); return true; } /** * The Profile has changed. * @profile the new profile. */ void InterfaceSetupImp::setProfile(const QString &profile){ - QString newInterfaceName = interface->getInterfaceName() + profile; - + QString newInterfaceName = interface->getInterfaceName(); + if(profile.length() > 0) + newInterfaceName += "_" + profile; + qDebug( newInterfaceName.latin1()); // See if we have to make a interface. if(!interfaces->setInterface(newInterfaceName)){ - interfaces->addInterface(newInterfaceName, INTERFACES_FAMILY_INET, INTERFACES_METHOD_DHCP); - if(!interfaces->setInterface(newInterfaceName)){ - qDebug("InterfaceSetupImp: Added interface, but still can't set."); - return; - } // Add making for this new interface if need too if(profile != ""){ + interfaces->copyInterface(interface->getInterfaceName(), newInterfaceName); if(!interfaces->setMapping(interface->getInterfaceName())){ interfaces->addMapping(interface->getInterfaceName()); if(!interfaces->setMapping(interface->getInterfaceName())){ qDebug("InterfaceSetupImp: Added Mapping, but still can't set."); return; } } - interfaces->setScript("getprofile.sh"); interfaces->setMap("map", newInterfaceName); + interfaces->setScript("getprofile.sh"); + } + else{ + interfaces->addInterface(newInterfaceName, INTERFACES_FAMILY_INET, INTERFACES_METHOD_DHCP); + if(!interfaces->setInterface(newInterfaceName)){ + qDebug("InterfaceSetupImp: Added interface, but still can't set."); + return; + } } } // We must have a valid interface to get this far so read some settings. // DHCP bool error = false; if(interfaces->getInterfaceMethod(error) == INTERFACES_METHOD_DHCP) dhcpCheckBox->setChecked(true); else dhcpCheckBox->setChecked(false); leaseTime->setValue(interfaces->getInterfaceOption("leasehours", error).toInt()); if(error) leaseTime->setValue(interfaces->getInterfaceOption("leasetime", error).toInt()/60/60); if(error) leaseTime->setValue(24); // IP Information autoStart->setChecked(interfaces->isAuto(interface->getInterfaceName())); QString dns = interfaces->getInterfaceOption("up interfacednsscript add", error); if(dns.contains(" ")){ firstDNSLineEdit->setText(dns.mid(0, dns.find(" "))); secondDNSLineEdit->setText(dns.mid(dns.find(" ")+1, dns.length())); } ipAddressEdit->setText(interfaces->getInterfaceOption("address", error)); subnetMaskEdit->setText(interfaces->getInterfaceOption("netmask", error)); gatewayEdit->setText(interfaces->getInterfaceOption("gateway", error)); } // interfacesetup.cpp diff --git a/noncore/net/networksetup/mainwindowimp.cpp b/noncore/net/networksetup/mainwindowimp.cpp index 3c0af6a..9f07f0d 100644 --- a/noncore/net/networksetup/mainwindowimp.cpp +++ b/noncore/net/networksetup/mainwindowimp.cpp @@ -183,114 +183,114 @@ void MainWindowImp::addClicked(){ }
/**
* Prompt the user to see if they really want to do this.
* If they do then remove from the list and unload.
*/
void MainWindowImp::removeClicked(){
QListViewItem *item = connectionList->currentItem();
if(!item) {
QMessageBox::information(this, "Error","Please select an interface.", "Ok");
return;
}
Interface *i = interfaceItems[item];
if(i->getModuleOwner() == NULL){
QMessageBox::information(this, "Can't remove interface.", "Interface is built in.", "Ok");
}
else{
if(!i->getModuleOwner()->remove(i))
QMessageBox::information(this, "Error", "Unable to remove.", "Ok");
else{
QMessageBox::information(this, "Success", "Interface was removed.", "Ok");
// TODO memory managment....
// who deletes the interface?
}
}
}
/**
* Pull up the configure about the currently selected interface.
* Report an error if no interface is selected.
* If the interface has a module owner then request its configure with a empty
* tab. If tab is !NULL then append the interfaces setup widget to it.
*/
void MainWindowImp::configureClicked(){
QListViewItem *item = connectionList->currentItem();
if(!item){
QMessageBox::information(this, "Error","Please select an interface.", QMessageBox::Ok);
return;
}
QString currentProfile = currentProfileLabel->text();
if(profilesList->count() <= 1 || currentProfile == "All"){
currentProfile = "";
}
Interface *i = interfaceItems[item];
if(i->getModuleOwner()){
- i->getModuleOwner()->setProfile(currentProfileLabel->text());
+ i->getModuleOwner()->setProfile(currentProfile);
QTabWidget *tabWidget = NULL;
QWidget *moduleConfigure = i->getModuleOwner()->configure(&tabWidget);
if(moduleConfigure != NULL){
if(tabWidget != NULL){
InterfaceSetupImp *configure = new InterfaceSetupImp(tabWidget, "InterfaceSetupImp", i, true);
- configure->setProfile(currentProfileLabel->text());
+ configure->setProfile(currentProfile);
tabWidget->insertTab(configure, "TCP/IP");
}
moduleConfigure->showMaximized();
moduleConfigure->show();
return;
}
}
InterfaceSetupImp *configure = new InterfaceSetupImp(0, "InterfaceSetupImp", i, true);
- configure->setProfile(currentProfileLabel->text());
+ configure->setProfile(currentProfile);
configure->showMaximized();
configure->show();
}
/**
* Pull up the information about the currently selected interface.
* Report an error if no interface is selected.
* If the interface has a module owner then request its configure with a empty
* tab. If tab is !NULL then append the interfaces setup widget to it.
*/
void MainWindowImp::informationClicked(){
QListViewItem *item = connectionList->currentItem();
if(!item){
QMessageBox::information(this, "Error","Please select an interface.", QMessageBox::Ok);
return;
}
Interface *i = interfaceItems[item];
if(!i->isAttached()){
QMessageBox::information(this, "Error","No information about\na disconnected interface.", QMessageBox::Ok);
return;
}
QStringList list;
for(uint i = 0; i < profilesList->count(); i++){
list.append(profilesList->text(i));
}
if(i->getModuleOwner()){
QTabWidget *tabWidget = NULL;
QWidget *moduleInformation = i->getModuleOwner()->information(&tabWidget);
if(moduleInformation != NULL){
if(tabWidget != NULL){
InterfaceInformationImp *information = new InterfaceInformationImp(tabWidget, "InterfaceSetupImp", i, true);
tabWidget->insertTab(information, "TCP/IP");
}
moduleInformation->showMaximized();
moduleInformation->show();
return;
}
}
InterfaceInformationImp *information = new InterfaceInformationImp(0, "InterfaceSetupImp", i, true);
information->showMaximized();
information->show();
}
/**
@@ -332,106 +332,108 @@ void MainWindowImp::jobDone(KProcess *process){ int space = line.find(" ");
if(space > 1){
// We have found an interface
QString interfaceName = line.mid(0, space);
Interface *i;
// We have found an interface
//qDebug(QString("MainWindowImp: Found Interface: %1").arg(line).latin1());
// See if we already have it
if(interfaceNames.find(interfaceName) == interfaceNames.end()){
if(fileName == TEMP_ALL)
i = new Interface(this, interfaceName, false);
else
i = new Interface(this, interfaceName, true);
i->setAttached(true);
QString hardName = "Ethernet";
int hardwareName = line.find("Link encap:");
int macAddress = line.find("HWaddr");
if(macAddress == -1)
macAddress = line.length();
if(hardwareName != -1)
i->setHardwareName(line.mid(hardwareName+11, macAddress-(hardwareName+11)) );
interfaceNames.insert(i->getInterfaceName(), i);
updateInterface(i);
connect(i, SIGNAL(updateInterface(Interface *)), this, SLOT(updateInterface(Interface *)));
}
// It was an interface we already had.
else{
if(fileName != TEMP_ALL)
(interfaceNames[interfaceName])->setStatus(true);
}
}
}
file.close();
QFile::remove(fileName);
if(threads.count() == 0){
Interfaces i;
QStringList list = i.getInterfaceList();
QMap<QString, Interface*>::Iterator it;
for ( QStringList::Iterator ni = list.begin(); ni != list.end(); ++ni ) {
bool found = false;
for( it = interfaceNames.begin(); it != interfaceNames.end(); ++it ){
if(it.key() == (*ni))
found = true;
}
if(!found){
+ if(!(*ni).contains("_")){
Interface *i = new Interface(this, *ni, false);
i->setAttached(false);
i->setHardwareName("Disconnected");
interfaceNames.insert(i->getInterfaceName(), i);
updateInterface(i);
connect(i, SIGNAL(updateInterface(Interface *)), this, SLOT(updateInterface(Interface *)));
}
}
}
}
+}
/**
* Update this interface. If no QListViewItem exists create one.
* @param Interface* pointer to the interface that needs to be updated.
*/
void MainWindowImp::updateInterface(Interface *i){
if(!advancedUserMode){
if(i->getInterfaceName() == "lo")
return;
}
QListViewItem *item = NULL;
// Find the interface, making it if needed.
if(items.find(i) == items.end()){
item = new QListViewItem(connectionList, "", "", "");
// See if you can't find a module owner for this interface
QMap<Module*, QLibrary*>::Iterator it;
for( it = libraries.begin(); it != libraries.end(); ++it ){
if(it.key()->isOwner(i))
i->setModuleOwner(it.key());
}
items.insert(i, item);
interfaceItems.insert(item, i);
}
else
item = items[i];
// Update the icons and information
item->setPixmap(0, (Resource::loadPixmap(i->getStatus() ? "up": "down")));
QString typeName = "lan";
if(i->getHardwareName().contains("Local Loopback"))
typeName = "lo";
if(i->getInterfaceName().contains("irda"))
typeName = "irda";
if(i->getInterfaceName().contains("wlan"))
typeName = "wlan";
if(i->getInterfaceName().contains("usb"))
typeName = "usb";
if(!i->isAttached())
typeName = "connect_no";
// Actually try to use the Module
if(i->getModuleOwner() != NULL)
typeName = i->getModuleOwner()->getPixmapName(i);
item->setPixmap(1, (Resource::loadPixmap(typeName)));
@@ -444,82 +446,86 @@ void MainWindowImp::newProfileChanged(const QString& newText){ if(newText.length() > 0)
newProfileButton->setEnabled(true);
else
newProfileButton->setEnabled(false);
}
/**
* Adds a new profile to the list of profiles.
* Don't add profiles that already exists.
* Appends to the list and QStringList
*/
void MainWindowImp::addProfile(){
QString newProfileName = newProfile->text();
if(profiles.grep(newProfileName).count() > 0){
QMessageBox::information(this, "Can't Add","Profile already exists.", "Ok");
return;
}
profiles.append(newProfileName);
profilesList->insertItem(newProfileName);
}
/**
* Removes the currently selected profile in the combo.
* Doesn't delete if there are less then 2 profiles.
*/
void MainWindowImp::removeProfile(){
if(profilesList->count() <= 1){
QMessageBox::information(this, "Can't remove.","At least one profile\nis needed.", "Ok");
return;
}
QString profileToRemove = profilesList->currentText();
if(profileToRemove == "All"){
QMessageBox::information(this, "Can't remove.","Can't remove default.", "Ok");
return;
}
// Can't remove the curent profile
if(profileToRemove == currentProfileLabel->text()){
QMessageBox::information(this, "Can't remove.",QString("%1 is the current profile.").arg(profileToRemove), "Ok");
return;
}
if(QMessageBox::information(this, "Question",QString("Remove profile: %1").arg(profileToRemove), QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok){
profiles = QStringList::split(" ", profiles.join(" ").replace(QRegExp(profileToRemove), ""));
profilesList->clear();
for ( QStringList::Iterator it = profiles.begin(); it != profiles.end(); ++it)
profilesList->insertItem((*it));
}
+
+ // Remove any interface settings and mappings.
+ //TODO
}
/**
* A new profile has been selected, change.
* @param newProfile the new profile.
*/
void MainWindowImp::changeProfile(){
if(profilesList->currentItem() == -1){
QMessageBox::information(this, "Can't Change.","Please select a profile.", "Ok");
return;
}
QString newProfile = profilesList->text(profilesList->currentItem());
if(newProfile != currentProfileLabel->text()){
currentProfileLabel->setText(newProfile);
+ QFile::remove(SCHEME);
QFile file(SCHEME);
if ( file.open(IO_ReadWrite) ) {
QTextStream stream( &file );
stream << QString("SCHEME=%1").arg(newProfile);
file.close();
}
// restart all up devices?
if(QMessageBox::information(this, "Question","Restart all running interfaces?", QMessageBox::Ok, QMessageBox::No) == QMessageBox::Ok){
// Go through them one by one
QMap<Interface*, QListViewItem*>::Iterator it;
for( it = items.begin(); it != items.end(); ++it ){
if(it.key()->getStatus() == true)
it.key()->restart();
}
}
}
}
// mainwindowimp.cpp
diff --git a/noncore/settings/networksettings/interfaces.cpp b/noncore/settings/networksettings/interfaces.cpp index eef42df..0927258 100644 --- a/noncore/settings/networksettings/interfaces.cpp +++ b/noncore/settings/networksettings/interfaces.cpp @@ -122,96 +122,122 @@ bool Interfaces::setAuto(QString interface, bool setAuto){ else{ qDebug(QString("Interfaces: Can't set interface %1 auto to false sense it is already false.").arg(interface).latin1()); } } return true; } /** * Set the current interface to interface. This needs to be done before you * can call getFamily(), getMethod, and get/setOption(). * @param interface the name of the interface to set. All whitespace is * removed from the interface name. * @return bool true if it is successfull. */ bool Interfaces::setInterface(QString interface){ interface = interface.simplifyWhiteSpace(); interface = interface.replace(QRegExp(" "), ""); return setStanza(IFACE, interface, currentIface); } /** * A quick helper funtion to see if the current interface is set. * @return bool true if set, false otherwise. */ bool Interfaces::isInterfaceSet(){ return (currentIface != interfaces.end()); } /** * Add a new interface of with the settings - family and method * @param interface the name of the interface to set. All whitespace is * removed from the interface name. * @param family the family of this interface inet or inet, ipx or inet6 * Must of one of the families defined in interfaces.h * @param method for the family. see interfaces man page for family methods. * @return true if successfull. */ bool Interfaces::addInterface(QString interface, QString family, QString method){ if(acceptedFamily.contains(family)==0) return false; interface = interface.simplifyWhiteSpace(); interface = interface.replace(QRegExp(" "), ""); interfaces.append(""); interfaces.append(QString(IFACE " %1 %2 %3").arg(interface).arg(family).arg(method)); return true; } /** + * Copies interface with name interface to name newInterface + * @param newInterface name of the new interface. + * @return bool true if successfull + */ +bool Interfaces::copyInterface(QString interface, QString newInterface){ + if(!setInterface(interface)) return false; + + QStringList::Iterator it = currentIface; + it++; + + bool error; + addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error)); + if(!setInterface(newInterface)) return false; + QStringList::Iterator newIface = currentIface; + newIface++; + + for ( it; it != interfaces.end(); ++it ){ + if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO))) + break; + newIface = interfaces.insert(newIface, *it); + } + + return true; +} + +/** * Remove the currently selected interface and all of its options. * @return bool if successfull or not. */ bool Interfaces::removeInterface(){ if(currentIface == interfaces.end()) return false; (*currentIface) = ""; return removeAllInterfaceOptions(); } /** * Gets the hardware name of the interface that is currently selected. * @return QString name of the hardware interface (eth0, usb2, wlan1...). * @param error set to true if any error occurs, false otherwise. */ QString Interfaces::getInterfaceName(bool &error){ if(currentIface == interfaces.end()){ error = true; return QString(); } QString line = (*currentIface); line = line.mid(QString(IFACE).length() +1, line.length()); line = line.simplifyWhiteSpace(); int findSpace = line.find(" "); if( findSpace < 0){ error = true; return QString(); } error = false; return line.mid(0, findSpace); } /** * Gets the family name of the interface that is currently selected. * @return QString name of the family (inet, inet6, ipx). * @param error set to true if any error occurs, false otherwise. */ QString Interfaces::getInterfaceFamily(bool &error){ QString name = getInterfaceName(error); if(error){ error = true; return QString(); } QString line = (*currentIface); line = line.mid(QString(IFACE).length() +1, line.length()); line = line.mid(name.length()+1, line.length()); line = line.simplifyWhiteSpace(); int findSpace = line.find(" "); @@ -356,104 +382,118 @@ void Interfaces::addMapping(QString option){ * @param value value to go with map * @return bool true if it is successfull. */ bool Interfaces::setMap(QString map, QString value){ return setOption(currentMapping, map, value); } /** * Get a map value within a mapping. * @param map map to get value of * @param bool true if it is successfull. * @return value that goes to the map */ QString Interfaces::getMap(QString map, bool &error){ return getOption(currentMapping, map, error); } /** * Sets a script value of the current mapping to argument. * @param argument the script name. * @return true if successfull. */ bool Interfaces::setScript(QString argument){ return setOption(currentMapping, "script", argument); } /** * @param error true if could not retrieve the current script argument. * @return QString the argument of the script for the current mapping. */ QString Interfaces::getScript(bool &error){ return getOption(currentMapping, "script", error); } /** * Helper function used to parse through the QStringList and put pointers in * the correct place. * @param stanza The stanza (auto, iface, mapping) to look for. * @param option string that must be in the stanza's main line. * @param interator interator to place at location of stanza if successfull. * @return bool true if the stanza is found. */ bool Interfaces::setStanza(QString stanza, QString option, QStringList::Iterator &iterator){ bool found = false; iterator = interfaces.end(); for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { QString line = (*it).simplifyWhiteSpace(); if(line.contains(stanza) && line.contains(option)){ + uint point = line.find(option); + bool valid = true; + if(point > 0){ + // There are more chars in the line. check +1 + if(line.at(point-1) != ' ') + valid = false; + } + point += option.length(); + if(point < line.length()-1){ + // There are more chars in the line. check -1 + if(line.at(point) != ' ') + valid = false; + } + if(valid){ if(found == true){ qDebug(QString("Interfaces: Found multiple stanza's for search: %1 %2").arg(stanza).arg(option).latin1()); } - qDebug("Found"); found = true; iterator = it; } } + } return found; } /** * Sets a value of an option in a stanza * @param start the start of the stanza * @param option the option to use when setting value. * @return bool true if successfull, false otherwise. */ bool Interfaces::setOption(QStringList::Iterator start, QString option, QString value){ if(start == interfaces.end()) return false; bool found = false; for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ if(!found && value != ""){ // Got to the end of the stanza without finding it, so append it. interfaces.insert(--it, QString("\t%1 %2").arg(option).arg(value)); } found = true; break; } if((*it).contains(option) && it != start){ // Found it in stanza so replace it. if(found) qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); found = true; if(value == "") (*it) = ""; else (*it) = QString("\t%1 %2").arg(option).arg(value); } } if(!found){ QStringList::Iterator p = start; interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value)); found = true; } return found; } /** * Removes all options in a stanza * @param start the start of the stanza * @return bool true if successfull, false otherwise. */ bool Interfaces::removeAllOptions(QStringList::Iterator start){ diff --git a/noncore/settings/networksettings/interfaces.h b/noncore/settings/networksettings/interfaces.h index e617c17..7cf04f0 100644 --- a/noncore/settings/networksettings/interfaces.h +++ b/noncore/settings/networksettings/interfaces.h @@ -1,71 +1,72 @@ #ifndef INTERFACES_H #define INTERFACES_H #include <qstring.h> #include <qstringlist.h> #define INTERFACES_LOOPBACK "loopback" #define INTERFACES_FAMILY_INET "inet" #define INTERFACES_FAMILY_IPX "ipx" #define INTERFACES_FAMILY_INET6 "inet6" #define INTERFACES_METHOD_DHCP "dhcp" #define INTERFACES_METHOD_STATIC "static" #define INTERFACES_METHOD_PPP "ppp" /** * This class provides a clean frontend for parsing the network interfaces file. * It provides helper functions to minipulate the options within the file. * See the interfaces man page for the syntax rules. */ class Interfaces { public: Interfaces(QString useInterfacesFile = "/etc/network/interfaces"); QStringList getInterfaceList(); bool isAuto(QString interface); bool setAuto(QString interface, bool setAuto); bool removeInterface(); bool addInterface(QString interface, QString family, QString method); + bool copyInterface(QString oldInterface, QString newInterface); bool setInterface(QString interface); bool isInterfaceSet(); QString getInterfaceName(bool &error); bool setInterfaceName(QString newName); QString getInterfaceFamily(bool &error); bool setInterfaceFamily(QString newName); QString getInterfaceMethod(bool &error); bool setInterfaceMethod(QString newName); QString getInterfaceOption(QString option, bool &error); bool setInterfaceOption(QString option, QString value); bool removeAllInterfaceOptions(); bool setMapping(QString interface); void addMapping(QString options); bool setMap(QString map, QString value); QString getMap(QString map, bool &error); bool setScript(QString); QString getScript(bool &error); bool write(); private: bool setStanza(QString stanza, QString option,QStringList::Iterator &iterator); bool setOption(QStringList::Iterator start, QString option, QString value); QString getOption(QStringList::Iterator start, QString option, bool &error); bool removeAllOptions(QStringList::Iterator start); QString interfacesFile; QStringList interfaces; QStringList::Iterator currentIface; QStringList::Iterator currentMapping; QStringList acceptedFamily; }; #endif // interfaces diff --git a/noncore/settings/networksettings/interfacesetupimp.cpp b/noncore/settings/networksettings/interfacesetupimp.cpp index c16d821..bdbdfde 100644 --- a/noncore/settings/networksettings/interfacesetupimp.cpp +++ b/noncore/settings/networksettings/interfacesetupimp.cpp @@ -44,100 +44,105 @@ void InterfaceSetupImp::accept(){ /** * Save the settings for the current Interface. * @return bool true if successfull, false otherwise */ bool InterfaceSetupImp::saveSettings(){ // eh can't really do anything about it other then return. :-D if(!interfaces->isInterfaceSet()) return true; bool error = false; // Loopback case if(interfaces->getInterfaceMethod(error) == INTERFACES_LOOPBACK){ interfaces->setAuto(interface->getInterfaceName(), autoStart->isChecked()); return true; } if(!dhcpCheckBox->isChecked() && (ipAddressEdit->text().isEmpty() || subnetMaskEdit->text().isEmpty() || firstDNSLineEdit->text().isEmpty())){ QMessageBox::information(this, "Empy Fields.", "Please fill in address, subnet,\n gateway and the first dns entries.", "Ok"); return false; } interfaces->removeAllInterfaceOptions(); // DHCP if(dhcpCheckBox->isChecked()){ interfaces->setInterfaceMethod(INTERFACES_METHOD_DHCP); interfaces->setInterfaceOption("leasehours", QString("%1").arg(leaseTime->value())); interfaces->setInterfaceOption("leasetime", QString("%1").arg(leaseTime->value()*60*60)); } else{ interfaces->setInterfaceMethod("static"); interfaces->setInterfaceOption("address", ipAddressEdit->text()); interfaces->setInterfaceOption("netmask", subnetMaskEdit->text()); interfaces->setInterfaceOption("gateway", gatewayEdit->text()); QString dns = firstDNSLineEdit->text() + " " + secondDNSLineEdit->text(); interfaces->setInterfaceOption("up "DNSSCRIPT" add ", dns); interfaces->setInterfaceOption("down "DNSSCRIPT" remove ", dns); } // IP Information interfaces->setAuto(interface->getInterfaceName(), autoStart->isChecked()); return true; } /** * The Profile has changed. * @profile the new profile. */ void InterfaceSetupImp::setProfile(const QString &profile){ - QString newInterfaceName = interface->getInterfaceName() + profile; - + QString newInterfaceName = interface->getInterfaceName(); + if(profile.length() > 0) + newInterfaceName += "_" + profile; + qDebug( newInterfaceName.latin1()); // See if we have to make a interface. if(!interfaces->setInterface(newInterfaceName)){ - interfaces->addInterface(newInterfaceName, INTERFACES_FAMILY_INET, INTERFACES_METHOD_DHCP); - if(!interfaces->setInterface(newInterfaceName)){ - qDebug("InterfaceSetupImp: Added interface, but still can't set."); - return; - } // Add making for this new interface if need too if(profile != ""){ + interfaces->copyInterface(interface->getInterfaceName(), newInterfaceName); if(!interfaces->setMapping(interface->getInterfaceName())){ interfaces->addMapping(interface->getInterfaceName()); if(!interfaces->setMapping(interface->getInterfaceName())){ qDebug("InterfaceSetupImp: Added Mapping, but still can't set."); return; } } - interfaces->setScript("getprofile.sh"); interfaces->setMap("map", newInterfaceName); + interfaces->setScript("getprofile.sh"); + } + else{ + interfaces->addInterface(newInterfaceName, INTERFACES_FAMILY_INET, INTERFACES_METHOD_DHCP); + if(!interfaces->setInterface(newInterfaceName)){ + qDebug("InterfaceSetupImp: Added interface, but still can't set."); + return; + } } } // We must have a valid interface to get this far so read some settings. // DHCP bool error = false; if(interfaces->getInterfaceMethod(error) == INTERFACES_METHOD_DHCP) dhcpCheckBox->setChecked(true); else dhcpCheckBox->setChecked(false); leaseTime->setValue(interfaces->getInterfaceOption("leasehours", error).toInt()); if(error) leaseTime->setValue(interfaces->getInterfaceOption("leasetime", error).toInt()/60/60); if(error) leaseTime->setValue(24); // IP Information autoStart->setChecked(interfaces->isAuto(interface->getInterfaceName())); QString dns = interfaces->getInterfaceOption("up interfacednsscript add", error); if(dns.contains(" ")){ firstDNSLineEdit->setText(dns.mid(0, dns.find(" "))); secondDNSLineEdit->setText(dns.mid(dns.find(" ")+1, dns.length())); } ipAddressEdit->setText(interfaces->getInterfaceOption("address", error)); subnetMaskEdit->setText(interfaces->getInterfaceOption("netmask", error)); gatewayEdit->setText(interfaces->getInterfaceOption("gateway", error)); } // interfacesetup.cpp diff --git a/noncore/settings/networksettings/mainwindowimp.cpp b/noncore/settings/networksettings/mainwindowimp.cpp index 3c0af6a..9f07f0d 100644 --- a/noncore/settings/networksettings/mainwindowimp.cpp +++ b/noncore/settings/networksettings/mainwindowimp.cpp @@ -183,114 +183,114 @@ void MainWindowImp::addClicked(){ }
/**
* Prompt the user to see if they really want to do this.
* If they do then remove from the list and unload.
*/
void MainWindowImp::removeClicked(){
QListViewItem *item = connectionList->currentItem();
if(!item) {
QMessageBox::information(this, "Error","Please select an interface.", "Ok");
return;
}
Interface *i = interfaceItems[item];
if(i->getModuleOwner() == NULL){
QMessageBox::information(this, "Can't remove interface.", "Interface is built in.", "Ok");
}
else{
if(!i->getModuleOwner()->remove(i))
QMessageBox::information(this, "Error", "Unable to remove.", "Ok");
else{
QMessageBox::information(this, "Success", "Interface was removed.", "Ok");
// TODO memory managment....
// who deletes the interface?
}
}
}
/**
* Pull up the configure about the currently selected interface.
* Report an error if no interface is selected.
* If the interface has a module owner then request its configure with a empty
* tab. If tab is !NULL then append the interfaces setup widget to it.
*/
void MainWindowImp::configureClicked(){
QListViewItem *item = connectionList->currentItem();
if(!item){
QMessageBox::information(this, "Error","Please select an interface.", QMessageBox::Ok);
return;
}
QString currentProfile = currentProfileLabel->text();
if(profilesList->count() <= 1 || currentProfile == "All"){
currentProfile = "";
}
Interface *i = interfaceItems[item];
if(i->getModuleOwner()){
- i->getModuleOwner()->setProfile(currentProfileLabel->text());
+ i->getModuleOwner()->setProfile(currentProfile);
QTabWidget *tabWidget = NULL;
QWidget *moduleConfigure = i->getModuleOwner()->configure(&tabWidget);
if(moduleConfigure != NULL){
if(tabWidget != NULL){
InterfaceSetupImp *configure = new InterfaceSetupImp(tabWidget, "InterfaceSetupImp", i, true);
- configure->setProfile(currentProfileLabel->text());
+ configure->setProfile(currentProfile);
tabWidget->insertTab(configure, "TCP/IP");
}
moduleConfigure->showMaximized();
moduleConfigure->show();
return;
}
}
InterfaceSetupImp *configure = new InterfaceSetupImp(0, "InterfaceSetupImp", i, true);
- configure->setProfile(currentProfileLabel->text());
+ configure->setProfile(currentProfile);
configure->showMaximized();
configure->show();
}
/**
* Pull up the information about the currently selected interface.
* Report an error if no interface is selected.
* If the interface has a module owner then request its configure with a empty
* tab. If tab is !NULL then append the interfaces setup widget to it.
*/
void MainWindowImp::informationClicked(){
QListViewItem *item = connectionList->currentItem();
if(!item){
QMessageBox::information(this, "Error","Please select an interface.", QMessageBox::Ok);
return;
}
Interface *i = interfaceItems[item];
if(!i->isAttached()){
QMessageBox::information(this, "Error","No information about\na disconnected interface.", QMessageBox::Ok);
return;
}
QStringList list;
for(uint i = 0; i < profilesList->count(); i++){
list.append(profilesList->text(i));
}
if(i->getModuleOwner()){
QTabWidget *tabWidget = NULL;
QWidget *moduleInformation = i->getModuleOwner()->information(&tabWidget);
if(moduleInformation != NULL){
if(tabWidget != NULL){
InterfaceInformationImp *information = new InterfaceInformationImp(tabWidget, "InterfaceSetupImp", i, true);
tabWidget->insertTab(information, "TCP/IP");
}
moduleInformation->showMaximized();
moduleInformation->show();
return;
}
}
InterfaceInformationImp *information = new InterfaceInformationImp(0, "InterfaceSetupImp", i, true);
information->showMaximized();
information->show();
}
/**
@@ -332,106 +332,108 @@ void MainWindowImp::jobDone(KProcess *process){ int space = line.find(" ");
if(space > 1){
// We have found an interface
QString interfaceName = line.mid(0, space);
Interface *i;
// We have found an interface
//qDebug(QString("MainWindowImp: Found Interface: %1").arg(line).latin1());
// See if we already have it
if(interfaceNames.find(interfaceName) == interfaceNames.end()){
if(fileName == TEMP_ALL)
i = new Interface(this, interfaceName, false);
else
i = new Interface(this, interfaceName, true);
i->setAttached(true);
QString hardName = "Ethernet";
int hardwareName = line.find("Link encap:");
int macAddress = line.find("HWaddr");
if(macAddress == -1)
macAddress = line.length();
if(hardwareName != -1)
i->setHardwareName(line.mid(hardwareName+11, macAddress-(hardwareName+11)) );
interfaceNames.insert(i->getInterfaceName(), i);
updateInterface(i);
connect(i, SIGNAL(updateInterface(Interface *)), this, SLOT(updateInterface(Interface *)));
}
// It was an interface we already had.
else{
if(fileName != TEMP_ALL)
(interfaceNames[interfaceName])->setStatus(true);
}
}
}
file.close();
QFile::remove(fileName);
if(threads.count() == 0){
Interfaces i;
QStringList list = i.getInterfaceList();
QMap<QString, Interface*>::Iterator it;
for ( QStringList::Iterator ni = list.begin(); ni != list.end(); ++ni ) {
bool found = false;
for( it = interfaceNames.begin(); it != interfaceNames.end(); ++it ){
if(it.key() == (*ni))
found = true;
}
if(!found){
+ if(!(*ni).contains("_")){
Interface *i = new Interface(this, *ni, false);
i->setAttached(false);
i->setHardwareName("Disconnected");
interfaceNames.insert(i->getInterfaceName(), i);
updateInterface(i);
connect(i, SIGNAL(updateInterface(Interface *)), this, SLOT(updateInterface(Interface *)));
}
}
}
}
+}
/**
* Update this interface. If no QListViewItem exists create one.
* @param Interface* pointer to the interface that needs to be updated.
*/
void MainWindowImp::updateInterface(Interface *i){
if(!advancedUserMode){
if(i->getInterfaceName() == "lo")
return;
}
QListViewItem *item = NULL;
// Find the interface, making it if needed.
if(items.find(i) == items.end()){
item = new QListViewItem(connectionList, "", "", "");
// See if you can't find a module owner for this interface
QMap<Module*, QLibrary*>::Iterator it;
for( it = libraries.begin(); it != libraries.end(); ++it ){
if(it.key()->isOwner(i))
i->setModuleOwner(it.key());
}
items.insert(i, item);
interfaceItems.insert(item, i);
}
else
item = items[i];
// Update the icons and information
item->setPixmap(0, (Resource::loadPixmap(i->getStatus() ? "up": "down")));
QString typeName = "lan";
if(i->getHardwareName().contains("Local Loopback"))
typeName = "lo";
if(i->getInterfaceName().contains("irda"))
typeName = "irda";
if(i->getInterfaceName().contains("wlan"))
typeName = "wlan";
if(i->getInterfaceName().contains("usb"))
typeName = "usb";
if(!i->isAttached())
typeName = "connect_no";
// Actually try to use the Module
if(i->getModuleOwner() != NULL)
typeName = i->getModuleOwner()->getPixmapName(i);
item->setPixmap(1, (Resource::loadPixmap(typeName)));
@@ -444,82 +446,86 @@ void MainWindowImp::newProfileChanged(const QString& newText){ if(newText.length() > 0)
newProfileButton->setEnabled(true);
else
newProfileButton->setEnabled(false);
}
/**
* Adds a new profile to the list of profiles.
* Don't add profiles that already exists.
* Appends to the list and QStringList
*/
void MainWindowImp::addProfile(){
QString newProfileName = newProfile->text();
if(profiles.grep(newProfileName).count() > 0){
QMessageBox::information(this, "Can't Add","Profile already exists.", "Ok");
return;
}
profiles.append(newProfileName);
profilesList->insertItem(newProfileName);
}
/**
* Removes the currently selected profile in the combo.
* Doesn't delete if there are less then 2 profiles.
*/
void MainWindowImp::removeProfile(){
if(profilesList->count() <= 1){
QMessageBox::information(this, "Can't remove.","At least one profile\nis needed.", "Ok");
return;
}
QString profileToRemove = profilesList->currentText();
if(profileToRemove == "All"){
QMessageBox::information(this, "Can't remove.","Can't remove default.", "Ok");
return;
}
// Can't remove the curent profile
if(profileToRemove == currentProfileLabel->text()){
QMessageBox::information(this, "Can't remove.",QString("%1 is the current profile.").arg(profileToRemove), "Ok");
return;
}
if(QMessageBox::information(this, "Question",QString("Remove profile: %1").arg(profileToRemove), QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok){
profiles = QStringList::split(" ", profiles.join(" ").replace(QRegExp(profileToRemove), ""));
profilesList->clear();
for ( QStringList::Iterator it = profiles.begin(); it != profiles.end(); ++it)
profilesList->insertItem((*it));
}
+
+ // Remove any interface settings and mappings.
+ //TODO
}
/**
* A new profile has been selected, change.
* @param newProfile the new profile.
*/
void MainWindowImp::changeProfile(){
if(profilesList->currentItem() == -1){
QMessageBox::information(this, "Can't Change.","Please select a profile.", "Ok");
return;
}
QString newProfile = profilesList->text(profilesList->currentItem());
if(newProfile != currentProfileLabel->text()){
currentProfileLabel->setText(newProfile);
+ QFile::remove(SCHEME);
QFile file(SCHEME);
if ( file.open(IO_ReadWrite) ) {
QTextStream stream( &file );
stream << QString("SCHEME=%1").arg(newProfile);
file.close();
}
// restart all up devices?
if(QMessageBox::information(this, "Question","Restart all running interfaces?", QMessageBox::Ok, QMessageBox::No) == QMessageBox::Ok){
// Go through them one by one
QMap<Interface*, QListViewItem*>::Iterator it;
for( it = items.begin(); it != items.end(); ++it ){
if(it.key()->getStatus() == true)
it.key()->restart();
}
}
}
}
// mainwindowimp.cpp
|