WebAPI: improve setting preferences behavior

Now the behavior is more intuitive for a few options when the client send in partial settings.
This change is backward compatible.

For example, now it is possible to have only one of `max_ratio_enabled` or `max_ratio` instead
of requiring both.

PR #22460.
This commit is contained in:
Chocobo1 2025-03-24 21:04:35 +08:00 committed by GitHub
commit 8c8a0ac54c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -501,7 +501,7 @@ void AppController::setPreferencesAction()
QVariantHash::ConstIterator it; QVariantHash::ConstIterator it;
const auto hasKey = [&it, &m](const QString &key) -> bool const auto hasKey = [&it, &m](const QString &key) -> bool
{ {
it = m.find(key); it = m.constFind(key);
return (it != m.constEnd()); return (it != m.constEnd());
}; };
@ -786,10 +786,12 @@ void AppController::setPreferencesAction()
// Scheduling // Scheduling
if (hasKey(u"scheduler_enabled"_s)) if (hasKey(u"scheduler_enabled"_s))
session->setBandwidthSchedulerEnabled(it.value().toBool()); session->setBandwidthSchedulerEnabled(it.value().toBool());
if (m.contains(u"schedule_from_hour"_s) && m.contains(u"schedule_from_min"_s)) if (const auto hourIter = m.constFind(u"schedule_from_hour"_s), minIter = m.constFind(u"schedule_from_min"_s)
pref->setSchedulerStartTime(QTime(m[u"schedule_from_hour"_s].toInt(), m[u"schedule_from_min"_s].toInt())); ; (hourIter != m.constEnd()) && (minIter != m.constEnd()))
if (m.contains(u"schedule_to_hour"_s) && m.contains(u"schedule_to_min"_s)) pref->setSchedulerStartTime({hourIter.value().toInt(), minIter.value().toInt()});
pref->setSchedulerEndTime(QTime(m[u"schedule_to_hour"_s].toInt(), m[u"schedule_to_min"_s].toInt())); if (const auto hourIter = m.constFind(u"schedule_to_hour"_s), minIter = m.constFind(u"schedule_to_min"_s)
; (hourIter != m.constEnd()) && (minIter != m.constEnd()))
pref->setSchedulerEndTime({hourIter.value().toInt(), minIter.value().toInt()});
if (hasKey(u"scheduler_days"_s)) if (hasKey(u"scheduler_days"_s))
pref->setSchedulerDays(static_cast<Scheduler::Days>(it.value().toInt())); pref->setSchedulerDays(static_cast<Scheduler::Days>(it.value().toInt()));
@ -826,25 +828,18 @@ void AppController::setPreferencesAction()
if (hasKey(u"slow_torrent_inactive_timer"_s)) if (hasKey(u"slow_torrent_inactive_timer"_s))
session->setSlowTorrentsInactivityTimer(it.value().toInt()); session->setSlowTorrentsInactivityTimer(it.value().toInt());
// Share Ratio Limiting // Share Ratio Limiting
if (hasKey(u"max_ratio_enabled"_s)) if (hasKey(u"max_ratio_enabled"_s) && !it.value().toBool())
{ session->setGlobalMaxRatio(-1);
if (it.value().toBool()) else if (hasKey(u"max_ratio"_s))
session->setGlobalMaxRatio(m[u"max_ratio"_s].toReal()); session->setGlobalMaxRatio(it.value().toReal());
else if (hasKey(u"max_seeding_time_enabled"_s) && !it.value().toBool())
session->setGlobalMaxRatio(-1); session->setGlobalMaxSeedingMinutes(-1);
} else if (hasKey(u"max_seeding_time"_s))
if (hasKey(u"max_seeding_time_enabled"_s)) session->setGlobalMaxSeedingMinutes(it.value().toInt());
{ if (hasKey(u"max_inactive_seeding_time_enabled"_s) && !it.value().toBool())
if (it.value().toBool()) session->setGlobalMaxInactiveSeedingMinutes(-1);
session->setGlobalMaxSeedingMinutes(m[u"max_seeding_time"_s].toInt()); else if (hasKey(u"max_inactive_seeding_time"_s))
else session->setGlobalMaxInactiveSeedingMinutes(it.value().toInt());
session->setGlobalMaxSeedingMinutes(-1);
}
if (hasKey(u"max_inactive_seeding_time_enabled"_s))
{
session->setGlobalMaxInactiveSeedingMinutes(it.value().toBool()
? m[u"max_inactive_seeding_time"_s].toInt() : -1);
}
if (hasKey(u"max_ratio_act"_s)) if (hasKey(u"max_ratio_act"_s))
{ {
switch (it.value().toInt()) switch (it.value().toInt())
@ -982,7 +977,7 @@ void AppController::setPreferencesAction()
{ {
return (!iface.addressEntries().isEmpty()) && (iface.name() == ifaceValue); return (!iface.addressEntries().isEmpty()) && (iface.name() == ifaceValue);
}); });
const QString ifaceName = (ifacesIter != ifaces.cend()) ? ifacesIter->humanReadableName() : QString {}; const QString ifaceName = (ifacesIter != ifaces.cend()) ? ifacesIter->humanReadableName() : QString();
session->setNetworkInterface(ifaceValue); session->setNetworkInterface(ifaceValue);
if (!ifaceName.isEmpty() || ifaceValue.isEmpty()) if (!ifaceName.isEmpty() || ifaceValue.isEmpty())
@ -992,7 +987,7 @@ void AppController::setPreferencesAction()
if (hasKey(u"current_interface_address"_s)) if (hasKey(u"current_interface_address"_s))
{ {
const QHostAddress ifaceAddress {it.value().toString().trimmed()}; const QHostAddress ifaceAddress {it.value().toString().trimmed()};
session->setNetworkInterfaceAddress(ifaceAddress.isNull() ? QString {} : ifaceAddress.toString()); session->setNetworkInterfaceAddress(ifaceAddress.isNull() ? QString() : ifaceAddress.toString());
} }
// Save resume data interval // Save resume data interval
if (hasKey(u"save_resume_data_interval"_s)) if (hasKey(u"save_resume_data_interval"_s))
@ -1148,7 +1143,7 @@ void AppController::setPreferencesAction()
if (hasKey(u"announce_ip"_s)) if (hasKey(u"announce_ip"_s))
{ {
const QHostAddress announceAddr {it.value().toString().trimmed()}; const QHostAddress announceAddr {it.value().toString().trimmed()};
session->setAnnounceIP(announceAddr.isNull() ? QString {} : announceAddr.toString()); session->setAnnounceIP(announceAddr.isNull() ? QString() : announceAddr.toString());
} }
if (hasKey(u"announce_port"_s)) if (hasKey(u"announce_port"_s))
session->setAnnouncePort(it.value().toInt()); session->setAnnouncePort(it.value().toInt());