From 49685c39895af67d7ffcc50fdc02150b6ee44f72 Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Tue, 5 Apr 2022 22:01:53 -0400 Subject: [PATCH] fix up a bunch of clang-tidy stuff mostly changing references, which i hope doesnt break stuff with models (TreeRow, iterators) since they gave me some strange problems in the past --- src/abaddon.cpp | 10 +- src/abaddon.hpp | 7 +- .../cellrendererpixbufanimation.cpp | 2 - .../cellrendererpixbufanimation.hpp | 2 +- src/components/channels.cpp | 20 +- src/components/channels.hpp | 2 +- src/components/channelscellrenderer.cpp | 29 +- src/components/channelscellrenderer.hpp | 4 +- src/components/chatinputindicator.cpp | 6 +- src/components/chatmessage.cpp | 57 ++-- src/components/chatmessage.hpp | 19 +- src/components/chatwindow.cpp | 5 +- src/components/chatwindow.hpp | 2 +- src/components/completer.cpp | 29 +- src/components/completer.hpp | 2 +- src/components/draglistbox.cpp | 4 +- src/components/friendslist.cpp | 8 +- src/components/lazyimage.cpp | 8 +- src/components/lazyimage.hpp | 2 +- src/components/memberlist.cpp | 8 +- src/components/ratelimitindicator.cpp | 2 +- src/components/statusindicator.cpp | 9 +- src/components/statusindicator.hpp | 2 +- src/dialogs/joinguild.hpp | 2 +- src/discord/channel.cpp | 5 +- src/discord/channel.hpp | 21 +- src/discord/discord.cpp | 262 ++++++++---------- src/discord/discord.hpp | 123 ++++---- src/discord/emoji.cpp | 2 +- src/discord/guild.cpp | 55 +--- src/discord/guild.hpp | 3 +- src/discord/httpclient.cpp | 32 ++- src/discord/httpclient.hpp | 12 +- src/discord/json.hpp | 8 +- src/discord/member.cpp | 2 +- src/discord/member.hpp | 2 +- src/discord/message.cpp | 4 +- src/discord/message.hpp | 6 +- src/discord/objects.cpp | 4 +- src/discord/objects.hpp | 2 +- src/discord/role.hpp | 4 +- src/discord/snowflake.cpp | 5 +- src/discord/snowflake.hpp | 4 +- src/discord/sticker.cpp | 9 - src/discord/sticker.hpp | 4 +- src/discord/store.cpp | 6 +- src/discord/store.hpp | 6 +- src/discord/user.cpp | 8 +- src/discord/user.hpp | 30 +- src/discord/websocket.cpp | 15 +- src/discord/websocket.hpp | 3 +- src/emojis.cpp | 21 +- src/emojis.hpp | 1 - src/filecache.cpp | 29 +- src/filecache.hpp | 6 +- src/http.cpp | 6 +- src/http.hpp | 2 +- src/imgmanager.cpp | 40 +-- src/imgmanager.hpp | 13 +- src/settings.hpp | 2 +- src/util.cpp | 40 +-- src/util.hpp | 19 +- src/windows/guildsettings/auditlogpane.cpp | 90 +++--- src/windows/guildsettings/banspane.cpp | 4 +- src/windows/guildsettings/emojispane.cpp | 12 +- src/windows/guildsettings/infopane.cpp | 8 +- src/windows/guildsettings/invitespane.cpp | 4 +- src/windows/guildsettings/memberspane.cpp | 12 +- src/windows/guildsettings/rolespane.cpp | 12 +- src/windows/mainwindow.cpp | 8 +- src/windows/mainwindow.hpp | 2 +- src/windows/profile/userinfopane.cpp | 12 +- src/windows/profilewindow.cpp | 2 +- src/windows/threadswindow.cpp | 2 +- src/windows/threadswindow.hpp | 2 +- 75 files changed, 518 insertions(+), 709 deletions(-) diff --git a/src/abaddon.cpp b/src/abaddon.cpp index 1ab6f8c..af28571 100644 --- a/src/abaddon.cpp +++ b/src/abaddon.cpp @@ -63,14 +63,14 @@ int Abaddon::StartGTK() { m_gtk_app = Gtk::Application::create("com.github.uowuo.abaddon"); m_css_provider = Gtk::CssProvider::create(); - m_css_provider->signal_parsing_error().connect([this](const Glib::RefPtr §ion, const Glib::Error &error) { + m_css_provider->signal_parsing_error().connect([](const Glib::RefPtr §ion, const Glib::Error &error) { Gtk::MessageDialog dlg("css failed parsing (" + error.what() + ")", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true); dlg.set_position(Gtk::WIN_POS_CENTER); dlg.run(); }); m_css_low_provider = Gtk::CssProvider::create(); - m_css_low_provider->signal_parsing_error().connect([this](const Glib::RefPtr §ion, const Glib::Error &error) { + m_css_low_provider->signal_parsing_error().connect([](const Glib::RefPtr §ion, const Glib::Error &error) { Gtk::MessageDialog dlg("low-priority css failed parsing (" + error.what() + ")", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true); dlg.set_position(Gtk::WIN_POS_CENTER); dlg.run(); @@ -165,7 +165,7 @@ void Abaddon::OnShutdown() { void Abaddon::LoadFromSettings() { std::string token = GetSettings().DiscordToken; - if (token.size()) { + if (!token.empty()) { m_discord_token = token; m_discord.UpdateToken(m_discord_token); } @@ -292,7 +292,7 @@ void Abaddon::ShowUserMenu(const GdkEvent *event, Snowflake id, Snowflake guild_ delete child; if (guild.has_value() && user.has_value()) { const auto roles = user->GetSortedRoles(); - m_user_menu_roles->set_visible(roles.size() > 0); + m_user_menu_roles->set_visible(!roles.empty()); for (const auto &role : roles) { auto *item = Gtk::manage(new Gtk::MenuItem(role.Name)); if (role.Color != 0) { @@ -691,7 +691,7 @@ void Abaddon::ActionSetStatus() { const auto status = dlg.GetStatusType(); const auto activity_type = dlg.GetActivityType(); const auto activity_name = dlg.GetActivityName(); - if (activity_name == "") { + if (activity_name.empty()) { m_discord.UpdateStatus(status, false); } else { ActivityData activity; diff --git a/src/abaddon.hpp b/src/abaddon.hpp index 311dcc5..3404633 100644 --- a/src/abaddon.hpp +++ b/src/abaddon.hpp @@ -14,14 +14,15 @@ class Abaddon { private: Abaddon(); + +public: + static Abaddon &Get(); + Abaddon(const Abaddon &) = delete; Abaddon &operator=(const Abaddon &) = delete; Abaddon(Abaddon &&) = delete; Abaddon &operator=(Abaddon &&) = delete; -public: - static Abaddon &Get(); - int StartGTK(); void OnShutdown(); diff --git a/src/components/cellrendererpixbufanimation.cpp b/src/components/cellrendererpixbufanimation.cpp index 2658967..d6f1f01 100644 --- a/src/components/cellrendererpixbufanimation.cpp +++ b/src/components/cellrendererpixbufanimation.cpp @@ -10,8 +10,6 @@ CellRendererPixbufAnimation::CellRendererPixbufAnimation() property_ypad() = 2; } -CellRendererPixbufAnimation::~CellRendererPixbufAnimation() {} - Glib::PropertyProxy> CellRendererPixbufAnimation::property_pixbuf() { return m_property_pixbuf.get_proxy(); } diff --git a/src/components/cellrendererpixbufanimation.hpp b/src/components/cellrendererpixbufanimation.hpp index f47e928..fe2141d 100644 --- a/src/components/cellrendererpixbufanimation.hpp +++ b/src/components/cellrendererpixbufanimation.hpp @@ -6,7 +6,7 @@ class CellRendererPixbufAnimation : public Gtk::CellRenderer { public: CellRendererPixbufAnimation(); - virtual ~CellRendererPixbufAnimation(); + ~CellRendererPixbufAnimation() override = default; Glib::PropertyProxy> property_pixbuf(); Glib::PropertyProxy> property_pixbuf_animation(); diff --git a/src/components/channels.cpp b/src/components/channels.cpp index 99bd07b..f0f7bcc 100644 --- a/src/components/channels.cpp +++ b/src/components/channels.cpp @@ -291,7 +291,7 @@ void ChannelList::UpdateChannel(Snowflake id) { auto channel = Abaddon::Get().GetDiscordClient().GetChannel(id); if (!iter || !channel.has_value()) return; if (channel->Type == ChannelType::GUILD_CATEGORY) return UpdateChannelCategory(*channel); - if (!IsTextChannel(channel->Type)) return; + if (!channel->IsText()) return; // refresh stuff that might have changed const bool is_orphan_TMP = !channel->ParentID.has_value(); @@ -399,7 +399,7 @@ void ChannelList::OnThreadListSync(const ThreadListSyncData &data) { queue.pop(); if ((*item)[m_columns.m_type] == RenderType::Thread) threads.push_back(static_cast((*item)[m_columns.m_id])); - for (auto child : item->children()) + for (const auto& child : item->children()) queue.push(child); } @@ -580,7 +580,7 @@ Gtk::TreeModel::iterator ChannelList::AddGuild(const GuildData &guild) { if (thread.has_value()) threads[*thread->ParentID].push_back(*thread); } - const auto add_threads = [&](const ChannelData &channel, Gtk::TreeRow row) { + const auto add_threads = [&](const ChannelData &channel, const Gtk::TreeRow& row) { row[m_columns.m_expanded] = true; const auto it = threads.find(channel.ID); @@ -648,7 +648,7 @@ Gtk::TreeModel::iterator ChannelList::CreateThreadRow(const Gtk::TreeNodeChildre thread_row[m_columns.m_type] = RenderType::Thread; thread_row[m_columns.m_id] = channel.ID; thread_row[m_columns.m_name] = "- " + Glib::Markup::escape_text(*channel.Name); - thread_row[m_columns.m_sort] = channel.ID; + thread_row[m_columns.m_sort] = static_cast(channel.ID); thread_row[m_columns.m_nsfw] = false; return thread_iter; @@ -692,7 +692,7 @@ bool ChannelList::IsTextChannel(ChannelType type) { } // this should be unncessary but something is behaving strange so its just in case -void ChannelList::OnRowCollapsed(const Gtk::TreeModel::iterator &iter, const Gtk::TreeModel::Path &path) { +void ChannelList::OnRowCollapsed(const Gtk::TreeModel::iterator &iter, const Gtk::TreeModel::Path &path) const { (*iter)[m_columns.m_expanded] = false; } @@ -737,14 +737,14 @@ void ChannelList::AddPrivateChannels() { std::optional top_recipient; const auto recipients = dm->GetDMRecipients(); - if (recipients.size() > 0) + if (!recipients.empty()) top_recipient = recipients[0]; auto iter = m_model->append(header_row->children()); auto row = *iter; row[m_columns.m_type] = RenderType::DM; row[m_columns.m_id] = dm_id; - row[m_columns.m_sort] = -(dm->LastMessageID.has_value() ? *dm->LastMessageID : dm_id); + row[m_columns.m_sort] = static_cast(-(dm->LastMessageID.has_value() ? *dm->LastMessageID : dm_id)); row[m_columns.m_icon] = img.GetPlaceholder(DMIconSize); if (dm->Type == ChannelType::DM && top_recipient.has_value()) @@ -781,7 +781,7 @@ void ChannelList::UpdateCreateDMChannel(const ChannelData &dm) { auto row = *iter; row[m_columns.m_type] = RenderType::DM; row[m_columns.m_id] = dm.ID; - row[m_columns.m_sort] = -(dm.LastMessageID.has_value() ? *dm.LastMessageID : dm.ID); + row[m_columns.m_sort] = static_cast(-(dm.LastMessageID.has_value() ? *dm.LastMessageID : dm.ID)); row[m_columns.m_icon] = img.GetPlaceholder(DMIconSize); if (dm.Type == ChannelType::DM && top_recipient.has_value()) @@ -817,7 +817,7 @@ void ChannelList::OnMessageCreate(const Message &msg) { if (!channel.has_value()) return; if (channel->Type == ChannelType::DM || channel->Type == ChannelType::GROUP_DM) { if (iter) - (*iter)[m_columns.m_sort] = -msg.ID; + (*iter)[m_columns.m_sort] = static_cast(-msg.ID); } if (channel->GuildID.has_value()) if ((iter = GetIteratorForGuildFromID(*channel->GuildID))) @@ -826,7 +826,7 @@ void ChannelList::OnMessageCreate(const Message &msg) { bool ChannelList::OnButtonPressEvent(GdkEventButton *ev) { if (ev->button == GDK_BUTTON_SECONDARY && ev->type == GDK_BUTTON_PRESS) { - if (m_view.get_path_at_pos(ev->x, ev->y, m_path_for_menu)) { + if (m_view.get_path_at_pos(static_cast(ev->x), static_cast(ev->y), m_path_for_menu)) { auto row = (*m_model->get_iter(m_path_for_menu)); switch (static_cast(row[m_columns.m_type])) { case RenderType::Guild: diff --git a/src/components/channels.hpp b/src/components/channels.hpp index a2553fd..d499f5b 100644 --- a/src/components/channels.hpp +++ b/src/components/channels.hpp @@ -84,7 +84,7 @@ protected: bool IsTextChannel(ChannelType type); - void OnRowCollapsed(const Gtk::TreeModel::iterator &iter, const Gtk::TreeModel::Path &path); + void OnRowCollapsed(const Gtk::TreeModel::iterator &iter, const Gtk::TreeModel::Path &path) const; void OnRowExpanded(const Gtk::TreeModel::iterator &iter, const Gtk::TreeModel::Path &path); bool SelectionFunc(const Glib::RefPtr &model, const Gtk::TreeModel::Path &path, bool is_currently_selected); bool OnButtonPressEvent(GdkEventButton *ev); diff --git a/src/components/channelscellrenderer.cpp b/src/components/channelscellrenderer.cpp index 141556a..9afce8a 100644 --- a/src/components/channelscellrenderer.cpp +++ b/src/components/channelscellrenderer.cpp @@ -27,9 +27,6 @@ CellRendererChannels::CellRendererChannels() }); } -CellRendererChannels::~CellRendererChannels() { -} - Glib::PropertyProxy CellRendererChannels::property_type() { return m_property_type.get_proxy(); } @@ -212,7 +209,10 @@ void CellRendererChannels::render_vfunc_guild(const Cairo::RefPtr(text_x), + static_cast(text_y), + static_cast(text_w), + static_cast(text_h)); static const auto color = Gdk::RGBA(Abaddon::Get().GetSettings().ChannelColor); m_renderer_text.property_foreground_rgba() = color; @@ -231,7 +231,11 @@ void CellRendererChannels::render_vfunc_guild(const Cairo::RefPtradvance()) - widget.queue_draw_area(icon_x, icon_y, icon_w, icon_h); + widget.queue_draw_area( + static_cast(icon_x), + static_cast(icon_y), + static_cast(icon_w), + static_cast(icon_h)); }; if ((hover_only && is_hovered) || !hover_only) @@ -264,12 +268,12 @@ void CellRendererChannels::render_vfunc_guild(const Cairo::RefPtrrectangle(x, y + h / 2 - 24 / 2, 3, 24); + cr->rectangle(x, y + h / 2.0 - 24.0 / 2.0, 3.0, 24.0); cr->fill(); } if (total_mentions < 1) return; - auto *paned = static_cast(widget.get_ancestor(Gtk::Paned::get_type())); + auto *paned = dynamic_cast(widget.get_ancestor(Gtk::Paned::get_type())); if (paned != nullptr) { const auto edge = std::min(paned->get_position(), background_area.get_width()); @@ -415,7 +419,7 @@ void CellRendererChannels::render_vfunc_channel(const Cairo::RefPtr(widget.get_ancestor(Gtk::Paned::get_type())); + auto *paned = dynamic_cast(widget.get_ancestor(Gtk::Paned::get_type())); if (paned != nullptr) { const auto edge = std::min(paned->get_position(), cell_area.get_width()); @@ -487,7 +491,7 @@ void CellRendererChannels::render_vfunc_thread(const Cairo::RefPtr(widget.get_ancestor(Gtk::Paned::get_type())); + auto *paned = dynamic_cast(widget.get_ancestor(Gtk::Paned::get_type())); if (paned != nullptr) { const auto edge = std::min(paned->get_position(), cell_area.get_width()); @@ -522,7 +526,7 @@ void CellRendererChannels::render_vfunc_dmheader(const Cairo::RefPtr(widget.get_ancestor(Gtk::Paned::get_type())); + auto *paned = dynamic_cast(widget.get_ancestor(Gtk::Paned::get_type())); if (paned != nullptr) { const auto edge = std::min(paned->get_position(), background_area.get_width()); if (const auto unread = Abaddon::Get().GetDiscordClient().GetUnreadDMsCount(); unread > 0) @@ -587,7 +591,10 @@ void CellRendererChannels::render_vfunc_dm(const Cairo::RefPtr & const double text_w = text_natural.width; const double text_h = text_natural.height; - Gdk::Rectangle text_cell_area(text_x, text_y, text_w, text_h); + Gdk::Rectangle text_cell_area(static_cast(text_x), + static_cast(text_y), + static_cast(text_w), + static_cast(text_h)); auto &discord = Abaddon::Get().GetDiscordClient(); const auto id = m_property_id.get_value(); diff --git a/src/components/channelscellrenderer.hpp b/src/components/channelscellrenderer.hpp index 95ff4fe..e2be9b2 100644 --- a/src/components/channelscellrenderer.hpp +++ b/src/components/channelscellrenderer.hpp @@ -18,7 +18,7 @@ enum class RenderType : uint8_t { class CellRendererChannels : public Gtk::CellRenderer { public: CellRendererChannels(); - virtual ~CellRendererChannels(); + ~CellRendererChannels() override = default; Glib::PropertyProxy property_type(); Glib::PropertyProxy property_id(); @@ -106,7 +106,7 @@ protected: Gtk::CellRendererState flags); static void cairo_path_rounded_rect(const Cairo::RefPtr &cr, double x, double y, double w, double h, double r); - void unread_render_mentions(const Cairo::RefPtr &cr, Gtk::Widget &widget, int mentions, int edge, const Gdk::Rectangle &cell_area); + static void unread_render_mentions(const Cairo::RefPtr &cr, Gtk::Widget &widget, int mentions, int edge, const Gdk::Rectangle &cell_area); private: Gtk::CellRendererText m_renderer_text; diff --git a/src/components/chatinputindicator.cpp b/src/components/chatinputindicator.cpp index ba794f3..b829c46 100644 --- a/src/components/chatinputindicator.cpp +++ b/src/components/chatinputindicator.cpp @@ -84,14 +84,14 @@ void ChatInputIndicator::OnMessageCreate(const Message &message) { void ChatInputIndicator::SetTypingString(const Glib::ustring &str) { m_label.set_text(str); - if (str == "") + if (str.empty()) m_img.hide(); else if (m_img.property_pixbuf_animation().get_value()) m_img.show(); } void ChatInputIndicator::ComputeTypingString() { - if (m_custom_markup != "") { + if (!m_custom_markup.empty()) { m_label.set_markup(m_custom_markup); m_img.hide(); return; @@ -104,7 +104,7 @@ void ChatInputIndicator::ComputeTypingString() { if (user.has_value()) typers.push_back(*user); } - if (typers.size() == 0) { + if (typers.empty()) { SetTypingString(""); } else if (typers.size() == 1) { SetTypingString(typers[0].Username + " is typing..."); diff --git a/src/components/chatmessage.cpp b/src/components/chatmessage.cpp index 823aaa6..4d6eec5 100644 --- a/src/components/chatmessage.cpp +++ b/src/components/chatmessage.cpp @@ -30,7 +30,7 @@ ChatMessageItemContainer *ChatMessageItemContainer::FromMessage(const Message &d if (data.Nonce.has_value()) container->Nonce = *data.Nonce; - if (data.Content.size() > 0 || data.Type != MessageType::DEFAULT) { + if (!data.Content.empty() || data.Type != MessageType::DEFAULT) { container->m_text_component = container->CreateTextComponent(data); container->AttachEventHandlers(*container->m_text_component); container->m_main.add(*container->m_text_component); @@ -78,7 +78,7 @@ ChatMessageItemContainer *ChatMessageItemContainer::FromMessage(const Message &d container->m_main.add(*widget); } - if (data.Reactions.has_value() && data.Reactions->size() > 0) { + if (data.Reactions.has_value() && !data.Reactions->empty()) { container->m_reactions_component = container->CreateReactionsComponent(data); container->m_main.add(*container->m_reactions_component); } @@ -114,7 +114,7 @@ void ChatMessageItemContainer::UpdateReactions() { } const auto data = Abaddon::Get().GetDiscordClient().GetMessage(ID); - if (data->Reactions.has_value() && data->Reactions->size() > 0) { + if (data->Reactions.has_value() && !data->Reactions->empty()) { m_reactions_component = CreateReactionsComponent(*data); m_reactions_component->show_all(); m_main.add(*m_reactions_component); @@ -150,7 +150,7 @@ void ChatMessageItemContainer::UpdateAttributes() { m_attrib_label->set_markup("[edited]"); } -void ChatMessageItemContainer::AddClickHandler(Gtk::Widget *widget, std::string url) { +void ChatMessageItemContainer::AddClickHandler(Gtk::Widget *widget, const std::string &url) { // clang-format off widget->signal_button_press_event().connect([url](GdkEventButton *event) -> bool { if (event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY) { @@ -224,13 +224,13 @@ void ChatMessageItemContainer::UpdateTextComponent(Gtk::TextView *tv) { } } break; case MessageType::RECIPIENT_ADD: { - if (data->Mentions.size() == 0) break; + if (data->Mentions.empty()) break; const auto &adder = Abaddon::Get().GetDiscordClient().GetUser(data->Author.ID); const auto &added = data->Mentions[0]; b->insert_markup(s, "" + adder->Username + " added " + added.Username + ""); } break; case MessageType::RECIPIENT_REMOVE: { - if (data->Mentions.size() == 0) break; + if (data->Mentions.empty()) break; const auto &adder = Abaddon::Get().GetDiscordClient().GetUser(data->Author.ID); const auto &added = data->Mentions[0]; if (adder->ID == added.ID) @@ -361,7 +361,7 @@ Gtk::Widget *ChatMessageItemContainer::CreateEmbedComponent(const EmbedData &emb if (embed.URL.has_value()) { AddPointerCursor(*title_ev); auto url = *embed.URL; - title_ev->signal_button_press_event().connect([this, url = std::move(url)](GdkEventButton *event) -> bool { + title_ev->signal_button_press_event().connect([url = std::move(url)](GdkEventButton *event) -> bool { if (event->button == GDK_BUTTON_PRIMARY) { LaunchBrowser(url); return true; @@ -389,7 +389,7 @@ Gtk::Widget *ChatMessageItemContainer::CreateEmbedComponent(const EmbedData &emb } // todo: handle inline fields - if (embed.Fields.has_value() && embed.Fields->size() > 0) { + if (embed.Fields.has_value() && !embed.Fields->empty()) { auto *flow = Gtk::manage(new Gtk::FlowBox); flow->set_orientation(Gtk::ORIENTATION_HORIZONTAL); flow->set_min_children_per_line(3); @@ -516,23 +516,6 @@ Gtk::Widget *ChatMessageItemContainer::CreateAttachmentComponent(const Attachmen return ev; } -Gtk::Widget *ChatMessageItemContainer::CreateStickerComponentDeprecated(const StickerData &data) { - auto *box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL)); - auto *imgw = Gtk::manage(new Gtk::Image); - box->add(*imgw); - auto &img = Abaddon::Get().GetImageManager(); - - if (data.FormatType == StickerFormatType::PNG || data.FormatType == StickerFormatType::APNG) { - auto cb = [this, imgw](const Glib::RefPtr &pixbuf) { - imgw->property_pixbuf() = pixbuf; - }; - img.LoadFromURL(data.GetURL(), sigc::track_obj(cb, *imgw)); - } - - AttachEventHandlers(*box); - return box; -} - Gtk::Widget *ChatMessageItemContainer::CreateStickersComponent(const std::vector &data) { auto *box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL)); @@ -608,7 +591,7 @@ Gtk::Widget *ChatMessageItemContainer::CreateReactionsComponent(const Message &d // image if (is_stock) { // unicode/stock const auto shortcode = emojis.GetShortCodeForPattern(reaction.Emoji.Name); - if (shortcode != "") + if (!shortcode.empty()) ev->set_tooltip_text(shortcode); const auto &pb = emojis.GetPixBuf(reaction.Emoji.Name); @@ -784,7 +767,7 @@ void ChatMessageItemContainer::HandleRoleMentions(const Glib::RefPtr &buf) { +void ChatMessageItemContainer::HandleUserMentions(const Glib::RefPtr &buf) const { constexpr static const auto mentions_regex = R"(<@!?(\d+)>)"; static auto rgx = Glib::Regex::create(mentions_regex); @@ -861,7 +844,7 @@ void ChatMessageItemContainer::HandleCustomEmojis(Gtk::TextView &tv) { const auto mark_start = buf->create_mark(start_it, false); end_it.backward_char(); const auto mark_end = buf->create_mark(end_it, false); - const auto cb = [this, &tv, buf, mark_start, mark_end](const Glib::RefPtr &pixbuf) { + const auto cb = [&tv, buf, mark_start, mark_end](const Glib::RefPtr &pixbuf) { auto start_it = mark_start->get_iter(); auto end_it = mark_end->get_iter(); end_it.forward_char(); @@ -879,7 +862,7 @@ void ChatMessageItemContainer::HandleCustomEmojis(Gtk::TextView &tv) { const auto mark_start = buf->create_mark(start_it, false); end_it.backward_char(); const auto mark_end = buf->create_mark(end_it, false); - const auto cb = [this, buf, mark_start, mark_end](const Glib::RefPtr &pixbuf) { + const auto cb = [buf, mark_start, mark_end](const Glib::RefPtr &pixbuf) { auto start_it = mark_start->get_iter(); auto end_it = mark_end->get_iter(); end_it.forward_char(); @@ -902,7 +885,7 @@ void ChatMessageItemContainer::HandleEmojis(Gtk::TextView &tv) { if (Abaddon::Get().GetSettings().ShowCustomEmojis) HandleCustomEmojis(tv); } -void ChatMessageItemContainer::CleanupEmojis(Glib::RefPtr buf) { +void ChatMessageItemContainer::CleanupEmojis(const Glib::RefPtr &buf) { static auto rgx = Glib::Regex::create(R"()"); auto text = GetText(buf); @@ -922,9 +905,9 @@ void ChatMessageItemContainer::CleanupEmojis(Glib::RefPtr buf) startpos = mend; const auto it = buf->erase(start_it, end_it); - const int alen = text.size(); + const int alen = static_cast(text.size()); text = GetText(buf); - const int blen = text.size(); + const int blen = static_cast(text.size()); startpos -= (alen - blen); buf->insert(it, new_term); @@ -933,7 +916,7 @@ void ChatMessageItemContainer::CleanupEmojis(Glib::RefPtr buf) } } -void ChatMessageItemContainer::HandleChannelMentions(Glib::RefPtr buf) { +void ChatMessageItemContainer::HandleChannelMentions(const Glib::RefPtr &buf) { static auto rgx = Glib::Regex::create(R"(<#(\d+)>)"); Glib::ustring text = GetText(buf); @@ -990,12 +973,12 @@ bool ChatMessageItemContainer::OnClickChannel(GdkEventButton *ev) { return false; int x, y; - m_text_component->window_to_buffer_coords(Gtk::TEXT_WINDOW_WIDGET, ev->x, ev->y, x, y); + m_text_component->window_to_buffer_coords(Gtk::TEXT_WINDOW_WIDGET, static_cast(ev->x), static_cast(ev->y), x, y); Gtk::TextBuffer::iterator iter; m_text_component->get_iter_at_location(iter, x, y); const auto tags = iter.get_tags(); - for (auto tag : tags) { + for (const auto &tag : tags) { const auto it = m_channel_tagmap.find(tag); if (it != m_channel_tagmap.end()) { m_signal_action_channel_click.emit(it->second); @@ -1053,12 +1036,12 @@ bool ChatMessageItemContainer::OnLinkClick(GdkEventButton *ev) { return false; int x, y; - m_text_component->window_to_buffer_coords(Gtk::TEXT_WINDOW_WIDGET, ev->x, ev->y, x, y); + m_text_component->window_to_buffer_coords(Gtk::TEXT_WINDOW_WIDGET, static_cast(ev->x), static_cast(ev->y), x, y); Gtk::TextBuffer::iterator iter; m_text_component->get_iter_at_location(iter, x, y); const auto tags = iter.get_tags(); - for (auto tag : tags) { + for (const auto &tag : tags) { const auto it = m_link_tagmap.find(tag); if (it != m_link_tagmap.end()) { if (ev->button == GDK_BUTTON_PRIMARY) { diff --git a/src/components/chatmessage.hpp b/src/components/chatmessage.hpp index 5e213ee..86c3fea 100644 --- a/src/components/chatmessage.hpp +++ b/src/components/chatmessage.hpp @@ -19,14 +19,13 @@ public: void SetFailed(); protected: - void AddClickHandler(Gtk::Widget *widget, std::string); + static void AddClickHandler(Gtk::Widget *widget, const std::string &); Gtk::TextView *CreateTextComponent(const Message &data); // Message.Content void UpdateTextComponent(Gtk::TextView *tv); Gtk::Widget *CreateEmbedsComponent(const std::vector &embeds); - Gtk::Widget *CreateEmbedComponent(const EmbedData &data); // Message.Embeds[0] + static Gtk::Widget *CreateEmbedComponent(const EmbedData &data); // Message.Embeds[0] Gtk::Widget *CreateImageComponent(const std::string &proxy_url, const std::string &url, int inw, int inh); Gtk::Widget *CreateAttachmentComponent(const AttachmentData &data); // non-image attachments - Gtk::Widget *CreateStickerComponentDeprecated(const StickerData &data); Gtk::Widget *CreateStickersComponent(const std::vector &data); Gtk::Widget *CreateReactionsComponent(const Message &data); Gtk::Widget *CreateReplyComponent(const Message &data); @@ -35,14 +34,14 @@ protected: static bool IsEmbedImageOnly(const EmbedData &data); - void HandleRoleMentions(const Glib::RefPtr &buf); - void HandleUserMentions(const Glib::RefPtr &buf); - void HandleStockEmojis(Gtk::TextView &tv); - void HandleCustomEmojis(Gtk::TextView &tv); - void HandleEmojis(Gtk::TextView &tv); - void CleanupEmojis(Glib::RefPtr buf); + static void HandleRoleMentions(const Glib::RefPtr &buf); + void HandleUserMentions(const Glib::RefPtr &buf) const; + static void HandleStockEmojis(Gtk::TextView &tv); + static void HandleCustomEmojis(Gtk::TextView &tv); + static void HandleEmojis(Gtk::TextView &tv); + static void CleanupEmojis(const Glib::RefPtr &buf); - void HandleChannelMentions(Glib::RefPtr buf); + void HandleChannelMentions(const Glib::RefPtr &buf); void HandleChannelMentions(Gtk::TextView *tv); bool OnClickChannel(GdkEventButton *ev); diff --git a/src/components/chatwindow.cpp b/src/components/chatwindow.cpp index df2b6b8..582343d 100644 --- a/src/components/chatwindow.cpp +++ b/src/components/chatwindow.cpp @@ -1,5 +1,4 @@ #include "chatwindow.hpp" -#include "chatmessage.hpp" #include "abaddon.hpp" #include "chatinputindicator.hpp" #include "ratelimitindicator.hpp" @@ -134,7 +133,7 @@ void ChatWindow::AddNewHistory(const std::vector &msgs) { m_chat->PrependMessages(msgs.crbegin(), msgs.crend()); } -void ChatWindow::InsertChatInput(std::string text) { +void ChatWindow::InsertChatInput(const std::string &text) { m_input->InsertText(text); } @@ -159,7 +158,7 @@ bool ChatWindow::OnInputSubmit(const Glib::ustring &text) { if (!m_rate_limit_indicator->CanSpeak()) return false; - if (text.size() == 0) + if (text.empty()) return false; if (m_active_channel.IsValid()) diff --git a/src/components/chatwindow.hpp b/src/components/chatwindow.hpp index db0bfc4..0f40e88 100644 --- a/src/components/chatwindow.hpp +++ b/src/components/chatwindow.hpp @@ -25,7 +25,7 @@ public: void DeleteMessage(Snowflake id); // add [deleted] indicator void UpdateMessage(Snowflake id); // add [edited] indicator void AddNewHistory(const std::vector &msgs); // prepend messages - void InsertChatInput(std::string text); + void InsertChatInput(const std::string& text); Snowflake GetOldestListedMessage(); // oldest message that is currently in the ListBox void UpdateReactions(Snowflake id); void SetTopic(const std::string &text); diff --git a/src/components/completer.cpp b/src/components/completer.cpp index 327ef95..128bef2 100644 --- a/src/components/completer.cpp +++ b/src/components/completer.cpp @@ -1,4 +1,5 @@ #include +#include #include "completer.hpp" #include "abaddon.hpp" #include "util.hpp" @@ -46,7 +47,7 @@ bool Completer::ProcessKeyPress(GdkEventKey *e) { switch (e->keyval) { case GDK_KEY_Down: { - if (m_entries.size() == 0) return true; + if (m_entries.empty()) return true; const auto index = static_cast(m_list.get_selected_row()->get_index()); if (index >= m_entries.size() - 1) return true; m_list.select_row(*m_entries[index + 1]); @@ -54,7 +55,7 @@ bool Completer::ProcessKeyPress(GdkEventKey *e) { } return true; case GDK_KEY_Up: { - if (m_entries.size() == 0) return true; + if (m_entries.empty()) return true; const auto index = static_cast(m_list.get_selected_row()->get_index()); if (index == 0) return true; m_list.select_row(*m_entries[index - 1]); @@ -62,7 +63,7 @@ bool Completer::ProcessKeyPress(GdkEventKey *e) { } return true; case GDK_KEY_Return: { - if (m_entries.size() == 0) return true; + if (m_entries.empty()) return true; DoCompletion(m_list.get_selected_row()); } return true; @@ -74,11 +75,11 @@ bool Completer::ProcessKeyPress(GdkEventKey *e) { } void Completer::SetGetRecentAuthors(get_recent_authors_cb cb) { - m_recent_authors_cb = cb; + m_recent_authors_cb = std::move(cb); } void Completer::SetGetChannelID(get_channel_id_cb cb) { - m_channel_id_cb = cb; + m_channel_id_cb = std::move(cb); } bool Completer::IsShown() const { @@ -86,7 +87,7 @@ bool Completer::IsShown() const { } CompleterEntry *Completer::CreateEntry(const Glib::ustring &completion) { - auto entry = Gtk::manage(new CompleterEntry(completion, m_entries.size())); + auto entry = Gtk::manage(new CompleterEntry(completion, static_cast(m_entries.size()))); m_entries.push_back(entry); entry->show_all(); m_list.add(*entry); @@ -152,7 +153,7 @@ void Completer::CompleteEmojis(const Glib::ustring &term) { const auto make_entry = [&](const Glib::ustring &name, const Glib::ustring &completion, const Glib::ustring &url = "", bool animated = false) -> CompleterEntry * { const auto entry = CreateEntry(completion); entry->SetText(name); - if (url == "") return entry; + if (url.empty()) return entry; if (animated) entry->SetAnimation(url); else @@ -173,8 +174,8 @@ void Completer::CompleteEmojis(const Glib::ustring &term) { const auto emoji = *discord.GetEmoji(tmp.ID); if (emoji.IsAnimated.has_value() && *emoji.IsAnimated) continue; if (emoji.IsAvailable.has_value() && !*emoji.IsAvailable) continue; - if (emoji.Roles.has_value() && emoji.Roles->size() > 0) continue; - if (term.size() > 0) + if (emoji.Roles.has_value() && !emoji.Roles->empty()) continue; + if (!term.empty()) if (!StringContainsCaseless(emoji.Name, term)) continue; if (i++ > MaxCompleterEntries) break; @@ -190,8 +191,8 @@ void Completer::CompleteEmojis(const Glib::ustring &term) { const auto emoji = *discord.GetEmoji(tmp.ID); const bool is_animated = emoji.IsAnimated.has_value() && *emoji.IsAnimated; if (emoji.IsAvailable.has_value() && !*emoji.IsAvailable) continue; - if (emoji.Roles.has_value() && emoji.Roles->size() > 0) continue; - if (term.size() > 0) + if (emoji.Roles.has_value() && !emoji.Roles->empty()) continue; + if (!term.empty()) if (!StringContainsCaseless(emoji.Name, term)) continue; if (i++ > MaxCompleterEntries) goto done; @@ -275,7 +276,7 @@ void Completer::OnTextBufferChanged() { default: break; } - if (m_entries.size() > 0) { + if (!m_entries.empty()) { m_list.select_row(*m_entries[0]); set_reveal_child(true); } else { @@ -329,8 +330,8 @@ Glib::ustring Completer::GetTerm() { return m_start.get_text(m_end); } -CompleterEntry::CompleterEntry(const Glib::ustring &completion, int index) - : m_completion(completion) +CompleterEntry::CompleterEntry(Glib::ustring completion, int index) + : m_completion(std::move(completion)) , m_index(index) , m_box(Gtk::ORIENTATION_HORIZONTAL) { set_halign(Gtk::ALIGN_START); diff --git a/src/components/completer.hpp b/src/components/completer.hpp index 6bd8be9..6906f02 100644 --- a/src/components/completer.hpp +++ b/src/components/completer.hpp @@ -8,7 +8,7 @@ constexpr static int CompleterImageSize = 24; class CompleterEntry : public Gtk::ListBoxRow { public: - CompleterEntry(const Glib::ustring &completion, int index); + CompleterEntry(Glib::ustring completion, int index); void SetTextColor(int color); // SetText will reset void SetText(const Glib::ustring &text); void SetImage(const Glib::RefPtr &pb); diff --git a/src/components/draglistbox.cpp b/src/components/draglistbox.cpp index 492abc3..50bb5b0 100644 --- a/src/components/draglistbox.cpp +++ b/src/components/draglistbox.cpp @@ -102,7 +102,7 @@ bool DragListBox::scroll() { } void DragListBox::on_drag_data_received(const Glib::RefPtr &context, int x, int y, const Gtk::SelectionData &selection_data, guint info, guint time) { - int index = 0; + int index; if (m_hover_row != nullptr) { if (m_top) { index = m_hover_row->get_index() - 1; @@ -130,7 +130,7 @@ void DragListBox::on_drag_data_received(const Glib::RefPtr &co void DragListBox::add_draggable(Gtk::ListBoxRow *widget) { widget->drag_source_set(m_entries, Gdk::BUTTON1_MASK, Gdk::ACTION_MOVE); widget->signal_drag_begin().connect(sigc::bind<0>(sigc::mem_fun(*this, &DragListBox::row_drag_begin), widget)); - widget->signal_drag_data_get().connect([this, widget](const Glib::RefPtr &context, Gtk::SelectionData &selection_data, guint info, guint time) { + widget->signal_drag_data_get().connect([widget](const Glib::RefPtr &context, Gtk::SelectionData &selection_data, guint info, guint time) { selection_data.set("GTK_LIST_BOX_ROW", 32, reinterpret_cast(&widget), sizeof(&widget)); }); add(*widget); diff --git a/src/components/friendslist.cpp b/src/components/friendslist.cpp index 1331d19..b908359 100644 --- a/src/components/friendslist.cpp +++ b/src/components/friendslist.cpp @@ -133,7 +133,7 @@ void FriendsList::OnActionRemove(Snowflake id) { break; } if (Abaddon::Get().ShowConfirm(str, window)) { - const auto cb = [this, window](DiscordError code) { + const auto cb = [window](DiscordError code) { if (code == DiscordError::NONE) return; Gtk::MessageDialog dlg(*window, "Failed to remove user", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true); dlg.set_position(Gtk::WIN_POS_CENTER); @@ -215,7 +215,7 @@ void FriendsListAddComponent::Submit() { if (hashpos == Glib::ustring::npos) return; const auto username = text.substr(0, hashpos); const auto discriminator = text.substr(hashpos + 1); - if (username.size() == 0 || discriminator.size() != 4) return; + if (username.empty() || discriminator.size() != 4) return; if (discriminator.find_first_not_of("0123456789") != Glib::ustring::npos) return; m_requesting = true; @@ -229,7 +229,9 @@ void FriendsListAddComponent::Submit() { m_label.set_text("Failed: "s + GetDiscordErrorDisplayString(code)); } }; - Abaddon::Get().GetDiscordClient().SendFriendRequest(username, std::stoul(discriminator), sigc::track_obj(cb, *this)); + Abaddon::Get().GetDiscordClient().SendFriendRequest(username, + static_cast(std::stoul(discriminator)), + sigc::track_obj(cb, *this)); } bool FriendsListAddComponent::OnKeyPress(GdkEventKey *e) { diff --git a/src/components/lazyimage.cpp b/src/components/lazyimage.cpp index 49bbdeb..1f005f3 100644 --- a/src/components/lazyimage.cpp +++ b/src/components/lazyimage.cpp @@ -1,4 +1,6 @@ #include "lazyimage.hpp" + +#include #include "abaddon.hpp" LazyImage::LazyImage(int w, int h, bool use_placeholder) @@ -9,8 +11,8 @@ LazyImage::LazyImage(int w, int h, bool use_placeholder) signal_draw().connect(sigc::mem_fun(*this, &LazyImage::OnDraw)); } -LazyImage::LazyImage(const std::string &url, int w, int h, bool use_placeholder) - : m_url(url) +LazyImage::LazyImage(std::string url, int w, int h, bool use_placeholder) + : m_url(std::move(url)) , m_width(w) , m_height(h) { if (use_placeholder) @@ -27,7 +29,7 @@ void LazyImage::SetURL(const std::string &url) { } bool LazyImage::OnDraw(const Cairo::RefPtr &context) { - if (!m_needs_request || m_url == "") return false; + if (!m_needs_request || m_url.empty()) return false; m_needs_request = false; if (m_animated) { diff --git a/src/components/lazyimage.hpp b/src/components/lazyimage.hpp index fae69df..4d5a216 100644 --- a/src/components/lazyimage.hpp +++ b/src/components/lazyimage.hpp @@ -5,7 +5,7 @@ class LazyImage : public Gtk::Image { public: LazyImage(int w, int h, bool use_placeholder = true); - LazyImage(const std::string &url, int w, int h, bool use_placeholder = true); + LazyImage(std::string url, int w, int h, bool use_placeholder = true); void SetAnimated(bool is_animated); void SetURL(const std::string &url); diff --git a/src/components/memberlist.cpp b/src/components/memberlist.cpp index 4f21700..4c95b63 100644 --- a/src/components/memberlist.cpp +++ b/src/components/memberlist.cpp @@ -151,7 +151,7 @@ void MemberList::UpdateMemberList() { if (!pos_role.has_value()) { roleless_users.push_back(id); continue; - }; + } pos_to_role[pos_role->Position] = *pos_role; pos_to_users[pos_role->Position].push_back(std::move(*user)); @@ -161,7 +161,7 @@ void MemberList::UpdateMemberList() { int num_rows = 0; const auto guild = *discord.GetGuild(m_guild_id); - auto add_user = [this, &user_to_color, &num_rows, guild](const UserData &data) -> bool { + auto add_user = [this, &num_rows, guild](const UserData &data) -> bool { if (num_rows++ > MaxMemberListRows) return false; auto *row = Gtk::manage(new MemberListUserRow(guild, data)); m_id_to_row[data.ID] = row; @@ -170,7 +170,7 @@ void MemberList::UpdateMemberList() { return true; }; - auto add_role = [this](std::string name) { + auto add_role = [this](const std::string &name) { auto *role_row = Gtk::manage(new Gtk::ListBoxRow); auto *role_lbl = Gtk::manage(new Gtk::Label); @@ -215,7 +215,7 @@ void MemberList::UpdateMemberList() { } void MemberList::AttachUserMenuHandler(Gtk::ListBoxRow *row, Snowflake id) { - row->signal_button_press_event().connect([this, row, id](GdkEventButton *e) -> bool { + row->signal_button_press_event().connect([this, id](GdkEventButton *e) -> bool { if (e->type == GDK_BUTTON_PRESS && e->button == GDK_BUTTON_SECONDARY) { Abaddon::Get().ShowUserMenu(reinterpret_cast(e), id, m_guild_id); return true; diff --git a/src/components/ratelimitindicator.cpp b/src/components/ratelimitindicator.cpp index ac4ef4b..820e395 100644 --- a/src/components/ratelimitindicator.cpp +++ b/src/components/ratelimitindicator.cpp @@ -66,7 +66,7 @@ int RateLimitIndicator::GetTimeLeft() const { if (sec_diff <= 0) return 0; else - return sec_diff; + return static_cast(sec_diff); } int RateLimitIndicator::GetRateLimit() const { diff --git a/src/components/statusindicator.cpp b/src/components/statusindicator.cpp index 42eb170..4d7415c 100644 --- a/src/components/statusindicator.cpp +++ b/src/components/statusindicator.cpp @@ -2,10 +2,6 @@ #include "abaddon.hpp" static const constexpr int Diameter = 8; -static const auto OnlineColor = Gdk::RGBA("#43B581"); -static const auto IdleColor = Gdk::RGBA("#FAA61A"); -static const auto DNDColor = Gdk::RGBA("#982929"); -static const auto OfflineColor = Gdk::RGBA("#808080"); StatusIndicator::StatusIndicator(Snowflake user_id) : Glib::ObjectBase("statusindicator") @@ -26,9 +22,6 @@ StatusIndicator::StatusIndicator(Snowflake user_id) CheckStatus(); } -StatusIndicator::~StatusIndicator() { -} - void StatusIndicator::CheckStatus() { const auto status = Abaddon::Get().GetDiscordClient().GetUserStatus(m_id); const auto last_status = m_status; @@ -121,7 +114,7 @@ bool StatusIndicator::on_draw(const Cairo::RefPtr &cr) { const auto color = get_style_context()->get_color(Gtk::STATE_FLAG_NORMAL); cr->set_source_rgb(color.get_red(), color.get_green(), color.get_blue()); - cr->arc(width / 2, height / 2, width / 3, 0.0, 2 * (4 * std::atan(1))); + cr->arc(width / 2.0, height / 2.0, width / 3.0, 0.0, 2 * (4 * std::atan(1))); cr->close_path(); cr->fill_preserve(); cr->stroke(); diff --git a/src/components/statusindicator.hpp b/src/components/statusindicator.hpp index b2cf0bd..ad9122b 100644 --- a/src/components/statusindicator.hpp +++ b/src/components/statusindicator.hpp @@ -6,7 +6,7 @@ class StatusIndicator : public Gtk::Widget { public: StatusIndicator(Snowflake user_id); - virtual ~StatusIndicator(); + ~StatusIndicator() override = default; protected: Gtk::SizeRequestMode get_request_mode_vfunc() const override; diff --git a/src/dialogs/joinguild.hpp b/src/dialogs/joinguild.hpp index 109d010..ba061f3 100644 --- a/src/dialogs/joinguild.hpp +++ b/src/dialogs/joinguild.hpp @@ -10,7 +10,7 @@ public: protected: void on_entry_changed(); - bool IsCode(std::string str); + static bool IsCode(std::string str); Gtk::Box m_layout; Gtk::Button m_ok; diff --git a/src/discord/channel.cpp b/src/discord/channel.cpp index 9d47076..0770581 100644 --- a/src/discord/channel.cpp +++ b/src/discord/channel.cpp @@ -116,5 +116,8 @@ std::vector ChannelData::GetDMRecipients() const { return ret; } - return std::vector(); + return {}; +} +bool ChannelData::IsText() const noexcept { + return Type == ChannelType::GUILD_TEXT || Type == ChannelType::GUILD_NEWS; } diff --git a/src/discord/channel.hpp b/src/discord/channel.hpp index 89e43a0..8feeb92 100644 --- a/src/discord/channel.hpp +++ b/src/discord/channel.hpp @@ -94,14 +94,15 @@ struct ChannelData { friend void from_json(const nlohmann::json &j, ChannelData &m); void update_from_json(const nlohmann::json &j); - bool NSFW() const; - bool IsDM() const noexcept; - bool IsThread() const noexcept; - bool IsJoinedThread() const; - bool IsCategory() const noexcept; - bool HasIcon() const noexcept; - std::string GetIconURL() const; - std::vector GetChildIDs() const; - std::optional GetOverwrite(Snowflake id) const; - std::vector GetDMRecipients() const; + [[nodiscard]] bool NSFW() const; + [[nodiscard]] bool IsDM() const noexcept; + [[nodiscard]] bool IsThread() const noexcept; + [[nodiscard]] bool IsJoinedThread() const; + [[nodiscard]] bool IsCategory() const noexcept; + [[nodiscard]] bool IsText() const noexcept; + [[nodiscard]] bool HasIcon() const noexcept; + [[nodiscard]] std::string GetIconURL() const; + [[nodiscard]] std::vector GetChildIDs() const; + [[nodiscard]] std::optional GetOverwrite(Snowflake id) const; + [[nodiscard]] std::vector GetDMRecipients() const; }; diff --git a/src/discord/discord.cpp b/src/discord/discord.cpp index c11210d..7b9103e 100644 --- a/src/discord/discord.cpp +++ b/src/discord/discord.cpp @@ -1,8 +1,8 @@ #include "abaddon.hpp" #include "discord.hpp" #include "util.hpp" -#include #include +#include using namespace std::string_literals; @@ -68,10 +68,6 @@ bool DiscordClient::IsStoreValid() const { return m_store.IsValid(); } -const UserSettings &DiscordClient::GetUserSettings() const { - return m_user_settings; -} - std::unordered_set DiscordClient::GetGuilds() const { return m_store.GetGuilds(); } @@ -87,7 +83,7 @@ std::vector DiscordClient::GetUserSortedGuilds() const { auto guilds = GetGuilds(); for (const auto &entry : m_user_settings.GuildFolders) { // can contain guilds not a part of for (const auto &id : entry.GuildIDs) { - if (std::find(guilds.begin(), guilds.end(), id) != guilds.end()) + if (guilds.find(id) != guilds.end()) folder_order.push_back(id); } } @@ -115,19 +111,19 @@ std::vector DiscordClient::GetMessagesBefore(Snowflake channel_id, Snow return m_store.GetMessagesBefore(channel_id, message_id, limit); } -void DiscordClient::FetchInvite(std::string code, sigc::slot)> callback) { - m_http.MakeGET("/invites/" + code + "?with_counts=true", [this, callback](http::response_type r) { +void DiscordClient::FetchInvite(const std::string &code, const sigc::slot)> &callback) { + m_http.MakeGET("/invites/" + code + "?with_counts=true", [callback](http::response_type r) { if (!CheckCode(r)) { if (r.status_code == 404) callback(std::nullopt); return; - }; + } callback(nlohmann::json::parse(r.text).get()); }); } -void DiscordClient::FetchMessagesInChannel(Snowflake id, sigc::slot &)> cb) { +void DiscordClient::FetchMessagesInChannel(Snowflake id, const sigc::slot &)> &cb) { std::string path = "/channels/" + std::to_string(id) + "/messages?limit=50"; m_http.MakeGET(path, [this, id, cb](const http::response_type &r) { if (!CheckCode(r)) { @@ -164,9 +160,9 @@ void DiscordClient::FetchMessagesInChannel(Snowflake id, sigc::slot &)> cb) { +void DiscordClient::FetchMessagesInChannelBefore(Snowflake channel_id, Snowflake before_id, const sigc::slot &)> &cb) { std::string path = "/channels/" + std::to_string(channel_id) + "/messages?limit=50&before=" + std::to_string(before_id); - m_http.MakeGET(path, [this, channel_id, cb](http::response_type r) { + m_http.MakeGET(path, [this, cb](http::response_type r) { if (!CheckCode(r)) return; std::vector msgs; @@ -210,10 +206,6 @@ std::optional DiscordClient::GetMember(Snowflake user_id, Snowflake return m_store.GetGuildMember(guild_id, user_id); } -std::optional DiscordClient::GetBan(Snowflake guild_id, Snowflake user_id) const { - return m_store.GetBan(guild_id, user_id); -} - std::optional DiscordClient::GetPermissionOverwrite(Snowflake channel_id, Snowflake id) const { return m_store.GetPermissionOverwrite(channel_id, id); } @@ -243,14 +235,14 @@ std::optional DiscordClient::GetMemberHighestRole(Snowflake guild_id, const auto data = GetMember(user_id, guild_id); if (!data.has_value()) return std::nullopt; - if (data->Roles.size() == 0) return std::nullopt; + if (data->Roles.empty()) return std::nullopt; if (data->Roles.size() == 1) return GetRole(data->Roles[0]); std::vector roles; for (const auto id : data->Roles) roles.push_back(*GetRole(id)); - return *std::max_element(roles.begin(), roles.end(), [this](const auto &a, const auto &b) -> bool { + return *std::max_element(roles.begin(), roles.end(), [](const auto &a, const auto &b) -> bool { return a.Position < b.Position; }); } @@ -281,7 +273,7 @@ std::vector DiscordClient::GetActiveThreads(Snowflake channel_id) c return m_store.GetActiveThreads(channel_id); } -void DiscordClient::GetArchivedPublicThreads(Snowflake channel_id, sigc::slot callback) { +void DiscordClient::GetArchivedPublicThreads(Snowflake channel_id, const sigc::slot &callback) { m_http.MakeGET("/channels/" + std::to_string(channel_id) + "/threads/archived/public", [this, callback](const http::response_type &r) { if (CheckCode(r)) { const auto data = nlohmann::json::parse(r.text).get(); @@ -294,7 +286,7 @@ void DiscordClient::GetArchivedPublicThreads(Snowflake channel_id, sigc::slot callback) { +void DiscordClient::GetArchivedPrivateThreads(Snowflake channel_id, const sigc::slot &callback) { m_http.MakeGET("/channels/" + std::to_string(channel_id) + "/users/@me/threads/archived/private", [this, callback](const http::response_type &r) { if (CheckCode(r)) { const auto data = nlohmann::json::parse(r.text).get(); @@ -411,7 +403,7 @@ bool DiscordClient::CanManageMember(Snowflake guild_id, Snowflake actor, Snowfla return actor_highest->Position > target_highest->Position; } -void DiscordClient::ChatMessageCallback(std::string nonce, const http::response_type &response) { +void DiscordClient::ChatMessageCallback(const std::string &nonce, const http::response_type &response) { if (!CheckCode(response)) { if (response.status_code == http::TooManyRequests) { try { // not sure if this body is guaranteed @@ -481,7 +473,7 @@ void DiscordClient::DeleteMessage(Snowflake channel_id, Snowflake id) { void DiscordClient::EditMessage(Snowflake channel_id, Snowflake id, std::string content) { std::string path = "/channels/" + std::to_string(channel_id) + "/messages/" + std::to_string(id); MessageEditObject obj; - obj.Content = content; + obj.Content = std::move(content); nlohmann::json j = obj; m_http.MakePATCH(path, j.dump(), [](auto) {}); } @@ -516,7 +508,7 @@ void DiscordClient::SendThreadLazyLoad(Snowflake id) { m_websocket.Send(msg); } -void DiscordClient::JoinGuild(std::string code) { +void DiscordClient::JoinGuild(const std::string &code) { m_http.MakePOST("/invites/" + code, "{}", [](auto) {}); } @@ -554,14 +546,10 @@ void DiscordClient::UpdateStatus(PresenceStatus status, bool is_afk, const Activ m_signal_presence_update.emit(GetUserData(), status); } -void DiscordClient::CreateDM(Snowflake user_id) { - CreateDM(user_id, [](...) {}); -} - -void DiscordClient::CreateDM(Snowflake user_id, sigc::slot callback) { +void DiscordClient::CreateDM(Snowflake user_id, const sigc::slot &callback) { CreateDMObject obj; obj.Recipients.push_back(user_id); - m_http.MakePOST("/users/@me/channels", nlohmann::json(obj).dump(), [this, callback](const http::response &response) { + m_http.MakePOST("/users/@me/channels", nlohmann::json(obj).dump(), [callback](const http::response &response) { if (!CheckCode(response)) { callback(DiscordError::NONE, Snowflake::Invalid); return; @@ -572,7 +560,7 @@ void DiscordClient::CreateDM(Snowflake user_id, sigc::slot callback) { +void DiscordClient::SetGuildName(Snowflake id, const Glib::ustring &name, const sigc::slot &callback) { ModifyGuildObject obj; obj.Name = name; - m_http.MakePATCH("/guilds/" + std::to_string(id), nlohmann::json(obj).dump(), [this, callback](const http::response_type &r) { + m_http.MakePATCH("/guilds/" + std::to_string(id), nlohmann::json(obj).dump(), [callback](const http::response_type &r) { if (CheckCode(r)) callback(DiscordError::NONE); else @@ -632,14 +616,10 @@ void DiscordClient::SetGuildName(Snowflake id, const Glib::ustring &name, sigc:: }); } -void DiscordClient::SetGuildIcon(Snowflake id, const std::string &data) { - SetGuildIcon(id, data, [](auto) {}); -} - -void DiscordClient::SetGuildIcon(Snowflake id, const std::string &data, sigc::slot callback) { +void DiscordClient::SetGuildIcon(Snowflake id, const std::string &data, const sigc::slot &callback) { ModifyGuildObject obj; obj.IconData = data; - m_http.MakePATCH("/guilds/" + std::to_string(id), nlohmann::json(obj).dump(), [this, callback](const http::response_type &r) { + m_http.MakePATCH("/guilds/" + std::to_string(id), nlohmann::json(obj).dump(), [callback](const http::response_type &r) { if (CheckCode(r)) callback(DiscordError::NONE); else @@ -647,12 +627,8 @@ void DiscordClient::SetGuildIcon(Snowflake id, const std::string &data, sigc::sl }); } -void DiscordClient::UnbanUser(Snowflake guild_id, Snowflake user_id) { - UnbanUser(guild_id, user_id, [](const auto) {}); -} - -void DiscordClient::UnbanUser(Snowflake guild_id, Snowflake user_id, sigc::slot callback) { - m_http.MakeDELETE("/guilds/" + std::to_string(guild_id) + "/bans/" + std::to_string(user_id), [this, callback](const http::response_type &response) { +void DiscordClient::UnbanUser(Snowflake guild_id, Snowflake user_id, const sigc::slot &callback) { + m_http.MakeDELETE("/guilds/" + std::to_string(guild_id) + "/bans/" + std::to_string(user_id), [callback](const http::response_type &response) { if (CheckCode(response, 204)) callback(DiscordError::NONE); else @@ -660,12 +636,8 @@ void DiscordClient::UnbanUser(Snowflake guild_id, Snowflake user_id, sigc::slot< }); } -void DiscordClient::DeleteInvite(const std::string &code) { - DeleteInvite(code, [](const auto) {}); -} - -void DiscordClient::DeleteInvite(const std::string &code, sigc::slot callback) { - m_http.MakeDELETE("/invites/" + code, [this, callback](const http::response_type &response) { +void DiscordClient::DeleteInvite(const std::string &code, const sigc::slot &callback) { + m_http.MakeDELETE("/invites/" + code, [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -674,21 +646,21 @@ void DiscordClient::DeleteInvite(const std::string &code, sigc::slot callback) { +void DiscordClient::ModifyRolePermissions(Snowflake guild_id, Snowflake role_id, Permission permissions, const sigc::slot &callback) { ModifyGuildRoleObject obj; obj.Permissions = permissions; - m_http.MakePATCH("/guilds/" + std::to_string(guild_id) + "/roles/" + std::to_string(role_id), nlohmann::json(obj).dump(), [this, callback](const http::response_type &response) { + m_http.MakePATCH("/guilds/" + std::to_string(guild_id) + "/roles/" + std::to_string(role_id), nlohmann::json(obj).dump(), [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -696,10 +668,10 @@ void DiscordClient::ModifyRolePermissions(Snowflake guild_id, Snowflake role_id, }); } -void DiscordClient::ModifyRoleName(Snowflake guild_id, Snowflake role_id, const Glib::ustring &name, sigc::slot callback) { +void DiscordClient::ModifyRoleName(Snowflake guild_id, Snowflake role_id, const Glib::ustring &name, const sigc::slot &callback) { ModifyGuildRoleObject obj; obj.Name = name; - m_http.MakePATCH("/guilds/" + std::to_string(guild_id) + "/roles/" + std::to_string(role_id), nlohmann::json(obj).dump(), [this, callback](const http::response_type &response) { + m_http.MakePATCH("/guilds/" + std::to_string(guild_id) + "/roles/" + std::to_string(role_id), nlohmann::json(obj).dump(), [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -707,10 +679,10 @@ void DiscordClient::ModifyRoleName(Snowflake guild_id, Snowflake role_id, const }); } -void DiscordClient::ModifyRoleColor(Snowflake guild_id, Snowflake role_id, uint32_t color, sigc::slot callback) { +void DiscordClient::ModifyRoleColor(Snowflake guild_id, Snowflake role_id, uint32_t color, const sigc::slot &callback) { ModifyGuildRoleObject obj; obj.Color = color; - m_http.MakePATCH("/guilds/" + std::to_string(guild_id) + "/roles/" + std::to_string(role_id), nlohmann::json(obj).dump(), [this, callback](const http::response_type &response) { + m_http.MakePATCH("/guilds/" + std::to_string(guild_id) + "/roles/" + std::to_string(role_id), nlohmann::json(obj).dump(), [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -718,7 +690,7 @@ void DiscordClient::ModifyRoleColor(Snowflake guild_id, Snowflake role_id, uint3 }); } -void DiscordClient::ModifyRoleColor(Snowflake guild_id, Snowflake role_id, Gdk::RGBA color, sigc::slot callback) { +void DiscordClient::ModifyRoleColor(Snowflake guild_id, Snowflake role_id, const Gdk::RGBA &color, const sigc::slot &callback) { uint32_t int_color = 0; int_color |= static_cast(color.get_blue() * 255.0) << 0; int_color |= static_cast(color.get_green() * 255.0) << 8; @@ -726,7 +698,7 @@ void DiscordClient::ModifyRoleColor(Snowflake guild_id, Snowflake role_id, Gdk:: ModifyRoleColor(guild_id, role_id, int_color, callback); } -void DiscordClient::ModifyRolePosition(Snowflake guild_id, Snowflake role_id, int position, sigc::slot callback) { +void DiscordClient::ModifyRolePosition(Snowflake guild_id, Snowflake role_id, int position, const sigc::slot &callback) { const auto guild = GetGuild(guild_id); if (!guild.has_value() || !guild->Roles.has_value()) return; const auto &roles = *guild->Roles; @@ -763,7 +735,7 @@ void DiscordClient::ModifyRolePosition(Snowflake guild_id, Snowflake role_id, in for (size_t i = range_from; i < range_to; i++) obj.Positions.push_back({ roles[i].ID, roles[i].Position + dir }); - m_http.MakePATCH("/guilds/" + std::to_string(guild_id) + "/roles", nlohmann::json(obj).dump(), [this, callback](const http::response_type &response) { + m_http.MakePATCH("/guilds/" + std::to_string(guild_id) + "/roles", nlohmann::json(obj).dump(), [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -771,11 +743,11 @@ void DiscordClient::ModifyRolePosition(Snowflake guild_id, Snowflake role_id, in }); } -void DiscordClient::ModifyEmojiName(Snowflake guild_id, Snowflake emoji_id, const Glib::ustring &name, sigc::slot callback) { +void DiscordClient::ModifyEmojiName(Snowflake guild_id, Snowflake emoji_id, const Glib::ustring &name, const sigc::slot &callback) { ModifyGuildEmojiObject obj; obj.Name = name; - m_http.MakePATCH("/guilds/" + std::to_string(guild_id) + "/emojis/" + std::to_string(emoji_id), nlohmann::json(obj).dump(), [this, callback](const http::response_type &response) { + m_http.MakePATCH("/guilds/" + std::to_string(guild_id) + "/emojis/" + std::to_string(emoji_id), nlohmann::json(obj).dump(), [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -783,8 +755,8 @@ void DiscordClient::ModifyEmojiName(Snowflake guild_id, Snowflake emoji_id, cons }); } -void DiscordClient::DeleteEmoji(Snowflake guild_id, Snowflake emoji_id, sigc::slot callback) { - m_http.MakeDELETE("/guilds/" + std::to_string(guild_id) + "/emojis/" + std::to_string(emoji_id), [this, callback](const http::response_type &response) { +void DiscordClient::DeleteEmoji(Snowflake guild_id, Snowflake emoji_id, const sigc::slot &callback) { + m_http.MakeDELETE("/guilds/" + std::to_string(guild_id) + "/emojis/" + std::to_string(emoji_id), [callback](const http::response_type &response) { if (CheckCode(response, 204)) callback(DiscordError::NONE); else @@ -792,14 +764,8 @@ void DiscordClient::DeleteEmoji(Snowflake guild_id, Snowflake emoji_id, sigc::sl }); } -std::optional DiscordClient::GetGuildApplication(Snowflake guild_id) const { - const auto it = m_guild_join_requests.find(guild_id); - if (it == m_guild_join_requests.end()) return std::nullopt; - return it->second; -} - -void DiscordClient::RemoveRelationship(Snowflake id, sigc::slot callback) { - m_http.MakeDELETE("/users/@me/relationships/" + std::to_string(id), [this, callback](const http::response_type &response) { +void DiscordClient::RemoveRelationship(Snowflake id, const sigc::slot &callback) { + m_http.MakeDELETE("/users/@me/relationships/" + std::to_string(id), [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -807,11 +773,11 @@ void DiscordClient::RemoveRelationship(Snowflake id, sigc::slot callback) { +void DiscordClient::SendFriendRequest(const Glib::ustring &username, int discriminator, const sigc::slot &callback) { FriendRequestObject obj; obj.Username = username; obj.Discriminator = discriminator; - m_http.MakePOST("/users/@me/relationships", nlohmann::json(obj).dump(), [this, callback](const http::response_type &response) { + m_http.MakePOST("/users/@me/relationships", nlohmann::json(obj).dump(), [callback](const http::response_type &response) { if (CheckCode(response, 204)) callback(DiscordError::NONE); else @@ -819,8 +785,8 @@ void DiscordClient::SendFriendRequest(const Glib::ustring &username, int discrim }); } -void DiscordClient::PutRelationship(Snowflake id, sigc::slot callback) { - m_http.MakePUT("/users/@me/relationships/" + std::to_string(id), "{}", [this, callback](const http::response_type &response) { +void DiscordClient::PutRelationship(Snowflake id, const sigc::slot &callback) { + m_http.MakePUT("/users/@me/relationships/" + std::to_string(id), "{}", [callback](const http::response_type &response) { if (CheckCode(response, 204)) callback(DiscordError::NONE); else @@ -828,8 +794,8 @@ void DiscordClient::PutRelationship(Snowflake id, sigc::slot callback) { - m_http.MakePUT("/channels/" + std::to_string(channel_id) + "/pins/" + std::to_string(message_id), "", [this, callback](const http::response_type &response) { +void DiscordClient::Pin(Snowflake channel_id, Snowflake message_id, const sigc::slot &callback) { + m_http.MakePUT("/channels/" + std::to_string(channel_id) + "/pins/" + std::to_string(message_id), "", [callback](const http::response_type &response) { if (CheckCode(response, 204)) callback(DiscordError::NONE); else @@ -837,8 +803,8 @@ void DiscordClient::Pin(Snowflake channel_id, Snowflake message_id, sigc::slot callback) { - m_http.MakeDELETE("/channels/" + std::to_string(channel_id) + "/pins/" + std::to_string(message_id), [this, callback](const http::response_type &response) { +void DiscordClient::Unpin(Snowflake channel_id, Snowflake message_id, const sigc::slot &callback) { + m_http.MakeDELETE("/channels/" + std::to_string(channel_id) + "/pins/" + std::to_string(message_id), [callback](const http::response_type &response) { if (CheckCode(response, 204)) callback(DiscordError::NONE); else @@ -848,8 +814,8 @@ void DiscordClient::Unpin(Snowflake channel_id, Snowflake message_id, sigc::slot // i dont know if the location parameter is necessary at all but discord's thread implementation is extremely strange // so its here just in case -void DiscordClient::LeaveThread(Snowflake channel_id, const std::string &location, sigc::slot callback) { - m_http.MakeDELETE("/channels/" + std::to_string(channel_id) + "/thread-members/@me?location=" + location, [this, callback](const http::response_type &response) { +void DiscordClient::LeaveThread(Snowflake channel_id, const std::string &location, const sigc::slot &callback) { + m_http.MakeDELETE("/channels/" + std::to_string(channel_id) + "/thread-members/@me?location=" + location, [callback](const http::response_type &response) { if (CheckCode(response, 204)) callback(DiscordError::NONE); else @@ -857,11 +823,11 @@ void DiscordClient::LeaveThread(Snowflake channel_id, const std::string &locatio }); } -void DiscordClient::ArchiveThread(Snowflake channel_id, sigc::slot callback) { +void DiscordClient::ArchiveThread(Snowflake channel_id, const sigc::slot &callback) { ModifyChannelObject obj; obj.Archived = true; obj.Locked = true; - m_http.MakePATCH("/channels/" + std::to_string(channel_id), nlohmann::json(obj).dump(), [this, callback](const http::response_type &response) { + m_http.MakePATCH("/channels/" + std::to_string(channel_id), nlohmann::json(obj).dump(), [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -869,11 +835,11 @@ void DiscordClient::ArchiveThread(Snowflake channel_id, sigc::slot callback) { +void DiscordClient::UnArchiveThread(Snowflake channel_id, const sigc::slot &callback) { ModifyChannelObject obj; obj.Archived = false; obj.Locked = false; - m_http.MakePATCH("/channels/" + std::to_string(channel_id), nlohmann::json(obj).dump(), [this, callback](const http::response_type &response) { + m_http.MakePATCH("/channels/" + std::to_string(channel_id), nlohmann::json(obj).dump(), [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -881,11 +847,11 @@ void DiscordClient::UnArchiveThread(Snowflake channel_id, sigc::slot callback) { +void DiscordClient::MarkChannelAsRead(Snowflake channel_id, const sigc::slot &callback) { if (m_unread.find(channel_id) == m_unread.end()) return; const auto iter = m_last_message_id.find(channel_id); if (iter == m_last_message_id.end()) return; - m_http.MakePOST("/channels/" + std::to_string(channel_id) + "/messages/" + std::to_string(iter->second) + "/ack", "{\"token\":null}", [this, callback](const http::response_type &response) { + m_http.MakePOST("/channels/" + std::to_string(channel_id) + "/messages/" + std::to_string(iter->second) + "/ack", "{\"token\":null}", [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -893,7 +859,7 @@ void DiscordClient::MarkChannelAsRead(Snowflake channel_id, sigc::slot callback) { +void DiscordClient::MarkGuildAsRead(Snowflake guild_id, const sigc::slot &callback) { AckBulkData data; const auto channels = GetChannelsInGuild(guild_id); for (const auto &[unread, mention_count] : m_unread) { @@ -908,7 +874,7 @@ void DiscordClient::MarkGuildAsRead(Snowflake guild_id, sigc::slot callback) { +void DiscordClient::MuteChannel(Snowflake channel_id, const sigc::slot &callback) { const auto channel = GetChannel(channel_id); if (!channel.has_value()) return; const auto guild_id_path = channel->GuildID.has_value() ? std::to_string(*channel->GuildID) : "@me"s; nlohmann::json j; j["channel_overrides"][std::to_string(channel_id)]["mute_config"] = MuteConfigData { std::nullopt, -1 }; j["channel_overrides"][std::to_string(channel_id)]["muted"] = true; - m_http.MakePATCH("/users/@me/guilds/" + guild_id_path + "/settings", j.dump(), [this, callback](const http::response_type &response) { + m_http.MakePATCH("/users/@me/guilds/" + guild_id_path + "/settings", j.dump(), [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -931,13 +897,13 @@ void DiscordClient::MuteChannel(Snowflake channel_id, sigc::slot callback) { +void DiscordClient::UnmuteChannel(Snowflake channel_id, const sigc::slot &callback) { const auto channel = GetChannel(channel_id); if (!channel.has_value()) return; const auto guild_id_path = channel->GuildID.has_value() ? std::to_string(*channel->GuildID) : "@me"s; nlohmann::json j; j["channel_overrides"][std::to_string(channel_id)]["muted"] = false; - m_http.MakePATCH("/users/@me/guilds/" + guild_id_path + "/settings", j.dump(), [this, callback](const http::response_type &response) { + m_http.MakePATCH("/users/@me/guilds/" + guild_id_path + "/settings", j.dump(), [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -945,7 +911,7 @@ void DiscordClient::UnmuteChannel(Snowflake channel_id, sigc::slot callback) { +void DiscordClient::MarkAllAsRead(const sigc::slot &callback) { AckBulkData data; for (const auto &[unread, mention_count] : m_unread) { const auto iter = m_last_message_id.find(unread); @@ -957,7 +923,7 @@ void DiscordClient::MarkAllAsRead(sigc::slot callback) if (data.ReadStates.empty()) return; - m_http.MakePOST("/read-states/ack-bulk", nlohmann::json(data).dump(), [this, callback](const http::response_type &response) { + m_http.MakePOST("/read-states/ack-bulk", nlohmann::json(data).dump(), [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -965,8 +931,8 @@ void DiscordClient::MarkAllAsRead(sigc::slot callback) }); } -void DiscordClient::MuteGuild(Snowflake id, sigc::slot callback) { - m_http.MakePATCH("/users/@me/guilds/" + std::to_string(id) + "/settings", R"({"muted":true})", [this, callback](const http::response_type &response) { +void DiscordClient::MuteGuild(Snowflake id, const sigc::slot &callback) { + m_http.MakePATCH("/users/@me/guilds/" + std::to_string(id) + "/settings", R"({"muted":true})", [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -974,8 +940,8 @@ void DiscordClient::MuteGuild(Snowflake id, sigc::slot }); } -void DiscordClient::UnmuteGuild(Snowflake id, sigc::slot callback) { - m_http.MakePATCH("/users/@me/guilds/" + std::to_string(id) + "/settings", R"({"muted":false})", [this, callback](const http::response_type &response) { +void DiscordClient::UnmuteGuild(Snowflake id, const sigc::slot &callback) { + m_http.MakePATCH("/users/@me/guilds/" + std::to_string(id) + "/settings", R"({"muted":false})", [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -983,8 +949,8 @@ void DiscordClient::UnmuteGuild(Snowflake id, sigc::slot callback) { - m_http.MakePATCH("/channels/" + std::to_string(id) + "/thread-members/@me/settings", R"({"muted":true})", [this, callback](const http::response_type &response) { +void DiscordClient::MuteThread(Snowflake id, const sigc::slot &callback) { + m_http.MakePATCH("/channels/" + std::to_string(id) + "/thread-members/@me/settings", R"({"muted":true})", [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -992,8 +958,8 @@ void DiscordClient::MuteThread(Snowflake id, sigc::slot }); } -void DiscordClient::UnmuteThread(Snowflake id, sigc::slot callback) { - m_http.MakePATCH("/channels/" + std::to_string(id) + "/thread-members/@me/settings", R"({"muted":false})", [this, callback](const http::response_type &response) { +void DiscordClient::UnmuteThread(Snowflake id, const sigc::slot &callback) { + m_http.MakePATCH("/channels/" + std::to_string(id) + "/thread-members/@me/settings", R"({"muted":false})", [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -1001,7 +967,7 @@ void DiscordClient::UnmuteThread(Snowflake id, sigc::slot, DiscordError code)> callback) { +void DiscordClient::FetchPinned(Snowflake id, const sigc::slot, DiscordError code)> &callback) { // return from db if we know the pins have already been requested if (m_channels_pinned_requested.find(id) != m_channels_pinned_requested.end()) { callback(m_store.GetPinnedMessages(id), DiscordError::NONE); @@ -1019,7 +985,7 @@ void DiscordClient::FetchPinned(Snowflake id, sigc::slot DiscordClient::GetBansInGuild(Snowflake guild_id) { return m_store.GetBans(guild_id); } -void DiscordClient::FetchGuildBan(Snowflake guild_id, Snowflake user_id, sigc::slot callback) { +void DiscordClient::FetchGuildBan(Snowflake guild_id, Snowflake user_id, const sigc::slot &callback) { m_http.MakeGET("/guilds/" + std::to_string(guild_id) + "/bans/" + std::to_string(user_id), [this, callback, guild_id](const http::response_type &response) { if (!CheckCode(response)) return; auto ban = nlohmann::json::parse(response.text).get(); @@ -1050,7 +1016,7 @@ void DiscordClient::FetchGuildBan(Snowflake guild_id, Snowflake user_id, sigc::s }); } -void DiscordClient::FetchGuildBans(Snowflake guild_id, sigc::slot)> callback) { +void DiscordClient::FetchGuildBans(Snowflake guild_id, const sigc::slot)> &callback) { m_http.MakeGET("/guilds/" + std::to_string(guild_id) + "/bans", [this, callback, guild_id](const http::response_type &response) { if (!CheckCode(response)) return; auto bans = nlohmann::json::parse(response.text).get>(); @@ -1064,8 +1030,8 @@ void DiscordClient::FetchGuildBans(Snowflake guild_id, sigc::slot)> callback) { - m_http.MakeGET("/guilds/" + std::to_string(guild_id) + "/invites", [this, callback, guild_id](const http::response_type &response) { +void DiscordClient::FetchGuildInvites(Snowflake guild_id, const sigc::slot)> &callback) { + m_http.MakeGET("/guilds/" + std::to_string(guild_id) + "/invites", [this, callback](const http::response_type &response) { // store? if (!CheckCode(response)) return; auto invites = nlohmann::json::parse(response.text).get>(); @@ -1080,7 +1046,7 @@ void DiscordClient::FetchGuildInvites(Snowflake guild_id, sigc::slot callback) { +void DiscordClient::FetchAuditLog(Snowflake guild_id, const sigc::slot &callback) { m_http.MakeGET("/guilds/" + std::to_string(guild_id) + "/audit-logs", [this, callback](const http::response &response) { if (!CheckCode(response)) return; auto data = nlohmann::json::parse(response.text).get(); @@ -1094,7 +1060,7 @@ void DiscordClient::FetchAuditLog(Snowflake guild_id, sigc::slot)> callback) { +void DiscordClient::FetchGuildEmojis(Snowflake guild_id, const sigc::slot)> &callback) { m_http.MakeGET("/guilds/" + std::to_string(guild_id) + "/emojis", [this, callback](const http::response_type &response) { if (!CheckCode(response)) return; auto emojis = nlohmann::json::parse(response.text).get>(); @@ -1102,19 +1068,19 @@ void DiscordClient::FetchGuildEmojis(Snowflake guild_id, sigc::slot callback) { - m_http.MakeGET("/users/" + std::to_string(user_id) + "/profile", [this, callback](const http::response_type &response) { +void DiscordClient::FetchUserProfile(Snowflake user_id, const sigc::slot &callback) { + m_http.MakeGET("/users/" + std::to_string(user_id) + "/profile", [callback](const http::response_type &response) { if (!CheckCode(response)) return; callback(nlohmann::json::parse(response.text).get()); }); } -void DiscordClient::FetchUserNote(Snowflake user_id, sigc::slot callback) { - m_http.MakeGET("/users/@me/notes/" + std::to_string(user_id), [this, callback](const http::response_type &response) { +void DiscordClient::FetchUserNote(Snowflake user_id, const sigc::slot &callback) { + m_http.MakeGET("/users/@me/notes/" + std::to_string(user_id), [callback](const http::response_type &response) { if (response.status_code == 404) return; if (!CheckCode(response)) return; const auto note = nlohmann::json::parse(response.text).get().Note; @@ -1123,14 +1089,10 @@ void DiscordClient::FetchUserNote(Snowflake user_id, sigc::slot callback) { +void DiscordClient::SetUserNote(Snowflake user_id, std::string note, const sigc::slot &callback) { UserSetNoteObject obj; - obj.Note = note; - m_http.MakePUT("/users/@me/notes/" + std::to_string(user_id), nlohmann::json(obj).dump(), [this, callback](const http::response_type &response) { + obj.Note = std::move(note); + m_http.MakePUT("/users/@me/notes/" + std::to_string(user_id), nlohmann::json(obj).dump(), [callback](const http::response_type &response) { if (CheckCode(response, 204)) callback(DiscordError::NONE); else @@ -1138,7 +1100,7 @@ void DiscordClient::SetUserNote(Snowflake user_id, std::string note, sigc::slot< }); } -void DiscordClient::FetchUserRelationships(Snowflake user_id, sigc::slot)> callback) { +void DiscordClient::FetchUserRelationships(Snowflake user_id, const sigc::slot)> &callback) { m_http.MakeGET("/users/" + std::to_string(user_id) + "/relationships", [this, callback](const http::response_type &response) { if (!CheckCode(response)) return; RelationshipsData data = nlohmann::json::parse(response.text); @@ -1148,26 +1110,26 @@ void DiscordClient::FetchUserRelationships(Snowflake user_id, sigc::slotIsPending.has_value()) return *member->IsPending; return false; } -void DiscordClient::GetVerificationGateInfo(Snowflake guild_id, sigc::slot)> callback) { - m_http.MakeGET("/guilds/" + std::to_string(guild_id) + "/member-verification", [this, callback](const http::response_type &response) { +void DiscordClient::GetVerificationGateInfo(Snowflake guild_id, const sigc::slot)> &callback) { + m_http.MakeGET("/guilds/" + std::to_string(guild_id) + "/member-verification", [callback](const http::response_type &response) { if (!CheckCode(response)) return; if (response.status_code == 204) callback(std::nullopt); callback(nlohmann::json::parse(response.text).get()); }); } -void DiscordClient::AcceptVerificationGate(Snowflake guild_id, VerificationGateInfoObject info, sigc::slot callback) { +void DiscordClient::AcceptVerificationGate(Snowflake guild_id, VerificationGateInfoObject info, const sigc::slot &callback) { if (info.VerificationFields.has_value()) for (auto &field : *info.VerificationFields) field.Response = true; - m_http.MakePUT("/guilds/" + std::to_string(guild_id) + "/requests/@me", nlohmann::json(info).dump(), [this, callback](const http::response_type &response) { + m_http.MakePUT("/guilds/" + std::to_string(guild_id) + "/requests/@me", nlohmann::json(info).dump(), [callback](const http::response_type &response) { if (CheckCode(response)) callback(DiscordError::NONE); else @@ -1175,14 +1137,14 @@ void DiscordClient::AcceptVerificationGate(Snowflake guild_id, VerificationGateI }); } -void DiscordClient::UpdateToken(std::string token) { +void DiscordClient::UpdateToken(const std::string &token) { if (!IsStarted()) { m_token = token; m_http.SetAuth(token); } } -void DiscordClient::SetUserAgent(std::string agent) { +void DiscordClient::SetUserAgent(const std::string &agent) { m_http.SetUserAgent(agent); m_websocket.SetUserAgent(agent); } @@ -1483,7 +1445,7 @@ void DiscordClient::HandleGatewayHello(const GatewayMessage &msg) { HelloMessageData d = msg.Data; m_heartbeat_msec = d.HeartbeatInterval; m_heartbeat_waiter.revive(); - m_heartbeat_thread = std::thread(std::bind(&DiscordClient::HeartbeatThread, this)); + m_heartbeat_thread = std::thread([this] { HeartbeatThread(); }); m_signal_connected.emit(); // socket is connected before this but emitting here should b fine m_reconnecting = false; // maybe should go elsewhere? if (m_wants_resume) { @@ -1736,7 +1698,7 @@ void DiscordClient::HandleGatewayGuildRoleCreate(const GatewayMessage &msg) { void DiscordClient::HandleGatewayGuildRoleDelete(const GatewayMessage &msg) { GuildRoleDeleteObject data = msg.Data; auto guild = *m_store.GetGuild(data.GuildID); - const auto pred = [this, id = data.RoleID](const RoleData &role) -> bool { + const auto pred = [id = data.RoleID](const RoleData &role) -> bool { return role.ID == id; }; guild.Roles->erase(std::remove_if(guild.Roles->begin(), guild.Roles->end(), pred), guild.Roles->end()); @@ -1826,12 +1788,12 @@ void DiscordClient::HandleGatewayInviteCreate(const GatewayMessage &msg) { invite.CreatedAt = std::move(data.CreatedAt); invite.Channel = *m_store.GetChannel(data.ChannelID); invite.Inviter = std::move(data.Inviter); - invite.IsTemporary = std::move(data.IsTemporary); - invite.MaxAge = std::move(data.MaxAge); - invite.MaxUses = std::move(data.MaxUses); + invite.IsTemporary = data.IsTemporary; + invite.MaxAge = data.MaxAge; + invite.MaxUses = data.MaxUses; invite.TargetUser = std::move(data.TargetUser); - invite.TargetUserType = std::move(data.TargetUserType); - invite.Uses = std::move(data.Uses); + invite.TargetUserType = data.TargetUserType; + invite.Uses = data.Uses; if (data.GuildID.has_value()) invite.Guild = m_store.GetGuild(*data.GuildID); m_signal_invite_create.emit(invite); @@ -1893,7 +1855,7 @@ void DiscordClient::HandleGatewayRelationshipAdd(const GatewayMessage &msg) { RelationshipAddData data = msg.Data; m_store.SetUser(data.ID, data.User); m_user_relationships[data.ID] = data.Type; - m_signal_relationship_add.emit(std::move(data)); + m_signal_relationship_add.emit(data); } // remarkably this doesnt actually mean a thread was created @@ -2168,7 +2130,7 @@ void DiscordClient::HandleGatewayGuildMemberListUpdate(const GatewayMessage &msg has_sync = true; for (const auto &item : *op.Items) { if (item->Type == "member") { - auto member = static_cast(item.get()); + auto member = dynamic_cast(item.get()); m_store.SetUser(member->User.ID, member->User); AddUserToGuild(member->User.ID, data.GuildID); m_store.SetGuildMember(data.GuildID, member->User.ID, member->GetAsMemberData()); @@ -2187,7 +2149,7 @@ void DiscordClient::HandleGatewayGuildMemberListUpdate(const GatewayMessage &msg } } else if (op.Op == "UPDATE") { if (op.OpItem.has_value() && op.OpItem.value()->Type == "member") { - const auto &m = static_cast(op.OpItem.value().get())->GetAsMemberData(); + const auto &m = dynamic_cast(op.OpItem.value().get())->GetAsMemberData(); m_store.SetGuildMember(data.GuildID, m.User->ID, m); m_signal_guild_member_update.emit(data.GuildID, m.User->ID); // cheeky } @@ -2472,12 +2434,6 @@ void DiscordClient::HandleReadyGuildSettings(const ReadyEventData &data) { } } -void DiscordClient::HandleUserGuildSettingsUpdateForDMs(const UserGuildSettingsUpdateData &data) { - const auto channels = GetPrivateChannels(); - std::set now_muted_channels; - const auto now = Snowflake::FromNow(); -} - void DiscordClient::LoadEventMap() { m_event_map["READY"] = GatewayEvent::READY; m_event_map["MESSAGE_CREATE"] = GatewayEvent::MESSAGE_CREATE; diff --git a/src/discord/discord.hpp b/src/discord/discord.hpp index 6add18f..7b0a00e 100644 --- a/src/discord/discord.hpp +++ b/src/discord/discord.hpp @@ -55,7 +55,6 @@ public: std::unordered_set GetGuilds() const; const UserData &GetUserData() const; - const UserSettings &GetUserSettings() const; std::vector GetUserSortedGuilds() const; std::vector GetMessagesForChannel(Snowflake id, size_t limit = 50) const; std::vector GetMessagesBefore(Snowflake channel_id, Snowflake message_id, size_t limit = 50) const; @@ -63,8 +62,8 @@ public: EPremiumType GetSelfPremiumType() const; - void FetchMessagesInChannel(Snowflake id, sigc::slot &)> cb); - void FetchMessagesInChannelBefore(Snowflake channel_id, Snowflake before_id, sigc::slot &)> cb); + void FetchMessagesInChannel(Snowflake id, const sigc::slot &)> &cb); + void FetchMessagesInChannelBefore(Snowflake channel_id, Snowflake before_id, const sigc::slot &)> &cb); std::optional GetMessage(Snowflake id) const; std::optional GetChannel(Snowflake id) const; std::optional GetEmoji(Snowflake id) const; @@ -73,15 +72,14 @@ public: std::optional GetRole(Snowflake id) const; std::optional GetGuild(Snowflake id) const; std::optional GetMember(Snowflake user_id, Snowflake guild_id) const; - std::optional GetBan(Snowflake guild_id, Snowflake user_id) const; Snowflake GetMemberHoistedRole(Snowflake guild_id, Snowflake user_id, bool with_color = false) const; std::optional GetMemberHighestRole(Snowflake guild_id, Snowflake user_id) const; std::set GetUsersInGuild(Snowflake id) const; std::set GetChannelsInGuild(Snowflake id) const; std::vector GetUsersInThread(Snowflake id) const; std::vector GetActiveThreads(Snowflake channel_id) const; - void GetArchivedPublicThreads(Snowflake channel_id, sigc::slot callback); - void GetArchivedPrivateThreads(Snowflake channel_id, sigc::slot callback); + void GetArchivedPublicThreads(Snowflake channel_id, const sigc::slot &callback); + void GetArchivedPrivateThreads(Snowflake channel_id, const sigc::slot &callback); std::vector GetChildChannelIDs(Snowflake parent_id) const; // get ids of given list of members for who we do not have the member data @@ -104,7 +102,7 @@ public: Permission ComputeOverwrites(Permission base, Snowflake member_id, Snowflake channel_id) const; bool CanManageMember(Snowflake guild_id, Snowflake actor, Snowflake target) const; // kick, ban, edit nickname (cant think of a better name) - void ChatMessageCallback(std::string nonce, const http::response_type &response); + void ChatMessageCallback(const std::string &nonce, const http::response_type &response); void SendChatMessage(const std::string &content, Snowflake channel); void SendChatMessage(const std::string &content, Snowflake channel, Snowflake referenced_message); @@ -112,53 +110,47 @@ public: void EditMessage(Snowflake channel_id, Snowflake id, std::string content); void SendLazyLoad(Snowflake id); void SendThreadLazyLoad(Snowflake id); - void JoinGuild(std::string code); + void JoinGuild(const std::string &code); void LeaveGuild(Snowflake id); void KickUser(Snowflake user_id, Snowflake guild_id); void BanUser(Snowflake user_id, Snowflake guild_id); // todo: reason, delete messages void UpdateStatus(PresenceStatus status, bool is_afk); void UpdateStatus(PresenceStatus status, bool is_afk, const ActivityData &obj); - void CreateDM(Snowflake user_id); - void CreateDM(Snowflake user_id, sigc::slot callback); + void CreateDM(Snowflake user_id, const sigc::slot &callback); void CloseDM(Snowflake channel_id); std::optional FindDM(Snowflake user_id); // wont find group dms void AddReaction(Snowflake id, Glib::ustring param); void RemoveReaction(Snowflake id, Glib::ustring param); - void SetGuildName(Snowflake id, const Glib::ustring &name); - void SetGuildName(Snowflake id, const Glib::ustring &name, sigc::slot callback); - void SetGuildIcon(Snowflake id, const std::string &data); - void SetGuildIcon(Snowflake id, const std::string &data, sigc::slot callback); - void UnbanUser(Snowflake guild_id, Snowflake user_id); - void UnbanUser(Snowflake guild_id, Snowflake user_id, sigc::slot callback); - void DeleteInvite(const std::string &code); - void DeleteInvite(const std::string &code, sigc::slot callback); + void SetGuildName(Snowflake id, const Glib::ustring &name, const sigc::slot &callback); + void SetGuildIcon(Snowflake id, const std::string &data, const sigc::slot &callback); + void UnbanUser(Snowflake guild_id, Snowflake user_id, const sigc::slot &callback); + void DeleteInvite(const std::string &code, const sigc::slot &callback); void AddGroupDMRecipient(Snowflake channel_id, Snowflake user_id); void RemoveGroupDMRecipient(Snowflake channel_id, Snowflake user_id); - void ModifyRolePermissions(Snowflake guild_id, Snowflake role_id, Permission permissions, sigc::slot callback); - void ModifyRoleName(Snowflake guild_id, Snowflake role_id, const Glib::ustring &name, sigc::slot callback); - void ModifyRoleColor(Snowflake guild_id, Snowflake role_id, uint32_t color, sigc::slot callback); - void ModifyRoleColor(Snowflake guild_id, Snowflake role_id, Gdk::RGBA color, sigc::slot callback); - void ModifyRolePosition(Snowflake guild_id, Snowflake role_id, int position, sigc::slot callback); - void ModifyEmojiName(Snowflake guild_id, Snowflake emoji_id, const Glib::ustring &name, sigc::slot callback); - void DeleteEmoji(Snowflake guild_id, Snowflake emoji_id, sigc::slot callback); - std::optional GetGuildApplication(Snowflake guild_id) const; - void RemoveRelationship(Snowflake id, sigc::slot callback); - void SendFriendRequest(const Glib::ustring &username, int discriminator, sigc::slot callback); - void PutRelationship(Snowflake id, sigc::slot callback); // send fr by id, accept incoming - void Pin(Snowflake channel_id, Snowflake message_id, sigc::slot callback); - void Unpin(Snowflake channel_id, Snowflake message_id, sigc::slot callback); - void LeaveThread(Snowflake channel_id, const std::string &location, sigc::slot callback); - void ArchiveThread(Snowflake channel_id, sigc::slot callback); - void UnArchiveThread(Snowflake channel_id, sigc::slot callback); - void MarkChannelAsRead(Snowflake channel_id, sigc::slot callback); - void MarkGuildAsRead(Snowflake guild_id, sigc::slot callback); - void MuteChannel(Snowflake channel_id, sigc::slot callback); - void UnmuteChannel(Snowflake channel_id, sigc::slot callback); - void MarkAllAsRead(sigc::slot callback); - void MuteGuild(Snowflake id, sigc::slot callback); - void UnmuteGuild(Snowflake id, sigc::slot callback); - void MuteThread(Snowflake id, sigc::slot callback); - void UnmuteThread(Snowflake id, sigc::slot callback); + void ModifyRolePermissions(Snowflake guild_id, Snowflake role_id, Permission permissions, const sigc::slot &callback); + void ModifyRoleName(Snowflake guild_id, Snowflake role_id, const Glib::ustring &name, const sigc::slot &callback); + void ModifyRoleColor(Snowflake guild_id, Snowflake role_id, uint32_t color, const sigc::slot &callback); + void ModifyRoleColor(Snowflake guild_id, Snowflake role_id, const Gdk::RGBA &color, const sigc::slot &callback); + void ModifyRolePosition(Snowflake guild_id, Snowflake role_id, int position, const sigc::slot &callback); + void ModifyEmojiName(Snowflake guild_id, Snowflake emoji_id, const Glib::ustring &name, const sigc::slot &callback); + void DeleteEmoji(Snowflake guild_id, Snowflake emoji_id, const sigc::slot &callback); + void RemoveRelationship(Snowflake id, const sigc::slot &callback); + void SendFriendRequest(const Glib::ustring &username, int discriminator, const sigc::slot &callback); + void PutRelationship(Snowflake id, const sigc::slot &callback); // send fr by id, accept incoming + void Pin(Snowflake channel_id, Snowflake message_id, const sigc::slot &callback); + void Unpin(Snowflake channel_id, Snowflake message_id, const sigc::slot &callback); + void LeaveThread(Snowflake channel_id, const std::string &location, const sigc::slot &callback); + void ArchiveThread(Snowflake channel_id, const sigc::slot &callback); + void UnArchiveThread(Snowflake channel_id, const sigc::slot &callback); + void MarkChannelAsRead(Snowflake channel_id, const sigc::slot &callback); + void MarkGuildAsRead(Snowflake guild_id, const sigc::slot &callback); + void MuteChannel(Snowflake channel_id, const sigc::slot &callback); + void UnmuteChannel(Snowflake channel_id, const sigc::slot &callback); + void MarkAllAsRead(const sigc::slot &callback); + void MuteGuild(Snowflake id, const sigc::slot &callback); + void UnmuteGuild(Snowflake id, const sigc::slot &callback); + void MuteThread(Snowflake id, const sigc::slot &callback); + void UnmuteThread(Snowflake id, const sigc::slot &callback); bool CanModifyRole(Snowflake guild_id, Snowflake role_id) const; bool CanModifyRole(Snowflake guild_id, Snowflake role_id, Snowflake user_id) const; @@ -177,7 +169,7 @@ public: // real client doesn't seem to use the single role endpoints so neither do we template - auto SetMemberRoles(Snowflake guild_id, Snowflake user_id, Iter begin, Iter end, sigc::slot callback) { + auto SetMemberRoles(Snowflake guild_id, Snowflake user_id, Iter begin, Iter end, const sigc::slot &callback) { ModifyGuildMemberObject obj; obj.Roles = { begin, end }; m_http.MakePATCH("/guilds/" + std::to_string(guild_id) + "/members/" + std::to_string(user_id), nlohmann::json(obj).dump(), [this, callback](const http::response_type &response) { @@ -190,30 +182,29 @@ public: // FetchGuildBans fetches all bans+reasons via api, this func fetches stored bans (so usually just GUILD_BAN_ADD data) std::vector GetBansInGuild(Snowflake guild_id); - void FetchGuildBan(Snowflake guild_id, Snowflake user_id, sigc::slot callback); - void FetchGuildBans(Snowflake guild_id, sigc::slot)> callback); + void FetchGuildBan(Snowflake guild_id, Snowflake user_id, const sigc::slot &callback); + void FetchGuildBans(Snowflake guild_id, const sigc::slot)> &callback); - void FetchInvite(std::string code, sigc::slot)> callback); - void FetchGuildInvites(Snowflake guild_id, sigc::slot)> callback); + void FetchInvite(const std::string &code, const sigc::slot)> &callback); + void FetchGuildInvites(Snowflake guild_id, const sigc::slot)> &callback); - void FetchAuditLog(Snowflake guild_id, sigc::slot callback); + void FetchAuditLog(Snowflake guild_id, const sigc::slot &callback); - void FetchGuildEmojis(Snowflake guild_id, sigc::slot)> callback); + void FetchGuildEmojis(Snowflake guild_id, const sigc::slot)> &callback); - void FetchUserProfile(Snowflake user_id, sigc::slot callback); - void FetchUserNote(Snowflake user_id, sigc::slot callback); - void SetUserNote(Snowflake user_id, std::string note); - void SetUserNote(Snowflake user_id, std::string note, sigc::slot callback); - void FetchUserRelationships(Snowflake user_id, sigc::slot)> callback); + void FetchUserProfile(Snowflake user_id, const sigc::slot &callback); + void FetchUserNote(Snowflake user_id, const sigc::slot &callback); + void SetUserNote(Snowflake user_id, std::string note, const sigc::slot &callback); + void FetchUserRelationships(Snowflake user_id, const sigc::slot)> &callback); - void FetchPinned(Snowflake id, sigc::slot, DiscordError code)> callback); + void FetchPinned(Snowflake id, const sigc::slot, DiscordError code)> &callback); - bool IsVerificationRequired(Snowflake guild_id); - void GetVerificationGateInfo(Snowflake guild_id, sigc::slot)> callback); - void AcceptVerificationGate(Snowflake guild_id, VerificationGateInfoObject info, sigc::slot callback); + bool IsVerificationRequired(Snowflake guild_id) const; + void GetVerificationGateInfo(Snowflake guild_id, const sigc::slot)> &callback); + void AcceptVerificationGate(Snowflake guild_id, VerificationGateInfoObject info, const sigc::slot &callback); - void UpdateToken(std::string token); - void SetUserAgent(std::string agent); + void UpdateToken(const std::string &token); + void SetUserAgent(const std::string &agent); bool IsChannelMuted(Snowflake id) const noexcept; bool IsGuildMuted(Snowflake id) const noexcept; @@ -233,8 +224,8 @@ private: std::vector m_decompress_buf; z_stream m_zstream; - std::string GetAPIURL(); - std::string GetGatewayURL(); + static std::string GetAPIURL(); + static std::string GetGatewayURL(); static DiscordError GetCodeFromResponse(const http::response_type &response); @@ -296,16 +287,14 @@ private: void HandleSocketOpen(); void HandleSocketClose(uint16_t code); - bool CheckCode(const http::response_type &r); - bool CheckCode(const http::response_type &r, int expected); + static bool CheckCode(const http::response_type &r); + static bool CheckCode(const http::response_type &r, int expected); void StoreMessageData(Message &msg); void HandleReadyReadState(const ReadyEventData &data); void HandleReadyGuildSettings(const ReadyEventData &data); - void HandleUserGuildSettingsUpdateForDMs(const UserGuildSettingsUpdateData &data); - std::string m_token; void AddUserToGuild(Snowflake user_id, Snowflake guild_id); diff --git a/src/discord/emoji.cpp b/src/discord/emoji.cpp index 1a97eb8..a21b9bd 100644 --- a/src/discord/emoji.cpp +++ b/src/discord/emoji.cpp @@ -16,7 +16,7 @@ void to_json(nlohmann::json &j, const EmojiData &m) { j["id"] = m.ID; else j["id"] = nullptr; - if (m.Name != "") + if (!m.Name.empty()) j["name"] = m.Name; else j["name"] = nullptr; diff --git a/src/discord/guild.cpp b/src/discord/guild.cpp index 966bd44..3995293 100644 --- a/src/discord/guild.cpp +++ b/src/discord/guild.cpp @@ -77,7 +77,7 @@ void GuildData::update_from_json(const nlohmann::json &j) { JS_RD("owner_id", OwnerID); std::string tmp; JS_RD("permissions", tmp); - if (tmp != "") + if (!tmp.empty()) Permissions = std::stoull(tmp); JS_RD("region", VoiceRegion); JS_RD("afk_channel_id", AFKChannelID); @@ -128,66 +128,17 @@ bool GuildData::HasFeature(const std::string &search_feature) { } bool GuildData::HasIcon() const { - return Icon != ""; + return !Icon.empty(); } bool GuildData::HasAnimatedIcon() const { return HasIcon() && Icon[0] == 'a' && Icon[1] == '_'; } -std::string GuildData::GetIconURL(std::string ext, std::string size) const { +std::string GuildData::GetIconURL(const std::string &ext, const std::string &size) const { return "https://cdn.discordapp.com/icons/" + std::to_string(ID) + "/" + Icon + "." + ext + "?size=" + size; } -std::vector GuildData::GetSortedChannels(Snowflake ignore) const { - std::vector ret; - - const auto &discord = Abaddon::Get().GetDiscordClient(); - auto channels = discord.GetChannelsInGuild(ID); - - std::unordered_map> category_to_channels; - std::map> position_to_categories; - std::map> orphan_channels; - for (const auto &channel_id : channels) { - const auto data = discord.GetChannel(channel_id); - if (!data->ParentID.has_value() && (data->Type == ChannelType::GUILD_TEXT || data->Type == ChannelType::GUILD_NEWS)) - orphan_channels[*data->Position].push_back(*data); - else if (data->ParentID.has_value() && (data->Type == ChannelType::GUILD_TEXT || data->Type == ChannelType::GUILD_NEWS)) - category_to_channels[*data->ParentID].push_back(*data); - else if (data->Type == ChannelType::GUILD_CATEGORY) - position_to_categories[*data->Position].push_back(*data); - } - - for (auto &[pos, channels] : orphan_channels) { - std::sort(channels.begin(), channels.end(), [&](const ChannelData &a, const ChannelData &b) -> bool { - return a.ID < b.ID; - }); - for (const auto &chan : channels) - ret.push_back(chan.ID); - } - - for (auto &[pos, categories] : position_to_categories) { - std::sort(categories.begin(), categories.end(), [&](const ChannelData &a, const ChannelData &b) -> bool { - return a.ID < b.ID; - }); - for (const auto &category : categories) { - ret.push_back(category.ID); - if (ignore == category.ID) continue; // stupid hack to save me some time - auto it = category_to_channels.find(category.ID); - if (it == category_to_channels.end()) continue; - auto &channels = it->second; - std::sort(channels.begin(), channels.end(), [&](const ChannelData &a, const ChannelData &b) -> bool { - return a.Position < b.Position; - }); - for (auto &channel : channels) { - ret.push_back(channel.ID); - } - } - } - - return ret; -} - void from_json(const nlohmann::json &j, GuildApplicationData &m) { JS_D("user_id", m.UserID); JS_D("guild_id", m.GuildID); diff --git a/src/discord/guild.hpp b/src/discord/guild.hpp index 51b5a01..3370fa8 100644 --- a/src/discord/guild.hpp +++ b/src/discord/guild.hpp @@ -94,6 +94,5 @@ struct GuildData { bool HasFeature(const std::string &feature); bool HasIcon() const; bool HasAnimatedIcon() const; - std::string GetIconURL(std::string ext = "png", std::string size = "32") const; - std::vector GetSortedChannels(Snowflake ignore = Snowflake::Invalid) const; + std::string GetIconURL(const std::string &ext = "png", const std::string &size = "32") const; }; diff --git a/src/discord/httpclient.cpp b/src/discord/httpclient.cpp index 05474df..f5b640c 100644 --- a/src/discord/httpclient.cpp +++ b/src/discord/httpclient.cpp @@ -1,5 +1,7 @@ #include "httpclient.hpp" +#include + //#define USE_LOCAL_PROXY HTTPClient::HTTPClient() { m_dispatcher.connect(sigc::mem_fun(*this, &HTTPClient::RunCallbacks)); @@ -10,19 +12,19 @@ void HTTPClient::SetBase(const std::string &url) { } void HTTPClient::SetUserAgent(std::string agent) { - m_agent = agent; + m_agent = std::move(agent); } void HTTPClient::SetAuth(std::string auth) { - m_authorization = auth; + m_authorization = std::move(auth); } -void HTTPClient::MakeDELETE(const std::string &path, std::function cb) { +void HTTPClient::MakeDELETE(const std::string &path, const std::function &cb) { printf("DELETE %s\n", path.c_str()); m_futures.push_back(std::async(std::launch::async, [this, path, cb] { http::request req(http::REQUEST_DELETE, m_api_base + path); req.set_header("Authorization", m_authorization); - req.set_user_agent(m_agent != "" ? m_agent : "Abaddon"); + req.set_user_agent(!m_agent.empty() ? m_agent : "Abaddon"); #ifdef USE_LOCAL_PROXY req.set_proxy("http://127.0.0.1:8888"); req.set_verify_ssl(false); @@ -34,13 +36,13 @@ void HTTPClient::MakeDELETE(const std::string &path, std::function cb) { +void HTTPClient::MakePATCH(const std::string &path, const std::string &payload, const std::function &cb) { printf("PATCH %s\n", path.c_str()); m_futures.push_back(std::async(std::launch::async, [this, path, cb, payload] { http::request req(http::REQUEST_PATCH, m_api_base + path); req.set_header("Authorization", m_authorization); req.set_header("Content-Type", "application/json"); - req.set_user_agent(m_agent != "" ? m_agent : "Abaddon"); + req.set_user_agent(!m_agent.empty() ? m_agent : "Abaddon"); req.set_body(payload); #ifdef USE_LOCAL_PROXY req.set_proxy("http://127.0.0.1:8888"); @@ -53,13 +55,13 @@ void HTTPClient::MakePATCH(const std::string &path, const std::string &payload, })); } -void HTTPClient::MakePOST(const std::string &path, const std::string &payload, std::function cb) { +void HTTPClient::MakePOST(const std::string &path, const std::string &payload, const std::function &cb) { printf("POST %s\n", path.c_str()); m_futures.push_back(std::async(std::launch::async, [this, path, cb, payload] { http::request req(http::REQUEST_POST, m_api_base + path); req.set_header("Authorization", m_authorization); req.set_header("Content-Type", "application/json"); - req.set_user_agent(m_agent != "" ? m_agent : "Abaddon"); + req.set_user_agent(!m_agent.empty() ? m_agent : "Abaddon"); req.set_body(payload); #ifdef USE_LOCAL_PROXY req.set_proxy("http://127.0.0.1:8888"); @@ -72,14 +74,14 @@ void HTTPClient::MakePOST(const std::string &path, const std::string &payload, s })); } -void HTTPClient::MakePUT(const std::string &path, const std::string &payload, std::function cb) { +void HTTPClient::MakePUT(const std::string &path, const std::string &payload, const std::function &cb) { printf("PUT %s\n", path.c_str()); m_futures.push_back(std::async(std::launch::async, [this, path, cb, payload] { http::request req(http::REQUEST_PUT, m_api_base + path); req.set_header("Authorization", m_authorization); - if (payload != "") + if (!payload.empty()) req.set_header("Content-Type", "application/json"); - req.set_user_agent(m_agent != "" ? m_agent : "Abaddon"); + req.set_user_agent(!m_agent.empty() ? m_agent : "Abaddon"); req.set_body(payload); #ifdef USE_LOCAL_PROXY req.set_proxy("http://127.0.0.1:8888"); @@ -92,13 +94,13 @@ void HTTPClient::MakePUT(const std::string &path, const std::string &payload, st })); } -void HTTPClient::MakeGET(const std::string &path, std::function cb) { +void HTTPClient::MakeGET(const std::string &path, const std::function &cb) { printf("GET %s\n", path.c_str()); m_futures.push_back(std::async(std::launch::async, [this, path, cb] { http::request req(http::REQUEST_GET, m_api_base + path); req.set_header("Authorization", m_authorization); req.set_header("Content-Type", "application/json"); - req.set_user_agent(m_agent != "" ? m_agent : "Abaddon"); + req.set_user_agent(!m_agent.empty() ? m_agent : "Abaddon"); #ifdef USE_LOCAL_PROXY req.set_proxy("http://127.0.0.1:8888"); req.set_verify_ssl(false); @@ -126,11 +128,11 @@ void HTTPClient::RunCallbacks() { m_mutex.unlock(); } -void HTTPClient::OnResponse(const http::response_type &r, std::function cb) { +void HTTPClient::OnResponse(const http::response_type &r, const std::function &cb) { CleanupFutures(); try { m_mutex.lock(); - m_queue.push([this, r, cb] { cb(r); }); + m_queue.push([r, cb] { cb(r); }); m_dispatcher.emit(); m_mutex.unlock(); } catch (const std::exception &e) { diff --git a/src/discord/httpclient.hpp b/src/discord/httpclient.hpp index da8be37..47b8a4d 100644 --- a/src/discord/httpclient.hpp +++ b/src/discord/httpclient.hpp @@ -17,14 +17,14 @@ public: void SetUserAgent(std::string agent); void SetAuth(std::string auth); - void MakeDELETE(const std::string &path, std::function cb); - void MakeGET(const std::string &path, std::function cb); - void MakePATCH(const std::string &path, const std::string &payload, std::function cb); - void MakePOST(const std::string &path, const std::string &payload, std::function cb); - void MakePUT(const std::string &path, const std::string &payload, std::function cb); + void MakeDELETE(const std::string &path, const std::function &cb); + void MakeGET(const std::string &path, const std::function &cb); + void MakePATCH(const std::string &path, const std::string &payload, const std::function &cb); + void MakePOST(const std::string &path, const std::string &payload, const std::function &cb); + void MakePUT(const std::string &path, const std::string &payload, const std::function &cb); private: - void OnResponse(const http::response_type &r, std::function cb); + void OnResponse(const http::response_type &r, const std::function &cb); void CleanupFutures(); mutable std::mutex m_mutex; diff --git a/src/discord/json.hpp b/src/discord/json.hpp index 837080b..3c6968d 100644 --- a/src/discord/json.hpp +++ b/src/discord/json.hpp @@ -141,8 +141,8 @@ inline void json_update_optional_nullable_default(const ::nlohmann::json &j, con } while (0) // set a json value from a std::optional only if it has a value -#define JS_IF(k, v) \ - do { \ - if (v.has_value()) \ - j[k] = *v; \ +#define JS_IF(k, v) \ + do { \ + if ((v).has_value()) \ + j[k] = *(v); \ } while (0) diff --git a/src/discord/member.cpp b/src/discord/member.cpp index 29c4fae..e424491 100644 --- a/src/discord/member.cpp +++ b/src/discord/member.cpp @@ -19,7 +19,7 @@ std::vector GuildMember::GetSortedRoles() const { for (const auto role_id : Roles) { const auto role = Abaddon::Get().GetDiscordClient().GetRole(role_id); if (!role.has_value()) continue; - roles.push_back(std::move(*role)); + roles.push_back(*role); } std::sort(roles.begin(), roles.end(), [](const RoleData &a, const RoleData &b) { diff --git a/src/discord/member.hpp b/src/discord/member.hpp index e17da05..e5d49f4 100644 --- a/src/discord/member.hpp +++ b/src/discord/member.hpp @@ -20,7 +20,7 @@ struct GuildMember { // undocuemtned moment !!!1 std::optional Avatar; - std::vector GetSortedRoles() const; + [[nodiscard]] std::vector GetSortedRoles() const; void update_from_json(const nlohmann::json &j); friend void from_json(const nlohmann::json &j, GuildMember &m); diff --git a/src/discord/message.cpp b/src/discord/message.cpp index 93d57c2..fb8a0ad 100644 --- a/src/discord/message.cpp +++ b/src/discord/message.cpp @@ -176,7 +176,7 @@ void to_json(nlohmann::json &j, const MessageApplicationData &m) { j["id"] = m.ID; JS_IF("cover_image", m.CoverImage); j["description"] = m.Description; - if (m.Icon == "") + if (m.Icon.empty()) j["icon"] = nullptr; else j["icon"] = m.Icon; @@ -230,7 +230,7 @@ void Message::from_json_edited(const nlohmann::json &j) { JS_O("content", Content); JS_O("timestamp", Timestamp); JS_ON("edited_timestamp", EditedTimestamp); - if (EditedTimestamp.size() > 0) + if (!EditedTimestamp.empty()) SetEdited(); JS_O("tts", IsTTS); JS_O("mention_everyone", DoesMentionEveryone); diff --git a/src/discord/message.hpp b/src/discord/message.hpp index 244b572..4e332b6 100644 --- a/src/discord/message.hpp +++ b/src/discord/message.hpp @@ -209,10 +209,10 @@ struct Message { void SetDeleted(); void SetEdited(); - bool IsDeleted() const; - bool IsEdited() const; + [[nodiscard]] bool IsDeleted() const; + [[nodiscard]] bool IsEdited() const; - bool DoesMention(Snowflake id) const noexcept; + [[nodiscard]] bool DoesMention(Snowflake id) const noexcept; private: bool m_deleted = false; diff --git a/src/discord/objects.cpp b/src/discord/objects.cpp index 3c9f770..6deaca0 100644 --- a/src/discord/objects.cpp +++ b/src/discord/objects.cpp @@ -251,7 +251,7 @@ void to_json(nlohmann::json &j, const IdentifyProperties &m) { j["referring_domain_current"] = m.ReferringDomainCurrent; j["release_channel"] = m.ReleaseChannel; j["client_build_number"] = m.ClientBuildNumber; - if (m.ClientEventSource == "") + if (m.ClientEventSource.empty()) j["client_event_source"] = nullptr; else j["client_event_source"] = m.ClientEventSource; @@ -290,7 +290,7 @@ void to_json(nlohmann::json &j, const CreateMessageObject &m) { } void to_json(nlohmann::json &j, const MessageEditObject &m) { - if (m.Content.size() > 0) + if (!m.Content.empty()) j["content"] = m.Content; // todo EmbedData to_json diff --git a/src/discord/objects.hpp b/src/discord/objects.hpp index 27542cc..db5fd76 100644 --- a/src/discord/objects.hpp +++ b/src/discord/objects.hpp @@ -197,7 +197,7 @@ struct GuildMemberListUpdateMessage { std::string HoistedRole; // null bool IsDefeaned; - GuildMember GetAsMemberData() const; + [[nodiscard]] GuildMember GetAsMemberData() const; friend void from_json(const nlohmann::json &j, MemberItem &m); diff --git a/src/discord/role.hpp b/src/discord/role.hpp index a526f4e..8df3a04 100644 --- a/src/discord/role.hpp +++ b/src/discord/role.hpp @@ -16,8 +16,8 @@ struct RoleData { bool IsManaged; bool IsMentionable; - bool HasColor() const noexcept; - Glib::ustring GetEscapedName() const; + [[nodiscard]] bool HasColor() const noexcept; + [[nodiscard]] Glib::ustring GetEscapedName() const; friend void from_json(const nlohmann::json &j, RoleData &m); }; diff --git a/src/discord/snowflake.cpp b/src/discord/snowflake.cpp index 3624c01..856e2f7 100644 --- a/src/discord/snowflake.cpp +++ b/src/discord/snowflake.cpp @@ -2,7 +2,6 @@ #include "util.hpp" #include #include -#include #include constexpr static uint64_t DiscordEpochSeconds = 1420070400; @@ -26,7 +25,7 @@ Snowflake::Snowflake(const Glib::ustring &str) { m_num = std::strtoull(str.c_str(), nullptr, 10); else m_num = Invalid; -}; +} Snowflake Snowflake::FromNow() { using namespace std::chrono; @@ -57,7 +56,7 @@ bool Snowflake::IsValid() const { Glib::ustring Snowflake::GetLocalTimestamp() const { const time_t secs_since_epoch = (m_num / SecondsInterval) + DiscordEpochSeconds; const std::tm tm = *localtime(&secs_since_epoch); - std::array tmp; + std::array tmp {}; std::strftime(tmp.data(), sizeof(tmp), "%X %x", &tm); return tmp.data(); } diff --git a/src/discord/snowflake.hpp b/src/discord/snowflake.hpp index e83317a..a5c0e6c 100644 --- a/src/discord/snowflake.hpp +++ b/src/discord/snowflake.hpp @@ -12,8 +12,8 @@ struct Snowflake { static Snowflake FromNow(); // not thread safe static Snowflake FromISO8601(std::string_view ts); - bool IsValid() const; - Glib::ustring GetLocalTimestamp() const; + [[nodiscard]] bool IsValid() const; + [[nodiscard]] Glib::ustring GetLocalTimestamp() const; bool operator==(const Snowflake &s) const noexcept { return m_num == s.m_num; diff --git a/src/discord/sticker.cpp b/src/discord/sticker.cpp index b92d031..0b163fc 100644 --- a/src/discord/sticker.cpp +++ b/src/discord/sticker.cpp @@ -22,15 +22,6 @@ void from_json(const nlohmann::json &j, StickerData &m) { JS_D("format_type", m.FormatType); } -std::string StickerData::GetURL() const { - if (!AssetHash.has_value()) return ""; - if (FormatType == StickerFormatType::PNG || FormatType == StickerFormatType::APNG) - return "https://media.discordapp.net/stickers/" + std::to_string(ID) + "/" + *AssetHash + ".png?size=256"; - else if (FormatType == StickerFormatType::LOTTIE) - return "https://media.discordapp.net/stickers/" + std::to_string(ID) + "/" + *AssetHash + ".json"; - return ""; -} - void to_json(nlohmann::json &j, const StickerItem &m) { j["id"] = m.ID; j["name"] = m.Name; diff --git a/src/discord/sticker.hpp b/src/discord/sticker.hpp index d23fe7b..77d4d83 100644 --- a/src/discord/sticker.hpp +++ b/src/discord/sticker.hpp @@ -24,8 +24,6 @@ struct StickerData { friend void to_json(nlohmann::json &j, const StickerData &m); friend void from_json(const nlohmann::json &j, StickerData &m); - - std::string GetURL() const; }; struct StickerItem { @@ -36,5 +34,5 @@ struct StickerItem { friend void to_json(nlohmann::json &j, const StickerItem &m); friend void from_json(const nlohmann::json &j, StickerItem &m); - std::string GetURL() const; + [[nodiscard]] std::string GetURL() const; }; diff --git a/src/discord/store.cpp b/src/discord/store.cpp index ede6364..71fdfcc 100644 --- a/src/discord/store.cpp +++ b/src/discord/store.cpp @@ -1001,7 +1001,7 @@ std::optional Store::GetRole(Snowflake id) const { return role; } -RoleData Store::GetRoleBound(std::unique_ptr &s) const { +RoleData Store::GetRoleBound(std::unique_ptr &s) { RoleData r; s->Get(0, r.ID); @@ -2249,11 +2249,11 @@ int Store::Statement::Bind(int index, Snowflake id) { int Store::Statement::Bind(int index, const char *str, size_t len) { if (len == -1) len = strlen(str); - return m_db->SetError(sqlite3_bind_blob(m_stmt, index, str, len, SQLITE_TRANSIENT)); + return m_db->SetError(sqlite3_bind_blob(m_stmt, index, str, static_cast(len), SQLITE_TRANSIENT)); } int Store::Statement::Bind(int index, const std::string &str) { - return m_db->SetError(sqlite3_bind_blob(m_stmt, index, str.c_str(), str.size(), SQLITE_TRANSIENT)); + return m_db->SetError(sqlite3_bind_blob(m_stmt, index, str.c_str(), static_cast(str.size()), SQLITE_TRANSIENT)); } int Store::Statement::Bind(int index) { diff --git a/src/discord/store.hpp b/src/discord/store.hpp index a1e5f81..4cf5e14 100644 --- a/src/discord/store.hpp +++ b/src/discord/store.hpp @@ -101,7 +101,7 @@ private: ~Statement(); Statement &operator=(Statement &other) = delete; - bool OK() const; + [[nodiscard]] bool OK() const; int Bind(int index, Snowflake id); int Bind(int index, const char *str, size_t len = -1); @@ -224,7 +224,7 @@ private: *first++ = id.get(); } - bool IsNull(int index) const; + [[nodiscard]] bool IsNull(int index) const; int Step(); bool Insert(); bool FetchOne(); @@ -238,7 +238,7 @@ private: }; Message GetMessageBound(std::unique_ptr &stmt) const; - RoleData GetRoleBound(std::unique_ptr &stmt) const; + static RoleData GetRoleBound(std::unique_ptr &stmt); void SetMessageInteractionPair(Snowflake message_id, const MessageInteractionData &interaction); diff --git a/src/discord/user.cpp b/src/discord/user.cpp index c2e6069..4393992 100644 --- a/src/discord/user.cpp +++ b/src/discord/user.cpp @@ -29,7 +29,7 @@ bool UserData::HasAnimatedAvatar(const std::optional &guild_id) const return HasAnimatedAvatar(); } -std::string UserData::GetAvatarURL(Snowflake guild_id, std::string ext, std::string size) const { +std::string UserData::GetAvatarURL(Snowflake guild_id, const std::string &ext, std::string size) const { const auto member = Abaddon::Get().GetDiscordClient().GetMember(ID, guild_id); if (member.has_value() && member->Avatar.has_value()) { if (ext == "gif" && !(member->Avatar.value()[0] == 'a' && member->Avatar.value()[1] == '_')) @@ -43,14 +43,14 @@ std::string UserData::GetAvatarURL(Snowflake guild_id, std::string ext, std::str } } -std::string UserData::GetAvatarURL(const std::optional &guild_id, std::string ext, std::string size) const { +std::string UserData::GetAvatarURL(const std::optional &guild_id, const std::string &ext, std::string size) const { if (guild_id.has_value()) return GetAvatarURL(*guild_id, ext, size); else return GetAvatarURL(ext, size); } -std::string UserData::GetAvatarURL(std::string ext, std::string size) const { +std::string UserData::GetAvatarURL(const std::string &ext, std::string size) const { if (HasAvatar()) return "https://cdn.discordapp.com/avatars/" + std::to_string(ID) + "/" + Avatar + "." + ext + "?size=" + size; else @@ -107,7 +107,7 @@ void to_json(nlohmann::json &j, const UserData &m) { j["id"] = m.ID; j["username"] = m.Username; j["discriminator"] = m.Discriminator; - if (m.Avatar == "") + if (m.Avatar.empty()) j["avatar"] = nullptr; else j["avatar"] = m.Avatar; diff --git a/src/discord/user.hpp b/src/discord/user.hpp index c058ea1..083f5c4 100644 --- a/src/discord/user.hpp +++ b/src/discord/user.hpp @@ -60,22 +60,22 @@ struct UserData { friend void to_json(nlohmann::json &j, const UserData &m); void update_from_json(const nlohmann::json &j); - bool IsDeleted() const; - bool HasAvatar() const; - bool HasAnimatedAvatar() const noexcept; - bool HasAnimatedAvatar(Snowflake guild_id) const; - bool HasAnimatedAvatar(const std::optional &guild_id) const; - std::string GetAvatarURL(Snowflake guild_id, std::string ext = "png", std::string size = "32") const; - std::string GetAvatarURL(const std::optional &guild_id, std::string ext = "png", std::string size = "32") const; - std::string GetAvatarURL(std::string ext = "png", std::string size = "32") const; - std::string GetDefaultAvatarURL() const; - Snowflake GetHoistedRole(Snowflake guild_id, bool with_color = false) const; - std::string GetMention() const; - std::string GetEscapedName() const; - std::string GetEscapedBoldName() const; - std::string GetEscapedString() const; + [[nodiscard]] bool IsDeleted() const; + [[nodiscard]] bool HasAvatar() const; + [[nodiscard]] bool HasAnimatedAvatar() const noexcept; + [[nodiscard]] bool HasAnimatedAvatar(Snowflake guild_id) const; + [[nodiscard]] bool HasAnimatedAvatar(const std::optional &guild_id) const; + [[nodiscard]] std::string GetAvatarURL(Snowflake guild_id, const std::string &ext = "png", std::string size = "32") const; + [[nodiscard]] std::string GetAvatarURL(const std::optional &guild_id, const std::string &ext = "png", std::string size = "32") const; + [[nodiscard]] std::string GetAvatarURL(const std::string &ext = "png", std::string size = "32") const; + [[nodiscard]] std::string GetDefaultAvatarURL() const; + [[nodiscard]] Snowflake GetHoistedRole(Snowflake guild_id, bool with_color = false) const; + [[nodiscard]] std::string GetMention() const; + [[nodiscard]] std::string GetEscapedName() const; + [[nodiscard]] std::string GetEscapedBoldName() const; + [[nodiscard]] std::string GetEscapedString() const; template - inline std::string GetEscapedBoldString() const { + [[nodiscard]] inline std::string GetEscapedBoldString() const { if constexpr (with_at) return "@" + Glib::Markup::escape_text(Username) + "#" + Discriminator; else diff --git a/src/discord/websocket.cpp b/src/discord/websocket.cpp index c7e43e9..46a4b90 100644 --- a/src/discord/websocket.cpp +++ b/src/discord/websocket.cpp @@ -1,18 +1,18 @@ #include "websocket.hpp" -#include +#include -Websocket::Websocket() {} +Websocket::Websocket() = default; -void Websocket::StartConnection(std::string url) { +void Websocket::StartConnection(const std::string &url) { m_websocket.disableAutomaticReconnection(); m_websocket.setUrl(url); - m_websocket.setOnMessageCallback(std::bind(&Websocket::OnMessage, this, std::placeholders::_1)); + m_websocket.setOnMessageCallback([this](auto &&msg) { OnMessage(std::forward(msg)); }); m_websocket.setExtraHeaders(ix::WebSocketHttpHeaders { { "User-Agent", m_agent } }); // idk if this actually works m_websocket.start(); } void Websocket::SetUserAgent(std::string agent) { - m_agent = agent; + m_agent = std::move(agent); } bool Websocket::GetPrintMessages() const noexcept { @@ -31,11 +31,6 @@ void Websocket::Stop(uint16_t code) { m_websocket.stop(code); } -bool Websocket::IsOpen() const { - auto state = m_websocket.getReadyState(); - return state == ix::ReadyState::Open; -} - void Websocket::Send(const std::string &str) { if (m_print_messages) printf("sending %s\n", str.c_str()); diff --git a/src/discord/websocket.hpp b/src/discord/websocket.hpp index 26cd5d4..ca7dcc6 100644 --- a/src/discord/websocket.hpp +++ b/src/discord/websocket.hpp @@ -9,7 +9,7 @@ class Websocket { public: Websocket(); - void StartConnection(std::string url); + void StartConnection(const std::string &url); void SetUserAgent(std::string agent); @@ -20,7 +20,6 @@ public: void Send(const nlohmann::json &j); void Stop(); void Stop(uint16_t code); - bool IsOpen() const; private: void OnMessage(const ix::WebSocketMessagePtr &msg); diff --git a/src/emojis.cpp b/src/emojis.cpp index dd7193a..25516d3 100644 --- a/src/emojis.cpp +++ b/src/emojis.cpp @@ -1,8 +1,9 @@ #include "emojis.hpp" #include +#include EmojiResource::EmojiResource(std::string filepath) - : m_filepath(filepath) {} + : m_filepath(std::move(filepath)) {} bool EmojiResource::Load() { m_fp = std::fopen(m_filepath.c_str(), "rb"); @@ -31,7 +32,7 @@ bool EmojiResource::Load() { std::fread(&surrogates_count, 4, 1, m_fp); std::string surrogates(surrogates_count, '\0'); std::fread(surrogates.data(), surrogates_count, 1, m_fp); - m_patterns.push_back(surrogates); + m_patterns.emplace_back(surrogates); int data_size, data_offset; std::fread(&data_size, 4, 1, m_fp); @@ -52,7 +53,7 @@ bool EmojiResource::Load() { Glib::RefPtr EmojiResource::GetPixBuf(const Glib::ustring &pattern) { const auto it = m_index.find(pattern); - if (it == m_index.end()) return Glib::RefPtr(); + if (it == m_index.end()) return {}; const int pos = it->second.first; const int len = it->second.second; std::fseek(m_fp, pos, SEEK_SET); @@ -86,17 +87,17 @@ void EmojiResource::ReplaceEmojis(Glib::RefPtr buf, int size) { else break; } - searchpos = r + pattern.size(); + searchpos = static_cast(r + pattern.size()); - const auto start_it = buf->get_iter_at_offset(r); - const auto end_it = buf->get_iter_at_offset(r + pattern.size()); + const auto start_it = buf->get_iter_at_offset(static_cast(r)); + const auto end_it = buf->get_iter_at_offset(static_cast(r + pattern.size())); auto it = buf->erase(start_it, end_it); buf->insert_pixbuf(it, pixbuf); - int alen = text.size(); + int alen = static_cast(text.size()); text = get_text(); - int blen = text.size(); + int blen = static_cast(text.size()); searchpos -= (alen - blen); } } @@ -109,10 +110,6 @@ std::string EmojiResource::GetShortCodeForPattern(const Glib::ustring &pattern) return ""; } -const std::vector &EmojiResource::GetPatterns() const { - return m_patterns; -} - const std::map &EmojiResource::GetShortCodes() const { return m_shortcode_index; } diff --git a/src/emojis.hpp b/src/emojis.hpp index 61b8a71..71d665d 100644 --- a/src/emojis.hpp +++ b/src/emojis.hpp @@ -12,7 +12,6 @@ public: EmojiResource(std::string filepath); bool Load(); Glib::RefPtr GetPixBuf(const Glib::ustring &pattern); - const std::vector &GetPatterns() const; const std::map &GetShortCodes() const; void ReplaceEmojis(Glib::RefPtr buf, int size = 24); std::string GetShortCodeForPattern(const Glib::ustring &pattern); diff --git a/src/filecache.cpp b/src/filecache.cpp index e04fbcb..3943e72 100644 --- a/src/filecache.cpp +++ b/src/filecache.cpp @@ -1,10 +1,12 @@ #include "abaddon.hpp" #include "filecache.hpp" + +#include #include "MurmurHash3.h" -std::string GetCachedName(std::string str) { +std::string GetCachedName(const std::string &str) { uint32_t out; - MurmurHash3_x86_32(str.c_str(), str.size(), 0, &out); + MurmurHash3_x86_32(str.c_str(), static_cast(str.size()), 0, &out); return std::to_string(out); } @@ -35,15 +37,15 @@ void Cache::ClearCache() { std::filesystem::remove_all(path); } -void Cache::RespondFromPath(std::filesystem::path path, callback_type cb) { +void Cache::RespondFromPath(const std::filesystem::path &path, const callback_type &cb) { cb(path.string()); } -void Cache::GetFileFromURL(std::string url, callback_type cb) { +void Cache::GetFileFromURL(const std::string &url, const callback_type &cb) { auto cache_path = m_tmp_path / GetCachedName(url); if (std::filesystem::exists(cache_path)) { m_mutex.lock(); - m_futures.push_back(std::async(std::launch::async, [this, cache_path, cb]() { RespondFromPath(cache_path, cb); })); + m_futures.push_back(std::async(std::launch::async, [cache_path, cb]() { RespondFromPath(cache_path, cb); })); m_mutex.unlock(); return; } @@ -58,7 +60,7 @@ void Cache::GetFileFromURL(std::string url, callback_type cb) { } } -std::string Cache::GetPathIfCached(std::string url) { +std::string Cache::GetPathIfCached(const std::string &url) { auto cache_path = m_tmp_path / GetCachedName(url); if (std::filesystem::exists(cache_path)) { return cache_path.string(); @@ -94,13 +96,13 @@ void Cache::OnResponse(const std::string &url) { void Cache::OnFetchComplete(const std::string &url) { m_mutex.lock(); - m_futures.push_back(std::async(std::launch::async, std::bind(&Cache::OnResponse, this, url))); + m_futures.push_back(std::async(std::launch::async, [this, url] { OnResponse(url); })); m_mutex.unlock(); } FileCacheWorkerThread::FileCacheWorkerThread() { m_multi_handle = curl_multi_init(); - m_thread = std::thread(std::bind(&FileCacheWorkerThread::loop, this)); + m_thread = std::thread([this] { loop(); }); } FileCacheWorkerThread::~FileCacheWorkerThread() { @@ -116,7 +118,7 @@ void FileCacheWorkerThread::set_file_path(const std::filesystem::path &path) { void FileCacheWorkerThread::add_image(const std::string &string, callback_type callback) { m_queue_mutex.lock(); - m_queue.push({ string, callback }); + m_queue.push({ string, std::move(callback) }); m_cv.notify_one(); m_queue_mutex.unlock(); } @@ -130,15 +132,14 @@ void FileCacheWorkerThread::stop() { } void FileCacheWorkerThread::loop() { - timeval timeout; + timeval timeout {}; timeout.tv_sec = 1; timeout.tv_usec = 0; while (!m_stop) { - if (m_handles.size() == 0) { + if (m_handles.empty()) { std::unique_lock lock(m_queue_mutex); - int s = m_queue.size(); - if (s == 0) + if (m_queue.empty()) m_cv.wait(lock); } @@ -146,7 +147,7 @@ void FileCacheWorkerThread::loop() { if (m_handles.size() < concurrency) { std::optional entry; m_queue_mutex.lock(); - if (m_queue.size() > 0) { + if (!m_queue.empty()) { entry = std::move(m_queue.front()); m_queue.pop(); } diff --git a/src/filecache.hpp b/src/filecache.hpp index d25fdb3..5214fb2 100644 --- a/src/filecache.hpp +++ b/src/filecache.hpp @@ -59,13 +59,13 @@ public: ~Cache(); using callback_type = std::function; - void GetFileFromURL(std::string url, callback_type cb); - std::string GetPathIfCached(std::string url); + void GetFileFromURL(const std::string &url, const callback_type &cb); + std::string GetPathIfCached(const std::string &url); void ClearCache(); private: void CleanupFutures(); - void RespondFromPath(std::filesystem::path path, callback_type cb); + static void RespondFromPath(const std::filesystem::path &path, const callback_type &cb); void OnResponse(const std::string &url); void OnFetchComplete(const std::string &url); diff --git a/src/http.cpp b/src/http.cpp index 877970a..7338d39 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -1,8 +1,10 @@ #include "http.hpp" +#include + namespace http { -request::request(EMethod method, const std::string &url) - : m_url(url) { +request::request(EMethod method, std::string url) + : m_url(std::move(url)) { switch (method) { case REQUEST_GET: m_method = "GET"; diff --git a/src/http.hpp b/src/http.hpp index c2e2765..4220a6e 100644 --- a/src/http.hpp +++ b/src/http.hpp @@ -97,7 +97,7 @@ struct response { }; struct request { - request(EMethod method, const std::string &url); + request(EMethod method, std::string url); ~request(); void set_verify_ssl(bool verify); diff --git a/src/imgmanager.cpp b/src/imgmanager.cpp index b97083c..dbfa5fa 100644 --- a/src/imgmanager.cpp +++ b/src/imgmanager.cpp @@ -1,4 +1,6 @@ #include "imgmanager.hpp" + +#include #include "util.hpp" #include "abaddon.hpp" @@ -6,17 +8,13 @@ ImageManager::ImageManager() { m_cb_dispatcher.connect(sigc::mem_fun(*this, &ImageManager::RunCallbacks)); } -Cache &ImageManager::GetCache() { - return m_cache; -} - void ImageManager::ClearCache() { m_cache.ClearCache(); } Glib::RefPtr ImageManager::ReadFileToPixbuf(std::string path) { - const auto &data = ReadWholeFile(path); - if (data.size() == 0) return Glib::RefPtr(nullptr); + const auto &data = ReadWholeFile(std::move(path)); + if (data.empty()) return Glib::RefPtr(nullptr); auto loader = Gdk::PixbufLoader::create(); loader->signal_size_prepared().connect([&loader](int w, int h) { int cw, ch; @@ -29,8 +27,8 @@ Glib::RefPtr ImageManager::ReadFileToPixbuf(std::string path) { } Glib::RefPtr ImageManager::ReadFileToPixbufAnimation(std::string path, int w, int h) { - const auto &data = ReadWholeFile(path); - if (data.size() == 0) return Glib::RefPtr(nullptr); + const auto &data = ReadWholeFile(std::move(path)); + if (data.empty()) return Glib::RefPtr(nullptr); auto loader = Gdk::PixbufLoader::create(); loader->signal_size_prepared().connect([&loader, w, h](int, int) { loader->set_size(w, h); @@ -40,10 +38,10 @@ Glib::RefPtr ImageManager::ReadFileToPixbufAnimation(std:: return loader->get_animation(); } -void ImageManager::LoadFromURL(std::string url, callback_type cb) { +void ImageManager::LoadFromURL(const std::string &url, const callback_type &cb) { sigc::signal)> signal; signal.connect(cb); - m_cache.GetFileFromURL(url, [this, url, signal](std::string path) { + m_cache.GetFileFromURL(url, [this, url, signal](const std::string &path) { try { auto buf = ReadFileToPixbuf(path); if (!buf) @@ -60,10 +58,10 @@ void ImageManager::LoadFromURL(std::string url, callback_type cb) { }); } -void ImageManager::LoadAnimationFromURL(std::string url, int w, int h, callback_anim_type cb) { +void ImageManager::LoadAnimationFromURL(const std::string &url, int w, int h, const callback_anim_type &cb) { sigc::signal)> signal; signal.connect(cb); - m_cache.GetFileFromURL(url, [this, url, signal, w, h](std::string path) { + m_cache.GetFileFromURL(url, [this, url, signal, w, h](const std::string &path) { try { auto buf = ReadFileToPixbufAnimation(path, w, h); if (!buf) @@ -80,7 +78,7 @@ void ImageManager::LoadAnimationFromURL(std::string url, int w, int h, callback_ }); } -void ImageManager::Prefetch(std::string url) { +void ImageManager::Prefetch(const std::string &url) { m_cache.GetFileFromURL(url, [](const auto &) {}); } @@ -91,22 +89,6 @@ void ImageManager::RunCallbacks() { m_cb_mutex.unlock(); } -Glib::RefPtr ImageManager::GetFromURLIfCached(std::string url) { - std::string path = m_cache.GetPathIfCached(url); - if (path != "") - return ReadFileToPixbuf(path); - - return Glib::RefPtr(nullptr); -} - -Glib::RefPtr ImageManager::GetAnimationFromURLIfCached(std::string url, int w, int h) { - std::string path = m_cache.GetPathIfCached(url); - if (path != "") - return ReadFileToPixbufAnimation(path, w, h); - - return Glib::RefPtr(nullptr); -} - Glib::RefPtr ImageManager::GetPlaceholder(int size) { std::string name = "/placeholder" + std::to_string(size); if (m_pixs.find(name) != m_pixs.end()) diff --git a/src/imgmanager.hpp b/src/imgmanager.hpp index eb8a590..63c604e 100644 --- a/src/imgmanager.hpp +++ b/src/imgmanager.hpp @@ -13,19 +13,16 @@ public: using callback_anim_type = sigc::slot)>; using callback_type = sigc::slot)>; - Cache &GetCache(); void ClearCache(); - void LoadFromURL(std::string url, callback_type cb); + void LoadFromURL(const std::string &url, const callback_type &cb); // animations need dimensions before loading since there is no (easy) way to scale a PixbufAnimation - void LoadAnimationFromURL(std::string url, int w, int h, callback_anim_type cb); - void Prefetch(std::string url); - Glib::RefPtr GetFromURLIfCached(std::string url); - Glib::RefPtr GetAnimationFromURLIfCached(std::string url, int w, int h); + void LoadAnimationFromURL(const std::string &url, int w, int h, const callback_anim_type &cb); + void Prefetch(const std::string &url); Glib::RefPtr GetPlaceholder(int size); private: - Glib::RefPtr ReadFileToPixbuf(std::string path); - Glib::RefPtr ReadFileToPixbufAnimation(std::string path, int w, int h); + static Glib::RefPtr ReadFileToPixbuf(std::string path); + static Glib::RefPtr ReadFileToPixbufAnimation(std::string path, int w, int h); mutable std::mutex m_load_mutex; void RunCallbacks(); diff --git a/src/settings.hpp b/src/settings.hpp index 52b20b9..3c9aebb 100644 --- a/src/settings.hpp +++ b/src/settings.hpp @@ -46,7 +46,7 @@ public: SettingsManager(const std::string &filename); void Close(); - bool IsValid() const; + [[nodiscard]] bool IsValid() const; Settings &GetSettings(); private: diff --git a/src/util.cpp b/src/util.cpp index 0ebe73e..ea38411 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,33 +1,15 @@ #include "util.hpp" #include #include -#include -Semaphore::Semaphore(int count) - : m_count(count) {} - -void Semaphore::notify() { - std::unique_lock lock(m_mutex); - m_count++; - lock.unlock(); - m_cv.notify_one(); -} - -void Semaphore::wait() { - std::unique_lock lock(m_mutex); - while (m_count == 0) - m_cv.wait(lock); - m_count--; -} - -void LaunchBrowser(Glib::ustring url) { +void LaunchBrowser(const Glib::ustring &url) { GError *err = nullptr; if (!gtk_show_uri_on_window(nullptr, url.c_str(), GDK_CURRENT_TIME, &err)) printf("failed to open uri: %s\n", err->message); } void GetImageDimensions(int inw, int inh, int &outw, int &outh, int clampw, int clamph) { - const auto frac = static_cast(inw) / inh; + const auto frac = static_cast(inw) / static_cast(inh); outw = inw; outh = inh; @@ -43,7 +25,7 @@ void GetImageDimensions(int inw, int inh, int &outw, int &outh, int clampw, int } } -std::vector ReadWholeFile(std::string path) { +std::vector ReadWholeFile(const std::string &path) { std::vector ret; FILE *fp = std::fopen(path.c_str(), "rb"); if (fp == nullptr) @@ -74,7 +56,7 @@ int GetTimezoneOffset() { std::time_t local_secs = std::mktime(tptr); tptr = std::gmtime(&secs); std::time_t gmt_secs = std::mktime(tptr); - return local_secs - gmt_secs; + return static_cast(local_secs - gmt_secs); } std::string FormatISO8601(const std::string &in, int extra_offset, const std::string &fmt) { @@ -82,7 +64,7 @@ std::string FormatISO8601(const std::string &in, int extra_offset, const std::st float milli; std::sscanf(in.c_str(), "%d-%d-%dT%d:%d:%d%f+%d:%d", &yr, &mon, &day, &hr, &min, &sec, &milli, &tzhr, &tzmin); - std::tm tm; + std::tm tm {}; tm.tm_year = yr - 1900; tm.tm_mon = mon - 1; tm.tm_mday = day; @@ -95,7 +77,7 @@ std::string FormatISO8601(const std::string &in, int extra_offset, const std::st int offset = GetTimezoneOffset(); tm.tm_sec += offset + extra_offset; mktime(&tm); - std::array tmp; + std::array tmp {}; std::strftime(tmp.data(), sizeof(tmp), fmt.c_str(), &tm); return tmp.data(); } @@ -144,13 +126,9 @@ Gdk::RGBA IntToRGBA(int color) { return ret; } -void AddWidgetMenuHandler(Gtk::Widget *widget, Gtk::Menu &menu) { - AddWidgetMenuHandler(widget, menu, []() {}); -} - // so widgets can modify the menu before it is displayed // maybe theres a better way to do this idk -void AddWidgetMenuHandler(Gtk::Widget *widget, Gtk::Menu &menu, sigc::slot pre_callback) { +void AddWidgetMenuHandler(Gtk::Widget *widget, Gtk::Menu &menu, const sigc::slot &pre_callback) { sigc::signal signal; signal.connect(pre_callback); widget->signal_button_press_event().connect([&menu, signal](GdkEventButton *ev) -> bool { @@ -169,7 +147,7 @@ std::vector StringSplit(const std::string &str, const char *delim) std::vector parts; char *token = std::strtok(const_cast(str.c_str()), delim); while (token != nullptr) { - parts.push_back(token); + parts.emplace_back(token); token = std::strtok(nullptr, delim); } return parts; @@ -178,7 +156,7 @@ std::vector StringSplit(const std::string &str, const char *delim) std::string GetExtension(std::string url) { url = StringSplit(url, "?")[0]; url = StringSplit(url, "/").back(); - return url.find(".") != std::string::npos ? url.substr(url.find_last_of(".")) : ""; + return url.find('.') != std::string::npos ? url.substr(url.find_last_of('.')) : ""; } bool IsURLViewableImage(const std::string &url) { diff --git a/src/util.hpp b/src/util.hpp index aa87301..4b14a7e 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -31,28 +31,15 @@ bool IsFile(std::string_view path); uint64_t TimeToEpoch(int year, int month, int day, int hour, int minute, int seconds); } // namespace util -class Semaphore { -public: - Semaphore(int count = 0); - void notify(); - void wait(); - -private: - std::mutex m_mutex; - std::condition_variable m_cv; - int m_count; -}; - -void LaunchBrowser(Glib::ustring url); +void LaunchBrowser(const Glib::ustring &url); void GetImageDimensions(int inw, int inh, int &outw, int &outh, int clampw = 400, int clamph = 300); std::string IntToCSSColor(int color); Gdk::RGBA IntToRGBA(int color); -void AddWidgetMenuHandler(Gtk::Widget *widget, Gtk::Menu &menu); -void AddWidgetMenuHandler(Gtk::Widget *widget, Gtk::Menu &menu, sigc::slot pre_callback); +void AddWidgetMenuHandler(Gtk::Widget *widget, Gtk::Menu &menu, const sigc::slot &pre_callback); std::vector StringSplit(const std::string &str, const char *delim); std::string GetExtension(std::string url); bool IsURLViewableImage(const std::string &url); -std::vector ReadWholeFile(std::string path); +std::vector ReadWholeFile(const std::string &path); std::string HumanReadableBytes(uint64_t bytes); std::string FormatISO8601(const std::string &in, int extra_offset = 0, const std::string &fmt = "%x %X"); void AddPointerCursor(Gtk::Widget &widget); diff --git a/src/windows/guildsettings/auditlogpane.cpp b/src/windows/guildsettings/auditlogpane.cpp index 08f99da..868a18f 100644 --- a/src/windows/guildsettings/auditlogpane.cpp +++ b/src/windows/guildsettings/auditlogpane.cpp @@ -30,7 +30,7 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { auto &discord = Abaddon::Get().GetDiscordClient(); auto guild = *discord.GetGuild(GuildID); for (const auto &entry : data.Entries) { - if (entry.TargetID == "") continue; + if (entry.TargetID.empty()) continue; auto expander = Gtk::manage(new Gtk::Expander); auto label = Gtk::manage(new Gtk::Label); @@ -56,7 +56,7 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { if (entry.Changes.has_value()) for (const auto &change : *entry.Changes) { if (change.Key == "icon_hash") { - extra_markup.push_back("Set the server icon"); + extra_markup.emplace_back("Set the server icon"); } else if (change.Key == "name") { auto new_name = change.NewValue; if (new_name.has_value()) @@ -64,7 +64,7 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { Glib::Markup::escape_text(new_name->get()) + ""); else - extra_markup.push_back("Set the server name"); + extra_markup.emplace_back("Set the server name"); } } } break; @@ -82,8 +82,8 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { Glib::Markup::escape_text(change.NewValue->get()) + ""); else if (change.Key == "nsfw" && change.NewValue.has_value()) - extra_markup.push_back((*change.NewValue ? "Marked" : "Unmarked") + - " the channel as NSFW"s); + extra_markup.emplace_back((*change.NewValue ? "Marked" : "Unmarked") + + " the channel as NSFW"s); } } break; @@ -119,16 +119,16 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { Glib::Markup::escape_text(change.NewValue->get()) + ""); else - extra_markup.push_back("Cleared the topic"); + extra_markup.emplace_back("Cleared the topic"); } else if (change.Key == "nsfw" && change.NewValue.has_value()) { - extra_markup.push_back((*change.NewValue ? "Marked" : "Unmarked") + " the channel as NSFW"s); + extra_markup.emplace_back((*change.NewValue ? "Marked" : "Unmarked") + " the channel as NSFW"s); } else if (change.Key == "rate_limit_per_user" && change.NewValue.has_value()) { const int secs = change.NewValue->get(); if (secs == 0) - extra_markup.push_back("Disabled slowmode"); + extra_markup.emplace_back("Disabled slowmode"); else - extra_markup.push_back("Set slowmode to " + - std::to_string(secs) + " seconds"); + extra_markup.emplace_back("Set slowmode to " + + std::to_string(secs) + " seconds"); } } } break; @@ -186,9 +186,9 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { " pruned " + *entry.Options->MembersRemoved + " members"; - extra_markup.push_back("For " + - *entry.Options->DeleteMemberDays + - " days of inactivity"); + extra_markup.emplace_back("For " + + *entry.Options->DeleteMemberDays + + " days of inactivity"); } break; case AuditLogActionType::MEMBER_BAN_ADD: { const auto target_user = discord.GetUser(entry.TargetID); @@ -213,13 +213,11 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { if (entry.Changes.has_value()) for (const auto &change : *entry.Changes) { if (change.Key == "deaf" && change.NewValue.has_value()) - extra_markup.push_back( - (change.NewValue->get() ? "Deafened"s : "Undeafened"s) + - " them"); + extra_markup.emplace_back((change.NewValue->get() ? "Deafened"s : "Undeafened"s) + + " them"); else if (change.Key == "mute" && change.NewValue.has_value()) - extra_markup.push_back( - (change.NewValue->get() ? "Muted"s : "Unmuted"s) + - " them"); + extra_markup.emplace_back((change.NewValue->get() ? "Muted"s : "Unmuted"s) + + " them"); else if (change.Key == "nick" && change.NewValue.has_value()) extra_markup.push_back("Set their nickname to " + Glib::Markup::escape_text(change.NewValue->get()) + @@ -289,17 +287,17 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { } else if (change.Key == "color" && change.NewValue.has_value()) { const auto col = change.NewValue->get(); if (col == 0) - extra_markup.push_back("Removed the color"); + extra_markup.emplace_back("Removed the color"); else - extra_markup.push_back("Set the color to " + - IntToCSSColor(col) + - ""); + extra_markup.emplace_back("Set the color to " + + IntToCSSColor(col) + + ""); } else if (change.Key == "permissions") { - extra_markup.push_back("Updated the permissions"); + extra_markup.emplace_back("Updated the permissions"); } else if (change.Key == "mentionable" && change.NewValue.has_value()) { - extra_markup.push_back(change.NewValue->get() ? "Mentionable" : "Not mentionable"); + extra_markup.emplace_back(change.NewValue->get() ? "Mentionable" : "Not mentionable"); } else if (change.Key == "hoist" && change.NewValue.has_value()) { - extra_markup.push_back(change.NewValue->get() ? "Not hoisted" : "Hoisted"); + extra_markup.emplace_back(change.NewValue->get() ? "Not hoisted" : "Hoisted"); } } } break; @@ -324,13 +322,13 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { } else if (change.Key == "max_uses" && change.NewValue.has_value()) { const auto uses = change.NewValue->get(); if (uses == 0) - extra_markup.push_back("Which has unlimited uses"); + extra_markup.emplace_back("Which has unlimited uses"); else - extra_markup.push_back("Which has " + std::to_string(uses) + " uses"); + extra_markup.emplace_back("Which has " + std::to_string(uses) + " uses"); } else if (change.Key == "temporary" && change.NewValue.has_value()) { - extra_markup.push_back("With temporary "s + - (change.NewValue->get() ? "on" : "off") + - ""); + extra_markup.emplace_back("With temporary "s + + (change.NewValue->get() ? "on" : "off") + + ""); } // no max_age cuz fuck time } } break; @@ -378,7 +376,7 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { Glib::Markup::escape_text(change.NewValue->get()) + ""); } else if (change.Key == "avatar_hash") { - extra_markup.push_back("Changed the avatar"); + extra_markup.emplace_back("Changed the avatar"); } else if (change.Key == "channel_id" && change.NewValue.has_value()) { const auto channel = discord.GetChannel(change.NewValue->get()); if (channel.has_value()) { @@ -386,7 +384,7 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { Glib::Markup::escape_text(*channel->Name) + ""); } else { - extra_markup.push_back("Changed the channel"); + extra_markup.emplace_back("Changed the channel"); } } } @@ -552,18 +550,18 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { if (change.Key == "name") extra_markup.push_back("Set the name to " + Glib::Markup::escape_text(change.NewValue->get()) + ""); else if (change.Key == "archived") - extra_markup.push_back(change.NewValue->get() ? "Archived the thread" : "Unarchived the thread"); + extra_markup.emplace_back(change.NewValue->get() ? "Archived the thread" : "Unarchived the thread"); else if (change.Key == "auto_archive_duration") - extra_markup.push_back("Set auto archive duration to "s + std::to_string(change.NewValue->get()) + " minutes"s); + extra_markup.emplace_back("Set auto archive duration to "s + std::to_string(change.NewValue->get()) + " minutes"s); else if (change.Key == "rate_limit_per_user" && change.NewValue.has_value()) { const int secs = change.NewValue->get(); if (secs == 0) - extra_markup.push_back("Disabled slowmode"); + extra_markup.emplace_back("Disabled slowmode"); else - extra_markup.push_back("Set slowmode to " + - std::to_string(secs) + " seconds"); + extra_markup.emplace_back("Set slowmode to " + + std::to_string(secs) + " seconds"); } else if (change.Key == "locked") - extra_markup.push_back(change.NewValue->get() ? "Locked the thread, restricting it to only be unarchived by moderators" : "Unlocked the thread, allowing it to be unarchived by non-moderators"); + extra_markup.emplace_back(change.NewValue->get() ? "Locked the thread, restricting it to only be unarchived by moderators" : "Unlocked the thread, allowing it to be unarchived by non-moderators"); } } } break; @@ -584,19 +582,19 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) { Glib::Markup::escape_text(change.NewValue->get()) + ""); else if (change.Key == "auto_archive_duration") - extra_markup.push_back("Set auto archive duration to "s + std::to_string(change.NewValue->get()) + " minutes"s); + extra_markup.emplace_back("Set auto archive duration to "s + std::to_string(change.NewValue->get()) + " minutes"s); else if (change.Key == "rate_limit_per_user" && change.NewValue.has_value()) { const int secs = change.NewValue->get(); if (secs == 0) - extra_markup.push_back("Disabled slowmode"); + extra_markup.emplace_back("Disabled slowmode"); else - extra_markup.push_back("Set slowmode to " + - std::to_string(secs) + - " seconds"); + extra_markup.emplace_back("Set slowmode to " + + std::to_string(secs) + + " seconds"); } else if (change.Key == "locked") - extra_markup.push_back(change.NewValue->get() ? "Locked the thread, restricting it to only be unarchived by moderators" : "Unlocked the thread, allowing it to be unarchived by non-moderators"); + extra_markup.emplace_back(change.NewValue->get() ? "Locked the thread, restricting it to only be unarchived by moderators" : "Unlocked the thread, allowing it to be unarchived by non-moderators"); else if (change.Key == "archived") - extra_markup.push_back(change.NewValue->get() ? "Archived the thread" : "Unarchived the thread"); + extra_markup.emplace_back(change.NewValue->get() ? "Archived the thread" : "Unarchived the thread"); } } break; case AuditLogActionType::THREAD_DELETE: { diff --git a/src/windows/guildsettings/banspane.cpp b/src/windows/guildsettings/banspane.cpp index 97a70c4..d4b002c 100644 --- a/src/windows/guildsettings/banspane.cpp +++ b/src/windows/guildsettings/banspane.cpp @@ -94,7 +94,7 @@ void GuildSettingsBansPane::OnMenuUnban() { auto selected_row = *m_view.get_selection()->get_selected(); if (selected_row) { Snowflake id = selected_row[m_columns.m_col_id]; - auto cb = [this](DiscordError code) { + auto cb = [](DiscordError code) { if (code != DiscordError::NONE) { Gtk::MessageDialog dlg("Failed to unban user", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true); dlg.set_position(Gtk::WIN_POS_CENTER); @@ -119,7 +119,7 @@ bool GuildSettingsBansPane::OnTreeButtonPress(GdkEventButton *event) { m_menu_unban.set_sensitive(can_ban); auto selection = m_view.get_selection(); Gtk::TreeModel::Path path; - if (m_view.get_path_at_pos(event->x, event->y, path)) { + if (m_view.get_path_at_pos(static_cast(event->x), static_cast(event->y), path)) { m_view.get_selection()->select(path); m_menu.popup_at_pointer(reinterpret_cast(event)); } diff --git a/src/windows/guildsettings/emojispane.cpp b/src/windows/guildsettings/emojispane.cpp index 57b697c..bdab6b8 100644 --- a/src/windows/guildsettings/emojispane.cpp +++ b/src/windows/guildsettings/emojispane.cpp @@ -52,7 +52,7 @@ GuildSettingsEmojisPane::GuildSettingsEmojisPane(Snowflake guild_id) m_filter->set_visible_func([this](const Gtk::TreeModel::const_iterator &iter) -> bool { const auto text = m_search.get_text(); - if (text == "") return true; + if (text.empty()) return true; return StringContainsCaseless((*iter)[m_columns.m_col_name], text); }); m_view.set_enable_search(false); @@ -71,12 +71,12 @@ GuildSettingsEmojisPane::GuildSettingsEmojisPane(Snowflake guild_id) column->pack_start(*renderer); column->add_attribute(renderer->property_text(), m_columns.m_col_name); renderer->property_editable() = true; - renderer->signal_edited().connect([this, renderer, column](const Glib::ustring &path, const Glib::ustring &text) { + renderer->signal_edited().connect([this](const Glib::ustring &path, const Glib::ustring &text) { std::string new_str; int size = 0; for (const auto ch : text) { if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_') - new_str += ch; + new_str += static_cast(ch); else if (ch == ' ') new_str += '_'; if (++size == 32) break; @@ -174,7 +174,7 @@ void GuildSettingsEmojisPane::OnFetchEmojis(std::vector emojis) { } void GuildSettingsEmojisPane::OnEditName(Snowflake id, const std::string &name) { - const auto cb = [this](DiscordError code) { + const auto cb = [](DiscordError code) { if (code != DiscordError::NONE) { Gtk::MessageDialog dlg("Failed to set emoji name", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK); dlg.set_position(Gtk::WIN_POS_CENTER); @@ -197,7 +197,7 @@ void GuildSettingsEmojisPane::OnMenuDelete() { const auto id = static_cast(selected_row[m_columns.m_col_id]); if (auto *window = dynamic_cast(get_toplevel())) if (Abaddon::Get().ShowConfirm("Are you sure you want to delete " + name + "?", window)) { - const auto cb = [this](DiscordError code) { + const auto cb = [](DiscordError code) { if (code != DiscordError::NONE) { Gtk::MessageDialog dlg("Failed to delete emoji", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK); dlg.set_position(Gtk::WIN_POS_CENTER); @@ -234,7 +234,7 @@ bool GuildSettingsEmojisPane::OnTreeButtonPress(GdkEventButton *event) { auto selection = m_view.get_selection(); Gtk::TreeModel::Path path; - if (m_view.get_path_at_pos(event->x, event->y, path)) { + if (m_view.get_path_at_pos(static_cast(event->x), static_cast(event->y), path)) { m_view.get_selection()->select(path); m_menu.popup_at_pointer(reinterpret_cast(event)); } diff --git a/src/windows/guildsettings/infopane.cpp b/src/windows/guildsettings/infopane.cpp index 9ef116f..bccfbf8 100644 --- a/src/windows/guildsettings/infopane.cpp +++ b/src/windows/guildsettings/infopane.cpp @@ -60,7 +60,7 @@ GuildSettingsInfoPane::GuildSettingsInfoPane(Snowflake id) guild_icon_url = guild.GetIconURL("gif", "512"); else guild_icon_url = guild.GetIconURL("png", "512"); - m_guild_icon_ev.signal_button_press_event().connect([this, guild_icon_url](GdkEventButton *event) -> bool { + m_guild_icon_ev.signal_button_press_event().connect([guild_icon_url](GdkEventButton *event) -> bool { if (event->type == GDK_BUTTON_PRESS) if (event->button == GDK_BUTTON_PRIMARY) LaunchBrowser(guild_icon_url); @@ -114,7 +114,7 @@ void GuildSettingsInfoPane::UpdateGuildIconFromData(const std::vector & auto encoded = "data:" + mime + ";base64," + Glib::Base64::encode(std::string(data.begin(), data.end())); auto &discord = Abaddon::Get().GetDiscordClient(); - auto cb = [this](DiscordError code) { + auto cb = [](DiscordError code) { if (code != DiscordError::NONE) { Gtk::MessageDialog dlg("Failed to set guild icon", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true); dlg.set_position(Gtk::WIN_POS_CENTER); @@ -160,7 +160,7 @@ void GuildSettingsInfoPane::UpdateGuildIconPicker() { loader->write(data.data(), data.size()); loader->close(); UpdateGuildIconFromPixbuf(loader->get_pixbuf()); - } catch (const std::exception &) {}; + } catch (const std::exception &) {} } }); @@ -209,7 +209,7 @@ void GuildSettingsInfoPane::UpdateGuildIconClipboard() { UpdateGuildIconFromPixbuf(pb); return; - } catch (const std::exception &) {}; + } catch (const std::exception &) {} } if (cb->wait_is_image_available()) { diff --git a/src/windows/guildsettings/invitespane.cpp b/src/windows/guildsettings/invitespane.cpp index bec4784..2d737e9 100644 --- a/src/windows/guildsettings/invitespane.cpp +++ b/src/windows/guildsettings/invitespane.cpp @@ -96,7 +96,7 @@ void GuildSettingsInvitesPane::OnMenuDelete() { auto selected_row = *m_view.get_selection()->get_selected(); if (selected_row) { auto code = static_cast(selected_row[m_columns.m_col_code]); - auto cb = [this](DiscordError code) { + auto cb = [](DiscordError code) { if (code != DiscordError::NONE) { Gtk::MessageDialog dlg("Failed to delete invite", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true); dlg.set_position(Gtk::WIN_POS_CENTER); @@ -115,7 +115,7 @@ bool GuildSettingsInvitesPane::OnTreeButtonPress(GdkEventButton *event) { m_menu_delete.set_sensitive(can_manage); auto selection = m_view.get_selection(); Gtk::TreeModel::Path path; - if (m_view.get_path_at_pos(event->x, event->y, path)) { + if (m_view.get_path_at_pos(static_cast(event->x), static_cast(event->y), path)) { m_view.get_selection()->select(path); m_menu.popup_at_pointer(reinterpret_cast(event)); } diff --git a/src/windows/guildsettings/memberspane.cpp b/src/windows/guildsettings/memberspane.cpp index bda92b4..34650ad 100644 --- a/src/windows/guildsettings/memberspane.cpp +++ b/src/windows/guildsettings/memberspane.cpp @@ -64,7 +64,7 @@ GuildSettingsMembersPaneMembers::GuildSettingsMembersPaneMembers(Snowflake id) m_list.set_filter_func([this](Gtk::ListBoxRow *row_) -> bool { const auto search_term = m_search.get_text(); - if (search_term.size() == 0) return true; + if (search_term.empty()) return true; if (auto *row = dynamic_cast(row_)) return StringContainsCaseless(row->DisplayTerm, m_search.get_text()); return true; @@ -208,12 +208,12 @@ void GuildSettingsMembersPaneInfo::SetUser(Snowflake user_id) { m_id.set_text("User ID: " + std::to_string(user_id)); m_created.set_text("Account created: " + user_id.GetLocalTimestamp()); - if (member.JoinedAt != "") + if (!member.JoinedAt.empty()) m_joined.set_text("Joined server: " + FormatISO8601(member.JoinedAt)); else m_joined.set_text("Joined server: Unknown"); m_nickname.set_text("Nickname: " + member.Nickname); - m_nickname.set_visible(member.Nickname != ""); + m_nickname.set_visible(!member.Nickname.empty()); if (member.PremiumSince.has_value()) { m_boosting.set_text("Boosting since " + FormatISO8601(*member.PremiumSince)); m_boosting.show(); @@ -244,7 +244,7 @@ GuildSettingsMembersPaneRoles::GuildSettingsMembersPaneRoles(Snowflake guild_id) } } - m_list.set_sort_func([this](Gtk::ListBoxRow *a, Gtk::ListBoxRow *b) -> int { + m_list.set_sort_func([](Gtk::ListBoxRow *a, Gtk::ListBoxRow *b) -> int { auto *rowa = dynamic_cast(a); auto *rowb = dynamic_cast(b); return rowb->Position - rowa->Position; @@ -319,8 +319,8 @@ void GuildSettingsMembersPaneRoles::OnRoleToggle(Snowflake role_id, bool new_set // hack to prevent cb from being called if SetRoles is called before callback completion sigc::signal tmp; - m_update_connection.push_back(tmp.connect(std::move(cb))); - const auto tmp_cb = [this, tmp = std::move(tmp)](DiscordError code) { tmp.emit(code == DiscordError::NONE); }; + m_update_connection.emplace_back(tmp.connect(std::move(cb))); + const auto tmp_cb = [tmp = std::move(tmp)](DiscordError code) { tmp.emit(code == DiscordError::NONE); }; discord.SetMemberRoles(GuildID, UserID, m_set_role_ids.begin(), m_set_role_ids.end(), sigc::track_obj(tmp_cb, *this)); } diff --git a/src/windows/guildsettings/rolespane.cpp b/src/windows/guildsettings/rolespane.cpp index 3567e95..d4fa7c0 100644 --- a/src/windows/guildsettings/rolespane.cpp +++ b/src/windows/guildsettings/rolespane.cpp @@ -61,14 +61,14 @@ GuildSettingsRolesPaneRoles::GuildSettingsRolesPaneRoles(Snowflake guild_id) if (static_cast(new_index) == num_rows) return true; // trying to move row below @everyone // make sure it wont modify a neighbor role u dont have perms to modify if (!discord.CanModifyRole(GuildID, row->RoleID)) return false; - const auto cb = [this](DiscordError code) { + const auto cb = [](DiscordError code) { if (code != DiscordError::NONE) { Gtk::MessageDialog dlg("Failed to set role position", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true); dlg.set_position(Gtk::WIN_POS_CENTER_ON_PARENT); dlg.run(); } }; - discord.ModifyRolePosition(GuildID, row->RoleID, new_pos, sigc::track_obj(cb, *this)); + discord.ModifyRolePosition(GuildID, row->RoleID, static_cast(new_pos), sigc::track_obj(cb, *this)); return true; } return false; @@ -95,7 +95,7 @@ GuildSettingsRolesPaneRoles::GuildSettingsRolesPaneRoles(Snowflake guild_id) } } - m_list.set_sort_func([this](Gtk::ListBoxRow *rowa_, Gtk::ListBoxRow *rowb_) -> int { + m_list.set_sort_func([](Gtk::ListBoxRow *rowa_, Gtk::ListBoxRow *rowb_) -> int { auto *rowa = dynamic_cast(rowa_); auto *rowb = dynamic_cast(rowb_); return rowb->Position - rowa->Position; @@ -104,7 +104,7 @@ GuildSettingsRolesPaneRoles::GuildSettingsRolesPaneRoles(Snowflake guild_id) m_list.set_filter_func([this](Gtk::ListBoxRow *row_) -> bool { const auto search_term = m_search.get_text(); - if (search_term.size() == 0) return true; + if (search_term.empty()) return true; if (auto *row = dynamic_cast(row_)) return StringContainsCaseless(row->DisplayTerm, m_search.get_text()); return true; @@ -380,8 +380,8 @@ void GuildSettingsRolesPaneInfo::OnPermissionToggle(Permission perm, bool new_se m_perms &= ~perm; sigc::signal tmp; - m_update_connections.push_back(tmp.connect(std::move(cb))); - const auto tmp_cb = [this, tmp = std::move(tmp)](DiscordError code) { tmp.emit(code == DiscordError::NONE); }; + m_update_connections.emplace_back(tmp.connect(std::move(cb))); + const auto tmp_cb = [tmp = std::move(tmp)](DiscordError code) { tmp.emit(code == DiscordError::NONE); }; discord.ModifyRolePermissions(GuildID, RoleID, m_perms, sigc::track_obj(tmp_cb, *this)); } diff --git a/src/windows/mainwindow.cpp b/src/windows/mainwindow.cpp index c8abb75..89e89f0 100644 --- a/src/windows/mainwindow.cpp +++ b/src/windows/mainwindow.cpp @@ -84,7 +84,7 @@ MainWindow::MainWindow() m_signal_action_set_status.emit(); }); - m_menu_file_clear_cache.signal_activate().connect([this] { + m_menu_file_clear_cache.signal_activate().connect([] { Abaddon::Get().GetImageManager().ClearCache(); }); @@ -115,7 +115,7 @@ MainWindow::MainWindow() } }); - m_menu_view_mark_all_as_read.signal_activate().connect([this] { + m_menu_view_mark_all_as_read.signal_activate().connect([] { Abaddon::Get().GetDiscordClient().MarkAllAsRead(NOOP_CALLBACK); }); @@ -231,7 +231,7 @@ void MainWindow::UpdateChatPrependHistory(const std::vector &msgs) { m_chat.AddNewHistory(msgs); // given vector should be sorted ascending } -void MainWindow::InsertChatInput(std::string text) { +void MainWindow::InsertChatInput(const std::string &text) { m_chat.InsertChatInput(text); } @@ -260,7 +260,7 @@ void MainWindow::OnDiscordSubmenuPopup(const Gdk::Rectangle *flipped_rect, const const bool discord_active = Abaddon::Get().GetDiscordClient().IsStarted(); std::string token = Abaddon::Get().GetDiscordToken(); - m_menu_discord_connect.set_sensitive(token.size() > 0 && !discord_active); + m_menu_discord_connect.set_sensitive(!token.empty() && !discord_active); m_menu_discord_disconnect.set_sensitive(discord_active); m_menu_discord_join_guild.set_sensitive(discord_active); m_menu_discord_set_token.set_sensitive(!discord_active); diff --git a/src/windows/mainwindow.hpp b/src/windows/mainwindow.hpp index 7afe782..5e48f8a 100644 --- a/src/windows/mainwindow.hpp +++ b/src/windows/mainwindow.hpp @@ -19,7 +19,7 @@ public: void UpdateChatMessageDeleted(Snowflake id, Snowflake channel_id); void UpdateChatMessageUpdated(Snowflake id, Snowflake channel_id); void UpdateChatPrependHistory(const std::vector &msgs); - void InsertChatInput(std::string text); + void InsertChatInput(const std::string &text); Snowflake GetChatOldestListedMessage(); void UpdateChatReactionAdd(Snowflake id, const Glib::ustring ¶m); void UpdateChatReactionRemove(Snowflake id, const Glib::ustring ¶m); diff --git a/src/windows/profile/userinfopane.cpp b/src/windows/profile/userinfopane.cpp index a95a14c..a17dbff 100644 --- a/src/windows/profile/userinfopane.cpp +++ b/src/windows/profile/userinfopane.cpp @@ -39,8 +39,8 @@ ConnectionItem::ConnectionItem(const ConnectionData &conn) m_name.set_single_line_mode(true); m_name.set_ellipsize(Pango::ELLIPSIZE_END); m_box.add(m_name); - if (url != "") { - auto cb = [this, url](GdkEventButton *event) -> bool { + if (!url.empty()) { + auto cb = [url](GdkEventButton *event) -> bool { if (event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY) { LaunchBrowser(url); return true; @@ -102,7 +102,7 @@ void ConnectionsContainer::SetConnections(const std::vector &con if (supported_services.find(conn.Type) == supported_services.end()) continue; auto widget = Gtk::manage(new ConnectionItem(conn)); widget->show(); - attach(*widget, i % 2, i / 2, 1, 1); + attach(*widget, static_cast(i % 2), static_cast(i / 2), 1, 1); } set_halign(Gtk::ALIGN_FILL); @@ -185,7 +185,7 @@ ProfileUserInfoPane::ProfileUserInfoPane(Snowflake ID) m_created.get_style_context()->add_class("profile-info-created"); m_note.signal_update_note().connect([this](const Glib::ustring ¬e) { - auto cb = [this](DiscordError code) { + auto cb = [](DiscordError code) { if (code != DiscordError::NONE) { Gtk::MessageDialog dlg("Failed to set note", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true); dlg.set_position(Gtk::WIN_POS_CENTER); @@ -196,7 +196,7 @@ ProfileUserInfoPane::ProfileUserInfoPane(Snowflake ID) }); auto &discord = Abaddon::Get().GetDiscordClient(); - auto note_update_cb = [this](Snowflake id, std::string note) { + auto note_update_cb = [this](Snowflake id, const std::string ¬e) { if (id == UserID) m_note.SetNote(note); }; @@ -225,7 +225,7 @@ ProfileUserInfoPane::ProfileUserInfoPane(Snowflake ID) } void ProfileUserInfoPane::SetProfile(const UserProfileData &data) { - if (data.User.Bio.has_value() && *data.User.Bio != "") { + if (data.User.Bio.has_value() && !data.User.Bio->empty()) { m_bio.SetBio(*data.User.Bio); m_bio.show(); } else { diff --git a/src/windows/profilewindow.cpp b/src/windows/profilewindow.cpp index 9d93564..aff98c5 100644 --- a/src/windows/profilewindow.cpp +++ b/src/windows/profilewindow.cpp @@ -34,7 +34,7 @@ ProfileWindow::ProfileWindow(Snowflake user_id) if (user.HasAvatar()) AddPointerCursor(m_avatar_ev); - m_avatar_ev.signal_button_press_event().connect([this, user](GdkEventButton *event) -> bool { + m_avatar_ev.signal_button_press_event().connect([user](GdkEventButton *event) -> bool { if (event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY) { if (user.HasAnimatedAvatar()) LaunchBrowser(user.GetAvatarURL("gif", "512")); diff --git a/src/windows/threadswindow.cpp b/src/windows/threadswindow.cpp index c18819b..62cae19 100644 --- a/src/windows/threadswindow.cpp +++ b/src/windows/threadswindow.cpp @@ -62,7 +62,7 @@ ThreadsWindow::ThreadsWindow(const ChannelData &channel) add(m_box); } -bool ThreadsWindow::ListFilterFunc(Gtk::ListBoxRow *row_) { +bool ThreadsWindow::ListFilterFunc(Gtk::ListBoxRow *row_) const { if (auto *row = dynamic_cast(row_)) return (m_filter_mode == FILTER_PUBLIC && (row->Type == ChannelType::GUILD_PUBLIC_THREAD || row->Type == ChannelType::GUILD_NEWS_THREAD)) || (m_filter_mode == FILTER_PRIVATE && row->Type == ChannelType::GUILD_PRIVATE_THREAD); diff --git a/src/windows/threadswindow.hpp b/src/windows/threadswindow.hpp index ebcbd13..276e067 100644 --- a/src/windows/threadswindow.hpp +++ b/src/windows/threadswindow.hpp @@ -43,7 +43,7 @@ public: private: // this filtering is rather cringe but idk what a better alternative would be - bool ListFilterFunc(Gtk::ListBoxRow *row_); + bool ListFilterFunc(Gtk::ListBoxRow *row_) const; enum FilterMode { FILTER_PUBLIC = 0,