Browse Source

Store enum type in settings directly

Affected settings will be migrated to new keys so nothing should break.

PR #15800.
adaptive-webui-19844
Chocobo1 3 years ago committed by GitHub
parent
commit
e3fe66d3ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 118
      src/app/upgrade.cpp
  2. 30
      src/base/bittorrent/bandwidthscheduler.cpp
  3. 18
      src/base/net/dnsupdater.cpp
  4. 2
      src/base/net/dnsupdater.h
  5. 18
      src/base/preferences.cpp
  6. 63
      src/base/preferences.h
  7. 12
      src/gui/mainwindow.cpp
  8. 9
      src/gui/optionsdialog.cpp
  9. 8
      src/webui/api/appcontroller.cpp
  10. 3
      src/webui/api/synccontroller.cpp

118
src/app/upgrade.cpp

@ -33,6 +33,7 @@
#include "base/bittorrent/torrentcontentlayout.h" #include "base/bittorrent/torrentcontentlayout.h"
#include "base/logger.h" #include "base/logger.h"
#include "base/preferences.h"
#include "base/profile.h" #include "base/profile.h"
#include "base/settingsstorage.h" #include "base/settingsstorage.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"
@ -110,6 +111,120 @@ namespace
settingsStorage->removeValue(oldKey); settingsStorage->removeValue(oldKey);
} }
} }
void upgradeSchedulerDaysSettings()
{
auto *settingsStorage = SettingsStorage::instance();
const auto key = QString::fromLatin1("Preferences/Scheduler/days");
const auto value = settingsStorage->loadValue<QString>(key);
bool ok = false;
const auto number = value.toInt(&ok);
if (ok)
{
switch (number)
{
case 0:
settingsStorage->storeValue(key, Scheduler::Days::EveryDay);
break;
case 1:
settingsStorage->storeValue(key, Scheduler::Days::Weekday);
break;
case 2:
settingsStorage->storeValue(key, Scheduler::Days::Weekend);
break;
case 3:
settingsStorage->storeValue(key, Scheduler::Days::Monday);
break;
case 4:
settingsStorage->storeValue(key, Scheduler::Days::Tuesday);
break;
case 5:
settingsStorage->storeValue(key, Scheduler::Days::Wednesday);
break;
case 6:
settingsStorage->storeValue(key, Scheduler::Days::Thursday);
break;
case 7:
settingsStorage->storeValue(key, Scheduler::Days::Friday);
break;
case 8:
settingsStorage->storeValue(key, Scheduler::Days::Saturday);
break;
case 9:
settingsStorage->storeValue(key, Scheduler::Days::Sunday);
break;
default:
LogMsg(QObject::tr("Invalid value found in configuration file, reverting it to default. Key: \"%1\". Invalid value: \"%2\".")
.arg(key, QString::number(number)), Log::WARNING);
settingsStorage->removeValue(key);
break;
}
}
}
void upgradeDNSServiceSettings()
{
auto *settingsStorage = SettingsStorage::instance();
const auto key = QString::fromLatin1("Preferences/DynDNS/Service");
const auto value = settingsStorage->loadValue<QString>(key);
bool ok = false;
const auto number = value.toInt(&ok);
if (ok)
{
switch (number)
{
case -1:
settingsStorage->storeValue(key, DNS::Service::None);
break;
case 0:
settingsStorage->storeValue(key, DNS::Service::DynDNS);
break;
case 1:
settingsStorage->storeValue(key, DNS::Service::NoIP);
break;
default:
LogMsg(QObject::tr("Invalid value found in configuration file, reverting it to default. Key: \"%1\". Invalid value: \"%2\".")
.arg(key, QString::number(number)), Log::WARNING);
settingsStorage->removeValue(key);
break;
}
}
}
void upgradeTrayIconStyleSettings()
{
auto *settingsStorage = SettingsStorage::instance();
const auto key = QString::fromLatin1("Preferences/Advanced/TrayIconStyle");
const auto value = settingsStorage->loadValue<QString>(key);
bool ok = false;
const auto number = value.toInt(&ok);
if (ok)
{
switch (number)
{
case 0:
settingsStorage->storeValue(key, TrayIcon::Style::Normal);
break;
case 1:
settingsStorage->storeValue(key, TrayIcon::Style::MonoDark);
break;
case 2:
settingsStorage->storeValue(key, TrayIcon::Style::MonoLight);
break;
default:
LogMsg(QObject::tr("Invalid value found in configuration file, reverting it to default. Key: \"%1\". Invalid value: \"%2\".")
.arg(key, QString::number(number)), Log::WARNING);
settingsStorage->removeValue(key);
break;
}
}
}
} }
bool upgrade(const bool /*ask*/) bool upgrade(const bool /*ask*/)
@ -117,6 +232,9 @@ bool upgrade(const bool /*ask*/)
exportWebUIHttpsFiles(); exportWebUIHttpsFiles();
upgradeTorrentContentLayout(); upgradeTorrentContentLayout();
upgradeListenPortSettings(); upgradeListenPortSettings();
upgradeSchedulerDaysSettings();
upgradeDNSServiceSettings();
upgradeTrayIconStyleSettings();
return true; return true;
} }

