Merge pull request #3988 from zhaowenlan1779/multiplayer-block

citra_qt/multiplayer: allow blocking other players in chat room
This commit is contained in:
Ben 2018-07-30 14:07:40 +02:00 committed by GitHub
commit b4524eb70b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 0 deletions

View File

@ -8,6 +8,8 @@
#include <QImage> #include <QImage>
#include <QList> #include <QList>
#include <QLocale> #include <QLocale>
#include <QMenu>
#include <QMessageBox>
#include <QMetaType> #include <QMetaType>
#include <QTime> #include <QTime>
#include <QtConcurrent/QtConcurrentRun> #include <QtConcurrent/QtConcurrentRun>
@ -77,6 +79,7 @@ ChatRoom::ChatRoom(QWidget* parent) : QWidget(parent), ui(std::make_unique<Ui::C
player_list = new QStandardItemModel(ui->player_view); player_list = new QStandardItemModel(ui->player_view);
ui->player_view->setModel(player_list); ui->player_view->setModel(player_list);
ui->player_view->setContextMenuPolicy(Qt::CustomContextMenu);
player_list->insertColumns(0, COLUMN_COUNT); player_list->insertColumns(0, COLUMN_COUNT);
player_list->setHeaderData(COLUMN_NAME, Qt::Horizontal, tr("Name")); player_list->setHeaderData(COLUMN_NAME, Qt::Horizontal, tr("Name"));
player_list->setHeaderData(COLUMN_GAME, Qt::Horizontal, tr("Game")); player_list->setHeaderData(COLUMN_GAME, Qt::Horizontal, tr("Game"));
@ -98,6 +101,8 @@ ChatRoom::ChatRoom(QWidget* parent) : QWidget(parent), ui(std::make_unique<Ui::C
} }
// Connect all the widgets to the appropriate events // Connect all the widgets to the appropriate events
connect(ui->player_view, &QTreeView::customContextMenuRequested, this,
&ChatRoom::PopupContextMenu);
connect(ui->chat_message, &QLineEdit::returnPressed, ui->send_message, &QPushButton::pressed); connect(ui->chat_message, &QLineEdit::returnPressed, ui->send_message, &QPushButton::pressed);
connect(ui->chat_message, &QLineEdit::textChanged, this, &::ChatRoom::OnChatTextChanged); connect(ui->chat_message, &QLineEdit::textChanged, this, &::ChatRoom::OnChatTextChanged);
connect(ui->send_message, &QPushButton::pressed, this, &ChatRoom::OnSendChat); connect(ui->send_message, &QPushButton::pressed, this, &ChatRoom::OnSendChat);
@ -107,6 +112,7 @@ ChatRoom::~ChatRoom() = default;
void ChatRoom::Clear() { void ChatRoom::Clear() {
ui->chat_history->clear(); ui->chat_history->clear();
block_list.clear();
} }
void ChatRoom::AppendStatusMessage(const QString& msg) { void ChatRoom::AppendStatusMessage(const QString& msg) {
@ -153,6 +159,11 @@ void ChatRoom::OnChatReceive(const Network::ChatEntry& chat) {
LOG_INFO(Network, "Chat message received from unknown player. Ignoring it."); LOG_INFO(Network, "Chat message received from unknown player. Ignoring it.");
return; return;
} }
if (block_list.count(chat.nickname)) {
LOG_INFO(Network, "Chat message received from blocked player {}. Ignoring it.",
chat.nickname);
return;
}
auto player = std::distance(members.begin(), it); auto player = std::distance(members.begin(), it);
ChatMessage m(chat); ChatMessage m(chat);
AppendChatMessage(m.GetPlayerChatMessage(player)); AppendChatMessage(m.GetPlayerChatMessage(player));
@ -209,3 +220,39 @@ void ChatRoom::OnChatTextChanged() {
if (ui->chat_message->text().length() > Network::MaxMessageSize) if (ui->chat_message->text().length() > Network::MaxMessageSize)
ui->chat_message->setText(ui->chat_message->text().left(Network::MaxMessageSize)); ui->chat_message->setText(ui->chat_message->text().left(Network::MaxMessageSize));
} }
void ChatRoom::PopupContextMenu(const QPoint& menu_location) {
QModelIndex item = ui->player_view->indexAt(menu_location);
if (!item.isValid())
return;
std::string nickname = player_list->item(item.row())->text().toStdString();
if (auto room = Network::GetRoomMember().lock()) {
// You can't block yourself
if (nickname == room->GetNickname())
return;
}
QMenu context_menu;
QAction* block_action = context_menu.addAction(tr("Block Player"));
block_action->setCheckable(true);
block_action->setChecked(block_list.count(nickname) > 0);
connect(block_action, &QAction::triggered, [this, nickname] {
if (block_list.count(nickname)) {
block_list.erase(nickname);
} else {
QMessageBox::StandardButton result = QMessageBox::question(
this, tr("Block Player"),
tr("When you block a player, you will no longer receive chat messages from "
"them.<br><br>Are you sure you would like to block %1?")
.arg(QString::fromStdString(nickname)),
QMessageBox::Yes | QMessageBox::No);
if (result == QMessageBox::Yes)
block_list.emplace(nickname);
}
});
context_menu.exec(ui->player_view->viewport()->mapToGlobal(menu_location));
}

View File

@ -5,6 +5,7 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include <unordered_set>
#include <QDialog> #include <QDialog>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include <QStandardItemModel> #include <QStandardItemModel>
@ -39,6 +40,7 @@ public slots:
void OnChatReceive(const Network::ChatEntry&); void OnChatReceive(const Network::ChatEntry&);
void OnSendChat(); void OnSendChat();
void OnChatTextChanged(); void OnChatTextChanged();
void PopupContextMenu(const QPoint& menu_location);
void Disable(); void Disable();
void Enable(); void Enable();
@ -51,6 +53,7 @@ private:
bool ValidateMessage(const std::string&); bool ValidateMessage(const std::string&);
QStandardItemModel* player_list; QStandardItemModel* player_list;
std::unique_ptr<Ui::ChatRoom> ui; std::unique_ptr<Ui::ChatRoom> ui;
std::unordered_set<std::string> block_list;
}; };
Q_DECLARE_METATYPE(Network::ChatEntry); Q_DECLARE_METATYPE(Network::ChatEntry);