diff options
author | Erik Verbruggen <erik@verbruggen.consulting> | 2021-10-15 15:05:01 +0300 |
---|---|---|
committer | Erik Verbruggen <Erik.Verbruggen@Me.com> | 2021-12-03 12:45:08 +0300 |
commit | 470bf76e6362a4379e6548e466971e3a2e532ccd (patch) | |
tree | f697e03da9fde7e1d97394d867c6c3eed7f314d9 /src/gui/folderman.cpp | |
parent | ed603153da4fdc385f90982c88660fe0cec2f7ea (diff) |
Use the refcounted AccountStatePtr as much as possible
The AccountManager creates AccountState objects, and stores them in a
shared pointer.
Previously, the raw pointer was given out, and stored in other objects.
That made removal very tricky: when an account gets removed, the
underlying object gets deleted, and then all classes that listen get
notified of the deletion. Those classes would sometimes put a nullptr
into the AccountState pointer they stored, and in each usage would
(hopefully) check for a nullptr. The problem was that a number of checks
were missing, which the clang static analyser pointed out.
This patch changes nearly all uses of a raw pointer into the shared
pointer, thereby making sure all usages have a valid reference, even
when account deletion happens. The two places where a raw pointer is
still used, now put it into a refcounted pointer as soon as possible.
Diffstat (limited to 'src/gui/folderman.cpp')
-rw-r--r-- | src/gui/folderman.cpp | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/src/gui/folderman.cpp b/src/gui/folderman.cpp index 65046642e..62ce4fbd5 100644 --- a/src/gui/folderman.cpp +++ b/src/gui/folderman.cpp @@ -359,8 +359,7 @@ void FolderMan::setupFoldersHelper(QSettings &settings, AccountStatePtr account, qFatal("Could not load plugin"); } - Folder *f = addFolderInternal(std::move(folderDefinition), account.data(), std::move(vfs)); - if (f) { + if (Folder *f = addFolderInternal(std::move(folderDefinition), account, std::move(vfs))) { // Migrate the old "usePlaceholders" setting to the root folder pin state if (settings.value(versionC(), 1).toInt() == 1 && settings.value(QLatin1String("usePlaceholders"), false).toBool()) { @@ -397,16 +396,16 @@ int FolderMan::setupFoldersMigration() const auto &list = dir.entryList(); // Normally there should be only one account when migrating. - AccountState *accountState = AccountManager::instance()->accounts().value(0).data(); - for (const auto &alias : list) { - Folder *f = setupFolderFromOldConfigFile(alias, accountState); - if (f) { - scheduleFolder(f); - emit folderSyncStateChange(f); + if (AccountStatePtr accountState = AccountManager::instance()->accounts().value(nullptr)) { + for (const auto &alias : list) { + if (Folder *f = setupFolderFromOldConfigFile(alias, accountState)) { + scheduleFolder(f); + emit folderSyncStateChange(f); + } } - } - emit folderListChanged(_folderMap); + emit folderListChanged(_folderMap); + } // return the number of valid folders. return _folderMap.size(); @@ -523,7 +522,7 @@ QString FolderMan::unescapeAlias(const QString &alias) // filename is the name of the file only, it does not include // the configuration directory path // WARNING: Do not remove this code, it is used for predefined/automated deployments (2016) -Folder *FolderMan::setupFolderFromOldConfigFile(const QString &file, AccountState *accountState) +Folder *FolderMan::setupFolderFromOldConfigFile(const QString &file, AccountStatePtr accountState) { Folder *folder = nullptr; @@ -754,7 +753,7 @@ void FolderMan::slotRunOneEtagJob() void FolderMan::slotAccountStateChanged() { - AccountState *accountState = qobject_cast<AccountState *>(sender()); + AccountStatePtr accountState(qobject_cast<AccountState *>(sender())); if (!accountState) { return; } @@ -907,7 +906,7 @@ void FolderMan::slotEtagPollTimerTimeout() } } -void FolderMan::slotRemoveFoldersForAccount(AccountState *accountState) +void FolderMan::slotRemoveFoldersForAccount(AccountStatePtr accountState) { QList<Folder *> foldersToRemove; // reserve a magic number @@ -1053,7 +1052,7 @@ void FolderMan::slotFolderSyncFinished(const SyncResult &) startScheduledSyncSoon(); } -Folder *FolderMan::addFolder(AccountState *accountState, const FolderDefinition &folderDefinition) +Folder *FolderMan::addFolder(AccountStatePtr accountState, const FolderDefinition &folderDefinition) { // Choose a db filename auto definition = folderDefinition; @@ -1096,7 +1095,7 @@ Folder *FolderMan::addFolder(AccountState *accountState, const FolderDefinition Folder *FolderMan::addFolderInternal( FolderDefinition folderDefinition, - AccountState *accountState, + AccountStatePtr accountState, std::unique_ptr<Vfs> vfs) { auto alias = folderDefinition.alias; |