Skip to content

Commit 8cda11a

Browse files
authored
Merge pull request #9813 from nextcloud/bugfix/unloadfolder
fix(folderman): disconnect slotFolderCanSyncChanged when unloading folder.
2 parents 166390a + 1a0bd15 commit 8cda11a

3 files changed

Lines changed: 49 additions & 9 deletions

File tree

src/gui/folderman.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ void FolderMan::unloadFolder(Folder *f)
131131
this, &FolderMan::slotForwardFolderSyncStateChange);
132132
disconnect(f, &Folder::syncPausedChanged,
133133
this, &FolderMan::slotFolderSyncPaused);
134+
disconnect(f, &Folder::canSyncChanged,
135+
this, &FolderMan::slotFolderCanSyncChanged);
134136
disconnect(&f->syncEngine().syncFileStatusTracker(), &SyncFileStatusTracker::fileStatusChanged,
135137
_socketApi.data(), &SocketApi::broadcastStatusPushMessage);
136138
disconnect(f, &Folder::watchedFileChangedExternally,
@@ -748,12 +750,12 @@ void FolderMan::slotFolderSyncPaused(Folder *f, bool paused)
748750

749751
void FolderMan::slotFolderCanSyncChanged()
750752
{
751-
auto *f = qobject_cast<Folder *>(sender());
752-
ASSERT(f);
753-
if (f->canSync()) {
754-
_socketApi->slotRegisterPath(f->alias());
753+
auto folder = qobject_cast<Folder *>(sender());
754+
ASSERT(folder);
755+
if (folder->canSync()) {
756+
_socketApi->slotRegisterPath(folder->alias());
755757
} else {
756-
_socketApi->slotUnregisterPath(f->alias());
758+
_socketApi->slotUnregisterPath(folder->alias());
757759
}
758760
}
759761

src/gui/socketapi/socketapi.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,12 +470,16 @@ void SocketApi::slotRegisterPath(const QString &alias)
470470

471471
void SocketApi::slotUnregisterPath(const QString &alias)
472472
{
473-
if (!_registeredAliases.contains(alias))
473+
if (!_registeredAliases.contains(alias)) {
474474
return;
475+
}
475476

476-
Folder *f = FolderMan::instance()->folder(alias);
477-
if (f)
478-
broadcastMessage(buildMessage(QLatin1String("UNREGISTER_PATH"), removeTrailingSlash(f->path()), QString()), true);
477+
auto folder = FolderMan::instance()->folder(alias);
478+
if (folder) {
479+
broadcastMessage(buildMessage(QLatin1String("UNREGISTER_PATH"),
480+
removeTrailingSlash(folder->path()),
481+
QString()));
482+
}
479483

480484
_registeredAliases.remove(alias);
481485
}

test/testfolderman.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "accountstate.h"
2020
#include <accountmanager.h>
2121
#include "configfile.h"
22+
#include "socketapi/socketapi.h"
2223
#include "syncenginetestutils.h"
2324
#include "testhelper.h"
2425

@@ -554,6 +555,39 @@ private slots:
554555
verifyFolderSyncChangesOnReceivedFileIdNotification(user2, {50}, {"2"});
555556
verifyFolderSyncChangesOnReceivedFileIdNotification(user2, {10, 11, 17, 18, 404}, {});
556557
}
558+
559+
void testUnloadAndDeleteAllFolders()
560+
{
561+
_fm.reset({});
562+
_fm.reset(new FolderMan{});
563+
564+
QTemporaryDir dir;
565+
ConfigFile::setConfDir(dir.path());
566+
QVERIFY(dir.isValid());
567+
QVERIFY(QDir(dir.path()).mkpath(QStringLiteral("folder1")));
568+
QVERIFY(QDir(dir.path()).mkpath(QStringLiteral("folder2")));
569+
570+
auto account = Account::create();
571+
account->setCredentials(new FakeCredentials{new FakeQNAM({})});
572+
account->setUrl(QUrl(QStringLiteral("http://example.de")));
573+
auto accountState = new FakeAccountState(account);
574+
575+
const auto folder1 = FolderMan::instance()->addFolder(accountState, folderDefinition(dir.path() + QStringLiteral("/folder1")));
576+
const auto folder2 = FolderMan::instance()->addFolder(accountState, folderDefinition(dir.path() + QStringLiteral("/folder2")));
577+
QVERIFY(folder1);
578+
QVERIFY(folder2);
579+
QCOMPARE(FolderMan::instance()->map().count(), 2);
580+
581+
auto socketApi = FolderMan::instance()->socketApi();
582+
583+
// verifies that calling slotUnregisterPath twice on the same alias doesn't crash
584+
socketApi->slotUnregisterPath(folder1->alias());
585+
socketApi->slotUnregisterPath(folder1->alias());
586+
587+
FolderMan::instance()->unloadAndDeleteAllFolders();
588+
589+
QCOMPARE(FolderMan::instance()->map().count(), 0);
590+
}
557591
};
558592

559593
QTEST_GUILESS_MAIN(TestFolderMan)

0 commit comments

Comments
 (0)