aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2019-12-13 14:18:31 +0300
committerKitsune Ral <Kitsune-Ral@users.sf.net>2019-12-13 14:18:31 +0300
commit646186d802cfe7213146e4a68f8f926e21931b17 (patch)
tree622f2d8bd87bccd871c64ec748b030e25afa02c6 /lib
parent5a3f154b2795d90f2c7bc31dfead49ad0e746a8a (diff)
downloadlibquotient-646186d802cfe7213146e4a68f8f926e21931b17.tar.gz
libquotient-646186d802cfe7213146e4a68f8f926e21931b17.zip
Room::postFile(): minor refactoring
Diffstat (limited to 'lib')
-rw-r--r--lib/room.cpp25
1 files changed, 10 insertions, 15 deletions
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<RoomMessageEvent>(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<RoomMessageEvent>(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);