From b541cecd2e0aaf5870051f334f872037c0262d71 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Thu, 30 Jan 2020 03:45:27 +0100 Subject: [PATCH] Enable ban and kick button in UserProfile (and try to fix centering) --- src/ChatPage.cpp | 162 ++++++++++++++++++------------------ src/ChatPage.h | 5 ++ src/Utils.cpp | 3 +- src/dialogs/UserProfile.cpp | 17 ++-- 4 files changed, 97 insertions(+), 90 deletions(-) diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 59245390..9574751b 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -280,87 +280,10 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) connect(text_input_, &TextInputWidget::sendJoinRoomRequest, this, &ChatPage::joinRoom); // invites and bans via quick command - connect(text_input_, - &TextInputWidget::sendInviteRoomRequest, - this, - [this](QString userid, QString reason) { - http::client()->invite_user( - current_room_.toStdString(), - userid.toStdString(), - [this, userid, room = current_room_](const mtx::responses::Empty &, - mtx::http::RequestErr err) { - if (err) { - emit showNotification(tr("Failed to invite %1 to %2: %3") - .arg(userid) - .arg(room) - .arg(QString::fromStdString( - err->matrix_error.error))); - } else - emit showNotification(tr("Invited user: %1").arg(userid)); - }, - reason.trimmed().toStdString()); - }); - connect(text_input_, - &TextInputWidget::sendKickRoomRequest, - this, - [this](QString userid, QString reason) { - http::client()->kick_user( - current_room_.toStdString(), - userid.toStdString(), - [this, userid, room = current_room_](const mtx::responses::Empty &, - mtx::http::RequestErr err) { - if (err) { - emit showNotification(tr("Failed to kick %1 to %2: %3") - .arg(userid) - .arg(room) - .arg(QString::fromStdString( - err->matrix_error.error))); - } else - emit showNotification(tr("Kicked user: %1").arg(userid)); - }, - reason.trimmed().toStdString()); - }); - connect(text_input_, - &TextInputWidget::sendBanRoomRequest, - this, - [this](QString userid, QString reason) { - http::client()->ban_user( - current_room_.toStdString(), - userid.toStdString(), - [this, userid, room = current_room_](const mtx::responses::Empty &, - mtx::http::RequestErr err) { - if (err) { - emit showNotification(tr("Failed to ban %1 in %2: %3") - .arg(userid) - .arg(room) - .arg(QString::fromStdString( - err->matrix_error.error))); - } else - emit showNotification(tr("Banned user: %1").arg(userid)); - }, - reason.trimmed().toStdString()); - }); - connect( - text_input_, - &TextInputWidget::sendUnbanRoomRequest, - this, - [this](QString userid, QString reason) { - http::client()->unban_user( - current_room_.toStdString(), - userid.toStdString(), - [this, userid, room = current_room_](const mtx::responses::Empty &, - mtx::http::RequestErr err) { - if (err) { - emit showNotification( - tr("Failed to unban %1 in %2: %3") - .arg(userid) - .arg(room) - .arg(QString::fromStdString(err->matrix_error.error))); - } else - emit showNotification(tr("Unbanned user: %1").arg(userid)); - }, - reason.trimmed().toStdString()); - }); + connect(text_input_, &TextInputWidget::sendInviteRoomRequest, this, &ChatPage::inviteUser); + connect(text_input_, &TextInputWidget::sendKickRoomRequest, this, &ChatPage::kickUser); + connect(text_input_, &TextInputWidget::sendBanRoomRequest, this, &ChatPage::banUser); + connect(text_input_, &TextInputWidget::sendUnbanRoomRequest, this, &ChatPage::unbanUser); connect( text_input_, @@ -1138,6 +1061,83 @@ ChatPage::leaveRoom(const QString &room_id) }); } +void +ChatPage::inviteUser(QString userid, QString reason) +{ + http::client()->invite_user( + current_room_.toStdString(), + userid.toStdString(), + [this, userid, room = current_room_](const mtx::responses::Empty &, + mtx::http::RequestErr err) { + if (err) { + emit showNotification( + tr("Failed to invite %1 to %2: %3") + .arg(userid) + .arg(room) + .arg(QString::fromStdString(err->matrix_error.error))); + } else + emit showNotification(tr("Invited user: %1").arg(userid)); + }, + reason.trimmed().toStdString()); +} +void +ChatPage::kickUser(QString userid, QString reason) +{ + http::client()->kick_user( + current_room_.toStdString(), + userid.toStdString(), + [this, userid, room = current_room_](const mtx::responses::Empty &, + mtx::http::RequestErr err) { + if (err) { + emit showNotification( + tr("Failed to kick %1 to %2: %3") + .arg(userid) + .arg(room) + .arg(QString::fromStdString(err->matrix_error.error))); + } else + emit showNotification(tr("Kicked user: %1").arg(userid)); + }, + reason.trimmed().toStdString()); +} +void +ChatPage::banUser(QString userid, QString reason) +{ + http::client()->ban_user( + current_room_.toStdString(), + userid.toStdString(), + [this, userid, room = current_room_](const mtx::responses::Empty &, + mtx::http::RequestErr err) { + if (err) { + emit showNotification( + tr("Failed to ban %1 in %2: %3") + .arg(userid) + .arg(room) + .arg(QString::fromStdString(err->matrix_error.error))); + } else + emit showNotification(tr("Banned user: %1").arg(userid)); + }, + reason.trimmed().toStdString()); +} +void +ChatPage::unbanUser(QString userid, QString reason) +{ + http::client()->unban_user( + current_room_.toStdString(), + userid.toStdString(), + [this, userid, room = current_room_](const mtx::responses::Empty &, + mtx::http::RequestErr err) { + if (err) { + emit showNotification( + tr("Failed to unban %1 in %2: %3") + .arg(userid) + .arg(room) + .arg(QString::fromStdString(err->matrix_error.error))); + } else + emit showNotification(tr("Unbanned user: %1").arg(userid)); + }, + reason.trimmed().toStdString()); +} + void ChatPage::sendTypingNotifications() { diff --git a/src/ChatPage.h b/src/ChatPage.h index 354a21f3..9bc5fb73 100644 --- a/src/ChatPage.h +++ b/src/ChatPage.h @@ -90,6 +90,11 @@ public slots: void leaveRoom(const QString &room_id); void createRoom(const mtx::requests::CreateRoom &req); + void inviteUser(QString userid, QString reason); + void kickUser(QString userid, QString reason); + void banUser(QString userid, QString reason); + void unbanUser(QString userid, QString reason); + signals: void connectionLost(); void connectionRestored(); diff --git a/src/Utils.cpp b/src/Utils.cpp index 76ffed31..00796a53 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -613,7 +613,8 @@ utils::centerWidget(QWidget *widget, QWidget *parent) }; if (parent) { - widget->move(findCenter(parent->geometry())); + widget->move(parent->window()->frameGeometry().topLeft() + + parent->window()->rect().center() - widget->rect().center()); return; } diff --git a/src/dialogs/UserProfile.cpp b/src/dialogs/UserProfile.cpp index 50c1c990..755e8395 100644 --- a/src/dialogs/UserProfile.cpp +++ b/src/dialogs/UserProfile.cpp @@ -49,7 +49,6 @@ UserProfile::UserProfile(QWidget *parent) { setAutoFillBackground(true); setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint); - setWindowModality(Qt::WindowModal); setAttribute(Qt::WA_DeleteOnClose, true); QIcon banIcon, kickIcon, ignoreIcon, startChatIcon; @@ -61,7 +60,6 @@ UserProfile::UserProfile(QWidget *parent) banBtn_->setIcon(banIcon); banBtn_->setIconSize(QSize(BUTTON_RADIUS, BUTTON_RADIUS)); banBtn_->setToolTip(tr("Ban the user from the room")); - banBtn_->setDisabled(true); // Not used yet. ignoreIcon.addFile(":/icons/icons/ui/volume-off-indicator.png"); ignoreBtn_ = new FlatButton(this); @@ -79,7 +77,6 @@ UserProfile::UserProfile(QWidget *parent) kickBtn_->setIcon(kickIcon); kickBtn_->setIconSize(QSize(BUTTON_RADIUS, BUTTON_RADIUS)); kickBtn_->setToolTip(tr("Kick the user from the room")); - kickBtn_->setDisabled(true); // Not used yet. startChatIcon.addFile(":/icons/icons/ui/black-bubble-speech.png"); startChat_ = new FlatButton(this); @@ -102,6 +99,13 @@ UserProfile::UserProfile(QWidget *parent) emit ChatPage::instance()->createRoom(req); }); + connect(banBtn_, &QPushButton::clicked, this, [this] { + ChatPage::instance()->banUser(userIdLabel_->text(), ""); + }); + connect(kickBtn_, &QPushButton::clicked, this, [this] { + ChatPage::instance()->kickUser(userIdLabel_->text(), ""); + }); + // Button line auto btnLayout = new QHBoxLayout; btnLayout->addStretch(1); @@ -166,10 +170,6 @@ UserProfile::UserProfile(QWidget *parent) vlayout->setAlignment(avatar_, Qt::AlignCenter | Qt::AlignTop); vlayout->setAlignment(userIdLabel_, Qt::AlignCenter | Qt::AlignTop); - setAutoFillBackground(true); - setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint); - setWindowModality(Qt::WindowModal); - QFont largeFont; largeFont.setPointSizeF(largeFont.pointSizeF() * 1.5); @@ -180,7 +180,8 @@ UserProfile::UserProfile(QWidget *parent) vlayout->setSpacing(WIDGET_SPACING); vlayout->setContentsMargins(WIDGET_MARGIN, TOP_WIDGET_MARGIN, WIDGET_MARGIN, WIDGET_MARGIN); - qRegisterMetaType>(); + static auto ignored = qRegisterMetaType>(); + (void)ignored; auto closeShortcut = new QShortcut(QKeySequence(QKeySequence::Cancel), this); connect(closeShortcut, &QShortcut::activated, this, &UserProfile::close);