author | benmeyer <benmeyer> | 2002-10-17 15:03:35 (UTC) |
---|---|---|
committer | benmeyer <benmeyer> | 2002-10-17 15:03:35 (UTC) |
commit | 1fa7ebd9512ac0497c3ede198621899a10e961af (patch) (side-by-side diff) | |
tree | 45be4e45b5408f46c3f2e59b1241aebf9a9bf869 | |
parent | 6c8ae3c8af454c87f5f467fe17cbdffe4c8f5494 (diff) | |
download | opie-1fa7ebd9512ac0497c3ede198621899a10e961af.zip opie-1fa7ebd9512ac0497c3ede198621899a10e961af.tar.gz opie-1fa7ebd9512ac0497c3ede198621899a10e961af.tar.bz2 |
added interface listing
-rw-r--r-- | noncore/net/networksetup/TODO | 1 | ||||
-rw-r--r-- | noncore/net/networksetup/interfaces.cpp | 26 | ||||
-rw-r--r-- | noncore/net/networksetup/interfaces.h | 1 | ||||
-rw-r--r-- | noncore/net/networksetup/mainwindowimp.cpp | 22 | ||||
-rw-r--r-- | noncore/net/networksetup/networksetup.pro | 6 | ||||
-rw-r--r-- | noncore/settings/networksettings/TODO | 1 | ||||
-rw-r--r-- | noncore/settings/networksettings/interfaces.cpp | 26 | ||||
-rw-r--r-- | noncore/settings/networksettings/interfaces.h | 1 | ||||
-rw-r--r-- | noncore/settings/networksettings/mainwindowimp.cpp | 22 | ||||
-rw-r--r-- | noncore/settings/networksettings/networksetup.pro | 6 |
10 files changed, 104 insertions, 8 deletions
diff --git a/noncore/net/networksetup/TODO b/noncore/net/networksetup/TODO index 70d6717..7386646 100644 --- a/noncore/net/networksetup/TODO +++ b/noncore/net/networksetup/TODO @@ -1,7 +1,8 @@ [ ] Wlanmodule needs to check if an interface supports wireless extensions. [x] When you set options in wlanmodule, hit OK, it exits all of networksetup, doesnt bring you back to the main screen. [x] Wlanmodule isnt writing out wireless.opts [ ] Need a means of bringing an interface up and down (calling out ifup/ifdown) from the gui. + -Ben- Click information, then click up or down... :-D diff --git a/noncore/net/networksetup/interfaces.cpp b/noncore/net/networksetup/interfaces.cpp index b8a3e7f..1287d90 100644 --- a/noncore/net/networksetup/interfaces.cpp +++ b/noncore/net/networksetup/interfaces.cpp @@ -1,430 +1,454 @@ #include "interfaces.h" #include <qfile.h> #include <qtextstream.h> #include <qregexp.h> #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)){ qDebug(QString("Interfaces: Can't open file: %1 for reading.").arg(interfacesFile).latin1()); 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. Usefull 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 = 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 + * @param interface interface to check to see if it is on or not. * @return true is interface is in auto */ bool Interfaces::isAuto(QString interface){ QStringList autoLines = interfaces.grep(QRegExp(AUTO)); QStringList awi = autoLines.grep(QRegExp(interface)); if(awi.count() > 1) qDebug(QString("Interfaces: Found more then auto group with interface: %1.").arg(interface).latin1()); if(awi.count() < 1) return false; return true; } /** * 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(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{ if((*it).contains(interface)){ (*it) = (*it).replace(QRegExp(interface), ""); // clean up QString line = (*it).simplifyWhiteSpace(); line = line.replace(QRegExp(" "),""); if(line == AUTO) (*it) = ""; changed = true; // Don't break because we want to make sure we remove all cases. } } } } if(changed == false){ if(setAuto == true) interfaces.append(QString(AUTO" %1").arg(interface)); else{ qDebug(QString("Interfaces: Can't set interface %1 auto to false sense it is already false.").arg(interface).latin1()); } } 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 successfull. */ 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(){ return (currentIface != interfaces.end()); } /** * 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 successfull. */ bool Interfaces::addInterface(QString interface, QString family, QString method){ if(acceptedFamily.contains(family)==0) return false; interface = interface.simplifyWhiteSpace(); interface = interface.replace(QRegExp(" "), ""); interfaces.append(""); interfaces.append(QString(IFACE " %1 %2 %3").arg(interface).arg(family).arg(method)); return true; } /** * Remove the currently selected interface and all of its options. * @return bool if successfull or not. */ bool Interfaces::removeInterface(){ if(currentIface == interfaces.end()) return false; (*currentIface) = ""; return removeAllInterfaceOptions(); } /** * 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){ error = true; 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){ error = true; return QString(); } QString family = getInterfaceFamily(error); if(error){ error = true; 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 successfull. */ bool Interfaces::setInterfaceName(QString newName){ if(currentIface == interfaces.end()) return false; newName = newName.simplifyWhiteSpace(); newName = newName.replace(QRegExp(" "), ""); bool returnValue = false; (*currentIface) = QString("iface %1 %2 %3").arg(newName).arg(getInterfaceFamily(returnValue)).arg(getInterfaceMethod(returnValue)); 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 successfull. */ bool Interfaces::setInterfaceFamily(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 successfull. */ bool Interfaces::setInterfaceMethod(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(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 is set to an * empty string then option is removed. * @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(QString option, QString value){ return setOption(currentIface, option, value); } /** * Removes all of the options from the currently selected interface. * @return bool error if if successfull */ 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 successfull. */ bool Interfaces::setMapping(QString interface){ interface = interface.simplifyWhiteSpace(); interface = interface.replace(QRegExp(" "), ""); return setStanza(MAPPING, interface, 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(QString interfaces){ interfaces.append(""); interfaces.append(QString(MAPPING " %1").arg(interfaces)); } /** * 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 successfull. */ bool Interfaces::setMap(QString map, QString value){ return setOption(currentMapping, map, value); } /** * Get a map value within a mapping. * @param map map to get value of * @param bool true if it is successfull. * @return value that goes to the map */ QString Interfaces::getMap(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 successfull. */ bool Interfaces::setScript(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 successfull. * @return bool true if the stanza is found. */ bool Interfaces::setStanza(QString stanza, 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)){ if(found == true){ qDebug(QString("Interfaces: Found multiple stanza's for search: %1 %2").arg(stanza).arg(option).latin1()); } 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 successfull, false otherwise. */ bool Interfaces::setOption(QStringList::Iterator start, QString option, 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){ if(!found && value != ""){ // Got to the end of the stanza without finding it, so append it. interfaces.insert(--it, QString("\t%1 %2").arg(option).arg(value)); } break; } if((*it).contains(option)){ // Found it in stanza so replace it. if(found) qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); found = true; if(value == "") (*it) = ""; else (*it) = QString("\t%1 %2").arg(option).arg(value); } } return true; } /** * Removes all options in a stanza * @param start the start of the stanza * @return bool true if successfull, false otherwise. */ bool Interfaces::removeAllOptions(QStringList::Iterator start){ if(start == interfaces.end()) return false; diff --git a/noncore/net/networksetup/interfaces.h b/noncore/net/networksetup/interfaces.h index 2cc9689..8b4788c 100644 --- a/noncore/net/networksetup/interfaces.h +++ b/noncore/net/networksetup/interfaces.h @@ -1,70 +1,71 @@ #ifndef INTERFACES_H #define INTERFACES_H #include <qstring.h> #include <qstringlist.h> #define INTERFACES_LOOPBACK "loopback" #define INTERFACES_FAMILY_INET "inet" #define INTERFACES_FAMILY_IPX "ipx" #define INTERFACES_FAMILY_INET6 "inet6" #define INTERFACES_METHOD_DHCP "dhcp" #define INTERFACES_METHOD_STATIC "static" #define INTERFACES_METHOD_PPP "ppp" /** * This class provides a clean frontend for parsing the network interfaces file. * It provides helper functions to minipulate the options within the file. * See the interfaces man page for the syntax rules. */ class Interfaces { public: Interfaces(QString useInterfacesFile = "/etc/network/interfaces"); + QStringList getInterfaceList(); bool isAuto(QString interface); bool setAuto(QString interface, bool setAuto); bool removeInterface(); bool addInterface(QString interface, QString family, QString method); bool setInterface(QString interface); bool isInterfaceSet(); QString getInterfaceName(bool &error); bool setInterfaceName(QString newName); QString getInterfaceFamily(bool &error); bool setInterfaceFamily(QString newName); QString getInterfaceMethod(bool &error); bool setInterfaceMethod(QString newName); QString getInterfaceOption(QString option, bool &error); bool setInterfaceOption(QString option, QString value); bool removeAllInterfaceOptions(); bool setMapping(QString interface); void addMapping(QString interfaces); bool setMap(QString map, QString value); QString getMap(QString map, bool &error); bool setScript(QString); QString getScript(bool &error); bool write(); private: bool setStanza(QString stanza, QString option,QStringList::Iterator &iterator); bool setOption(QStringList::Iterator start, QString option, QString value); QString getOption(QStringList::Iterator start, QString option, bool &error); bool removeAllOptions(QStringList::Iterator start); QString interfacesFile; QStringList interfaces; QStringList::Iterator currentIface; QStringList::Iterator currentMapping; QStringList acceptedFamily; }; #endif // interfaces diff --git a/noncore/net/networksetup/mainwindowimp.cpp b/noncore/net/networksetup/mainwindowimp.cpp index 36f12e0..24af1ec 100644 --- a/noncore/net/networksetup/mainwindowimp.cpp +++ b/noncore/net/networksetup/mainwindowimp.cpp @@ -1,428 +1,450 @@ #include "mainwindowimp.h"
#include "addconnectionimp.h"
#include "interfaceinformationimp.h"
#include "interfacesetupimp.h"
+#include "interfaces.h"
+
#include "module.h"
#include "kprocess.h"
#include <qpushbutton.h>
#include <qtabwidget.h>
#include <qlistbox.h>
#include <qlineedit.h>
#include <qlistview.h>
#include <qheader.h>
#include <qlabel.h>
#include <qmainwindow.h>
#include <qmessagebox.h>
#include <qpe/config.h>
#include <qpe/qlibrary.h>
#include <qpe/resource.h>
#include <qpe/qpeapplication.h>
#include <qlist.h>
#include <qdir.h>
#include <qfile.h>
#include <qtextstream.h>
#define TEMP_ALL "/tmp/ifconfig-a"
#define TEMP_UP "/tmp/ifconfig"
MainWindowImp::MainWindowImp(QWidget *parent, const char *name) : MainWindow(parent, name, true), advancedUserMode(false){
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&)));
// Load connections.
loadModules(QPEApplication::qpeDir() + "/plugins/networksetup");
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));
advancedUserMode = cfg.readBoolEntry("AdvancedUserMode", false);
}
/**
* Deconstructor. Save profiles. Delete loaded libraries.
*/
MainWindowImp::~MainWindowImp(){
// Save profiles.
if(profiles.count() > 1){
Config cfg("NetworkSetup");
cfg.setGroup("General");
cfg.writeEntry("Profiles", profiles.join(" "));
}
// Delete Modules and Libraries
QMap<Module*, QLibrary*>::Iterator it;
for( it = libraries.begin(); it != libraries.end(); ++it ){
delete it.key();
delete it.data();
}
}
/**
* 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(QString path){
qDebug(path.latin1());
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()) ) {
if(fi->fileName().contains(".so")){
loadPlugin(path + "/" + fi->fileName());
}
++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(QString pluginFileName, QString resolveString){
qDebug(QString("MainWindowImp::loadPlugin: %1").arg(pluginFileName).latin1());
QLibrary *lib = new QLibrary(pluginFileName);
void *functionPointer = lib->resolve(resolveString);
if( !functionPointer ){
qDebug(QString("MainWindowImp: File: %1 is not a plugin, but though was.").arg(pluginFileName).latin1());
delete lib;
return NULL;
}
// Try to get an object.
Module *object = ((Module* (*)()) functionPointer)();
if(object == NULL){
qDebug("MainWindowImp: Couldn't create object, but did load library!");
delete lib;
return NULL;
}
// Store for deletion later
libraries.insert(object, lib);
return object;
}
/**
* 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;
list.insert("USB (PPP) / (ADD_TEST)", "A dialup connection over the USB port");
list.insert("IrDa (PPP) / (ADD_TEST)", "A dialup connection over the IdDa port");
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.", "Ok");
return;
}
AddConnectionImp addNewConnection(this, "AddConnectionImp", true);
addNewConnection.addConnections(list);
addNewConnection.showMaximized();
if(QDialog::Accepted == addNewConnection.exec()){
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){
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, "Error","Please select an interface.", "Ok");
return;
}
Interface *i = interfaceItems[item];
if(i->getModuleOwner() == NULL){
QMessageBox::information(this, "Can't remove interface.", "Interface is built in.", "Ok");
}
else{
if(!i->getModuleOwner()->remove(i))
QMessageBox::information(this, "Error", "Unable to remove.", "Ok");
else{
QMessageBox::information(this, "Success", "Interface was removed.", "Ok");
// TODO memory managment....
// who deletes the interface?
}
}
}
/**
* 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 with a empty
* tab. If tab is !NULL then append the interfaces setup widget to it.
*/
void MainWindowImp::configureClicked(){
QListViewItem *item = connectionList->currentItem();
if(!item){
QMessageBox::information(this, "Error","Please select an interface.", QMessageBox::Ok);
return;
}
Interface *i = interfaceItems[item];
if(i->getModuleOwner()){
QTabWidget *tabWidget = NULL;
QWidget *moduleConfigure = i->getModuleOwner()->configure(&tabWidget);
if(moduleConfigure != NULL){
if(tabWidget != NULL){
InterfaceSetupImp *configure = new InterfaceSetupImp(tabWidget, "InterfaceSetupImp", i, true);
tabWidget->insertTab(configure, "TCP/IP");
}
moduleConfigure->showMaximized();
moduleConfigure->show();
return;
}
}
InterfaceSetupImp *configure = new InterfaceSetupImp(0, "InterfaceSetupImp", i, true);
configure->showMaximized();
configure->show();
}
/**
* 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 with a empty
* tab. If tab is !NULL then append the interfaces setup widget to it.
*/
void MainWindowImp::informationClicked(){
QListViewItem *item = connectionList->currentItem();
if(!item){
QMessageBox::information(this, "Error","Please select an interface.", QMessageBox::Ok);
return;
}
Interface *i = interfaceItems[item];
if(i->getModuleOwner()){
QTabWidget *tabWidget = NULL;
QWidget *moduleInformation = i->getModuleOwner()->information(&tabWidget);
if(moduleInformation != NULL){
if(tabWidget != NULL){
InterfaceInformationImp *information = new InterfaceInformationImp(tabWidget, "InterfaceSetupImp", i, true);
tabWidget->insertTab(information, "TCP/IP");
}
moduleInformation->showMaximized();
moduleInformation->show();
return;
}
}
InterfaceInformationImp *information = new InterfaceInformationImp(0, "InterfaceSetupImp", i, true);
information->showMaximized();
information->show();
}
/**
* Aquire the list of active interfaces from ifconfig
* Call ifconfig and ifconfig -a
*/
void MainWindowImp::getInterfaceList(){
KShellProcess *processAll = new KShellProcess();
*processAll << "/sbin/ifconfig" << "-a" << " > " TEMP_ALL;
connect(processAll, SIGNAL(processExited(KProcess *)),
this, SLOT(jobDone(KProcess *)));
threads.insert(processAll, TEMP_ALL);
processAll->start(KShellProcess::NotifyOnExit);
KShellProcess *process = new KShellProcess();
*process << "/sbin/ifconfig" << " > " TEMP_UP;
connect(process, SIGNAL(processExited(KProcess *)),
this, SLOT(jobDone(KProcess *)));
threads.insert(process, TEMP_UP);
process->start(KShellProcess::NotifyOnExit);
}
void MainWindowImp::jobDone(KProcess *process){
QString fileName = threads[process];
threads.remove(process);
delete process;
QFile file(fileName);
if (!file.open(IO_ReadOnly)){
qDebug(QString("MainWindowImp: Can't open file: %1").arg(fileName).latin1());
return;
}
QTextStream stream( &file );
QString line;
while ( !stream.eof() ) {
line = stream.readLine();
int space = line.find(" ");
if(space > 1){
// We have found an interface
QString interfaceName = line.mid(0, space);
if(!advancedUserMode){
if(interfaceName == "lo")
break;
}
Interface *i;
// See if we already have it
if(interfaceNames.find(interfaceName) == interfaceNames.end()){
if(fileName == TEMP_ALL)
i = new Interface(interfaceName, false);
else
i = new Interface(interfaceName, true);
}
else{
i = interfaceNames[interfaceName];
if(fileName != TEMP_ALL)
i->setStatus(true);
}
i->setAttached(true);
i->setInterfaceName(interfaceName);
QString hardName = "Ethernet";
int hardwareName = line.find("Link encap:");
int macAddress = line.find("HWaddr");
if(macAddress == -1)
macAddress = line.length();
if(hardwareName != -1)
i->setHardwareName(line.mid(hardwareName+11, macAddress-(hardwareName+11)) + QString(" (%1)").arg(i->getInterfaceName()));
// We have found an interface
//qDebug(QString("MainWindowImp: Found Interface: %1").arg(line).latin1());
interfaceNames.insert(i->getInterfaceName(), i);
updateInterface(i);
}
}
file.close();
QFile::remove(fileName);
+ if(threads.count() == 0){
+ Interfaces i;
+ QStringList list = i.getInterfaceList();
+ QMap<QString, Interface*>::Iterator it;
+ for ( QStringList::Iterator ni = list.begin(); ni != list.end(); ++ni ) {
+ for( it = interfaceNames.begin(); it != interfaceNames.end(); ++it ){
+ if(it.key() == (*ni)){
+ Interface *i = new Interface(*ni, false);
+ i->setAttached(false);
+ i->setHardwareName(QString("Disconnected (%1)").arg(*ni));
+ i->setInterfaceName(*ni);
+ interfaceNames.insert(i->getInterfaceName(), i);
+ updateInterface(i);
+ }
+ }
+ }
+ }
}
/**
* 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){
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
item->setPixmap(0, (Resource::loadPixmap(i->getStatus() ? "up": "down")));
QString typeName = "lan";
if(i->getHardwareName().contains("Local Loopback"))
typeName = "lo";
if(i->getInterfaceName().contains("irda"))
typeName = "irda";
if(i->getInterfaceName().contains("wlan"))
typeName = "wlan";
+
+ if(!i->isAttached())
+ typeName = "connect_no";
// Actually try to use the Module
if(i->getModuleOwner() != NULL)
typeName = i->getModuleOwner()->getPixmapName(i);
item->setPixmap(1, (Resource::loadPixmap(typeName)));
item->setText(2, i->getHardwareName());
item->setText(3, (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.", "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 anything.","Need One Profile.", "Ok");
return;
}
QString profileToRemove = profilesList->currentText();
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));
}
}
/**
* A new profile has been selected, change.
* @param newProfile the new profile.
*/
void MainWindowImp::changeProfile(){
currentProfileLabel->setText(profilesList->text(profilesList->currentItem()));
}
// mainwindowimp.cpp
diff --git a/noncore/net/networksetup/networksetup.pro b/noncore/net/networksetup/networksetup.pro index f09db93..441bbaa 100644 --- a/noncore/net/networksetup/networksetup.pro +++ b/noncore/net/networksetup/networksetup.pro @@ -1,11 +1,11 @@ -DESTDIR = $(OPIEDIR)/bin +#DESTDIR = $(OPIEDIR)/bin TEMPLATE = app #CONFIG = qt warn_on debug CONFIG = qt warn_on release HEADERS = mainwindowimp.h addconnectionimp.h interface.h interfaceinformationimp.h interfacesetupimp.h interfaces.h defaultmodule.h kprocctrl.h module.h kprocess.h SOURCES = main.cpp mainwindowimp.cpp addconnectionimp.cpp interface.cpp interfaceinformationimp.cpp interfacesetupimp.cpp kprocctrl.cpp kprocess.cpp interfaces.cpp -INCLUDEPATH += $(OPIEDIR)/include -DEPENDPATH += $(OPIEDIR)/include +#INCLUDEPATH += $(OPIEDIR)/include +#DEPENDPATH += $(OPIEDIR)/include LIBS += -lqpe INTERFACES = mainwindow.ui addconnection.ui interfaceinformation.ui interfaceadvanced.ui interfacesetup.ui TARGET = networksetup diff --git a/noncore/settings/networksettings/TODO b/noncore/settings/networksettings/TODO index 70d6717..7386646 100644 --- a/noncore/settings/networksettings/TODO +++ b/noncore/settings/networksettings/TODO @@ -1,7 +1,8 @@ [ ] Wlanmodule needs to check if an interface supports wireless extensions. [x] When you set options in wlanmodule, hit OK, it exits all of networksetup, doesnt bring you back to the main screen. [x] Wlanmodule isnt writing out wireless.opts [ ] Need a means of bringing an interface up and down (calling out ifup/ifdown) from the gui. + -Ben- Click information, then click up or down... :-D diff --git a/noncore/settings/networksettings/interfaces.cpp b/noncore/settings/networksettings/interfaces.cpp index b8a3e7f..1287d90 100644 --- a/noncore/settings/networksettings/interfaces.cpp +++ b/noncore/settings/networksettings/interfaces.cpp @@ -1,430 +1,454 @@ #include "interfaces.h" #include <qfile.h> #include <qtextstream.h> #include <qregexp.h> #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)){ qDebug(QString("Interfaces: Can't open file: %1 for reading.").arg(interfacesFile).latin1()); 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. Usefull 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 = 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 + * @param interface interface to check to see if it is on or not. * @return true is interface is in auto */ bool Interfaces::isAuto(QString interface){ QStringList autoLines = interfaces.grep(QRegExp(AUTO)); QStringList awi = autoLines.grep(QRegExp(interface)); if(awi.count() > 1) qDebug(QString("Interfaces: Found more then auto group with interface: %1.").arg(interface).latin1()); if(awi.count() < 1) return false; return true; } /** * 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(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{ if((*it).contains(interface)){ (*it) = (*it).replace(QRegExp(interface), ""); // clean up QString line = (*it).simplifyWhiteSpace(); line = line.replace(QRegExp(" "),""); if(line == AUTO) (*it) = ""; changed = true; // Don't break because we want to make sure we remove all cases. } } } } if(changed == false){ if(setAuto == true) interfaces.append(QString(AUTO" %1").arg(interface)); else{ qDebug(QString("Interfaces: Can't set interface %1 auto to false sense it is already false.").arg(interface).latin1()); } } 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 successfull. */ 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(){ return (currentIface != interfaces.end()); } /** * 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 successfull. */ bool Interfaces::addInterface(QString interface, QString family, QString method){ if(acceptedFamily.contains(family)==0) return false; interface = interface.simplifyWhiteSpace(); interface = interface.replace(QRegExp(" "), ""); interfaces.append(""); interfaces.append(QString(IFACE " %1 %2 %3").arg(interface).arg(family).arg(method)); return true; } /** * Remove the currently selected interface and all of its options. * @return bool if successfull or not. */ bool Interfaces::removeInterface(){ if(currentIface == interfaces.end()) return false; (*currentIface) = ""; return removeAllInterfaceOptions(); } /** * 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){ error = true; 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){ error = true; return QString(); } QString family = getInterfaceFamily(error); if(error){ error = true; 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 successfull. */ bool Interfaces::setInterfaceName(QString newName){ if(currentIface == interfaces.end()) return false; newName = newName.simplifyWhiteSpace(); newName = newName.replace(QRegExp(" "), ""); bool returnValue = false; (*currentIface) = QString("iface %1 %2 %3").arg(newName).arg(getInterfaceFamily(returnValue)).arg(getInterfaceMethod(returnValue)); 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 successfull. */ bool Interfaces::setInterfaceFamily(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 successfull. */ bool Interfaces::setInterfaceMethod(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(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 is set to an * empty string then option is removed. * @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(QString option, QString value){ return setOption(currentIface, option, value); } /** * Removes all of the options from the currently selected interface. * @return bool error if if successfull */ 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 successfull. */ bool Interfaces::setMapping(QString interface){ interface = interface.simplifyWhiteSpace(); interface = interface.replace(QRegExp(" "), ""); return setStanza(MAPPING, interface, 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(QString interfaces){ interfaces.append(""); interfaces.append(QString(MAPPING " %1").arg(interfaces)); } /** * 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 successfull. */ bool Interfaces::setMap(QString map, QString value){ return setOption(currentMapping, map, value); } /** * Get a map value within a mapping. * @param map map to get value of * @param bool true if it is successfull. * @return value that goes to the map */ QString Interfaces::getMap(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 successfull. */ bool Interfaces::setScript(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 successfull. * @return bool true if the stanza is found. */ bool Interfaces::setStanza(QString stanza, 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)){ if(found == true){ qDebug(QString("Interfaces: Found multiple stanza's for search: %1 %2").arg(stanza).arg(option).latin1()); } 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 successfull, false otherwise. */ bool Interfaces::setOption(QStringList::Iterator start, QString option, 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){ if(!found && value != ""){ // Got to the end of the stanza without finding it, so append it. interfaces.insert(--it, QString("\t%1 %2").arg(option).arg(value)); } break; } if((*it).contains(option)){ // Found it in stanza so replace it. if(found) qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); found = true; if(value == "") (*it) = ""; else (*it) = QString("\t%1 %2").arg(option).arg(value); } } return true; } /** * Removes all options in a stanza * @param start the start of the stanza * @return bool true if successfull, false otherwise. */ bool Interfaces::removeAllOptions(QStringList::Iterator start){ if(start == interfaces.end()) return false; diff --git a/noncore/settings/networksettings/interfaces.h b/noncore/settings/networksettings/interfaces.h index 2cc9689..8b4788c 100644 --- a/noncore/settings/networksettings/interfaces.h +++ b/noncore/settings/networksettings/interfaces.h @@ -1,70 +1,71 @@ #ifndef INTERFACES_H #define INTERFACES_H #include <qstring.h> #include <qstringlist.h> #define INTERFACES_LOOPBACK "loopback" #define INTERFACES_FAMILY_INET "inet" #define INTERFACES_FAMILY_IPX "ipx" #define INTERFACES_FAMILY_INET6 "inet6" #define INTERFACES_METHOD_DHCP "dhcp" #define INTERFACES_METHOD_STATIC "static" #define INTERFACES_METHOD_PPP "ppp" /** * This class provides a clean frontend for parsing the network interfaces file. * It provides helper functions to minipulate the options within the file. * See the interfaces man page for the syntax rules. */ class Interfaces { public: Interfaces(QString useInterfacesFile = "/etc/network/interfaces"); + QStringList getInterfaceList(); bool isAuto(QString interface); bool setAuto(QString interface, bool setAuto); bool removeInterface(); bool addInterface(QString interface, QString family, QString method); bool setInterface(QString interface); bool isInterfaceSet(); QString getInterfaceName(bool &error); bool setInterfaceName(QString newName); QString getInterfaceFamily(bool &error); bool setInterfaceFamily(QString newName); QString getInterfaceMethod(bool &error); bool setInterfaceMethod(QString newName); QString getInterfaceOption(QString option, bool &error); bool setInterfaceOption(QString option, QString value); bool removeAllInterfaceOptions(); bool setMapping(QString interface); void addMapping(QString interfaces); bool setMap(QString map, QString value); QString getMap(QString map, bool &error); bool setScript(QString); QString getScript(bool &error); bool write(); private: bool setStanza(QString stanza, QString option,QStringList::Iterator &iterator); bool setOption(QStringList::Iterator start, QString option, QString value); QString getOption(QStringList::Iterator start, QString option, bool &error); bool removeAllOptions(QStringList::Iterator start); QString interfacesFile; QStringList interfaces; QStringList::Iterator currentIface; QStringList::Iterator currentMapping; QStringList acceptedFamily; }; #endif // interfaces diff --git a/noncore/settings/networksettings/mainwindowimp.cpp b/noncore/settings/networksettings/mainwindowimp.cpp index 36f12e0..24af1ec 100644 --- a/noncore/settings/networksettings/mainwindowimp.cpp +++ b/noncore/settings/networksettings/mainwindowimp.cpp @@ -1,428 +1,450 @@ #include "mainwindowimp.h"
#include "addconnectionimp.h"
#include "interfaceinformationimp.h"
#include "interfacesetupimp.h"
+#include "interfaces.h"
+
#include "module.h"
#include "kprocess.h"
#include <qpushbutton.h>
#include <qtabwidget.h>
#include <qlistbox.h>
#include <qlineedit.h>
#include <qlistview.h>
#include <qheader.h>
#include <qlabel.h>
#include <qmainwindow.h>
#include <qmessagebox.h>
#include <qpe/config.h>
#include <qpe/qlibrary.h>
#include <qpe/resource.h>
#include <qpe/qpeapplication.h>
#include <qlist.h>
#include <qdir.h>
#include <qfile.h>
#include <qtextstream.h>
#define TEMP_ALL "/tmp/ifconfig-a"
#define TEMP_UP "/tmp/ifconfig"
MainWindowImp::MainWindowImp(QWidget *parent, const char *name) : MainWindow(parent, name, true), advancedUserMode(false){
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&)));
// Load connections.
loadModules(QPEApplication::qpeDir() + "/plugins/networksetup");
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));
advancedUserMode = cfg.readBoolEntry("AdvancedUserMode", false);
}
/**
* Deconstructor. Save profiles. Delete loaded libraries.
*/
MainWindowImp::~MainWindowImp(){
// Save profiles.
if(profiles.count() > 1){
Config cfg("NetworkSetup");
cfg.setGroup("General");
cfg.writeEntry("Profiles", profiles.join(" "));
}
// Delete Modules and Libraries
QMap<Module*, QLibrary*>::Iterator it;
for( it = libraries.begin(); it != libraries.end(); ++it ){
delete it.key();
delete it.data();
}
}
/**
* 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(QString path){
qDebug(path.latin1());
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()) ) {
if(fi->fileName().contains(".so")){
loadPlugin(path + "/" + fi->fileName());
}
++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(QString pluginFileName, QString resolveString){
qDebug(QString("MainWindowImp::loadPlugin: %1").arg(pluginFileName).latin1());
QLibrary *lib = new QLibrary(pluginFileName);
void *functionPointer = lib->resolve(resolveString);
if( !functionPointer ){
qDebug(QString("MainWindowImp: File: %1 is not a plugin, but though was.").arg(pluginFileName).latin1());
delete lib;
return NULL;
}
// Try to get an object.
Module *object = ((Module* (*)()) functionPointer)();
if(object == NULL){
qDebug("MainWindowImp: Couldn't create object, but did load library!");
delete lib;
return NULL;
}
// Store for deletion later
libraries.insert(object, lib);
return object;
}
/**
* 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;
list.insert("USB (PPP) / (ADD_TEST)", "A dialup connection over the USB port");
list.insert("IrDa (PPP) / (ADD_TEST)", "A dialup connection over the IdDa port");
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.", "Ok");
return;
}
AddConnectionImp addNewConnection(this, "AddConnectionImp", true);
addNewConnection.addConnections(list);
addNewConnection.showMaximized();
if(QDialog::Accepted == addNewConnection.exec()){
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){
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, "Error","Please select an interface.", "Ok");
return;
}
Interface *i = interfaceItems[item];
if(i->getModuleOwner() == NULL){
QMessageBox::information(this, "Can't remove interface.", "Interface is built in.", "Ok");
}
else{
if(!i->getModuleOwner()->remove(i))
QMessageBox::information(this, "Error", "Unable to remove.", "Ok");
else{
QMessageBox::information(this, "Success", "Interface was removed.", "Ok");
// TODO memory managment....
// who deletes the interface?
}
}
}
/**
* 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 with a empty
* tab. If tab is !NULL then append the interfaces setup widget to it.
*/
void MainWindowImp::configureClicked(){
QListViewItem *item = connectionList->currentItem();
if(!item){
QMessageBox::information(this, "Error","Please select an interface.", QMessageBox::Ok);
return;
}
Interface *i = interfaceItems[item];
if(i->getModuleOwner()){
QTabWidget *tabWidget = NULL;
QWidget *moduleConfigure = i->getModuleOwner()->configure(&tabWidget);
if(moduleConfigure != NULL){
if(tabWidget != NULL){
InterfaceSetupImp *configure = new InterfaceSetupImp(tabWidget, "InterfaceSetupImp", i, true);
tabWidget->insertTab(configure, "TCP/IP");
}
moduleConfigure->showMaximized();
moduleConfigure->show();
return;
}
}
InterfaceSetupImp *configure = new InterfaceSetupImp(0, "InterfaceSetupImp", i, true);
configure->showMaximized();
configure->show();
}
/**
* 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 with a empty
* tab. If tab is !NULL then append the interfaces setup widget to it.
*/
void MainWindowImp::informationClicked(){
QListViewItem *item = connectionList->currentItem();
if(!item){
QMessageBox::information(this, "Error","Please select an interface.", QMessageBox::Ok);
return;
}
Interface *i = interfaceItems[item];
if(i->getModuleOwner()){
QTabWidget *tabWidget = NULL;
QWidget *moduleInformation = i->getModuleOwner()->information(&tabWidget);
if(moduleInformation != NULL){
if(tabWidget != NULL){
InterfaceInformationImp *information = new InterfaceInformationImp(tabWidget, "InterfaceSetupImp", i, true);
tabWidget->insertTab(information, "TCP/IP");
}
moduleInformation->showMaximized();
moduleInformation->show();
return;
}
}
InterfaceInformationImp *information = new InterfaceInformationImp(0, "InterfaceSetupImp", i, true);
information->showMaximized();
information->show();
}
/**
* Aquire the list of active interfaces from ifconfig
* Call ifconfig and ifconfig -a
*/
void MainWindowImp::getInterfaceList(){
KShellProcess *processAll = new KShellProcess();
*processAll << "/sbin/ifconfig" << "-a" << " > " TEMP_ALL;
connect(processAll, SIGNAL(processExited(KProcess *)),
this, SLOT(jobDone(KProcess *)));
threads.insert(processAll, TEMP_ALL);
processAll->start(KShellProcess::NotifyOnExit);
KShellProcess *process = new KShellProcess();
*process << "/sbin/ifconfig" << " > " TEMP_UP;
connect(process, SIGNAL(processExited(KProcess *)),
this, SLOT(jobDone(KProcess *)));
threads.insert(process, TEMP_UP);
process->start(KShellProcess::NotifyOnExit);
}
void MainWindowImp::jobDone(KProcess *process){
QString fileName = threads[process];
threads.remove(process);
delete process;
QFile file(fileName);
if (!file.open(IO_ReadOnly)){
qDebug(QString("MainWindowImp: Can't open file: %1").arg(fileName).latin1());
return;
}
QTextStream stream( &file );
QString line;
while ( !stream.eof() ) {
line = stream.readLine();
int space = line.find(" ");
if(space > 1){
// We have found an interface
QString interfaceName = line.mid(0, space);
if(!advancedUserMode){
if(interfaceName == "lo")
break;
}
Interface *i;
// See if we already have it
if(interfaceNames.find(interfaceName) == interfaceNames.end()){
if(fileName == TEMP_ALL)
i = new Interface(interfaceName, false);
else
i = new Interface(interfaceName, true);
}
else{
i = interfaceNames[interfaceName];
if(fileName != TEMP_ALL)
i->setStatus(true);
}
i->setAttached(true);
i->setInterfaceName(interfaceName);
QString hardName = "Ethernet";
int hardwareName = line.find("Link encap:");
int macAddress = line.find("HWaddr");
if(macAddress == -1)
macAddress = line.length();
if(hardwareName != -1)
i->setHardwareName(line.mid(hardwareName+11, macAddress-(hardwareName+11)) + QString(" (%1)").arg(i->getInterfaceName()));
// We have found an interface
//qDebug(QString("MainWindowImp: Found Interface: %1").arg(line).latin1());
interfaceNames.insert(i->getInterfaceName(), i);
updateInterface(i);
}
}
file.close();
QFile::remove(fileName);
+ if(threads.count() == 0){
+ Interfaces i;
+ QStringList list = i.getInterfaceList();
+ QMap<QString, Interface*>::Iterator it;
+ for ( QStringList::Iterator ni = list.begin(); ni != list.end(); ++ni ) {
+ for( it = interfaceNames.begin(); it != interfaceNames.end(); ++it ){
+ if(it.key() == (*ni)){
+ Interface *i = new Interface(*ni, false);
+ i->setAttached(false);
+ i->setHardwareName(QString("Disconnected (%1)").arg(*ni));
+ i->setInterfaceName(*ni);
+ interfaceNames.insert(i->getInterfaceName(), i);
+ updateInterface(i);
+ }
+ }
+ }
+ }
}
/**
* 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){
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
item->setPixmap(0, (Resource::loadPixmap(i->getStatus() ? "up": "down")));
QString typeName = "lan";
if(i->getHardwareName().contains("Local Loopback"))
typeName = "lo";
if(i->getInterfaceName().contains("irda"))
typeName = "irda";
if(i->getInterfaceName().contains("wlan"))
typeName = "wlan";
+
+ if(!i->isAttached())
+ typeName = "connect_no";
// Actually try to use the Module
if(i->getModuleOwner() != NULL)
typeName = i->getModuleOwner()->getPixmapName(i);
item->setPixmap(1, (Resource::loadPixmap(typeName)));
item->setText(2, i->getHardwareName());
item->setText(3, (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.", "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 anything.","Need One Profile.", "Ok");
return;
}
QString profileToRemove = profilesList->currentText();
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));
}
}
/**
* A new profile has been selected, change.
* @param newProfile the new profile.
*/
void MainWindowImp::changeProfile(){
currentProfileLabel->setText(profilesList->text(profilesList->currentItem()));
}
// mainwindowimp.cpp
diff --git a/noncore/settings/networksettings/networksetup.pro b/noncore/settings/networksettings/networksetup.pro index f09db93..441bbaa 100644 --- a/noncore/settings/networksettings/networksetup.pro +++ b/noncore/settings/networksettings/networksetup.pro @@ -1,11 +1,11 @@ -DESTDIR = $(OPIEDIR)/bin +#DESTDIR = $(OPIEDIR)/bin TEMPLATE = app #CONFIG = qt warn_on debug CONFIG = qt warn_on release HEADERS = mainwindowimp.h addconnectionimp.h interface.h interfaceinformationimp.h interfacesetupimp.h interfaces.h defaultmodule.h kprocctrl.h module.h kprocess.h SOURCES = main.cpp mainwindowimp.cpp addconnectionimp.cpp interface.cpp interfaceinformationimp.cpp interfacesetupimp.cpp kprocctrl.cpp kprocess.cpp interfaces.cpp -INCLUDEPATH += $(OPIEDIR)/include -DEPENDPATH += $(OPIEDIR)/include +#INCLUDEPATH += $(OPIEDIR)/include +#DEPENDPATH += $(OPIEDIR)/include LIBS += -lqpe INTERFACES = mainwindow.ui addconnection.ui interfaceinformation.ui interfaceadvanced.ui interfacesetup.ui TARGET = networksetup |