From 571575df28af878ccbe5abc91440f8d1fc36727a Mon Sep 17 00:00:00 2001 From: Albert Vaca Cintora Date: Thu, 25 May 2023 02:12:29 +0200 Subject: [PATCH] When receiving files, delay de-duplicating filenames When receiving two files with the same name, the first file might not be saved to disk already when we have to decide the name for the second if we do it too early. BUG: 470078 --- core/filetransferjob.cpp | 17 +++++++++++++---- core/filetransferjob.h | 5 +++++ plugins/share/shareplugin.cpp | 5 +---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/core/filetransferjob.cpp b/core/filetransferjob.cpp index a5dd1d9c6..82aa31a81 100644 --- a/core/filetransferjob.cpp +++ b/core/filetransferjob.cpp @@ -15,6 +15,7 @@ #include #include +#include FileTransferJob::FileTransferJob(const NetworkPacket *np, const QUrl &destination) : KJob() @@ -26,6 +27,7 @@ FileTransferJob::FileTransferJob(const NetworkPacket *np, const QUrl &destinatio , m_written(0) , m_size(np->payloadSize()) , m_np(np) + , m_autoRename(false) { Q_ASSERT(m_origin); // Disabled this assert: QBluetoothSocket doesn't report "->isReadable() == true" until it's connected @@ -48,10 +50,17 @@ void FileTransferJob::start() void FileTransferJob::doStart() { if (m_destination.isLocalFile() && QFile::exists(m_destination.toLocalFile())) { - setError(2); - setErrorText(i18n("Filename already present")); - emitResult(); - return; + if (m_autoRename) { + QFileInfo fileInfo(m_destination.toLocalFile()); + QString path = fileInfo.path(); + QString fileName = fileInfo.fileName(); + m_destination.setPath(path + QStringLiteral("/") + KFileUtils::suggestName(QUrl(path), fileName), QUrl::DecodedMode); + } else { + setError(2); + setErrorText(i18n("Filename already present")); + emitResult(); + return; + } } if (m_origin->bytesAvailable()) diff --git a/core/filetransferjob.h b/core/filetransferjob.h index 6f5827c59..e17f3abc7 100644 --- a/core/filetransferjob.h +++ b/core/filetransferjob.h @@ -45,6 +45,10 @@ public: { m_from = from; } + void setAutoRenameIfDestinatinonExists(bool autoRename) + { + m_autoRename = autoRename; + } const NetworkPacket *networkPacket() { return m_np; @@ -71,6 +75,7 @@ private: qint64 m_written; qint64 m_size; const NetworkPacket *m_np; + bool m_autoRename; }; #endif diff --git a/plugins/share/shareplugin.cpp b/plugins/share/shareplugin.cpp index 3a2cde1d4..7de6e1706 100644 --- a/plugins/share/shareplugin.cpp +++ b/plugins/share/shareplugin.cpp @@ -16,7 +16,6 @@ #include #include -#include #include #include #include @@ -60,9 +59,6 @@ QUrl SharePlugin::getFileDestination(const QString filename) const const QUrl dir = destinationDir().adjusted(QUrl::StripTrailingSlash); QUrl destination(dir); destination.setPath(dir.path() + QStringLiteral("/") + filename, QUrl::DecodedMode); - if (destination.isLocalFile() && QFile::exists(destination.toLocalFile())) { - destination.setPath(dir.path() + QStringLiteral("/") + KFileUtils::suggestName(dir, filename), QUrl::DecodedMode); - } return destination; } @@ -133,6 +129,7 @@ bool SharePlugin::receivePacket(const NetworkPacket &np) FileTransferJob *job = np.createPayloadTransferJob(destination); job->setOriginName(device()->name() + QStringLiteral(": ") + filename); + job->setAutoRenameIfDestinatinonExists(true); connect(job, &KJob::result, this, [this, dateCreated, dateModified, open](KJob *job) -> void { finished(job, dateCreated, dateModified, open); });