From f855b692e03124a53ee20914faa113ef68d37e1b Mon Sep 17 00:00:00 2001 From: harshasiddartha Date: Fri, 31 Oct 2025 23:21:55 +0530 Subject: [PATCH] fix(gui): remember last selected account after client restart Fixes #8986 - Add ConfigFile methods to save/load last selected account identifier - Save account id when user changes accounts (not during initialization) - Restore last selected account on startup - Fallback to first account if saved account was deleted - Handle empty accounts case explicitly Uses account()->id() as a stable identifier and prevents saving during initialization to avoid redundant writes. --- src/gui/tray/usermodel.cpp | 38 +++++++++++++++++++++++++++++++++++--- src/libsync/configfile.cpp | 13 +++++++++++++ src/libsync/configfile.h | 4 ++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/gui/tray/usermodel.cpp b/src/gui/tray/usermodel.cpp index db883e7ed7a8f..7ab3ddc05d672 100644 --- a/src/gui/tray/usermodel.cpp +++ b/src/gui/tray/usermodel.cpp @@ -1334,7 +1334,6 @@ UserModel *UserModel::instance() UserModel::UserModel(QObject *parent) : QAbstractListModel(parent) { - // TODO: Remember selected user from last quit via settings file if (AccountManager::instance()->accounts().size() > 0) { buildUserList(); } @@ -1349,8 +1348,34 @@ void UserModel::buildUserList() auto user = AccountManager::instance()->accounts().at(i); addUser(user); } - if (_init) { - _users.first()->setCurrentUser(true); + if (_init && !_users.isEmpty()) { + // Try to restore the last selected account + ConfigFile cfg; + const auto lastSelectedAccountId = cfg.lastSelectedAccount(); + + if (!lastSelectedAccountId.isEmpty()) { + // Find the account by id (more stable than displayName) + int foundIndex = -1; + for (int i = 0; i < _users.size(); i++) { + if (_users[i]->account()->id() == lastSelectedAccountId) { + foundIndex = i; + break; + } + } + + if (foundIndex >= 0 && foundIndex < _users.size()) { + setCurrentUserId(foundIndex); + _init = false; + return; + } + } + + // Fallback to first account if last selected account not found + setCurrentUserId(0); + _init = false; + } else if (_init && _users.isEmpty()) { + // No accounts available - ensure initialization is complete + // _currentUserId remains -1 (no account selected) _init = false; } } @@ -1525,6 +1550,13 @@ void UserModel::setCurrentUserId(const int id) emit currentUserChanged(); } else if (_currentUserId != id) { _currentUserId = id; + + // Save the last selected account identifier (but not during initialization) + if (!_init) { + ConfigFile cfg; + cfg.setLastSelectedAccount(_users[id]->account()->id()); + } + emit currentUserChanged(); } } diff --git a/src/libsync/configfile.cpp b/src/libsync/configfile.cpp index 9f9ad7df61270..810b18fa6b41c 100644 --- a/src/libsync/configfile.cpp +++ b/src/libsync/configfile.cpp @@ -95,6 +95,7 @@ static const QStringList enterpriseUpdateChannelsList { QStringLiteral("stable") static const QString defaultEnterpriseChannel = "enterprise"; static constexpr char languageC[] = "language"; +static constexpr char lastSelectedAccountC[] = "lastSelectedAccount"; static constexpr int deleteFilesThresholdDefaultValue = 100; } @@ -1255,6 +1256,18 @@ void ConfigFile::setLanguage(const QString& language) settings.setValue(QLatin1String(languageC), language); } +QString ConfigFile::lastSelectedAccount() const +{ + QSettings settings(configFile(), QSettings::IniFormat); + return settings.value(QLatin1String(lastSelectedAccountC), QLatin1String("")).toString(); +} + +void ConfigFile::setLastSelectedAccount(const QString &accountIdentifier) +{ + QSettings settings(configFile(), QSettings::IniFormat); + settings.setValue(QLatin1String(lastSelectedAccountC), accountIdentifier); +} + Q_GLOBAL_STATIC(QString, g_configFileName) std::unique_ptr ConfigFile::settingsWithGroup(const QString &group, QObject *parent) diff --git a/src/libsync/configfile.h b/src/libsync/configfile.h index dd865020272b1..b556374674fc4 100644 --- a/src/libsync/configfile.h +++ b/src/libsync/configfile.h @@ -238,6 +238,10 @@ class OWNCLOUDSYNC_EXPORT ConfigFile [[nodiscard]] QString language() const; void setLanguage(const QString &language); + /// Store and retrieve the last selected account identifier + [[nodiscard]] QString lastSelectedAccount() const; + void setLastSelectedAccount(const QString &accountIdentifier); + /** Returns a new settings pre-set in a specific group. The Settings will be created with the given parent. If no parent is specified, the caller must destroy the settings */ static std::unique_ptr settingsWithGroup(const QString &group, QObject *parent = nullptr);