mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-22 11:00:48 +03:00
Use proxy objects on lambdas instead of raw pointers
When the object is destroyed the connections will be removed automatically by Qt. fixes #433
This commit is contained in:
parent
a1af99becb
commit
896fe069b6
10 changed files with 79 additions and 67 deletions
|
@ -333,6 +333,7 @@ qt5_wrap_cpp(MOC_HEADERS
|
|||
src/CommunitiesList.h
|
||||
src/LoginPage.h
|
||||
src/MainWindow.h
|
||||
src/MatrixClient.h
|
||||
src/InviteeItem.h
|
||||
src/QuickSwitcher.h
|
||||
src/RegisterPage.h
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <QMetaType>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
#include <mtx/responses.hpp>
|
||||
|
@ -17,6 +18,16 @@ Q_DECLARE_METATYPE(std::string)
|
|||
Q_DECLARE_METATYPE(std::vector<std::string>)
|
||||
Q_DECLARE_METATYPE(std::vector<QString>)
|
||||
|
||||
class MediaProxy : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
signals:
|
||||
void imageDownloaded(const QPixmap &);
|
||||
void imageSaved(const QString &, const QByteArray &);
|
||||
void fileDownloaded(const QByteArray &);
|
||||
};
|
||||
|
||||
namespace http {
|
||||
mtx::http::Client *
|
||||
client();
|
||||
|
|
|
@ -90,20 +90,6 @@ EditModal::EditModal(const QString &roomId, QWidget *parent)
|
|||
labelLayout->addWidget(errorField_);
|
||||
layout->addLayout(labelLayout);
|
||||
|
||||
connect(this, &EditModal::stateEventErrorCb, this, [this](const QString &msg) {
|
||||
errorField_->setText(msg);
|
||||
errorField_->show();
|
||||
});
|
||||
connect(this, &EditModal::nameEventSentCb, this, [this](const QString &newName) {
|
||||
errorField_->hide();
|
||||
emit nameChanged(newName);
|
||||
close();
|
||||
});
|
||||
connect(this, &EditModal::topicEventSentCb, this, [this]() {
|
||||
errorField_->hide();
|
||||
close();
|
||||
});
|
||||
|
||||
connect(applyBtn_, &QPushButton::clicked, [this]() {
|
||||
// Check if the values are changed from the originals.
|
||||
auto newName = nameInput_->text().trimmed();
|
||||
|
@ -117,6 +103,21 @@ EditModal::EditModal(const QString &roomId, QWidget *parent)
|
|||
}
|
||||
|
||||
using namespace mtx::events;
|
||||
auto proxy = std::make_shared<ThreadProxy>();
|
||||
connect(proxy.get(), &ThreadProxy::topicEventSent, this, [this]() {
|
||||
errorField_->hide();
|
||||
close();
|
||||
});
|
||||
connect(
|
||||
proxy.get(), &ThreadProxy::nameEventSent, this, [this](const QString &newName) {
|
||||
errorField_->hide();
|
||||
emit nameChanged(newName);
|
||||
close();
|
||||
});
|
||||
connect(proxy.get(), &ThreadProxy::error, this, [this](const QString &msg) {
|
||||
errorField_->setText(msg);
|
||||
errorField_->show();
|
||||
});
|
||||
|
||||
if (newName != initialName_ && !newName.isEmpty()) {
|
||||
state::Name body;
|
||||
|
@ -125,15 +126,15 @@ EditModal::EditModal(const QString &roomId, QWidget *parent)
|
|||
http::client()->send_state_event<state::Name, EventType::RoomName>(
|
||||
roomId_.toStdString(),
|
||||
body,
|
||||
[this, newName](const mtx::responses::EventId &,
|
||||
mtx::http::RequestErr err) {
|
||||
[proxy, newName](const mtx::responses::EventId &,
|
||||
mtx::http::RequestErr err) {
|
||||
if (err) {
|
||||
emit stateEventErrorCb(
|
||||
emit proxy->error(
|
||||
QString::fromStdString(err->matrix_error.error));
|
||||
return;
|
||||
}
|
||||
|
||||
emit nameEventSentCb(newName);
|
||||
emit proxy->nameEventSent(newName);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -144,14 +145,14 @@ EditModal::EditModal(const QString &roomId, QWidget *parent)
|
|||
http::client()->send_state_event<state::Topic, EventType::RoomTopic>(
|
||||
roomId_.toStdString(),
|
||||
body,
|
||||
[this](const mtx::responses::EventId &, mtx::http::RequestErr err) {
|
||||
[proxy](const mtx::responses::EventId &, mtx::http::RequestErr err) {
|
||||
if (err) {
|
||||
emit stateEventErrorCb(
|
||||
emit proxy->error(
|
||||
QString::fromStdString(err->matrix_error.error));
|
||||
return;
|
||||
}
|
||||
|
||||
emit topicEventSentCb();
|
||||
emit proxy->topicEventSent();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
@ -366,15 +367,15 @@ RoomSettings::RoomSettings(const QString &room_id, QWidget *parent)
|
|||
connect(filter, &ClickableFilter::clicked, this, &RoomSettings::updateAvatar);
|
||||
}
|
||||
|
||||
auto roomNameLabel = new QLabel(QString::fromStdString(info_.name), this);
|
||||
roomNameLabel->setFont(doubleFont);
|
||||
roomNameLabel_ = new QLabel(QString::fromStdString(info_.name), this);
|
||||
roomNameLabel_->setFont(doubleFont);
|
||||
|
||||
auto membersLabel = new QLabel(tr("%n member(s)", "", info_.member_count), this);
|
||||
|
||||
auto textLayout = new QVBoxLayout;
|
||||
textLayout->addWidget(roomNameLabel);
|
||||
textLayout->addWidget(roomNameLabel_);
|
||||
textLayout->addWidget(membersLabel);
|
||||
textLayout->setAlignment(roomNameLabel, Qt::AlignCenter | Qt::AlignTop);
|
||||
textLayout->setAlignment(roomNameLabel_, Qt::AlignCenter | Qt::AlignTop);
|
||||
textLayout->setAlignment(membersLabel, Qt::AlignCenter | Qt::AlignTop);
|
||||
textLayout->setSpacing(TEXT_SPACING);
|
||||
textLayout->setMargin(0);
|
||||
|
@ -458,8 +459,9 @@ RoomSettings::setupEditButton()
|
|||
modal->setFields(QString::fromStdString(info_.name),
|
||||
QString::fromStdString(info_.topic));
|
||||
modal->show();
|
||||
connect(modal, &EditModal::nameChanged, this, [](const QString &newName) {
|
||||
Q_UNUSED(newName);
|
||||
connect(modal, &EditModal::nameChanged, this, [this](const QString &newName) {
|
||||
if (roomNameLabel_)
|
||||
roomNameLabel_->setText(newName);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -53,6 +53,8 @@ class ThreadProxy : public QObject
|
|||
signals:
|
||||
void error(const QString &msg);
|
||||
void avatarChanged(const QImage &img);
|
||||
void nameEventSent(const QString &);
|
||||
void topicEventSent();
|
||||
};
|
||||
|
||||
class EditModal : public QWidget
|
||||
|
@ -66,9 +68,6 @@ public:
|
|||
|
||||
signals:
|
||||
void nameChanged(const QString &roomName);
|
||||
void nameEventSentCb(const QString &newName);
|
||||
void topicEventSentCb();
|
||||
void stateEventErrorCb(const QString &msg);
|
||||
|
||||
private:
|
||||
QString roomId_;
|
||||
|
@ -138,6 +137,7 @@ private:
|
|||
QString room_id_;
|
||||
QImage avatarImg_;
|
||||
|
||||
QLabel *roomNameLabel_ = nullptr;
|
||||
QLabel *errorLabel_ = nullptr;
|
||||
LoadingIndicator *spinner_ = nullptr;
|
||||
|
||||
|
|
|
@ -55,7 +55,6 @@ AudioItem::init()
|
|||
player_->setVolume(100);
|
||||
player_->setNotifyInterval(1000);
|
||||
|
||||
connect(this, &AudioItem::fileDownloadedCb, this, &AudioItem::fileDownloaded);
|
||||
connect(player_, &QMediaPlayer::stateChanged, this, [this](QMediaPlayer::State state) {
|
||||
if (state == QMediaPlayer::StoppedState) {
|
||||
state_ = AudioState::Play;
|
||||
|
@ -120,19 +119,22 @@ AudioItem::mousePressEvent(QMouseEvent *event)
|
|||
if (filenameToSave_.isEmpty())
|
||||
return;
|
||||
|
||||
auto proxy = std::make_shared<MediaProxy>();
|
||||
connect(proxy.get(), &MediaProxy::fileDownloaded, this, &AudioItem::fileDownloaded);
|
||||
|
||||
http::client()->download(
|
||||
url_.toString().toStdString(),
|
||||
[this](const std::string &data,
|
||||
const std::string &,
|
||||
const std::string &,
|
||||
mtx::http::RequestErr err) {
|
||||
[proxy = std::move(proxy), url = url_](const std::string &data,
|
||||
const std::string &,
|
||||
const std::string &,
|
||||
mtx::http::RequestErr err) {
|
||||
if (err) {
|
||||
nhlog::net()->info("failed to retrieve m.audio content: {}",
|
||||
url_.toString().toStdString());
|
||||
url.toString().toStdString());
|
||||
return;
|
||||
}
|
||||
|
||||
emit fileDownloadedCb(QByteArray(data.data(), data.size()));
|
||||
emit proxy->fileDownloaded(QByteArray(data.data(), data.size()));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,9 +69,6 @@ protected:
|
|||
void resizeEvent(QResizeEvent *event) override;
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
|
||||
signals:
|
||||
void fileDownloadedCb(const QByteArray &data);
|
||||
|
||||
private slots:
|
||||
void fileDownloaded(const QByteArray &data);
|
||||
|
||||
|
|
|
@ -50,8 +50,6 @@ FileItem::init()
|
|||
icon_.addFile(":/icons/icons/ui/arrow-pointing-down.png");
|
||||
|
||||
setFixedHeight(Height);
|
||||
|
||||
connect(this, &FileItem::fileDownloadedCb, this, &FileItem::fileDownloaded);
|
||||
}
|
||||
|
||||
FileItem::FileItem(const mtx::events::RoomEvent<mtx::events::msg::File> &event, QWidget *parent)
|
||||
|
@ -110,19 +108,22 @@ FileItem::mousePressEvent(QMouseEvent *event)
|
|||
if (filenameToSave_.isEmpty())
|
||||
return;
|
||||
|
||||
auto proxy = std::make_shared<MediaProxy>();
|
||||
connect(proxy.get(), &MediaProxy::fileDownloaded, this, &FileItem::fileDownloaded);
|
||||
|
||||
http::client()->download(
|
||||
url_.toString().toStdString(),
|
||||
[this](const std::string &data,
|
||||
const std::string &,
|
||||
const std::string &,
|
||||
mtx::http::RequestErr err) {
|
||||
[proxy = std::move(proxy), url = url_](const std::string &data,
|
||||
const std::string &,
|
||||
const std::string &,
|
||||
mtx::http::RequestErr err) {
|
||||
if (err) {
|
||||
nhlog::ui()->warn("failed to retrieve m.file content: {}",
|
||||
url_.toString().toStdString());
|
||||
url.toString().toStdString());
|
||||
return;
|
||||
}
|
||||
|
||||
emit fileDownloadedCb(QByteArray(data.data(), data.size()));
|
||||
emit proxy->fileDownloaded(QByteArray(data.data(), data.size()));
|
||||
});
|
||||
} else {
|
||||
openUrl();
|
||||
|
|
|
@ -52,9 +52,6 @@ public:
|
|||
QColor iconColor() const { return iconColor_; }
|
||||
QColor backgroundColor() const { return backgroundColor_; }
|
||||
|
||||
signals:
|
||||
void fileDownloadedCb(const QByteArray &data);
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
|
|
|
@ -33,11 +33,14 @@
|
|||
void
|
||||
ImageItem::downloadMedia(const QUrl &url)
|
||||
{
|
||||
auto proxy = std::make_shared<MediaProxy>();
|
||||
connect(proxy.get(), &MediaProxy::imageDownloaded, this, &ImageItem::setImage);
|
||||
|
||||
http::client()->download(url.toString().toStdString(),
|
||||
[this, url](const std::string &data,
|
||||
const std::string &,
|
||||
const std::string &,
|
||||
mtx::http::RequestErr err) {
|
||||
[proxy = std::move(proxy), url](const std::string &data,
|
||||
const std::string &,
|
||||
const std::string &,
|
||||
mtx::http::RequestErr err) {
|
||||
if (err) {
|
||||
nhlog::net()->warn(
|
||||
"failed to retrieve image {}: {} {}",
|
||||
|
@ -49,7 +52,8 @@ ImageItem::downloadMedia(const QUrl &url)
|
|||
|
||||
QPixmap img;
|
||||
img.loadFromData(QByteArray(data.data(), data.size()));
|
||||
emit imageDownloaded(img);
|
||||
|
||||
emit proxy->imageDownloaded(img);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -76,8 +80,6 @@ ImageItem::init()
|
|||
setCursor(Qt::PointingHandCursor);
|
||||
setAttribute(Qt::WA_Hover, true);
|
||||
|
||||
connect(this, &ImageItem::imageDownloaded, this, &ImageItem::setImage);
|
||||
connect(this, &ImageItem::imageSaved, this, &ImageItem::saveImage);
|
||||
downloadMedia(url_);
|
||||
}
|
||||
|
||||
|
@ -240,12 +242,15 @@ ImageItem::saveAs()
|
|||
|
||||
const auto url = url_.toString().toStdString();
|
||||
|
||||
auto proxy = std::make_shared<MediaProxy>();
|
||||
connect(proxy.get(), &MediaProxy::imageSaved, this, &ImageItem::saveImage);
|
||||
|
||||
http::client()->download(
|
||||
url,
|
||||
[this, filename, url](const std::string &data,
|
||||
const std::string &,
|
||||
const std::string &,
|
||||
mtx::http::RequestErr err) {
|
||||
[proxy = std::move(proxy), filename, url](const std::string &data,
|
||||
const std::string &,
|
||||
const std::string &,
|
||||
mtx::http::RequestErr err) {
|
||||
if (err) {
|
||||
nhlog::net()->warn("failed to retrieve image {}: {} {}",
|
||||
url,
|
||||
|
@ -254,6 +259,6 @@ ImageItem::saveAs()
|
|||
return;
|
||||
}
|
||||
|
||||
emit imageSaved(filename, QByteArray(data.data(), data.size()));
|
||||
emit proxy->imageSaved(filename, QByteArray(data.data(), data.size()));
|
||||
});
|
||||
}
|
||||
|
|
|
@ -48,10 +48,6 @@ public slots:
|
|||
void setImage(const QPixmap &image);
|
||||
void saveImage(const QString &filename, const QByteArray &data);
|
||||
|
||||
signals:
|
||||
void imageDownloaded(const QPixmap &img);
|
||||
void imageSaved(const QString &filename, const QByteArray &data);
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
|
|
Loading…
Reference in a new issue