use a singleton (oh no) instead of passing Abaddon* everywhere

This commit is contained in:
ouwou 2020-09-07 00:27:41 -04:00
parent 3832ff9a15
commit 902c96ee53
13 changed files with 44 additions and 89 deletions

View File

@ -28,6 +28,11 @@ Abaddon::~Abaddon() {
m_discord.Stop();
}
Abaddon &Abaddon::Get() {
static Abaddon instance;
return instance;
}
int Abaddon::StartGTK() {
m_gtk_app = Gtk::Application::create("com.github.lorpus.abaddon");
@ -39,7 +44,6 @@ int Abaddon::StartGTK() {
});
m_main_window = std::make_unique<MainWindow>();
m_main_window->SetAbaddon(this);
m_main_window->set_title(APP_TITLE);
m_main_window->show();
m_main_window->UpdateComponents();
@ -260,6 +264,5 @@ void Abaddon::ActionReloadCSS() {
int main(int argc, char **argv) {
Gtk::Main::init_gtkmm_internals(); // why???
Abaddon abaddon;
return abaddon.StartGTK();
return Abaddon::Get().StartGTK();
}

View File

@ -10,9 +10,16 @@
#define APP_TITLE "Abaddon"
class Abaddon {
public:
private:
Abaddon();
~Abaddon();
Abaddon(const Abaddon &) = delete;
Abaddon &operator=(const Abaddon &) = delete;
Abaddon(Abaddon &&) = delete;
Abaddon &operator=(Abaddon &&) = delete;
public:
static Abaddon &Get();
int StartGTK();
void StartDiscord();

View File

@ -31,10 +31,6 @@ ChannelList::ChannelList() {
m_update_dispatcher.connect(sigc::mem_fun(*this, &ChannelList::SetListingFromGuildsInternal));
}
void ChannelList::SetAbaddon(Abaddon *ptr) {
m_abaddon = ptr;
}
Gtk::Widget *ChannelList::GetRoot() const {
return m_main;
}
@ -57,7 +53,7 @@ void ChannelList::on_row_activated(Gtk::ListBoxRow *row) {
info.IsUserCollapsed = new_collapsed;
if (info.Type == ListItemInfo::ListItemType::Channel) {
m_abaddon->ActionListChannelItemClick(info.ID);
Abaddon::Get().ActionListChannelItemClick(info.ID);
}
if (info.CatArrow != nullptr)
@ -87,7 +83,7 @@ void ChannelList::on_row_activated(Gtk::ListBoxRow *row) {
}
void ChannelList::AddPrivateChannels() {
auto dms = m_abaddon->GetDiscordClient().GetPrivateChannels();
auto dms = Abaddon::Get().GetDiscordClient().GetPrivateChannels();
auto *parent_row = Gtk::manage(new Gtk::ListBoxRow);
auto *parent_ev = Gtk::manage(new Gtk::EventBox);
@ -108,7 +104,7 @@ void ChannelList::AddPrivateChannels() {
parent_info.Type = ListItemInfo::ListItemType::Guild; // good nuf
for (const auto &dmid : dms) {
auto *data = m_abaddon->GetDiscordClient().GetChannel(dmid);
auto *data = Abaddon::Get().GetDiscordClient().GetChannel(dmid);
auto *dm_row = Gtk::manage(new Gtk::ListBoxRow);
auto *dm_ev = Gtk::manage(new Gtk::EventBox);
@ -301,7 +297,7 @@ void ChannelList::SetListingFromGuildsInternal() {
m_infos[guild_row] = std::move(info);
};
const auto &discord = m_abaddon->GetDiscordClient();
const auto &discord = Abaddon::Get().GetDiscordClient();
const auto &sorted_guilds = discord.GetUserSortedGuilds();
for (const auto &id : sorted_guilds) {
add_guild(id, *discord.GetGuild(id));
@ -310,17 +306,17 @@ void ChannelList::SetListingFromGuildsInternal() {
void ChannelList::on_menu_move_up() {
auto row = m_list->get_selected_row();
m_abaddon->ActionMoveGuildUp(m_infos[row].ID);
Abaddon::Get().ActionMoveGuildUp(m_infos[row].ID);
}
void ChannelList::on_menu_move_down() {
auto row = m_list->get_selected_row();
m_abaddon->ActionMoveGuildDown(m_infos[row].ID);
Abaddon::Get().ActionMoveGuildDown(m_infos[row].ID);
}
void ChannelList::on_menu_copyid() {
auto row = m_list->get_selected_row();
m_abaddon->ActionCopyGuildID(m_infos[row].ID);
Abaddon::Get().ActionCopyGuildID(m_infos[row].ID);
}
void ChannelList::AttachMenuHandler(Gtk::ListBoxRow *row) {

View File

@ -6,7 +6,6 @@
#include <unordered_set>
#include "../discord/discord.hpp"
class Abaddon;
class ChannelList {
public:
ChannelList();
@ -14,8 +13,6 @@ public:
void SetListingFromGuilds(const DiscordClient::guilds_type &guilds);
void ClearListing();
void SetAbaddon(Abaddon *ptr);
protected:
Gtk::ListBox *m_list;
Gtk::ScrolledWindow *m_main;
@ -54,6 +51,4 @@ protected:
void AddPrivateChannels(); // retard moment
void SetListingFromGuildsInternal();
void AttachMenuHandler(Gtk::ListBoxRow *row);
Abaddon *m_abaddon = nullptr;
};

View File

@ -50,13 +50,8 @@ ChatMessageContainer::ChatMessageContainer(const MessageData *data) {
show();
}
void ChatMessageContainer::SetAbaddon(Abaddon *ptr) {
m_abaddon = ptr;
}
void ChatMessageContainer::Update() {
if (m_abaddon == nullptr) return;
auto &discord = m_abaddon->GetDiscordClient();
auto &discord = Abaddon::Get().GetDiscordClient();
auto guild_id = discord.GetChannel(ChannelID)->GuildID;
auto role_id = discord.GetMemberHoistedRole(guild_id, UserID, true);
auto *user = discord.GetUser(UserID);
@ -94,16 +89,12 @@ ChatMessageItem::ChatMessageItem() {
m_menu.show_all();
}
void ChatMessageItem::SetAbaddon(Abaddon *ptr) {
m_abaddon = ptr;
}
void ChatMessageItem::on_menu_message_delete() {
m_abaddon->ActionChatDeleteMessage(ChannelID, ID);
Abaddon::Get().ActionChatDeleteMessage(ChannelID, ID);
}
void ChatMessageItem::on_menu_message_edit() {
m_abaddon->ActionChatEditMessage(ChannelID, ID);
Abaddon::Get().ActionChatEditMessage(ChannelID, ID);
}
void ChatMessageItem::on_menu_copy_id() {
@ -125,7 +116,7 @@ void ChatMessageItem::AttachMenuHandler(Gtk::Widget *widget) {
// clang-format on
void ChatMessageItem::ShowMenu(const GdkEvent *event) {
auto &client = m_abaddon->GetDiscordClient();
auto &client = Abaddon::Get().GetDiscordClient();
auto *data = client.GetMessage(ID);
bool can_manage = client.GetUserData().ID == data->Author.ID;
m_menu_delete_message->set_sensitive(can_manage);

View File

@ -18,13 +18,10 @@ public:
Snowflake ChannelID;
ChatMessageContainer(const MessageData *data);
void SetAbaddon(Abaddon *ptr);
void AddNewContent(Gtk::Widget *widget, bool prepend = false);
void Update();
protected:
Abaddon *m_abaddon = nullptr;
Gtk::Box *m_main_box;
Gtk::Box *m_content_box;
Gtk::Box *m_meta_box;
@ -35,7 +32,6 @@ protected:
class ChatMessageItem {
public:
ChatMessageItem();
void SetAbaddon(Abaddon *ptr);
Snowflake ChannelID;
Snowflake ID;
@ -56,8 +52,6 @@ protected:
Gtk::MenuItem *m_menu_copy_id;
Gtk::MenuItem *m_menu_delete_message;
Gtk::MenuItem *m_menu_edit_message;
Abaddon *m_abaddon = nullptr;
};
class ChatMessageTextItem

View File

@ -66,10 +66,6 @@ ChatWindow::ChatWindow() {
m_main->add(*m_entry_scroll);
}
void ChatWindow::SetAbaddon(Abaddon *ptr) {
m_abaddon = ptr;
}
Gtk::Widget *ChatWindow::GetRoot() const {
return m_main;
}
@ -112,7 +108,6 @@ void ChatWindow::ProcessMessage(const MessageData *data, bool prepend) {
container = last_row;
} else {
container = Gtk::manage(new ChatMessageContainer(data)); // only accesses timestamp and user
container->SetAbaddon(m_abaddon);
container->Update();
m_num_rows++;
}
@ -122,14 +117,12 @@ void ChatWindow::ProcessMessage(const MessageData *data, bool prepend) {
auto *text = Gtk::manage(new ChatMessageTextItem(data));
text->ID = data->ID;
text->ChannelID = m_active_channel;
text->SetAbaddon(m_abaddon);
container->AddNewContent(text, prepend);
m_id_to_widget[data->ID] = text;
} else if (type == ChatDisplayType::Embed) {
auto *widget = Gtk::manage(new ChatMessageEmbedItem(data));
widget->ID = data->ID;
widget->ChannelID = m_active_channel;
widget->SetAbaddon(m_abaddon);
container->AddNewContent(widget, prepend);
m_id_to_widget[data->ID] = widget;
}
@ -155,7 +148,7 @@ bool ChatWindow::on_key_press_event(GdkEventKey *e) {
auto text = buffer->get_text();
buffer->set_text("");
m_abaddon->ActionChatInputSubmit(text, m_active_channel);
Abaddon::Get().ActionChatInputSubmit(text, m_active_channel);
return true;
}
@ -165,7 +158,7 @@ bool ChatWindow::on_key_press_event(GdkEventKey *e) {
void ChatWindow::on_scroll_edge_overshot(Gtk::PositionType pos) {
if (pos == Gtk::POS_TOP)
m_abaddon->ActionChatLoadHistory(m_active_channel);
Abaddon::Get().ActionChatLoadHistory(m_active_channel);
}
void ChatWindow::SetMessages(std::set<Snowflake> msgs) {
@ -223,7 +216,7 @@ void ChatWindow::AddNewMessageInternal() {
m_new_message_queue.pop();
}
auto data = m_abaddon->GetDiscordClient().GetMessage(id);
auto data = Abaddon::Get().GetDiscordClient().GetMessage(id);
ProcessMessage(data);
}
@ -237,7 +230,7 @@ void ChatWindow::AddNewHistoryInternal() {
}
for (auto it = msgs.rbegin(); it != msgs.rend(); it++) {
ProcessMessage(m_abaddon->GetDiscordClient().GetMessage(*it), true);
ProcessMessage(Abaddon::Get().GetDiscordClient().GetMessage(*it), true);
}
{
@ -274,7 +267,7 @@ void ChatWindow::UpdateMessageContentInternal() {
if (m_id_to_widget.find(id) == m_id_to_widget.end())
return;
auto *msg = m_abaddon->GetDiscordClient().GetMessage(id);
auto *msg = Abaddon::Get().GetDiscordClient().GetMessage(id);
auto *item = dynamic_cast<ChatMessageTextItem *>(m_id_to_widget.at(id));
if (item != nullptr) {
item->EditContent(msg->Content);
@ -303,7 +296,7 @@ void ChatWindow::SetMessagesInternal() {
// sort
std::map<Snowflake, const MessageData *> sorted_messages;
for (const auto id : *msgs)
sorted_messages[id] = m_abaddon->GetDiscordClient().GetMessage(id);
sorted_messages[id] = Abaddon::Get().GetDiscordClient().GetMessage(id);
for (const auto &[id, msg] : sorted_messages) {
ProcessMessage(msg);

View File

@ -6,11 +6,9 @@
#include "chatmessage.hpp"
#include "../discord/discord.hpp"
class Abaddon;
class ChatWindow {
public:
ChatWindow();
void SetAbaddon(Abaddon *ptr);
Gtk::Widget *GetRoot() const;
void SetActiveChannel(Snowflake id);
@ -60,6 +58,4 @@ protected:
Gtk::ScrolledWindow *m_scroll;
Gtk::ScrolledWindow *m_entry_scroll;
Gtk::TextView *m_input;
Abaddon *m_abaddon = nullptr;
};

View File

@ -32,7 +32,7 @@ Gtk::Widget *MemberList::GetRoot() const {
void MemberList::SetActiveChannel(Snowflake id) {
std::scoped_lock<std::mutex> guard(m_mutex);
m_chan_id = id;
m_guild_id = m_abaddon->GetDiscordClient().GetChannel(id)->GuildID;
m_guild_id = Abaddon::Get().GetDiscordClient().GetChannel(id)->GuildID;
}
void MemberList::UpdateMemberList() {
@ -50,7 +50,7 @@ void MemberList::UpdateMemberListInternal() {
if (!m_chan_id.IsValid()) return;
auto &discord = m_abaddon->GetDiscordClient();
auto &discord = Abaddon::Get().GetDiscordClient();
auto *chan = discord.GetChannel(m_chan_id);
std::unordered_set<Snowflake> ids;
if (chan->Type == ChannelType::DM || chan->Type == ChannelType::GROUP_DM) {
@ -165,7 +165,7 @@ void MemberList::on_copy_id_activate() {
void MemberList::on_insert_mention_activate() {
auto *row = dynamic_cast<MemberListUserRow *>(m_row_menu_target);
if (row == nullptr) return;
m_abaddon->ActionInsertMention(row->ID);
Abaddon::Get().ActionInsertMention(row->ID);
}
void MemberList::AttachUserMenuHandler(Gtk::ListBoxRow *row, Snowflake id) {
@ -179,7 +179,3 @@ void MemberList::AttachUserMenuHandler(Gtk::ListBoxRow *row, Snowflake id) {
return false;
});
}
void MemberList::SetAbaddon(Abaddon *ptr) {
m_abaddon = ptr;
}

View File

@ -3,7 +3,6 @@
#include <mutex>
#include "../discord/discord.hpp"
class Abaddon;
class MemberList {
public:
class MemberListUserRow : public Gtk::ListBoxRow {
@ -17,8 +16,6 @@ public:
void UpdateMemberList();
void SetActiveChannel(Snowflake id);
void SetAbaddon(Abaddon *ptr);
private:
void on_copy_id_activate();
void on_insert_mention_activate();
@ -39,5 +36,4 @@ private:
Snowflake m_guild_id;
Snowflake m_chan_id;
Abaddon *m_abaddon = nullptr;
};

View File

@ -113,7 +113,6 @@ private:
UserData m_user_data;
UserSettingsData m_user_settings;
Abaddon *m_abaddon = nullptr;
Store m_store;
HTTPClient m_http;
Websocket m_websocket;

View File

@ -29,19 +29,19 @@ MainWindow::MainWindow()
m_menu_bar.append(m_menu_discord);
m_menu_discord_connect.signal_activate().connect([&] {
m_abaddon->ActionConnect();
Abaddon::Get().ActionConnect();
});
m_menu_discord_disconnect.signal_activate().connect([&] {
m_abaddon->ActionDisconnect();
Abaddon::Get().ActionDisconnect();
});
m_menu_discord_set_token.signal_activate().connect([&] {
m_abaddon->ActionSetToken();
Abaddon::Get().ActionSetToken();
});
m_menu_file_reload_css.signal_activate().connect([this] {
m_abaddon->ActionReloadCSS();
Abaddon::Get().ActionReloadCSS();
});
m_content_box.set_hexpand(true);
@ -83,9 +83,9 @@ MainWindow::MainWindow()
}
void MainWindow::UpdateComponents() {
bool discord_active = m_abaddon->IsDiscordActive();
bool discord_active = Abaddon::Get().IsDiscordActive();
std::string token = m_abaddon->GetDiscordToken();
std::string token = Abaddon::Get().GetDiscordToken();
m_menu_discord_connect.set_sensitive(token.size() > 0 && !discord_active);
m_menu_discord_disconnect.set_sensitive(discord_active);
@ -104,18 +104,18 @@ void MainWindow::UpdateMembers() {
}
void MainWindow::UpdateChannelListing() {
auto &discord = m_abaddon->GetDiscordClient();
auto &discord = Abaddon::Get().GetDiscordClient();
m_channel_list.SetListingFromGuilds(discord.GetGuilds());
}
void MainWindow::UpdateChatWindowContents() {
auto &discord = m_abaddon->GetDiscordClient();
auto &discord = Abaddon::Get().GetDiscordClient();
m_chat.SetMessages(discord.GetMessagesForChannel(m_chat.GetActiveChannel()));
m_members.UpdateMemberList();
}
void MainWindow::UpdateChatActiveChannel(Snowflake id) {
auto &discord = m_abaddon->GetDiscordClient();
auto &discord = Abaddon::Get().GetDiscordClient();
m_chat.SetActiveChannel(id);
m_members.SetActiveChannel(id);
}
@ -125,7 +125,7 @@ Snowflake MainWindow::GetChatActiveChannel() const {
}
void MainWindow::UpdateChatNewMessage(Snowflake id) {
if (m_abaddon->GetDiscordClient().GetMessage(id)->ChannelID == GetChatActiveChannel()) {
if (Abaddon::Get().GetDiscordClient().GetMessage(id)->ChannelID == GetChatActiveChannel()) {
m_chat.AddNewMessage(id);
m_members.UpdateMemberList();
}
@ -149,10 +149,3 @@ void MainWindow::UpdateChatPrependHistory(const std::vector<Snowflake> &msgs) {
void MainWindow::InsertChatInput(std::string text) {
m_chat.InsertChatInput(text);
}
void MainWindow::SetAbaddon(Abaddon *ptr) {
m_abaddon = ptr;
m_channel_list.SetAbaddon(ptr);
m_chat.SetAbaddon(ptr);
m_members.SetAbaddon(ptr);
}

View File

@ -4,11 +4,9 @@
#include "../components/memberlist.hpp"
#include <gtkmm.h>
class Abaddon;
class MainWindow : public Gtk::Window {
public:
MainWindow();
void SetAbaddon(Abaddon *ptr);
void UpdateComponents();
void UpdateMembers();
@ -42,6 +40,4 @@ protected:
Gtk::MenuItem m_menu_file;
Gtk::Menu m_menu_file_sub;
Gtk::MenuItem m_menu_file_reload_css;
Abaddon *m_abaddon = nullptr;
};