Diffstat (limited to 'noncore/net/networksetup/interfaces/interfaces.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/networksetup/interfaces/interfaces.cpp | 77 |
1 files changed, 40 insertions, 37 deletions
diff --git a/noncore/net/networksetup/interfaces/interfaces.cpp b/noncore/net/networksetup/interfaces/interfaces.cpp index 708f399..e49998e 100644 --- a/noncore/net/networksetup/interfaces/interfaces.cpp +++ b/noncore/net/networksetup/interfaces/interfaces.cpp | |||
@@ -1,12 +1,13 @@ | |||
1 | #include "interfaces.h" | 1 | #include "interfaces.h" |
2 | 2 | ||
3 | #include <qfile.h> | 3 | #include <qfile.h> |
4 | #include <qtextstream.h> | 4 | #include <qtextstream.h> |
5 | #include <qregexp.h> | 5 | #include <qregexp.h> |
6 | 6 | ||
7 | // The three stanza's | ||
7 | #define AUTO "auto" | 8 | #define AUTO "auto" |
8 | #define IFACE "iface" | 9 | #define IFACE "iface" |
9 | #define MAPPING "mapping" | 10 | #define MAPPING "mapping" |
10 | 11 | ||
11 | /** | 12 | /** |
12 | * Constructor. Reads in the interfaces file and then split the file up by | 13 | * 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){ | |||
99 | (*it) = (*it) += " " + interface; | 100 | (*it) = (*it) += " " + interface; |
100 | // Don't care to have such thins as: auto eth0 lo usb0 | 101 | // Don't care to have such thins as: auto eth0 lo usb0 |
101 | (*it) = (*it).simplifyWhiteSpace(); | 102 | (*it) = (*it).simplifyWhiteSpace(); |
102 | changed = true; | 103 | changed = true; |
103 | break; | 104 | break; |
104 | } | 105 | } |
106 | // else see if we need to remove from this one | ||
105 | else{ | 107 | else{ |
106 | if((*it).contains(interface)){ | 108 | if((*it).contains(interface)){ |
107 | (*it) = (*it).replace(QRegExp(interface), ""); | 109 | (*it) = (*it).replace(QRegExp(interface), ""); |
108 | // clean up | 110 | // if AUTO is the only thing left clear the line |
109 | QString line = (*it).simplifyWhiteSpace(); | 111 | if(((*it).simplifyWhiteSpace()).replace(QRegExp(" "),"") == AUTO) |
110 | line = line.replace(QRegExp(" "),""); | ||
111 | if(line == AUTO) | ||
112 | (*it) = ""; | 112 | (*it) = ""; |
113 | changed = true; | 113 | changed = true; |
114 | // Don't break because we want to make sure we remove all cases. | 114 | // Don't break because we want to make sure we remove all cases. |
115 | } | 115 | } |
116 | } | 116 | } |
117 | } | 117 | } |
118 | } | 118 | } |
119 | if(changed == false){ | 119 | // In the case where there is no AUTO field add one. |
120 | if(setAuto == true) | 120 | if(!changed && setAuto) |
121 | interfaces.append(QString(AUTO" %1").arg(interface)); | 121 | interfaces.append(QString(AUTO" %1").arg(interface)); |
122 | else{ | ||
123 | qDebug(QString("Interfaces: Can't set interface %1 auto to false sense it is already false.").arg(interface).latin1()); | ||
124 | } | ||
125 | } | ||
126 | return true; | 122 | return true; |
127 | } | 123 | } |
128 | 124 | ||
129 | /** | 125 | /** |
130 | * Set the current interface to interface. This needs to be done before you | 126 | * Set the current interface to interface. This needs to be done before you |
131 | * can call getFamily(), getMethod, and get/setOption(). | 127 | * can call getFamily(), getMethod, and get/setOption(). |
@@ -154,13 +150,13 @@ bool Interfaces::isInterfaceSet(){ | |||
154 | * @param family the family of this interface inet or inet, ipx or inet6 | 150 | * @param family the family of this interface inet or inet, ipx or inet6 |
155 | * Must of one of the families defined in interfaces.h | 151 | * Must of one of the families defined in interfaces.h |
156 | * @param method for the family. see interfaces man page for family methods. | 152 | * @param method for the family. see interfaces man page for family methods. |
157 | * @return true if successfull. | 153 | * @return true if successfull. |
158 | */ | 154 | */ |
159 | bool Interfaces::addInterface(const QString &interface, const QString &family, const QString &method){ | 155 | bool Interfaces::addInterface(const QString &interface, const QString &family, const QString &method){ |
160 | if(acceptedFamily.contains(family)==0) | 156 | if(0 == acceptedFamily.contains(family)) |
161 | return false; | 157 | return false; |
162 | QString newInterface = interface.simplifyWhiteSpace(); | 158 | QString newInterface = interface.simplifyWhiteSpace(); |
163 | newInterface = newInterface.replace(QRegExp(" "), ""); | 159 | newInterface = newInterface.replace(QRegExp(" "), ""); |
164 | interfaces.append(""); | 160 | interfaces.append(""); |
165 | interfaces.append(QString(IFACE " %1 %2 %3").arg(newInterface).arg(family).arg(method)); | 161 | interfaces.append(QString(IFACE " %1 %2 %3").arg(newInterface).arg(family).arg(method)); |
166 | return true; | 162 | return true; |
@@ -169,23 +165,29 @@ bool Interfaces::addInterface(const QString &interface, const QString &family, c | |||
169 | /** | 165 | /** |
170 | * Copies interface with name interface to name newInterface | 166 | * Copies interface with name interface to name newInterface |
171 | * @param newInterface name of the new interface. | 167 | * @param newInterface name of the new interface. |
172 | * @return bool true if successfull | 168 | * @return bool true if successfull |
173 | */ | 169 | */ |
174 | bool Interfaces::copyInterface(const QString &interface, const QString &newInterface){ | 170 | bool Interfaces::copyInterface(const QString &interface, const QString &newInterface){ |
175 | if(!setInterface(interface)) return false; | 171 | if(!setInterface(interface)) |
176 | 172 | return false; | |
173 | |||
174 | // Store the old interface and bump past the stanza line. | ||
177 | QStringList::Iterator it = currentIface; | 175 | QStringList::Iterator it = currentIface; |
178 | it++; | 176 | it++; |
179 | 177 | ||
178 | // Add the new interface | ||
180 | bool error; | 179 | bool error; |
181 | addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error)); | 180 | addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error)); |
182 | if(!setInterface(newInterface)) return false; | 181 | if(!setInterface(newInterface)) |
182 | return false; | ||
183 | |||
183 | QStringList::Iterator newIface = currentIface; | 184 | QStringList::Iterator newIface = currentIface; |
184 | newIface++; | 185 | newIface++; |
185 | 186 | ||
187 | // Copy all of the lines | ||
186 | for ( ; it != interfaces.end(); ++it ){ | 188 | for ( ; it != interfaces.end(); ++it ){ |
187 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO))) | 189 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO))) |
188 | break; | 190 | break; |
189 | newIface = interfaces.insert(newIface, *it); | 191 | newIface = interfaces.insert(newIface, *it); |
190 | } | 192 | } |
191 | 193 | ||
@@ -194,16 +196,13 @@ bool Interfaces::copyInterface(const QString &interface, const QString &newInter | |||
194 | 196 | ||
195 | /** | 197 | /** |
196 | * Remove the currently selected interface and all of its options. | 198 | * Remove the currently selected interface and all of its options. |
197 | * @return bool if successfull or not. | 199 | * @return bool if successfull or not. |
198 | */ | 200 | */ |
199 | bool Interfaces::removeInterface(){ | 201 | bool Interfaces::removeInterface(){ |
200 | if(currentIface == interfaces.end()) | 202 | return removeStanza(currentIface); |
201 | return false; | ||
202 | (*currentIface) = ""; | ||
203 | return removeAllInterfaceOptions(); | ||
204 | } | 203 | } |
205 | 204 | ||
206 | /** | 205 | /** |
207 | * Gets the hardware name of the interface that is currently selected. | 206 | * Gets the hardware name of the interface that is currently selected. |
208 | * @return QString name of the hardware interface (eth0, usb2, wlan1...). | 207 | * @return QString name of the hardware interface (eth0, usb2, wlan1...). |
209 | * @param error set to true if any error occurs, false otherwise. | 208 | * @param error set to true if any error occurs, false otherwise. |
@@ -229,16 +228,14 @@ QString Interfaces::getInterfaceName(bool &error){ | |||
229 | * Gets the family name of the interface that is currently selected. | 228 | * Gets the family name of the interface that is currently selected. |
230 | * @return QString name of the family (inet, inet6, ipx). | 229 | * @return QString name of the family (inet, inet6, ipx). |
231 | * @param error set to true if any error occurs, false otherwise. | 230 | * @param error set to true if any error occurs, false otherwise. |
232 | */ | 231 | */ |
233 | QString Interfaces::getInterfaceFamily(bool &error){ | 232 | QString Interfaces::getInterfaceFamily(bool &error){ |
234 | QString name = getInterfaceName(error); | 233 | QString name = getInterfaceName(error); |
235 | if(error){ | 234 | if(error) |
236 | error = true; | ||
237 | return QString(); | 235 | return QString(); |
238 | } | ||
239 | QString line = (*currentIface); | 236 | QString line = (*currentIface); |
240 | line = line.mid(QString(IFACE).length() +1, line.length()); | 237 | line = line.mid(QString(IFACE).length() +1, line.length()); |
241 | line = line.mid(name.length()+1, line.length()); | 238 | line = line.mid(name.length()+1, line.length()); |
242 | line = line.simplifyWhiteSpace(); | 239 | line = line.simplifyWhiteSpace(); |
243 | int findSpace = line.find(" "); | 240 | int findSpace = line.find(" "); |
244 | if( findSpace < 0){ | 241 | if( findSpace < 0){ |
@@ -254,21 +251,17 @@ QString Interfaces::getInterfaceFamily(bool &error){ | |||
254 | * @return QString name of the method such as staic or dhcp. | 251 | * @return QString name of the method such as staic or dhcp. |
255 | * See the man page of interfaces for possible methods depending on the family. | 252 | * See the man page of interfaces for possible methods depending on the family. |
256 | * @param error set to true if any error occurs, false otherwise. | 253 | * @param error set to true if any error occurs, false otherwise. |
257 | */ | 254 | */ |
258 | QString Interfaces::getInterfaceMethod(bool &error){ | 255 | QString Interfaces::getInterfaceMethod(bool &error){ |
259 | QString name = getInterfaceName(error); | 256 | QString name = getInterfaceName(error); |
260 | if(error){ | 257 | if(error) |
261 | error = true; | ||
262 | return QString(); | 258 | return QString(); |
263 | } | ||
264 | QString family = getInterfaceFamily(error); | 259 | QString family = getInterfaceFamily(error); |
265 | if(error){ | 260 | if(error) |
266 | error = true; | ||
267 | return QString(); | 261 | return QString(); |
268 | } | ||
269 | QString line = (*currentIface); | 262 | QString line = (*currentIface); |
270 | line = line.mid(QString(IFACE).length()+1, line.length()); | 263 | line = line.mid(QString(IFACE).length()+1, line.length()); |
271 | line = line.mid(name.length()+1, line.length()); | 264 | line = line.mid(name.length()+1, line.length()); |
272 | line = line.mid(family.length()+1, line.length()); | 265 | line = line.mid(family.length()+1, line.length()); |
273 | line = line.simplifyWhiteSpace(); | 266 | line = line.simplifyWhiteSpace(); |
274 | error = false; | 267 | error = false; |
@@ -388,16 +381,13 @@ void Interfaces::addMapping(const QString &option){ | |||
388 | 381 | ||
389 | /** | 382 | /** |
390 | * Remove the currently selected map and all of its options. | 383 | * Remove the currently selected map and all of its options. |
391 | * @return bool if successfull or not. | 384 | * @return bool if successfull or not. |
392 | */ | 385 | */ |
393 | bool Interfaces::removeMapping(){ | 386 | bool Interfaces::removeMapping(){ |
394 | if(currentMapping == interfaces.end()) | 387 | return removeStanza(currentMapping); |
395 | return false; | ||
396 | (*currentMapping) = ""; | ||
397 | return removeAllOptions(currentMapping); | ||
398 | } | 388 | } |
399 | 389 | ||
400 | /** | 390 | /** |
401 | * Set a map option within a mapping. | 391 | * Set a map option within a mapping. |
402 | * @param map map to use | 392 | * @param map map to use |
403 | * @param value value to go with map | 393 | * @param value value to go with map |
@@ -517,12 +507,25 @@ bool Interfaces::setOption(const QStringList::Iterator &start, const QString &op | |||
517 | QStringList::Iterator p = start; | 507 | QStringList::Iterator p = start; |
518 | interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value)); | 508 | interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value)); |
519 | found = true; | 509 | found = true; |
520 | } | 510 | } |
521 | return found; | 511 | return found; |
522 | } | 512 | } |
513 | |||
514 | /** | ||
515 | * Removes a stanza and all of its options | ||
516 | * @param stanza the stanza to remove | ||
517 | * @return bool true if successfull. | ||
518 | */ | ||
519 | bool Interfaces::removeStanza(QStringList::Iterator &stanza){ | ||
520 | if(stanza == interfaces.end()) | ||
521 | return false; | ||
522 | (*stanza) = ""; | ||
523 | return removeAllOptions(stanza); | ||
524 | } | ||
525 | |||
523 | /** | 526 | /** |
524 | * Removes a option in a stanza | 527 | * Removes a option in a stanza |
525 | * @param start the start of the stanza | 528 | * @param start the start of the stanza |
526 | * @param option the option to use when setting value. | 529 | * @param option the option to use when setting value. |
527 | * @return bool true if successfull, false otherwise. | 530 | * @return bool true if successfull, false otherwise. |
528 | */ | 531 | */ |
@@ -588,20 +591,20 @@ QString Interfaces::getOption(const QStringList::Iterator &start, const QString | |||
588 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { | 591 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { |
589 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ | 592 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ |
590 | break; | 593 | break; |
591 | } | 594 | } |
592 | if((*it).contains(option) && (*it).at(0) != '#'){ | 595 | if((*it).contains(option) && (*it).at(0) != '#'){ |
593 | if(found) | 596 | if(found) |
594 | qDebug(QString("Interfaces: Get Options found more then one value: %1 for option: %2 in stanza %3").arg((*it)).arg(option).arg((*start)).latin1()); | 597 | qDebug(QString("Interfaces: getOption found more then one value: %1 for option: %2 in stanza %3").arg((*it)).arg(option).arg((*start)).latin1()); |
595 | found = true; | 598 | found = true; |
596 | QString line = (*it).simplifyWhiteSpace(); | 599 | QString line = (*it).simplifyWhiteSpace(); |
597 | int space = line.find(" ", option.length()); | 600 | int space = line.find(" ", option.length()); |
598 | if(space != -1) | 601 | if(space != -1){ |
599 | value = line.mid(space+1, line.length()); | 602 | value = line.mid(space+1, line.length()); |
600 | else | 603 | break; |
601 | qDebug(QString("Interfaces: Option %1 with no value").arg(option).latin1()); | 604 | } |
602 | } | 605 | } |
603 | } | 606 | } |
604 | error = !found; | 607 | error = !found; |
605 | return value; | 608 | return value; |
606 | } | 609 | } |
607 | 610 | ||