Skip to content

Commit 80a3b86

Browse files
authored
Merge pull request #9061 from DmySyz/preserve-selected-account-between-runs
feat: preserve the last selected account between runs
2 parents 14befed + 6d73994 commit 80a3b86

File tree

6 files changed

+67
-6
lines changed

6 files changed

+67
-6
lines changed

src/gui/accountmanager.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,21 @@ AccountManager::AccountsRestoreResult AccountManager::restore(const bool alsoRes
108108
if (skipSettingsKeys.contains(settings->group())) {
109109
// Should not happen: bad container keys should have been deleted
110110
qCWarning(lcAccountManager) << "Accounts structure is too new, ignoring";
111+
emit(accountListInitialized());
111112
return AccountsRestoreSuccessWithSkipped;
112113
}
113114

114115
// If there are no accounts, check the old format.
115116
#if !DISABLE_ACCOUNT_MIGRATION
116117
if (settings->childGroups().isEmpty() && !settings->contains(QLatin1String(versionC)) && alsoRestoreLegacySettings) {
117118
restoreFromLegacySettings();
119+
emit(accountListInitialized());
118120
return AccountsRestoreSuccessFromLegacyVersion;
119121
}
120122
#endif
121123

122124
if (settings->childGroups().isEmpty()) {
125+
emit(accountListInitialized());
123126
return AccountsNotFound;
124127
}
125128

@@ -149,6 +152,8 @@ AccountManager::AccountsRestoreResult AccountManager::restore(const bool alsoRes
149152
}
150153
}
151154

155+
emit(accountListInitialized());
156+
152157
ConfigFile().cleanupGlobalNetworkConfiguration();
153158
ClientProxy().cleanupGlobalNetworkConfiguration();
154159

src/gui/accountmanager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public slots:
108108
void removeAccountFolders(OCC::AccountState *account);
109109
void forceLegacyImportChanged();
110110
void capabilitiesChanged();
111+
void accountListInitialized();
111112

112113
private:
113114
// saving and loading Account to settings

src/gui/tray/usermodel.cpp

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,13 +1334,14 @@ 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) {
1339-
buildUserList();
1338+
setInitialUser();
13401339
}
13411340

13421341
connect(AccountManager::instance(), &AccountManager::accountAdded,
1343-
this, &UserModel::buildUserList);
1342+
this, &UserModel::addAccsToUserList);
1343+
connect(AccountManager::instance(), &AccountManager::accountListInitialized,
1344+
this, &UserModel::setInitialUser);
13441345
}
13451346

13461347
void UserModel::buildUserList()
@@ -1349,10 +1350,41 @@ void UserModel::buildUserList()
13491350
auto user = AccountManager::instance()->accounts().at(i);
13501351
addUser(user);
13511352
}
1353+
}
1354+
1355+
void UserModel::addAccsToUserList()
1356+
{
13521357
if (_init) {
1353-
_users.first()->setCurrentUser(true);
1354-
_init = false;
1358+
return;
13551359
}
1360+
1361+
buildUserList();
1362+
}
1363+
1364+
void UserModel::setInitialUser()
1365+
{
1366+
if (!_init) {
1367+
return;
1368+
}
1369+
1370+
buildUserList();
1371+
1372+
if(!_users.isEmpty()) {
1373+
ConfigFile cfg;
1374+
const uint lastSelectedAccountId = cfg.lastSelectedAccount();
1375+
1376+
for (int i = 0; i < _users.size(); i++) {
1377+
if (_users.at(i)->account()->id().toUInt() == lastSelectedAccountId) {
1378+
setCurrentUserId(i);
1379+
}
1380+
}
1381+
1382+
if (_currentUserId < 0) {
1383+
setCurrentUserId(0);
1384+
}
1385+
}
1386+
1387+
_init = false;
13561388
}
13571389

13581390
int UserModel::numUsers()
@@ -1430,7 +1462,7 @@ void UserModel::addUser(AccountStatePtr &user, const bool &isCurrent)
14301462
});
14311463

14321464
_users << u;
1433-
if (isCurrent || _currentUserId < 0) {
1465+
if (isCurrent || (_currentUserId < 0 && !_init)) {
14341466
setCurrentUserId(_users.size() - 1);
14351467
}
14361468

@@ -1524,6 +1556,8 @@ void UserModel::setCurrentUserId(const int id)
15241556
// order has changed, index remained the same
15251557
emit currentUserChanged();
15261558
} else if (_currentUserId != id) {
1559+
ConfigFile cfg;
1560+
cfg.setLastSelectedAccount(_users[id]->account()->id().toUInt());
15271561
_currentUserId = id;
15281562
emit currentUserChanged();
15291563
}

src/gui/tray/usermodel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ public slots:
292292
bool _init = true;
293293

294294
void buildUserList();
295+
void addAccsToUserList();
296+
void setInitialUser();
295297
};
296298

297299
class ImageProvider : public QQuickAsyncImageProvider

src/libsync/configfile.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ static const QStringList enterpriseUpdateChannelsList { QStringLiteral("stable")
8383
static const QString defaultEnterpriseChannel = "enterprise";
8484

8585
static constexpr char languageC[] = "language";
86+
87+
static constexpr char lastSelectedAccountC[] = "lastSelectedAccount";
88+
8689
static constexpr int deleteFilesThresholdDefaultValue = 100;
8790
}
8891

@@ -1227,6 +1230,18 @@ void ConfigFile::setLanguage(const QString& language)
12271230
settings.setValue(QLatin1String(languageC), language);
12281231
}
12291232

1233+
uint ConfigFile::lastSelectedAccount() const
1234+
{
1235+
QSettings settings(configFile(), QSettings::IniFormat);
1236+
return settings.value(QLatin1String(lastSelectedAccountC), QLatin1String("")).toUInt();
1237+
}
1238+
1239+
void ConfigFile::setLastSelectedAccount(const uint accountId)
1240+
{
1241+
QSettings settings(configFile(), QSettings::IniFormat);
1242+
settings.setValue(QLatin1String(lastSelectedAccountC), accountId);
1243+
}
1244+
12301245
Q_GLOBAL_STATIC(QString, g_configFileName)
12311246

12321247
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
@@ -242,6 +242,10 @@ class OWNCLOUDSYNC_EXPORT ConfigFile
242242
[[nodiscard]] QString language() const;
243243
void setLanguage(const QString &language);
244244

245+
/// Store and retrieve the last selected account identifier
246+
[[nodiscard]] uint lastSelectedAccount() const;
247+
void setLastSelectedAccount(const uint accountId);
248+
245249
/** Returns a new settings pre-set in a specific group. The Settings will be created
246250
with the given parent. If no parent is specified, the caller must destroy the settings */
247251
static std::unique_ptr<QSettings> settingsWithGroup(const QString &group, QObject *parent = nullptr);

0 commit comments

Comments
 (0)