Skip to content

Commit

Permalink
docsteer#203 Allow localhost selection
Browse files Browse the repository at this point in the history
If last selection was localhost/offline, then nic selection dialog will prompt user on every use.
  • Loading branch information
marcusbirkin committed Jan 26, 2020
1 parent 6f1ae83 commit d62db97
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
29 changes: 21 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
29 changes: 27 additions & 2 deletions src/nicselectdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "ui_nicselectdialog.h"
#include "preferences.h"
#include <QNetworkInterface>
#include <QMessageBox>

NICSelectDialog::NICSelectDialog(QWidget *parent) :
QDialog(parent),
Expand Down Expand Up @@ -47,7 +48,7 @@ NICSelectDialog::NICSelectDialog(QWidget *parent) :
}
}

ui->btnSelect->setEnabled(false);
on_listWidget_itemSelectionChanged();
}

NICSelectDialog::~NICSelectDialog()
Expand All @@ -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));
}


Expand All @@ -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();
}
23 changes: 17 additions & 6 deletions src/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/preferencesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d62db97

Please sign in to comment.