Skip to content

Commit

Permalink
Still improve settings management
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrePTJ committed Jun 30, 2024
1 parent f1c8511 commit 26c02f9
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 193 deletions.
5 changes: 5 additions & 0 deletions src/client/kimaiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/client/kimaiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
113 changes: 62 additions & 51 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);

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

/*
* 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(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);
});
Expand All @@ -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())
Expand All @@ -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)
Expand All @@ -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<KimaiClient>();
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<KimaiClient>();

mSession = std::make_shared<KemaiSession>(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<KemaiSession>(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()
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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();
Expand All @@ -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()));
}
}

Expand All @@ -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();
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -508,20 +518,21 @@ 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);
}
}
}
}

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();
}

Expand Down
70 changes: 44 additions & 26 deletions src/gui/settingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ SettingsDialog::SettingsDialog(const std::shared_ptr<DesktopEventsMonitor>& 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);
Expand Down 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&) {
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."));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion 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
const auto& kemaiSettings = SettingsHandler::instance().get();
const auto& kemaiSettings = SettingsHelper::load();

// Create Qt logger model before spdlog sinks
auto loggerTreeModel = std::make_shared<LoggerTreeModel>();
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()
{
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)
Expand Down
Loading

0 comments on commit 26c02f9

Please sign in to comment.