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) (show whitespace changes)
-rw-r--r--noncore/settings/networksettings/interfaces/interfaces.cpp54
1 files changed, 44 insertions, 10 deletions
diff --git a/noncore/settings/networksettings/interfaces/interfaces.cpp b/noncore/settings/networksettings/interfaces/interfaces.cpp
index 71d0cf5..436e449 100644
--- a/noncore/settings/networksettings/interfaces/interfaces.cpp
+++ b/noncore/settings/networksettings/interfaces/interfaces.cpp
@@ -1,26 +1,27 @@
#include "interfaces.h"
+#include <qcheckbox.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
* the \n for interfaces variable.
* @param useInterfacesFile if an interface file other then the default is
* desired to be used it should be passed in.
*/
Interfaces::Interfaces(QString useInterfacesFile){
acceptedFamily.append(INTERFACES_FAMILY_INET);
acceptedFamily.append(INTERFACES_FAMILY_IPX);
acceptedFamily.append(INTERFACES_FAMILY_INET6);
interfacesFile = useInterfacesFile;
QFile file(interfacesFile);
if (!file.open(IO_ReadOnly)){
qDebug("Interfaces: Can't open file: %s for reading.", interfacesFile.latin1() );
@@ -130,63 +131,65 @@ bool Interfaces::setAuto(const QString &interface, bool setAuto){
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() const {
return (interfaces.end() != currentIface);
}
/**
* 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(const QString &interface, const QString &family, const QString &method){
+ qDebug("Interfaces::addInterface(%s)",interface.latin1());
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;
}
/**
* 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){
+ qDebug("copy interface %s to %s", interface.latin1(), newInterface.latin1());
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;
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);
}
return true;
@@ -251,54 +254,58 @@ QString Interfaces::getInterfaceFamily(bool &error){
* @param error set to true if any error occurs, false otherwise.
*/
QString Interfaces::getInterfaceMethod(bool &error){
QString name = getInterfaceName(error);
if(error)
return QString();
QString family = getInterfaceFamily(error);
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;
return line;
}
/**
* Sets the interface name to newName.
* @param newName the new name of the interface. All whitespace is removed.
* @return bool true if successfull.
*/
bool Interfaces::setInterfaceName(const QString &newName){
+ qDebug("setInterfaceName %s", newName.latin1());
if(currentIface == interfaces.end())
return false;
QString name = newName.simplifyWhiteSpace();
name = name.replace(QRegExp(" "), "");
bool returnValue = false;
- (*currentIface) = QString("iface %1 %2 %3").arg(name).arg(getInterfaceFamily(returnValue)).arg(getInterfaceMethod(returnValue));
+ QString tmp = QString("iface %1 %2 %3").arg(name).arg(getInterfaceFamily(returnValue)).arg(getInterfaceMethod(returnValue));
+ qDebug("setting %s",tmp.latin1());
+
+ (*currentIface) = tmp;
return !returnValue;
}
/**
* Sets the interface family to newName.
* @param newName the new name of the interface. Must be one of the families
* defined in the interfaces.h file.
* @return bool true if successfull.
*/
bool Interfaces::setInterfaceFamily(const QString &newName){
if(currentIface == interfaces.end())
return false;
if(acceptedFamily.contains(newName)==0)
return false;
bool returnValue = false;
(*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(newName).arg(getInterfaceMethod(returnValue));
return !returnValue;
}
/**
* Sets the interface method to newName
* @param newName the new name of the interface
* @return bool true if successfull.
*/
@@ -306,54 +313,60 @@ bool Interfaces::setInterfaceMethod(const QString &newName){
if(currentIface == interfaces.end())
return false;
bool returnValue = false;
(*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(getInterfaceFamily(returnValue)).arg(newName);
return !returnValue;
}
/**
* Get a value for an option in the currently selected interface. For example
* calling getInterfaceOption("address") on the following stanza would
* return 192.168.1.1.
* iface eth0 static
* address 192.168.1.1
* @param option the options to get the value.
* @param error set to true if any error occurs, false otherwise.
* @return QString the options value. QString::null if error == true
*/
QString Interfaces::getInterfaceOption(const QString &option, bool &error){
return getOption(currentIface, option, error);
}
/**
* Set a value for an option in the currently selected interface. If option
* doesn't exist then it is added along with the value.
+ * If value isEmpty() then we will remove the option
+ *
* @param option the options to set the value.
* @param value the value that option should be set to.
* @param error set to true if any error occurs, false otherwise.
* @return QString the options value. QString::null if error == true
*/
bool Interfaces::setInterfaceOption(const QString &option, const QString &value){
+ if( value.stripWhiteSpace().isEmpty() )
+ return removeInterfaceOption( option );
+
+ qDebug("iface >%s< option >%s< value >%s<", (*currentIface).latin1(), option.latin1(),value.latin1());
return setOption(currentIface, option, value);
}
/**
* Removes a value for an option in the currently selected interface.
* @param option the options to set the value.
* @param error set to true if any error occurs, false otherwise.
* @return QString the options value. QString::null if error == true
*/
bool Interfaces::removeInterfaceOption(const QString &option){
return removeOption(currentIface, option);
}
/**
* Removes a value for an option in the currently selected interface.
* @param option the options to set the value.
* @param value the value that option should be set to.
* @param error set to true if any error occurs, false otherwise.
* @return QString the options value. QString::null if error == true
*/
bool Interfaces::removeInterfaceOption(const QString &option, const QString &value){
return removeOption(currentIface, option, value);
}
@@ -471,136 +484,157 @@ bool Interfaces::setStanza(const QString &stanza, const QString &option, QString
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());
}
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(const QStringList::Iterator &start, const QString &option, const QString &value){
if(start == interfaces.end())
return false;
-
+ qDebug("setting option");
bool found = false;
+ bool replaced = false;
+ QStringList::Iterator insertAt = NULL;
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));
- }
+ qDebug(" Interfaces::setOption got line >%s<",(*it).latin1());
+ // FIXME: was not completly stupid just wrong sice all options got inserted bevore the iface line
+ // but since it works with an empty interfaces file I (tille) will not do anything more
+ if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) ){
+ if (found) break;
+// && it != start){
+// if(!found && value != ""){
+// // Got to the end of the stanza without finding it, so append it.
+// qDebug(" Got to the end of the stanza without finding it, so append it.");
+// interfaces.insert(--it, QString("\t%1 %2").arg(option).arg(value));
+// }
+ qDebug("found 1");
+// interfaces.insert(++it, QString("\t%1 %2").arg(option).arg(value));
found = true;
- break;
+ insertAt = it;
+
}
if((*it).contains(option) && it != start && (*it).at(0) != '#'){
// Found it in stanza so replace it.
+ qDebug("found 2");
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;
+ replaced = true;
(*it) = QString("\t%1 %2").arg(option).arg(value);
}
}
if(!found){
+ qDebug("! found insert anyway");
QStringList::Iterator p = start;
interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value));
found = true;
}
+
+ if(found && !replaced){
+ qDebug("found iface but not the option so insert it here...");
+ interfaces.insert(++insertAt, QString("\t%1 %2").arg(option).arg(value));
+ }
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 remove
* @return bool true if successfull, false otherwise.
*/
bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option){
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){
// got to the end without finding it
break;
}
if((*it).contains(option) && it != start && (*it).at(0) != '#'){
// 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;
- (*it) = "";
+ it = interfaces.remove( it ); // we really want to remove the line
+ --it; // we do ++it later in the head of the for loop
}
}
return found;
}
/**
* 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.
*/
bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option, const 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){
// got to the end without finding it
break;
}
if((*it).contains(option) && (*it).contains(value) && it != start && (*it).at(0) != '#'){
// 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;
- (*it) = "";
+ it = interfaces.remove( it ); // we really want to remove the line
+ --it; // we do ++it later in the head of the for loop
}
}
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(const QStringList::Iterator &start){
if(start == interfaces.end())
return false;
QStringList::Iterator it = start;
it = ++it;
for (; it != interfaces.end(); ++it ) {
if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){
break;
}
it = interfaces.remove(it);
it = --it;
}
// Leave a space between this interface and the next.