summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings/interfaces/interfaces.cpp
Side-by-side diff
Diffstat (limited to 'noncore/settings/networksettings/interfaces/interfaces.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/networksettings/interfaces/interfaces.cpp77
1 files changed, 40 insertions, 37 deletions
diff --git a/noncore/settings/networksettings/interfaces/interfaces.cpp b/noncore/settings/networksettings/interfaces/interfaces.cpp
index 708f399..e49998e 100644
--- a/noncore/settings/networksettings/interfaces/interfaces.cpp
+++ b/noncore/settings/networksettings/interfaces/interfaces.cpp
@@ -1,12 +1,13 @@
#include "interfaces.h"
#include <qfile.h>
#include <qtextstream.h>
#include <qregexp.h>
+// The three stanza's
#define AUTO "auto"
#define IFACE "iface"
#define MAPPING "mapping"
/**
* Constructor. Reads in the interfaces file and then split the file up by
@@ -99,33 +100,28 @@ bool Interfaces::setAuto(const QString &interface, bool setAuto){
(*it) = (*it) += " " + interface;
// Don't care to have such thins as: auto eth0 lo usb0
(*it) = (*it).simplifyWhiteSpace();
changed = true;
break;
}
+ // else see if we need to remove from this one
else{
if((*it).contains(interface)){
(*it) = (*it).replace(QRegExp(interface), "");
- // clean up
- QString line = (*it).simplifyWhiteSpace();
- line = line.replace(QRegExp(" "),"");
- if(line == AUTO)
+ // if AUTO is the only thing left clear the line
+ if(((*it).simplifyWhiteSpace()).replace(QRegExp(" "),"") == AUTO)
(*it) = "";
changed = true;
// Don't break because we want to make sure we remove all cases.
}
}
}
}
- if(changed == false){
- if(setAuto == true)
- interfaces.append(QString(AUTO" %1").arg(interface));
- else{
- qDebug(QString("Interfaces: Can't set interface %1 auto to false sense it is already false.").arg(interface).latin1());
- }
- }
+ // In the case where there is no AUTO field add one.
+ if(!changed && setAuto)
+ interfaces.append(QString(AUTO" %1").arg(interface));
return true;
}
/**
* Set the current interface to interface. This needs to be done before you
* can call getFamily(), getMethod, and get/setOption().
@@ -154,13 +150,13 @@ bool Interfaces::isInterfaceSet(){
* @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(const QString &interface, const QString &family, const QString &method){
- if(acceptedFamily.contains(family)==0)
+ if(0 == acceptedFamily.contains(family))
return false;
QString newInterface = interface.simplifyWhiteSpace();
newInterface = newInterface.replace(QRegExp(" "), "");
interfaces.append("");
interfaces.append(QString(IFACE " %1 %2 %3").arg(newInterface).arg(family).arg(method));
return true;
@@ -169,23 +165,29 @@ bool Interfaces::addInterface(const QString &interface, const QString &family, c
/**
* Copies interface with name interface to name newInterface
* @param newInterface name of the new interface.
* @return bool true if successfull
*/
bool Interfaces::copyInterface(const QString &interface, const QString &newInterface){
- if(!setInterface(interface)) return false;
-
+ if(!setInterface(interface))
+ return false;
+
+ // Store the old interface and bump past the stanza line.
QStringList::Iterator it = currentIface;
it++;
+ // Add the new interface
bool error;
addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error));
- if(!setInterface(newInterface)) return false;
+ if(!setInterface(newInterface))
+ return false;
+
QStringList::Iterator newIface = currentIface;
newIface++;
-
+
+ // Copy all of the lines
for ( ; it != interfaces.end(); ++it ){
if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)))
break;
newIface = interfaces.insert(newIface, *it);
}
@@ -194,16 +196,13 @@ bool Interfaces::copyInterface(const QString &interface, const QString &newInter
/**
* 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();
+ return removeStanza(currentIface);
}
/**
* 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.
@@ -229,16 +228,14 @@ QString Interfaces::getInterfaceName(bool &error){
* 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;
+ if(error)
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(" ");
if( findSpace < 0){
@@ -254,21 +251,17 @@ QString Interfaces::getInterfaceFamily(bool &error){
* @return QString name of the method such as staic or dhcp.
* See the man page of interfaces for possible methods depending on the family.
* @param error set to true if any error occurs, false otherwise.
*/
QString Interfaces::getInterfaceMethod(bool &error){
QString name = getInterfaceName(error);
- if(error){
- error = true;
+ if(error)
return QString();
- }
QString family = getInterfaceFamily(error);
- if(error){
- error = true;
+ if(error)
return QString();
- }
QString line = (*currentIface);
line = line.mid(QString(IFACE).length()+1, line.length());
line = line.mid(name.length()+1, line.length());
line = line.mid(family.length()+1, line.length());
line = line.simplifyWhiteSpace();
error = false;
@@ -388,16 +381,13 @@ void Interfaces::addMapping(const QString &option){
/**
* Remove the currently selected map and all of its options.
* @return bool if successfull or not.
*/
bool Interfaces::removeMapping(){
- if(currentMapping == interfaces.end())
- return false;
- (*currentMapping) = "";
- return removeAllOptions(currentMapping);
+ return removeStanza(currentMapping);
}
/**
* Set a map option within a mapping.
* @param map map to use
* @param value value to go with map
@@ -517,12 +507,25 @@ bool Interfaces::setOption(const QStringList::Iterator &start, const QString &op
QStringList::Iterator p = start;
interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value));
found = true;
}
return found;
}
+
+/**
+ * Removes a stanza and all of its options
+ * @param stanza the stanza to remove
+ * @return bool true if successfull.
+ */
+bool Interfaces::removeStanza(QStringList::Iterator &stanza){
+ if(stanza == interfaces.end())
+ return false;
+ (*stanza) = "";
+ return removeAllOptions(stanza);
+}
+
/**
* Removes a 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.
*/
@@ -588,20 +591,20 @@ QString Interfaces::getOption(const QStringList::Iterator &start, const QString
for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) {
if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){
break;
}
if((*it).contains(option) && (*it).at(0) != '#'){
if(found)
- qDebug(QString("Interfaces: Get Options found more then one value: %1 for option: %2 in stanza %3").arg((*it)).arg(option).arg((*start)).latin1());
+ qDebug(QString("Interfaces: getOption found more then one value: %1 for option: %2 in stanza %3").arg((*it)).arg(option).arg((*start)).latin1());
found = true;
QString line = (*it).simplifyWhiteSpace();
int space = line.find(" ", option.length());
- if(space != -1)
+ if(space != -1){
value = line.mid(space+1, line.length());
- else
- qDebug(QString("Interfaces: Option %1 with no value").arg(option).latin1());
+ break;
+ }
}
}
error = !found;
return value;
}