summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings/interfaces/interfaces.cpp
Unidiff
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,18 +1,19 @@
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
13 * the \n for interfaces variable. 14 * the \n for interfaces variable.
14 * @param useInterfacesFile if an interface file other then the default is 15 * @param useInterfacesFile if an interface file other then the default is
15 * desired to be used it should be passed in. 16 * desired to be used it should be passed in.
16 */ 17 */
17Interfaces::Interfaces(QString useInterfacesFile){ 18Interfaces::Interfaces(QString useInterfacesFile){
18 acceptedFamily.append(INTERFACES_FAMILY_INET); 19 acceptedFamily.append(INTERFACES_FAMILY_INET);
@@ -93,45 +94,40 @@ bool Interfaces::setAuto(const QString &interface, bool setAuto){
93 94
94 bool changed = false; 95 bool changed = false;
95 for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { 96 for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) {
96 if((*it).contains(AUTO)){ 97 if((*it).contains(AUTO)){
97 //We know that they are not in any group so let add to this auto. 98 //We know that they are not in any group so let add to this auto.
98 if(setAuto){ 99 if(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().
132 * @param interface the name of the interface to set. All whitespace is 128 * @param interface the name of the interface to set. All whitespace is
133 * removed from the interface name. 129 * removed from the interface name.
134 * @return bool true if it is successfull. 130 * @return bool true if it is successfull.
135 */ 131 */
136bool Interfaces::setInterface(QString interface){ 132bool Interfaces::setInterface(QString interface){
137 interface = interface.simplifyWhiteSpace(); 133 interface = interface.simplifyWhiteSpace();
@@ -148,68 +144,71 @@ bool Interfaces::isInterfaceSet(){
148} 144}
149 145
150/** 146/**
151 * Add a new interface of with the settings - family and method 147 * Add a new interface of with the settings - family and method
152 * @param interface the name of the interface to set. All whitespace is 148 * @param interface the name of the interface to set. All whitespace is
153 * removed from the interface name. 149 * removed from the interface name.
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 */
159bool Interfaces::addInterface(const QString &interface, const QString &family, const QString &method){ 155bool 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;
167} 163}
168 164
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 */
174bool Interfaces::copyInterface(const QString &interface, const QString &newInterface){ 170bool 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
192 return true; 194 return true;
193} 195}
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 */
199bool Interfaces::removeInterface(){ 201bool 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.
210 */ 209 */
211QString Interfaces::getInterfaceName(bool &error){ 210QString Interfaces::getInterfaceName(bool &error){
212 if(currentIface == interfaces.end()){ 211 if(currentIface == interfaces.end()){
213 error = true; 212 error = true;
214 return QString(); 213 return QString();
215 } 214 }
@@ -223,58 +222,52 @@ QString Interfaces::getInterfaceName(bool &error){
223 } 222 }
224 error = false; 223 error = false;
225 return line.mid(0, findSpace); 224 return line.mid(0, findSpace);
226} 225}
227 226
228/** 227/**
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 */
233QString Interfaces::getInterfaceFamily(bool &error){ 232QString 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){
245 error = true; 242 error = true;
246 return QString(); 243 return QString();
247 } 244 }
248 error = false; 245 error = false;
249 return line.mid(0, findSpace); 246 return line.mid(0, findSpace);
250} 247}
251 248
252/** 249/**
253 * Gets the method of the interface that is currently selected. 250 * Gets the method of the interface that is currently selected.
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 */
258QString Interfaces::getInterfaceMethod(bool &error){ 255QString 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;
275 return line; 268 return line;
276} 269}
277 270
278/** 271/**
279 * Sets the interface name to newName. 272 * Sets the interface name to newName.
280 * @param newName the new name of the interface. All whitespace is removed. 273 * @param newName the new name of the interface. All whitespace is removed.
@@ -382,28 +375,25 @@ bool Interfaces::setMapping(const QString &interface){
382 * @param interface the name(s) of the interfaces to set to this mapping 375 * @param interface the name(s) of the interfaces to set to this mapping
383 */ 376 */
384void Interfaces::addMapping(const QString &option){ 377void Interfaces::addMapping(const QString &option){
385 interfaces.append(""); 378 interfaces.append("");
386 interfaces.append(QString(MAPPING " %1").arg(option)); 379 interfaces.append(QString(MAPPING " %1").arg(option));
387} 380}
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 */
393bool Interfaces::removeMapping(){ 386bool 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
404 * @return bool true if it is successfull. 394 * @return bool true if it is successfull.
405 */ 395 */
406bool Interfaces::setMap(const QString &map, const QString &value){ 396bool Interfaces::setMap(const QString &map, const QString &value){
407 return setOption(currentMapping, map, value); 397 return setOption(currentMapping, map, value);
408} 398}
409 399
@@ -511,24 +501,37 @@ bool Interfaces::setOption(const QStringList::Iterator &start, const QString &op
511 qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); 501 qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1());
512 found = true; 502 found = true;
513 (*it) = QString("\t%1 %2").arg(option).arg(value); 503 (*it) = QString("\t%1 %2").arg(option).arg(value);
514 } 504 }
515 } 505 }
516 if(!found){ 506 if(!found){
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 */
519bool 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 */
529bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option, const QString &value){ 532bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option, const QString &value){
530 if(start == interfaces.end()) 533 if(start == interfaces.end())
531 return false; 534 return false;
532 535
533 bool found = false; 536 bool found = false;
534 for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { 537 for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) {
@@ -582,32 +585,32 @@ QString Interfaces::getOption(const QStringList::Iterator &start, const QString
582 error = false; 585 error = false;
583 return QString(); 586 return QString();
584 } 587 }
585 588
586 QString value; 589 QString value;
587 bool found = false; 590 bool found = false;
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
608/** 611/**
609 * Write out the interfaces file to the file passed into the constructor. 612 * Write out the interfaces file to the file passed into the constructor.
610 * Removes any excess blank lines over 1 line long. 613 * Removes any excess blank lines over 1 line long.
611 * @return bool true if successfull, false if not. 614 * @return bool true if successfull, false if not.
612 */ 615 */
613bool Interfaces::write(){ 616bool Interfaces::write(){