From 646186d802cfe7213146e4a68f8f926e21931b17 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 13 Dec 2019 14:18:31 +0300 Subject: Room::postFile(): minor refactoring --- lib/room.cpp | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/room.cpp b/lib/room.cpp index 69ebd677..0a192203 100644 --- a/lib/room.cpp +++ b/lib/room.cpp @@ -1628,19 +1628,16 @@ QString Room::postFile(const QString& plainText, const QUrl& localPath, QFileInfo localFile { localPath.toLocalFile() }; Q_ASSERT(localFile.isFile()); - const auto txnId = connection()->generateTxnId(); + const auto txnId = + d->addAsPending( + makeEvent(plainText, localFile, asGenericFile)) + ->transactionId(); // Remote URL will only be known after upload; fill in the local path // to enable the preview while the event is pending. uploadFile(txnId, localPath); - { - auto&& event = - makeEvent(plainText, localFile, asGenericFile); - event->setTransactionId(txnId); - d->addAsPending(std::move(event)); - } - auto* context = new QObject(this); - connect(this, &Room::fileTransferCompleted, context, - [context, this, txnId](const QString& id, QUrl, const QUrl& mxcUri) { + // Below, the upload job is used as a context object to clean up connections + connect(this, &Room::fileTransferCompleted, d->fileTransfers[txnId].job, + [this, txnId](const QString& id, QUrl, const QUrl& mxcUri) { if (id == txnId) { auto it = findPendingEvent(txnId); if (it != d->unsyncedEvents.end()) { @@ -1656,11 +1653,10 @@ QString Room::postFile(const QString& plainText, const QUrl& localPath, << "but the event referring to it was " "cancelled"; } - context->deleteLater(); } }); - connect(this, &Room::fileTransferCancelled, this, - [context, this, txnId](const QString& id) { + connect(this, &Room::fileTransferCancelled, d->fileTransfers[txnId].job, + [this, txnId](const QString& id) { if (id == txnId) { auto it = findPendingEvent(txnId); if (it != d->unsyncedEvents.end()) { @@ -1670,7 +1666,6 @@ QString Room::postFile(const QString& plainText, const QUrl& localPath, d->unsyncedEvents.erase(d->unsyncedEvents.begin() + idx); emit pendingEventDiscarded(); } - context->deleteLater(); } }); @@ -1849,7 +1844,7 @@ void Room::uploadFile(const QString& id, const QUrl& localFilename, auto fileName = localFilename.toLocalFile(); auto job = connection()->uploadFile(fileName, overrideContentType); if (isJobRunning(job)) { - d->fileTransfers.insert(id, { job, fileName, true }); + d->fileTransfers[id] = { job, fileName, true }; connect(job, &BaseJob::uploadProgress, this, [this, id](qint64 sent, qint64 total) { d->fileTransfers[id].update(sent, total); -- cgit v1.2.3