summary refs log tree commit diff
path: root/src/timeline/widgets
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2018-03-07 21:23:57 +0200
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2018-03-07 21:23:57 +0200
commit39abf163b8ec42b27a3ece4bb861aad4d3cfe5f1 (patch)
tree8576b2915a81c392234b58d6ec383ff1b2e1252d /src/timeline/widgets
parentAdd a unique id for locally uploaded images (diff)
downloadnheko-39abf163b8ec42b27a3ece4bb861aad4d3cfe5f1.tar.xz
Crete a proxy for media to uniquely match signal to the caller
Diffstat (limited to 'src/timeline/widgets')
-rw-r--r--src/timeline/widgets/AudioItem.cc15
-rw-r--r--src/timeline/widgets/FileItem.cc16
-rw-r--r--src/timeline/widgets/ImageItem.cc31
3 files changed, 31 insertions, 31 deletions
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_);
-
-        connect(client_.data(),
-                &MatrixClient::imageDownloaded,
-                this,
-                [this](const QString &id, const QPixmap &img) {
-                        if (id == QString::fromStdString(event_.event_id))
-                                setImage(img);
-                });
+        auto proxy = client_.data()->downloadImage(url_);
+
+        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