Crete a proxy for media to uniquely match signal to the caller

This commit is contained in:
Konstantinos Sideris 2018-03-07 21:23:57 +02:00
parent ea22bdce18
commit 39abf163b8
7 changed files with 57 additions and 48 deletions

View File

@ -22,6 +22,15 @@
#include <QUrl>
#include <mtx.hpp>
class DownloadMediaProxy : public QObject
{
Q_OBJECT
signals:
void imageDownloaded(const QPixmap &data);
void fileDownloaded(const QByteArray &data);
};
/*
* MatrixClient provides the high level API to communicate with
* a Matrix homeserver. All the responses are returned through signals.
@ -55,8 +64,8 @@ public:
void fetchCommunityAvatar(const QString &communityId, const QUrl &avatarUrl);
void fetchCommunityProfile(const QString &communityId);
void fetchCommunityRooms(const QString &communityId);
void downloadImage(const QString &event_id, const QUrl &url);
void downloadFile(const QString &event_id, const QUrl &url);
DownloadMediaProxy *downloadImage(const QUrl &url);
DownloadMediaProxy *downloadFile(const QUrl &url);
void messages(const QString &room_id, const QString &from_token, int limit = 30) noexcept;
void uploadImage(const QString &roomid,
const QString &filename,
@ -140,8 +149,6 @@ signals:
void communityAvatarRetrieved(const QString &communityId, const QPixmap &img);
void communityProfileRetrieved(const QString &communityId, const QJsonObject &profile);
void communityRoomsRetrieved(const QString &communityId, const QJsonObject &rooms);
void imageDownloaded(const QString &event_id, const QPixmap &img);
void fileDownloaded(const QString &event_id, const QByteArray &data);
// Returned profile data for the user's account.
void getOwnProfileResponse(const QUrl &avatar_url, const QString &display_name);

View File

@ -72,11 +72,9 @@ protected:
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
private slots:
void fileDownloaded(const QString &event_id, const QByteArray &data);
private:
void init();
void fileDownloaded(const QByteArray &data);
enum class AudioState
{

View File

@ -60,12 +60,10 @@ protected:
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
private slots:
void fileDownloaded(const QString &event_id, const QByteArray &data);
private:
void openUrl();
void init();
void fileDownloaded(const QByteArray &data);
QUrl url_;
QString text_;

View File

@ -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

View File

@ -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_);

View File

@ -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_);

View File

@ -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