mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-10-30 09:30:47 +03:00
Small refactoring on the EditModal
This commit is contained in:
parent
079faa67e6
commit
19626dbb10
2 changed files with 81 additions and 68 deletions
|
@ -90,72 +90,7 @@ EditModal::EditModal(const QString &roomId, QWidget *parent)
|
|||
labelLayout->addWidget(errorField_);
|
||||
layout->addLayout(labelLayout);
|
||||
|
||||
connect(applyBtn_, &QPushButton::clicked, [this]() {
|
||||
// Check if the values are changed from the originals.
|
||||
auto newName = nameInput_->text().trimmed();
|
||||
auto newTopic = topicInput_->text().trimmed();
|
||||
|
||||
errorField_->hide();
|
||||
|
||||
if (newName == initialName_ && newTopic == initialTopic_) {
|
||||
close();
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
body.name = newName.toStdString();
|
||||
|
||||
http::client()->send_state_event<state::Name, EventType::RoomName>(
|
||||
roomId_.toStdString(),
|
||||
body,
|
||||
[proxy, newName](const mtx::responses::EventId &,
|
||||
mtx::http::RequestErr err) {
|
||||
if (err) {
|
||||
emit proxy->error(
|
||||
QString::fromStdString(err->matrix_error.error));
|
||||
return;
|
||||
}
|
||||
|
||||
emit proxy->nameEventSent(newName);
|
||||
});
|
||||
}
|
||||
|
||||
if (newTopic != initialTopic_ && !newTopic.isEmpty()) {
|
||||
state::Topic body;
|
||||
body.topic = newTopic.toStdString();
|
||||
|
||||
http::client()->send_state_event<state::Topic, EventType::RoomTopic>(
|
||||
roomId_.toStdString(),
|
||||
body,
|
||||
[proxy](const mtx::responses::EventId &, mtx::http::RequestErr err) {
|
||||
if (err) {
|
||||
emit proxy->error(
|
||||
QString::fromStdString(err->matrix_error.error));
|
||||
return;
|
||||
}
|
||||
|
||||
emit proxy->topicEventSent();
|
||||
});
|
||||
}
|
||||
});
|
||||
connect(applyBtn_, &QPushButton::clicked, this, &EditModal::applyClicked);
|
||||
connect(cancelBtn_, &QPushButton::clicked, this, &EditModal::close);
|
||||
|
||||
auto window = QApplication::activeWindow();
|
||||
|
@ -163,6 +98,63 @@ EditModal::EditModal(const QString &roomId, QWidget *parent)
|
|||
move(center.x() - (width() * 0.5), center.y() - (height() * 0.5));
|
||||
}
|
||||
|
||||
void
|
||||
EditModal::applyClicked()
|
||||
{
|
||||
// Check if the values are changed from the originals.
|
||||
auto newName = nameInput_->text().trimmed();
|
||||
auto newTopic = topicInput_->text().trimmed();
|
||||
|
||||
errorField_->hide();
|
||||
|
||||
if (newName == initialName_ && newTopic == initialTopic_) {
|
||||
close();
|
||||
return;
|
||||
}
|
||||
|
||||
using namespace mtx::events;
|
||||
auto proxy = std::make_shared<ThreadProxy>();
|
||||
connect(proxy.get(), &ThreadProxy::topicEventSent, this, &EditModal::topicEventSent);
|
||||
connect(proxy.get(), &ThreadProxy::nameEventSent, this, &EditModal::nameEventSent);
|
||||
connect(proxy.get(), &ThreadProxy::error, this, &EditModal::error);
|
||||
|
||||
if (newName != initialName_ && !newName.isEmpty()) {
|
||||
state::Name body;
|
||||
body.name = newName.toStdString();
|
||||
|
||||
http::client()->send_state_event<state::Name, EventType::RoomName>(
|
||||
roomId_.toStdString(),
|
||||
body,
|
||||
[proxy, newName](const mtx::responses::EventId &, mtx::http::RequestErr err) {
|
||||
if (err) {
|
||||
emit proxy->error(
|
||||
QString::fromStdString(err->matrix_error.error));
|
||||
return;
|
||||
}
|
||||
|
||||
emit proxy->nameEventSent(newName);
|
||||
});
|
||||
}
|
||||
|
||||
if (newTopic != initialTopic_ && !newTopic.isEmpty()) {
|
||||
state::Topic body;
|
||||
body.topic = newTopic.toStdString();
|
||||
|
||||
http::client()->send_state_event<state::Topic, EventType::RoomTopic>(
|
||||
roomId_.toStdString(),
|
||||
body,
|
||||
[proxy](const mtx::responses::EventId &, mtx::http::RequestErr err) {
|
||||
if (err) {
|
||||
emit proxy->error(
|
||||
QString::fromStdString(err->matrix_error.error));
|
||||
return;
|
||||
}
|
||||
|
||||
emit proxy->topicEventSent();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
EditModal::setFields(const QString &roomName, const QString &roomTopic)
|
||||
{
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <QEvent>
|
||||
#include <QFrame>
|
||||
#include <QImage>
|
||||
#include <QLabel>
|
||||
|
||||
#include "Cache.h"
|
||||
|
||||
|
@ -12,8 +13,6 @@ class QComboBox;
|
|||
class QHBoxLayout;
|
||||
class QShowEvent;
|
||||
class LoadingIndicator;
|
||||
class QLabel;
|
||||
class QLabel;
|
||||
class QLayout;
|
||||
class QPixmap;
|
||||
class TextField;
|
||||
|
@ -69,6 +68,28 @@ public:
|
|||
signals:
|
||||
void nameChanged(const QString &roomName);
|
||||
|
||||
private slots:
|
||||
void topicEventSent()
|
||||
{
|
||||
errorField_->hide();
|
||||
close();
|
||||
}
|
||||
|
||||
void nameEventSent(const QString &name)
|
||||
{
|
||||
errorField_->hide();
|
||||
emit nameChanged(name);
|
||||
close();
|
||||
}
|
||||
|
||||
void error(const QString &msg)
|
||||
{
|
||||
errorField_->setText(msg);
|
||||
errorField_->show();
|
||||
}
|
||||
|
||||
void applyClicked();
|
||||
|
||||
private:
|
||||
QString roomId_;
|
||||
QString initialName_;
|
||||
|
|
Loading…
Reference in a new issue