Skip to content

Commit fb9fd94

Browse files
committed
feat: preserve the last selected account between runs
Added an extra setting in the config that contains an account id to store the last one selected Modified usermodel logic to auto select the account using the id from the config Modified the account manager and user model to init user list after account restoration completes Signed-off-by: Dmytro Syzov <[email protected]>
1 parent 6e621f4 commit fb9fd94

File tree

6 files changed

+66
-6
lines changed

6 files changed

+66
-6
lines changed

src/gui/accountmanager.cpp

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

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

121123
if (settings->childGroups().isEmpty()) {
124+
emit(accountListInitialized());
122125
return AccountsNotFound;
123126
}
124127

@@ -148,6 +151,8 @@ AccountManager::AccountsRestoreResult AccountManager::restore(const bool alsoRes
148151
}
149152
}
150153

154+
emit(accountListInitialized());
155+
151156
ConfigFile().cleanupGlobalNetworkConfiguration();
152157
ClientProxy().cleanupGlobalNetworkConfiguration();
153158

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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ static const QString defaultEnterpriseChannel = "enterprise";
9696

9797
static constexpr char languageC[] = "language";
9898

99+
static constexpr char lastSelectedAccountC[] = "lastSelectedAccount";
100+
99101
static constexpr int deleteFilesThresholdDefaultValue = 100;
100102
}
101103

@@ -1255,6 +1257,18 @@ void ConfigFile::setLanguage(const QString& language)
12551257
settings.setValue(QLatin1String(languageC), language);
12561258
}
12571259

1260+
uint ConfigFile::lastSelectedAccount() const
1261+
{
1262+
QSettings settings(configFile(), QSettings::IniFormat);
1263+
return settings.value(QLatin1String(lastSelectedAccountC), QLatin1String("")).toUInt();
1264+
}
1265+
1266+
void ConfigFile::setLastSelectedAccount(const uint accountId)
1267+
{
1268+
QSettings settings(configFile(), QSettings::IniFormat);
1269+
settings.setValue(QLatin1String(lastSelectedAccountC), accountId);
1270+
}
1271+
12581272
Q_GLOBAL_STATIC(QString, g_configFileName)
12591273

12601274
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]] uint lastSelectedAccount() const;
243+
void setLastSelectedAccount(const uint accountId);
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)