From 26c02f97dc5a3b3f90ff77e44d83ce5e91b86f7c Mon Sep 17 00:00:00 2001 From: Alexandre Petitjean Date: Sun, 30 Jun 2024 23:35:03 +0200 Subject: [PATCH] Still improve settings management --- src/client/kimaiClient.cpp | 5 + src/client/kimaiClient.h | 1 + src/gui/mainWindow.cpp | 113 ++++++++++++---------- src/gui/settingsDialog.cpp | 70 +++++++++----- src/main.cpp | 2 +- src/monitor/kimaiEventsMonitor.cpp | 2 +- src/settings/settings.cpp | 150 ++++++++++------------------- src/settings/settings.h | 19 +--- 8 files changed, 169 insertions(+), 193 deletions(-) diff --git a/src/client/kimaiClient.cpp b/src/client/kimaiClient.cpp index cfd8d6e..e5cca03 100644 --- a/src/client/kimaiClient.cpp +++ b/src/client/kimaiClient.cpp @@ -180,6 +180,11 @@ void KimaiClient::setHost(const QString& host) mD->host = host; } +QString KimaiClient::host() const +{ + return mD->host; +} + void KimaiClient::setLegacyAuth(const QString& username, const QString& token) { mD->username = username; diff --git a/src/client/kimaiClient.h b/src/client/kimaiClient.h index 9c48e59..9829f62 100644 --- a/src/client/kimaiClient.h +++ b/src/client/kimaiClient.h @@ -34,6 +34,7 @@ class KimaiClient : public QObject ~KimaiClient() override; void setHost(const QString& host); + QString host() const; void setLegacyAuth(const QString& username, const QString& token); bool isUsingLegacyAuth() const; diff --git a/src/gui/mainWindow.cpp b/src/gui/mainWindow.cpp index cc8ad8b..c8bb9ff 100644 --- a/src/gui/mainWindow.cpp +++ b/src/gui/mainWindow.cpp @@ -31,7 +31,7 @@ MainWindow::MainWindow() : mUi(std::make_unique()) { mUi->setupUi(this); - const auto& settings = SettingsHandler::instance().get(); + const auto& settings = SettingsHelper::load(); /* * Setup icon @@ -163,7 +163,7 @@ MainWindow::MainWindow() : mUi(std::make_unique()) if (settings.kemai.checkUpdateAtStartup) { QTimer::singleShot(FirstRequestDelayMs, [&]() { - auto ignoreVersion = QVersionNumber::fromString(SettingsHandler::instance().get().kemai.ignoredVersion); + auto ignoreVersion = QVersionNumber::fromString(SettingsHelper::load().kemai.ignoredVersion); auto currentVersion = QVersionNumber::fromString(KEMAI_VERSION); mUpdater.checkAvailableNewVersion(currentVersion >= ignoreVersion ? currentVersion : ignoreVersion, true); }); @@ -188,19 +188,19 @@ void MainWindow::closeEvent(QCloseEvent* event) mLoggerWidget.close(); } - auto settings = SettingsHandler::instance().get(); + auto settings = SettingsHelper::load(); if (settings.kemai.closeToSystemTray) { hide(); event->ignore(); } settings.kemai.geometry = saveGeometry(); - SettingsHandler::instance().set(settings); + SettingsHelper::save(settings); } void MainWindow::hideEvent(QHideEvent* event) { - auto settings = SettingsHandler::instance().get(); + auto settings = SettingsHelper::load(); if (settings.kemai.minimizeToSystemTray) { if (event->spontaneous() && isMinimized()) @@ -210,7 +210,7 @@ void MainWindow::hideEvent(QHideEvent* event) } } settings.kemai.geometry = saveGeometry(); - SettingsHandler::instance().set(settings); + SettingsHelper::save(settings); } void MainWindow::createKemaiSession(const Settings::Profile& profile) @@ -229,30 +229,39 @@ void MainWindow::createKemaiSession(const Settings::Profile& profile) mStatusInstanceLabel.setText(tr("Not connected")); } - auto settings = SettingsHandler::instance().get(); - if (settings.hasValidProfile()) + // Ensure profile has all required infos + const auto haveHost = !profile.host.isEmpty(); + const auto haveLegacyAuth = !profile.username.isEmpty() && !profile.token.isEmpty(); + const auto haveAPIToken = !profile.apiToken.isEmpty(); + const auto isProfileValidated = haveHost && (haveLegacyAuth || haveAPIToken); + if (!isProfileValidated) { - auto kimaiClient = std::make_shared(); + spdlog::error("Invalid profile. Check settings."); + return; + } - kimaiClient->setHost(profile.host); - kimaiClient->setLegacyAuth(profile.username, profile.token); - kimaiClient->setAPIToken(profile.apiToken); + // Create session + auto kimaiClient = std::make_shared(); - mSession = std::make_shared(kimaiClient); - connect(mSession.get(), &KemaiSession::currentTimeSheetChanged, this, &MainWindow::onCurrentTimeSheetChanged); - connect(mSession.get(), &KemaiSession::pluginsChanged, this, &MainWindow::onPluginsChanged); - connect(mSession.get(), &KemaiSession::versionChanged, this, &MainWindow::onSessionVersionChanged); + kimaiClient->setHost(profile.host); + kimaiClient->setLegacyAuth(profile.username, profile.token); + kimaiClient->setAPIToken(profile.apiToken); - mActivityWidget->setKemaiSession(mSession); + mSession = std::make_shared(kimaiClient); + connect(mSession.get(), &KemaiSession::currentTimeSheetChanged, this, &MainWindow::onCurrentTimeSheetChanged); + connect(mSession.get(), &KemaiSession::pluginsChanged, this, &MainWindow::onPluginsChanged); + connect(mSession.get(), &KemaiSession::versionChanged, this, &MainWindow::onSessionVersionChanged); - mSession->refreshCache(); - mSession->refreshSessionInfos(); - mSession->refreshCurrentTimeSheet(); + mActivityWidget->setKemaiSession(mSession); - // Save profile connection - settings.kemai.lastConnectedProfile = profile.id; - SettingsHandler::instance().set(settings); - } + mSession->refreshCache(); + mSession->refreshSessionInfos(); + mSession->refreshCurrentTimeSheet(); + + // Save profile connection + auto settings = SettingsHelper::load(); + settings.kemai.lastConnectedProfile = profile.id; + SettingsHelper::save(settings); } void MainWindow::showSelectedView() @@ -283,12 +292,14 @@ void MainWindow::setViewActionsEnabled(bool enable) void MainWindow::updateProfilesMenu() { - const auto& settings = SettingsHandler::instance().get(); + const auto& settings = SettingsHelper::load(); - // Removes obsoletes profiles + // Removes previous profiles for (auto action : mActGroupProfiles->actions()) { - if (!settings.findProfile(action->data().toUuid()).has_value()) + auto it = std::find_if(settings.profiles.begin(), settings.profiles.end(), + [profileId = action->data().toUuid()](const auto& profile) { return profile.id == profileId; }); + if (it != settings.profiles.end()) { mProfileMenu->removeAction(action); mActGroupProfiles->removeAction(action); @@ -318,26 +329,26 @@ void MainWindow::updateProfilesMenu() void MainWindow::processAutoConnect() { - const auto& settings = SettingsHandler::instance().get(); + const auto& settings = SettingsHelper::load(); if (settings.profiles.empty()) { return; } - auto profile = settings.findProfile(settings.kemai.lastConnectedProfile); - if (!profile.has_value()) - { - profile = settings.profiles.front(); - } - - for (auto& action : mActGroupProfiles->actions()) + auto profileIt = std::find_if(settings.profiles.begin(), settings.profiles.end(), + [profileId = settings.kemai.lastConnectedProfile](const auto& profile) { return profile.id == profileId; }); + if (profileIt != settings.profiles.end()) { - if (action->data().toUuid() == profile->id) + for (auto& action : mActGroupProfiles->actions()) { - action->setChecked(true); + if (action->data().toUuid() == profileIt->id) + { + action->setChecked(true); + } } + + createKemaiSession(*profileIt); } - createKemaiSession(profile.value()); } void MainWindow::onCurrentTimeSheetChanged() @@ -387,10 +398,10 @@ void MainWindow::onSessionVersionChanged() void MainWindow::onActionSettingsTriggered() { SettingsDialog settingsDialog(mDesktopEventsMonitor, this); - settingsDialog.setSettings(SettingsHandler::instance().get()); + settingsDialog.setSettings(SettingsHelper::load()); if (settingsDialog.exec() == QDialog::Accepted) { - SettingsHandler::instance().set(settingsDialog.settings()); + SettingsHelper::save(settingsDialog.settings()); showSelectedView(); updateProfilesMenu(); @@ -410,10 +421,9 @@ void MainWindow::onActionCheckUpdateTriggered() void MainWindow::onActionOpenHostTriggered() { - const auto& settings = SettingsHandler::instance().get(); - if (settings.hasValidProfile()) + if (mSession) { - QDesktopServices::openUrl(QUrl::fromUserInput(settings.profiles.front().host)); + QDesktopServices::openUrl(QUrl::fromUserInput(mSession->client()->host())); } } @@ -436,7 +446,7 @@ void MainWindow::onSystemTrayActivated(QSystemTrayIcon::ActivationReason reason) switch (reason) { case QSystemTrayIcon::Trigger: { - const auto& settings = SettingsHandler::instance().get(); + const auto& settings = SettingsHelper::load(); if (isVisible() && (settings.kemai.minimizeToSystemTray || settings.kemai.closeToSystemTray)) { hide(); @@ -472,9 +482,9 @@ void MainWindow::onNewVersionCheckFinished(const VersionDetails& details) break; case QMessageBox::Ignore: { - auto settings = SettingsHandler::instance().get(); + auto settings = SettingsHelper::load(); settings.kemai.ignoredVersion = details.vn.toString(); - SettingsHandler::instance().set(settings); + SettingsHelper::save(settings); } break; @@ -508,12 +518,13 @@ void MainWindow::onProfilesActionGroupTriggered(QAction* action) { if (action->isChecked()) { - auto settings = SettingsHandler::instance().get(); + auto settings = SettingsHelper::load(); auto profileId = action->data().toUuid(); - auto profile = settings.findProfile(profileId); - if (profile.has_value()) + auto profileIt = + std::find_if(settings.profiles.begin(), settings.profiles.end(), [profileId](const auto& profile) { return profile.id == profileId; }); + if (profileIt != settings.profiles.end()) { - createKemaiSession(profile.value()); + createKemaiSession(*profileIt); } } } @@ -521,7 +532,7 @@ void MainWindow::onProfilesActionGroupTriggered(QAction* action) void MainWindow::onDesktopIdleDetected() { - spdlog::info("System is idle since {} minutes. Stop current TimeSheet.", SettingsHandler::instance().get().events.idleDelayMinutes); + spdlog::info("System is idle since {} minutes. Stop current TimeSheet.", SettingsHelper::load().events.idleDelayMinutes); mActivityWidget->stopCurrentTimeSheet(); } diff --git a/src/gui/settingsDialog.cpp b/src/gui/settingsDialog.cpp index 65328bd..eabb68d 100644 --- a/src/gui/settingsDialog.cpp +++ b/src/gui/settingsDialog.cpp @@ -40,7 +40,7 @@ SettingsDialog::SettingsDialog(const std::shared_ptr& desk auto addLanguage = [cbLanguage = mUi->cbLanguage](const QString& language) { QLocale locale(language); - cbLanguage->addItem(QString("%1 [%2]").arg(QLocale::languageToString(locale.language()), QLocale::countryToString(locale.country())), locale); + cbLanguage->addItem(QString("%1 [%2]").arg(QLocale::languageToString(locale.language()), QLocale::territoryToString(locale.territory())), locale); }; mActToggleTokenVisible = mUi->leToken->addAction(QIcon(":/icons/visible-off"), QLineEdit::TrailingPosition); @@ -76,7 +76,7 @@ SettingsDialog::SettingsDialog(const std::shared_ptr& desk // show dialog if language changes from settings connect(mUi->cbLanguage, &QComboBox::currentTextChanged, [&](const QString&) { - const auto& settings = SettingsHandler::instance().get(); + const auto& settings = SettingsHelper::load(); if (settings.kemai.language != mUi->cbLanguage->currentData().toLocale()) { QMessageBox::warning(this, tr(""), tr("Language changed. Application restart is required.")); @@ -169,20 +169,32 @@ void SettingsDialog::onProfilesListCurrentItemChanged(QListWidgetItem* current, QSignalBlocker hostSignalBlocker(mUi->leHost); QSignalBlocker usernameSignalBlocker(mUi->leUsername); QSignalBlocker tokenSignalBlocker(mUi->leToken); + QSignalBlocker apiTokenSignalBlocker(mUi->leAPIToken); - Settings::Profile profile; + mUi->leName->clear(); + mUi->leHost->clear(); + mUi->leUsername->clear(); + mUi->leToken->clear(); + mUi->leAPIToken->clear(); + + auto hasProfile = false; if (current != nullptr) { - profile = m_settings.findProfile(current->data(Qt::UserRole).toUuid()).value_or(Settings::Profile{}); - } + auto profileIt = std::find_if(m_settings.profiles.begin(), m_settings.profiles.end(), + [profileId = current->data(Qt::UserRole).toUuid()](const auto& profile) { return profile.id == profileId; }); + + if (profileIt != m_settings.profiles.end()) + { + mUi->leName->setText(profileIt->name); + mUi->leHost->setText(profileIt->host); + mUi->leUsername->setText(profileIt->username); + mUi->leToken->setText(profileIt->token); + mUi->leAPIToken->setText(profileIt->apiToken); - auto hasProfile = !profile.id.isNull(); - mUi->leName->setText(profile.name); - mUi->leHost->setText(profile.host); - mUi->leUsername->setText(profile.username); - mUi->leToken->setText(profile.token); - mUi->leAPIToken->setText(profile.apiToken); + hasProfile = true; + } + } mUi->delProfileButton->setEnabled(hasProfile); mUi->leName->setEnabled(hasProfile); @@ -224,16 +236,19 @@ void SettingsDialog::onProfileFieldValueChanged() auto item = mUi->profilesListWidget->currentItem(); if (item != nullptr) { - auto profile = m_settings.findProfileIt(item->data(Qt::UserRole).toUuid()); - if (profile != m_settings.profiles.end()) + const auto profileId = item->data(Qt::UserRole).toUuid(); + auto profileIt = + std::find_if(m_settings.profiles.begin(), m_settings.profiles.end(), [&profileId](const auto& profile) { return profile.id == profileId; }); + + if (profileIt != m_settings.profiles.end()) { - profile->name = mUi->leName->text(); - profile->host = mUi->leHost->text(); - profile->username = mUi->leUsername->text(); - profile->token = mUi->leToken->text(); - profile->apiToken = mUi->leAPIToken->text(); + profileIt->name = mUi->leName->text(); + profileIt->host = mUi->leHost->text(); + profileIt->username = mUi->leUsername->text(); + profileIt->token = mUi->leToken->text(); + profileIt->apiToken = mUi->leAPIToken->text(); - item->setText(profile->name); + item->setText(profileIt->name); } } } @@ -259,13 +274,16 @@ void SettingsDialog::onProfileDelButtonClicked() auto item = mUi->profilesListWidget->takeItem(mUi->profilesListWidget->currentRow()); if (item != nullptr) { - auto it = std::find_if(m_settings.profiles.begin(), m_settings.profiles.end(), - [profileId = item->data(Qt::UserRole).toUuid()](const Settings::Profile& profile) { return profile.id == profileId; }); - if (it != m_settings.profiles.end()) - { - auto pos = std::distance(m_settings.profiles.begin(), it); - m_settings.profiles.removeAt(pos); - } + // auto it = std::find_if(m_settings.profiles.begin(), m_settings.profiles.end(), + // [profileId = item->data(Qt::UserRole).toUuid()](const Settings::Profile& profile) { return profile.id == profileId; }); + // if (it != m_settings.profiles.end()) + // { + // auto pos = std::distance(m_settings.profiles.begin(), it); + // m_settings.profiles.removeAt(pos); + // } + + std::erase_if(m_settings.profiles, + [profileId = item->data(Qt::UserRole).toUuid()](const Settings::Profile& profile) { return profile.id == profileId; }); delete item; } diff --git a/src/main.cpp b/src/main.cpp index d5a2f53..3c74ec0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,7 +55,7 @@ int main(int argc, char* argv[]) QApplication::setApplicationVersion(KEMAI_VERSION); // Get kemai data directory and log file path - const auto& kemaiSettings = SettingsHandler::instance().get(); + const auto& kemaiSettings = SettingsHelper::load(); // Create Qt logger model before spdlog sinks auto loggerTreeModel = std::make_shared(); diff --git a/src/monitor/kimaiEventsMonitor.cpp b/src/monitor/kimaiEventsMonitor.cpp index 15f267c..0eadc7e 100644 --- a/src/monitor/kimaiEventsMonitor.cpp +++ b/src/monitor/kimaiEventsMonitor.cpp @@ -37,7 +37,7 @@ bool KimaiEventsMonitor::hasCurrentTimeSheet() const void KimaiEventsMonitor::onSecondTimeout() { - const auto& settings = SettingsHandler::instance().get(); + auto settings = SettingsHelper::load(); if (settings.events.autoRefreshCurrentTimeSheet && mLastTimeSheetUpdate.has_value()) { if (mLastTimeSheetUpdate->secsTo(QDateTime::currentDateTime()) >= settings.events.autoRefreshCurrentTimeSheetDelaySeconds) diff --git a/src/settings/settings.cpp b/src/settings/settings.cpp index e1fe10b..9623a67 100644 --- a/src/settings/settings.cpp +++ b/src/settings/settings.cpp @@ -72,130 +72,84 @@ Settings loadFromLegacyIni(const QSettings& qset) /* * Class impl */ - -bool Settings::hasValidProfile() const -{ - if (!profiles.empty()) - { - const auto profile = profiles.begin(); - const auto haveHost = !profile->host.isEmpty(); - const auto haveLegacyAuth = !profile->username.isEmpty() && !profile->token.isEmpty(); - const auto haveAPIToken = !profile->apiToken.isEmpty(); - - return haveHost && (haveLegacyAuth || haveAPIToken); - } - return false; -} - -QList::iterator Settings::findProfileIt(const QUuid& profileId) +Settings SettingsHelper::load() { - return std::find_if(profiles.begin(), profiles.end(), [&profileId](const auto& profile) { return profile.id == profileId; }); -} + auto settings = Settings{}; -QList::const_iterator Settings::findProfileIt(const QUuid& profileId) const -{ - return std::find_if(profiles.begin(), profiles.end(), [&profileId](const auto& profile) { return profile.id == profileId; }); -} + auto qset = getQSettingsInstance(); + auto jsonSettingsPath = getJsonSettingsPath(); -std::optional Settings::findProfile(const QUuid& profileId) const -{ - auto it = findProfileIt(profileId); - if (it != profiles.end()) + // Migrate from ini settings to json + if (QFile::exists(qset.fileName()) && !QFile::exists(jsonSettingsPath)) { - return *it; + save(loadFromLegacyIni(qset)); } - return std::nullopt; -} - -SettingsHandler& SettingsHandler::instance() -{ - static SettingsHandler gSettingsHandler; - return gSettingsHandler; -} -const Settings& SettingsHandler::get() -{ - if (!mSettings.has_value()) + // Load from previous save + if (QFile::exists(jsonSettingsPath)) { - mSettings = Settings{}; + QFile jsonFile(jsonSettingsPath); + jsonFile.open(QIODevice::ReadOnly | QIODevice::Text); + auto jsonDocument = QJsonDocument::fromJson(jsonFile.readAll()); + auto root = jsonDocument.object(); + jsonFile.close(); - auto qset = getQSettingsInstance(); - auto jsonSettingsPath = getJsonSettingsPath(); + const auto cfgVersion = root.value("version").toInt(); - // Migrate from ini settings to json - if (QFile::exists(qset.fileName()) && !QFile::exists(jsonSettingsPath)) + for (const auto& certificationValue : root.value("trustedCertificates").toArray()) { - auto settings = loadFromLegacyIni(qset); - set(settings); - return mSettings.value(); + settings.trustedCertificates.append(certificationValue.toString()); } - // Load from previous save - if (QFile::exists(jsonSettingsPath)) + auto kemaiObject = root.value("kemai").toObject(); + settings.kemai.closeToSystemTray = kemaiObject.value("closeToSystemTray").toBool(); + settings.kemai.minimizeToSystemTray = kemaiObject.value("minimizeToSystemTray").toBool(); + settings.kemai.checkUpdateAtStartup = kemaiObject.value("checkUpdateAtStartup").toBool(); + settings.kemai.ignoredVersion = kemaiObject.value("ignoredVersion").toString(); + settings.kemai.geometry = QByteArray::fromBase64(kemaiObject.value("geometry").toString().toLocal8Bit()); + settings.kemai.language = QLocale(kemaiObject.value("language").toString()); + if (kemaiObject.contains("lastConnectedProfile")) { - QFile jsonFile(jsonSettingsPath); - jsonFile.open(QIODevice::ReadOnly | QIODevice::Text); - auto jsonDocument = QJsonDocument::fromJson(jsonFile.readAll()); - auto root = jsonDocument.object(); - jsonFile.close(); + settings.kemai.lastConnectedProfile = QUuid(kemaiObject.value("lastConnectedProfile").toString()); + } - const auto cfgVersion = root.value("version").toInt(); + for (const auto& profileValue : root.value("profiles").toArray()) + { + const auto profileObject = profileValue.toObject(); - for (const auto& certificationValue : root.value("trustedCertificates").toArray()) - { - mSettings->trustedCertificates.append(certificationValue.toString()); - } + Settings::Profile profile; + profile.id = QUuid(profileObject.value("id").toString()); + profile.name = profileObject.value("name").toString(); + profile.host = profileObject.value("host").toString(); + profile.username = profileObject.value("username").toString(); + profile.token = profileObject.value("token").toString(); - auto kemaiObject = root.value("kemai").toObject(); - mSettings->kemai.closeToSystemTray = kemaiObject.value("closeToSystemTray").toBool(); - mSettings->kemai.minimizeToSystemTray = kemaiObject.value("minimizeToSystemTray").toBool(); - mSettings->kemai.checkUpdateAtStartup = kemaiObject.value("checkUpdateAtStartup").toBool(); - mSettings->kemai.ignoredVersion = kemaiObject.value("ignoredVersion").toString(); - mSettings->kemai.geometry = QByteArray::fromBase64(kemaiObject.value("geometry").toString().toLocal8Bit()); - mSettings->kemai.language = QLocale(kemaiObject.value("language").toString()); - if (kemaiObject.contains("lastConnectedProfile")) + if (cfgVersion >= gCfgVersion_3) { - mSettings->kemai.lastConnectedProfile = QUuid(kemaiObject.value("lastConnectedProfile").toString()); + profile.apiToken = profileObject.value("apiToken").toString(); } - for (const auto& profileValue : root.value("profiles").toArray()) - { - const auto profileObject = profileValue.toObject(); - - Settings::Profile profile; - profile.id = QUuid(profileObject.value("id").toString()); - profile.name = profileObject.value("name").toString(); - profile.host = profileObject.value("host").toString(); - profile.username = profileObject.value("username").toString(); - profile.token = profileObject.value("token").toString(); - - if (cfgVersion >= gCfgVersion_3) - { - profile.apiToken = profileObject.value("apiToken").toString(); - } + settings.profiles.push_back(profile); + } - mSettings->profiles << profile; - } + if (root.contains("events")) + { + auto eventsObject = root.value("events").toObject(); + settings.events.stopOnLock = eventsObject.value("stopOnLock").toBool(); + settings.events.stopOnIdle = eventsObject.value("stopOnIdle").toBool(); + settings.events.idleDelayMinutes = eventsObject.value("idleDelayMinutes").toInt(); - if (root.contains("events")) + if (cfgVersion >= gCfgVersion_2) { - auto eventsObject = root.value("events").toObject(); - mSettings->events.stopOnLock = eventsObject.value("stopOnLock").toBool(); - mSettings->events.stopOnIdle = eventsObject.value("stopOnIdle").toBool(); - mSettings->events.idleDelayMinutes = eventsObject.value("idleDelayMinutes").toInt(); - - if (cfgVersion >= gCfgVersion_2) - { - mSettings->events.autoRefreshCurrentTimeSheet = eventsObject.value("autoRefreshCurrentTimeSheet").toBool(); - mSettings->events.autoRefreshCurrentTimeSheetDelaySeconds = eventsObject.value("autoRefreshCurrentTimeSheetDelaySeconds").toInt(); - } + settings.events.autoRefreshCurrentTimeSheet = eventsObject.value("autoRefreshCurrentTimeSheet").toBool(); + settings.events.autoRefreshCurrentTimeSheetDelaySeconds = eventsObject.value("autoRefreshCurrentTimeSheetDelaySeconds").toInt(); } } } - return mSettings.value(); + return settings; } -void SettingsHandler::set(const Settings& settings) +void SettingsHelper::save(const Settings& settings) { QJsonArray profilesArray; for (const auto& profile : settings.profiles) @@ -249,6 +203,4 @@ void SettingsHandler::set(const Settings& settings) testStream << QJsonDocument(root).toJson(); jsonFile.close(); - - mSettings = settings; } diff --git a/src/settings/settings.h b/src/settings/settings.h index c61b40f..042c9e6 100644 --- a/src/settings/settings.h +++ b/src/settings/settings.h @@ -19,7 +19,7 @@ struct Settings QString token; QString apiToken; // Since Kimai 2.14 }; - QList profiles; + std::vector profiles; QStringList trustedCertificates; @@ -42,24 +42,13 @@ struct Settings bool autoRefreshCurrentTimeSheet = false; int autoRefreshCurrentTimeSheetDelaySeconds = 5; } events; - - bool hasValidProfile() const; - - QList::iterator findProfileIt(const QUuid& profileId); - QList::const_iterator findProfileIt(const QUuid& profileId) const; - std::optional findProfile(const QUuid& profileId) const; }; -class SettingsHandler +class SettingsHelper { public: - static SettingsHandler& instance(); - - const Settings& get(); - void set(const Settings& settings); - -private: - std::optional mSettings = std::nullopt; + static Settings load(); + static void save(const Settings& settings); }; } // namespace kemai