Skip to content

Commit f855b69

Browse files
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.
1 parent d7d8883 commit f855b69

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/gui/tray/usermodel.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,6 @@ UserModel *UserModel::instance()
13341334
UserModel::UserModel(QObject *parent)
13351335
: QAbstractListModel(parent)
13361336
{
1337-
// TODO: Remember selected user from last quit via settings file
13381337
if (AccountManager::instance()->accounts().size() > 0) {
13391338
buildUserList();
13401339
}
@@ -1349,8 +1348,34 @@ void UserModel::buildUserList()
13491348
auto user = AccountManager::instance()->accounts().at(i);
13501349
addUser(user);
13511350
}
1352-
if (_init) {
1353-
_users.first()->setCurrentUser(true);
1351+
if (_init && !_users.isEmpty()) {
1352+
// Try to restore the last selected account
1353+
ConfigFile cfg;
1354+
const auto lastSelectedAccountId = cfg.lastSelectedAccount();
1355+
1356+
if (!lastSelectedAccountId.isEmpty()) {
1357+
// Find the account by id (more stable than displayName)
1358+
int foundIndex = -1;
1359+
for (int i = 0; i < _users.size(); i++) {
1360+
if (_users[i]->account()->id() == lastSelectedAccountId) {
1361+
foundIndex = i;
1362+
break;
1363+
}
1364+
}
1365+
1366+
if (foundIndex >= 0 && foundIndex < _users.size()) {
1367+
setCurrentUserId(foundIndex);
1368+
_init = false;
1369+
return;
1370+
}
1371+
}
1372+
1373+
// Fallback to first account if last selected account not found
1374+
setCurrentUserId(0);
1375+
_init = false;
1376+
} else if (_init && _users.isEmpty()) {
1377+
// No accounts available - ensure initialization is complete
1378+
// _currentUserId remains -1 (no account selected)
13541379
_init = false;
13551380
}
13561381
}
@@ -1525,6 +1550,13 @@ void UserModel::setCurrentUserId(const int id)
15251550
emit currentUserChanged();
15261551
} else if (_currentUserId != id) {
15271552
_currentUserId = id;
1553+
1554+
// Save the last selected account identifier (but not during initialization)
1555+
if (!_init) {
1556+
ConfigFile cfg;
1557+
cfg.setLastSelectedAccount(_users[id]->account()->id());
1558+
}
1559+
15281560
emit currentUserChanged();
15291561
}
15301562
}

src/libsync/configfile.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ static const QStringList enterpriseUpdateChannelsList { QStringLiteral("stable")
9595
static const QString defaultEnterpriseChannel = "enterprise";
9696

9797
static constexpr char languageC[] = "language";
98+
static constexpr char lastSelectedAccountC[] = "lastSelectedAccount";
9899

99100
static constexpr int deleteFilesThresholdDefaultValue = 100;
100101
}
@@ -1255,6 +1256,18 @@ void ConfigFile::setLanguage(const QString& language)
12551256
settings.setValue(QLatin1String(languageC), language);
12561257
}
12571258

1259+
QString ConfigFile::lastSelectedAccount() const
1260+
{
1261+
QSettings settings(configFile(), QSettings::IniFormat);
1262+
return settings.value(QLatin1String(lastSelectedAccountC), QLatin1String("")).toString();
1263+
}
1264+
1265+
void ConfigFile::setLastSelectedAccount(const QString &accountIdentifier)
1266+
{
1267+
QSettings settings(configFile(), QSettings::IniFormat);
1268+
settings.setValue(QLatin1String(lastSelectedAccountC), accountIdentifier);
1269+
}
1270+
12581271
Q_GLOBAL_STATIC(QString, g_configFileName)
12591272

12601273
std::unique_ptr<QSettings> ConfigFile::settingsWithGroup(const QString &group, QObject *parent)

src/libsync/configfile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ class OWNCLOUDSYNC_EXPORT ConfigFile
238238
[[nodiscard]] QString language() const;
239239
void setLanguage(const QString &language);
240240

241+
/// Store and retrieve the last selected account identifier
242+
[[nodiscard]] QString lastSelectedAccount() const;
243+
void setLastSelectedAccount(const QString &accountIdentifier);
244+
241245
/** Returns a new settings pre-set in a specific group. The Settings will be created
242246
with the given parent. If no parent is specified, the caller must destroy the settings */
243247
static std::unique_ptr<QSettings> settingsWithGroup(const QString &group, QObject *parent = nullptr);

0 commit comments

Comments
 (0)