30
src/base/bittorrent/bandwidthscheduler.cpp

@ -61,7 +61,7 @@ bool BandwidthScheduler::isTimeForAlternative() const
QTime start = pref->getSchedulerStartTime(); QTime start = pref->getSchedulerStartTime();
QTime end = pref->getSchedulerEndTime(); QTime end = pref->getSchedulerEndTime();
const QTime now = QTime::currentTime(); const QTime now = QTime::currentTime();
const int schedulerDays = pref->getSchedulerDays(); const Scheduler::Days schedulerDays = pref->getSchedulerDays();
const int day = QDate::currentDate().dayOfWeek(); const int day = QDate::currentDate().dayOfWeek();
bool alternative = false; bool alternative = false;
@ -75,20 +75,34 @@ bool BandwidthScheduler::isTimeForAlternative() const
{ {
switch (schedulerDays) switch (schedulerDays)
{ {
case EVERY_DAY: case Scheduler::Days::EveryDay:
alternative = !alternative; alternative = !alternative;
break; break;
case WEEK_ENDS: case Scheduler::Days::Monday:
if ((day == 6) || (day == 7)) case Scheduler::Days::Tuesday:
case Scheduler::Days::Wednesday:
case Scheduler::Days::Thursday:
case Scheduler::Days::Friday:
case Scheduler::Days::Saturday:
case Scheduler::Days::Sunday:
{
const int offset = static_cast<int>(Scheduler::Days::Monday) - 1;
const int dayOfWeek = static_cast<int>(schedulerDays) - offset;
if (day == dayOfWeek)
alternative = !alternative;
}
break;
case Scheduler::Days::Weekday:
if ((day >= 1) && (day <= 5))
alternative = !alternative; alternative = !alternative;
break; break;
case WEEK_DAYS: case Scheduler::Days::Weekend:
if ((day != 6) && (day != 7)) if ((day == 6) || (day == 7))
alternative = !alternative; alternative = !alternative;
break; break;
default: default:
if (day == (schedulerDays - 2)) Q_ASSERT(false);
alternative = !alternative; break;
} }
} }

18
src/base/net/dnsupdater.cpp

