2018-07-20 16:27:43 -06:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#ifdef USE_DISCORD_PRESENCE
|
|
|
|
|
2018-08-06 15:56:40 -06:00
|
|
|
#include "DolphinQt/DiscordHandler.h"
|
|
|
|
|
2019-06-13 15:34:25 -06:00
|
|
|
#include <chrono>
|
2018-07-20 16:27:43 -06:00
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
|
|
|
#include "Common/Thread.h"
|
|
|
|
|
|
|
|
#include "UICommon/DiscordPresence.h"
|
|
|
|
|
2018-08-06 15:56:40 -06:00
|
|
|
#include "DolphinQt/DiscordJoinRequestDialog.h"
|
|
|
|
#include "DolphinQt/QtUtils/RunOnObject.h"
|
|
|
|
|
2018-07-20 16:27:43 -06:00
|
|
|
DiscordHandler::DiscordHandler(QWidget* parent) : QObject{parent}, m_parent{parent}
|
|
|
|
{
|
|
|
|
connect(this, &DiscordHandler::JoinRequest, this, &DiscordHandler::ShowNewJoinRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
DiscordHandler::~DiscordHandler()
|
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiscordHandler::Start()
|
|
|
|
{
|
|
|
|
m_stop_requested.Set(false);
|
|
|
|
m_thread = std::thread(&DiscordHandler::Run, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiscordHandler::Stop()
|
|
|
|
{
|
2019-06-13 15:34:25 -06:00
|
|
|
if (!m_thread.joinable())
|
|
|
|
return;
|
2018-07-20 16:27:43 -06:00
|
|
|
|
2019-06-13 15:34:25 -06:00
|
|
|
m_stop_requested.Set(true);
|
|
|
|
m_wakeup_event.Set();
|
|
|
|
m_thread.join();
|
2018-07-20 16:27:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiscordHandler::DiscordJoinRequest(const char* id, const std::string& discord_tag,
|
|
|
|
const char* avatar)
|
|
|
|
{
|
2018-08-06 15:56:40 -06:00
|
|
|
emit DiscordHandler::JoinRequest(id, discord_tag, avatar);
|
2018-07-20 16:27:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiscordHandler::DiscordJoin()
|
|
|
|
{
|
|
|
|
emit DiscordHandler::Join();
|
|
|
|
}
|
|
|
|
|
2018-08-06 15:56:40 -06:00
|
|
|
void DiscordHandler::ShowNewJoinRequest(const std::string& id, const std::string& discord_tag,
|
|
|
|
const std::string& avatar)
|
2018-07-20 16:27:43 -06:00
|
|
|
{
|
2018-08-06 15:56:40 -06:00
|
|
|
std::lock_guard<std::mutex> lock(m_request_dialogs_mutex);
|
|
|
|
m_request_dialogs.emplace_front(m_parent, id, discord_tag, avatar);
|
|
|
|
DiscordJoinRequestDialog& request_dialog = m_request_dialogs.front();
|
|
|
|
request_dialog.show();
|
|
|
|
request_dialog.raise();
|
|
|
|
request_dialog.activateWindow();
|
2018-07-20 16:27:43 -06:00
|
|
|
QApplication::alert(nullptr, DiscordJoinRequestDialog::s_max_lifetime_seconds * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiscordHandler::Run()
|
|
|
|
{
|
2020-08-22 03:55:31 -06:00
|
|
|
Common::SetCurrentThreadName("DiscordHandler");
|
|
|
|
|
2018-07-20 16:27:43 -06:00
|
|
|
while (!m_stop_requested.IsSet())
|
|
|
|
{
|
2019-06-13 15:34:25 -06:00
|
|
|
Discord::CallPendingCallbacks();
|
2018-07-20 16:27:43 -06:00
|
|
|
|
|
|
|
// close and remove dead requests
|
|
|
|
{
|
2018-08-06 15:56:40 -06:00
|
|
|
std::lock_guard<std::mutex> lock(m_request_dialogs_mutex);
|
|
|
|
for (auto request_dialog = m_request_dialogs.begin();
|
|
|
|
request_dialog != m_request_dialogs.end();)
|
|
|
|
{
|
|
|
|
if (std::time(nullptr) < request_dialog->GetCloseTimestamp())
|
|
|
|
{
|
|
|
|
++request_dialog;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RunOnObject(m_parent, [this, &request_dialog] {
|
|
|
|
request_dialog->close();
|
|
|
|
request_dialog = m_request_dialogs.erase(request_dialog);
|
|
|
|
return nullptr;
|
|
|
|
});
|
|
|
|
}
|
2018-07-20 16:27:43 -06:00
|
|
|
}
|
|
|
|
|
2019-06-13 15:34:25 -06:00
|
|
|
m_wakeup_event.WaitFor(std::chrono::seconds{2});
|
2018-07-20 16:27:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|