From 76b0e3e9f74f652d490abba79fe050516edcecee Mon Sep 17 00:00:00 2001 From: Akshay Joshi Date: Tue, 8 Sep 2020 12:00:24 +0530 Subject: [PATCH] Moved logic to check fixed port is in use to ConfigWindow class. refs #5751 --- runtime/ConfigWindow.cpp | 56 ++++++++++++++++++++++++++++++---------- runtime/ConfigWindow.h | 2 ++ runtime/MenuActions.cpp | 1 + runtime/Runtime.cpp | 24 ----------------- runtime/Runtime.h | 1 - 5 files changed, 46 insertions(+), 38 deletions(-) diff --git a/runtime/ConfigWindow.cpp b/runtime/ConfigWindow.cpp index da78b59b4..17169751c 100644 --- a/runtime/ConfigWindow.cpp +++ b/runtime/ConfigWindow.cpp @@ -14,6 +14,8 @@ #include "ui_ConfigWindow.h" #include +#include +#include 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; +} diff --git a/runtime/ConfigWindow.h b/runtime/ConfigWindow.h index 850fda2fa..3af28ebd6 100644 --- a/runtime/ConfigWindow.h +++ b/runtime/ConfigWindow.h @@ -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 diff --git a/runtime/MenuActions.cpp b/runtime/MenuActions.cpp index ec4b98eeb..bcf573d43 100644 --- a/runtime/MenuActions.cpp +++ b/runtime/MenuActions.cpp @@ -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(); diff --git a/runtime/Runtime.cpp b/runtime/Runtime.cpp index 432ee0904..1dce5f223 100644 --- a/runtime/Runtime.cpp +++ b/runtime/Runtime.cpp @@ -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) { diff --git a/runtime/Runtime.h b/runtime/Runtime.h index 655acbb2a..1fcec34ec 100644 --- a/runtime/Runtime.h +++ b/runtime/Runtime.h @@ -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: