4 files changed, 7 insertions, 0 deletions
diff --git a/noncore/settings/networksettings/interfaces/interface.cpp b/noncore/settings/networksettings/interfaces/interface.cpp index d2b106a..46f3e19 100644 --- a/noncore/settings/networksettings/interfaces/interface.cpp +++ b/noncore/settings/networksettings/interfaces/interface.cpp @@ -1,302 +1,305 @@ /** * $Author$ * $Date$ */ #include "interface.h" + +#include <opie2/odebug.h> + #include <qdatetime.h> #include <qfile.h> #include <qdir.h> #include <qfileinfo.h> #include <qtextstream.h> #define IFCONFIG "/sbin/ifconfig" #define DHCP_INFO_DIR "/etc/dhcpc" #include <stdio.h> #include <stdlib.h> Interface::Interface(QObject * parent, const char * name, bool newSatus): QObject(parent, name), hardwareName("Unknown"), moduleOwner(NULL), status(newSatus), attached(false), dhcp(false), macAddress(""), ip("0.0.0.0"), broadcast(""), subnetMask("0.0.0.0"){ refresh(); } /** * Set status * @param newStatus - the new status * emit updateInterface */ void Interface::setStatus(bool newStatus){ if(status != newStatus){ status = newStatus; refresh(); } }; /** * Set if attached or not (802.11 card pulled out for example) * @param isAttached - if attached * emit updateInterface */ void Interface::setAttached(bool isAttached){ attached = isAttached; emit(updateInterface(this)); }; /** * Set Hardware name * @param name - the new name * emit updateInterface */ void Interface::setHardwareName(const QString &name){ hardwareName = name; emit(updateInterface(this)); }; /** * Set Module owner * @param owner - the new owner * emit updateInterface */ void Interface::setModuleOwner(Module *owner){ moduleOwner = owner; emit(updateInterface(this)); }; /** * Try to start the interface. */ void Interface::start(){ // check to see if we are already running. if(true == status){ emit (updateMessage("Unable to start interface,\n already started")); return; } int ret = system(QString("%1 %2 up").arg(IFCONFIG).arg(this->name()).latin1()); // See if it was successful... if(ret != 0){ emit (updateMessage("Starting interface failed")); return; } status = true; refresh(); emit (updateMessage("Start successful")); } /** * Try to stop the interface. */ void Interface::stop(){ // check to see if we are already stopped. if(false == status){ emit (updateMessage("Unable to stop interface,\n already stopped")); return; } int ret = system(QString("%1 %2 down").arg(IFCONFIG).arg(this->name()).latin1()); if(ret != 0){ emit (updateMessage("Stopping interface failed")); return; } status = false; refresh(); emit (updateMessage("Stop successful")); } /** * Try to restart the interface. */ void Interface::restart(){ stop(); start(); } /** * Try to refresh the information about the interface. * First call ifconfig, then check the dhcp-info file * @return bool true if successful. */ bool Interface::refresh(){ // See if we are up. if(status == false){ macAddress = ""; ip = "0.0.0.0"; subnetMask = "0.0.0.0"; broadcast = ""; dhcp = false; dhcpServerIp = ""; leaseObtained = ""; leaseExpires = ""; emit(updateInterface(this)); return true; } QString fileName = QString("/tmp/%1_ifconfig_info").arg(this->name()); int ret = system(QString("LANG=C %1 %2 > %3").arg(IFCONFIG).arg(this->name()).arg(fileName).latin1()); if(ret != 0){ odebug << QString("Interface: Ifconfig return value: %1, is not 0").arg(ret).latin1() << oendl; return false; } QFile file(fileName); if (!file.open(IO_ReadOnly)){ odebug << QString("Interface: Can't open file: %1").arg(fileName).latin1() << oendl; return false; } // Set to the defaults macAddress = ""; ip = "0.0.0.0"; subnetMask = "0.0.0.0"; broadcast = ""; QTextStream stream( &file ); QString line; while ( !stream.eof() ) { line = stream.readLine(); if(line.contains("HWaddr")){ int mac = line.find("HWaddr"); macAddress = line.mid(mac+7, line.length()); } if(line.contains("inet addr")){ int ipl = line.find("inet addr"); int space = line.find(" ", ipl+10); ip = line.mid(ipl+10, space-ipl-10); } if(line.contains("Mask")){ int mask = line.find("Mask"); subnetMask = line.mid(mask+5, line.length()); } if(line.contains("Bcast")){ int mask = line.find("Bcast"); int space = line.find(" ", mask+6); broadcast = line.mid(mask+6, space-mask-6); } } file.close(); QFile::remove(fileName); // DHCP TESTING // reset DHCP info dhcpServerIp = ""; leaseObtained = ""; leaseExpires = ""; dhcp = false; QString dhcpDirectory(DHCP_INFO_DIR); QDir d(dhcpDirectory); if(!d.exists(dhcpDirectory)) dhcpDirectory = "/var/run"; // See if we have QString dhcpFile(QString(dhcpDirectory+"/dhcpcd-%1.info").arg(this->name())); // If there is no DHCP information then exit now with no errors. if(!QFile::exists(dhcpFile)){ emit(updateInterface(this)); return true; } file.setName(dhcpFile); if (!file.open(IO_ReadOnly)){ odebug << QString("Interface: Can't open file: %1").arg(dhcpFile).latin1() << oendl; return false; } // leaseTime and renewalTime and used if pid and deamon exe can be accessed. int leaseTime = 0; int renewalTime = 0; stream.setDevice( &file ); while ( !stream.eof() ) { line = stream.readLine(); if(line.contains("DHCPSIADDR=")) dhcpServerIp = line.mid(11, line.length()); if(line.contains("LEASETIME=")) leaseTime = line.mid(10, line.length()).toInt(); if(line.contains("RENEWALTIME=")) renewalTime = line.mid(12, line.length()).toInt(); } file.close(); //odebug << QString("Interface: leaseTime: %1").arg(leaseTime).latin1() << oendl; //odebug << QString("Interface: renewalTime: %1").arg(renewalTime).latin1() << oendl; // Get the pid of the deamond dhcpFile = (QString(dhcpDirectory+"/dhcpcd-%1.pid").arg(this->name())); file.setName(dhcpFile); if (!file.open(IO_ReadOnly)){ odebug << QString("Interface: Can't open file: %1").arg(dhcpFile).latin1() << oendl; return false; } int pid = -1; stream.setDevice( &file ); while ( !stream.eof() ) { line = stream.readLine(); pid = line.toInt(); } file.close(); if( pid == -1){ odebug << "Interface: Could not get pid of dhcpc deamon." << oendl; return false; } // Get the start running time of the deamon fileName = (QString("/proc/%1/stat").arg(pid)); file.setName(fileName); stream.setDevice( &file ); if (!file.open(IO_ReadOnly)){ odebug << QString("Interface: Can't open file: %1").arg(fileName).latin1() << oendl; return false; } while ( !stream.eof() ) { line = stream.readLine(); } file.close(); long time = 0; // Grab the start time // pid com state ppid pgrp session tty_nr tpgid flags sscanf(line.latin1(), "%*d %*s %*c %*d %*d %*d %*d %*d %*u " // minflt cminflt majflt cmajflt utime stime cutime cstime priority "%*u %*u %*u %*u %*u %*u %*d %*d %*d " // nice 0 itrealvalue starttime "%*d %*d %*d %lu", (long*) &time); time = time/100; QDateTime datetime(QDateTime::currentDateTime()); // Get the uptime of the computer. QFile f("/proc/uptime"); if ( f.open(IO_ReadOnly) ) { // file opened successfully QTextStream t( &f ); // use a text stream int sec = 0; t >> sec; datetime = datetime.addSecs((-1*sec)); f.close(); } else{ odebug << "Interface: Can't open /proc/uptime to retrive uptime." << oendl; return false; } datetime = datetime.addSecs(time); //odebug << QString("Interface: %1 %2").arg(datetime.toString()).arg(pid).latin1() << oendl; // Calculate the start and renew times leaseObtained = datetime.toString(); // Calculate the start and renew times datetime = datetime.addSecs(leaseTime); leaseExpires = datetime.toString(); dhcp = true; emit(updateInterface(this)); return true; } // interface.cpp diff --git a/noncore/settings/networksettings/interfaces/interfaces.cpp b/noncore/settings/networksettings/interfaces/interfaces.cpp index e283926..5ce4b58 100644 --- a/noncore/settings/networksettings/interfaces/interfaces.cpp +++ b/noncore/settings/networksettings/interfaces/interfaces.cpp @@ -1,712 +1,714 @@ #include "interfaces.h" +#include <opie2/odebug.h> + #include <qcheckbox.h> #include <qfile.h> #include <qtextstream.h> #include <qregexp.h> // The three stanza's #define AUTO "auto" #define IFACE "iface" #define MAPPING "mapping" /** * Constructor. Reads in the interfaces file and then split the file up by * the \n for interfaces variable. * @param useInterfacesFile if an interface file other then the default is * desired to be used it should be passed in. */ Interfaces::Interfaces(QString useInterfacesFile){ acceptedFamily.append(INTERFACES_FAMILY_INET); acceptedFamily.append(INTERFACES_FAMILY_IPX); acceptedFamily.append(INTERFACES_FAMILY_INET6); interfacesFile = useInterfacesFile; QFile file(interfacesFile); if (!file.open(IO_ReadOnly)){ odebug << "Interfaces: Can't open file: " << interfacesFile.latin1() << " for reading." << oendl; currentIface = interfaces.end(); currentMapping = interfaces.end(); return; } QTextStream stream( &file ); QString line; while ( !stream.eof() ) { line += stream.readLine(); line += "\n"; } file.close(); interfaces = QStringList::split("\n", line, true); currentIface = interfaces.end(); currentMapping = interfaces.end(); } /** * Get a list of all interfaces in the interface file. Useful for * hardware that is not currently connected such as an 802.11b card * not plugged in, but configured for when it is plugged in. * @return Return string list of interfaces. **/ QStringList Interfaces::getInterfaceList(){ QStringList list; for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { QString line = (*it).simplifyWhiteSpace(); if(line.contains(IFACE) && line.at(0) != '#'){ line = line.mid(QString(IFACE).length() +1, line.length()); line = line.simplifyWhiteSpace(); int findSpace = line.find(" "); if( findSpace >= 0){ line = line.mid(0, findSpace); list.append(line); } } } return list; } /** * Find out if interface is in an "auto" group or not. * Report any duplicates such as eth0 being in two differnt auto's * @param interface interface to check to see if it is on or not. * @return true is interface is in auto */ bool Interfaces::isAuto(const QString &interface) const { QStringList autoLines = interfaces.grep(QRegExp(AUTO)); QStringList awi = autoLines.grep(QRegExp(interface)); if(awi.count() > 1) odebug << QString("Interfaces: Found more then auto group with interface: %1.").arg(interface).latin1() << oendl; return awi.count() > 0; } /** * Attempt to set the auto option for interface to setAuto. * @param interface the interface to set * @param setAuto the value to set interface to. * @return false if already set to setAuto. * */ bool Interfaces::setAuto(const QString &interface, bool setAuto){ // Don't need to set it if it is already set. if(isAuto(interface) == setAuto) return false; bool changed = false; for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { if((*it).contains(AUTO)){ //We know that they are not in any group so let add to this auto. if(setAuto){ (*it) = (*it) += " " + interface; // Don't care to have such thins as: auto eth0 lo usb0 (*it) = (*it).simplifyWhiteSpace(); changed = true; break; } // else see if we need to remove from this one else{ if((*it).contains(interface)){ (*it) = (*it).replace(QRegExp(interface), ""); // if AUTO is the only thing left clear the line if(((*it).simplifyWhiteSpace()).replace(QRegExp(" "),"") == AUTO) (*it) = ""; changed = true; // Don't break because we want to make sure we remove all cases. } } } } // In the case where there is no AUTO field add one. if(!changed && setAuto) interfaces.append(QString(AUTO" %1").arg(interface)); return true; } /** * Set the current interface to interface. This needs to be done before you * can call getFamily(), getMethod, and get/setOption(). * @param interface the name of the interface to set. All whitespace is * removed from the interface name. * @return bool true if it is successful. */ bool Interfaces::setInterface(QString interface){ interface = interface.simplifyWhiteSpace(); interface = interface.replace(QRegExp(" "), ""); return setStanza(IFACE, interface, currentIface); } /** * A quick helper funtion to see if the current interface is set. * @return bool true if set, false otherwise. */ bool Interfaces::isInterfaceSet() const { return (interfaces.end() != currentIface); } /** * Add a new interface of with the settings - family and method * @param interface the name of the interface to set. All whitespace is * removed from the interface name. * @param family the family of this interface inet or inet, ipx or inet6 * Must of one of the families defined in interfaces.h * @param method for the family. see interfaces man page for family methods. * @return true if successful. */ bool Interfaces::addInterface(const QString &interface, const QString &family, const QString &method){ odebug << "Interfaces::addInterface(" << interface.latin1() << ")" << oendl; if(0 == acceptedFamily.contains(family)) return false; QString newInterface = interface.simplifyWhiteSpace(); newInterface = newInterface.replace(QRegExp(" "), ""); interfaces.append(""); interfaces.append(QString(IFACE " %1 %2 %3").arg(newInterface).arg(family).arg(method)); return true; } /** * Copies interface with name interface to name newInterface * @param newInterface name of the new interface. * @return bool true if successful */ bool Interfaces::copyInterface(const QString &interface, const QString &newInterface){ odebug << "copy interface " << interface.latin1() << " to " << newInterface.latin1() << "" << oendl; if(!setInterface(interface)) return false; // Store the old interface and bump past the stanza line. QStringList::Iterator it = currentIface; it++; // Add the new interface bool error; addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error)); if(!setInterface(newInterface)) return false; QStringList::Iterator newIface = currentIface; newIface++; // Copy all of the lines for ( ; it != interfaces.end(); ++it ){ if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO))) break; newIface = interfaces.insert(newIface, *it); } return true; } /** * Remove the currently selected interface and all of its options. * @return bool if successful or not. */ bool Interfaces::removeInterface(){ return removeStanza(currentIface); } /** * Gets the hardware name of the interface that is currently selected. * @return QString name of the hardware interface (eth0, usb2, wlan1...). * @param error set to true if any error occurs, false otherwise. */ QString Interfaces::getInterfaceName(bool &error){ if(currentIface == interfaces.end()){ error = true; return QString(); } QString line = (*currentIface); line = line.mid(QString(IFACE).length() +1, line.length()); line = line.simplifyWhiteSpace(); int findSpace = line.find(" "); if( findSpace < 0){ error = true; return QString(); } error = false; return line.mid(0, findSpace); } /** * Gets the family name of the interface that is currently selected. * @return QString name of the family (inet, inet6, ipx). * @param error set to true if any error occurs, false otherwise. */ QString Interfaces::getInterfaceFamily(bool &error){ QString name = getInterfaceName(error); if(error) return QString(); QString line = (*currentIface); line = line.mid(QString(IFACE).length() +1, line.length()); line = line.mid(name.length()+1, line.length()); line = line.simplifyWhiteSpace(); int findSpace = line.find(" "); if( findSpace < 0){ error = true; return QString(); } error = false; return line.mid(0, findSpace); } /** * Gets the method of the interface that is currently selected. * @return QString name of the method such as staic or dhcp. * See the man page of interfaces for possible methods depending on the family. * @param error set to true if any error occurs, false otherwise. */ QString Interfaces::getInterfaceMethod(bool &error){ QString name = getInterfaceName(error); if(error) return QString(); QString family = getInterfaceFamily(error); if(error) return QString(); QString line = (*currentIface); line = line.mid(QString(IFACE).length()+1, line.length()); line = line.mid(name.length()+1, line.length()); line = line.mid(family.length()+1, line.length()); line = line.simplifyWhiteSpace(); error = false; return line; } /** * Sets the interface name to newName. * @param newName the new name of the interface. All whitespace is removed. * @return bool true if successful. */ bool Interfaces::setInterfaceName(const QString &newName){ odebug << "setInterfaceName " << newName.latin1() << "" << oendl; if(currentIface == interfaces.end()) return false; QString name = newName.simplifyWhiteSpace(); name = name.replace(QRegExp(" "), ""); bool returnValue = false; QString tmp = QString("iface %1 %2 %3").arg(name).arg(getInterfaceFamily(returnValue)).arg(getInterfaceMethod(returnValue)); odebug << "setting " << tmp.latin1() << "" << oendl; (*currentIface) = tmp; return !returnValue; } /** * Sets the interface family to newName. * @param newName the new name of the interface. Must be one of the families * defined in the interfaces.h file. * @return bool true if successful. */ bool Interfaces::setInterfaceFamily(const QString &newName){ if(currentIface == interfaces.end()) return false; if(acceptedFamily.contains(newName)==0) return false; bool returnValue = false; (*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(newName).arg(getInterfaceMethod(returnValue)); return !returnValue; } /** * Sets the interface method to newName * @param newName the new name of the interface * @return bool true if successful. */ bool Interfaces::setInterfaceMethod(const QString &newName){ if(currentIface == interfaces.end()) return false; bool returnValue = false; (*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(getInterfaceFamily(returnValue)).arg(newName); return !returnValue; } /** * Get a value for an option in the currently selected interface. For example * calling getInterfaceOption("address") on the following stanza would * return 192.168.1.1. * iface eth0 static * address 192.168.1.1 * @param option the options to get the value. * @param error set to true if any error occurs, false otherwise. * @return QString the options value. QString::null if error == true */ QString Interfaces::getInterfaceOption(const QString &option, bool &error){ return getOption(currentIface, option, error); } /** * Set a value for an option in the currently selected interface. If option * doesn't exist then it is added along with the value. * If value isEmpty() then we will remove the option * * @param option the options to set the value. * @param value the value that option should be set to. * @param error set to true if any error occurs, false otherwise. * @return QString the options value. QString::null if error == true */ bool Interfaces::setInterfaceOption(const QString &option, const QString &value){ if( value.stripWhiteSpace().isEmpty() ) return removeInterfaceOption( option ); odebug << "iface >" << (*currentIface).latin1() << "< option >" << option.latin1() << "< value >" << value.latin1() << "<" << oendl; return setOption(currentIface, option, value); } /** * Removes a value for an option in the currently selected interface. * @param option the options to set the value. * @param error set to true if any error occurs, false otherwise. * @return QString the options value. QString::null if error == true */ bool Interfaces::removeInterfaceOption(const QString &option){ return removeOption(currentIface, option); } /** * Removes a value for an option in the currently selected interface. * @param option the options to set the value. * @param value the value that option should be set to. * @param error set to true if any error occurs, false otherwise. * @return QString the options value. QString::null if error == true */ bool Interfaces::removeInterfaceOption(const QString &option, const QString &value){ return removeOption(currentIface, option, value); } /** * Removes all of the options from the currently selected interface. * @return bool error if if successful */ bool Interfaces::removeAllInterfaceOptions(){ return removeAllOptions(currentIface); } /** * Set the current map to interface's map. This needs to be done before you * can call addMapping(), set/getMap(), and get/setScript(). * @param interface the name of the interface to set. All whitespace is * removed from the interface name. * @return bool true if it is successful. */ bool Interfaces::setMapping(const QString &interface){ QString interfaceName = interface.simplifyWhiteSpace(); interfaceName = interfaceName.replace(QRegExp(" "), ""); return setStanza(MAPPING, interfaceName, currentMapping); } /** * Adds a new Mapping to the interfaces file with interfaces. * @param interface the name(s) of the interfaces to set to this mapping */ void Interfaces::addMapping(const QString &option){ interfaces.append(""); interfaces.append(QString(MAPPING " %1").arg(option)); } /** * Remove the currently selected map and all of its options. * @return bool if successful or not. */ bool Interfaces::removeMapping(){ return removeStanza(currentMapping); } /** * Set a map option within a mapping. * @param map map to use * @param value value to go with map * @return bool true if it is successful. */ bool Interfaces::setMap(const QString &map, const QString &value){ return setOption(currentMapping, map, value); } /** * Removes a map option within a mapping. * @param map map to use * @param value value to go with map * @return bool true if it is successful. */ bool Interfaces::removeMap(const QString &map, const QString &value){ return removeOption(currentMapping, map, value); } /** * Get a map value within a mapping. * @param map map to get value of * @param bool true if it is successful. * @return value that goes to the map */ QString Interfaces::getMap(const QString &map, bool &error){ return getOption(currentMapping, map, error); } /** * Sets a script value of the current mapping to argument. * @param argument the script name. * @return true if successful. */ bool Interfaces::setScript(const QString &argument){ return setOption(currentMapping, "script", argument); } /** * @param error true if could not retrieve the current script argument. * @return QString the argument of the script for the current mapping. */ QString Interfaces::getScript(bool &error){ return getOption(currentMapping, "script", error); } /** * Helper function used to parse through the QStringList and put pointers in * the correct place. * @param stanza The stanza (auto, iface, mapping) to look for. * @param option string that must be in the stanza's main line. * @param interator interator to place at location of stanza if successful. * @return bool true if the stanza is found. */ bool Interfaces::setStanza(const QString &stanza, const QString &option, QStringList::Iterator &iterator){ bool found = false; iterator = interfaces.end(); for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { QString line = (*it).simplifyWhiteSpace(); if(line.contains(stanza) && line.contains(option) && line.at(0) != '#'){ uint point = line.find(option); bool valid = true; if(point > 0){ // There are more chars in the line. check +1 if(line.at(point-1) != ' ') valid = false; } point += option.length(); if(point < line.length()-1){ // There are more chars in the line. check -1 if(line.at(point) != ' ') valid = false; } if(valid){ if(found == true){ odebug << QString("Interfaces: Found multiple stanza's for search: %1 %2").arg(stanza).arg(option).latin1() << oendl; } found = true; iterator = it; } } } return found; } /** * Sets a value of an option in a stanza * @param start the start of the stanza * @param option the option to use when setting value. * @return bool true if successful, false otherwise. */ bool Interfaces::setOption(const QStringList::Iterator &start, const QString &option, const QString &value){ if(start == interfaces.end()) return false; odebug << "setting option" << oendl; bool found = false; bool replaced = false; QStringList::Iterator insertAt = NULL; for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { odebug << " Interfaces::setOption got line >" << (*it).latin1() << "<" << oendl; // FIXME: was not completly stupid just wrong sice all options got inserted bevore the iface line // but since it works with an empty interfaces file I (tille) will not do anything more if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) ){ if (found) break; // && it != start){ // if(!found && value != ""){ // // Got to the end of the stanza without finding it, so append it. // odebug << " Got to the end of the stanza without finding it, so append it." << oendl; // interfaces.insert(--it, QString("\t%1 %2").arg(option).arg(value)); // } odebug << "found 1" << oendl; // interfaces.insert(++it, QString("\t%1 %2").arg(option).arg(value)); found = true; insertAt = it; } if((*it).contains(option) && it != start && (*it).at(0) != '#'){ // Found it in stanza so replace it. odebug << "found 2" << oendl; if(found) odebug << QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1() << oendl; found = true; replaced = true; (*it) = QString("\t%1 %2").arg(option).arg(value); } } if(!found){ odebug << "! found insert anyway" << oendl; QStringList::Iterator p = start; interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value)); found = true; } if(found && !replaced){ odebug << "found iface but not the option so insert it here..." << oendl; interfaces.insert(++insertAt, QString("\t%1 %2").arg(option).arg(value)); } return found; } /** * Removes a stanza and all of its options * @param stanza the stanza to remove * @return bool true if successful. */ bool Interfaces::removeStanza(QStringList::Iterator &stanza){ if(stanza == interfaces.end()) return false; (*stanza) = ""; return removeAllOptions(stanza); } /** * Removes a option in a stanza * @param start the start of the stanza * @param option the option to remove * @return bool true if successful, false otherwise. */ bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option){ if(start == interfaces.end()) return false; bool found = false; for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ // got to the end without finding it break; } if((*it).contains(option) && it != start && (*it).at(0) != '#'){ // Found it in stanza so replace it. if(found) odebug << QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1() << oendl; found = true; it = interfaces.remove( it ); // we really want to remove the line --it; // we do ++it later in the head of the for loop } } return found; } /** * Removes a option in a stanza * @param start the start of the stanza * @param option the option to use when setting value. * @return bool true if successful, false otherwise. */ bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option, const QString &value){ if(start == interfaces.end()) return false; bool found = false; for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ // got to the end without finding it break; } if((*it).contains(option) && (*it).contains(value) && it != start && (*it).at(0) != '#'){ // Found it in stanza so replace it. if(found) odebug << QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1() << oendl; found = true; it = interfaces.remove( it ); // we really want to remove the line --it; // we do ++it later in the head of the for loop } } return found; } /** * Removes all options in a stanza * @param start the start of the stanza * @return bool true if successful, false otherwise. */ bool Interfaces::removeAllOptions(const QStringList::Iterator &start){ if(start == interfaces.end()) return false; QStringList::Iterator it = start; it = ++it; for (; it != interfaces.end(); ++it ) { if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ break; } it = interfaces.remove(it); it = --it; } // Leave a space between this interface and the next. interfaces.insert(it, QString("")); return true; } /** * Gets a value of an option in a stanza * @param start the start of the stanza * @param option the option to use when getting the value. * @param bool true if errors false otherwise. * @return QString the value of option QString::null() if error == true. */ QString Interfaces::getOption(const QStringList::Iterator &start, const QString &option, bool &error){ if(start == interfaces.end()){ error = false; return QString(); } QString value; bool found = false; for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ break; } if((*it).contains(option) && (*it).at(0) != '#'){ if(found) odebug << QString("Interfaces: getOption found more then one value: %1 for option: %2 in stanza %3").arg((*it)).arg(option).arg((*start)).latin1() << oendl; found = true; QString line = (*it).simplifyWhiteSpace(); int space = line.find(" ", option.length()); if(space != -1){ value = line.mid(space+1, line.length()); break; } } } error = !found; return value; } /** * Write out the interfaces file to the file passed into the constructor. * Removes any excess blank lines over 1 line long. * @return bool true if successful, false if not. */ bool Interfaces::write(){ QFile::remove(interfacesFile); QFile file(interfacesFile); if (!file.open(IO_ReadWrite)){ odebug << QString("Interfaces: Can't open file: %1 for writing.").arg(interfacesFile).latin1() << oendl; return false; } QTextStream stream( &file ); int whiteSpaceCount = 0; for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { QString line = (*it).simplifyWhiteSpace(); line = line.replace(QRegExp(" "),""); if(line.length() == 0) whiteSpaceCount++; else whiteSpaceCount = 0; if(whiteSpaceCount < 2){ odebug << (*it).latin1() << oendl; stream << (*it) << '\n'; } } file.close(); return true; } // interfaces.cpp diff --git a/noncore/settings/networksettings/interfaces/interfacesetupimp.cpp b/noncore/settings/networksettings/interfaces/interfacesetupimp.cpp index ec3bad3..8498759 100644 --- a/noncore/settings/networksettings/interfaces/interfacesetupimp.cpp +++ b/noncore/settings/networksettings/interfaces/interfacesetupimp.cpp @@ -1,208 +1,209 @@ #include "interfacesetupimp.h" #include "interface.h" #include <qcheckbox.h> #include <qlineedit.h> #include <qspinbox.h> #include <qgroupbox.h> #include <qlabel.h> #include <qmessagebox.h> #include <opie2/oprocess.h> #ifdef QWS #include <opie2/owait.h> +#include <opie2/odebug.h> #include <qpe/global.h> #include <qapplication.h> #endif #define DNSSCRIPT "changedns" /** * Constuctor. Set up the connection. A profile must be set. */ using namespace Opie::Ui; using namespace Opie::Core; InterfaceSetupImp::InterfaceSetupImp(QWidget* parent, const char* name, Interface *i, Interfaces *j, WFlags fl) : InterfaceSetup(parent, name, fl), interface(i), interfaces(j), delInterfaces(false){ if (j == 0) { delInterfaces = true; interfaces = new Interfaces; } } /** * Destructor */ InterfaceSetupImp::~InterfaceSetupImp(){ if(delInterfaces) { delete interfaces; } } /** * Save the current settings, then write out the interfaces file and close. */ bool InterfaceSetupImp::saveChanges(){ bool error; QString iface = interfaces->getInterfaceName(error); odebug << "InterfaceSetupImp::saveChanges saves interface " << iface.latin1() << "" << oendl; if(!saveSettings()) return false; interfaces->write(); if (interface->getStatus()) { QString ifup; ifup += "ifdown "; ifup += iface; ifup += "; ifup "; ifup += iface; ifup += ";"; OProcess restart; restart << "sh"; restart << "-c"; restart << ifup; OWait *owait = new OWait(); Global::statusMessage( tr( "Restarting interface" ) ); owait->show(); qApp->processEvents(); if (!restart.start(OProcess::Block, OProcess::NoCommunication) ) { owarn << "unstable to spawn ifdown/ifup" << oendl; } owait->hide(); delete owait; interface->refresh(); } return true; } /** * Save the settings for the current Interface. * @return bool true if successful, false otherwise */ bool InterfaceSetupImp::saveSettings(){ // eh can't really do anything about it other then return. :-D if(!interfaces->isInterfaceSet()) return true; bool error = false; // Loopback case if(interfaces->getInterfaceMethod(error) == INTERFACES_LOOPBACK){ interfaces->setAuto(interface->getInterfaceName(), autoStart->isChecked()); return true; } if(!dhcpCheckBox->isChecked() && (ipAddressEdit->text().isEmpty() || subnetMaskEdit->text().isEmpty())){ QMessageBox::information(this, "Not Saved.", "Please fill in the IP address and\n subnet entries.", QMessageBox::Ok); return false; } // DHCP if(dhcpCheckBox->isChecked()) { interfaces->setInterfaceMethod(INTERFACES_METHOD_DHCP); interfaces->removeInterfaceOption("address"); interfaces->removeInterfaceOption("netmask"); interfaces->removeInterfaceOption("gateway"); interfaces->removeInterfaceOption("up "DNSSCRIPT" -a "); interfaces->removeInterfaceOption("down "DNSSCRIPT" -r "); } else{ interfaces->setInterfaceMethod("static"); interfaces->setInterfaceOption("address", ipAddressEdit->text()); interfaces->setInterfaceOption("netmask", subnetMaskEdit->text()); interfaces->setInterfaceOption("gateway", gatewayEdit->text()); if(!firstDNSLineEdit->text().isEmpty() || !secondDNSLineEdit->text().isEmpty()){ QString dns = firstDNSLineEdit->text() + " " + secondDNSLineEdit->text(); interfaces->setInterfaceOption("up "DNSSCRIPT" -a ", dns); interfaces->setInterfaceOption("down "DNSSCRIPT" -r ", dns); }else{ interfaces->removeInterfaceOption("up "DNSSCRIPT" -a "); interfaces->removeInterfaceOption("down "DNSSCRIPT" -r"); } } // IP Information interfaces->setAuto(interface->getInterfaceName(), autoStart->isChecked()); return true; } /** * The Profile has changed. * @param QString profile the new profile. */ void InterfaceSetupImp::setProfile(const QString &profile){ /* bool error = false; if(interfaces->getInterfaceMethod(error) == INTERFACES_LOOPBACK){ staticGroupBox->hide(); dhcpCheckBox->hide(); leaseTime->hide(); leaseHoursLabel->hide(); } */ QString newInterfaceName = interface->getInterfaceName(); if(profile.length() > 0) newInterfaceName += "_" + profile; // See if we have to make a interface. if(!interfaces->setInterface(newInterfaceName)){ // Add making for this new interface if need too if(profile != ""){ interfaces->copyInterface(interface->getInterfaceName(), newInterfaceName); if(!interfaces->setMapping(interface->getInterfaceName())){ interfaces->addMapping(interface->getInterfaceName()); if(!interfaces->setMapping(interface->getInterfaceName())){ odebug << "InterfaceSetupImp: Added Mapping, but still can't setInterface." << oendl; return; } } interfaces->setMap("map", newInterfaceName); interfaces->setScript("getprofile.sh"); } else{ interfaces->addInterface(newInterfaceName, INTERFACES_FAMILY_INET, INTERFACES_METHOD_DHCP); if(!interfaces->setInterface(newInterfaceName)){ odebug << "InterfaceSetupImp: Added interface, but still can't setInterface." << oendl; return; } } } // We must have a valid interface to get this far so read some settings. // DHCP bool error = false; if(interfaces->getInterfaceMethod(error) == INTERFACES_METHOD_DHCP) dhcpCheckBox->setChecked(true); else dhcpCheckBox->setChecked(false); // IP Information autoStart->setChecked(interfaces->isAuto(interface->getInterfaceName())); QString dns = interfaces->getInterfaceOption("up "DNSSCRIPT" -a", error); odebug << "dns >" << dns.latin1() << "<" << oendl; if(dns.contains(" ")){ firstDNSLineEdit->setText(dns.mid(0, dns.find(" "))); secondDNSLineEdit->setText(dns.mid(dns.find(" ")+1, dns.length())); }else firstDNSLineEdit->setText(dns); ipAddressEdit->setText(interfaces->getInterfaceOption("address", error)); subnetMaskEdit->setText(interfaces->getInterfaceOption("netmask", error)); if (subnetMaskEdit->text().isEmpty()) subnetMaskEdit->setText( "255.255.255.0" ); gatewayEdit->setText(interfaces->getInterfaceOption("gateway", error)); owarn << "InterfaceSetupImp::setProfile(" << profile.latin1() << ")\n" << oendl; owarn << "InterfaceSetupImp::setProfile: iface is " << interfaces->getInterfaceName(error).latin1() << "\n" << oendl; } // interfacesetup.cpp diff --git a/noncore/settings/networksettings/mainwindowimp.cpp b/noncore/settings/networksettings/mainwindowimp.cpp index 3e1a650..5184630 100644 --- a/noncore/settings/networksettings/mainwindowimp.cpp +++ b/noncore/settings/networksettings/mainwindowimp.cpp @@ -1,748 +1,749 @@ #include "mainwindowimp.h" #include "addconnectionimp.h" #include "interfaceinformationimp.h" #include "interfacesetupimp.h" #include "interfaces.h" #include "module.h" /* OPIE */ +#include <opie2/odebug.h> #include <qpe/qcopenvelope_qws.h> #include <qpe/qpeapplication.h> #include <qpe/config.h> #include <qpe/qlibrary.h> #include <qpe/resource.h> /* QT */ #include <qpushbutton.h> #include <qlistbox.h> #include <qlineedit.h> #include <qlistview.h> #include <qheader.h> #include <qlabel.h> #include <qtabwidget.h> // in order to disable the profiles tab #include <qmessagebox.h> #if QT_VERSION < 300 #include <qlist.h> #else #include <qptrlist.h> #endif #include <qdir.h> #include <qfile.h> #include <qtextstream.h> #include <qregexp.h> /* STD */ #include <net/if.h> #include <sys/ioctl.h> #include <sys/socket.h> #define DEFAULT_SCHEME "/var/lib/pcmcia/scheme" #define _PROCNETDEV "/proc/net/dev" MainWindowImp::MainWindowImp(QWidget *parent, const char *name, WFlags) : MainWindow(parent, name, Qt::WStyle_ContextHelp), advancedUserMode(true), scheme(DEFAULT_SCHEME) { connect(addConnectionButton, SIGNAL(clicked()), this, SLOT(addClicked())); connect(removeConnectionButton, SIGNAL(clicked()), this, SLOT(removeClicked())); connect(informationConnectionButton, SIGNAL(clicked()), this, SLOT(informationClicked())); connect(configureConnectionButton, SIGNAL(clicked()), this, SLOT(configureClicked())); connect(newProfileButton, SIGNAL(clicked()), this, SLOT(addProfile())); connect(removeProfileButton, SIGNAL(clicked()), this, SLOT(removeProfile())); connect(setCurrentProfileButton, SIGNAL(clicked()), this, SLOT(changeProfile())); connect(newProfile, SIGNAL(textChanged(const QString&)), this, SLOT(newProfileChanged(const QString&))); //FIXME: disable profiles for the moment: tabWidget->setTabEnabled( tab, false ); // Load connections. // /usr/local/kde/lib/libinterfaces.la #ifdef QWS loadModules(QPEApplication::qpeDir() + "plugins/networksettings"); #else loader = KLibLoader::self(); loadModules(QString("/usr/")+KStandardDirs::kde_default("lib")); #endif getAllInterfaces(); Interfaces i; QStringList list = i.getInterfaceList(); QMap<QString, Interface*>::Iterator it; for ( QStringList::Iterator ni = list.begin(); ni != list.end(); ++ni ) { /* * we skipped it in getAllInterfaces now * we need to ignore it as well */ if (m_handledIfaces.contains( *ni) ) { odebug << "Not up iface handled by module" << oendl; continue; } bool found = false; for( it = interfaceNames.begin(); it != interfaceNames.end(); ++it ) { if(it.key() == (*ni)) found = true; } if(!found) { if(!(*ni).contains("_")) { Interface *i = new Interface(this, *ni, false); i->setAttached(false); i->setHardwareName(tr("Disconnected")); interfaceNames.insert(i->getInterfaceName(), i); updateInterface(i); connect(i, SIGNAL(updateInterface(Interface*)), this, SLOT(updateInterface(Interface*))); } } } //getInterfaceList(); connectionList->header()->hide(); Config cfg("NetworkSetup"); profiles = QStringList::split(" ", cfg.readEntry("Profiles", "All")); for ( QStringList::Iterator it = profiles.begin(); it != profiles.end(); ++it) profilesList->insertItem((*it)); currentProfileLabel->setText(cfg.readEntry("CurrentProfile", "All")); advancedUserMode = cfg.readBoolEntry("AdvancedUserMode", false); scheme = cfg.readEntry("SchemeFile", DEFAULT_SCHEME); QFile file(scheme); if ( file.open(IO_ReadOnly) ) { // file opened successfully QTextStream stream( &file ); // use a text stream while ( !stream.eof() ) { // until end of file... QString line = stream.readLine(); // line of text excluding '\n' if(line.contains("SCHEME")) { line = line.mid(7, line.length()); currentProfileLabel->setText(line); break; } } file.close(); } makeChannel(); } /** * Deconstructor. Save profiles. Delete loaded libraries. */ MainWindowImp::~MainWindowImp() { // Save profiles. Config cfg("NetworkSetup"); cfg.setGroup("General"); cfg.writeEntry("Profiles", profiles.join(" ")); // Delete all interfaces that don't have owners. QMap<Interface*, QListViewItem*>::Iterator iIt; for( iIt = items.begin(); iIt != items.end(); ++iIt ) { if(iIt.key()->getModuleOwner() == NULL) delete iIt.key(); } #ifdef QWS // Delete Modules and Libraries QMap<Module*, QLibrary*>::Iterator it; for( it = libraries.begin(); it != libraries.end(); ++it ) { delete it.key(); // I wonder why I can't delete the libraries // What fucking shit this is. //delete it.data(); } #else // klibloader automaticly deletes the libraries for us... #endif } /** * Query the kernel for all of the interfaces. */ void MainWindowImp::getAllInterfaces() { int sockfd = socket(PF_INET, SOCK_DGRAM, 0); if(sockfd == -1) return; struct ifreq ifr; QStringList ifaces; QFile procFile(QString(_PROCNETDEV)); int result; Interface *i; if (! procFile.exists()) { struct ifreq ifrs[100]; struct ifconf ifc; ifc.ifc_len = sizeof(ifrs); ifc.ifc_req = ifrs; result = ioctl(sockfd, SIOCGIFCONF, &ifc); for (unsigned int i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++) { struct ifreq *pifr = &ifrs[i]; ifaces += pifr->ifr_name; } } else { procFile.open(IO_ReadOnly); QString line; QTextStream procTs(&procFile); int loc = -1; procTs.readLine(); // eat a line procTs.readLine(); // eat a line while((line = procTs.readLine().simplifyWhiteSpace()) != QString::null) { if((loc = line.find(":")) != -1) { ifaces += line.left(loc); } } } for (QStringList::Iterator it = ifaces.begin(); it != ifaces.end(); ++it) { int flags = 0; if ( m_handledIfaces.contains( (*it) ) ) { odebug << " " << (*it).latin1() << " is handled by a module" << oendl; continue; } // int family; i = NULL; strcpy(ifr.ifr_name, (*it).latin1()); struct ifreq ifcopy; ifcopy = ifr; result = ioctl(sockfd, SIOCGIFFLAGS, &ifcopy); flags = ifcopy.ifr_flags; i = new Interface(this, ifr.ifr_name, false); i->setAttached(true); if ((flags & IFF_UP) == IFF_UP) i->setStatus(true); else i->setStatus(false); if ((flags & IFF_BROADCAST) == IFF_BROADCAST) i->setHardwareName("Ethernet"); else if ((flags & IFF_POINTOPOINT) == IFF_POINTOPOINT) i->setHardwareName("Point to Point"); else if ((flags & IFF_MULTICAST) == IFF_MULTICAST) i->setHardwareName("Multicast"); else if ((flags & IFF_LOOPBACK) == IFF_LOOPBACK) i->setHardwareName("Loopback"); else i->setHardwareName("Unknown"); owarn << "Adding interface " << ifr.ifr_name << " to interfaceNames\n" << oendl; interfaceNames.insert(i->getInterfaceName(), i); updateInterface(i); connect(i, SIGNAL(updateInterface(Interface*)), this, SLOT(updateInterface(Interface*))); } // now lets ask the plugins too ;) QMap<Module*, QLibrary*>::Iterator it; QList<Interface> ilist; for( it = libraries.begin(); it != libraries.end(); ++it ) { if(it.key()) { ilist = it.key()->getInterfaces(); for( i = ilist.first(); i != 0; i = ilist.next() ) { owarn << "Adding interface " << i->getInterfaceName().latin1() << " to interfaceNames\n" << oendl; interfaceNames.insert(i->getInterfaceName(), i); updateInterface(i); connect(i, SIGNAL(updateInterface(Interface*)), this, SLOT(updateInterface(Interface*))); } } } } /** * Load all modules that are found in the path * @param path a directory that is scaned for any plugins that can be loaded * and attempts to load them */ void MainWindowImp::loadModules(const QString &path) { #ifdef DEBUG odebug << "MainWindowImp::loadModules: " << path.latin1() << "" << oendl; #endif QDir d(path); if(!d.exists()) return; // Don't want sym links d.setFilter( QDir::Files | QDir::NoSymLinks ); const QFileInfoList *list = d.entryInfoList(); QFileInfoListIterator it( *list ); QFileInfo *fi; while ( (fi=it.current()) ) { #ifdef QWS if(fi->fileName().contains(".so")) { #else if(fi->fileName().contains(".so") && fi->fileName().contains("networksettings_")) { #endif loadPlugin(path + "/" + fi->fileName()); odebug << "loaded plugin: >" << QString(path + "/" + fi->fileName()).latin1() << "< " << oendl; } ++it; } } /** * Attempt to load a function and resolve a function. * @param pluginFileName - the name of the file in which to attempt to load * @param resolveString - function pointer to resolve * @return pointer to the function with name resolveString or NULL */ Module* MainWindowImp::loadPlugin(const QString &pluginFileName, const QString &resolveString) { #ifdef DEBUG odebug << "MainWindowImp::loadPlugin: " << pluginFileName.latin1() << ": resolving " << resolveString.latin1() << "" << oendl; #endif #ifdef QWS QLibrary *lib = new QLibrary(pluginFileName); void *functionPointer = lib->resolve(resolveString); if( !functionPointer ) { #ifdef DEBUG odebug << "MainWindowImp::loadPlugin: Warning: " << pluginFileName.latin1() << " is not a plugin" << oendl; #endif delete lib; return NULL; } // Try to get an object. Module *object = ((Module* (*)()) functionPointer)(); if(object == NULL) { #ifdef DEBUG odebug << "MainWindowImp: Couldn't create object, but did load library!" << oendl; #endif delete lib; return NULL; } m_handledIfaces += object->handledInterfaceNames(); // Store for deletion later libraries.insert(object, lib); return object; #else QLibrary *lib = loader->library(pluginFileName); if( !lib || !lib->hasSymbol(resolveString) ) { odebug << QString("MainWindowImp::loadPlugin: File: %1 is not a plugin, but though was.").arg(pluginFileName).latin1() << oendl; return NULL; } // Try to get an object. Module *object = ((Module* (*)()) lib->symbol(resolveString))(); if(object == NULL) { #ifdef DEBUG odebug << "MainWindowImp: Couldn't create object, but did load library!" << oendl; #endif return NULL; } #ifdef DEBUG odebug << "MainWindowImp::loadPlugin:: Found object, storing." << oendl; #endif // Store for deletion later libraries.insert(object, lib); return object; #endif } /** * The Add button was clicked. Bring up the add dialog and if OK is hit * load the plugin and append it to the list */ void MainWindowImp::addClicked() { QMap<Module*, QLibrary*>::Iterator it; QMap<QString, QString> list; QMap<QString, Module*> newInterfaceOwners; for( it = libraries.begin(); it != libraries.end(); ++it ) { if(it.key()) { (it.key())->possibleNewInterfaces(list); } } // See if the list has anything that we can add. if(list.count() == 0) { QMessageBox::information(this, "Sorry", "Nothing to add.", QMessageBox::Ok); return; } AddConnectionImp addNewConnection(this, "AddConnectionImp", true); addNewConnection.addConnections(list); if( QDialog::Accepted == QPEApplication::execDialog( &addNewConnection ) ) { QListViewItem *item = addNewConnection.registeredServicesList->currentItem(); if(!item) return; for( it = libraries.begin(); it != libraries.end(); ++it ) { if(it.key()) { Interface *i = (it.key())->addNewInterface(item->text(0)); if(i) { odebug << "iface name " << i->getInterfaceName().latin1() << "" << oendl; interfaceNames.insert(i->getInterfaceName(), i); updateInterface(i); } } } } } /** * Prompt the user to see if they really want to do this. * If they do then remove from the list and unload. */ void MainWindowImp::removeClicked() { QListViewItem *item = connectionList->currentItem(); if(!item) { QMessageBox::information(this, "Sorry","Please select an interface First.", QMessageBox::Ok); return; } Interface *i = interfaceItems[item]; if(i->getModuleOwner() == NULL) { QMessageBox::information(this, "Can't remove interface.", "Interface is built in.", QMessageBox::Ok); } else { if(!i->getModuleOwner()->remove(i)) QMessageBox::information(this, tr("Error"), tr("Unable to remove."), QMessageBox::Ok); else { delete item; // QMessageBox::information(this, "Success", "Interface was removed.", QMessageBox::Ok); } } } /** * Pull up the configure about the currently selected interface. * Report an error if no interface is selected. * If the interface has a module owner then request its configure. */ void MainWindowImp::configureClicked() { QListViewItem *item = connectionList->currentItem(); if(!item) { QMessageBox::information(this, tr("Sorry"),tr("Please select an interface first."), QMessageBox::Ok); return; } QString currentProfileText = currentProfileLabel->text(); if(currentProfileText.upper() == "ALL"); currentProfileText = ""; Interface *i = interfaceItems[item]; if(i->getModuleOwner()) { QWidget *moduleConfigure = i->getModuleOwner()->configure(i); if(moduleConfigure != NULL) { i->getModuleOwner()->setProfile(currentProfileText); QPEApplication::showWidget( moduleConfigure ); return; } } InterfaceSetupImpDialog *configure = new InterfaceSetupImpDialog(this, "InterfaceSetupImp", i, true, Qt::WDestructiveClose | Qt::WStyle_ContextHelp ); configure->setProfile(currentProfileText); QPEApplication::showDialog( configure ); } /** * Pull up the information about the currently selected interface. * Report an error if no interface is selected. * If the interface has a module owner then request its configure. */ void MainWindowImp::informationClicked() { QListViewItem *item = connectionList->currentItem(); if(!item) { QMessageBox::information(this, "Sorry","Please select an interface First.", QMessageBox::Ok); return; } Interface *i = interfaceItems[item]; // if(!i->isAttached()){ // QMessageBox::information(this, "Sorry","No information about\na disconnected interface.", QMessageBox::Ok); // return; // } if(i->getModuleOwner()) { QWidget *moduleInformation = i->getModuleOwner()->information(i); if(moduleInformation != NULL) { QPEApplication::showWidget( moduleInformation ); #ifdef DEBUG odebug << "MainWindowImp::informationClicked:: Module owner has created, we showed." << oendl; #endif return; } } InterfaceInformationImp *information = new InterfaceInformationImp(this, "InterfaceSetupImp", i, Qt::WType_Modal | Qt::WDestructiveClose | Qt::WStyle_Dialog | Qt::WStyle_ContextHelp); QPEApplication::showWidget( information ); } /** * Update this interface. If no QListViewItem exists create one. * @param Interface* pointer to the interface that needs to be updated. */ void MainWindowImp::updateInterface(Interface *i) { if(!advancedUserMode) { if(i->getInterfaceName() == "lo") return; } QListViewItem *item = NULL; // Find the interface, making it if needed. if(items.find(i) == items.end()) { item = new QListViewItem(connectionList, "", "", ""); // See if you can't find a module owner for this interface QMap<Module*, QLibrary*>::Iterator it; for( it = libraries.begin(); it != libraries.end(); ++it ) { if(it.key()->isOwner(i)) i->setModuleOwner(it.key()); } items.insert(i, item); interfaceItems.insert(item, i); } else item = items[i]; // Update the icons and information #ifdef QWS item->setPixmap(0, (Resource::loadPixmap(i->getStatus() ? "up": "down"))); #else item->setPixmap(0, (SmallIcon(i->getStatus() ? "up": "down"))); #endif QString typeName = "lan"; if(i->getInterfaceName() == "lo") typeName = "lo"; if(i->getInterfaceName().contains("irda")) typeName = "irda"; if(i->getInterfaceName().contains("wlan")) typeName = "wlan"; if(i->getInterfaceName().contains("usb")) typeName = "usb"; if(!i->isAttached()) typeName = "connect_no"; // Actually try to use the Module if(i->getModuleOwner() != NULL) typeName = i->getModuleOwner()->getPixmapName(i); #ifdef QWS item->setPixmap(1, (Resource::loadPixmap(QString("networksettings/") + typeName))); #else item->setPixmap(1, (SmallIcon(typeName))); #endif item->setText(2, i->getHardwareName()); item->setText(3, QString("(%1)").arg(i->getInterfaceName())); item->setText(4, (i->getStatus()) ? i->getIp() : QString("")); } void MainWindowImp::newProfileChanged(const QString& newText) { if(newText.length() > 0) newProfileButton->setEnabled(true); else newProfileButton->setEnabled(false); } /** * Adds a new profile to the list of profiles. * Don't add profiles that already exists. * Appends to the list and QStringList */ void MainWindowImp::addProfile() { QString newProfileName = newProfile->text(); if(profiles.grep(newProfileName).count() > 0) { QMessageBox::information(this, "Can't Add","Profile already exists.", QMessageBox::Ok); return; } profiles.append(newProfileName); profilesList->insertItem(newProfileName); } /** * Removes the currently selected profile in the combo. * Doesn't delete if there are less then 2 profiles. */ void MainWindowImp::removeProfile() { if(profilesList->count() <= 1) { QMessageBox::information(this, "Can't remove.","At least one profile\nis needed.", QMessageBox::Ok); return; } QString profileToRemove = profilesList->currentText(); if(profileToRemove == "All") { QMessageBox::information(this, "Can't remove.","Can't remove default.", QMessageBox::Ok); return; } // Can't remove the curent profile if(profileToRemove == currentProfileLabel->text()) { QMessageBox::information(this, "Can't remove.",QString("%1 is the current profile.").arg(profileToRemove), QMessageBox::Ok); return; } if(QMessageBox::information(this, "Question",QString("Remove profile: %1").arg(profileToRemove), QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok) { profiles = QStringList::split(" ", profiles.join(" ").replace(QRegExp(profileToRemove), "")); profilesList->clear(); for ( QStringList::Iterator it = profiles.begin(); it != profiles.end(); ++it) profilesList->insertItem((*it)); // Remove any interface settings and mappings. Interfaces interfaces; // Go through them one by one QMap<Interface*, QListViewItem*>::Iterator it; for( it = items.begin(); it != items.end(); ++it ) { QString interfaceName = it.key()->getInterfaceName(); odebug << interfaceName.latin1() << oendl; if(interfaces.setInterface(interfaceName + "_" + profileToRemove)) { interfaces.removeInterface(); if(interfaces.setMapping(interfaceName)) { if(profilesList->count() == 1) interfaces.removeMapping(); else { interfaces.removeMap("map", interfaceName + "_" + profileToRemove); } } interfaces.write(); break; } } } } /** * A new profile has been selected, change. * @param newProfile the new profile. */ void MainWindowImp::changeProfile() { if(profilesList->currentItem() == -1) { QMessageBox::information(this, "Can't Change.","Please select a profile.", QMessageBox::Ok); return; } QString newProfile = profilesList->text(profilesList->currentItem()); if(newProfile != currentProfileLabel->text()) { currentProfileLabel->setText(newProfile); QFile::remove(scheme); QFile file(scheme); if ( file.open(IO_ReadWrite) ) { QTextStream stream( &file ); stream << QString("SCHEME=%1").arg(newProfile); file.close(); } // restart all up devices? if(QMessageBox::information(this, "Question","Restart all running interfaces?", QMessageBox::Ok, QMessageBox::No) == QMessageBox::Ok) { // Go through them one by one QMap<Interface*, QListViewItem*>::Iterator it; for( it = items.begin(); it != items.end(); ++it ) { if(it.key()->getStatus() == true) it.key()->restart(); } } } // TODO change the profile in the modules } void MainWindowImp::makeChannel() { channel = new QCopChannel( "QPE/Application/networksettings", this ); connect( channel, SIGNAL(received(const QCString&,const QByteArray&)), this, SLOT(receive(const QCString&,const QByteArray&)) ); } void MainWindowImp::receive(const QCString &msg, const QByteArray &arg) { bool found = false; odebug << "MainWindowImp::receive QCop msg >"+msg+"<" << oendl; if (msg == "raise") { raise(); return; } QString dest = msg.left(msg.find("(")); QCString param = msg.right(msg.length() - msg.find("(") - 1); param = param.left( param.length() - 1 ); odebug << "dest >" << dest.latin1() << "< param >"+param+"<" << oendl; QMap<Module*, QLibrary*>::Iterator it; for( it = libraries.begin(); it != libraries.end(); ++it ) { odebug << "plugin >" << it.key()->type().latin1() << "<" << oendl; if(it.key()->type() == dest) { it.key()->receive( param, arg ); found = true; } } if (found) QPEApplication::setKeepRunning(); else odebug << "Huh what do ya want" << oendl; } |