From 921037a7c30a069e4a1cb80adddcea7d25b29f46 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Tue, 24 Jul 2018 15:24:21 +0800 Subject: [PATCH] citra_qt/multiplayer: allow blocking other players in chat room --- src/citra_qt/multiplayer/chat_room.cpp | 47 ++++++++++++++++++++++++++ src/citra_qt/multiplayer/chat_room.h | 3 ++ 2 files changed, 50 insertions(+) diff --git a/src/citra_qt/multiplayer/chat_room.cpp b/src/citra_qt/multiplayer/chat_room.cpp index de25949db..0c1a2310c 100644 --- a/src/citra_qt/multiplayer/chat_room.cpp +++ b/src/citra_qt/multiplayer/chat_room.cpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include #include #include @@ -77,6 +79,7 @@ ChatRoom::ChatRoom(QWidget* parent) : QWidget(parent), ui(std::make_uniqueplayer_view); ui->player_view->setModel(player_list); + ui->player_view->setContextMenuPolicy(Qt::CustomContextMenu); player_list->insertColumns(0, COLUMN_COUNT); player_list->setHeaderData(COLUMN_NAME, Qt::Horizontal, tr("Name")); player_list->setHeaderData(COLUMN_GAME, Qt::Horizontal, tr("Game")); @@ -98,6 +101,8 @@ ChatRoom::ChatRoom(QWidget* parent) : QWidget(parent), ui(std::make_uniqueplayer_view, &QTreeView::customContextMenuRequested, this, + &ChatRoom::PopupContextMenu); connect(ui->chat_message, &QLineEdit::returnPressed, ui->send_message, &QPushButton::pressed); connect(ui->chat_message, &QLineEdit::textChanged, this, &::ChatRoom::OnChatTextChanged); connect(ui->send_message, &QPushButton::pressed, this, &ChatRoom::OnSendChat); @@ -107,6 +112,7 @@ ChatRoom::~ChatRoom() = default; void ChatRoom::Clear() { ui->chat_history->clear(); + block_list.clear(); } 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."); 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); ChatMessage m(chat); AppendChatMessage(m.GetPlayerChatMessage(player)); @@ -209,3 +220,39 @@ void ChatRoom::OnChatTextChanged() { if (ui->chat_message->text().length() > 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.

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)); +} diff --git a/src/citra_qt/multiplayer/chat_room.h b/src/citra_qt/multiplayer/chat_room.h index 4604c3395..284f02bca 100644 --- a/src/citra_qt/multiplayer/chat_room.h +++ b/src/citra_qt/multiplayer/chat_room.h @@ -5,6 +5,7 @@ #pragma once #include +#include #include #include #include @@ -39,6 +40,7 @@ public slots: void OnChatReceive(const Network::ChatEntry&); void OnSendChat(); void OnChatTextChanged(); + void PopupContextMenu(const QPoint& menu_location); void Disable(); void Enable(); @@ -51,6 +53,7 @@ private: bool ValidateMessage(const std::string&); QStandardItemModel* player_list; std::unique_ptr ui; + std::unordered_set block_list; }; Q_DECLARE_METATYPE(Network::ChatEntry);