From 2503ded168cd12a100bed7d3d39b579df37ce60b Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Fri, 18 Aug 2023 19:12:16 +0800 Subject: [PATCH] Lock files when relevant lock files found by folder watcher Signed-off-by: Claudio Cambra --- src/gui/folder.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/gui/folder.h | 4 ++++ 2 files changed, 40 insertions(+) diff --git a/src/gui/folder.cpp b/src/gui/folder.cpp index 92b05d269..29a9f46bc 100644 --- a/src/gui/folder.cpp +++ b/src/gui/folder.cpp @@ -682,6 +682,40 @@ void Folder::slotFilesLockImposed(const QSet &files) } } +void Folder::slotLockedFilesFound(const QSet &files) +{ + qCDebug(lcFolder) << "Found new lock files" << files; + + for (const auto &file : files) { + const auto fileRecordPath = fileFromLocalPath(file); + SyncJournalFileRecord rec; + + const auto canLockFile = journalDb()->getFileRecord(fileRecordPath, &rec) && rec.isValid() + && (!rec._lockstate._locked + || (rec._lockstate._lockOwnerType == static_cast(SyncFileItem::LockOwnerType::UserLock) + && rec._lockstate._lockOwnerId == _accountState->account()->davUser())); + + if (!canLockFile) { + qCDebug(lcFolder) << "Skipping locking file" << file << "with rec.isValid():" << rec.isValid() + << "and rec._lockstate._lockOwnerId:" << rec._lockstate._lockOwnerId << "and davUser:" << _accountState->account()->davUser(); + continue; + } + + const QString remoteFilePath = remotePathTrailingSlash() + rec.path(); + qCDebug(lcFolder) << "Automatically locking file on server" << remoteFilePath; + _fileLockSuccess = connect(_accountState->account().data(), &Account::lockFileSuccess, this, [this, remoteFilePath] { + disconnect(_fileLockSuccess); + qCDebug(lcFolder) << "Locking file succeeded" << remoteFilePath; + startSync(); + }); + _fileLockFailure = connect(_accountState->account().data(), &Account::lockFileError, this, [this, remoteFilePath](const QString &message) { + disconnect(_fileLockFailure); + qCWarning(lcFolder) << "Failed to lock a file:" << remoteFilePath << message; + }); + _accountState->account()->setLockFileState(remoteFilePath, journalDb(), SyncFileItem::LockStatus::LockedItem); + } +} + void Folder::implicitlyHydrateFile(const QString &relativepath) { qCInfo(lcFolder) << "Implicitly hydrate virtual file:" << relativepath; @@ -1475,6 +1509,7 @@ void Folder::slotCapabilitiesChanged() { if (_accountState->account()->capabilities().filesLockAvailable()) { connect(_folderWatcher.data(), &FolderWatcher::filesLockReleased, this, &Folder::slotFilesLockReleased, Qt::UniqueConnection); + connect(_folderWatcher.data(), &FolderWatcher::lockedFilesFound, this, &Folder::slotLockedFilesFound, Qt::UniqueConnection); } } @@ -1520,6 +1555,7 @@ void Folder::registerFolderWatcher() this, &Folder::slotWatcherUnreliable); if (_accountState->account()->capabilities().filesLockAvailable()) { connect(_folderWatcher.data(), &FolderWatcher::filesLockReleased, this, &Folder::slotFilesLockReleased); + connect(_folderWatcher.data(), &FolderWatcher::lockedFilesFound, this, &Folder::slotLockedFilesFound); } connect(_folderWatcher.data(), &FolderWatcher::filesLockImposed, this, &Folder::slotFilesLockImposed, Qt::UniqueConnection); _folderWatcher->init(path()); diff --git a/src/gui/folder.h b/src/gui/folder.h index edc8484b4..f0622f48c 100644 --- a/src/gui/folder.h +++ b/src/gui/folder.h @@ -359,6 +359,8 @@ public slots: */ void slotFilesLockImposed(const QSet &files); + void slotLockedFilesFound(const QSet &files); + /** * Mark a virtual file as being requested for download, and start a sync. * @@ -562,6 +564,8 @@ private: QMetaObject::Connection _officeFileLockReleaseUnlockSuccess; QMetaObject::Connection _officeFileLockReleaseUnlockFailure; + QMetaObject::Connection _fileLockSuccess; + QMetaObject::Connection _fileLockFailure; }; } -- 2.30.2