2018-01-19 13:42:21 +00:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-04-09 18:00:56 +01:00
|
|
|
#include <array>
|
2018-01-19 13:42:21 +00:00
|
|
|
#include <future>
|
|
|
|
#include <QColor>
|
2018-12-16 15:10:48 +00:00
|
|
|
#include <QDesktopServices>
|
2018-10-27 08:49:00 +01:00
|
|
|
#include <QFutureWatcher>
|
2018-01-19 13:42:21 +00:00
|
|
|
#include <QImage>
|
|
|
|
#include <QList>
|
|
|
|
#include <QLocale>
|
2018-07-24 08:24:21 +01:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QMessageBox>
|
2018-01-19 13:42:21 +00:00
|
|
|
#include <QMetaType>
|
|
|
|
#include <QTime>
|
2018-10-27 08:49:00 +01:00
|
|
|
#include <QUrl>
|
2018-01-19 13:42:21 +00:00
|
|
|
#include <QtConcurrent/QtConcurrentRun>
|
|
|
|
#include "citra_qt/game_list_p.h"
|
|
|
|
#include "citra_qt/multiplayer/chat_room.h"
|
|
|
|
#include "citra_qt/multiplayer/message.h"
|
|
|
|
#include "common/logging/log.h"
|
2020-08-20 20:54:01 +01:00
|
|
|
#include "network/announce_multiplayer_session.h"
|
2018-04-18 17:29:03 +01:00
|
|
|
#include "ui_chat_room.h"
|
2018-10-27 08:49:00 +01:00
|
|
|
#ifdef ENABLE_WEB_SERVICE
|
|
|
|
#include "web_service/web_backend.h"
|
|
|
|
#endif
|
2018-01-19 13:42:21 +00:00
|
|
|
|
|
|
|
class ChatMessage {
|
|
|
|
public:
|
|
|
|
explicit ChatMessage(const Network::ChatEntry& chat, QTime ts = {}) {
|
|
|
|
/// Convert the time to their default locale defined format
|
2018-04-18 06:06:02 +01:00
|
|
|
QLocale locale;
|
2018-01-19 13:42:21 +00:00
|
|
|
timestamp = locale.toString(ts.isValid() ? ts : QTime::currentTime(), QLocale::ShortFormat);
|
|
|
|
nickname = QString::fromStdString(chat.nickname);
|
2018-10-27 08:49:00 +01:00
|
|
|
username = QString::fromStdString(chat.username);
|
2018-01-19 13:42:21 +00:00
|
|
|
message = QString::fromStdString(chat.message);
|
2018-12-01 01:28:55 +00:00
|
|
|
|
|
|
|
// Check for user pings
|
|
|
|
QString cur_nickname, cur_username;
|
|
|
|
if (auto room = Network::GetRoomMember().lock()) {
|
|
|
|
cur_nickname = QString::fromStdString(room->GetNickname());
|
|
|
|
cur_username = QString::fromStdString(room->GetUsername());
|
|
|
|
}
|
2019-01-13 14:21:04 +00:00
|
|
|
|
|
|
|
// Handle pings at the beginning and end of message
|
2019-11-09 01:45:47 +00:00
|
|
|
QString fixed_message = QStringLiteral(" %1 ").arg(message);
|
|
|
|
if (fixed_message.contains(QStringLiteral(" @%1 ").arg(cur_nickname)) ||
|
2019-01-13 14:21:04 +00:00
|
|
|
(!cur_username.isEmpty() &&
|
2019-11-09 01:45:47 +00:00
|
|
|
fixed_message.contains(QStringLiteral(" @%1 ").arg(cur_username)))) {
|
2018-12-01 01:28:55 +00:00
|
|
|
|
|
|
|
contains_ping = true;
|
|
|
|
} else {
|
|
|
|
contains_ping = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContainsPing() const {
|
|
|
|
return contains_ping;
|
2018-01-19 13:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Format the message using the players color
|
|
|
|
QString GetPlayerChatMessage(u16 player) const {
|
|
|
|
auto color = player_color[player % 16];
|
2018-10-27 08:49:00 +01:00
|
|
|
QString name;
|
|
|
|
if (username.isEmpty() || username == nickname) {
|
|
|
|
name = nickname;
|
|
|
|
} else {
|
2019-11-09 01:45:47 +00:00
|
|
|
name = QStringLiteral("%1 (%2)").arg(nickname, username);
|
2018-10-27 08:49:00 +01:00
|
|
|
}
|
2018-12-01 01:28:55 +00:00
|
|
|
|
2019-01-13 14:21:04 +00:00
|
|
|
QString style, text_color;
|
2018-12-01 01:28:55 +00:00
|
|
|
if (ContainsPing()) {
|
|
|
|
// Add a background color to these messages
|
2019-11-09 01:45:47 +00:00
|
|
|
style = QStringLiteral("background-color: %1").arg(QString::fromStdString(ping_color));
|
2019-01-13 14:21:04 +00:00
|
|
|
// Add a font color
|
2019-11-09 01:45:47 +00:00
|
|
|
text_color = QStringLiteral("color='#000000'");
|
2018-12-01 01:28:55 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 01:45:47 +00:00
|
|
|
return QStringLiteral("[%1] <font color='%2'><%3></font> <font style='%4' "
|
|
|
|
"%5>%6</font>")
|
|
|
|
.arg(timestamp, QString::fromStdString(color), name.toHtmlEscaped(), style, text_color,
|
2019-01-13 14:21:04 +00:00
|
|
|
message.toHtmlEscaped());
|
2018-01-19 13:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-04-09 18:00:56 +01:00
|
|
|
static constexpr std::array<const char*, 16> player_color = {
|
2018-01-19 13:42:21 +00:00
|
|
|
{"#0000FF", "#FF0000", "#8A2BE2", "#FF69B4", "#1E90FF", "#008000", "#00FF7F", "#B22222",
|
|
|
|
"#DAA520", "#FF4500", "#2E8B57", "#5F9EA0", "#D2691E", "#9ACD32", "#FF7F50", "FFFF00"}};
|
2018-12-01 01:28:55 +00:00
|
|
|
static constexpr char ping_color[] = "#FFFF00";
|
2018-04-09 18:00:56 +01:00
|
|
|
|
2018-01-19 13:42:21 +00:00
|
|
|
QString timestamp;
|
|
|
|
QString nickname;
|
2018-10-27 08:49:00 +01:00
|
|
|
QString username;
|
2018-01-19 13:42:21 +00:00
|
|
|
QString message;
|
2018-12-01 01:28:55 +00:00
|
|
|
bool contains_ping;
|
2018-01-19 13:42:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class StatusMessage {
|
|
|
|
public:
|
|
|
|
explicit StatusMessage(const QString& msg, QTime ts = {}) {
|
|
|
|
/// Convert the time to their default locale defined format
|
2018-04-09 18:00:56 +01:00
|
|
|
QLocale locale;
|
2018-01-19 13:42:21 +00:00
|
|
|
timestamp = locale.toString(ts.isValid() ? ts : QTime::currentTime(), QLocale::ShortFormat);
|
|
|
|
message = msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GetSystemChatMessage() const {
|
2019-11-09 01:45:47 +00:00
|
|
|
return QStringLiteral("[%1] <font color='%2'>* %3</font>")
|
|
|
|
.arg(timestamp, QString::fromStdString(system_color), message);
|
2018-01-19 13:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-11-09 13:55:57 +00:00
|
|
|
static constexpr const char system_color[] = "#FF8C00";
|
2018-01-19 13:42:21 +00:00
|
|
|
QString timestamp;
|
|
|
|
QString message;
|
|
|
|
};
|
|
|
|
|
2018-10-27 08:49:00 +01:00
|
|
|
class PlayerListItem : public QStandardItem {
|
|
|
|
public:
|
|
|
|
static const int NicknameRole = Qt::UserRole + 1;
|
|
|
|
static const int UsernameRole = Qt::UserRole + 2;
|
|
|
|
static const int AvatarUrlRole = Qt::UserRole + 3;
|
|
|
|
static const int GameNameRole = Qt::UserRole + 4;
|
|
|
|
|
|
|
|
PlayerListItem() = default;
|
|
|
|
explicit PlayerListItem(const std::string& nickname, const std::string& username,
|
|
|
|
const std::string& avatar_url, const std::string& game_name) {
|
|
|
|
setEditable(false);
|
|
|
|
setData(QString::fromStdString(nickname), NicknameRole);
|
|
|
|
setData(QString::fromStdString(username), UsernameRole);
|
|
|
|
setData(QString::fromStdString(avatar_url), AvatarUrlRole);
|
|
|
|
if (game_name.empty()) {
|
|
|
|
setData(QObject::tr("Not playing a game"), GameNameRole);
|
|
|
|
} else {
|
|
|
|
setData(QString::fromStdString(game_name), GameNameRole);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant data(int role) const override {
|
|
|
|
if (role != Qt::DisplayRole) {
|
|
|
|
return QStandardItem::data(role);
|
|
|
|
}
|
|
|
|
QString name;
|
|
|
|
const QString nickname = data(NicknameRole).toString();
|
|
|
|
const QString username = data(UsernameRole).toString();
|
|
|
|
if (username.isEmpty() || username == nickname) {
|
|
|
|
name = nickname;
|
|
|
|
} else {
|
2019-11-09 01:45:47 +00:00
|
|
|
name = QStringLiteral("%1 (%2)").arg(nickname, username);
|
2018-10-27 08:49:00 +01:00
|
|
|
}
|
2019-11-09 01:45:47 +00:00
|
|
|
return QStringLiteral("%1\n %2").arg(name, data(GameNameRole).toString());
|
2018-10-27 08:49:00 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-18 06:06:02 +01:00
|
|
|
ChatRoom::ChatRoom(QWidget* parent) : QWidget(parent), ui(std::make_unique<Ui::ChatRoom>()) {
|
2018-01-19 13:42:21 +00:00
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
// set the item_model for player_view
|
|
|
|
|
|
|
|
player_list = new QStandardItemModel(ui->player_view);
|
|
|
|
ui->player_view->setModel(player_list);
|
2018-07-24 08:24:21 +01:00
|
|
|
ui->player_view->setContextMenuPolicy(Qt::CustomContextMenu);
|
2018-10-27 08:49:00 +01:00
|
|
|
// set a header to make it look better though there is only one column
|
|
|
|
player_list->insertColumns(0, 1);
|
|
|
|
player_list->setHeaderData(0, Qt::Horizontal, tr("Members"));
|
2018-01-19 13:42:21 +00:00
|
|
|
|
|
|
|
ui->chat_history->document()->setMaximumBlockCount(max_chat_lines);
|
|
|
|
|
|
|
|
// register the network structs to use in slots and signals
|
|
|
|
qRegisterMetaType<Network::ChatEntry>();
|
2018-11-09 13:55:57 +00:00
|
|
|
qRegisterMetaType<Network::StatusMessageEntry>();
|
2018-01-19 13:42:21 +00:00
|
|
|
qRegisterMetaType<Network::RoomInformation>();
|
|
|
|
qRegisterMetaType<Network::RoomMember::State>();
|
|
|
|
|
|
|
|
// setup the callbacks for network updates
|
|
|
|
if (auto member = Network::GetRoomMember().lock()) {
|
|
|
|
member->BindOnChatMessageRecieved(
|
|
|
|
[this](const Network::ChatEntry& chat) { emit ChatReceived(chat); });
|
2018-11-09 13:55:57 +00:00
|
|
|
member->BindOnStatusMessageReceived(
|
|
|
|
[this](const Network::StatusMessageEntry& status_message) {
|
|
|
|
emit StatusMessageReceived(status_message);
|
|
|
|
});
|
2018-01-19 13:42:21 +00:00
|
|
|
connect(this, &ChatRoom::ChatReceived, this, &ChatRoom::OnChatReceive);
|
2018-11-09 13:55:57 +00:00
|
|
|
connect(this, &ChatRoom::StatusMessageReceived, this, &ChatRoom::OnStatusMessageReceive);
|
2018-01-19 13:42:21 +00:00
|
|
|
} else {
|
|
|
|
// TODO (jroweboy) network was not initialized?
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect all the widgets to the appropriate events
|
2018-07-24 08:24:21 +01:00
|
|
|
connect(ui->player_view, &QTreeView::customContextMenuRequested, this,
|
|
|
|
&ChatRoom::PopupContextMenu);
|
2019-07-22 22:28:10 +01:00
|
|
|
connect(ui->chat_message, &QLineEdit::returnPressed, this, &ChatRoom::OnSendChat);
|
|
|
|
connect(ui->chat_message, &QLineEdit::textChanged, this, &ChatRoom::OnChatTextChanged);
|
|
|
|
connect(ui->send_message, &QPushButton::clicked, this, &ChatRoom::OnSendChat);
|
2018-01-19 13:42:21 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 17:29:03 +01:00
|
|
|
ChatRoom::~ChatRoom() = default;
|
|
|
|
|
2018-11-24 08:26:12 +00:00
|
|
|
void ChatRoom::SetModPerms(bool is_mod) {
|
|
|
|
has_mod_perms = is_mod;
|
|
|
|
}
|
|
|
|
|
2018-10-09 16:08:33 +01:00
|
|
|
void ChatRoom::RetranslateUi() {
|
|
|
|
ui->retranslateUi(this);
|
|
|
|
}
|
|
|
|
|
2018-01-19 13:42:21 +00:00
|
|
|
void ChatRoom::Clear() {
|
|
|
|
ui->chat_history->clear();
|
2018-07-24 08:24:21 +01:00
|
|
|
block_list.clear();
|
2018-01-19 13:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatRoom::AppendStatusMessage(const QString& msg) {
|
|
|
|
ui->chat_history->append(StatusMessage(msg).GetSystemChatMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatRoom::AppendChatMessage(const QString& msg) {
|
|
|
|
ui->chat_history->append(msg);
|
|
|
|
}
|
|
|
|
|
2018-11-24 08:26:12 +00:00
|
|
|
void ChatRoom::SendModerationRequest(Network::RoomMessageTypes type, const std::string& nickname) {
|
|
|
|
if (auto room = Network::GetRoomMember().lock()) {
|
|
|
|
auto members = room->GetMemberInformation();
|
|
|
|
auto it = std::find_if(members.begin(), members.end(),
|
|
|
|
[&nickname](const Network::RoomMember::MemberInformation& member) {
|
|
|
|
return member.nickname == nickname;
|
|
|
|
});
|
|
|
|
if (it == members.end()) {
|
2020-02-29 23:48:58 +00:00
|
|
|
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::NO_SUCH_USER);
|
2018-11-24 08:26:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
room->SendModerationRequest(type, nickname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 13:42:21 +00:00
|
|
|
bool ChatRoom::ValidateMessage(const std::string& msg) {
|
|
|
|
return !msg.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatRoom::OnRoomUpdate(const Network::RoomInformation& info) {
|
|
|
|
// TODO(B3N30): change title
|
|
|
|
if (auto room_member = Network::GetRoomMember().lock()) {
|
|
|
|
SetPlayerList(room_member->GetMemberInformation());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatRoom::Disable() {
|
|
|
|
ui->send_message->setDisabled(true);
|
|
|
|
ui->chat_message->setDisabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatRoom::Enable() {
|
|
|
|
ui->send_message->setEnabled(true);
|
|
|
|
ui->chat_message->setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatRoom::OnChatReceive(const Network::ChatEntry& chat) {
|
|
|
|
if (!ValidateMessage(chat.message)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auto room = Network::GetRoomMember().lock()) {
|
|
|
|
// get the id of the player
|
|
|
|
auto members = room->GetMemberInformation();
|
|
|
|
auto it = std::find_if(members.begin(), members.end(),
|
|
|
|
[&chat](const Network::RoomMember::MemberInformation& member) {
|
2018-10-27 08:49:00 +01:00
|
|
|
return member.nickname == chat.nickname &&
|
|
|
|
member.username == chat.username;
|
2018-01-19 13:42:21 +00:00
|
|
|
});
|
|
|
|
if (it == members.end()) {
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_INFO(Network, "Chat message received from unknown player. Ignoring it.");
|
2018-01-19 13:42:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-07-24 08:24:21 +01:00
|
|
|
if (block_list.count(chat.nickname)) {
|
|
|
|
LOG_INFO(Network, "Chat message received from blocked player {}. Ignoring it.",
|
|
|
|
chat.nickname);
|
|
|
|
return;
|
|
|
|
}
|
2018-01-19 13:42:21 +00:00
|
|
|
auto player = std::distance(members.begin(), it);
|
|
|
|
ChatMessage m(chat);
|
2018-12-01 01:28:55 +00:00
|
|
|
if (m.ContainsPing()) {
|
|
|
|
emit UserPinged();
|
|
|
|
}
|
2018-01-19 13:42:21 +00:00
|
|
|
AppendChatMessage(m.GetPlayerChatMessage(player));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 13:55:57 +00:00
|
|
|
void ChatRoom::OnStatusMessageReceive(const Network::StatusMessageEntry& status_message) {
|
|
|
|
QString name;
|
|
|
|
if (status_message.username.empty() || status_message.username == status_message.nickname) {
|
|
|
|
name = QString::fromStdString(status_message.nickname);
|
|
|
|
} else {
|
2019-11-09 01:45:47 +00:00
|
|
|
name = QStringLiteral("%1 (%2)").arg(QString::fromStdString(status_message.nickname),
|
|
|
|
QString::fromStdString(status_message.username));
|
2018-11-09 13:55:57 +00:00
|
|
|
}
|
|
|
|
QString message;
|
|
|
|
switch (status_message.type) {
|
|
|
|
case Network::IdMemberJoin:
|
|
|
|
message = tr("%1 has joined").arg(name);
|
|
|
|
break;
|
|
|
|
case Network::IdMemberLeave:
|
|
|
|
message = tr("%1 has left").arg(name);
|
|
|
|
break;
|
2018-11-24 08:26:12 +00:00
|
|
|
case Network::IdMemberKicked:
|
|
|
|
message = tr("%1 has been kicked").arg(name);
|
|
|
|
break;
|
|
|
|
case Network::IdMemberBanned:
|
|
|
|
message = tr("%1 has been banned").arg(name);
|
|
|
|
break;
|
|
|
|
case Network::IdAddressUnbanned:
|
|
|
|
message = tr("%1 has been unbanned").arg(name);
|
|
|
|
break;
|
2018-11-09 13:55:57 +00:00
|
|
|
}
|
|
|
|
if (!message.isEmpty())
|
|
|
|
AppendStatusMessage(message);
|
|
|
|
}
|
|
|
|
|
2018-01-19 13:42:21 +00:00
|
|
|
void ChatRoom::OnSendChat() {
|
|
|
|
if (auto room = Network::GetRoomMember().lock()) {
|
2018-12-15 09:13:46 +00:00
|
|
|
if (room->GetState() != Network::RoomMember::State::Joined &&
|
|
|
|
room->GetState() != Network::RoomMember::State::Moderator) {
|
|
|
|
|
2018-04-09 18:00:56 +01:00
|
|
|
return;
|
2018-01-19 13:42:21 +00:00
|
|
|
}
|
2018-04-09 18:00:56 +01:00
|
|
|
auto message = ui->chat_message->text().toStdString();
|
|
|
|
if (!ValidateMessage(message)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto nick = room->GetNickname();
|
2018-10-27 08:49:00 +01:00
|
|
|
auto username = room->GetUsername();
|
|
|
|
Network::ChatEntry chat{nick, username, message};
|
2018-04-09 18:00:56 +01:00
|
|
|
|
|
|
|
auto members = room->GetMemberInformation();
|
|
|
|
auto it = std::find_if(members.begin(), members.end(),
|
|
|
|
[&chat](const Network::RoomMember::MemberInformation& member) {
|
2018-10-27 08:49:00 +01:00
|
|
|
return member.nickname == chat.nickname &&
|
|
|
|
member.username == chat.username;
|
2018-04-09 18:00:56 +01:00
|
|
|
});
|
|
|
|
if (it == members.end()) {
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_INFO(Network, "Cannot find self in the player list when sending a message.");
|
2018-04-09 18:00:56 +01:00
|
|
|
}
|
|
|
|
auto player = std::distance(members.begin(), it);
|
|
|
|
ChatMessage m(chat);
|
|
|
|
room->SendChatMessage(message);
|
|
|
|
AppendChatMessage(m.GetPlayerChatMessage(player));
|
|
|
|
ui->chat_message->clear();
|
2018-01-19 13:42:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-27 08:49:00 +01:00
|
|
|
void ChatRoom::UpdateIconDisplay() {
|
|
|
|
for (int row = 0; row < player_list->invisibleRootItem()->rowCount(); ++row) {
|
|
|
|
QStandardItem* item = player_list->invisibleRootItem()->child(row);
|
|
|
|
const std::string avatar_url =
|
|
|
|
item->data(PlayerListItem::AvatarUrlRole).toString().toStdString();
|
|
|
|
if (icon_cache.count(avatar_url)) {
|
|
|
|
item->setData(icon_cache.at(avatar_url), Qt::DecorationRole);
|
2019-01-16 14:01:21 +00:00
|
|
|
} else {
|
2019-11-09 01:45:47 +00:00
|
|
|
item->setData(QIcon::fromTheme(QStringLiteral("no_avatar")).pixmap(48),
|
|
|
|
Qt::DecorationRole);
|
2018-10-27 08:49:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 13:42:21 +00:00
|
|
|
void ChatRoom::SetPlayerList(const Network::RoomMember::MemberList& member_list) {
|
|
|
|
// TODO(B3N30): Remember which row is selected
|
|
|
|
player_list->removeRows(0, player_list->rowCount());
|
|
|
|
for (const auto& member : member_list) {
|
2018-04-09 18:00:56 +01:00
|
|
|
if (member.nickname.empty())
|
2018-01-19 13:42:21 +00:00
|
|
|
continue;
|
2018-10-27 08:49:00 +01:00
|
|
|
QStandardItem* name_item = new PlayerListItem(member.nickname, member.username,
|
|
|
|
member.avatar_url, member.game_info.name);
|
|
|
|
|
|
|
|
#ifdef ENABLE_WEB_SERVICE
|
2019-01-16 14:01:21 +00:00
|
|
|
if (!icon_cache.count(member.avatar_url) && !member.avatar_url.empty()) {
|
|
|
|
// Start a request to get the member's avatar
|
|
|
|
const QUrl url(QString::fromStdString(member.avatar_url));
|
|
|
|
QFuture<std::string> future = QtConcurrent::run([url] {
|
|
|
|
WebService::Client client(
|
2019-11-09 01:45:47 +00:00
|
|
|
QStringLiteral("%1://%2").arg(url.scheme(), url.host()).toStdString(), "", "");
|
2019-01-16 14:01:21 +00:00
|
|
|
auto result = client.GetImage(url.path().toStdString(), true);
|
|
|
|
if (result.returned_data.empty()) {
|
|
|
|
LOG_ERROR(WebService, "Failed to get avatar");
|
|
|
|
}
|
|
|
|
return result.returned_data;
|
|
|
|
});
|
|
|
|
auto* future_watcher = new QFutureWatcher<std::string>(this);
|
|
|
|
connect(future_watcher, &QFutureWatcher<std::string>::finished, this,
|
|
|
|
[this, future_watcher, avatar_url = member.avatar_url] {
|
|
|
|
const std::string result = future_watcher->result();
|
|
|
|
if (result.empty())
|
|
|
|
return;
|
|
|
|
QPixmap pixmap;
|
|
|
|
if (!pixmap.loadFromData(reinterpret_cast<const u8*>(result.data()),
|
2022-05-12 05:23:57 +01:00
|
|
|
static_cast<u32>(result.size())))
|
2019-01-16 14:01:21 +00:00
|
|
|
return;
|
|
|
|
icon_cache[avatar_url] =
|
|
|
|
pixmap.scaled(48, 48, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
|
|
|
// Update all the displayed icons with the new icon_cache
|
|
|
|
UpdateIconDisplay();
|
|
|
|
});
|
|
|
|
future_watcher->setFuture(future);
|
2018-01-19 13:42:21 +00:00
|
|
|
}
|
2019-01-16 14:01:21 +00:00
|
|
|
#endif
|
2018-10-27 08:49:00 +01:00
|
|
|
|
|
|
|
player_list->invisibleRootItem()->appendRow(name_item);
|
2018-01-19 13:42:21 +00:00
|
|
|
}
|
2019-01-16 14:01:21 +00:00
|
|
|
UpdateIconDisplay();
|
2018-01-19 13:42:21 +00:00
|
|
|
// TODO(B3N30): Restore row selection
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatRoom::OnChatTextChanged() {
|
2019-07-14 19:17:54 +01:00
|
|
|
if (ui->chat_message->text().length() > static_cast<int>(Network::MaxMessageSize))
|
|
|
|
ui->chat_message->setText(
|
|
|
|
ui->chat_message->text().left(static_cast<int>(Network::MaxMessageSize)));
|
2018-01-19 13:42:21 +00:00
|
|
|
}
|
2018-07-24 08:24:21 +01:00
|
|
|
|
|
|
|
void ChatRoom::PopupContextMenu(const QPoint& menu_location) {
|
|
|
|
QModelIndex item = ui->player_view->indexAt(menu_location);
|
|
|
|
if (!item.isValid())
|
|
|
|
return;
|
|
|
|
|
2018-10-27 08:49:00 +01:00
|
|
|
std::string nickname =
|
|
|
|
player_list->item(item.row())->data(PlayerListItem::NicknameRole).toString().toStdString();
|
2018-07-24 08:24:21 +01:00
|
|
|
|
|
|
|
QMenu context_menu;
|
|
|
|
|
2018-12-16 15:10:48 +00:00
|
|
|
QString username = player_list->item(item.row())->data(PlayerListItem::UsernameRole).toString();
|
|
|
|
if (!username.isEmpty()) {
|
|
|
|
QAction* view_profile_action = context_menu.addAction(tr("View Profile"));
|
|
|
|
connect(view_profile_action, &QAction::triggered, [username] {
|
|
|
|
QDesktopServices::openUrl(
|
2019-11-09 01:45:47 +00:00
|
|
|
QUrl(QStringLiteral("https://community.citra-emu.org/u/%1").arg(username)));
|
2018-12-16 15:10:48 +00:00
|
|
|
});
|
|
|
|
}
|
2018-07-24 08:24:21 +01:00
|
|
|
|
2018-12-16 15:10:48 +00:00
|
|
|
std::string cur_nickname;
|
|
|
|
if (auto room = Network::GetRoomMember().lock()) {
|
|
|
|
cur_nickname = room->GetNickname();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nickname != cur_nickname) { // You can't block yourself
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-07-24 08:24:21 +01:00
|
|
|
|
2018-12-16 15:10:48 +00:00
|
|
|
if (has_mod_perms && nickname != cur_nickname) { // You can't kick or ban yourself
|
2018-11-24 08:26:12 +00:00
|
|
|
context_menu.addSeparator();
|
|
|
|
|
|
|
|
QAction* kick_action = context_menu.addAction(tr("Kick"));
|
|
|
|
QAction* ban_action = context_menu.addAction(tr("Ban"));
|
|
|
|
|
|
|
|
connect(kick_action, &QAction::triggered, [this, nickname] {
|
|
|
|
QMessageBox::StandardButton result =
|
|
|
|
QMessageBox::question(this, tr("Kick Player"),
|
|
|
|
tr("Are you sure you would like to <b>kick</b> %1?")
|
|
|
|
.arg(QString::fromStdString(nickname)),
|
|
|
|
QMessageBox::Yes | QMessageBox::No);
|
|
|
|
if (result == QMessageBox::Yes)
|
|
|
|
SendModerationRequest(Network::IdModKick, nickname);
|
|
|
|
});
|
|
|
|
connect(ban_action, &QAction::triggered, [this, nickname] {
|
|
|
|
QMessageBox::StandardButton result = QMessageBox::question(
|
|
|
|
this, tr("Ban Player"),
|
|
|
|
tr("Are you sure you would like to <b>kick and ban</b> %1?\n\nThis would "
|
|
|
|
"ban both their forum username and their IP address.")
|
|
|
|
.arg(QString::fromStdString(nickname)),
|
|
|
|
QMessageBox::Yes | QMessageBox::No);
|
|
|
|
if (result == QMessageBox::Yes)
|
|
|
|
SendModerationRequest(Network::IdModBan, nickname);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-24 08:24:21 +01:00
|
|
|
context_menu.exec(ui->player_view->viewport()->mapToGlobal(menu_location));
|
|
|
|
}
|