dolphin/Source/Core/DolphinQt/DiscordJoinRequestDialog.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

91 lines
2.7 KiB
C++
Raw Normal View History

// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#ifdef USE_DISCORD_PRESENCE
2018-08-06 15:56:40 -06:00
#include "DolphinQt/DiscordJoinRequestDialog.h"
#include <QGridLayout>
#include <QLabel>
#include <QPixmap>
#include <QPushButton>
#include <discord_rpc.h>
#include "Common/HttpRequest.h"
#include "Common/StringUtil.h"
2018-08-06 15:56:40 -06:00
DiscordJoinRequestDialog::DiscordJoinRequestDialog(QWidget* parent, const std::string& id,
const std::string& discord_tag,
2018-08-06 15:56:40 -06:00
const std::string& avatar)
: QDialog(parent), m_user_id(id), m_close_timestamp(std::time(nullptr) + s_max_lifetime_seconds)
{
setWindowTitle(tr("Request to Join Your Party"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
QPixmap avatar_pixmap;
2018-08-06 15:56:40 -06:00
if (!avatar.empty())
{
2018-08-06 15:56:40 -06:00
const std::string avatar_endpoint = StringFromFormat(
"https://cdn.discordapp.com/avatars/%s/%s.png", id.c_str(), avatar.c_str());
Common::HttpRequest request;
Common::HttpRequest::Response response = request.Get(avatar_endpoint);
if (response.has_value())
avatar_pixmap.loadFromData(response->data(), static_cast<uint>(response->size()), "png");
}
2018-08-06 15:56:40 -06:00
CreateLayout(discord_tag, avatar_pixmap);
ConnectWidgets();
}
std::time_t DiscordJoinRequestDialog::GetCloseTimestamp() const
{
return m_close_timestamp;
}
2018-08-06 15:56:40 -06:00
void DiscordJoinRequestDialog::CreateLayout(const std::string& discord_tag, const QPixmap& avatar)
{
m_main_layout = new QGridLayout;
2018-08-06 15:56:40 -06:00
m_invite_button = new QPushButton(tr("\u2714 Invite"));
m_decline_button = new QPushButton(tr("\u2716 Decline"));
m_ignore_button = new QPushButton(tr("Ignore"));
2018-08-06 15:56:40 -06:00
QLabel* text =
new QLabel(tr("%1\nwants to join your party.").arg(QString::fromStdString(discord_tag)));
text->setAlignment(Qt::AlignCenter);
if (!avatar.isNull())
{
QLabel* picture = new QLabel();
picture->setPixmap(avatar);
m_main_layout->addWidget(picture, 1, 0, 1, 3, Qt::AlignHCenter);
}
2018-08-06 15:56:40 -06:00
m_main_layout->addWidget(text, 2, 0, 3, 3, Qt::AlignHCenter);
m_main_layout->addWidget(m_invite_button, 8, 0);
m_main_layout->addWidget(m_decline_button, 8, 1);
m_main_layout->addWidget(m_ignore_button, 8, 2);
setLayout(m_main_layout);
}
void DiscordJoinRequestDialog::ConnectWidgets()
{
connect(m_invite_button, &QPushButton::clicked, [this] { Reply(DISCORD_REPLY_YES); });
connect(m_decline_button, &QPushButton::clicked, [this] { Reply(DISCORD_REPLY_NO); });
connect(m_ignore_button, &QPushButton::clicked, [this] { Reply(DISCORD_REPLY_IGNORE); });
connect(this, &QDialog::rejected, [this] { Reply(DISCORD_REPLY_IGNORE); });
}
void DiscordJoinRequestDialog::Reply(int reply)
{
2018-08-06 15:56:40 -06:00
Discord_Respond(m_user_id.c_str(), reply);
close();
}
#endif