mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-26 04:58:49 +03:00
Crete a proxy for media to uniquely match signal to the caller
This commit is contained in:
parent
ea22bdce18
commit
39abf163b8
7 changed files with 57 additions and 48 deletions
|
@ -22,6 +22,15 @@
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
#include <mtx.hpp>
|
#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
|
* MatrixClient provides the high level API to communicate with
|
||||||
* a Matrix homeserver. All the responses are returned through signals.
|
* a Matrix homeserver. All the responses are returned through signals.
|
||||||
|
@ -55,8 +64,8 @@ public:
|
||||||
void fetchCommunityAvatar(const QString &communityId, const QUrl &avatarUrl);
|
void fetchCommunityAvatar(const QString &communityId, const QUrl &avatarUrl);
|
||||||
void fetchCommunityProfile(const QString &communityId);
|
void fetchCommunityProfile(const QString &communityId);
|
||||||
void fetchCommunityRooms(const QString &communityId);
|
void fetchCommunityRooms(const QString &communityId);
|
||||||
void downloadImage(const QString &event_id, const QUrl &url);
|
DownloadMediaProxy *downloadImage(const QUrl &url);
|
||||||
void downloadFile(const QString &event_id, const QUrl &url);
|
DownloadMediaProxy *downloadFile(const QUrl &url);
|
||||||
void messages(const QString &room_id, const QString &from_token, int limit = 30) noexcept;
|
void messages(const QString &room_id, const QString &from_token, int limit = 30) noexcept;
|
||||||
void uploadImage(const QString &roomid,
|
void uploadImage(const QString &roomid,
|
||||||
const QString &filename,
|
const QString &filename,
|
||||||
|
@ -140,8 +149,6 @@ signals:
|
||||||
void communityAvatarRetrieved(const QString &communityId, const QPixmap &img);
|
void communityAvatarRetrieved(const QString &communityId, const QPixmap &img);
|
||||||
void communityProfileRetrieved(const QString &communityId, const QJsonObject &profile);
|
void communityProfileRetrieved(const QString &communityId, const QJsonObject &profile);
|
||||||
void communityRoomsRetrieved(const QString &communityId, const QJsonObject &rooms);
|
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.
|
// Returned profile data for the user's account.
|
||||||
void getOwnProfileResponse(const QUrl &avatar_url, const QString &display_name);
|
void getOwnProfileResponse(const QUrl &avatar_url, const QString &display_name);
|
||||||
|
|
|
@ -72,11 +72,9 @@ protected:
|
||||||
void paintEvent(QPaintEvent *event) override;
|
void paintEvent(QPaintEvent *event) override;
|
||||||
void mousePressEvent(QMouseEvent *event) override;
|
void mousePressEvent(QMouseEvent *event) override;
|
||||||
|
|
||||||
private slots:
|
|
||||||
void fileDownloaded(const QString &event_id, const QByteArray &data);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void init();
|
void init();
|
||||||
|
void fileDownloaded(const QByteArray &data);
|
||||||
|
|
||||||
enum class AudioState
|
enum class AudioState
|
||||||
{
|
{
|
||||||
|
|
|
@ -60,12 +60,10 @@ protected:
|
||||||
void paintEvent(QPaintEvent *event) override;
|
void paintEvent(QPaintEvent *event) override;
|
||||||
void mousePressEvent(QMouseEvent *event) override;
|
void mousePressEvent(QMouseEvent *event) override;
|
||||||
|
|
||||||
private slots:
|
|
||||||
void fileDownloaded(const QString &event_id, const QByteArray &data);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void openUrl();
|
void openUrl();
|
||||||
void init();
|
void init();
|
||||||
|
void fileDownloaded(const QByteArray &data);
|
||||||
|
|
||||||
QUrl url_;
|
QUrl url_;
|
||||||
QString text_;
|
QString text_;
|
||||||
|
|
|
@ -738,13 +738,14 @@ MatrixClient::fetchUserAvatar(const QUrl &avatarUrl,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
DownloadMediaProxy *
|
||||||
MatrixClient::downloadImage(const QString &event_id, const QUrl &url)
|
MatrixClient::downloadImage(const QUrl &url)
|
||||||
{
|
{
|
||||||
QNetworkRequest image_request(url);
|
QNetworkRequest image_request(url);
|
||||||
|
|
||||||
auto reply = get(image_request);
|
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();
|
reply->deleteLater();
|
||||||
|
|
||||||
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
|
@ -762,17 +763,20 @@ MatrixClient::downloadImage(const QString &event_id, const QUrl &url)
|
||||||
QPixmap pixmap;
|
QPixmap pixmap;
|
||||||
pixmap.loadFromData(img);
|
pixmap.loadFromData(img);
|
||||||
|
|
||||||
emit imageDownloaded(event_id, pixmap);
|
emit proxy->imageDownloaded(pixmap);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return proxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
DownloadMediaProxy *
|
||||||
MatrixClient::downloadFile(const QString &event_id, const QUrl &url)
|
MatrixClient::downloadFile(const QUrl &url)
|
||||||
{
|
{
|
||||||
QNetworkRequest fileRequest(url);
|
QNetworkRequest fileRequest(url);
|
||||||
|
|
||||||
auto reply = get(fileRequest);
|
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();
|
reply->deleteLater();
|
||||||
|
|
||||||
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
|
@ -788,8 +792,10 @@ MatrixClient::downloadFile(const QString &event_id, const QUrl &url)
|
||||||
if (data.size() == 0)
|
if (data.size() == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
emit fileDownloaded(event_id, data);
|
emit proxy->fileDownloaded(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return proxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
@ -64,7 +64,6 @@ AudioItem::init()
|
||||||
player_->setVolume(100);
|
player_->setVolume(100);
|
||||||
player_->setNotifyInterval(1000);
|
player_->setNotifyInterval(1000);
|
||||||
|
|
||||||
connect(client_.data(), &MatrixClient::fileDownloaded, this, &AudioItem::fileDownloaded);
|
|
||||||
connect(player_, &QMediaPlayer::stateChanged, this, [this](QMediaPlayer::State state) {
|
connect(player_, &QMediaPlayer::stateChanged, this, [this](QMediaPlayer::State state) {
|
||||||
if (state == QMediaPlayer::StoppedState) {
|
if (state == QMediaPlayer::StoppedState) {
|
||||||
state_ = AudioState::Play;
|
state_ = AudioState::Play;
|
||||||
|
@ -135,16 +134,20 @@ AudioItem::mousePressEvent(QMouseEvent *event)
|
||||||
if (filenameToSave_.isEmpty())
|
if (filenameToSave_.isEmpty())
|
||||||
return;
|
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
|
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 {
|
try {
|
||||||
QFile file(filenameToSave_);
|
QFile file(filenameToSave_);
|
||||||
|
|
||||||
|
|
|
@ -57,8 +57,6 @@ FileItem::init()
|
||||||
QString media_params = url_parts[1];
|
QString media_params = url_parts[1];
|
||||||
url_ = QString("%1/_matrix/media/r0/download/%2")
|
url_ = QString("%1/_matrix/media/r0/download/%2")
|
||||||
.arg(client_.data()->getHomeServer().toString(), media_params);
|
.arg(client_.data()->getHomeServer().toString(), media_params);
|
||||||
|
|
||||||
connect(client_.data(), &MatrixClient::fileDownloaded, this, &FileItem::fileDownloaded);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FileItem::FileItem(QSharedPointer<MatrixClient> client,
|
FileItem::FileItem(QSharedPointer<MatrixClient> client,
|
||||||
|
@ -122,18 +120,22 @@ FileItem::mousePressEvent(QMouseEvent *event)
|
||||||
if (filenameToSave_.isEmpty())
|
if (filenameToSave_.isEmpty())
|
||||||
return;
|
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 {
|
} else {
|
||||||
openUrl();
|
openUrl();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
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 {
|
try {
|
||||||
QFile file(filenameToSave_);
|
QFile file(filenameToSave_);
|
||||||
|
|
||||||
|
|
|
@ -53,15 +53,13 @@ ImageItem::ImageItem(QSharedPointer<MatrixClient> client,
|
||||||
url_ = QString("%1/_matrix/media/r0/download/%2")
|
url_ = QString("%1/_matrix/media/r0/download/%2")
|
||||||
.arg(client_.data()->getHomeServer().toString(), media_params);
|
.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(),
|
connect(
|
||||||
&MatrixClient::imageDownloaded,
|
proxy, &DownloadMediaProxy::imageDownloaded, this, [this, proxy](const QPixmap &img) {
|
||||||
this,
|
proxy->deleteLater();
|
||||||
[this](const QString &id, const QPixmap &img) {
|
setImage(img);
|
||||||
if (id == QString::fromStdString(event_.event_id))
|
});
|
||||||
setImage(img);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ImageItem::ImageItem(QSharedPointer<MatrixClient> client,
|
ImageItem::ImageItem(QSharedPointer<MatrixClient> client,
|
||||||
|
@ -91,16 +89,13 @@ ImageItem::ImageItem(QSharedPointer<MatrixClient> client,
|
||||||
url_ = QString("%1/_matrix/media/r0/download/%2")
|
url_ = QString("%1/_matrix/media/r0/download/%2")
|
||||||
.arg(client_.data()->getHomeServer().toString(), media_params);
|
.arg(client_.data()->getHomeServer().toString(), media_params);
|
||||||
|
|
||||||
const auto request_id = QUuid::createUuid().toString();
|
auto proxy = client_.data()->downloadImage(url_);
|
||||||
client_.data()->downloadImage(request_id, url_);
|
|
||||||
|
|
||||||
connect(client_.data(),
|
connect(
|
||||||
&MatrixClient::imageDownloaded,
|
proxy, &DownloadMediaProxy::imageDownloaded, this, [proxy, this](const QPixmap &img) {
|
||||||
this,
|
proxy->deleteLater();
|
||||||
[request_id, this](const QString &id, const QPixmap &img) {
|
setImage(img);
|
||||||
if (id == request_id)
|
});
|
||||||
setImage(img);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
Loading…
Reference in a new issue