@ -41,7 +41,7 @@ using namespace Net;
DNSUpdater::DNSUpdater(QObject *parent) DNSUpdater::DNSUpdater(QObject *parent)
: QObject(parent) : QObject(parent)
, m_state(OK) , m_state(OK)
, m_service(DNS::NONE) , m_service(DNS::Service::None)
{ {
updateCredentials(); updateCredentials();
@ -143,15 +143,16 @@ QString DNSUpdater::getUpdateUrl() const
// Service specific // Service specific
switch (m_service) switch (m_service)
{ {
case DNS::DYNDNS: case DNS::Service::DynDNS:
url.setHost("members.dyndns.org"); url.setHost("members.dyndns.org");
break; break;
case DNS::NOIP: case DNS::Service::NoIP:
url.setHost("dynupdate.no-ip.com"); url.setHost("dynupdate.no-ip.com");
break; break;
default: default:
qWarning() << "Unrecognized Dynamic DNS service!"; qWarning() << "Unrecognized Dynamic DNS service!";
Q_ASSERT(0); Q_ASSERT(false);
break;
} }
url.setPath("/nic/update"); url.setPath("/nic/update");
@ -295,16 +296,17 @@ void DNSUpdater::updateCredentials()
} }
} }
QUrl DNSUpdater::getRegistrationUrl(const int service) QUrl DNSUpdater::getRegistrationUrl(const DNS::Service service)
{ {
switch (service) switch (service)
{ {
case DNS::DYNDNS: case DNS::Service::DynDNS:
return {"https://account.dyn.com/entrance/"}; return {"https://account.dyn.com/entrance/"};
case DNS::NOIP: case DNS::Service::NoIP:
return {"https://www.noip.com/remote-access"}; return {"https://www.noip.com/remote-access"};
default: default:
Q_ASSERT(0); Q_ASSERT(false);
break;
} }
return {}; return {};
} }

2
src/base/net/dnsupdater.h

@ -48,7 +48,7 @@ namespace Net
explicit DNSUpdater(QObject *parent = nullptr); explicit DNSUpdater(QObject *parent = nullptr);
~DNSUpdater(); ~DNSUpdater();
static QUrl getRegistrationUrl(int service); static QUrl getRegistrationUrl(DNS::Service service);
public slots: public slots:
void updateCredentials(); void updateCredentials();

18
src/base/preferences.cpp

@ -493,14 +493,14 @@ void Preferences::setSchedulerEndTime(const QTime &time)
setValue("Preferences/Scheduler/end_time", time); setValue("Preferences/Scheduler/end_time", time);
} }
SchedulerDays Preferences::getSchedulerDays() const Scheduler::Days Preferences::getSchedulerDays() const
{ {
return static_cast<SchedulerDays>(value("Preferences/Scheduler/days", EVERY_DAY).toInt()); return SettingsStorage::instance()->loadValue("Preferences/Scheduler/days", Scheduler::Days::EveryDay);
} }
void Preferences::setSchedulerDays(const SchedulerDays days) void Preferences::setSchedulerDays(const Scheduler::Days days)
{ {
setValue("Preferences/Scheduler/days", static_cast<int>(days)); SettingsStorage::instance()->storeValue("Preferences/Scheduler/days", days);
} }
// Search // Search
@ -816,12 +816,12 @@ void Preferences::setDynDNSEnabled(const bool enabled)
DNS::Service Preferences::getDynDNSService() const DNS::Service Preferences::getDynDNSService() const
{ {
return DNS::Service(value("Preferences/DynDNS/Service", DNS::DYNDNS).toInt()); return SettingsStorage::instance()->loadValue("Preferences/DynDNS/Service", DNS::Service::DynDNS);
} }
void Preferences::setDynDNSService(const int service) void Preferences::setDynDNSService(const DNS::Service service)
{ {
setValue("Preferences/DynDNS/Service", service); SettingsStorage::instance()->storeValue("Preferences/DynDNS/Service", service);
} }
QString Preferences::getDynDomainName() const QString Preferences::getDynDomainName() const
@ -1211,12 +1211,12 @@ void Preferences::setConfirmRemoveAllTags(const bool enabled)
#ifndef Q_OS_MACOS #ifndef Q_OS_MACOS
TrayIcon::Style Preferences::trayIconStyle() const TrayIcon::Style Preferences::trayIconStyle() const
{ {
return TrayIcon::Style(value("Preferences/Advanced/TrayIconStyle", TrayIcon::NORMAL).toInt()); return SettingsStorage::instance()->loadValue("Preferences/Advanced/TrayIconStyle", TrayIcon::Style::Normal);
} }
void Preferences::setTrayIconStyle(const TrayIcon::Style style) void Preferences::setTrayIconStyle(const TrayIcon::Style style)
{ {
setValue("Preferences/Advanced/TrayIconStyle", style); SettingsStorage::instance()->storeValue("Preferences/Advanced/TrayIconStyle", style);
} }
#endif #endif

