Moved logic to check fixed port is in use to ConfigWindow class.

refs #5751
This commit is contained in:
Akshay Joshi 2020-09-08 12:00:24 +05:30
parent 98e86a8160
commit 76b0e3e9f7
5 changed files with 46 additions and 38 deletions

View File

@ -14,6 +14,8 @@
#include "ui_ConfigWindow.h"
#include <QSettings>
#include <QTcpSocket>
#include <QtWidgets>
ConfigWindow::ConfigWindow(QWidget *parent) :
QDialog(parent)
@ -23,13 +25,18 @@ ConfigWindow::ConfigWindow(QWidget *parent) :
void ConfigWindow::initConfigWindow()
{
QSettings settings;
ui = new Ui::ConfigWindow;
ui->setupUi(this);
m_needRestart = false;
setConfigValues();
}
void ConfigWindow::setConfigValues()
{
QSettings settings;
ui->browserCommandLineEdit->setText(settings.value("BrowserCommand").toString());
if(settings.value("FixedPort").toBool())
@ -70,19 +77,28 @@ void ConfigWindow::on_buttonBox_accepted()
QString pythonpath = ui->pythonPathLineEdit->text();
QString applicationpath = ui->applicationPathLineEdit->text();
m_needRestart = (settings.value("FixedPort").toBool() != fixedport ||
settings.value("PortNumber").toInt() != portnumber ||
settings.value("PythonPath").toString() != pythonpath ||
settings.value("ApplicationPath").toString() != applicationpath);
if (fixedport && (settings.value("FixedPort").toBool() != fixedport ||
settings.value("PortNumber").toInt() != portnumber) && isPortInUse(portnumber))
{
QString error = QString(QWidget::tr("The specified fixed port is already in use. Please provide any other valid port."));
QMessageBox::critical(Q_NULLPTR, QString(QWidget::tr("Fatal Error")), error);
}
else
{
m_needRestart = (settings.value("FixedPort").toBool() != fixedport ||
settings.value("PortNumber").toInt() != portnumber ||
settings.value("PythonPath").toString() != pythonpath ||
settings.value("ApplicationPath").toString() != applicationpath);
settings.setValue("BrowserCommand", browsercommand);
settings.setValue("FixedPort", fixedport);
settings.setValue("PortNumber", portnumber);
settings.setValue("OpenTabAtStartup", opentabatstartup);
settings.setValue("PythonPath", pythonpath);
settings.setValue("ApplicationPath", applicationpath);
settings.setValue("BrowserCommand", browsercommand);
settings.setValue("FixedPort", fixedport);
settings.setValue("PortNumber", portnumber);
settings.setValue("OpenTabAtStartup", opentabatstartup);
settings.setValue("PythonPath", pythonpath);
settings.setValue("ApplicationPath", applicationpath);
settings.sync();
settings.sync();
}
emit accepted(m_needRestart);
emit closing(true);
@ -104,3 +120,17 @@ void ConfigWindow::on_chkFixedPort_stateChanged(int state)
ui->spinPortNumber->setEnabled(false);
}
bool ConfigWindow::isPortInUse(const quint16 port) const
{
QTcpSocket socket;
// Bind the socket on the specified port.
socket.bind(port, QTcpSocket::DontShareAddress);
// Returns the host port number of the local socket if available; otherwise returns 0
quint16 tmpPort = socket.localPort();
if (tmpPort == 0)
return true;
return false;
}

View File

@ -24,6 +24,7 @@ class ConfigWindow : public QDialog
public:
explicit ConfigWindow(QWidget *parent = Q_NULLPTR);
void setConfigValues();
signals:
void accepted(bool needRestart);
@ -39,6 +40,7 @@ private:
bool m_needRestart;
void initConfigWindow();
bool isPortInUse(const quint16 port) const;
};
#endif // CONFIGWINDOW_H

View File

@ -68,6 +68,7 @@ void MenuActions::onConfig()
if (!m_configWindow)
m_configWindow = new ConfigWindow();
m_configWindow->setConfigValues();
m_configWindow->show();
m_configWindow->raise();
m_configWindow->activateWindow();

View File

@ -342,20 +342,6 @@ FloatingWindow * Runtime::createFloatingWindow(MenuActions *menuActions)
return floatingWindow;
}
bool Runtime::isPortInUse(const quint16 port) const
{
QTcpSocket socket;
// Bind the socket on the specified port.
socket.bind(port, QTcpSocket::DontShareAddress);
// Returns the host port number of the local socket if available; otherwise returns 0
quint16 tmpPort = socket.localPort();
if (tmpPort == 0)
return true;
return false;
}
void Runtime::openConfigureWindow(const QString errorMsg)
{
@ -411,16 +397,6 @@ Server * Runtime::startServerLoop(QString key)
while (!done)
{
if (isPortInUse(m_port))
{
QString error = QString(QWidget::tr("The specified port is already in use."));
// Open the configuration window
openConfigureWindow(error);
continue;
}
server = startServer(key);
if (server == NULL)
{

View File

@ -60,7 +60,6 @@ private:
void openBrowserTab(QString url) const;
QString serverRequest(QUrl url, QString path);
bool pingServer(QUrl url);
bool isPortInUse(const quint16 port) const;
void openConfigureWindow(const QString errorMsg);
private slots: