Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AccountManager keep unique_ptr #10993

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
24 changes: 12 additions & 12 deletions src/gui/accountmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,15 @@ AccountStatePtr AccountManager::account(const QString &name)
{
for (const auto &acc : qAsConst(_accounts)) {
if (acc->account()->displayName() == name) {
return acc;
return acc.get();
}
}
return AccountStatePtr();
}

AccountStatePtr AccountManager::account(const QUuid uuid) {
return _accounts.value(uuid);
// assumes we always find one
return std::find_if(_accounts.cbegin(), _accounts.cend(), [uuid](const auto &accountState) { return accountState->account()->uuid() == uuid; })->get();
}

AccountStatePtr AccountManager::addAccount(const AccountPtr &newAccount)
Expand All @@ -369,12 +370,13 @@ AccountStatePtr AccountManager::addAccount(const AccountPtr &newAccount)

void AccountManager::deleteAccount(AccountStatePtr account)
{
auto it = std::find(_accounts.begin(), _accounts.end(), account);
if (it == _accounts.end()) {
auto it = std::find_if(_accounts.cbegin(), _accounts.cend(), [&](const auto &accountState) { return account.get() == accountState.get(); });
if (it == _accounts.cend()) {
return;
}
// The argument keeps a strong reference to the AccountState, so we can safely remove other
// AccountStatePtr occurrences:

// this unique ptr will delete the object once we leave the scope
auto accountStateToBeRemoved = std::move(_accounts.at(std::distance(_accounts.cbegin(), it)));
_accounts.erase(it);

// Forget account credentials, cookies
Expand All @@ -385,7 +387,6 @@ void AccountManager::deleteAccount(AccountStatePtr account)
settings->remove(account->account()->id());

emit accountRemoved(account);
account->deleteLater();
}

AccountPtr AccountManager::createAccount(const QUuid &uuid)
Expand All @@ -398,7 +399,7 @@ void AccountManager::shutdown()
{
const auto accounts = std::move(_accounts);
for (const auto &acc : accounts) {
emit accountRemoved(acc);
emit accountRemoved(acc.get());
}
}

Expand Down Expand Up @@ -434,9 +435,8 @@ AccountStatePtr AccountManager::addAccountState(std::unique_ptr<AccountState> &&
saveAccount(rawAccount, false);
});

AccountStatePtr statePtr = accountState.release();
_accounts.insert(statePtr->account()->uuid(), statePtr);
emit accountAdded(statePtr);
return statePtr;
_accounts.push_back(std::move(accountState));
emit accountAdded(_accounts.back().get());
return _accounts.back().get();
}
}
4 changes: 2 additions & 2 deletions src/gui/accountmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class AccountManager : public QObject
* Return a map of all accounts.
* (this is a map of QSharedPointer for internal reasons, one should normally not keep a copy of them)
*/
const QMap<QUuid, AccountStatePtr> &accounts() { return _accounts; }
const auto &accounts() { return _accounts; }

