Skip to content

Commit 2e8a4db

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 2e8a4db

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/gui/tray/usermodel.cpp

Lines changed: 18 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
}
@@ -1343,14 +1342,26 @@ UserModel::UserModel(QObject *parent)
13431342
this, &UserModel::buildUserList);
13441343
}
13451344

1345+
// TODO: refactor buildUserList usage. Currently it is called for every account
13461346
void UserModel::buildUserList()
13471347
{
13481348
for (int i = 0; i < AccountManager::instance()->accounts().size(); i++) {
13491349
auto user = AccountManager::instance()->accounts().at(i);
13501350
addUser(user);
13511351
}
1352+
1353+
// TODO: consider generating random IDs in the future instead of 0, 1, 2 etc
1354+
if(!_users.isEmpty()) {
1355+
ConfigFile cfg;
1356+
const int lastSelectedAccountId = cfg.lastSelectedAccount();
1357+
if(lastSelectedAccountId < _users.size() && lastSelectedAccountId >= 0) {
1358+
setCurrentUserId(lastSelectedAccountId);
1359+
} else {
1360+
setCurrentUserId(0);
1361+
}
1362+
}
1363+
13521364
if (_init) {
1353-
_users.first()->setCurrentUser(true);
13541365
_init = false;
13551366
}
13561367
}
@@ -1524,6 +1535,11 @@ void UserModel::setCurrentUserId(const int id)
15241535
// order has changed, index remained the same
15251536
emit currentUserChanged();
15261537
} else if (_currentUserId != id) {
1538+
// avoid overwriting as it will always be set to 0 at first
1539+
if (_currentUserId >= 0) {
1540+
ConfigFile cfg;
1541+
cfg.setLastSelectedAccount(id);
1542+
}
15271543
_currentUserId = id;
15281544
emit currentUserChanged();
15291545
}

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+
int ConfigFile::lastSelectedAccount() const
1261+
{
1262+
QSettings settings(configFile(), QSettings::IniFormat);
1263+
return settings.value(QLatin1String(lastSelectedAccountC), QLatin1String("")).toInt();
1264+
}
1265+
1266+
void ConfigFile::setLastSelectedAccount(const int 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]] int lastSelectedAccount() const;
243+
void setLastSelectedAccount(const int 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)