00001 #include "interfaces.h"
00002
00003 #include <opie2/odebug.h>
00004
00005 #include <qcheckbox.h>
00006 #include <qfile.h>
00007 #include <qtextstream.h>
00008 #include <qregexp.h>
00009
00010
00011 #define AUTO "auto"
00012 #define IFACE "iface"
00013 #define MAPPING "mapping"
00014
00021 Interfaces::Interfaces(QString useInterfacesFile){
00022 acceptedFamily.append(INTERFACES_FAMILY_INET);
00023 acceptedFamily.append(INTERFACES_FAMILY_IPX);
00024 acceptedFamily.append(INTERFACES_FAMILY_INET6);
00025
00026 interfacesFile = useInterfacesFile;
00027 QFile file(interfacesFile);
00028 if (!file.open(IO_ReadOnly)){
00029 odebug << "Interfaces: Can't open file: " << interfacesFile.latin1() << " for reading." << oendl;
00030 currentIface = interfaces.end();
00031 currentMapping = interfaces.end();
00032 return;
00033 }
00034 QTextStream stream( &file );
00035 QString line;
00036 while ( !stream.eof() ) {
00037 line += stream.readLine();
00038 line += "\n";
00039 }
00040 file.close();
00041 interfaces = QStringList::split("\n", line, true);
00042
00043 currentIface = interfaces.end();
00044 currentMapping = interfaces.end();
00045 }
00046
00047
00054 QStringList Interfaces::getInterfaceList(){
00055 QStringList list;
00056 for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) {
00057 QString line = (*it).simplifyWhiteSpace();
00058 if(line.contains(IFACE) && line.at(0) != '#'){
00059 line = line.mid(QString(IFACE).length() +1, line.length());
00060 line = line.simplifyWhiteSpace();
00061 int findSpace = line.find(" ");
00062 if( findSpace >= 0){
00063 line = line.mid(0, findSpace);
00064 list.append(line);
00065 }
00066 }
00067 }
00068 return list;
00069 }
00070
00077 bool Interfaces::isAuto(const QString &interface) const {
00078 QStringList autoLines = interfaces.grep(QRegExp(AUTO));
00079 QStringList awi = autoLines.grep(QRegExp(interface));
00080 if(awi.count() > 1)
00081 odebug << QString("Interfaces: Found more then auto group with interface: %1.").arg(interface).latin1() << oendl;
00082 return awi.count() > 0;
00083 }
00084
00091 bool Interfaces::setAuto(const QString &interface, bool setAuto){
00092
00093 if(isAuto(interface) == setAuto)
00094 return false;
00095
00096 bool changed = false;
00097 for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) {
00098 if((*it).contains(AUTO)){
00099
00100 if(setAuto){
00101 (*it) = (*it) += " " + interface;
00102
00103 (*it) = (*it).simplifyWhiteSpace();
00104 changed = true;
00105 break;
00106 }
00107
00108 else{
00109 if((*it).contains(interface)){
00110 (*it) = (*it).replace(QRegExp(interface), "");
00111
00112 if(((*it).simplifyWhiteSpace()).replace(QRegExp(" "),"") == AUTO)
00113 (*it) = "";
00114 changed = true;
00115
00116 }
00117 }
00118 }
00119 }
00120
00121 if(!changed && setAuto)
00122 interfaces.append(QString(AUTO" %1").arg(interface));
00123 return true;
00124 }
00125
00133 bool Interfaces::setInterface(QString interface){
00134 interface = interface.simplifyWhiteSpace();
00135 interface = interface.replace(QRegExp(" "), "");
00136 return setStanza(IFACE, interface, currentIface);
00137 }
00138
00143 bool Interfaces::isInterfaceSet() const {
00144 return (interfaces.end() != currentIface);
00145 }
00146
00156 bool Interfaces::addInterface(const QString &interface, const QString &family, const QString &method){
00157 odebug << "Interfaces::addInterface(" << interface.latin1() << ")" << oendl;
00158 if(0 == acceptedFamily.contains(family))
00159 return false;
00160 QString newInterface = interface.simplifyWhiteSpace();
00161 newInterface = newInterface.replace(QRegExp(" "), "");
00162 interfaces.append("");
00163 interfaces.append(QString(IFACE " %1 %2 %3").arg(newInterface).arg(family).arg(method));
00164 return true;
00165 }
00166
00172 bool Interfaces::copyInterface(const QString &interface, const QString &newInterface){
00173 odebug << "copy interface " << interface.latin1() << " to " << newInterface.latin1() << "" << oendl;
00174 if(!setInterface(interface))
00175 return false;
00176
00177
00178 QStringList::Iterator it = currentIface;
00179 it++;
00180
00181
00182 bool error;
00183 addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error));
00184 if(!setInterface(newInterface))
00185 return false;
00186
00187 QStringList::Iterator newIface = currentIface;
00188 newIface++;
00189
00190
00191 for ( ; it != interfaces.end(); ++it ){
00192 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)))
00193 break;
00194 newIface = interfaces.insert(newIface, *it);
00195 }
00196
00197 return true;
00198 }
00199
00204 bool Interfaces::removeInterface(){
00205 return removeStanza(currentIface);
00206 }
00207
00213 QString Interfaces::getInterfaceName(bool &error){
00214 if(currentIface == interfaces.end()){
00215 error = true;
00216 return QString();
00217 }
00218 QString line = (*currentIface);
00219 line = line.mid(QString(IFACE).length() +1, line.length());
00220 line = line.simplifyWhiteSpace();
00221 int findSpace = line.find(" ");
00222 if( findSpace < 0){
00223 error = true;
00224 return QString();
00225 }
00226 error = false;
00227 return line.mid(0, findSpace);
00228 }
00229
00235 QString Interfaces::getInterfaceFamily(bool &error){
00236 QString name = getInterfaceName(error);
00237 if(error)
00238 return QString();
00239 QString line = (*currentIface);
00240 line = line.mid(QString(IFACE).length() +1, line.length());
00241 line = line.mid(name.length()+1, line.length());
00242 line = line.simplifyWhiteSpace();
00243 int findSpace = line.find(" ");
00244 if( findSpace < 0){
00245 error = true;
00246 return QString();
00247 }
00248 error = false;
00249 return line.mid(0, findSpace);
00250 }
00251
00258 QString Interfaces::getInterfaceMethod(bool &error){
00259 QString name = getInterfaceName(error);
00260 if(error)
00261 return QString();
00262 QString family = getInterfaceFamily(error);
00263 if(error)
00264 return QString();
00265 QString line = (*currentIface);
00266 line = line.mid(QString(IFACE).length()+1, line.length());
00267 line = line.mid(name.length()+1, line.length());
00268 line = line.mid(family.length()+1, line.length());
00269 line = line.simplifyWhiteSpace();
00270 error = false;
00271 return line;
00272 }
00273
00279 bool Interfaces::setInterfaceName(const QString &newName){
00280 odebug << "setInterfaceName " << newName.latin1() << "" << oendl;
00281 if(currentIface == interfaces.end())
00282 return false;
00283 QString name = newName.simplifyWhiteSpace();
00284 name = name.replace(QRegExp(" "), "");
00285 bool returnValue = false;
00286 QString tmp = QString("iface %1 %2 %3").arg(name).arg(getInterfaceFamily(returnValue)).arg(getInterfaceMethod(returnValue));
00287 odebug << "setting " << tmp.latin1() << "" << oendl;
00288
00289 (*currentIface) = tmp;
00290 return !returnValue;
00291 }
00292
00299 bool Interfaces::setInterfaceFamily(const QString &newName){
00300 if(currentIface == interfaces.end())
00301 return false;
00302 if(acceptedFamily.contains(newName)==0)
00303 return false;
00304 bool returnValue = false;
00305 (*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(newName).arg(getInterfaceMethod(returnValue));
00306 return !returnValue;
00307 }
00308
00314 bool Interfaces::setInterfaceMethod(const QString &newName){
00315 if(currentIface == interfaces.end())
00316 return false;
00317 bool returnValue = false;
00318 (*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(getInterfaceFamily(returnValue)).arg(newName);
00319 return !returnValue;
00320 }
00321
00332 QString Interfaces::getInterfaceOption(const QString &option, bool &error){
00333 return getOption(currentIface, option, error);
00334 }
00335
00346 bool Interfaces::setInterfaceOption(const QString &option, const QString &value){
00347 if( value.stripWhiteSpace().isEmpty() )
00348 return removeInterfaceOption( option );
00349
00350 odebug << "iface >" << (*currentIface).latin1() << "< option >" << option.latin1() << "< value >" << value.latin1() << "<" << oendl;
00351 return setOption(currentIface, option, value);
00352 }
00353
00360 bool Interfaces::removeInterfaceOption(const QString &option){
00361 return removeOption(currentIface, option);
00362 }
00363
00371 bool Interfaces::removeInterfaceOption(const QString &option, const QString &value){
00372 return removeOption(currentIface, option, value);
00373 }
00374
00379 bool Interfaces::removeAllInterfaceOptions(){
00380 return removeAllOptions(currentIface);
00381 }
00382
00390 bool Interfaces::setMapping(const QString &interface){
00391 QString interfaceName = interface.simplifyWhiteSpace();
00392 interfaceName = interfaceName.replace(QRegExp(" "), "");
00393 return setStanza(MAPPING, interfaceName, currentMapping);
00394 }
00395
00400 void Interfaces::addMapping(const QString &option){
00401 interfaces.append("");
00402 interfaces.append(QString(MAPPING " %1").arg(option));
00403 }
00404
00409 bool Interfaces::removeMapping(){
00410 return removeStanza(currentMapping);
00411 }
00412
00419 bool Interfaces::setMap(const QString &map, const QString &value){
00420 return setOption(currentMapping, map, value);
00421 }
00422
00429 bool Interfaces::removeMap(const QString &map, const QString &value){
00430 return removeOption(currentMapping, map, value);
00431 }
00432
00439 QString Interfaces::getMap(const QString &map, bool &error){
00440 return getOption(currentMapping, map, error);
00441 }
00442
00448 bool Interfaces::setScript(const QString &argument){
00449 return setOption(currentMapping, "script", argument);
00450 }
00451
00456 QString Interfaces::getScript(bool &error){
00457 return getOption(currentMapping, "script", error);
00458 }
00459
00460
00461
00470 bool Interfaces::setStanza(const QString &stanza, const QString &option, QStringList::Iterator &iterator){
00471 bool found = false;
00472 iterator = interfaces.end();
00473 for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) {
00474 QString line = (*it).simplifyWhiteSpace();
00475 if(line.contains(stanza) && line.contains(option) && line.at(0) != '#'){
00476 uint point = line.find(option);
00477 bool valid = true;
00478 if(point > 0){
00479
00480 if(line.at(point-1) != ' ')
00481 valid = false;
00482 }
00483 point += option.length();
00484 if(point < line.length()-1){
00485
00486 if(line.at(point) != ' ')
00487 valid = false;
00488 }
00489 if(valid){
00490 if(found == true){
00491 odebug << QString("Interfaces: Found multiple stanza's for search: %1 %2").arg(stanza).arg(option).latin1() << oendl;
00492 }
00493 found = true;
00494 iterator = it;
00495 }
00496 }
00497 }
00498 return found;
00499 }
00500
00507 bool Interfaces::setOption(const QStringList::Iterator &start, const QString &option, const QString &value){
00508 if(start == interfaces.end())
00509 return false;
00510 odebug << "setting option" << oendl;
00511 bool found = false;
00512 bool replaced = false;
00513 QStringList::Iterator insertAt = NULL;
00514 for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) {
00515 odebug << " Interfaces::setOption got line >" << (*it).latin1() << "<" << oendl;
00516
00517
00518 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) ){
00519 if (found) break;
00520
00521
00522
00523
00524
00525
00526 odebug << "found 1" << oendl;
00527
00528 found = true;
00529 insertAt = it;
00530
00531 }
00532 if((*it).contains(option) && it != start && (*it).at(0) != '#'){
00533
00534 odebug << "found 2" << oendl;
00535 if(found)
00536 odebug << QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1() << oendl;
00537 found = true;
00538 replaced = true;
00539 (*it) = QString("\t%1 %2").arg(option).arg(value);
00540 }
00541 }
00542 if(!found){
00543 odebug << "! found insert anyway" << oendl;
00544 QStringList::Iterator p = start;
00545 interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value));
00546 found = true;
00547 }
00548
00549 if(found && !replaced){
00550 odebug << "found iface but not the option so insert it here..." << oendl;
00551 interfaces.insert(++insertAt, QString("\t%1 %2").arg(option).arg(value));
00552 }
00553 return found;
00554 }
00555
00561 bool Interfaces::removeStanza(QStringList::Iterator &stanza){
00562 if(stanza == interfaces.end())
00563 return false;
00564 (*stanza) = "";
00565 return removeAllOptions(stanza);
00566 }
00567
00574 bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option){
00575 if(start == interfaces.end())
00576 return false;
00577
00578 bool found = false;
00579 for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) {
00580 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){
00581
00582 break;
00583 }
00584 if((*it).contains(option) && it != start && (*it).at(0) != '#'){
00585
00586 if(found)
00587 odebug << QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1() << oendl;
00588 found = true;
00589 it = interfaces.remove( it );
00590 --it;
00591 }
00592 }
00593 return found;
00594 }
00595
00602 bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option, const QString &value){
00603 if(start == interfaces.end())
00604 return false;
00605
00606 bool found = false;
00607 for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) {
00608 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){
00609
00610 break;
00611 }
00612 if((*it).contains(option) && (*it).contains(value) && it != start && (*it).at(0) != '#'){
00613
00614 if(found)
00615 odebug << QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1() << oendl;
00616 found = true;
00617 it = interfaces.remove( it );
00618 --it;
00619 }
00620 }
00621 return found;
00622 }
00623
00629 bool Interfaces::removeAllOptions(const QStringList::Iterator &start){
00630 if(start == interfaces.end())
00631 return false;
00632
00633 QStringList::Iterator it = start;
00634 it = ++it;
00635 for (; it != interfaces.end(); ++it ) {
00636 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){
00637 break;
00638 }
00639 it = interfaces.remove(it);
00640 it = --it;
00641 }
00642
00643 interfaces.insert(it, QString(""));
00644 return true;
00645 }
00646
00654 QString Interfaces::getOption(const QStringList::Iterator &start, const QString &option, bool &error){
00655 if(start == interfaces.end()){
00656 error = false;
00657 return QString();
00658 }
00659
00660 QString value;
00661 bool found = false;
00662 for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) {
00663 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){
00664 break;
00665 }
00666 if((*it).contains(option) && (*it).at(0) != '#'){
00667 if(found)
00668 odebug << QString("Interfaces: getOption found more then one value: %1 for option: %2 in stanza %3").arg((*it)).arg(option).arg((*start)).latin1() << oendl;
00669 found = true;
00670 QString line = (*it).simplifyWhiteSpace();
00671 int space = line.find(" ", option.length());
00672 if(space != -1){
00673 value = line.mid(space+1, line.length());
00674 break;
00675 }
00676 }
00677 }
00678 error = !found;
00679 return value;
00680 }
00681
00687 bool Interfaces::write(){
00688 QFile::remove(interfacesFile);
00689 QFile file(interfacesFile);
00690
00691 if (!file.open(IO_ReadWrite)){
00692 odebug << QString("Interfaces: Can't open file: %1 for writing.").arg(interfacesFile).latin1() << oendl;
00693 return false;
00694 }
00695 QTextStream stream( &file );
00696 int whiteSpaceCount = 0;
00697 for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) {
00698 QString line = (*it).simplifyWhiteSpace();
00699 line = line.replace(QRegExp(" "),"");
00700 if(line.length() == 0)
00701 whiteSpaceCount++;
00702 else
00703 whiteSpaceCount = 0;
00704 if(whiteSpaceCount < 2){
00705 odebug << (*it).latin1() << oendl;
00706 stream << (*it) << '\n';
00707 }
00708 }
00709 file.close();
00710 return true;
00711 }
00712
00713
00714