summaryrefslogtreecommitdiff
path: root/src/dialogs/joinguild.cpp
diff options
context:
space:
mode:
authorDylam De La Torre <DyXel04@gmail.com>2021-11-23 05:21:56 +0100
committerGitHub <noreply@github.com>2021-11-23 04:21:56 +0000
commita51a54bc5979a2491f152abc47ad54e6b63f27c8 (patch)
treece67092b2f6df366033a65a6111e4650866766b2 /src/dialogs/joinguild.cpp
parentd88079000a79e6bcbe51c5a2868d57b303b5fcb6 (diff)
downloadabaddon-portaudio-a51a54bc5979a2491f152abc47ad54e6b63f27c8.tar.gz
abaddon-portaudio-a51a54bc5979a2491f152abc47ad54e6b63f27c8.zip
Restructure source and resource files (#46)
importantly, res is now res/res and css is now res/css
Diffstat (limited to 'src/dialogs/joinguild.cpp')
-rw-r--r--src/dialogs/joinguild.cpp97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/dialogs/joinguild.cpp b/src/dialogs/joinguild.cpp
new file mode 100644
index 0000000..14fab53
--- /dev/null
+++ b/src/dialogs/joinguild.cpp
@@ -0,0 +1,97 @@
+#include "joinguild.hpp"
+#include "abaddon.hpp"
+#include <nlohmann/json.hpp>
+#include <regex>
+
+JoinGuildDialog::JoinGuildDialog(Gtk::Window &parent)
+ : Gtk::Dialog("Join Server", parent, true)
+ , m_layout(Gtk::ORIENTATION_VERTICAL)
+ , m_ok("OK")
+ , m_cancel("Cancel")
+ , m_info("Enter code") {
+ set_default_size(300, 50);
+ get_style_context()->add_class("app-window");
+ get_style_context()->add_class("app-popup");
+
+ Glib::signal_idle().connect(sigc::mem_fun(*this, &JoinGuildDialog::on_idle_slot));
+
+ m_entry.signal_changed().connect(sigc::mem_fun(*this, &JoinGuildDialog::on_entry_changed));
+
+ m_ok.set_sensitive(false);
+
+ m_ok.signal_clicked().connect([&]() {
+ response(Gtk::RESPONSE_OK);
+ });
+
+ m_cancel.signal_clicked().connect([&]() {
+ response(Gtk::RESPONSE_CANCEL);
+ });
+
+ m_entry.set_hexpand(true);
+ m_layout.add(m_entry);
+ m_lower.set_hexpand(true);
+ m_lower.pack_start(m_info);
+ m_info.set_halign(Gtk::ALIGN_START);
+ m_lower.pack_start(m_ok, Gtk::PACK_SHRINK);
+ m_lower.pack_start(m_cancel, Gtk::PACK_SHRINK);
+ m_ok.set_halign(Gtk::ALIGN_END);
+ m_cancel.set_halign(Gtk::ALIGN_END);
+ m_layout.add(m_lower);
+ get_content_area()->add(m_layout);
+
+ show_all_children();
+}
+
+void JoinGuildDialog::on_entry_changed() {
+ std::string s = m_entry.get_text();
+ std::regex invite_regex(R"((https?:\/\/)?discord\.(gg(\/invite)?\/|com\/invite\/)([A-Za-z0-9\-]+))", std::regex_constants::ECMAScript);
+ std::smatch match;
+ bool full_url = std::regex_search(s, match, invite_regex);
+ if (full_url || IsCode(s)) {
+ m_code = full_url ? match[4].str() : s;
+ m_needs_request = true;
+ m_ok.set_sensitive(false);
+ } else {
+ m_ok.set_sensitive(false);
+ }
+}
+
+void JoinGuildDialog::CheckCode() {
+ auto cb = [this](const std::optional<InviteData> &invite) {
+ if (invite.has_value()) {
+ m_ok.set_sensitive(true);
+ if (invite->Guild.has_value()) {
+ if (invite->MemberCount.has_value())
+ m_info.set_text(invite->Guild->Name + " (" + std::to_string(*invite->MemberCount) + " members)");
+ else
+ m_info.set_text(invite->Guild->Name);
+ } else {
+ m_info.set_text("Group DM (" + std::to_string(*invite->MemberCount) + " members)");
+ }
+ } else {
+ m_ok.set_sensitive(false);
+ m_info.set_text("Invalid invite");
+ }
+ };
+ Abaddon::Get().GetDiscordClient().FetchInvite(m_code, sigc::track_obj(cb, *this));
+}
+
+bool JoinGuildDialog::IsCode(std::string str) {
+ return str.length() >= 2 && std::all_of(str.begin(), str.end(), [](char c) -> bool { return std::isalnum(c) || c == '-'; });
+}
+
+std::string JoinGuildDialog::GetCode() {
+ return m_code;
+}
+
+static const constexpr int RateLimitMS = 1500;
+bool JoinGuildDialog::on_idle_slot() {
+ const auto now = std::chrono::steady_clock::now();
+ if (m_needs_request && ((now - m_last_req_time) > std::chrono::milliseconds(RateLimitMS))) {
+ m_needs_request = false;
+ m_last_req_time = now;
+ CheckCode();
+ }
+
+ return true;
+}