diff --git a/src/main.cpp b/src/main.cpp index 3aceac78..48158fd3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -98,14 +98,25 @@ int main(int argc, char *argv[]) // Setup interface bool newInterface = false; - if(!Preferences::getInstance()->defaultInterfaceAvailable()) + // Interface not avaliable, or last selection was offline/localhost + if (!Preferences::getInstance()->defaultInterfaceAvailable() + || Preferences::getInstance()->getInstance()->networkInterface().flags().testFlag(QNetworkInterface::IsLoopBack) + ) { NICSelectDialog d; int result = d.exec(); - if(result==QDialog::Accepted) - { - Preferences::getInstance()->setNetworkInterface(d.getSelectedInterface()); + switch (result) { + case QDialog::Accepted: + { + Preferences::getInstance()->setNetworkInterface(d.getSelectedInterface()); + break; + } + case QDialog::Rejected: + { + // Exit application + return -1; + } } newInterface = true; @@ -140,10 +151,12 @@ int main(int argc, char *argv[]) w->showMaximized(); // Show interface name on statusbar - w->statusBar()->showMessage(QObject::tr("Selected interface: %1").arg( - Preferences::getInstance()->networkInterface().humanReadableName()) - ); - + if (Preferences::getInstance()->networkInterface().flags().testFlag(QNetworkInterface::IsLoopBack)) + w->statusBar()->showMessage(QObject::tr(" WORKING OFFLINE")); + else + w->statusBar()->showMessage( + QObject::tr("Selected interface: %1").arg( + Preferences::getInstance()->networkInterface().humanReadableName())); // Check firewall if not newly selected if (!newInterface) { diff --git a/src/nicselectdialog.cpp b/src/nicselectdialog.cpp index 6e0ce70b..8a23b021 100644 --- a/src/nicselectdialog.cpp +++ b/src/nicselectdialog.cpp @@ -17,6 +17,7 @@ #include "ui_nicselectdialog.h" #include "preferences.h" #include +#include NICSelectDialog::NICSelectDialog(QWidget *parent) : QDialog(parent), @@ -47,7 +48,7 @@ NICSelectDialog::NICSelectDialog(QWidget *parent) : } } - ui->btnSelect->setEnabled(false); + on_listWidget_itemSelectionChanged(); } NICSelectDialog::~NICSelectDialog() @@ -57,7 +58,17 @@ NICSelectDialog::~NICSelectDialog() void NICSelectDialog::on_listWidget_itemSelectionChanged() { - ui->btnSelect->setEnabled(true); + // Empty or no selection + if (!ui->listWidget->count() || ui->listWidget->currentRow() == -1) + { + ui->btnSelect->setEnabled(false); + return; + } + + // Show select if not loopback + qDebug() << ui->listWidget->currentRow(); + ui->btnSelect->setEnabled( + !m_interfaceList[ui->listWidget->currentRow()].flags().testFlag(QNetworkInterface::IsLoopBack)); } @@ -69,5 +80,19 @@ void NICSelectDialog::on_btnSelect_pressed() void NICSelectDialog::on_btnWorkOffline_pressed() { + // Select localhost + for (auto interface : m_interfaceList) + { + if (interface.flags().testFlag(QNetworkInterface::IsLoopBack)) + m_selectedInterface = interface; + accept(); + return; + } + + QMessageBox msgBox; + msgBox.setStandardButtons(QMessageBox::Ok); + msgBox.setIcon(QMessageBox::Critical); + msgBox.setText("Work Offline failed\r\nUnable to locate localhost adapter"); + msgBox.exec(); reject(); } diff --git a/src/preferences.cpp b/src/preferences.cpp index 5222a1d9..29b1ff06 100644 --- a/src/preferences.cpp +++ b/src/preferences.cpp @@ -75,6 +75,7 @@ void Preferences::setNetworkInterface(const QNetworkInterface &value) void Preferences::SetNetworkListenAll(const bool &value) { + if (networkInterface().flags().testFlag(QNetworkInterface::IsLoopBack)) return; m_interfaceListenAll = value; } @@ -83,6 +84,7 @@ bool Preferences::GetNetworkListenAll() #ifdef TARGET_WINXP return false; #else + if (networkInterface().flags().testFlag(QNetworkInterface::IsLoopBack)) return false; return m_interfaceListenAll; #endif } @@ -204,14 +206,23 @@ bool Preferences::defaultInterfaceAvailable() bool Preferences::interfaceSuitable(QNetworkInterface *inter) { - // Up, can multicast, and has IPv4? - if (inter->isValid() - && inter->flags().testFlag(QNetworkInterface::IsRunning) - && inter->flags().testFlag(QNetworkInterface::IsUp) - && inter->flags().testFlag(QNetworkInterface::CanMulticast) - && !inter->flags().testFlag(QNetworkInterface::IsLoopBack) + + if (inter->isValid() && ( + ( + // Up, can multicast... + inter->flags().testFlag(QNetworkInterface::IsRunning) + && inter->flags().testFlag(QNetworkInterface::IsUp) + && inter->flags().testFlag(QNetworkInterface::CanMulticast) + ) || ( + // Up, is loopback... + inter->flags().testFlag(QNetworkInterface::IsRunning) + && inter->flags().testFlag(QNetworkInterface::IsUp) + && inter->flags().testFlag(QNetworkInterface::IsLoopBack) + ) ) + ) { + // ...has IPv4 foreach (QNetworkAddressEntry addr, inter->addressEntries()) { if(addr.ip().protocol() == QAbstractSocket::IPv4Protocol) return true; diff --git a/src/preferencesdialog.cpp b/src/preferencesdialog.cpp index cb4844ce..bf06e0a3 100644 --- a/src/preferencesdialog.cpp +++ b/src/preferencesdialog.cpp @@ -61,6 +61,8 @@ PreferencesDialog::PreferencesDialog(QWidget *parent) : } } ui->cbListenAll->setChecked(Preferences::getInstance()->GetNetworkListenAll()); + if (Preferences::getInstance()->networkInterface().flags().testFlag(QNetworkInterface::IsLoopBack)) + ui->cbListenAll->setEnabled(false); #ifdef TARGET_WINXP ui->cbListenAll->setEnabled(false); #endif