Networksettings API API Documentation

interfaces.cpp

00001 #include "interfaces.h"
00002 
00003 #include <qcheckbox.h>
00004 #include <qfile.h>
00005 #include <qtextstream.h>
00006 #include <qregexp.h>
00007 
00008 // The three stanza's
00009 #define AUTO "auto"
00010 #define IFACE "iface"
00011 #define MAPPING "mapping"
00012 
00019 Interfaces::Interfaces(QString useInterfacesFile){
00020   acceptedFamily.append(INTERFACES_FAMILY_INET);
00021   acceptedFamily.append(INTERFACES_FAMILY_IPX);
00022   acceptedFamily.append(INTERFACES_FAMILY_INET6);
00023 
00024   interfacesFile = useInterfacesFile;
00025   QFile file(interfacesFile);
00026   if (!file.open(IO_ReadOnly)){
00027     qDebug("Interfaces: Can't open file: %s for reading.", interfacesFile.latin1() );
00028     currentIface = interfaces.end();
00029     currentMapping = interfaces.end();
00030     return;
00031   }
00032   QTextStream stream( &file );
00033   QString line;
00034   while ( !stream.eof() ) {
00035     line += stream.readLine();
00036     line += "\n";
00037   }
00038   file.close();
00039   interfaces = QStringList::split("\n", line, true);
00040 
00041   currentIface = interfaces.end();
00042   currentMapping = interfaces.end();
00043 }
00044 
00045 
00052 QStringList Interfaces::getInterfaceList(){
00053   QStringList list;
00054   for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) {
00055     QString line = (*it).simplifyWhiteSpace();
00056     if(line.contains(IFACE) && line.at(0) != '#'){
00057       line = line.mid(QString(IFACE).length() +1, line.length());
00058       line = line.simplifyWhiteSpace();
00059       int findSpace = line.find(" ");
00060       if( findSpace >= 0){
00061         line = line.mid(0, findSpace);
00062         list.append(line);
00063       }
00064     }
00065   }
00066   return list;
00067 }
00068 
00075 bool Interfaces::isAuto(const QString &interface) const {
00076   QStringList autoLines = interfaces.grep(QRegExp(AUTO));
00077   QStringList awi = autoLines.grep(QRegExp(interface));
00078   if(awi.count() > 1)
00079     qDebug(QString("Interfaces: Found more then auto group with interface: %1.").arg(interface).latin1());
00080   return awi.count() > 0;
00081 }
00082 
00089 bool Interfaces::setAuto(const QString &interface, bool setAuto){
00090   // Don't need to set it if it is already set.
00091   if(isAuto(interface) == setAuto)
00092     return false;
00093 
00094   bool changed = false;
00095   for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) {
00096     if((*it).contains(AUTO)){
00097       //We know that they are not in any group so let add to this auto.
00098       if(setAuto){
00099         (*it) = (*it) += " " + interface;
00100         // Don't care to have such thins as: auto eth0   lo  usb0
00101     (*it) = (*it).simplifyWhiteSpace();
00102         changed = true;
00103         break;
00104       }
00105       // else see if we need to remove from this one
00106       else{
00107         if((*it).contains(interface)){
00108           (*it) = (*it).replace(QRegExp(interface), "");
00109           // if AUTO is the only thing left clear the line
00110           if(((*it).simplifyWhiteSpace()).replace(QRegExp(" "),"") == AUTO)
00111             (*it) = "";
00112           changed = true;
00113       // Don't break because we want to make sure we remove all cases.
00114         }
00115       }
00116     }
00117   }
00118   // In the case where there is no AUTO field add one.
00119   if(!changed && setAuto)
00120     interfaces.append(QString(AUTO" %1").arg(interface));
00121   return true;
00122 }
00123 
00131 bool Interfaces::setInterface(QString interface){
00132   interface = interface.simplifyWhiteSpace();
00133   interface = interface.replace(QRegExp(" "), "");
00134   return setStanza(IFACE, interface, currentIface);
00135 }
00136 
00141 bool Interfaces::isInterfaceSet() const {
00142   return (interfaces.end() != currentIface);
00143 }
00144 
00154 bool Interfaces::addInterface(const QString &interface, const QString &family, const QString &method){
00155     qDebug("Interfaces::addInterface(%s)",interface.latin1());
00156   if(0 == acceptedFamily.contains(family))
00157     return false;
00158   QString newInterface = interface.simplifyWhiteSpace();
00159   newInterface = newInterface.replace(QRegExp(" "), "");
00160   interfaces.append("");
00161   interfaces.append(QString(IFACE " %1 %2 %3").arg(newInterface).arg(family).arg(method));
00162   return true;
00163 }
00164 
00170 bool Interfaces::copyInterface(const QString &interface, const QString &newInterface){
00171     qDebug("copy interface %s to %s", interface.latin1(), newInterface.latin1());
00172   if(!setInterface(interface))
00173     return false;
00174 
00175   // Store the old interface and bump past the stanza line.
00176   QStringList::Iterator it = currentIface;
00177   it++;
00178 
00179   // Add the new interface
00180   bool error;
00181   addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error));
00182   if(!setInterface(newInterface))
00183     return false;
00184 
00185   QStringList::Iterator newIface = currentIface;
00186   newIface++;
00187 
00188   // Copy all of the lines
00189   for ( ; it != interfaces.end(); ++it ){
00190     if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)))
00191       break;
00192     newIface = interfaces.insert(newIface, *it);
00193   }
00194 
00195   return true;
00196 }
00197 
00202 bool Interfaces::removeInterface(){
00203   return removeStanza(currentIface);
00204 }
00205 
00211 QString Interfaces::getInterfaceName(bool &error){
00212   if(currentIface == interfaces.end()){
00213     error = true;
00214     return QString();
00215   }
00216   QString line = (*currentIface);
00217   line = line.mid(QString(IFACE).length() +1, line.length());
00218   line = line.simplifyWhiteSpace();
00219   int findSpace = line.find(" ");
00220   if( findSpace < 0){
00221     error = true;
00222     return QString();
00223   }
00224   error = false;
00225   return line.mid(0, findSpace);
00226 }
00227 
00233 QString Interfaces::getInterfaceFamily(bool &error){
00234   QString name = getInterfaceName(error);
00235   if(error)
00236     return QString();
00237   QString line = (*currentIface);
00238   line = line.mid(QString(IFACE).length() +1, line.length());
00239   line = line.mid(name.length()+1, line.length());
00240   line = line.simplifyWhiteSpace();
00241   int findSpace = line.find(" ");
00242   if( findSpace < 0){
00243     error = true;
00244     return QString();
00245   }
00246   error = false;
00247   return line.mid(0, findSpace);
00248 }
00249 
00256 QString Interfaces::getInterfaceMethod(bool &error){
00257   QString name = getInterfaceName(error);
00258   if(error)
00259     return QString();
00260   QString family = getInterfaceFamily(error);
00261   if(error)
00262     return QString();
00263   QString line = (*currentIface);
00264   line = line.mid(QString(IFACE).length()+1, line.length());
00265   line = line.mid(name.length()+1, line.length());
00266   line = line.mid(family.length()+1, line.length());
00267   line = line.simplifyWhiteSpace();
00268   error = false;
00269   return line;
00270 }
00271 
00277 bool Interfaces::setInterfaceName(const QString &newName){
00278     qDebug("setInterfaceName %s", newName.latin1());
00279   if(currentIface == interfaces.end())
00280     return false;
00281   QString name = newName.simplifyWhiteSpace();
00282   name = name.replace(QRegExp(" "), "");
00283   bool returnValue = false;
00284   QString tmp = QString("iface %1 %2 %3").arg(name).arg(getInterfaceFamily(returnValue)).arg(getInterfaceMethod(returnValue));
00285   qDebug("setting  %s",tmp.latin1());
00286 
00287   (*currentIface) = tmp;
00288   return !returnValue;
00289 }
00290 
00297 bool Interfaces::setInterfaceFamily(const QString &newName){
00298   if(currentIface == interfaces.end())
00299     return false;
00300   if(acceptedFamily.contains(newName)==0)
00301     return false;
00302   bool returnValue = false;
00303   (*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(newName).arg(getInterfaceMethod(returnValue));
00304   return !returnValue;
00305 }
00306 
00312 bool Interfaces::setInterfaceMethod(const QString &newName){
00313   if(currentIface == interfaces.end())
00314     return false;
00315   bool returnValue = false;
00316   (*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(getInterfaceFamily(returnValue)).arg(newName);
00317   return !returnValue;
00318 }
00319 
00330 QString Interfaces::getInterfaceOption(const QString &option, bool &error){
00331   return getOption(currentIface, option, error);
00332 }
00333 
00344 bool Interfaces::setInterfaceOption(const QString &option, const QString &value){
00345     if( value.stripWhiteSpace().isEmpty() )
00346     return removeInterfaceOption( option );
00347 
00348     qDebug("iface >%s< option >%s< value >%s<", (*currentIface).latin1(), option.latin1(),value.latin1());
00349     return setOption(currentIface, option, value);
00350 }
00351 
00358 bool Interfaces::removeInterfaceOption(const QString &option){
00359   return removeOption(currentIface, option);
00360 }
00361 
00369 bool Interfaces::removeInterfaceOption(const QString &option, const QString &value){
00370   return removeOption(currentIface, option, value);
00371 }
00372 
00377 bool Interfaces::removeAllInterfaceOptions(){
00378   return removeAllOptions(currentIface);
00379 }
00380 
00388 bool Interfaces::setMapping(const QString &interface){
00389   QString interfaceName = interface.simplifyWhiteSpace();
00390   interfaceName = interfaceName.replace(QRegExp(" "), "");
00391   return setStanza(MAPPING, interfaceName, currentMapping);
00392 }
00393 
00398 void Interfaces::addMapping(const QString &option){
00399   interfaces.append("");
00400   interfaces.append(QString(MAPPING " %1").arg(option));
00401 }
00402 
00407 bool Interfaces::removeMapping(){
00408   return removeStanza(currentMapping);
00409 }
00410 
00417 bool Interfaces::setMap(const QString &map, const QString &value){
00418   return setOption(currentMapping, map, value);
00419 }
00420 
00427 bool Interfaces::removeMap(const QString &map, const QString &value){
00428   return removeOption(currentMapping, map, value);
00429 }
00430 
00437 QString Interfaces::getMap(const QString &map, bool &error){
00438   return getOption(currentMapping, map, error);
00439 }
00440 
00446 bool Interfaces::setScript(const QString &argument){
00447   return setOption(currentMapping, "script", argument);
00448 }
00449 
00454 QString Interfaces::getScript(bool &error){
00455   return getOption(currentMapping, "script", error);
00456 }
00457 
00458 
00459 
00468 bool Interfaces::setStanza(const QString &stanza, const QString &option, QStringList::Iterator &iterator){
00469   bool found = false;
00470   iterator = interfaces.end();
00471   for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) {
00472     QString line = (*it).simplifyWhiteSpace();
00473     if(line.contains(stanza) && line.contains(option) && line.at(0) != '#'){
00474       uint point = line.find(option);
00475       bool valid = true;
00476       if(point > 0){
00477     // There are more chars in the line. check +1
00478         if(line.at(point-1) != ' ')
00479       valid = false;
00480       }
00481       point += option.length();
00482       if(point < line.length()-1){
00483     // There are more chars in the line. check -1
00484         if(line.at(point) != ' ')
00485       valid = false;
00486       }
00487       if(valid){
00488         if(found == true){
00489           qDebug(QString("Interfaces: Found multiple stanza's for search: %1 %2").arg(stanza).arg(option).latin1());
00490         }
00491         found = true;
00492         iterator = it;
00493       }
00494     }
00495   }
00496   return found;
00497 }
00498 
00505 bool Interfaces::setOption(const QStringList::Iterator &start, const QString &option, const QString &value){
00506   if(start == interfaces.end())
00507     return false;
00508   qDebug("setting option");
00509   bool found = false;
00510   bool replaced = false;
00511   QStringList::Iterator insertAt = NULL;
00512   for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) {
00513       qDebug(" Interfaces::setOption got line >%s<",(*it).latin1());
00514       // FIXME: was  not completly stupid just wrong sice all options got inserted bevore the iface line
00515       // but since it works with an empty interfaces file I (tille) will not do anything more
00516       if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) ){
00517           if (found) break;
00518 //  && it != start){
00519 //       if(!found && value != ""){
00520 //         // Got to the end of the stanza without finding it, so append it.
00521 //           qDebug(" Got to the end of the stanza without finding it, so append it.");
00522 //         interfaces.insert(--it, QString("\t%1 %2").arg(option).arg(value));
00523 //       }
00524          qDebug("found 1");
00525 //         interfaces.insert(++it, QString("\t%1 %2").arg(option).arg(value));
00526        found = true;
00527        insertAt = it;
00528 
00529      }
00530     if((*it).contains(option) && it != start && (*it).at(0) != '#'){
00531       // Found it in stanza so replace it.
00532         qDebug("found 2");
00533       if(found)
00534         qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1());
00535       found = true;
00536       replaced = true;
00537         (*it) = QString("\t%1 %2").arg(option).arg(value);
00538     }
00539   }
00540   if(!found){
00541       qDebug("! found insert anyway");
00542     QStringList::Iterator p = start;
00543     interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value));
00544     found = true;
00545   }
00546 
00547   if(found && !replaced){
00548       qDebug("found iface but not the option so insert it here...");
00549       interfaces.insert(++insertAt, QString("\t%1 %2").arg(option).arg(value));
00550   }
00551   return found;
00552 }
00553 
00559 bool Interfaces::removeStanza(QStringList::Iterator &stanza){
00560   if(stanza == interfaces.end())
00561     return false;
00562   (*stanza) = "";
00563   return removeAllOptions(stanza);
00564 }
00565 
00572 bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option){
00573   if(start == interfaces.end())
00574     return false;
00575 
00576   bool found = false;
00577   for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) {
00578     if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO))  && it != start){
00579       // got to the end without finding it
00580       break;
00581     }
00582     if((*it).contains(option) && it != start && (*it).at(0) != '#'){
00583       // Found it in stanza so replace it.
00584       if(found)
00585         qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1());
00586       found = true;
00587       it = interfaces.remove( it ); // we really want to remove the line
00588       --it; // we do ++it later in the head of the for loop
00589     }
00590   }
00591   return found;
00592 }
00593 
00600 bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option, const QString &value){
00601   if(start == interfaces.end())
00602     return false;
00603 
00604   bool found = false;
00605   for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) {
00606     if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO))  && it != start){
00607       // got to the end without finding it
00608       break;
00609     }
00610     if((*it).contains(option) && (*it).contains(value) && it != start && (*it).at(0) != '#'){
00611       // Found it in stanza so replace it.
00612       if(found)
00613         qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1());
00614       found = true;
00615       it = interfaces.remove( it ); // we really want to remove the line
00616       --it; // we do ++it later in the head of the for loop
00617     }
00618   }
00619   return found;
00620 }
00621 
00627 bool Interfaces::removeAllOptions(const QStringList::Iterator &start){
00628   if(start == interfaces.end())
00629     return false;
00630 
00631   QStringList::Iterator it = start;
00632   it = ++it;
00633   for (; it != interfaces.end(); ++it ) {
00634     if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO))  && it != start){
00635       break;
00636     }
00637     it = interfaces.remove(it);
00638     it = --it;
00639   }
00640   // Leave a space between this interface and the next.
00641   interfaces.insert(it, QString(""));
00642   return true;
00643 }
00644 
00652 QString Interfaces::getOption(const QStringList::Iterator &start, const QString &option, bool &error){
00653   if(start == interfaces.end()){
00654     error = false;
00655     return QString();
00656   }
00657 
00658   QString value;
00659   bool found = false;
00660   for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) {
00661     if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO))  && it != start){
00662       break;
00663     }
00664     if((*it).contains(option) && (*it).at(0) != '#'){
00665       if(found)
00666         qDebug(QString("Interfaces: getOption found more then one value: %1 for option: %2 in stanza %3").arg((*it)).arg(option).arg((*start)).latin1());
00667       found = true;
00668       QString line = (*it).simplifyWhiteSpace();
00669       int space = line.find(" ", option.length());
00670       if(space != -1){
00671         value = line.mid(space+1, line.length());
00672         break;
00673       }
00674     }
00675   }
00676   error = !found;
00677   return value;
00678 }
00679 
00685 bool Interfaces::write(){
00686   QFile::remove(interfacesFile);
00687   QFile file(interfacesFile);
00688 
00689   if (!file.open(IO_ReadWrite)){
00690     qDebug(QString("Interfaces: Can't open file: %1 for writing.").arg(interfacesFile).latin1());
00691     return false;
00692   }
00693   QTextStream stream( &file );
00694   int whiteSpaceCount = 0;
00695   for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) {
00696     QString line = (*it).simplifyWhiteSpace();
00697     line = line.replace(QRegExp(" "),"");
00698     if(line.length() == 0)
00699       whiteSpaceCount++;
00700     else
00701       whiteSpaceCount = 0;
00702     if(whiteSpaceCount < 2){
00703       qDebug((*it).latin1());
00704       stream << (*it) << '\n';
00705     }
00706   }
00707   file.close();
00708   return true;
00709 }
00710 
00711 // interfaces.cpp
00712 
KDE Logo
This file is part of the documentation for OPIE Version 1.0.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:26:44 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001