| 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/DiscordHandler.h" |
| 7 | |
| 8 | #include <chrono> |
| 9 | |
| 10 | #include <QApplication> |
| 11 | |
| 12 | #include "Common/Thread.h" |
| 13 | |
| 14 | #include "UICommon/DiscordPresence.h" |
| 15 | |
| 16 | #include "DolphinQt/DiscordJoinRequestDialog.h" |
| 17 | #include "DolphinQt/QtUtils/RunOnObject.h" |
| 18 | |
| 19 | DiscordHandler::DiscordHandler(QWidget* parent) : QObject{parent}, m_parent{parent} |
| 20 | { |
| 21 | connect(this, &DiscordHandler::JoinRequest, this, &DiscordHandler::ShowNewJoinRequest); |
| 22 | } |
| 23 | |
| 24 | DiscordHandler::~DiscordHandler() |
| 25 | { |
| 26 | Stop(); |
| 27 | } |
| 28 | |
| 29 | void DiscordHandler::Start() |
| 30 | { |
| 31 | m_stop_requested.Set(false); |
| 32 | m_thread = std::thread(&DiscordHandler::Run, this); |
| 33 | } |
| 34 | |
| 35 | void DiscordHandler::Stop() |
| 36 | { |
| 37 | if (!m_thread.joinable()) |
| 38 | return; |
| 39 | |
| 40 | m_stop_requested.Set(true); |
| 41 | m_wakeup_event.Set(); |
| 42 | m_thread.join(); |
| 43 | } |
| 44 | |
| 45 | void DiscordHandler::DiscordJoinRequest(const char* id, const std::string& discord_tag, |
| 46 | const char* avatar) |
| 47 | { |
| 48 | emit DiscordHandler::JoinRequest(id, discord_tag, avatar); |
| 49 | } |
| 50 | |
| 51 | void DiscordHandler::DiscordJoin() |
| 52 | { |
| 53 | emit DiscordHandler::Join(); |
| 54 | } |
| 55 | |
| 56 | void DiscordHandler::ShowNewJoinRequest(const std::string& id, const std::string& discord_tag, |
| 57 | const std::string& avatar) |
| 58 | { |
| 59 | std::lock_guard<std::mutex> lock(m_request_dialogs_mutex); |
| 60 | m_request_dialogs.emplace_front(m_parent, id, discord_tag, avatar); |
| 61 | DiscordJoinRequestDialog& request_dialog = m_request_dialogs.front(); |
| 62 | request_dialog.show(); |
| 63 | request_dialog.raise(); |
| 64 | request_dialog.activateWindow(); |
| 65 | QApplication::alert(nullptr, DiscordJoinRequestDialog::s_max_lifetime_seconds * 1000); |
| 66 | } |
| 67 | |
| 68 | void DiscordHandler::Run() |
| 69 | { |
| 70 | Common::SetCurrentThreadName("DiscordHandler"); |
| 71 | |
| 72 | while (!m_stop_requested.IsSet()) |
| 73 | { |
| 74 | Discord::CallPendingCallbacks(); |
| 75 | |
| 76 | // close and remove dead requests |
| 77 | { |
| 78 | std::lock_guard<std::mutex> lock(m_request_dialogs_mutex); |
| 79 | for (auto request_dialog = m_request_dialogs.begin(); |
| 80 | request_dialog != m_request_dialogs.end();) |
| 81 | { |
| 82 | if (std::time(nullptr) < request_dialog->GetCloseTimestamp()) |
| 83 | { |
| 84 | ++request_dialog; |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | RunOnObject(m_parent, [this, &request_dialog] { |
| 89 | request_dialog->close(); |
| 90 | request_dialog = m_request_dialogs.erase(request_dialog); |
| 91 | return nullptr; |
| 92 | }); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | m_wakeup_event.WaitFor(std::chrono::seconds{2}); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | #endif |