From e3fe66d3ec03f3e5fe894c698d36823024149c1b Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 6 Dec 2021 13:53:52 +0800 Subject: [PATCH] Store enum type in settings directly Affected settings will be migrated to new keys so nothing should break. PR #15800. --- src/app/upgrade.cpp | 118 +++++++++++++++++++++ src/base/bittorrent/bandwidthscheduler.cpp | 30 ++++-- src/base/net/dnsupdater.cpp | 18 ++-- src/base/net/dnsupdater.h | 2 +- src/base/preferences.cpp | 18 ++-- src/base/preferences.h | 63 ++++++----- src/gui/mainwindow.cpp | 12 +-- src/gui/optionsdialog.cpp | 9 +- src/webui/api/appcontroller.cpp | 8 +- src/webui/api/synccontroller.cpp | 3 +- 10 files changed, 215 insertions(+), 66 deletions(-) diff --git a/src/app/upgrade.cpp b/src/app/upgrade.cpp index cdd7d60b5..5cf8dbec8 100644 --- a/src/app/upgrade.cpp +++ b/src/app/upgrade.cpp @@ -33,6 +33,7 @@ #include "base/bittorrent/torrentcontentlayout.h" #include "base/logger.h" +#include "base/preferences.h" #include "base/profile.h" #include "base/settingsstorage.h" #include "base/utils/fs.h" @@ -110,6 +111,120 @@ namespace settingsStorage->removeValue(oldKey); } } + + void upgradeSchedulerDaysSettings() + { + auto *settingsStorage = SettingsStorage::instance(); + const auto key = QString::fromLatin1("Preferences/Scheduler/days"); + const auto value = settingsStorage->loadValue(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(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(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*/) @@ -117,6 +232,9 @@ bool upgrade(const bool /*ask*/) exportWebUIHttpsFiles(); upgradeTorrentContentLayout(); upgradeListenPortSettings(); + upgradeSchedulerDaysSettings(); + upgradeDNSServiceSettings(); + upgradeTrayIconStyleSettings(); return true; } diff --git a/src/base/bittorrent/bandwidthscheduler.cpp b/src/base/bittorrent/bandwidthscheduler.cpp index bf0f493bc..d51cb3c74 100644 --- a/src/base/bittorrent/bandwidthscheduler.cpp +++ b/src/base/bittorrent/bandwidthscheduler.cpp @@ -61,7 +61,7 @@ bool BandwidthScheduler::isTimeForAlternative() const QTime start = pref->getSchedulerStartTime(); QTime end = pref->getSchedulerEndTime(); const QTime now = QTime::currentTime(); - const int schedulerDays = pref->getSchedulerDays(); + const Scheduler::Days schedulerDays = pref->getSchedulerDays(); const int day = QDate::currentDate().dayOfWeek(); bool alternative = false; @@ -75,20 +75,34 @@ bool BandwidthScheduler::isTimeForAlternative() const { switch (schedulerDays) { - case EVERY_DAY: + case Scheduler::Days::EveryDay: alternative = !alternative; break; - case WEEK_ENDS: - if ((day == 6) || (day == 7)) + case Scheduler::Days::Monday: + 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(Scheduler::Days::Monday) - 1; + const int dayOfWeek = static_cast(schedulerDays) - offset; + if (day == dayOfWeek) + alternative = !alternative; + } + break; + case Scheduler::Days::Weekday: + if ((day >= 1) && (day <= 5)) alternative = !alternative; break; - case WEEK_DAYS: - if ((day != 6) && (day != 7)) + case Scheduler::Days::Weekend: + if ((day == 6) || (day == 7)) alternative = !alternative; break; default: - if (day == (schedulerDays - 2)) - alternative = !alternative; + Q_ASSERT(false); + break; } } diff --git a/src/base/net/dnsupdater.cpp b/src/base/net/dnsupdater.cpp index 0f213d02a..8b989a0bd 100644 --- a/src/base/net/dnsupdater.cpp +++ b/src/base/net/dnsupdater.cpp @@ -41,7 +41,7 @@ using namespace Net; DNSUpdater::DNSUpdater(QObject *parent) : QObject(parent) , m_state(OK) - , m_service(DNS::NONE) + , m_service(DNS::Service::None) { updateCredentials(); @@ -143,15 +143,16 @@ QString DNSUpdater::getUpdateUrl() const // Service specific switch (m_service) { - case DNS::DYNDNS: + case DNS::Service::DynDNS: url.setHost("members.dyndns.org"); break; - case DNS::NOIP: + case DNS::Service::NoIP: url.setHost("dynupdate.no-ip.com"); break; default: qWarning() << "Unrecognized Dynamic DNS service!"; - Q_ASSERT(0); + Q_ASSERT(false); + break; } 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) { - case DNS::DYNDNS: + case DNS::Service::DynDNS: return {"https://account.dyn.com/entrance/"}; - case DNS::NOIP: + case DNS::Service::NoIP: return {"https://www.noip.com/remote-access"}; default: - Q_ASSERT(0); + Q_ASSERT(false); + break; } return {}; } diff --git a/src/base/net/dnsupdater.h b/src/base/net/dnsupdater.h index 06723d531..3236833f1 100644 --- a/src/base/net/dnsupdater.h +++ b/src/base/net/dnsupdater.h @@ -48,7 +48,7 @@ namespace Net explicit DNSUpdater(QObject *parent = nullptr); ~DNSUpdater(); - static QUrl getRegistrationUrl(int service); + static QUrl getRegistrationUrl(DNS::Service service); public slots: void updateCredentials(); diff --git a/src/base/preferences.cpp b/src/base/preferences.cpp index 7bbf961f4..f46403a32 100644 --- a/src/base/preferences.cpp +++ b/src/base/preferences.cpp @@ -493,14 +493,14 @@ void Preferences::setSchedulerEndTime(const QTime &time) setValue("Preferences/Scheduler/end_time", time); } -SchedulerDays Preferences::getSchedulerDays() const +Scheduler::Days Preferences::getSchedulerDays() const { - return static_cast(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(days)); + SettingsStorage::instance()->storeValue("Preferences/Scheduler/days", days); } // Search @@ -816,12 +816,12 @@ void Preferences::setDynDNSEnabled(const bool enabled) 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 @@ -1211,12 +1211,12 @@ void Preferences::setConfirmRemoveAllTags(const bool enabled) #ifndef Q_OS_MACOS 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) { - setValue("Preferences/Advanced/TrayIconStyle", style); + SettingsStorage::instance()->storeValue("Preferences/Advanced/TrayIconStyle", style); } #endif diff --git a/src/base/preferences.h b/src/base/preferences.h index 8769bc5e4..9b998c975 100644 --- a/src/base/preferences.h +++ b/src/base/preferences.h @@ -29,6 +29,7 @@ #pragma once +#include #include #include #include @@ -40,38 +41,50 @@ class QNetworkCookie; class QSize; class QTime; -enum SchedulerDays +namespace Scheduler { - EVERY_DAY, - WEEK_DAYS, - WEEK_ENDS, - MON, - TUE, - WED, - THU, - FRI, - SAT, - SUN -}; + Q_NAMESPACE -namespace TrayIcon -{ - enum Style + enum class Days : int { - NORMAL = 0, - MONO_DARK, - MONO_LIGHT + EveryDay = 0, + Weekday = 1, + Weekend = 2, + Monday = 3, + Tuesday = 4, + Wednesday = 5, + Thursday = 6, + Friday = 7, + Saturday = 8, + Sunday = 9 }; + Q_ENUM_NS(Days) } 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, - NOIP, - NONE = -1 + Normal = 0, + MonoDark = 1, + MonoLight = 2 }; + Q_ENUM_NS(Style) } class Preferences : public QObject @@ -161,8 +174,8 @@ public: void setSchedulerStartTime(const QTime &time); QTime getSchedulerEndTime() const; void setSchedulerEndTime(const QTime &time); - SchedulerDays getSchedulerDays() const; - void setSchedulerDays(SchedulerDays days); + Scheduler::Days getSchedulerDays() const; + void setSchedulerDays(Scheduler::Days days); // Search bool isSearchEnabled() const; @@ -236,7 +249,7 @@ public: bool isDynDNSEnabled() const; void setDynDNSEnabled(bool enabled); DNS::Service getDynDNSService() const; - void setDynDNSService(int service); + void setDynDNSService(DNS::Service service); QString getDynDomainName() const; void setDynDomainName(const QString &name); QString getDynDNSUsername() const; diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index cbfd77797..1db316dde 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -2080,11 +2080,11 @@ QIcon MainWindow::getSystrayIcon() const #if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) switch (style) { - case TrayIcon::NORMAL: + case TrayIcon::Style::Normal: return QIcon::fromTheme(QLatin1String("qbittorrent-tray")); - case TrayIcon::MONO_DARK: + case TrayIcon::Style::MonoDark: return QIcon::fromTheme(QLatin1String("qbittorrent-tray-dark")); - case TrayIcon::MONO_LIGHT: + case TrayIcon::Style::MonoLight: return QIcon::fromTheme(QLatin1String("qbittorrent-tray-light")); default: break; @@ -2092,11 +2092,11 @@ QIcon MainWindow::getSystrayIcon() const #else switch (style) { - case TrayIcon::NORMAL: + case TrayIcon::Style::Normal: return UIThemeManager::instance()->getIcon(QLatin1String("qbittorrent-tray")); - case TrayIcon::MONO_DARK: + case TrayIcon::Style::MonoDark: return UIThemeManager::instance()->getIcon(QLatin1String("qbittorrent-tray-dark")); - case TrayIcon::MONO_LIGHT: + case TrayIcon::Style::MonoLight: return UIThemeManager::instance()->getIcon(QLatin1String("qbittorrent-tray-light")); default: break; diff --git a/src/gui/optionsdialog.cpp b/src/gui/optionsdialog.cpp index ff64a6445..5a6dae4f6 100644 --- a/src/gui/optionsdialog.cpp +++ b/src/gui/optionsdialog.cpp @@ -783,7 +783,7 @@ void OptionsDialog::saveOptions() session->setIgnoreLimitsOnLAN(!m_ui->checkLimitLocalPeerRate->isChecked()); pref->setSchedulerStartTime(m_ui->timeEditScheduleFrom->time()); pref->setSchedulerEndTime(m_ui->timeEditScheduleTo->time()); - pref->setSchedulerDays(static_cast(m_ui->comboBoxScheduleDays->currentIndex())); + pref->setSchedulerDays(static_cast(m_ui->comboBoxScheduleDays->currentIndex())); session->setBandwidthSchedulerEnabled(m_ui->groupBoxSchedule->isChecked()); auto proxyConfigManager = Net::ProxyConfigurationManager::instance(); @@ -867,7 +867,7 @@ void OptionsDialog::saveOptions() pref->setWebUIHostHeaderValidationEnabled(m_ui->groupHostHeaderValidation->isChecked()); // DynDNS pref->setDynDNSEnabled(m_ui->checkDynDNS->isChecked()); - pref->setDynDNSService(m_ui->comboDNSService->currentIndex()); + pref->setDynDNSService(static_cast(m_ui->comboDNSService->currentIndex())); pref->setDynDomainName(m_ui->domainNameTxt->text()); pref->setDynDNSUsername(m_ui->DNSUsernameTxt->text()); pref->setDynDNSPassword(m_ui->DNSPasswordTxt->text()); @@ -941,7 +941,7 @@ void OptionsDialog::loadOptions() { m_ui->checkMinimizeToSysTray->setChecked(pref->minimizeToTray()); m_ui->checkCloseToSystray->setChecked(pref->closeToTray()); - m_ui->comboTrayIcon->setCurrentIndex(pref->trayIconStyle()); + m_ui->comboTrayIcon->setCurrentIndex(static_cast(pref->trayIconStyle())); } #endif @@ -1832,7 +1832,8 @@ void OptionsDialog::showConnectionTab() void OptionsDialog::on_registerDNSBtn_clicked() { - QDesktopServices::openUrl(Net::DNSUpdater::getRegistrationUrl(m_ui->comboDNSService->currentIndex())); + const auto service = static_cast(m_ui->comboDNSService->currentIndex()); + QDesktopServices::openUrl(Net::DNSUpdater::getRegistrationUrl(service)); } void OptionsDialog::on_IpFilterRefreshBtn_clicked() diff --git a/src/webui/api/appcontroller.cpp b/src/webui/api/appcontroller.cpp index a6be72dd5..36cbd80a9 100644 --- a/src/webui/api/appcontroller.cpp +++ b/src/webui/api/appcontroller.cpp @@ -198,7 +198,7 @@ void AppController::preferencesAction() const QTime end_time = pref->getSchedulerEndTime(); data["schedule_to_hour"] = end_time.hour(); data["schedule_to_min"] = end_time.minute(); - data["scheduler_days"] = pref->getSchedulerDays(); + data["scheduler_days"] = static_cast(pref->getSchedulerDays()); // Bittorrent // Privacy @@ -264,7 +264,7 @@ void AppController::preferencesAction() data["web_ui_reverse_proxies_list"] = pref->getWebUITrustedReverseProxiesList(); // Update my dynamic domain name data["dyndns_enabled"] = pref->isDynDNSEnabled(); - data["dyndns_service"] = pref->getDynDNSService(); + data["dyndns_service"] = static_cast(pref->getDynDNSService()); data["dyndns_username"] = pref->getDynDNSUsername(); data["dyndns_password"] = pref->getDynDNSPassword(); data["dyndns_domain"] = pref->getDynDomainName(); @@ -560,7 +560,7 @@ void AppController::setPreferencesAction() if (m.contains("schedule_to_hour") && m.contains("schedule_to_min")) pref->setSchedulerEndTime(QTime(m["schedule_to_hour"].toInt(), m["schedule_to_min"].toInt())); if (hasKey("scheduler_days")) - pref->setSchedulerDays(SchedulerDays(it.value().toInt())); + pref->setSchedulerDays(static_cast(it.value().toInt())); // Bittorrent // Privacy @@ -698,7 +698,7 @@ void AppController::setPreferencesAction() if (hasKey("dyndns_enabled")) pref->setDynDNSEnabled(it.value().toBool()); if (hasKey("dyndns_service")) - pref->setDynDNSService(it.value().toInt()); + pref->setDynDNSService(static_cast(it.value().toInt())); if (hasKey("dyndns_username")) pref->setDynDNSUsername(it.value().toString()); if (hasKey("dyndns_password")) diff --git a/src/webui/api/synccontroller.cpp b/src/webui/api/synccontroller.cpp index 7f7798abe..2aac15741 100644 --- a/src/webui/api/synccontroller.cpp +++ b/src/webui/api/synccontroller.cpp @@ -282,7 +282,8 @@ namespace } break; default: - Q_ASSERT(0); + Q_ASSERT(false); + break; } }