Use spinbox special value to represent "Use any available port"

WebAPI functionality is preserved (deprecated) for now and should be
removed in the future.
This commit is contained in:
Chocobo1 2021-07-28 12:24:03 +08:00
parent 09e558ae0b
commit 49aab492e0
No known key found for this signature in database
GPG key ID: 210D9C873253A68C
9 changed files with 39 additions and 54 deletions

View file

@ -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<bool>(oldKey))
settingsStorage->storeValue(newKey, 0);
settingsStorage->removeValue(oldKey);
}
}
}
bool upgrade(const bool /*ask*/)
{
exportWebUIHttpsFiles();
upgradeTorrentContentLayout();
upgradeListenPortSettings();
return true;
}

View file

@ -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;

View file

@ -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<bool> m_isBandwidthSchedulerEnabled;
CachedSettingValue<int> m_saveResumeDataInterval;
CachedSettingValue<int> m_port;
CachedSettingValue<bool> m_useAnyAvailablePort;
CachedSettingValue<QString> m_networkInterface;
CachedSettingValue<QString> m_networkInterfaceName;
CachedSettingValue<QString> m_networkInterfaceAddress;

View file

@ -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;

View file

@ -79,6 +79,7 @@ public:
}
void removeValue(const QString &key);
bool hasKey(const QString &key) const;
public slots:
bool save();

View file

@ -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<BitTorrent::BTProtocol>(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<int>(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)

View file

@ -1457,15 +1457,12 @@ Manual: Various torrent properties (e.g. save path) must be assigned manually</s
</item>
<item>
<widget class="QSpinBox" name="spinPort">
<property name="minimum">
<number>1</number>
<property name="specialValueText">
<string>Any</string>
</property>
<property name="maximum">
<number>65535</number>
</property>
<property name="value">
<number>8999</number>
</property>
</widget>
</item>
<item>
@ -1490,13 +1487,6 @@ Manual: Various torrent properties (e.g. save path) must be assigned manually</s
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="useAnyPort">
<property name="text">
<string>Use any available port</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkUPnP">
<property name="text">
@ -3469,7 +3459,6 @@ Use ';' to split multiple entries. Can use wildcard '*'.</string>
<tabstop>customThemeFilePath</tabstop>
<tabstop>checkStartPaused</tabstop>
<tabstop>spinPort</tabstop>
<tabstop>useAnyPort</tabstop>
<tabstop>checkUPnP</tabstop>
<tabstop>textWebUiUsername</tabstop>
<tabstop>checkWebUi</tabstop>

View file

@ -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());

View file

@ -234,10 +234,6 @@
<input type="text" id="port_value" style="width: 4em;" />
<button style="margin-left: 1em;" onclick="qBittorrent.Preferences.generateRandomPort();">QBT_TR(Random)QBT_TR[CONTEXT=OptionsDialog]</button>
</div>
<div class="formRow">
<input type="checkbox" id="useAnyPortCheckbox" onclick="qBittorrent.Preferences.updatePortValueEnabled();" />
<label for="useAnyPortCheckbox">QBT_TR(Use any available port)QBT_TR[CONTEXT=OptionsDialog]</label>
</div>
<div class="formRow">
<input type="checkbox" id="upnp_checkbox" />
<label for="upnp_checkbox">QBT_TR(Use UPnP / NAT-PMP port forwarding from my router)QBT_TR[CONTEXT=OptionsDialog]</label>
@ -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;