Skip to content

Commit 1de5dd4

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 Signed-off-by: Dmytro Syzov <[email protected]>
1 parent 9ecd746 commit 1de5dd4

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

src/gui/tray/usermodel.cpp

Lines changed: 21 additions & 2 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,23 @@ void UserModel::buildUserList()
13491348
auto user = AccountManager::instance()->accounts().at(i);
13501349
addUser(user);
13511350
}
1351+
1352+
if(!_users.isEmpty()) {
1353+
ConfigFile cfg;
1354+
const uint lastSelectedAccountId = cfg.lastSelectedAccount();
1355+
1356+
for (int i = 0; i < _users.size(); i++) {
1357+
if (_users.at(i)->account()->id().toUInt() == lastSelectedAccountId) {
1358+
setCurrentUserId(i);
1359+
}
1360+
}
1361+
1362+
if (_currentUserId < 0) {
1363+
setCurrentUserId(0);
1364+
}
1365+
}
1366+
13521367
if (_init) {
1353-
_users.first()->setCurrentUser(true);
13541368
_init = false;
13551369
}
13561370
}
@@ -1524,6 +1538,11 @@ void UserModel::setCurrentUserId(const int id)
15241538
// order has changed, index remained the same
15251539
emit currentUserChanged();
15261540
} else if (_currentUserId != id) {
1541+
// avoid overwriting as it will always be set to 0 at first
1542+
if (_currentUserId >= 0) {
1543+
ConfigFile cfg;
1544+
cfg.setLastSelectedAccount(_users[id]->account()->id().toUInt());
1545+
}
15271546
_currentUserId = id;
15281547
emit currentUserChanged();
15291548
}

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)