author | alwin <alwin> | 2005-03-07 21:22:42 (UTC) |
---|---|---|
committer | alwin <alwin> | 2005-03-07 21:22:42 (UTC) |
commit | ca82c064ab8834546c55b29c6b2f5c922075b450 (patch) (side-by-side diff) | |
tree | 5a7a097400cfb4388c3b742d3d4f517209789740 /noncore | |
parent | b9b5ec22a5969c57a2e2b940faf42ae6f9fd74d2 (diff) | |
download | opie-ca82c064ab8834546c55b29c6b2f5c922075b450.zip opie-ca82c064ab8834546c55b29c6b2f5c922075b450.tar.gz opie-ca82c064ab8834546c55b29c6b2f5c922075b450.tar.bz2 |
hostname should be filtered. Check return value of setting the hostname
and write operations.
-rw-r--r-- | noncore/settings/networksettings/mainwindow/mainwindowimp.cpp | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/noncore/settings/networksettings/mainwindow/mainwindowimp.cpp b/noncore/settings/networksettings/mainwindow/mainwindowimp.cpp index 0a3312c..3222e50 100644 --- a/noncore/settings/networksettings/mainwindow/mainwindowimp.cpp +++ b/noncore/settings/networksettings/mainwindow/mainwindowimp.cpp @@ -1,124 +1,125 @@ #include "mainwindowimp.h" #include "addconnectionimp.h" #include "interfaceinformationimp.h" #include "interfacesetupimp.h" #include "interfaces.h" #include "module.h" /* OPIE */ #include <opie2/odebug.h> #include <opie2/oprocess.h> #include <qpe/applnk.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> #include <qtextstream.h> +#include <qregexp.h> #if QT_VERSION < 0x030000 #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" // is this always right? #define _HOSTFILE "/etc/hostname" #define _IRDANAME "/proc/sys/net/irda/devname" using namespace Opie::Core; 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())); //remove tab with no function tabWidget->removePage( tab ); // Load connections. // /usr/local/kde/lib/libinterfaces.la loadModules(QPEApplication::qpeDir() + "plugins/networksettings"); 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")) { @@ -633,143 +634,158 @@ void MainWindowImp::removeProfile() 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; } void MainWindowImp::setHostname() { - QFile f(_HOSTFILE); + static QRegExp filter("[^A-Za-z0-9_\\-\\.]"); + if (filter.match(m_Nameinput->text())!=-1) { + odebug << "Wrong hostname" << oendl; + QMessageBox::critical(0, tr("Sorry"), tr("This is a wrong name.<br>Please use A-Z, a-z, _, - or a single dot.")); + return; + } - OProcess*h = new OProcess; + OProcess h; _procTemp=""; - *h << "hostname" << m_Nameinput->text(); - connect(h,SIGNAL(receivedStderr(Opie::Core::OProcess*,char*,int)),this,SLOT(slotHostname(Opie::Core::OProcess*,char*,int))); - h->start(OProcess::Block,OProcess::Stderr); - odebug << "Got " << _procTemp << " - " << h->exitStatus() << oendl; + h << "hostname" << m_Nameinput->text(); + connect(&h,SIGNAL(receivedStderr(Opie::Core::OProcess*,char*,int)),this,SLOT(slotHostname(Opie::Core::OProcess*,char*,int))); + h.start(OProcess::Block,OProcess::Stderr); + odebug << "Got " << _procTemp << " - " << h.exitStatus() << oendl; + if (h.exitStatus()!=0) { + QMessageBox::critical(0, tr("Sorry"), QString(tr("Could not set name.\n%1")).arg(_procTemp.stripWhiteSpace())); + return; + } _procTemp=""; - delete h; + QFile f(_HOSTFILE); if (f.open(IO_Truncate|IO_WriteOnly)) { QTextStream s(&f); s << m_Nameinput->text(); + } else { + QMessageBox::critical(0, tr("Sorry"), tr("Could not save name.")); + return; } + f.close(); f.setName(_IRDANAME); if (f.open(IO_WriteOnly)) { QTextStream s(&f); s << m_Nameinput->text(); + } else { + QMessageBox::critical(0, tr("Sorry"), tr("Could not set infrared name.")); } } void MainWindowImp::initHostname() { OProcess h; _procTemp=""; h << "hostname"; connect(&h,SIGNAL(receivedStdout(Opie::Core::OProcess*,char*,int)),this,SLOT(slotHostname(Opie::Core::OProcess*,char*,int))); h.start(OProcess::Block,OProcess::AllOutput); odebug << "Got " << _procTemp <<oendl; - m_Nameinput->setText(_procTemp); + m_Nameinput->setText(_procTemp.stripWhiteSpace()); _procTemp=""; } void MainWindowImp::slotHostname(Opie::Core::OProcess *proc, char *buffer, int buflen) { if (buflen < 1 || buffer==0) return; char*_t = new char[buflen+1]; ::memset(_t,0,buflen+1); ::memcpy(_t,buffer,buflen); _procTemp+=_t; delete[]_t; } |