diff options
author | Sebastien Braun | 2010-10-04 00:11:13 +0200 |
---|---|---|
committer | Sebastien Braun | 2010-10-05 22:12:26 +0200 |
commit | b003c313227e330c3ba92714655a4fc0f408f9fa (patch) | |
tree | 6c671fb9ca811dab632418f38bf5334d562a7d29 | |
parent | Implement Network-wide Configuration Interface for Multicast File Transfer (diff) | |
download | pvs-b003c313227e330c3ba92714655a4fc0f408f9fa.tar.gz pvs-b003c313227e330c3ba92714655a4fc0f408f9fa.tar.xz pvs-b003c313227e330c3ba92714655a4fc0f408f9fa.zip |
Remove system-dependent interface enumeration code and introduce proper
Model-View portable code in its stead.
-rw-r--r-- | src/gui/clientConfigDialog.cpp | 52 | ||||
-rw-r--r-- | src/gui/clientConfigDialog.h | 5 | ||||
-rw-r--r-- | src/net/pvsNetworkInterfaceListModel.cpp | 81 | ||||
-rw-r--r-- | src/net/pvsNetworkInterfaceListModel.h | 35 |
4 files changed, 124 insertions, 49 deletions
diff --git a/src/gui/clientConfigDialog.cpp b/src/gui/clientConfigDialog.cpp index 3867118..70d38fc 100644 --- a/src/gui/clientConfigDialog.cpp +++ b/src/gui/clientConfigDialog.cpp @@ -17,16 +17,13 @@ */ #include <QtDebug> +#include <QNetworkInterface> +#include <QStandardItemModel> #include "clientConfigDialog.h" #include <cerrno> #include <cstdlib> #include <cstring> - -// For getting the network interface list: -#ifdef __linux -# include <sys/ioctl.h> -# include <net/if.h> -#endif +#include <src/net/pvsNetworkInterfaceListModel.h> using namespace std; @@ -38,11 +35,10 @@ ClientConfigDialog::ClientConfigDialog(QWidget *parent) : connect(this, SIGNAL(accepted()), this, SLOT(writeSettings())); connect(radioButtonOtherRO, SIGNAL(clicked()), this, SLOT(checkPermissions())); - connect(reloadInterfaceListButton, SIGNAL(clicked()), this, SLOT(reloadNetworkInterfaceList())); - reloadNetworkInterfaceList(); + _interfaceListModel = new PVSNetworkInterfaceListModel(this); interfaceList->setModel(_interfaceListModel); interfaceList->setModelColumn(0); - + connect(reloadInterfaceListButton, SIGNAL(clicked()), _interfaceListModel, SLOT(reloadInterfaceList())); } ClientConfigDialog::~ClientConfigDialog() @@ -124,41 +120,3 @@ void ClientConfigDialog::checkPermissions() if (radioButtonLecturerNO->isChecked() && radioButtonOtherRO->isChecked()) radioButtonLecturerRO->setChecked(true); } - -void ClientConfigDialog::reloadNetworkInterfaceList() -{ -#ifdef __linux - static struct ifreq ifreqs[20]; - ifconf ifconfigs; - memset(&ifconfigs, 0, sizeof(ifconfigs)); - ifconfigs.ifc_len = sizeof(ifreqs); - ifconfigs.ifc_buf = (char*)&ifreqs; - - int nosock = socket(AF_INET, SOCK_STREAM, 0); - if (nosock < 0) - { - qWarning() << "Could not get a socket descriptor:" << strerror(errno); - } - int retval; - if ((retval = ioctl(nosock, SIOCGIFCONF, (char*)(&ifconfigs))) < 0) - { - qWarning() << "Could not get the list of interfaces:" << strerror(errno); - return; - } - - QStringList interfaces; - for(int i = 0; i < ifconfigs.ifc_len/sizeof(struct ifreq); i++) - { - char ifname[IFNAMSIZ + 1]; - strncpy(ifname, ifreqs[i].ifr_name, IFNAMSIZ); - ifname[IFNAMSIZ] = '\0'; - interfaces << QString::fromLocal8Bit(ifname); - } - if(!_interfaceListModel) - _interfaceListModel = new QStringListModel(interfaces, this); - else - _interfaceListModel->setStringList(interfaces); -#else -# warning "We have no way to get your system's network interface list. Some porting may be required." -#endif -} diff --git a/src/gui/clientConfigDialog.h b/src/gui/clientConfigDialog.h index 803f2c8..5582ec6 100644 --- a/src/gui/clientConfigDialog.h +++ b/src/gui/clientConfigDialog.h @@ -17,6 +17,8 @@ #include <QtGui> #include "ui_clientConfigDialog.h" +class QAbstractItemModel; + class ClientConfigDialog: public QDialog, private Ui::ClientConfigDialogClass { Q_OBJECT @@ -35,11 +37,10 @@ Q_SIGNALS: private Q_SLOTS: void checkPermissions(); - void reloadNetworkInterfaceList(); private: QSettings _settings; - QStringListModel* _interfaceListModel; + QAbstractItemModel* _interfaceListModel; }; diff --git a/src/net/pvsNetworkInterfaceListModel.cpp b/src/net/pvsNetworkInterfaceListModel.cpp new file mode 100644 index 0000000..67d0c0a --- /dev/null +++ b/src/net/pvsNetworkInterfaceListModel.cpp @@ -0,0 +1,81 @@ +/* + * pvsNetworkInterfaceListModel.cpp + * + * Created on: 04.08.2010 + * Author: brs + */ + +#include "pvsNetworkInterfaceListModel.h" +#include <QStringList> + +PVSNetworkInterfaceListModel::PVSNetworkInterfaceListModel(QObject* parent) : + QAbstractListModel(parent) +{ + reloadInterfaceList(); +} + +PVSNetworkInterfaceListModel::~PVSNetworkInterfaceListModel() +{ +} + +void PVSNetworkInterfaceListModel::reloadInterfaceList() +{ + _interfaces = QNetworkInterface::allInterfaces(); + reset(); +} + +QVariant PVSNetworkInterfaceListModel::data(QModelIndex const& index, int role) const +{ + int i = index.row(); + if(0 > i || i >= _interfaces.size()) + { + return QVariant(); + } + QNetworkInterface intf = _interfaces.at(i); + + switch(role) + { + case Qt::DisplayRole: + { + QString name = intf.humanReadableName(); + QList<QNetworkAddressEntry> addresses = intf.addressEntries(); + QStringList l; + + foreach(QNetworkAddressEntry addr, addresses) + { + l.append(addr.ip().toString()); + } + + return QString("%1 (%2)").arg(name).arg(l.join(", ")); + } + case Qt::EditRole: + case Qt::UserRole: + return intf.name(); + default: + return QVariant(); + } +} + +QVariant PVSNetworkInterfaceListModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if(section == 0 && orientation == Qt::Vertical && role == Qt::DisplayRole) + { + return tr("Interface"); + } + else + { + return QVariant(); + } +} + +int PVSNetworkInterfaceListModel::rowCount(QModelIndex const& parent) const +{ + if(parent.isValid()) + { + return 0; + } + else + { + return _interfaces.size(); + } +} diff --git a/src/net/pvsNetworkInterfaceListModel.h b/src/net/pvsNetworkInterfaceListModel.h new file mode 100644 index 0000000..3a9b95d --- /dev/null +++ b/src/net/pvsNetworkInterfaceListModel.h @@ -0,0 +1,35 @@ +/* + * pvsNetworkInterfaceListModel.h + * + * Created on: 04.08.2010 + * Author: brs + */ + +#ifndef PVSNETWORKINTERFACELISTMODEL_H_ +#define PVSNETWORKINTERFACELISTMODEL_H_ + +#include <QAbstractListModel> +#include <QList> +#include <QNetworkInterface> +#include <QVariant> + +class PVSNetworkInterfaceListModel : public QAbstractListModel +{ + Q_OBJECT + +public: + PVSNetworkInterfaceListModel(QObject* parent = 0); + virtual ~PVSNetworkInterfaceListModel(); + + QVariant data(QModelIndex const& index, int role = Qt::DisplayRole) const; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + int rowCount(QModelIndex const&) const; + +private: + QList<QNetworkInterface> _interfaces; + +public slots: + void reloadInterfaceList(); +}; + +#endif /* PVSNETWORKINTERFACELISTMODEL_H_ */ |