63
src/base/preferences.h

@ -29,6 +29,7 @@
#pragma once #pragma once
#include <QObject>
#include <QtContainerFwd> #include <QtContainerFwd>
#include <QtGlobal> #include <QtGlobal>
#include <QVariant> #include <QVariant>
@ -40,38 +41,50 @@ class QNetworkCookie;
class QSize; class QSize;
class QTime; class QTime;
enum SchedulerDays namespace Scheduler
{ {
EVERY_DAY, Q_NAMESPACE
WEEK_DAYS,
WEEK_ENDS,
MON,
TUE,
WED,
THU,
FRI,
SAT,
SUN
};
namespace TrayIcon enum class Days : int
{
enum Style
{ {
NORMAL = 0, EveryDay = 0,
MONO_DARK, Weekday = 1,
MONO_LIGHT Weekend = 2,
Monday = 3,
Tuesday = 4,
Wednesday = 5,
Thursday = 6,
Friday = 7,
Saturday = 8,
Sunday = 9
}; };
Q_ENUM_NS(Days)
} }
namespace DNS namespace DNS
{ {
enum Service Q_NAMESPACE
enum class Service : int
{
DynDNS = 0,
NoIP = 1,
None = -1
};
Q_ENUM_NS(Service)
}
namespace TrayIcon
{
Q_NAMESPACE
enum class Style : int
{ {
DYNDNS, Normal = 0,
NOIP, MonoDark = 1,
NONE = -1 MonoLight = 2
}; };
Q_ENUM_NS(Style)
} }
class Preferences : public QObject class Preferences : public QObject
@ -161,8 +174,8 @@ public:
void setSchedulerStartTime(const QTime &time); void setSchedulerStartTime(const QTime &time);
QTime getSchedulerEndTime() const; QTime getSchedulerEndTime() const;
void setSchedulerEndTime(const QTime &time); void setSchedulerEndTime(const QTime &time);
SchedulerDays getSchedulerDays() const; Scheduler::Days getSchedulerDays() const;
void setSchedulerDays(SchedulerDays days); void setSchedulerDays(Scheduler::Days days);
// Search // Search
bool isSearchEnabled() const; bool isSearchEnabled() const;
@ -236,7 +249,7 @@ public:
bool isDynDNSEnabled() const; bool isDynDNSEnabled() const;
void setDynDNSEnabled(bool enabled); void setDynDNSEnabled(bool enabled);
DNS::Service getDynDNSService() const; DNS::Service getDynDNSService() const;
void setDynDNSService(int service); void setDynDNSService(DNS::Service service);
QString getDynDomainName() const; QString getDynDomainName() const;
void setDynDomainName(const QString &name); void setDynDomainName(const QString &name);
QString getDynDNSUsername() const; QString getDynDNSUsername() const;

12
src/gui/mainwindow.cpp

