diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc
index 5471e410..1e7a5339 100644
--- a/src/MatrixClient.cc
+++ b/src/MatrixClient.cc
@@ -738,13 +738,14 @@ MatrixClient::fetchUserAvatar(const QUrl &avatarUrl,
});
}
-void
-MatrixClient::downloadImage(const QString &event_id, const QUrl &url)
+DownloadMediaProxy *
+MatrixClient::downloadImage(const QUrl &url)
{
QNetworkRequest image_request(url);
auto reply = get(image_request);
- connect(reply, &QNetworkReply::finished, this, [this, reply, event_id]() {
+ auto proxy = new DownloadMediaProxy;
+ connect(reply, &QNetworkReply::finished, this, [this, reply, proxy]() {
reply->deleteLater();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
@@ -762,17 +763,20 @@ MatrixClient::downloadImage(const QString &event_id, const QUrl &url)
QPixmap pixmap;
pixmap.loadFromData(img);
- emit imageDownloaded(event_id, pixmap);
+ emit proxy->imageDownloaded(pixmap);
});
+
+ return proxy;
}
-void
-MatrixClient::downloadFile(const QString &event_id, const QUrl &url)
+DownloadMediaProxy *
+MatrixClient::downloadFile(const QUrl &url)
{
QNetworkRequest fileRequest(url);
auto reply = get(fileRequest);
- connect(reply, &QNetworkReply::finished, this, [this, reply, event_id]() {
+ auto proxy = new DownloadMediaProxy;
+ connect(reply, &QNetworkReply::finished, this, [this, reply, proxy]() {
reply->deleteLater();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
@@ -788,8 +792,10 @@ MatrixClient::downloadFile(const QString &event_id, const QUrl &url)
if (data.size() == 0)
return;
- emit fileDownloaded(event_id, data);
+ emit proxy->fileDownloaded(data);
});
+
+ return proxy;
}
void
diff --git a/src/timeline/widgets/AudioItem.cc b/src/timeline/widgets/AudioItem.cc
index f171e78b..9f8b5dd1 100644
--- a/src/timeline/widgets/AudioItem.cc
+++ b/src/timeline/widgets/AudioItem.cc
@@ -64,7 +64,6 @@ AudioItem::init()
player_->setVolume(100);
player_->setNotifyInterval(1000);
- connect(client_.data(), &MatrixClient::fileDownloaded, this, &AudioItem::fileDownloaded);
connect(player_, &QMediaPlayer::stateChanged, this, [this](QMediaPlayer::State state) {
if (state == QMediaPlayer::StoppedState) {
state_ = AudioState::Play;
@@ -135,16 +134,20 @@ AudioItem::mousePressEvent(QMouseEvent *event)
if (filenameToSave_.isEmpty())
return;
- client_->downloadFile(QString::fromStdString(event_.event_id), url_);
+ auto proxy = client_->downloadFile(url_);
+ connect(proxy,
+ &DownloadMediaProxy::fileDownloaded,
+ this,
+ [proxy, this](const QByteArray &data) {
+ proxy->deleteLater();
+ fileDownloaded(data);
+ });
}
}
void
-AudioItem::fileDownloaded(const QString &event_id, const QByteArray &data)
+AudioItem::fileDownloaded(const QByteArray &data)
{
- if (event_id != QString::fromStdString(event_.event_id))
- return;
-
try {
QFile file(filenameToSave_);
diff --git a/src/timeline/widgets/FileItem.cc b/src/timeline/widgets/FileItem.cc
index 258536e7..d11ebe91 100644
--- a/src/timeline/widgets/FileItem.cc
+++ b/src/timeline/widgets/FileItem.cc
@@ -57,8 +57,6 @@ FileItem::init()
QString media_params = url_parts[1];
url_ = QString("%1/_matrix/media/r0/download/%2")
.arg(client_.data()->getHomeServer().toString(), media_params);
-
- connect(client_.data(), &MatrixClient::fileDownloaded, this, &FileItem::fileDownloaded);
}
FileItem::FileItem(QSharedPointer<MatrixClient> client,
@@ -122,18 +120,22 @@ FileItem::mousePressEvent(QMouseEvent *event)
if (filenameToSave_.isEmpty())
return;
- client_->downloadFile(QString::fromStdString(event_.event_id), url_);
+ auto proxy = client_->downloadFile(url_);
+ connect(proxy,
+ &DownloadMediaProxy::fileDownloaded,
+ this,
+ [proxy, this](const QByteArray &data) {
+ proxy->deleteLater();
+ fileDownloaded(data);
+ });
} else {
openUrl();
}
}
void
-FileItem::fileDownloaded(const QString &event_id, const QByteArray &data)
+FileItem::fileDownloaded(const QByteArray &data)
{
- if (event_id != QString::fromStdString(event_.event_id))
- return;
-
try {
QFile file(filenameToSave_);
diff --git a/src/timeline/widgets/ImageItem.cc b/src/timeline/widgets/ImageItem.cc
index a4f1ca57..2784f386 100644
--- a/src/timeline/widgets/ImageItem.cc
+++ b/src/timeline/widgets/ImageItem.cc
@@ -53,15 +53,13 @@ ImageItem::ImageItem(QSharedPointer<MatrixClient> client,
url_ = QString("%1/_matrix/media/r0/download/%2")
.arg(client_.data()->getHomeServer().toString(), media_params);
- client_.data()->downloadImage(QString::fromStdString(event.event_id), url_);
+ auto proxy = client_.data()->downloadImage(url_);
- connect(client_.data(),
- &MatrixClient::imageDownloaded,
- this,
- [this](const QString &id, const QPixmap &img) {
- if (id == QString::fromStdString(event_.event_id))
- setImage(img);
- });
+ connect(
+ proxy, &DownloadMediaProxy::imageDownloaded, this, [this, proxy](const QPixmap &img) {
+ proxy->deleteLater();
+ setImage(img);
+ });
}
ImageItem::ImageItem(QSharedPointer<MatrixClient> client,
@@ -91,16 +89,13 @@ ImageItem::ImageItem(QSharedPointer<MatrixClient> client,
url_ = QString("%1/_matrix/media/r0/download/%2")
.arg(client_.data()->getHomeServer().toString(), media_params);
- const auto request_id = QUuid::createUuid().toString();
- client_.data()->downloadImage(request_id, url_);
+ auto proxy = client_.data()->downloadImage(url_);
- connect(client_.data(),
- &MatrixClient::imageDownloaded,
- this,
- [request_id, this](const QString &id, const QPixmap &img) {
- if (id == request_id)
- setImage(img);
- });
+ connect(
+ proxy, &DownloadMediaProxy::imageDownloaded, this, [proxy, this](const QPixmap &img) {
+ proxy->deleteLater();
+ setImage(img);
+ });
}
void
|