-rw-r--r-- | noncore/settings/networksettings/interfaces/interfaces.cpp | 37 | ||||
-rw-r--r-- | noncore/settings/networksettings/interfaces/interfaces.h | 2 |
2 files changed, 39 insertions, 0 deletions
diff --git a/noncore/settings/networksettings/interfaces/interfaces.cpp b/noncore/settings/networksettings/interfaces/interfaces.cpp index 8f685fe..8d3e151 100644 --- a/noncore/settings/networksettings/interfaces/interfaces.cpp +++ b/noncore/settings/networksettings/interfaces/interfaces.cpp @@ -296,96 +296,106 @@ bool Interfaces::setInterfaceFamily(const QString &newName){ 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. */ 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. * @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){ 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); } /** * Removes all of the options from the currently selected interface. * @return bool error if if successfull */ bool Interfaces::removeAllInterfaceOptions(){ return removeAllOptions(currentIface); } /** * Set the current map to interface's map. This needs to be done before you * can call addMapping(), set/getMap(), and get/setScript(). * @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::setMapping(const QString &interface){ QString interfaceName = interface.simplifyWhiteSpace(); interfaceName = interfaceName.replace(QRegExp(" "), ""); return setStanza(MAPPING, interfaceName, currentMapping); } /** * Adds a new Mapping to the interfaces file with interfaces. * @param interface the name(s) of the interfaces to set to this mapping */ void Interfaces::addMapping(const QString &option){ interfaces.append(""); interfaces.append(QString(MAPPING " %1").arg(option)); } /** * Remove the currently selected map and all of its options. * @return bool if successfull or not. */ bool Interfaces::removeMapping(){ return removeStanza(currentMapping); } /** * Set a map option within a mapping. @@ -481,96 +491,123 @@ bool Interfaces::setStanza(const QString &stanza, const QString &option, QString * @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; 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 && (*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) = 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 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) = ""; + } + } + 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) = ""; } } 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. interfaces.insert(it, QString("")); return true; } /** diff --git a/noncore/settings/networksettings/interfaces/interfaces.h b/noncore/settings/networksettings/interfaces/interfaces.h index bac2a7e..bc9eaaa 100644 --- a/noncore/settings/networksettings/interfaces/interfaces.h +++ b/noncore/settings/networksettings/interfaces/interfaces.h @@ -1,77 +1,79 @@ #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(const QString &interface) const ; bool setAuto(const QString &interface, bool setAuto); bool removeInterface(); bool addInterface(const QString &interface, const QString &family, const QString &method); bool copyInterface(const QString &oldInterface, const QString &newInterface); bool setInterface(QString interface); bool isInterfaceSet() const ; QString getInterfaceName(bool &error); bool setInterfaceName(const QString &newName); QString getInterfaceFamily(bool &error); bool setInterfaceFamily(const QString &newName); QString getInterfaceMethod(bool &error); bool setInterfaceMethod(const QString &newName); QString getInterfaceOption(const QString &option, bool &error); bool setInterfaceOption(const QString &option, const QString &value); + bool removeInterfaceOption(const QString &option); bool removeInterfaceOption(const QString &option, const QString &value); bool removeAllInterfaceOptions(); bool setMapping(const QString &interface); bool removeMapping(); void addMapping(const QString &options); bool setMap(const QString &map, const QString &value); bool removeMap(const QString &map, const QString &value); QString getMap(const QString &map, bool &error); bool setScript(const QString &argument); QString getScript(bool &error); bool write(); private: bool setStanza(const QString &stanza, const QString &option, QStringList::Iterator &iterator); bool removeStanza(QStringList::Iterator &stanza); bool setOption(const QStringList::Iterator &start, const QString &option, const QString &value); bool removeAllOptions(const QStringList::Iterator &start); + bool removeOption(const QStringList::Iterator &start, const QString &option); bool removeOption(const QStringList::Iterator &start, const QString &option, const QString &value); QString getOption(const QStringList::Iterator &start, const QString &option, bool &error); QString interfacesFile; QStringList interfaces; QStringList::Iterator currentIface; QStringList::Iterator currentMapping; QStringList acceptedFamily; }; #endif // interfaces |