@ -2080,11 +2080,11 @@ QIcon MainWindow::getSystrayIcon() const
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) #if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS))
switch (style) switch (style)
{ {
case TrayIcon::NORMAL: case TrayIcon::Style::Normal:
return QIcon::fromTheme(QLatin1String("qbittorrent-tray")); return QIcon::fromTheme(QLatin1String("qbittorrent-tray"));
case TrayIcon::MONO_DARK: case TrayIcon::Style::MonoDark:
return QIcon::fromTheme(QLatin1String("qbittorrent-tray-dark")); return QIcon::fromTheme(QLatin1String("qbittorrent-tray-dark"));
case TrayIcon::MONO_LIGHT: case TrayIcon::Style::MonoLight:
return QIcon::fromTheme(QLatin1String("qbittorrent-tray-light")); return QIcon::fromTheme(QLatin1String("qbittorrent-tray-light"));
default: default:
break; break;
@ -2092,11 +2092,11 @@ QIcon MainWindow::getSystrayIcon() const
#else #else
switch (style) switch (style)
{ {
case TrayIcon::NORMAL: case TrayIcon::Style::Normal:
return UIThemeManager::instance()->getIcon(QLatin1String("qbittorrent-tray")); return UIThemeManager::instance()->getIcon(QLatin1String("qbittorrent-tray"));
case TrayIcon::MONO_DARK: case TrayIcon::Style::MonoDark:
return UIThemeManager::instance()->getIcon(QLatin1String("qbittorrent-tray-dark")); return UIThemeManager::instance()->getIcon(QLatin1String("qbittorrent-tray-dark"));
case TrayIcon::MONO_LIGHT: case TrayIcon::Style::MonoLight:
return UIThemeManager::instance()->getIcon(QLatin1String("qbittorrent-tray-light")); return UIThemeManager::instance()->getIcon(QLatin1String("qbittorrent-tray-light"));
default: default:
break; break;

9
src/gui/optionsdialog.cpp

@ -783,7 +783,7 @@ void OptionsDialog::saveOptions()
session->setIgnoreLimitsOnLAN(!m_ui->checkLimitLocalPeerRate->isChecked()); session->setIgnoreLimitsOnLAN(!m_ui->checkLimitLocalPeerRate->isChecked());
pref->setSchedulerStartTime(m_ui->timeEditScheduleFrom->time()); pref->setSchedulerStartTime(m_ui->timeEditScheduleFrom->time());
pref->setSchedulerEndTime(m_ui->timeEditScheduleTo->time()); pref->setSchedulerEndTime(m_ui->timeEditScheduleTo->time());
pref->setSchedulerDays(static_cast<SchedulerDays>(m_ui->comboBoxScheduleDays->currentIndex())); pref->setSchedulerDays(static_cast<Scheduler::Days>(m_ui->comboBoxScheduleDays->currentIndex()));
session->setBandwidthSchedulerEnabled(m_ui->groupBoxSchedule->isChecked()); session->setBandwidthSchedulerEnabled(m_ui->groupBoxSchedule->isChecked());
auto proxyConfigManager = Net::ProxyConfigurationManager::instance(); auto proxyConfigManager = Net::ProxyConfigurationManager::instance();
@ -867,7 +867,7 @@ void OptionsDialog::saveOptions()
pref->setWebUIHostHeaderValidationEnabled(m_ui->groupHostHeaderValidation->isChecked()); pref->setWebUIHostHeaderValidationEnabled(m_ui->groupHostHeaderValidation->isChecked());
// DynDNS // DynDNS
pref->setDynDNSEnabled(m_ui->checkDynDNS->isChecked()); pref->setDynDNSEnabled(m_ui->checkDynDNS->isChecked());
pref->setDynDNSService(m_ui->comboDNSService->currentIndex()); pref->setDynDNSService(static_cast<DNS::Service>(m_ui->comboDNSService->currentIndex()));
pref->setDynDomainName(m_ui->domainNameTxt->text()); pref->setDynDomainName(m_ui->domainNameTxt->text());
pref->setDynDNSUsername(m_ui->DNSUsernameTxt->text()); pref->setDynDNSUsername(m_ui->DNSUsernameTxt->text());
pref->setDynDNSPassword(m_ui->DNSPasswordTxt->text()); pref->setDynDNSPassword(m_ui->DNSPasswordTxt->text());
@ -941,7 +941,7 @@ void OptionsDialog::loadOptions()
{ {
m_ui->checkMinimizeToSysTray->setChecked(pref->minimizeToTray()); m_ui->checkMinimizeToSysTray->setChecked(pref->minimizeToTray());
m_ui->checkCloseToSystray->setChecked(pref->closeToTray()); m_ui->checkCloseToSystray->setChecked(pref->closeToTray());
m_ui->comboTrayIcon->setCurrentIndex(pref->trayIconStyle()); m_ui->comboTrayIcon->setCurrentIndex(static_cast<int>(pref->trayIconStyle()));
} }
#endif #endif
@ -1832,7 +1832,8 @@ void OptionsDialog::showConnectionTab()
void OptionsDialog::on_registerDNSBtn_clicked() void OptionsDialog::on_registerDNSBtn_clicked()
{ {
QDesktopServices::openUrl(Net::DNSUpdater::getRegistrationUrl(m_ui->comboDNSService->currentIndex())); const auto service = static_cast<DNS::Service>(m_ui->comboDNSService->currentIndex());
QDesktopServices::openUrl(Net::DNSUpdater::getRegistrationUrl(service));
} }
void OptionsDialog::on_IpFilterRefreshBtn_clicked() void OptionsDialog::on_IpFilterRefreshBtn_clicked()

8
src/webui/api/appcontroller.cpp

@ -198,7 +198,7 @@ void AppController::preferencesAction()
const QTime end_time = pref->getSchedulerEndTime(); const QTime end_time = pref->getSchedulerEndTime();
data["schedule_to_hour"] = end_time.hour(); data["schedule_to_hour"] = end_time.hour();
data["schedule_to_min"] = end_time.minute(); data["schedule_to_min"] = end_time.minute();
data["scheduler_days"] = pref->getSchedulerDays(); data["scheduler_days"] = static_cast<int>(pref->getSchedulerDays());
// Bittorrent // Bittorrent
// Privacy // Privacy
@ -264,7 +264,7 @@ void AppController::preferencesAction()
data["web_ui_reverse_proxies_list"] = pref->getWebUITrustedReverseProxiesList(); data["web_ui_reverse_proxies_list"] = pref->getWebUITrustedReverseProxiesList();
// Update my dynamic domain name // Update my dynamic domain name
data["dyndns_enabled"] = pref->isDynDNSEnabled(); data["dyndns_enabled"] = pref->isDynDNSEnabled();
data["dyndns_service"] = pref->getDynDNSService(); data["dyndns_service"] = static_cast<int>(pref->getDynDNSService());
data["dyndns_username"] = pref->getDynDNSUsername(); data["dyndns_username"] = pref->getDynDNSUsername();
data["dyndns_password"] = pref->getDynDNSPassword(); data["dyndns_password"] = pref->getDynDNSPassword();
data["dyndns_domain"] = pref->getDynDomainName(); data["dyndns_domain"] = pref->getDynDomainName();
@ -560,7 +560,7 @@ void AppController::setPreferencesAction()
if (m.contains("schedule_to_hour") && m.contains("schedule_to_min")) if (m.contains("schedule_to_hour") && m.contains("schedule_to_min"))
pref->setSchedulerEndTime(QTime(m["schedule_to_hour"].toInt(), m["schedule_to_min"].toInt())); pref->setSchedulerEndTime(QTime(m["schedule_to_hour"].toInt(), m["schedule_to_min"].toInt()));
if (hasKey("scheduler_days")) if (hasKey("scheduler_days"))
pref->setSchedulerDays(SchedulerDays(it.value().toInt())); pref->setSchedulerDays(static_cast<Scheduler::Days>(it.value().toInt()));
// Bittorrent // Bittorrent
// Privacy // Privacy
@ -698,7 +698,7 @@ void AppController::setPreferencesAction()
if (hasKey("dyndns_enabled")) if (hasKey("dyndns_enabled"))
pref->setDynDNSEnabled(it.value().toBool()); pref->setDynDNSEnabled(it.value().toBool());
if (hasKey("dyndns_service")) if (hasKey("dyndns_service"))
pref->setDynDNSService(it.value().toInt()); pref->setDynDNSService(static_cast<DNS::Service>(it.value().toInt()));
if (hasKey("dyndns_username")) if (hasKey("dyndns_username"))
pref->setDynDNSUsername(it.value().toString()); pref->setDynDNSUsername(it.value().toString());
if (hasKey("dyndns_password")) if (hasKey("dyndns_password"))

3
src/webui/api/synccontroller.cpp

@ -282,7 +282,8 @@ namespace
} }
break; break;
default: default:
Q_ASSERT(0); Q_ASSERT(false);
break;
} }
} }

Loading…
Cancel
Save