Skip to content

Commit

Permalink
Improve settings handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrePTJ committed Jun 25, 2024
1 parent 1fcbece commit dbbd110
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 128 deletions.
44 changes: 22 additions & 22 deletions src/gui/mainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ MainWindow::MainWindow() : mUi(std::make_unique<Ui::MainWindow>())
{
mUi->setupUi(this);

auto settings = Settings::get();
const auto& settings = SettingsHandler::instance().get();

/*
* Setup icon
Expand Down Expand Up @@ -163,7 +163,7 @@ MainWindow::MainWindow() : mUi(std::make_unique<Ui::MainWindow>())
if (settings.kemai.checkUpdateAtStartup)
{
QTimer::singleShot(FirstRequestDelayMs, [&]() {
auto ignoreVersion = QVersionNumber::fromString(Settings::get().kemai.ignoredVersion);
auto ignoreVersion = QVersionNumber::fromString(SettingsHandler::instance().get().kemai.ignoredVersion);
auto currentVersion = QVersionNumber::fromString(KEMAI_VERSION);
mUpdater.checkAvailableNewVersion(currentVersion >= ignoreVersion ? currentVersion : ignoreVersion, true);
});
Expand All @@ -188,19 +188,19 @@ void MainWindow::closeEvent(QCloseEvent* event)
mLoggerWidget.close();
}

auto settings = Settings::get();
auto settings = SettingsHandler::instance().get();
if (settings.kemai.closeToSystemTray)
{
hide();
event->ignore();
}
settings.kemai.geometry = saveGeometry();
Settings::save(settings);
SettingsHandler::instance().set(settings);
}

void MainWindow::hideEvent(QHideEvent* event)
{
auto settings = Settings::get();
auto settings = SettingsHandler::instance().get();
if (settings.kemai.minimizeToSystemTray)
{
if (event->spontaneous() && isMinimized())
Expand All @@ -210,7 +210,7 @@ void MainWindow::hideEvent(QHideEvent* event)
}
}
settings.kemai.geometry = saveGeometry();
Settings::save(settings);
SettingsHandler::instance().set(settings);
}

void MainWindow::createKemaiSession(const Settings::Profile& profile)
Expand All @@ -229,8 +229,8 @@ void MainWindow::createKemaiSession(const Settings::Profile& profile)
mStatusInstanceLabel.setText(tr("Not connected"));
}

auto settings = Settings::get();
if (settings.isReady())
auto settings = SettingsHandler::instance().get();
if (settings.hasValidProfile())
{
auto kimaiClient = std::make_shared<KimaiClient>();

Expand All @@ -251,7 +251,7 @@ void MainWindow::createKemaiSession(const Settings::Profile& profile)

// Save profile connection
settings.kemai.lastConnectedProfile = profile.id;
Settings::save(settings);
SettingsHandler::instance().set(settings);
}
}

Expand Down Expand Up @@ -283,7 +283,7 @@ void MainWindow::setViewActionsEnabled(bool enable)

void MainWindow::updateProfilesMenu()
{
auto settings = Settings::get();
const auto& settings = SettingsHandler::instance().get();

// Removes obsoletes profiles
for (auto action : mActGroupProfiles->actions())
Expand Down Expand Up @@ -318,8 +318,8 @@ void MainWindow::updateProfilesMenu()

void MainWindow::processAutoConnect()
{
auto settings = Settings::get();
if (settings.profiles.isEmpty())
const auto& settings = SettingsHandler::instance().get();
if (settings.profiles.empty())
{
return;
}
Expand Down Expand Up @@ -387,10 +387,10 @@ void MainWindow::onSessionVersionChanged()
void MainWindow::onActionSettingsTriggered()
{
SettingsDialog settingsDialog(mDesktopEventsMonitor, this);
settingsDialog.setSettings(Settings::get());
settingsDialog.setSettings(SettingsHandler::instance().get());
if (settingsDialog.exec() == QDialog::Accepted)
{
Settings::save(settingsDialog.settings());
SettingsHandler::instance().set(settingsDialog.settings());

showSelectedView();
updateProfilesMenu();
Expand All @@ -410,10 +410,10 @@ void MainWindow::onActionCheckUpdateTriggered()

void MainWindow::onActionOpenHostTriggered()
{
auto settings = Settings::get();
if (settings.isReady())
const auto& settings = SettingsHandler::instance().get();
if (settings.hasValidProfile())
{
QDesktopServices::openUrl(QUrl::fromUserInput(settings.profiles.first().host));
QDesktopServices::openUrl(QUrl::fromUserInput(settings.profiles.front().host));
}
}

Expand All @@ -436,7 +436,7 @@ void MainWindow::onSystemTrayActivated(QSystemTrayIcon::ActivationReason reason)
switch (reason)
{
case QSystemTrayIcon::Trigger: {
auto settings = Settings::get();
const auto& settings = SettingsHandler::instance().get();
if (isVisible() && (settings.kemai.minimizeToSystemTray || settings.kemai.closeToSystemTray))
{
hide();
Expand Down Expand Up @@ -472,9 +472,9 @@ void MainWindow::onNewVersionCheckFinished(const VersionDetails& details)
break;

case QMessageBox::Ignore: {
auto settings = Settings::get();
auto settings = SettingsHandler::instance().get();
settings.kemai.ignoredVersion = details.vn.toString();
Settings::save(settings);
SettingsHandler::instance().set(settings);
}
break;

Expand Down Expand Up @@ -508,7 +508,7 @@ void MainWindow::onProfilesActionGroupTriggered(QAction* action)
{
if (action->isChecked())
{
auto settings = Settings::get();
auto settings = SettingsHandler::instance().get();
auto profileId = action->data().toUuid();
auto profile = settings.findProfile(profileId);
if (profile.has_value())
Expand All @@ -521,7 +521,7 @@ void MainWindow::onProfilesActionGroupTriggered(QAction* action)

void MainWindow::onDesktopIdleDetected()
{
spdlog::info("System is idle since {} minutes. Stop current TimeSheet.", Settings::get().events.idleDelayMinutes);
spdlog::info("System is idle since {} minutes. Stop current TimeSheet.", SettingsHandler::instance().get().events.idleDelayMinutes);
mActivityWidget->stopCurrentTimeSheet();
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/settingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ SettingsDialog::SettingsDialog(const std::shared_ptr<DesktopEventsMonitor>& desk

// show dialog if language changes from settings
connect(mUi->cbLanguage, &QComboBox::currentTextChanged, [&](const QString&) {
auto settings = Settings::get();
const auto& settings = SettingsHandler::instance().get();
if (settings.kemai.language != mUi->cbLanguage->currentData().toLocale())
{
QMessageBox::warning(this, tr(""), tr("Language changed. Application restart is required."));
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int main(int argc, char* argv[])
QApplication::setApplicationVersion(KEMAI_VERSION);

// Get kemai data directory and log file path
auto kemaiSettings = Settings::get();
const auto& kemaiSettings = SettingsHandler::instance().get();

// Create Qt logger model before spdlog sinks
auto loggerTreeModel = std::make_shared<LoggerTreeModel>();
Expand All @@ -64,7 +64,7 @@ int main(int argc, char* argv[])
std::vector<spdlog::sink_ptr> sinks;
sinks.emplace_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
sinks.emplace_back(std::make_shared<LoggerTreeModelSink>(loggerTreeModel));

if (!QDir(helpers::getLogDirPath()).mkpath(".")) // Ensure log dir exists before adding sink
{
sinks.emplace_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(helpers::getLogFilePath().toStdString(), MaxLogFileSize, 3));
Expand Down
2 changes: 1 addition & 1 deletion src/monitor/kimaiEventsMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ bool KimaiEventsMonitor::hasCurrentTimeSheet() const

void KimaiEventsMonitor::onSecondTimeout()
{
auto settings = Settings::get();
const auto& settings = SettingsHandler::instance().get();
if (settings.events.autoRefreshCurrentTimeSheet && mLastTimeSheetUpdate.has_value())
{
if (mLastTimeSheetUpdate->secsTo(QDateTime::currentDateTime()) >= settings.events.autoRefreshCurrentTimeSheetDelaySeconds)
Expand Down
Loading

0 comments on commit dbbd110

Please sign in to comment.