Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/gui/tray/usermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,6 @@ UserModel *UserModel::instance()
UserModel::UserModel(QObject *parent)
: QAbstractListModel(parent)
{
// TODO: Remember selected user from last quit via settings file
if (AccountManager::instance()->accounts().size() > 0) {
buildUserList();
}
Expand All @@ -1349,8 +1348,34 @@ void UserModel::buildUserList()
auto user = AccountManager::instance()->accounts().at(i);
addUser(user);
}
if (_init) {
_users.first()->setCurrentUser(true);
if (_init && !_users.isEmpty()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Complexity could be reduced here a bit, but more importantly, return at the middle might not be optimal. If later we add some more functionality after the _init setup, it's very easy to miss that return.

Suggested change
if (_init && !_users.isEmpty()) {
if (_init) {
if(!_users.isEmpty()) {
// Try to restore the last selected account
ConfigFile cfg;
const auto lastSelectedAccountId = cfg.lastSelectedAccount();
bool lastSelectedAccountFound = false;
if (!lastSelectedAccountId.isEmpty()) {
// Find the account by id (more stable than displayName)
int foundIndex = -1;
for (int i = 0; i < _users.size(); i++) {
if (_users[i]->account()->id() == lastSelectedAccountId) {
foundIndex = i;
break;
}
}
if (foundIndex >= 0 && foundIndex < _users.size()) {
setCurrentUserId(foundIndex);
lastSelectedAccountFound = true;
}
}
if(!lastSelectedAccountFound ) {
// Fallback to first account if last selected account not found
setCurrentUserId(0);
}
}
_init = false;
}

// Try to restore the last selected account
ConfigFile cfg;
const auto lastSelectedAccountId = cfg.lastSelectedAccount();

if (!lastSelectedAccountId.isEmpty()) {
// Find the account by id (more stable than displayName)
int foundIndex = -1;
for (int i = 0; i < _users.size(); i++) {
if (_users[i]->account()->id() == lastSelectedAccountId) {
foundIndex = i;
break;
}
}

if (foundIndex >= 0 && foundIndex < _users.size()) {
setCurrentUserId(foundIndex);
_init = false;
return;
}
}

// Fallback to first account if last selected account not found
setCurrentUserId(0);
_init = false;
} else if (_init && _users.isEmpty()) {
// No accounts available - ensure initialization is complete
// _currentUserId remains -1 (no account selected)
_init = false;
}
}
Expand Down Expand Up @@ -1525,6 +1550,13 @@ void UserModel::setCurrentUserId(const int id)
emit currentUserChanged();
} else if (_currentUserId != id) {
_currentUserId = id;

// Save the last selected account identifier (but not during initialization)
if (!_init) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should save the last selected account id even during initialization. What if the last selected account was an account that has been deleted somehow? Then the newly selected account won't be saved to config. Not a practical issue, but for the sake of consistency, I would remove this condition.

ConfigFile cfg;
cfg.setLastSelectedAccount(_users[id]->account()->id());
}

emit currentUserChanged();
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/libsync/configfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ static const QStringList enterpriseUpdateChannelsList { QStringLiteral("stable")
static const QString defaultEnterpriseChannel = "enterprise";

static constexpr char languageC[] = "language";
static constexpr char lastSelectedAccountC[] = "lastSelectedAccount";

static constexpr int deleteFilesThresholdDefaultValue = 100;
}
Expand Down Expand Up @@ -1255,6 +1256,18 @@ void ConfigFile::setLanguage(const QString& language)
settings.setValue(QLatin1String(languageC), language);
}

QString ConfigFile::lastSelectedAccount() const
{
QSettings settings(configFile(), QSettings::IniFormat);
return settings.value(QLatin1String(lastSelectedAccountC), QLatin1String("")).toString();
}

void ConfigFile::setLastSelectedAccount(const QString &accountIdentifier)
{
QSettings settings(configFile(), QSettings::IniFormat);
settings.setValue(QLatin1String(lastSelectedAccountC), accountIdentifier);
}

Q_GLOBAL_STATIC(QString, g_configFileName)

std::unique_ptr<QSettings> ConfigFile::settingsWithGroup(const QString &group, QObject *parent)
Expand Down
4 changes: 4 additions & 0 deletions src/libsync/configfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ class OWNCLOUDSYNC_EXPORT ConfigFile
[[nodiscard]] QString language() const;
void setLanguage(const QString &language);

/// Store and retrieve the last selected account identifier
[[nodiscard]] QString lastSelectedAccount() const;
void setLastSelectedAccount(const QString &accountIdentifier);

/** Returns a new settings pre-set in a specific group. The Settings will be created
with the given parent. If no parent is specified, the caller must destroy the settings */
static std::unique_ptr<QSettings> settingsWithGroup(const QString &group, QObject *parent = nullptr);
Expand Down