diff options
author | Nicolas Werner <nicolas.werner@hotmail.de> | 2022-03-30 06:45:31 +0200 |
---|---|---|
committer | Nicolas Werner <nicolas.werner@hotmail.de> | 2022-03-30 06:45:31 +0200 |
commit | 656fcac91cc3da305cca2e9ee7abd0a8557d176e (patch) | |
tree | e63e46dd791313db5df8672f10b908a802a16d7c /src/ui/RoomSettings.cpp | |
parent | Fix arrow keys (diff) | |
download | nheko-656fcac91cc3da305cca2e9ee7abd0a8557d176e.tar.xz |
Don't use a modal to edit room name and topic
Diffstat (limited to 'src/ui/RoomSettings.cpp')
-rw-r--r-- | src/ui/RoomSettings.cpp | 267 |
1 files changed, 96 insertions, 171 deletions
diff --git a/src/ui/RoomSettings.cpp b/src/ui/RoomSettings.cpp index 43a11b7f..42db1955 100644 --- a/src/ui/RoomSettings.cpp +++ b/src/ui/RoomSettings.cpp @@ -5,13 +5,10 @@ #include "RoomSettings.h" -#include <QApplication> #include <QFileDialog> -#include <QHBoxLayout> #include <QImageReader> #include <QMimeDatabase> #include <QStandardPaths> -#include <QVBoxLayout> #include <mtx/events/event_type.hpp> #include <mtx/responses/common.hpp> #include <mtx/responses/media.hpp> @@ -23,153 +20,9 @@ #include "Logging.h" #include "MatrixClient.h" #include "Utils.h" -#include "ui/TextField.h" using namespace mtx::events; -EditModal::EditModal(const QString &roomId, QWidget *parent) - : QWidget(parent) - , roomId_{roomId} -{ - setAutoFillBackground(true); - setAttribute(Qt::WA_DeleteOnClose, true); - setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint); - setWindowModality(Qt::WindowModal); - - QFont largeFont; - largeFont.setPointSizeF(largeFont.pointSizeF() * 1.4); - setMinimumWidth(conf::window::minModalWidth); - setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); - - auto layout = new QVBoxLayout(this); - - applyBtn_ = new QPushButton(tr("Apply"), this); - cancelBtn_ = new QPushButton(tr("Cancel"), this); - cancelBtn_->setDefault(true); - - auto btnLayout = new QHBoxLayout; - btnLayout->addStretch(1); - btnLayout->setSpacing(15); - btnLayout->addWidget(cancelBtn_); - btnLayout->addWidget(applyBtn_); - - nameInput_ = new TextField(this); - nameInput_->setLabel(tr("Name").toUpper()); - topicInput_ = new TextField(this); - topicInput_->setLabel(tr("Topic").toUpper()); - - errorField_ = new QLabel(this); - errorField_->setWordWrap(true); - errorField_->hide(); - - layout->addWidget(nameInput_); - layout->addWidget(topicInput_); - layout->addLayout(btnLayout, 1); - - auto labelLayout = new QHBoxLayout; - labelLayout->setAlignment(Qt::AlignHCenter); - labelLayout->addWidget(errorField_); - layout->addLayout(labelLayout); - - connect(applyBtn_, &QPushButton::clicked, this, &EditModal::applyClicked); - connect(cancelBtn_, &QPushButton::clicked, this, &EditModal::close); - - auto window = QApplication::activeWindow(); - - if (window != nullptr) { - auto center = window->frameGeometry().center(); - move(center.x() - (width() * 0.5), center.y() - (height() * 0.5)); - } -} - -void -EditModal::topicEventSent(const QString &topic) -{ - errorField_->hide(); - emit topicChanged(topic); - close(); -} - -void -EditModal::nameEventSent(const QString &name) -{ - errorField_->hide(); - emit nameChanged(name); - close(); -} - -void -EditModal::error(const QString &msg) -{ - errorField_->setText(msg); - errorField_->show(); -} - -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( - 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( - roomId_.toStdString(), - body, - [proxy, newTopic](const mtx::responses::EventId &, mtx::http::RequestErr err) { - if (err) { - emit proxy->error(QString::fromStdString(err->matrix_error.error)); - return; - } - - emit proxy->topicEventSent(newTopic); - }); - } -} - -void -EditModal::setFields(const QString &roomName, const QString &roomTopic) -{ - initialName_ = roomName; - initialTopic_ = roomTopic; - - nameInput_->setText(roomName); - topicInput_->setText(roomTopic); -} - RoomSettings::RoomSettings(QString roomid, QObject *parent) : QObject(parent) , roomid_{std::move(roomid)} @@ -245,6 +98,18 @@ RoomSettings::roomTopic() const } QString +RoomSettings::plainRoomName() const +{ + return QString::fromStdString(info_.name); +} + +QString +RoomSettings::plainRoomTopic() const +{ + return QString::fromStdString(info_.topic); +} + +QString RoomSettings::roomId() const { return roomid_; @@ -340,12 +205,24 @@ RoomSettings::canChangeJoinRules() const } bool -RoomSettings::canChangeNameAndTopic() const +RoomSettings::canChangeName() const { try { - return cache::hasEnoughPowerLevel({EventType::RoomName, EventType::RoomTopic}, - roomid_.toStdString(), - utils::localUser().toStdString()); + return cache::hasEnoughPowerLevel( + {EventType::RoomName}, roomid_.toStdString(), utils::localUser().toStdString()); + } catch (const lmdb::error &e) { + nhlog::db()->warn("lmdb error: {}", e.what()); + } + + return false; +} + +bool +RoomSettings::canChangeTopic() const +{ + try { + return cache::hasEnoughPowerLevel( + {EventType::RoomTopic}, roomid_.toStdString(), utils::localUser().toStdString()); } catch (const lmdb::error &e) { nhlog::db()->warn("lmdb error: {}", e.what()); } @@ -388,26 +265,6 @@ RoomSettings::supportsRestricted() const } void -RoomSettings::openEditModal() -{ - retrieveRoomInfo(); - - auto modal = new EditModal(roomid_); - modal->setFields(QString::fromStdString(info_.name), QString::fromStdString(info_.topic)); - modal->raise(); - modal->show(); - connect(modal, &EditModal::nameChanged, this, [this](const QString &newName) { - info_.name = newName.toStdString(); - emit roomNameChanged(); - }); - - connect(modal, &EditModal::topicChanged, this, [this](const QString &newTopic) { - info_.topic = newTopic.toStdString(); - emit roomTopicChanged(); - }); -} - -void RoomSettings::changeNotifications(int currentIndex) { notifications_ = currentIndex; @@ -503,6 +360,74 @@ RoomSettings::changeAccessRules(int index) } void +RoomSettings::changeName(QString name) +{ + // Check if the values are changed from the originals. + auto newName = name.trimmed().toStdString(); + + if (newName == info_.name) { + return; + } + + using namespace mtx::events; + auto proxy = std::make_shared<ThreadProxy>(); + connect(proxy.get(), &ThreadProxy::nameEventSent, this, [this](QString newRoomName) { + this->info_.name = newRoomName.toStdString(); + emit roomNameChanged(); + }); + connect(proxy.get(), &ThreadProxy::error, this, &RoomSettings::displayError); + + state::Name body; + body.name = newName; + + http::client()->send_state_event( + 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(QString::fromStdString(newName)); + }); +} + +void +RoomSettings::changeTopic(QString topic) +{ + // Check if the values are changed from the originals. + auto newTopic = topic.trimmed().toStdString(); + + if (newTopic == info_.topic) { + return; + } + + using namespace mtx::events; + auto proxy = std::make_shared<ThreadProxy>(); + connect(proxy.get(), &ThreadProxy::topicEventSent, this, [this](QString newRoomTopic) { + this->info_.topic = newRoomTopic.toStdString(); + emit roomTopicChanged(); + }); + connect(proxy.get(), &ThreadProxy::error, this, &RoomSettings::displayError); + + state::Topic body; + body.topic = newTopic; + + http::client()->send_state_event( + roomid_.toStdString(), + body, + [proxy, newTopic](const mtx::responses::EventId &, mtx::http::RequestErr err) { + if (err) { + emit proxy->error(QString::fromStdString(err->matrix_error.error)); + return; + } + + emit proxy->topicEventSent(QString::fromStdString(newTopic)); + }); +} + +void RoomSettings::updateAccessRules(const std::string &room_id, const mtx::events::state::JoinRules &join_rule, const mtx::events::state::GuestAccess &guest_access) |