From 49aab492e03077b892faf2800a9810b7d555a023 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 28 Jul 2021 12:24:03 +0800 Subject: [PATCH] Use spinbox special value to represent "Use any available port" WebAPI functionality is preserved (deprecated) for now and should be removed in the future. --- src/app/upgrade.cpp | 16 ++++++++++++++++ src/base/bittorrent/session.cpp | 16 ++-------------- src/base/bittorrent/session.h | 3 --- src/base/settingsstorage.cpp | 7 +++++++ src/base/settingsstorage.h | 1 + src/gui/optionsdialog.cpp | 7 +------ src/gui/optionsdialog.ui | 15 ++------------- src/webui/api/appcontroller.cpp | 12 ++++++++---- src/webui/www/private/views/preferences.html | 16 ++-------------- 9 files changed, 39 insertions(+), 54 deletions(-) diff --git a/src/app/upgrade.cpp b/src/app/upgrade.cpp index c2673d45c..592a7fafb 100644 --- a/src/app/upgrade.cpp +++ b/src/app/upgrade.cpp @@ -102,12 +102,28 @@ namespace settingsStorage->storeValue(newKey, Utils::String::fromEnum(torrentContentLayout)); settingsStorage->removeValue(oldKey); } + + void upgradeListenPortSettings() + { + const auto oldKey = QString::fromLatin1("BitTorrent/Session/UseRandomPort"); + const auto newKey = QString::fromLatin1("Preferences/Connection/PortRangeMin"); + auto *settingsStorage = SettingsStorage::instance(); + + if (settingsStorage->hasKey(oldKey)) + { + if (settingsStorage->loadValue(oldKey)) + settingsStorage->storeValue(newKey, 0); + + settingsStorage->removeValue(oldKey); + } + } } bool upgrade(const bool /*ask*/) { exportWebUIHttpsFiles(); upgradeTorrentContentLayout(); + upgradeListenPortSettings(); return true; } diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 419fd7122..e89fb4089 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -407,7 +407,6 @@ Session::Session(QObject *parent) , m_isBandwidthSchedulerEnabled(BITTORRENT_SESSION_KEY("BandwidthSchedulerEnabled"), false) , m_saveResumeDataInterval(BITTORRENT_SESSION_KEY("SaveResumeDataInterval"), 60) , m_port(BITTORRENT_SESSION_KEY("Port"), -1) - , m_useAnyAvailablePort(BITTORRENT_SESSION_KEY("UseRandomPort"), false) , m_networkInterface(BITTORRENT_SESSION_KEY("Interface")) , m_networkInterfaceName(BITTORRENT_SESSION_KEY("InterfaceName")) , m_networkInterfaceAddress(BITTORRENT_SESSION_KEY("InterfaceAddress")) @@ -1421,13 +1420,12 @@ void Session::configureNetworkInterfaces(lt::settings_pack &settingsPack) if (m_listenInterfaceConfigured) return; - const int port = useAnyAvailablePort() ? 0 : this->port(); - if (port > 0) // user specified port + if (port() > 0) // user has specified port number settingsPack.set_int(lt::settings_pack::max_retry_port_bind, 0); QStringList endpoints; QStringList outgoingInterfaces; - const QString portString = ':' + QString::number(port); + const QString portString = ':' + QString::number(port()); for (const QString &ip : asConst(getListeningIPs())) { @@ -2751,16 +2749,6 @@ void Session::setPort(const int port) } } -bool Session::useAnyAvailablePort() const -{ - return m_useAnyAvailablePort; -} - -void Session::setUseAnyAvailablePort(const bool value) -{ - m_useAnyAvailablePort = value; -} - QString Session::networkInterface() const { return m_networkInterface; diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index 5e507fe9a..e5f422374 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -306,8 +306,6 @@ namespace BitTorrent void setSaveResumeDataInterval(int value); int port() const; void setPort(int port); - bool useAnyAvailablePort() const; - void setUseAnyAvailablePort(bool value); QString networkInterface() const; void setNetworkInterface(const QString &iface); QString networkInterfaceName() const; @@ -722,7 +720,6 @@ namespace BitTorrent CachedSettingValue m_isBandwidthSchedulerEnabled; CachedSettingValue m_saveResumeDataInterval; CachedSettingValue m_port; - CachedSettingValue m_useAnyAvailablePort; CachedSettingValue m_networkInterface; CachedSettingValue m_networkInterfaceName; CachedSettingValue m_networkInterfaceAddress; diff --git a/src/base/settingsstorage.cpp b/src/base/settingsstorage.cpp index 73acdb98a..c5a9d08b6 100644 --- a/src/base/settingsstorage.cpp +++ b/src/base/settingsstorage.cpp @@ -234,6 +234,13 @@ void SettingsStorage::removeValue(const QString &key) } } +bool SettingsStorage::hasKey(const QString &key) const +{ + const QString realKey = mapKey(key); + const QReadLocker locker {&m_lock}; + return m_data.contains(realKey); +} + QVariantHash TransactionalSettings::read() const { QVariantHash res; diff --git a/src/base/settingsstorage.h b/src/base/settingsstorage.h index 52f6188f1..3ad9db493 100644 --- a/src/base/settingsstorage.h +++ b/src/base/settingsstorage.h @@ -79,6 +79,7 @@ public: } void removeValue(const QString &key); + bool hasKey(const QString &key) const; public slots: bool save(); diff --git a/src/gui/optionsdialog.cpp b/src/gui/optionsdialog.cpp index 0035e5c9a..80b94d80c 100644 --- a/src/gui/optionsdialog.cpp +++ b/src/gui/optionsdialog.cpp @@ -300,7 +300,6 @@ OptionsDialog::OptionsDialog(QWidget *parent) void (QSpinBox::*qSpinBoxValueChanged)(int) = &QSpinBox::valueChanged; connect(m_ui->comboProxyType, qComboBoxCurrentIndexChanged, this, &ThisType::enableProxy); - connect(m_ui->useAnyPort, &QAbstractButton::toggled, m_ui->spinPort, &ThisType::setDisabled); // Apply button is activated when a value is changed // Behavior tab @@ -408,7 +407,6 @@ OptionsDialog::OptionsDialog(QWidget *parent) // Connection tab connect(m_ui->comboProtocol, qComboBoxCurrentIndexChanged, this, &ThisType::enableApplyButton); connect(m_ui->spinPort, qSpinBoxValueChanged, this, &ThisType::enableApplyButton); - connect(m_ui->useAnyPort, &QAbstractButton::toggled, this, &ThisType::enableApplyButton); connect(m_ui->checkUPnP, &QAbstractButton::toggled, this, &ThisType::enableApplyButton); connect(m_ui->spinUploadLimit, qSpinBoxValueChanged, this, &ThisType::enableApplyButton); connect(m_ui->spinDownloadLimit, qSpinBoxValueChanged, this, &ThisType::enableApplyButton); @@ -770,7 +768,6 @@ void OptionsDialog::saveOptions() // Connection preferences session->setBTProtocol(static_cast(m_ui->comboProtocol->currentIndex())); session->setPort(getPort()); - session->setUseAnyAvailablePort(m_ui->useAnyPort->isChecked()); Net::PortForwarder::instance()->setEnabled(isUPnPEnabled()); session->setGlobalDownloadSpeedLimit(m_ui->spinDownloadLimit->value() * 1024); session->setGlobalUploadSpeedLimit(m_ui->spinUploadLimit->value() * 1024); @@ -1067,10 +1064,8 @@ void OptionsDialog::loadOptions() // Connection preferences m_ui->comboProtocol->setCurrentIndex(static_cast(session->btProtocol())); - m_ui->checkUPnP->setChecked(Net::PortForwarder::instance()->isEnabled()); - m_ui->useAnyPort->setChecked(session->useAnyAvailablePort()); m_ui->spinPort->setValue(session->port()); - m_ui->spinPort->setDisabled(m_ui->useAnyPort->isChecked()); + m_ui->checkUPnP->setChecked(Net::PortForwarder::instance()->isEnabled()); intValue = session->maxConnections(); if (intValue > 0) diff --git a/src/gui/optionsdialog.ui b/src/gui/optionsdialog.ui index d9fb0c094..4481efa11 100644 --- a/src/gui/optionsdialog.ui +++ b/src/gui/optionsdialog.ui @@ -1457,15 +1457,12 @@ Manual: Various torrent properties (e.g. save path) must be assigned manually - - 1 + + Any 65535 - - 8999 - @@ -1490,13 +1487,6 @@ Manual: Various torrent properties (e.g. save path) must be assigned manually - - - - Use any available port - - - @@ -3469,7 +3459,6 @@ Use ';' to split multiple entries. Can use wildcard '*'. customThemeFilePath checkStartPaused spinPort - useAnyPort checkUPnP textWebUiUsername checkWebUi diff --git a/src/webui/api/appcontroller.cpp b/src/webui/api/appcontroller.cpp index 7745509e7..201892d7e 100644 --- a/src/webui/api/appcontroller.cpp +++ b/src/webui/api/appcontroller.cpp @@ -153,8 +153,8 @@ void AppController::preferencesAction() // Connection // Listening Port data["listen_port"] = session->port(); + data["random_port"] = (session->port() == 0); // deprecated data["upnp"] = Net::PortForwarder::instance()->isEnabled(); - data["random_port"] = session->useAnyAvailablePort(); // Connections Limits data["max_connec"] = session->maxConnections(); data["max_connec_per_torrent"] = session->maxConnectionsPerTorrent(); @@ -482,12 +482,16 @@ void AppController::setPreferencesAction() // Connection // Listening Port - if (hasKey("listen_port")) + if (hasKey("random_port") && it.value().toBool()) // deprecated + { + session->setPort(0); + } + else if (hasKey("listen_port")) + { session->setPort(it.value().toInt()); + } if (hasKey("upnp")) Net::PortForwarder::instance()->setEnabled(it.value().toBool()); - if (hasKey("random_port")) - session->setUseAnyAvailablePort(it.value().toBool()); // Connections Limits if (hasKey("max_connec")) session->setMaxConnections(it.value().toInt()); diff --git a/src/webui/www/private/views/preferences.html b/src/webui/www/private/views/preferences.html index 5b9ca18cb..ef1267de0 100644 --- a/src/webui/www/private/views/preferences.html +++ b/src/webui/www/private/views/preferences.html @@ -234,10 +234,6 @@ -
- - -
@@ -1275,7 +1271,6 @@ updateMailAuthSettings: updateMailAuthSettings, updateAutoRun: updateAutoRun, generateRandomPort: generateRandomPort, - updatePortValueEnabled: updatePortValueEnabled, updateMaxConnecEnabled: updateMaxConnecEnabled, updateMaxConnecPerTorrentEnabled: updateMaxConnecPerTorrentEnabled, updateMaxUploadsEnabled: updateMaxUploadsEnabled, @@ -1404,10 +1399,6 @@ }; // Connection tab - const updatePortValueEnabled = function() { - const checked = $('useAnyPortCheckbox').getProperty('checked'); - $('port_value').setProperty('disabled', checked); - }; const updateMaxConnecEnabled = function() { const isMaxConnecEnabled = $('max_connec_checkbox').getProperty('checked'); @@ -1708,8 +1699,6 @@ // Listening Port $('port_value').setProperty('value', pref.listen_port.toInt()); $('upnp_checkbox').setProperty('checked', pref.upnp); - $('useAnyPortCheckbox').setProperty('checked', pref.random_port); - updatePortValueEnabled(); // Connections Limits const max_connec = pref.max_connec.toInt(); @@ -2018,13 +2007,12 @@ // Connection tab // Listening Port const listen_port = $('port_value').getProperty('value').toInt(); - if (isNaN(listen_port) || listen_port < 1 || listen_port > 65535) { - alert("QBT_TR(The port used for incoming connections must be between 1 and 65535.)QBT_TR[CONTEXT=HttpServer]"); + if (isNaN(listen_port) || (listen_port < 0) || (listen_port > 65535)) { + alert("QBT_TR(The port used for incoming connections must be between 0 and 65535.)QBT_TR[CONTEXT=HttpServer]"); return; } settings.set('listen_port', listen_port); settings.set('upnp', $('upnp_checkbox').getProperty('checked')); - settings.set('random_port', $('useAnyPortCheckbox').getProperty('checked')); // Connections Limits let max_connec = -1;