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.cpp60
1 files changed, 47 insertions, 13 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,14 +1,15 @@
1#include "interfaces.h" 1#include "interfaces.h"
2 2
3#include <qcheckbox.h>
3#include <qfile.h> 4#include <qfile.h>
4#include <qtextstream.h> 5#include <qtextstream.h>
5#include <qregexp.h> 6#include <qregexp.h>
6 7
7// The three stanza's 8// The three stanza's
8#define AUTO "auto" 9#define AUTO "auto"
9#define IFACE "iface" 10#define IFACE "iface"
10#define MAPPING "mapping" 11#define MAPPING "mapping"
11 12
12/** 13/**
13 * Constructor. Reads in the interfaces file and then split the file up by 14 * Constructor. Reads in the interfaces file and then split the file up by
14 * the \n for interfaces variable. 15 * the \n for interfaces variable.
@@ -142,39 +143,41 @@ bool Interfaces::isInterfaceSet() const {
142} 143}
143 144
144/** 145/**
145 * Add a new interface of with the settings - family and method 146 * Add a new interface of with the settings - family and method
146 * @param interface the name of the interface to set. All whitespace is 147 * @param interface the name of the interface to set. All whitespace is
147 * removed from the interface name. 148 * removed from the interface name.
148 * @param family the family of this interface inet or inet, ipx or inet6 149 * @param family the family of this interface inet or inet, ipx or inet6
149 * Must of one of the families defined in interfaces.h 150 * Must of one of the families defined in interfaces.h
150 * @param method for the family. see interfaces man page for family methods. 151 * @param method for the family. see interfaces man page for family methods.
151 * @return true if successfull. 152 * @return true if successfull.
152 */ 153 */
153bool Interfaces::addInterface(const QString &interface, const QString &family, const QString &method){ 154bool Interfaces::addInterface(const QString &interface, const QString &family, const QString &method){
155 qDebug("Interfaces::addInterface(%s)",interface.latin1());
154 if(0 == acceptedFamily.contains(family)) 156 if(0 == acceptedFamily.contains(family))
155 return false; 157 return false;
156 QString newInterface = interface.simplifyWhiteSpace(); 158 QString newInterface = interface.simplifyWhiteSpace();
157 newInterface = newInterface.replace(QRegExp(" "), ""); 159 newInterface = newInterface.replace(QRegExp(" "), "");
158 interfaces.append(""); 160 interfaces.append("");
159 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));
160 return true; 162 return true;
161} 163}
162 164
163/** 165/**
164 * Copies interface with name interface to name newInterface 166 * Copies interface with name interface to name newInterface
165 * @param newInterface name of the new interface. 167 * @param newInterface name of the new interface.
166 * @return bool true if successfull 168 * @return bool true if successfull
167 */ 169 */
168bool Interfaces::copyInterface(const QString &interface, const QString &newInterface){ 170bool Interfaces::copyInterface(const QString &interface, const QString &newInterface){
171 qDebug("copy interface %s to %s", interface.latin1(), newInterface.latin1());
169 if(!setInterface(interface)) 172 if(!setInterface(interface))
170 return false; 173 return false;
171 174
172 // Store the old interface and bump past the stanza line. 175 // Store the old interface and bump past the stanza line.
173 QStringList::Iterator it = currentIface; 176 QStringList::Iterator it = currentIface;
174 it++; 177 it++;
175 178
176 // Add the new interface 179 // Add the new interface
177 bool error; 180 bool error;
178 addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error)); 181 addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error));
179 if(!setInterface(newInterface)) 182 if(!setInterface(newInterface))
180 return false; 183 return false;
@@ -263,30 +266,34 @@ QString Interfaces::getInterfaceMethod(bool &error){
263 line = line.mid(family.length()+1, line.length()); 266 line = line.mid(family.length()+1, line.length());
264 line = line.simplifyWhiteSpace(); 267 line = line.simplifyWhiteSpace();
265 error = false; 268 error = false;
266 return line; 269 return line;
267} 270}
268 271
269/** 272/**
270 * Sets the interface name to newName. 273 * Sets the interface name to newName.
271 * @param newName the new name of the interface. All whitespace is removed. 274 * @param newName the new name of the interface. All whitespace is removed.
272 * @return bool true if successfull. 275 * @return bool true if successfull.
273 */ 276 */
274bool Interfaces::setInterfaceName(const QString &newName){ 277bool Interfaces::setInterfaceName(const QString &newName){
278 qDebug("setInterfaceName %s", newName.latin1());
275 if(currentIface == interfaces.end()) 279 if(currentIface == interfaces.end())
276 return false; 280 return false;
277 QString name = newName.simplifyWhiteSpace(); 281 QString name = newName.simplifyWhiteSpace();
278 name = name.replace(QRegExp(" "), ""); 282 name = name.replace(QRegExp(" "), "");
279 bool returnValue = false; 283 bool returnValue = false;
280 (*currentIface) = QString("iface %1 %2 %3").arg(name).arg(getInterfaceFamily(returnValue)).arg(getInterfaceMethod(returnValue)); 284 QString tmp = QString("iface %1 %2 %3").arg(name).arg(getInterfaceFamily(returnValue)).arg(getInterfaceMethod(returnValue));
285 qDebug("setting %s",tmp.latin1());
286
287 (*currentIface) = tmp;
281 return !returnValue; 288 return !returnValue;
282} 289}
283 290
284/** 291/**
285 * Sets the interface family to newName. 292 * Sets the interface family to newName.
286 * @param newName the new name of the interface. Must be one of the families 293 * @param newName the new name of the interface. Must be one of the families
287 * defined in the interfaces.h file. 294 * defined in the interfaces.h file.
288 * @return bool true if successfull. 295 * @return bool true if successfull.
289 */ 296 */
290bool Interfaces::setInterfaceFamily(const QString &newName){ 297bool Interfaces::setInterfaceFamily(const QString &newName){
291 if(currentIface == interfaces.end()) 298 if(currentIface == interfaces.end())
292 return false; 299 return false;
@@ -318,31 +325,37 @@ bool Interfaces::setInterfaceMethod(const QString &newName){
318 * address 192.168.1.1 325 * address 192.168.1.1
319 * @param option the options to get the value. 326 * @param option the options to get the value.
320 * @param error set to true if any error occurs, false otherwise. 327 * @param error set to true if any error occurs, false otherwise.
321 * @return QString the options value. QString::null if error == true 328 * @return QString the options value. QString::null if error == true
322 */ 329 */
323QString Interfaces::getInterfaceOption(const QString &option, bool &error){ 330QString Interfaces::getInterfaceOption(const QString &option, bool &error){
324 return getOption(currentIface, option, error); 331 return getOption(currentIface, option, error);
325} 332}
326 333
327/** 334/**
328 * Set a value for an option in the currently selected interface. If option 335 * Set a value for an option in the currently selected interface. If option
329 * doesn't exist then it is added along with the value. 336 * doesn't exist then it is added along with the value.
337 * If value isEmpty() then we will remove the option
338 *
330 * @param option the options to set the value. 339 * @param option the options to set the value.
331 * @param value the value that option should be set to. 340 * @param value the value that option should be set to.
332 * @param error set to true if any error occurs, false otherwise. 341 * @param error set to true if any error occurs, false otherwise.
333 * @return QString the options value. QString::null if error == true 342 * @return QString the options value. QString::null if error == true
334 */ 343 */
335bool Interfaces::setInterfaceOption(const QString &option, const QString &value){ 344bool Interfaces::setInterfaceOption(const QString &option, const QString &value){
336 return setOption(currentIface, option, value); 345 if( value.stripWhiteSpace().isEmpty() )
346 return removeInterfaceOption( option );
347
348 qDebug("iface >%s< option >%s< value >%s<", (*currentIface).latin1(), option.latin1(),value.latin1());
349 return setOption(currentIface, option, value);
337} 350}
338 351
339/** 352/**
340 * Removes a value for an option in the currently selected interface. 353 * Removes a value for an option in the currently selected interface.
341 * @param option the options to set the value. 354 * @param option the options to set the value.
342 * @param error set to true if any error occurs, false otherwise. 355 * @param error set to true if any error occurs, false otherwise.
343 * @return QString the options value. QString::null if error == true 356 * @return QString the options value. QString::null if error == true
344 */ 357 */
345bool Interfaces::removeInterfaceOption(const QString &option){ 358bool Interfaces::removeInterfaceOption(const QString &option){
346 return removeOption(currentIface, option); 359 return removeOption(currentIface, option);
347} 360}
348 361
@@ -483,48 +496,67 @@ bool Interfaces::setStanza(const QString &stanza, const QString &option, QString
483 return found; 496 return found;
484} 497}
485 498
486/** 499/**
487 * Sets a value of an option in a stanza 500 * Sets a value of an option in a stanza
488 * @param start the start of the stanza 501 * @param start the start of the stanza
489 * @param option the option to use when setting value. 502 * @param option the option to use when setting value.
490 * @return bool true if successfull, false otherwise. 503 * @return bool true if successfull, false otherwise.
491 */ 504 */
492bool Interfaces::setOption(const QStringList::Iterator &start, const QString &option, const QString &value){ 505bool Interfaces::setOption(const QStringList::Iterator &start, const QString &option, const QString &value){
493 if(start == interfaces.end()) 506 if(start == interfaces.end())
494 return false; 507 return false;
495 508 qDebug("setting option");
496 bool found = false; 509 bool found = false;
510 bool replaced = false;
511 QStringList::Iterator insertAt = NULL;
497 for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { 512 for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) {
498 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ 513 qDebug(" Interfaces::setOption got line >%s<",(*it).latin1());
499 if(!found && value != ""){ 514 // FIXME: was not completly stupid just wrong sice all options got inserted bevore the iface line
500 // Got to the end of the stanza without finding it, so append it. 515 // but since it works with an empty interfaces file I (tille) will not do anything more
501 interfaces.insert(--it, QString("\t%1 %2").arg(option).arg(value)); 516 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) ){
502 } 517 if (found) break;
503 found = true; 518// && it != start){
504 break; 519// if(!found && value != ""){
505 } 520// // Got to the end of the stanza without finding it, so append it.
521// qDebug(" Got to the end of the stanza without finding it, so append it.");
522// interfaces.insert(--it, QString("\t%1 %2").arg(option).arg(value));
523// }
524 qDebug("found 1");
525// interfaces.insert(++it, QString("\t%1 %2").arg(option).arg(value));
526 found = true;
527 insertAt = it;
528
529 }
506 if((*it).contains(option) && it != start && (*it).at(0) != '#'){ 530 if((*it).contains(option) && it != start && (*it).at(0) != '#'){
507 // Found it in stanza so replace it. 531 // Found it in stanza so replace it.
532 qDebug("found 2");
508 if(found) 533 if(found)
509 qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); 534 qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1());
510 found = true; 535 found = true;
536 replaced = true;
511 (*it) = QString("\t%1 %2").arg(option).arg(value); 537 (*it) = QString("\t%1 %2").arg(option).arg(value);
512 } 538 }
513 } 539 }
514 if(!found){ 540 if(!found){
541 qDebug("! found insert anyway");
515 QStringList::Iterator p = start; 542 QStringList::Iterator p = start;
516 interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value)); 543 interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value));
517 found = true; 544 found = true;
518 } 545 }
546
547 if(found && !replaced){
548 qDebug("found iface but not the option so insert it here...");
549 interfaces.insert(++insertAt, QString("\t%1 %2").arg(option).arg(value));
550 }
519 return found; 551 return found;
520} 552}
521 553
522/** 554/**
523 * Removes a stanza and all of its options 555 * Removes a stanza and all of its options
524 * @param stanza the stanza to remove 556 * @param stanza the stanza to remove
525 * @return bool true if successfull. 557 * @return bool true if successfull.
526 */ 558 */
527bool Interfaces::removeStanza(QStringList::Iterator &stanza){ 559bool Interfaces::removeStanza(QStringList::Iterator &stanza){
528 if(stanza == interfaces.end()) 560 if(stanza == interfaces.end())
529 return false; 561 return false;
530 (*stanza) = ""; 562 (*stanza) = "";
@@ -543,25 +575,26 @@ bool Interfaces::removeOption(const QStringList::Iterator &start, const QString
543 575
544 bool found = false; 576 bool found = false;
545 for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { 577 for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) {
546 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ 578 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){
547 // got to the end without finding it 579 // got to the end without finding it
548 break; 580 break;
549 } 581 }
550 if((*it).contains(option) && it != start && (*it).at(0) != '#'){ 582 if((*it).contains(option) && it != start && (*it).at(0) != '#'){
551 // Found it in stanza so replace it. 583 // Found it in stanza so replace it.
552 if(found) 584 if(found)
553 qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); 585 qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1());
554 found = true; 586 found = true;
555 (*it) = ""; 587 it = interfaces.remove( it ); // we really want to remove the line
588 --it; // we do ++it later in the head of the for loop
556 } 589 }
557 } 590 }
558 return found; 591 return found;
559} 592}
560 593
561/** 594/**
562 * Removes a option in a stanza 595 * Removes a option in a stanza
563 * @param start the start of the stanza 596 * @param start the start of the stanza
564 * @param option the option to use when setting value. 597 * @param option the option to use when setting value.
565 * @return bool true if successfull, false otherwise. 598 * @return bool true if successfull, false otherwise.
566 */ 599 */
567bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option, const QString &value){ 600bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option, const QString &value){
@@ -570,25 +603,26 @@ bool Interfaces::removeOption(const QStringList::Iterator &start, const QString
570 603
571 bool found = false; 604 bool found = false;
572 for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { 605 for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) {
573 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ 606 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){
574 // got to the end without finding it 607 // got to the end without finding it
575 break; 608 break;
576 } 609 }
577 if((*it).contains(option) && (*it).contains(value) && it != start && (*it).at(0) != '#'){ 610 if((*it).contains(option) && (*it).contains(value) && it != start && (*it).at(0) != '#'){
578 // Found it in stanza so replace it. 611 // Found it in stanza so replace it.
579 if(found) 612 if(found)
580 qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); 613 qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1());
581 found = true; 614 found = true;
582 (*it) = ""; 615 it = interfaces.remove( it ); // we really want to remove the line
616 --it; // we do ++it later in the head of the for loop
583 } 617 }
584 } 618 }
585 return found; 619 return found;
586} 620}
587 621
588/** 622/**
589 * Removes all options in a stanza 623 * Removes all options in a stanza
590 * @param start the start of the stanza 624 * @param start the start of the stanza
591 * @return bool true if successfull, false otherwise. 625 * @return bool true if successfull, false otherwise.
592 */ 626 */
593bool Interfaces::removeAllOptions(const QStringList::Iterator &start){ 627bool Interfaces::removeAllOptions(const QStringList::Iterator &start){
594 if(start == interfaces.end()) 628 if(start == interfaces.end())