| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #ifdef USE_DISCORD_PRESENCE |
| 5 | |
| 6 | #include "DolphinQt/DiscordJoinRequestDialog.h" |
| 7 | |
| 8 | #include <QGridLayout> |
| 9 | #include <QLabel> |
| 10 | #include <QPixmap> |
| 11 | #include <QPushButton> |
| 12 | |
| 13 | #include <discord_rpc.h> |
| 14 | #include <fmt/format.h> |
| 15 | |
| 16 | #include "Common/HttpRequest.h" |
| 17 | |
| 18 | DiscordJoinRequestDialog::DiscordJoinRequestDialog(QWidget* parent, const std::string& id, |
| 19 | const std::string& discord_tag, |
| 20 | const std::string& avatar) |
| 21 | : QDialog(parent), m_user_id(id), m_close_timestamp(std::time(nullptr) + s_max_lifetime_seconds) |
| 22 | { |
| 23 | setWindowTitle(tr("Request to Join Your Party")); |
| 24 | |
| 25 | QPixmap avatar_pixmap; |
| 26 | |
| 27 | if (!avatar.empty()) |
| 28 | { |
| 29 | const std::string avatar_endpoint = |
| 30 | fmt::format("https://cdn.discordapp.com/avatars/{}/{}.png", id, avatar); |
| 31 | |
| 32 | Common::HttpRequest request; |
| 33 | Common::HttpRequest::Response response = request.Get(avatar_endpoint); |
| 34 | |
| 35 | if (response.has_value()) |
| 36 | avatar_pixmap.loadFromData(response->data(), static_cast<uint>(response->size()), "png"); |
| 37 | } |
| 38 | |
| 39 | CreateLayout(discord_tag, avatar_pixmap); |
| 40 | ConnectWidgets(); |
| 41 | } |
| 42 | |
| 43 | std::time_t DiscordJoinRequestDialog::GetCloseTimestamp() const |
| 44 | { |
| 45 | return m_close_timestamp; |
| 46 | } |
| 47 | |
| 48 | void DiscordJoinRequestDialog::CreateLayout(const std::string& discord_tag, const QPixmap& avatar) |
| 49 | { |
| 50 | m_main_layout = new QGridLayout; |
| 51 | |
| 52 | m_invite_button = new QPushButton(tr("\u2714 Invite")); |
| 53 | m_decline_button = new QPushButton(tr("\u2716 Decline")); |
| 54 | m_ignore_button = new QPushButton(tr("Ignore")); |
| 55 | |
| 56 | QLabel* text = |
| 57 | new QLabel(tr("%1\nwants to join your party.").arg(QString::fromStdString(discord_tag))); |
| 58 | text->setAlignment(Qt::AlignCenter); |
| 59 | |
| 60 | if (!avatar.isNull()) |
| 61 | { |
| 62 | QLabel* picture = new QLabel(); |
| 63 | picture->setPixmap(avatar); |
| 64 | m_main_layout->addWidget(picture, 1, 0, 1, 3, Qt::AlignHCenter); |
| 65 | } |
| 66 | |
| 67 | m_main_layout->addWidget(text, 2, 0, 3, 3, Qt::AlignHCenter); |
| 68 | m_main_layout->addWidget(m_invite_button, 8, 0); |
| 69 | m_main_layout->addWidget(m_decline_button, 8, 1); |
| 70 | m_main_layout->addWidget(m_ignore_button, 8, 2); |
| 71 | |
| 72 | setLayout(m_main_layout); |
| 73 | } |
| 74 | |
| 75 | void DiscordJoinRequestDialog::ConnectWidgets() |
| 76 | { |
| 77 | connect(m_invite_button, &QPushButton::clicked, [this] { Reply(DISCORD_REPLY_YES); }); |
| 78 | connect(m_decline_button, &QPushButton::clicked, [this] { Reply(DISCORD_REPLY_NO); }); |
| 79 | connect(m_ignore_button, &QPushButton::clicked, [this] { Reply(DISCORD_REPLY_IGNORE); }); |
| 80 | connect(this, &QDialog::rejected, [this] { Reply(DISCORD_REPLY_IGNORE); }); |
| 81 | } |
| 82 | |
| 83 | void DiscordJoinRequestDialog::Reply(int reply) |
| 84 | { |
| 85 | Discord_Respond(m_user_id.c_str(), reply); |
| 86 | close(); |
| 87 | } |
| 88 | |
| 89 | #endif |