/**
* Return the account state pointer for an account identified by its display name
Expand Down Expand Up @@ -116,7 +116,7 @@ public slots:

private:
AccountManager() {}
QMap<QUuid, AccountStatePtr> _accounts;
std::vector<std::unique_ptr<AccountState>> _accounts;
/// Account ids from settings that weren't read
QSet<QString> _additionalBlockedAccountIds;
};
Expand Down
4 changes: 2 additions & 2 deletions src/gui/activitywidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ void ActivityWidget::slotAccountActivityStatus(AccountStatePtr ast, int statusCo

void ActivityWidget::checkActivityTabVisibility()
{
int accountCount = AccountManager::instance()->accounts().count();
int accountCount = AccountManager::instance()->accounts().size();
bool hasAccountsWithActivity =
_accountsWithoutActivities.count() != accountCount;
bool hasNotifications = !_widgetForNotifId.isEmpty();
Expand Down Expand Up @@ -568,7 +568,7 @@ void ActivitySettings::slotRefresh(AccountStatePtr ptr)
void ActivitySettings::slotRegularNotificationCheck()
{
for (const auto &a : AccountManager::instance()->accounts()) {
slotRefresh(a);
slotRefresh(a.get());
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/gui/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ Application::Application(Platform *platform, bool debugMode, QObject *parent)
connect(AccountManager::instance(), &AccountManager::accountAdded, this, &Application::slotAccountStateAdded);
connect(AccountManager::instance(), &AccountManager::accountRemoved, this, &Application::slotAccountStateRemoved);
for (const auto &ai : AccountManager::instance()->accounts()) {
slotAccountStateAdded(ai);
slotAccountStateAdded(ai.get());
}

connect(FolderMan::instance()->socketApi(), &SocketApi::shareCommandReceived, _gui.data(), &ownCloudGui::slotShowShareDialog);
Expand All @@ -255,7 +255,7 @@ Application::Application(Platform *platform, bool debugMode, QObject *parent)
}
#endif

if (AccountManager::instance()->accounts().isEmpty()) {
if (AccountManager::instance()->accounts().empty()) {
// display the wizard if we don't have an account yet
QTimer::singleShot(0, gui(), &ownCloudGui::runNewAccountWizard);
}
Expand All @@ -273,7 +273,7 @@ Application::~Application()
void Application::slotAccountStateRemoved() const
{
// if there is no more account, show the wizard.
if (_gui && AccountManager::instance()->accounts().isEmpty()) {
if (_gui && AccountManager::instance()->accounts().empty()) {
// allow to add a new account if there is non any more. Always think
// about single account theming!
gui()->runNewAccountWizard();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/folderman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ int FolderMan::setupFolders()
// Should not happen: bad container keys should have been deleted
qCWarning(lcFolderMan) << "Folder structure" << groupName << "is too new, ignoring";
} else {
setupFoldersHelper(*settings, account, skipSettingsKeys, backwardsCompatible, foldersWithPlaceholders);
setupFoldersHelper(*settings, account.get(), skipSettingsKeys, backwardsCompatible, foldersWithPlaceholders);
}
settings->endGroup();
};
Expand Down
24 changes: 11 additions & 13 deletions src/gui/models/activitylistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,9 @@ bool ActivityListModel::canFetchMore(const QModelIndex &) const
if (_activityLists.isEmpty())
return true;

for (auto i = _activityLists.begin(); i != _activityLists.end(); ++i) {
AccountStatePtr accountState = i.key();
for (const auto &[accountState, activityList] : Utility::asKeyValueRange(_activityLists)) {
if (accountState && accountState->isConnected()) {
ActivityList activities = i.value();
if (activities.count() == 0 && !_currentlyFetching.contains(accountState)) {
if (activityList.count() == 0 && !_currentlyFetching.contains(accountState)) {
return true;
}
}
Expand Down Expand Up @@ -218,7 +216,7 @@ void ActivityListModel::startFetchJob(AccountStatePtr ast)
QDateTime::fromString(json.value(QStringLiteral("date")).toString(), Qt::ISODate)});
}

_activityLists[ast] = std::move(list);
_activityLists[ast.get()] = std::move(list);

emit activityJobStatusCode(ast, job->ocsStatus());

Expand Down Expand Up @@ -249,25 +247,25 @@ void ActivityListModel::setActivityList(const ActivityList &&resultList)

void ActivityListModel::fetchMore(const QModelIndex &)
{
for (const AccountStatePtr &asp : AccountManager::instance()->accounts()) {
if (!_activityLists.contains(asp) && asp->isConnected()) {
_activityLists[asp] = ActivityList();
startFetchJob(asp);
for (const auto &accountState : AccountManager::instance()->accounts()) {
if (!_activityLists.contains(accountState.get()) && accountState->isConnected()) {
_activityLists[accountState.get()] = ActivityList();
startFetchJob(accountState.get());
}
}
}

void ActivityListModel::slotRefreshActivity(const AccountStatePtr &ast)
{
if (ast && _activityLists.contains(ast)) {
_activityLists.remove(ast);
if (ast && _activityLists.contains(ast.get())) {
_activityLists.remove(ast.get());
}
startFetchJob(ast);
}

void ActivityListModel::slotRemoveAccount(AccountStatePtr ast)
{
if (_activityLists.contains(ast)) {
if (_activityLists.contains(ast.get())) {
const auto accountToRemove = ast->account()->uuid();

QMutableListIterator<Activity> it(_finalList);
Expand All @@ -283,7 +281,7 @@ void ActivityListModel::slotRemoveAccount(AccountStatePtr ast)
++i;
}
}
_activityLists.remove(ast);
_activityLists.remove(ast.get());
_currentlyFetching.remove(ast);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/models/activitylistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public slots:
void startFetchJob(AccountStatePtr s);
void combineActivityLists();

QMap<AccountStatePtr, ActivityList> _activityLists;
QMap<AccountState *, ActivityList> _activityLists;
ActivityList _finalList;
QSet<AccountStatePtr> _currentlyFetching;

Expand Down
2 changes: 1 addition & 1 deletion src/gui/networksettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void NetworkSettings::saveProxySettings()
// start the sync.
FolderMan::instance()->setDirtyProxy();

for (auto account : AccountManager::instance()->accounts()) {
for (auto &account : AccountManager::instance()->accounts()) {
account->freshConnectionAttempt();
}
}
Expand Down
48 changes: 24 additions & 24 deletions src/gui/owncloudgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,23 @@

using namespace std::chrono_literals;

using namespace OCC;

namespace {

using namespace OCC;
template <typename T>
void setPauseOnAllFoldersHelper(const std::vector<T> &accounts, bool pause)
{
for (auto *f : FolderMan::instance()->folders()) {
auto it = std::find_if(accounts.cbegin(), accounts.cend(), [f](const auto &accountState) { return accountState.get() == f->accountState(); });
if (it != accounts.cend()) {
f->setSyncPaused(pause);
if (pause) {
f->slotTerminateSync();
}
}
}
}

void setUpInitialSyncFolder(AccountStatePtr accountStatePtr, bool useVfs)
{
Expand Down Expand Up @@ -156,7 +170,7 @@ ownCloudGui::~ownCloudGui()
void ownCloudGui::slotOpenSettingsDialog()
{
// if account is set up, start the configuration wizard.
if (!AccountManager::instance()->accounts().isEmpty()) {
if (!AccountManager::instance()->accounts().empty()) {
if (QApplication::activeWindow() != _settingsDialog) {
slotShowSettings();
} else {
Expand Down Expand Up @@ -260,7 +274,7 @@ void ownCloudGui::slotComputeOverallSyncStatus()
allSignedOut = false;
}
if (!a->isConnected()) {
problemAccounts.append(a);
problemAccounts.append(a.get());
} else {
allDisconnected = false;
}
Expand Down Expand Up @@ -387,9 +401,9 @@ void ownCloudGui::addAccountContextMenu(AccountStatePtr accountState, QMenu *men

menu->addSeparator();
if (onePaused) {
menu->addAction(tr("Resume synchronization"), this, [accountState, this] { setPauseOnAllFoldersHelper({accountState}, false); });
menu->addAction(tr("Resume synchronization"), this, [accountState, this] { setPauseOnAllFoldersHelper<AccountStatePtr>({accountState}, false); });
} else {
menu->addAction(tr("Stop synchronization"), this, [accountState, this] { setPauseOnAllFoldersHelper({accountState}, true); });
menu->addAction(tr("Stop synchronization"), this, [accountState, this] { setPauseOnAllFoldersHelper<AccountStatePtr>({accountState}, true); });
}

if (accountState->isSignedOut()) {
Expand Down Expand Up @@ -596,7 +610,7 @@ void ownCloudGui::updateContextMenu()

const auto &accountList = AccountManager::instance()->accounts();

bool isConfigured = (!accountList.isEmpty());
bool isConfigured = (!accountList.empty());
bool atLeastOneConnected = false;
bool atLeastOnePaused = false;
bool atLeastOneNotPaused = false;
Expand All @@ -617,15 +631,13 @@ void ownCloudGui::updateContextMenu()
_contextMenu->addAction(Theme::instance()->applicationIcon(), tr("Show %1").arg(Theme::instance()->appNameGUI()), this, &ownCloudGui::slotShowSettings);
_contextMenu->addSeparator();
if (atLeastOnePaused) {
_contextMenu->addAction(
tr("Resume synchronization"), this, [this] { setPauseOnAllFoldersHelper(AccountManager::instance()->accounts().values(), false); });
_contextMenu->addAction(tr("Resume synchronization"), this, [this] { setPauseOnAllFoldersHelper(AccountManager::instance()->accounts(), false); });
} else {
_contextMenu->addAction(
tr("Stop synchronization"), this, [this] { setPauseOnAllFoldersHelper(AccountManager::instance()->accounts().values(), true); });
_contextMenu->addAction(tr("Stop synchronization"), this, [this] { setPauseOnAllFoldersHelper(AccountManager::instance()->accounts(), true); });
}
_contextMenu->addSeparator();

if (accountList.isEmpty()) {
if (accountList.empty()) {
_contextMenu->addAction(tr("Create a new account"), this, &ownCloudGui::runNewAccountWizard);
} else {
// submenus for accounts
Expand All @@ -634,7 +646,7 @@ void ownCloudGui::updateContextMenu()
_accountMenus.append(accountMenu);
_contextMenu->addMenu(accountMenu);

addAccountContextMenu(account, accountMenu);
addAccountContextMenu(account.get(), accountMenu);
}
}

Expand Down Expand Up @@ -938,18 +950,6 @@ void ownCloudGui::runNewAccountWizard()
}
}

void ownCloudGui::setPauseOnAllFoldersHelper(const QList<AccountStatePtr> &accounts, bool pause)
{
for (auto *f : FolderMan::instance()->folders()) {
if (accounts.contains(f->accountState())) {
f->setSyncPaused(pause);
if (pause) {
f->slotTerminateSync();
}
}
}
}

void ownCloudGui::slotShowSettings()
{
raiseDialog(_settingsDialog);
Expand Down
1 change: 0 additions & 1 deletion src/gui/owncloudgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ public slots:
void slotRemoveDestroyedShareDialogs();

private:
void setPauseOnAllFoldersHelper(const QList<AccountStatePtr> &accounts, bool pause);
void setupActions();
void addAccountContextMenu(AccountStatePtr accountState, QMenu *menu);

Expand Down
2 changes: 1 addition & 1 deletion src/gui/settingsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ SettingsDialog::SettingsDialog(ownCloudGui *gui, QWidget *parent)
connect(AccountManager::instance(), &AccountManager::accountRemoved,
this, &SettingsDialog::accountRemoved);
for (const auto &ai : AccountManager::instance()->accounts()) {
accountAdded(ai);
accountAdded(ai.get());
}

QTimer::singleShot(0, this, &SettingsDialog::showFirstPage);
Expand Down
2 changes: 1 addition & 1 deletion src/gui/socketapi/socketapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ Q_INVOKABLE void OCC::SocketApi::command_OPEN_APP_LINK(const QString &localFile,
void SocketApi::command_V2_LIST_ACCOUNTS(const QSharedPointer<SocketApiJobV2> &job) const
{
QJsonArray out;
for (auto acc : AccountManager::instance()->accounts()) {
for (const auto &acc : AccountManager::instance()->accounts()) {
out << QJsonObject({ { QStringLiteral("name"), acc->account()->displayName() },
{ QStringLiteral("id"), acc->account()->id() },
{ QStringLiteral("uuid"), acc->account()->uuid().toString(QUuid::WithoutBraces) } });
Expand Down
2 changes: 1 addition & 1 deletion test/modeltests/testactivitymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private Q_SLOTS:
Activity{Activity::ActivityType, QStringLiteral("021ad48a-80ae-4af6-b878-aeb836bd367d"), acc2, QStringLiteral("test"), QStringLiteral("test"),
QStringLiteral("foo.cpp"), QUrl(QStringLiteral("https://owncloud.com")), QDateTime::currentDateTime()},
});
model->slotRemoveAccount(AccountManager::instance()->accounts().first());
model->slotRemoveAccount(AccountManager::instance()->accounts().front().get());
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion test/testfoldermigration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private slots:
AccountManager::instance()->restore();

settings->beginGroup(QStringLiteral("0/Folders"));
TestUtils::folderMan()->setupFoldersHelper(*settings.get(), AccountManager::instance()->accounts().first(), {}, true, false);
TestUtils::folderMan()->setupFoldersHelper(*settings.get(), AccountManager::instance()->accounts().front().get(), {}, true, false);
settings->endGroup();

QCOMPARE(journalPaths.first(), settings->value(QStringLiteral("0/Folders/1/journalPath")));
Expand Down