2020-08-26 05:46:43 +00:00
|
|
|
#include "chatmessage.hpp"
|
2020-08-30 06:00:56 +00:00
|
|
|
#include "../abaddon.hpp"
|
2020-09-06 01:05:52 +00:00
|
|
|
#include "../util.hpp"
|
2020-10-24 23:42:06 +00:00
|
|
|
#include <unordered_map>
|
2020-08-26 05:46:43 +00:00
|
|
|
|
2020-12-22 06:23:56 +00:00
|
|
|
constexpr const int EmojiSize = 24; // settings eventually
|
2020-12-22 07:24:09 +00:00
|
|
|
constexpr const int AvatarSize = 32;
|
2020-12-26 08:57:08 +00:00
|
|
|
constexpr const int EmbedImageWidth = 400;
|
|
|
|
constexpr const int EmbedImageHeight = 300;
|
2020-12-22 06:23:56 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
ChatMessageItemContainer::ChatMessageItemContainer() {
|
|
|
|
m_main = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
|
|
|
|
add(*m_main);
|
2020-08-28 22:21:08 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
m_menu_copy_id = Gtk::manage(new Gtk::MenuItem("Copy ID"));
|
|
|
|
m_menu_copy_id->signal_activate().connect(sigc::mem_fun(*this, &ChatMessageItemContainer::on_menu_copy_id));
|
2020-08-30 06:00:56 +00:00
|
|
|
m_menu.append(*m_menu_copy_id);
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
m_menu_delete_message = Gtk::manage(new Gtk::MenuItem("Delete Message"));
|
|
|
|
m_menu_delete_message->signal_activate().connect(sigc::mem_fun(*this, &ChatMessageItemContainer::on_menu_delete_message));
|
2020-08-30 06:00:56 +00:00
|
|
|
m_menu.append(*m_menu_delete_message);
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
m_menu_edit_message = Gtk::manage(new Gtk::MenuItem("Edit Message"));
|
|
|
|
m_menu_edit_message->signal_activate().connect(sigc::mem_fun(*this, &ChatMessageItemContainer::on_menu_edit_message));
|
2020-08-31 02:55:36 +00:00
|
|
|
m_menu.append(*m_menu_edit_message);
|
|
|
|
|
2020-10-13 03:55:52 +00:00
|
|
|
m_menu_copy_content = Gtk::manage(new Gtk::MenuItem("Copy Content"));
|
|
|
|
m_menu_copy_content->signal_activate().connect(sigc::mem_fun(*this, &ChatMessageItemContainer::on_menu_copy_content));
|
|
|
|
m_menu.append(*m_menu_copy_content);
|
|
|
|
|
2020-08-30 06:00:56 +00:00
|
|
|
m_menu.show_all();
|
2020-11-06 05:20:31 +00:00
|
|
|
|
|
|
|
m_link_menu_copy = Gtk::manage(new Gtk::MenuItem("Copy Link"));
|
|
|
|
m_link_menu_copy->signal_activate().connect(sigc::mem_fun(*this, &ChatMessageItemContainer::on_link_menu_copy));
|
|
|
|
m_link_menu.append(*m_link_menu_copy);
|
|
|
|
|
|
|
|
m_link_menu.show_all();
|
2020-08-30 06:00:56 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
ChatMessageItemContainer *ChatMessageItemContainer::FromMessage(Snowflake id) {
|
2020-11-24 01:34:09 +00:00
|
|
|
const auto data = Abaddon::Get().GetDiscordClient().GetMessage(id);
|
|
|
|
if (!data.has_value()) return nullptr;
|
2020-08-31 02:55:36 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
auto *container = Gtk::manage(new ChatMessageItemContainer);
|
|
|
|
container->ID = data->ID;
|
|
|
|
container->ChannelID = data->ChannelID;
|
2020-08-30 06:00:56 +00:00
|
|
|
|
2020-09-26 04:49:25 +00:00
|
|
|
if (data->Content.size() > 0 || data->Type != MessageType::DEFAULT) {
|
2020-11-24 01:34:09 +00:00
|
|
|
container->m_text_component = container->CreateTextComponent(&*data);
|
2021-01-01 07:47:03 +00:00
|
|
|
container->AttachEventHandlers(*container->m_text_component);
|
2020-09-26 03:02:13 +00:00
|
|
|
container->m_main->add(*container->m_text_component);
|
2020-09-24 06:43:27 +00:00
|
|
|
}
|
2020-08-30 06:00:56 +00:00
|
|
|
|
2021-01-01 07:39:07 +00:00
|
|
|
if (data->MessageReference.has_value()) {
|
2021-01-01 07:47:03 +00:00
|
|
|
auto *widget = container->CreateReplyComponent(*data);
|
2021-01-01 07:39:07 +00:00
|
|
|
container->m_main->add(*widget);
|
|
|
|
container->m_main->child_property_position(*widget) = 0; // eek
|
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
// there should only ever be 1 embed (i think?)
|
|
|
|
if (data->Embeds.size() == 1) {
|
2020-12-26 08:57:08 +00:00
|
|
|
const auto &embed = data->Embeds[0];
|
|
|
|
if (IsEmbedImageOnly(embed)) {
|
|
|
|
auto *widget = container->CreateImageComponent(*embed.Thumbnail->ProxyURL, *embed.Thumbnail->URL, *embed.Thumbnail->Width, *embed.Thumbnail->Height);
|
2021-01-01 07:47:03 +00:00
|
|
|
container->AttachEventHandlers(*widget);
|
2020-12-26 08:57:08 +00:00
|
|
|
container->m_main->add(*widget);
|
|
|
|
} else {
|
|
|
|
container->m_embed_component = container->CreateEmbedComponent(embed);
|
2021-01-01 07:47:03 +00:00
|
|
|
container->AttachEventHandlers(*container->m_embed_component);
|
2020-12-26 08:57:08 +00:00
|
|
|
container->m_main->add(*container->m_embed_component);
|
|
|
|
}
|
2020-09-26 03:02:13 +00:00
|
|
|
}
|
2020-08-30 06:00:56 +00:00
|
|
|
|
2020-09-30 04:12:38 +00:00
|
|
|
// i dont think attachments can be edited
|
|
|
|
// also this can definitely be done much better holy shit
|
|
|
|
for (const auto &a : data->Attachments) {
|
2020-12-26 08:57:08 +00:00
|
|
|
if (IsURLViewableImage(a.ProxyURL) && a.Width.has_value() && a.Height.has_value()) {
|
|
|
|
auto *widget = container->CreateImageComponent(a.ProxyURL, a.URL, *a.Width, *a.Height);
|
2020-11-06 07:36:08 +00:00
|
|
|
container->m_main->add(*widget);
|
2020-09-30 04:12:38 +00:00
|
|
|
} else {
|
|
|
|
auto *widget = container->CreateAttachmentComponent(a);
|
|
|
|
container->m_main->add(*widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 22:36:10 +00:00
|
|
|
// only 1?
|
|
|
|
if (data->Stickers.has_value()) {
|
|
|
|
const auto &sticker = data->Stickers.value()[0];
|
2020-11-06 07:36:08 +00:00
|
|
|
// todo: lottie, proper apng
|
2020-11-02 22:36:10 +00:00
|
|
|
if (sticker.FormatType == StickerFormatType::PNG || sticker.FormatType == StickerFormatType::APNG) {
|
|
|
|
auto *widget = container->CreateStickerComponent(sticker);
|
|
|
|
container->m_main->add(*widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 06:51:49 +00:00
|
|
|
if (data->Reactions.has_value() && data->Reactions->size() > 0) {
|
2021-01-01 07:47:03 +00:00
|
|
|
container->m_reactions_component = container->CreateReactionsComponent(*data);
|
2020-12-15 06:51:49 +00:00
|
|
|
container->m_main->add(*container->m_reactions_component);
|
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
container->UpdateAttributes();
|
2020-09-20 05:12:54 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
return container;
|
2020-09-20 05:12:54 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 04:12:38 +00:00
|
|
|
// this doesnt rly make sense
|
2020-09-26 03:02:13 +00:00
|
|
|
void ChatMessageItemContainer::UpdateContent() {
|
2020-11-24 01:34:09 +00:00
|
|
|
const auto data = Abaddon::Get().GetDiscordClient().GetMessage(ID);
|
2020-09-26 03:02:13 +00:00
|
|
|
if (m_text_component != nullptr)
|
2020-10-05 06:09:15 +00:00
|
|
|
UpdateTextComponent(m_text_component);
|
2020-08-31 00:24:02 +00:00
|
|
|
|
2020-11-07 04:57:05 +00:00
|
|
|
if (m_embed_component != nullptr) {
|
2020-09-26 03:02:13 +00:00
|
|
|
delete m_embed_component;
|
2020-11-07 04:57:05 +00:00
|
|
|
m_embed_component = nullptr;
|
|
|
|
}
|
2020-08-29 05:14:07 +00:00
|
|
|
|
2020-10-05 04:03:43 +00:00
|
|
|
if (data->Embeds.size() == 1) {
|
2020-12-26 08:57:08 +00:00
|
|
|
m_embed_component = CreateEmbedComponent(data->Embeds[0]);
|
2020-10-05 04:03:43 +00:00
|
|
|
if (m_embed_imgurl.size() > 0) {
|
|
|
|
m_signal_image_load.emit(m_embed_imgurl);
|
2020-09-26 03:02:13 +00:00
|
|
|
}
|
2021-01-01 07:47:03 +00:00
|
|
|
AttachEventHandlers(*m_embed_component);
|
2020-10-05 04:03:43 +00:00
|
|
|
m_main->add(*m_embed_component);
|
2020-09-26 03:02:13 +00:00
|
|
|
}
|
2020-08-31 00:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 22:49:22 +00:00
|
|
|
void ChatMessageItemContainer::UpdateImage(std::string url, Glib::RefPtr<Gdk::Pixbuf> buf) {
|
|
|
|
if (!buf) return;
|
|
|
|
|
|
|
|
if (m_embed_img != nullptr && m_embed_imgurl == url) {
|
|
|
|
int w, h;
|
|
|
|
m_embed_img->get_size_request(w, h);
|
|
|
|
m_embed_img->property_pixbuf() = buf->scale_simple(w, h, Gdk::INTERP_BILINEAR);
|
|
|
|
|
|
|
|
return;
|
2020-09-30 04:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 22:49:22 +00:00
|
|
|
auto it = m_img_loadmap.find(url);
|
|
|
|
if (it != m_img_loadmap.end()) {
|
2020-12-26 08:57:08 +00:00
|
|
|
const auto inw = it->second.second.first;
|
|
|
|
const auto inh = it->second.second.second;
|
|
|
|
int w, h;
|
|
|
|
GetImageDimensions(inw, inh, w, h);
|
|
|
|
it->second.first->property_pixbuf() = buf->scale_simple(w, h, Gdk::INTERP_BILINEAR);
|
2020-09-30 04:12:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 06:51:49 +00:00
|
|
|
void ChatMessageItemContainer::UpdateReactions() {
|
|
|
|
if (m_reactions_component != nullptr)
|
|
|
|
delete m_reactions_component;
|
|
|
|
|
|
|
|
const auto data = Abaddon::Get().GetDiscordClient().GetMessage(ID);
|
|
|
|
if (data->Reactions.has_value() && data->Reactions->size() > 0) {
|
2021-01-01 07:47:03 +00:00
|
|
|
m_reactions_component = CreateReactionsComponent(*data);
|
2020-12-15 06:51:49 +00:00
|
|
|
m_reactions_component->show_all();
|
|
|
|
m_main->add(*m_reactions_component);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
void ChatMessageItemContainer::UpdateAttributes() {
|
2020-11-24 01:34:09 +00:00
|
|
|
const auto data = Abaddon::Get().GetDiscordClient().GetMessage(ID);
|
|
|
|
if (!data.has_value()) return;
|
2020-09-03 05:54:40 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
const bool deleted = data->IsDeleted();
|
|
|
|
const bool edited = data->IsEdited();
|
|
|
|
|
|
|
|
if (!deleted && !edited) return;
|
2020-09-03 05:54:40 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
if (m_attrib_label == nullptr) {
|
|
|
|
m_attrib_label = Gtk::manage(new Gtk::Label);
|
|
|
|
m_attrib_label->set_halign(Gtk::ALIGN_START);
|
|
|
|
m_attrib_label->show();
|
|
|
|
m_main->add(*m_attrib_label); // todo: maybe insert markup into existing text widget's buffer if the circumstances are right (or pack horizontally)
|
|
|
|
}
|
2020-09-20 05:12:54 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
if (deleted)
|
|
|
|
m_attrib_label->set_markup("<span color='#ff0000'>[deleted]</span>");
|
|
|
|
else if (edited)
|
|
|
|
m_attrib_label->set_markup("<span color='#999999'>[edited]</span>");
|
2020-09-03 05:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 19:12:52 +00:00
|
|
|
bool ChatMessageItemContainer::EmitImageLoad(std::string url) {
|
|
|
|
m_signal_image_load.emit(url);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-30 04:12:38 +00:00
|
|
|
void ChatMessageItemContainer::AddClickHandler(Gtk::Widget *widget, std::string url) {
|
|
|
|
// clang-format off
|
|
|
|
widget->signal_button_press_event().connect([url](GdkEventButton *event) -> bool {
|
2020-12-22 07:35:57 +00:00
|
|
|
if (event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY) {
|
2020-09-30 04:12:38 +00:00
|
|
|
LaunchBrowser(url);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}, false);
|
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
Gtk::TextView *ChatMessageItemContainer::CreateTextComponent(const Message *data) {
|
|
|
|
auto *tv = Gtk::manage(new Gtk::TextView);
|
2020-09-03 05:54:40 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
tv->get_style_context()->add_class("message-text");
|
|
|
|
tv->set_can_focus(false);
|
|
|
|
tv->set_editable(false);
|
|
|
|
tv->set_wrap_mode(Gtk::WRAP_WORD_CHAR);
|
|
|
|
tv->set_halign(Gtk::ALIGN_FILL);
|
|
|
|
tv->set_hexpand(true);
|
2020-09-26 04:49:25 +00:00
|
|
|
|
2020-10-05 06:09:15 +00:00
|
|
|
UpdateTextComponent(tv);
|
|
|
|
|
|
|
|
return tv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatMessageItemContainer::UpdateTextComponent(Gtk::TextView *tv) {
|
2020-11-24 01:34:09 +00:00
|
|
|
const auto data = Abaddon::Get().GetDiscordClient().GetMessage(ID);
|
|
|
|
if (!data.has_value()) return;
|
2020-10-05 06:09:15 +00:00
|
|
|
|
2020-09-26 04:49:25 +00:00
|
|
|
auto b = tv->get_buffer();
|
2020-10-05 06:09:15 +00:00
|
|
|
b->set_text("");
|
|
|
|
Gtk::TextBuffer::iterator s, e;
|
2020-09-26 04:49:25 +00:00
|
|
|
b->get_bounds(s, e);
|
|
|
|
switch (data->Type) {
|
|
|
|
case MessageType::DEFAULT:
|
2020-12-21 07:29:00 +00:00
|
|
|
case MessageType::INLINE_REPLY:
|
2020-11-10 06:00:53 +00:00
|
|
|
b->insert(s, data->Content);
|
2021-01-01 07:39:07 +00:00
|
|
|
HandleUserMentions(b);
|
2021-01-01 07:47:03 +00:00
|
|
|
HandleLinks(*tv);
|
2020-10-11 03:38:58 +00:00
|
|
|
HandleChannelMentions(tv);
|
2021-01-01 07:47:03 +00:00
|
|
|
HandleEmojis(*tv);
|
2020-09-26 04:49:25 +00:00
|
|
|
break;
|
|
|
|
case MessageType::GUILD_MEMBER_JOIN:
|
|
|
|
b->insert_markup(s, "<span color='#999999'><i>[user joined]</i></span>");
|
|
|
|
break;
|
|
|
|
case MessageType::CHANNEL_PINNED_MESSAGE:
|
|
|
|
b->insert_markup(s, "<span color='#999999'><i>[message pinned]</i></span>");
|
|
|
|
break;
|
2020-12-23 02:18:39 +00:00
|
|
|
case MessageType::APPLICATION_COMMAND: {
|
|
|
|
if (data->Application.has_value()) {
|
|
|
|
static const auto regex = Glib::Regex::create(R"(</(.*?):(\d+)>)");
|
|
|
|
Glib::MatchInfo match;
|
|
|
|
if (regex->match(data->Content, match)) {
|
|
|
|
const auto cmd = match.fetch(1);
|
|
|
|
const auto app = data->Application->Name;
|
|
|
|
b->insert_markup(s, "<i>used <span color='#697ec4'>" + cmd + "</span> with " + app + "</i>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
2020-12-23 03:32:05 +00:00
|
|
|
case MessageType::RECIPIENT_ADD: {
|
|
|
|
const auto &adder = Abaddon::Get().GetDiscordClient().GetUser(data->Author.ID);
|
|
|
|
const auto &added = data->Mentions[0];
|
|
|
|
b->insert_markup(s, "<i><span color='#999999'><span color='#eeeeee'>" + adder->Username + "</span> added <span color='#eeeeee'>" + added.Username + "</span></span></i>");
|
|
|
|
} break;
|
|
|
|
case MessageType::RECIPIENT_REMOVE: {
|
|
|
|
const auto &adder = Abaddon::Get().GetDiscordClient().GetUser(data->Author.ID);
|
|
|
|
const auto &added = data->Mentions[0];
|
|
|
|
if (adder->ID == added.ID)
|
|
|
|
b->insert_markup(s, "<i><span color='#999999'><span color='#eeeeee'>" + adder->Username + "</span> left</span></i>");
|
|
|
|
else
|
|
|
|
b->insert_markup(s, "<i><span color='#999999'><span color='#eeeeee'>" + adder->Username + "</span> removed <span color='#eeeeee'>" + added.Username + "</span></span></i>");
|
|
|
|
} break;
|
2020-09-26 04:49:25 +00:00
|
|
|
default: break;
|
|
|
|
}
|
2020-09-26 03:02:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-26 08:57:08 +00:00
|
|
|
Gtk::Widget *ChatMessageItemContainer::CreateEmbedComponent(const EmbedData &embed) {
|
2020-09-26 03:02:13 +00:00
|
|
|
Gtk::EventBox *ev = Gtk::manage(new Gtk::EventBox);
|
|
|
|
ev->set_can_focus(true);
|
|
|
|
Gtk::Box *main = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
|
2020-09-03 05:54:40 +00:00
|
|
|
|
2020-11-24 01:34:09 +00:00
|
|
|
if (embed.Author.has_value() && embed.Author->Name.has_value()) {
|
2020-09-04 02:36:57 +00:00
|
|
|
auto *author_lbl = Gtk::manage(new Gtk::Label);
|
|
|
|
author_lbl->set_halign(Gtk::ALIGN_START);
|
|
|
|
author_lbl->set_line_wrap(true);
|
|
|
|
author_lbl->set_line_wrap_mode(Pango::WRAP_WORD_CHAR);
|
|
|
|
author_lbl->set_hexpand(false);
|
2020-11-24 01:34:09 +00:00
|
|
|
author_lbl->set_text(*embed.Author->Name);
|
2020-09-04 02:36:57 +00:00
|
|
|
author_lbl->get_style_context()->add_class("embed-author");
|
2020-09-26 03:02:13 +00:00
|
|
|
main->pack_start(*author_lbl);
|
2020-09-04 02:36:57 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 01:34:09 +00:00
|
|
|
if (embed.Title.has_value()) {
|
2020-09-03 05:54:40 +00:00
|
|
|
auto *title_label = Gtk::manage(new Gtk::Label);
|
|
|
|
title_label->set_use_markup(true);
|
2020-11-24 01:34:09 +00:00
|
|
|
title_label->set_markup("<b>" + Glib::Markup::escape_text(*embed.Title) + "</b>");
|
2020-09-03 05:54:40 +00:00
|
|
|
title_label->set_halign(Gtk::ALIGN_CENTER);
|
2020-09-03 21:44:23 +00:00
|
|
|
title_label->set_hexpand(false);
|
2020-09-09 22:32:45 +00:00
|
|
|
title_label->get_style_context()->add_class("embed-title");
|
2020-09-26 03:02:13 +00:00
|
|
|
title_label->set_single_line_mode(false);
|
|
|
|
title_label->set_line_wrap(true);
|
|
|
|
title_label->set_line_wrap_mode(Pango::WRAP_WORD_CHAR);
|
|
|
|
title_label->set_max_width_chars(50);
|
|
|
|
main->pack_start(*title_label);
|
2020-09-03 05:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-26 08:57:08 +00:00
|
|
|
if (!embed.Provider.has_value() || embed.Provider->Name != "YouTube") { // youtube link = no description
|
|
|
|
if (embed.Description.has_value()) {
|
|
|
|
auto *desc_label = Gtk::manage(new Gtk::Label);
|
|
|
|
desc_label->set_text(*embed.Description);
|
|
|
|
desc_label->set_line_wrap(true);
|
|
|
|
desc_label->set_line_wrap_mode(Pango::WRAP_WORD_CHAR);
|
|
|
|
desc_label->set_max_width_chars(50);
|
|
|
|
desc_label->set_halign(Gtk::ALIGN_START);
|
|
|
|
desc_label->set_hexpand(false);
|
|
|
|
desc_label->get_style_context()->add_class("embed-description");
|
|
|
|
main->pack_start(*desc_label);
|
|
|
|
}
|
2020-09-03 05:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
// todo: handle inline fields
|
2020-11-24 01:34:09 +00:00
|
|
|
if (embed.Fields.has_value() && embed.Fields->size() > 0) {
|
2020-09-03 22:47:45 +00:00
|
|
|
auto *flow = Gtk::manage(new Gtk::FlowBox);
|
|
|
|
flow->set_orientation(Gtk::ORIENTATION_HORIZONTAL);
|
|
|
|
flow->set_min_children_per_line(3);
|
2020-09-04 02:36:57 +00:00
|
|
|
flow->set_max_children_per_line(3);
|
2020-09-03 22:47:45 +00:00
|
|
|
flow->set_halign(Gtk::ALIGN_START);
|
|
|
|
flow->set_hexpand(false);
|
|
|
|
flow->set_column_spacing(10);
|
2020-09-04 02:36:57 +00:00
|
|
|
flow->set_selection_mode(Gtk::SELECTION_NONE);
|
2020-09-26 03:02:13 +00:00
|
|
|
main->pack_start(*flow);
|
2020-09-03 22:47:45 +00:00
|
|
|
|
2020-11-24 01:34:09 +00:00
|
|
|
for (const auto &field : *embed.Fields) {
|
2020-09-03 22:47:45 +00:00
|
|
|
auto *field_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
|
|
|
|
auto *field_lbl = Gtk::manage(new Gtk::Label);
|
|
|
|
auto *field_val = Gtk::manage(new Gtk::Label);
|
|
|
|
field_box->set_hexpand(false);
|
|
|
|
field_box->set_halign(Gtk::ALIGN_START);
|
2020-09-04 02:36:57 +00:00
|
|
|
field_box->set_valign(Gtk::ALIGN_START);
|
2020-09-03 22:47:45 +00:00
|
|
|
field_lbl->set_hexpand(false);
|
|
|
|
field_lbl->set_halign(Gtk::ALIGN_START);
|
2020-09-04 02:36:57 +00:00
|
|
|
field_lbl->set_valign(Gtk::ALIGN_START);
|
2020-09-03 22:47:45 +00:00
|
|
|
field_val->set_hexpand(false);
|
|
|
|
field_val->set_halign(Gtk::ALIGN_START);
|
2020-09-04 02:36:57 +00:00
|
|
|
field_val->set_valign(Gtk::ALIGN_START);
|
2020-09-03 22:47:45 +00:00
|
|
|
field_lbl->set_use_markup(true);
|
|
|
|
field_lbl->set_markup("<b>" + Glib::Markup::escape_text(field.Name) + "</b>");
|
|
|
|
field_lbl->set_max_width_chars(20);
|
|
|
|
field_lbl->set_line_wrap(true);
|
|
|
|
field_lbl->set_line_wrap_mode(Pango::WRAP_WORD_CHAR);
|
|
|
|
field_val->set_text(field.Value);
|
|
|
|
field_val->set_max_width_chars(20);
|
|
|
|
field_val->set_line_wrap(true);
|
|
|
|
field_val->set_line_wrap_mode(Pango::WRAP_WORD_CHAR);
|
|
|
|
field_box->pack_start(*field_lbl);
|
|
|
|
field_box->pack_start(*field_val);
|
2020-09-09 22:32:45 +00:00
|
|
|
field_lbl->get_style_context()->add_class("embed-field-title");
|
|
|
|
field_val->get_style_context()->add_class("embed-field-value");
|
2020-09-03 22:47:45 +00:00
|
|
|
flow->insert(*field_box, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 05:12:43 +00:00
|
|
|
bool is_img = embed.Image.has_value() && embed.Image->ProxyURL.has_value();
|
|
|
|
bool is_thumb = embed.Thumbnail.has_value() && embed.Thumbnail->ProxyURL.has_value();
|
|
|
|
if (is_img || is_thumb) {
|
|
|
|
auto *img = Gtk::manage(new Gtk::Image);
|
|
|
|
img->set_halign(Gtk::ALIGN_CENTER);
|
2020-12-26 08:57:08 +00:00
|
|
|
img->set_margin_top(5);
|
2020-12-11 05:12:43 +00:00
|
|
|
int w = 0, h = 0;
|
|
|
|
if (is_img)
|
2020-12-26 08:57:08 +00:00
|
|
|
GetImageDimensions(*embed.Image->Width, *embed.Image->Height, w, h, EmbedImageWidth, EmbedImageHeight);
|
2020-12-11 05:12:43 +00:00
|
|
|
else
|
2020-12-26 08:57:08 +00:00
|
|
|
GetImageDimensions(*embed.Thumbnail->Width, *embed.Thumbnail->Height, w, h, EmbedImageWidth, EmbedImageHeight);
|
2020-12-11 05:12:43 +00:00
|
|
|
img->set_size_request(w, h);
|
|
|
|
main->pack_start(*img);
|
|
|
|
m_embed_img = img;
|
|
|
|
if (is_img)
|
|
|
|
m_embed_imgurl = *embed.Image->ProxyURL;
|
|
|
|
else
|
|
|
|
m_embed_imgurl = *embed.Thumbnail->ProxyURL;
|
|
|
|
|
|
|
|
Abaddon::Get().GetImageManager().LoadFromURL(m_embed_imgurl,
|
|
|
|
sigc::mem_fun(*this, &ChatMessageItemContainer::OnEmbedImageLoad));
|
2020-09-30 04:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 01:34:09 +00:00
|
|
|
if (embed.Footer.has_value()) {
|
2020-09-04 02:36:57 +00:00
|
|
|
auto *footer_lbl = Gtk::manage(new Gtk::Label);
|
|
|
|
footer_lbl->set_halign(Gtk::ALIGN_START);
|
|
|
|
footer_lbl->set_line_wrap(true);
|
|
|
|
footer_lbl->set_line_wrap_mode(Pango::WRAP_WORD_CHAR);
|
|
|
|
footer_lbl->set_hexpand(false);
|
2020-11-24 01:34:09 +00:00
|
|
|
footer_lbl->set_text(embed.Footer->Text);
|
2020-09-04 02:36:57 +00:00
|
|
|
footer_lbl->get_style_context()->add_class("embed-footer");
|
2020-09-26 03:02:13 +00:00
|
|
|
main->pack_start(*footer_lbl);
|
2020-09-04 02:36:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
auto style = main->get_style_context();
|
2020-09-03 05:54:40 +00:00
|
|
|
|
2020-11-24 01:34:09 +00:00
|
|
|
if (embed.Color.has_value()) {
|
2020-09-03 05:54:40 +00:00
|
|
|
auto provider = Gtk::CssProvider::create(); // this seems wrong
|
2020-11-24 01:34:09 +00:00
|
|
|
std::string css = ".embed { border-left: 2px solid #" + IntToCSSColor(*embed.Color) + "; }";
|
2020-09-06 01:05:52 +00:00
|
|
|
provider->load_from_data(css);
|
2020-09-03 05:54:40 +00:00
|
|
|
style->add_provider(provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
|
|
|
}
|
|
|
|
|
|
|
|
style->add_class("embed");
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
main->set_margin_bottom(8);
|
|
|
|
main->set_hexpand(false);
|
|
|
|
main->set_hexpand(false);
|
|
|
|
main->set_halign(Gtk::ALIGN_START);
|
|
|
|
main->set_halign(Gtk::ALIGN_START);
|
2020-09-03 05:54:40 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
ev->add(*main);
|
|
|
|
ev->show_all();
|
2020-09-03 05:54:40 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
return ev;
|
|
|
|
}
|
2020-09-03 05:54:40 +00:00
|
|
|
|
2020-12-26 08:57:08 +00:00
|
|
|
Gtk::Widget *ChatMessageItemContainer::CreateImageComponent(const std::string &proxy_url, const std::string &url, int inw, int inh) {
|
2020-09-30 04:12:38 +00:00
|
|
|
int w, h;
|
2020-12-26 08:57:08 +00:00
|
|
|
GetImageDimensions(inw, inh, w, h);
|
2020-09-30 04:12:38 +00:00
|
|
|
|
2020-11-06 07:36:08 +00:00
|
|
|
Gtk::EventBox *ev = Gtk::manage(new Gtk::EventBox);
|
2020-09-30 04:12:38 +00:00
|
|
|
Gtk::Image *widget = Gtk::manage(new Gtk::Image);
|
2020-11-06 07:36:08 +00:00
|
|
|
ev->add(*widget);
|
2020-09-30 04:12:38 +00:00
|
|
|
widget->set_halign(Gtk::ALIGN_START);
|
|
|
|
widget->set_size_request(w, h);
|
|
|
|
|
2021-01-01 07:47:03 +00:00
|
|
|
AttachEventHandlers(*ev);
|
2020-12-26 08:57:08 +00:00
|
|
|
AddClickHandler(ev, url);
|
2021-01-01 07:47:03 +00:00
|
|
|
HandleImage(w, h, *widget, proxy_url);
|
2020-09-30 04:12:38 +00:00
|
|
|
|
2020-11-06 07:36:08 +00:00
|
|
|
return ev;
|
2020-09-30 04:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 07:36:08 +00:00
|
|
|
Gtk::Widget *ChatMessageItemContainer::CreateAttachmentComponent(const AttachmentData &data) {
|
2020-09-30 04:12:38 +00:00
|
|
|
auto *ev = Gtk::manage(new Gtk::EventBox);
|
|
|
|
auto *btn = Gtk::manage(new Gtk::Label(data.Filename + " " + HumanReadableBytes(data.Bytes))); // Gtk::LinkButton flat out doesn't work :D
|
2020-12-15 05:30:11 +00:00
|
|
|
ev->set_hexpand(false);
|
|
|
|
ev->set_halign(Gtk::ALIGN_START);
|
2020-11-06 07:36:08 +00:00
|
|
|
ev->get_style_context()->add_class("message-attachment-box");
|
2020-09-30 04:12:38 +00:00
|
|
|
ev->add(*btn);
|
2020-11-06 07:36:08 +00:00
|
|
|
|
2021-01-01 07:47:03 +00:00
|
|
|
AttachEventHandlers(*ev);
|
2020-11-06 07:36:08 +00:00
|
|
|
AddClickHandler(ev, data.URL);
|
|
|
|
|
|
|
|
return ev;
|
2020-09-30 04:12:38 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 07:41:49 +00:00
|
|
|
Gtk::Widget *ChatMessageItemContainer::CreateStickerComponent(const StickerData &data) {
|
2020-11-02 22:36:10 +00:00
|
|
|
auto *box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
|
|
|
|
auto *imgw = Gtk::manage(new Gtk::Image);
|
2020-11-06 07:36:08 +00:00
|
|
|
box->add(*imgw);
|
2020-11-02 22:36:10 +00:00
|
|
|
auto &img = Abaddon::Get().GetImageManager();
|
|
|
|
|
|
|
|
if (data.FormatType == StickerFormatType::PNG || data.FormatType == StickerFormatType::APNG) {
|
2021-01-10 04:33:31 +00:00
|
|
|
auto cb = [this, imgw](const Glib::RefPtr<Gdk::Pixbuf> &pixbuf) {
|
2020-11-02 22:36:10 +00:00
|
|
|
imgw->property_pixbuf() = pixbuf;
|
2021-01-10 04:33:31 +00:00
|
|
|
};
|
|
|
|
img.LoadFromURL(data.GetURL(), sigc::track_obj(cb, *imgw));
|
2020-11-02 22:36:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 07:47:03 +00:00
|
|
|
AttachEventHandlers(*box);
|
2020-11-02 22:36:10 +00:00
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
2021-01-01 07:47:03 +00:00
|
|
|
Gtk::Widget *ChatMessageItemContainer::CreateReactionsComponent(const Message &data) {
|
2020-12-15 06:51:49 +00:00
|
|
|
auto *flow = Gtk::manage(new Gtk::FlowBox);
|
|
|
|
flow->set_orientation(Gtk::ORIENTATION_HORIZONTAL);
|
|
|
|
flow->set_min_children_per_line(5);
|
|
|
|
flow->set_max_children_per_line(20);
|
|
|
|
flow->set_halign(Gtk::ALIGN_START);
|
|
|
|
flow->set_hexpand(false);
|
|
|
|
flow->set_column_spacing(2);
|
|
|
|
flow->set_selection_mode(Gtk::SELECTION_NONE);
|
|
|
|
|
|
|
|
auto &imgr = Abaddon::Get().GetImageManager();
|
|
|
|
auto &emojis = Abaddon::Get().GetEmojis();
|
|
|
|
const auto &placeholder = imgr.GetPlaceholder(16);
|
|
|
|
|
2021-01-01 07:47:03 +00:00
|
|
|
for (const auto &reaction : *data.Reactions) {
|
2020-12-15 06:51:49 +00:00
|
|
|
auto *ev = Gtk::manage(new Gtk::EventBox);
|
|
|
|
auto *box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
|
|
|
|
box->get_style_context()->add_class("reaction-box");
|
|
|
|
ev->add(*box);
|
|
|
|
flow->add(*ev);
|
|
|
|
|
|
|
|
bool is_stock = !reaction.Emoji.ID.IsValid();
|
|
|
|
|
|
|
|
bool has_reacted = reaction.HasReactedWith;
|
|
|
|
if (has_reacted)
|
|
|
|
box->get_style_context()->add_class("reacted");
|
|
|
|
|
|
|
|
ev->signal_button_press_event().connect([this, has_reacted, is_stock, reaction](GdkEventButton *event) -> bool {
|
|
|
|
if (event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY) {
|
|
|
|
Glib::ustring param; // escaped in client
|
|
|
|
if (is_stock)
|
|
|
|
param = reaction.Emoji.Name;
|
|
|
|
else
|
|
|
|
param = std::to_string(reaction.Emoji.ID);
|
|
|
|
if (has_reacted)
|
|
|
|
m_signal_action_reaction_remove.emit(param);
|
|
|
|
else
|
|
|
|
m_signal_action_reaction_add.emit(param);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
ev->signal_realize().connect([ev]() {
|
|
|
|
auto window = ev->get_window();
|
|
|
|
auto display = window->get_display();
|
|
|
|
auto cursor = Gdk::Cursor::create(display, "pointer");
|
|
|
|
window->set_cursor(cursor);
|
|
|
|
});
|
|
|
|
|
|
|
|
// image
|
|
|
|
if (is_stock) { // unicode/stock
|
|
|
|
const auto &pb = emojis.GetPixBuf(reaction.Emoji.Name);
|
2020-12-15 07:45:37 +00:00
|
|
|
Gtk::Image *img;
|
|
|
|
if (pb)
|
|
|
|
img = Gtk::manage(new Gtk::Image(pb->scale_simple(16, 16, Gdk::INTERP_BILINEAR)));
|
|
|
|
else
|
|
|
|
img = Gtk::manage(new Gtk::Image(placeholder));
|
2020-12-15 06:51:49 +00:00
|
|
|
img->set_can_focus(false);
|
|
|
|
box->add(*img);
|
|
|
|
} else { // custom
|
|
|
|
const auto &pb = imgr.GetFromURLIfCached(reaction.Emoji.GetURL());
|
|
|
|
Gtk::Image *img;
|
|
|
|
if (pb) {
|
|
|
|
img = Gtk::manage(new Gtk::Image(pb->scale_simple(16, 16, Gdk::INTERP_BILINEAR)));
|
|
|
|
} else {
|
|
|
|
img = Gtk::manage(new Gtk::Image(placeholder));
|
|
|
|
imgr.LoadFromURL(reaction.Emoji.GetURL(), sigc::bind<0>(sigc::mem_fun(*this, &ChatMessageItemContainer::ReactionUpdateImage), img));
|
|
|
|
}
|
|
|
|
img->set_can_focus(false);
|
|
|
|
box->add(*img);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *lbl = Gtk::manage(new Gtk::Label(std::to_string(reaction.Count)));
|
|
|
|
lbl->set_margin_left(5);
|
|
|
|
lbl->get_style_context()->add_class("reaction-count");
|
|
|
|
box->add(*lbl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return flow;
|
|
|
|
}
|
|
|
|
|
2021-01-01 07:47:03 +00:00
|
|
|
Gtk::Widget *ChatMessageItemContainer::CreateReplyComponent(const Message &data) {
|
2021-01-01 07:39:07 +00:00
|
|
|
auto *box = Gtk::manage(new Gtk::Box);
|
|
|
|
auto *lbl = Gtk::manage(new Gtk::Label);
|
|
|
|
lbl->set_single_line_mode(true);
|
|
|
|
lbl->set_line_wrap(false);
|
|
|
|
lbl->set_use_markup(true);
|
|
|
|
lbl->set_ellipsize(Pango::ELLIPSIZE_END);
|
|
|
|
lbl->get_style_context()->add_class("message-text"); // good idea?
|
|
|
|
lbl->get_style_context()->add_class("message-reply");
|
|
|
|
box->add(*lbl);
|
|
|
|
|
2021-01-01 07:47:03 +00:00
|
|
|
if (data.ReferencedMessage.has_value()) {
|
|
|
|
if (data.ReferencedMessage.value().get() == nullptr) {
|
2021-01-01 07:39:07 +00:00
|
|
|
lbl->set_markup("<i>deleted message</i>");
|
|
|
|
} else {
|
2021-01-01 07:47:03 +00:00
|
|
|
const auto &referenced = *data.ReferencedMessage.value().get();
|
2021-01-01 07:39:07 +00:00
|
|
|
Glib::ustring text;
|
|
|
|
if (referenced.Content == "") {
|
|
|
|
if (referenced.Attachments.size() > 0) {
|
|
|
|
text = "<i>attachment</i>";
|
|
|
|
} else if (referenced.Embeds.size() > 0) {
|
|
|
|
text = "<i>embed</i>";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto buf = Gtk::TextBuffer::create();
|
|
|
|
Gtk::TextBuffer::iterator start, end;
|
|
|
|
buf->get_bounds(start, end);
|
|
|
|
buf->set_text(referenced.Content);
|
|
|
|
CleanupEmojis(buf);
|
|
|
|
HandleUserMentions(buf);
|
|
|
|
HandleChannelMentions(buf);
|
|
|
|
text = Glib::Markup::escape_text(buf->get_text());
|
|
|
|
}
|
|
|
|
// getting markup out of a textbuffer seems like something that to me should be really simple
|
|
|
|
// but actually is horribly annoying. replies won't have mention colors because you can't do this
|
|
|
|
// also no emojis because idk how to make a textview act like a label
|
|
|
|
// which of course would not be an issue if i could figure out how to get fonts to work on this god-forsaken framework
|
|
|
|
// oh well
|
|
|
|
// but ill manually get colors for the user who is being replied to
|
|
|
|
const auto &discord = Abaddon::Get().GetDiscordClient();
|
|
|
|
if (referenced.GuildID.has_value()) {
|
|
|
|
const auto role_id = discord.GetMemberHoistedRole(*referenced.GuildID, referenced.Author.ID, true);
|
|
|
|
if (role_id.IsValid()) {
|
|
|
|
const auto role = discord.GetRole(role_id);
|
|
|
|
if (role.has_value()) {
|
|
|
|
const auto author = discord.GetUser(referenced.Author.ID);
|
|
|
|
// clang-format off
|
|
|
|
lbl->set_markup(
|
|
|
|
"<b><span color=\"#" + IntToCSSColor(role->Color) + "\">"
|
|
|
|
+ Glib::Markup::escape_text(author->Username + "#" + author->Discriminator)
|
|
|
|
+ "</span></b>: "
|
|
|
|
+ text
|
|
|
|
);
|
|
|
|
// clang-format on
|
|
|
|
return box;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto author = discord.GetUser(referenced.Author.ID);
|
|
|
|
lbl->set_markup("<b>" + Glib::Markup::escape_text(author->Username + "#" + author->Discriminator) + "</b>: " + text);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lbl->set_markup("<i>reply unavailable</i>");
|
|
|
|
}
|
|
|
|
|
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
2020-12-15 06:51:49 +00:00
|
|
|
void ChatMessageItemContainer::ReactionUpdateImage(Gtk::Image *img, const Glib::RefPtr<Gdk::Pixbuf> &pb) {
|
|
|
|
img->property_pixbuf() = pb->scale_simple(16, 16, Gdk::INTERP_BILINEAR);
|
|
|
|
}
|
|
|
|
|
2021-01-01 07:47:03 +00:00
|
|
|
void ChatMessageItemContainer::HandleImage(int w, int h, Gtk::Image &img, std::string url) {
|
|
|
|
m_img_loadmap[url] = { &img, { w, h } };
|
2020-09-30 19:12:52 +00:00
|
|
|
// ask the chatwindow to call UpdateImage because dealing with lifetimes sucks
|
|
|
|
Glib::signal_idle().connect(sigc::bind(sigc::mem_fun(*this, &ChatMessageItemContainer::EmitImageLoad), url));
|
2020-09-30 04:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 05:12:43 +00:00
|
|
|
void ChatMessageItemContainer::OnEmbedImageLoad(const Glib::RefPtr<Gdk::Pixbuf> &pixbuf) {
|
|
|
|
int w, h;
|
|
|
|
m_embed_img->get_size_request(w, h);
|
|
|
|
m_embed_img->property_pixbuf() = pixbuf->scale_simple(w, h, Gdk::INTERP_BILINEAR);
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:54:28 +00:00
|
|
|
Glib::ustring ChatMessageItemContainer::GetText(const Glib::RefPtr<Gtk::TextBuffer> &buf) {
|
2020-10-24 23:42:06 +00:00
|
|
|
Gtk::TextBuffer::iterator a, b;
|
|
|
|
buf->get_bounds(a, b);
|
|
|
|
auto slice = buf->get_slice(a, b, true);
|
|
|
|
return slice;
|
|
|
|
}
|
|
|
|
|
2020-12-26 08:57:08 +00:00
|
|
|
bool ChatMessageItemContainer::IsEmbedImageOnly(const EmbedData &data) {
|
|
|
|
if (!data.Thumbnail.has_value()) return false;
|
|
|
|
if (data.Author.has_value()) return false;
|
|
|
|
if (data.Description.has_value()) return false;
|
|
|
|
if (data.Fields.has_value()) return false;
|
|
|
|
if (data.Footer.has_value()) return false;
|
|
|
|
if (data.Image.has_value()) return false;
|
|
|
|
if (data.Timestamp.has_value()) return false;
|
|
|
|
return data.Thumbnail->ProxyURL.has_value() && data.Thumbnail->URL.has_value() && data.Thumbnail->Width.has_value() && data.Thumbnail->Height.has_value();
|
|
|
|
}
|
|
|
|
|
2021-01-01 07:39:07 +00:00
|
|
|
void ChatMessageItemContainer::HandleUserMentions(Glib::RefPtr<Gtk::TextBuffer> buf) {
|
2020-10-13 00:20:37 +00:00
|
|
|
constexpr static const auto mentions_regex = R"(<@!?(\d+)>)";
|
2020-10-05 06:09:15 +00:00
|
|
|
|
2020-10-30 19:54:28 +00:00
|
|
|
static auto rgx = Glib::Regex::create(mentions_regex);
|
2020-10-05 06:09:15 +00:00
|
|
|
|
2020-10-30 19:54:28 +00:00
|
|
|
Glib::ustring text = GetText(buf);
|
2020-10-13 00:20:37 +00:00
|
|
|
const auto &discord = Abaddon::Get().GetDiscordClient();
|
2020-10-05 06:09:15 +00:00
|
|
|
|
2020-10-30 19:54:28 +00:00
|
|
|
int startpos = 0;
|
|
|
|
Glib::MatchInfo match;
|
|
|
|
while (rgx->match(text, startpos, match)) {
|
|
|
|
int mstart, mend;
|
|
|
|
if (!match.fetch_pos(0, mstart, mend)) break;
|
|
|
|
const Glib::ustring user_id = match.fetch(1);
|
2020-11-20 00:18:59 +00:00
|
|
|
const auto user = discord.GetUser(user_id);
|
2020-12-10 08:50:40 +00:00
|
|
|
const auto channel = discord.GetChannel(ChannelID);
|
|
|
|
if (!user.has_value() || !channel.has_value()) {
|
2020-10-30 19:54:28 +00:00
|
|
|
startpos = mend;
|
2020-10-13 00:20:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:54:28 +00:00
|
|
|
Glib::ustring replacement;
|
2020-10-05 06:09:15 +00:00
|
|
|
|
|
|
|
if (channel->Type == ChannelType::DM || channel->Type == ChannelType::GROUP_DM)
|
2020-10-13 00:20:37 +00:00
|
|
|
replacement = "<b>@" + Glib::Markup::escape_text(user->Username) + "#" + user->Discriminator + "</b>";
|
|
|
|
else {
|
2020-12-10 08:50:40 +00:00
|
|
|
const auto role_id = user->GetHoistedRole(*channel->GuildID, true);
|
2020-11-24 01:34:09 +00:00
|
|
|
const auto role = discord.GetRole(role_id);
|
|
|
|
if (!role.has_value())
|
2020-10-13 00:20:37 +00:00
|
|
|
replacement = "<b>@" + Glib::Markup::escape_text(user->Username) + "#" + user->Discriminator + "</b>";
|
|
|
|
else
|
|
|
|
replacement = "<b><span color=\"#" + IntToCSSColor(role->Color) + "\">@" + Glib::Markup::escape_text(user->Username) + "#" + user->Discriminator + "</span></b>";
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:54:28 +00:00
|
|
|
// regex returns byte positions and theres no straightforward way in the c++ bindings to deal with that :(
|
|
|
|
const auto chars_start = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mstart);
|
|
|
|
const auto chars_end = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mend);
|
|
|
|
const auto start_it = buf->get_iter_at_offset(chars_start);
|
|
|
|
const auto end_it = buf->get_iter_at_offset(chars_end);
|
2020-10-05 06:09:15 +00:00
|
|
|
|
2020-10-30 19:54:28 +00:00
|
|
|
auto it = buf->erase(start_it, end_it);
|
2020-10-13 00:20:37 +00:00
|
|
|
buf->insert_markup(it, replacement);
|
2020-10-05 06:09:15 +00:00
|
|
|
|
2020-10-30 19:54:28 +00:00
|
|
|
text = GetText(buf);
|
|
|
|
startpos = 0;
|
2020-10-13 00:20:37 +00:00
|
|
|
}
|
2020-10-05 06:09:15 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 07:47:03 +00:00
|
|
|
void ChatMessageItemContainer::HandleStockEmojis(Gtk::TextView &tv) {
|
|
|
|
Abaddon::Get().GetEmojis().ReplaceEmojis(tv.get_buffer(), EmojiSize);
|
2020-10-24 23:42:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 07:47:03 +00:00
|
|
|
void ChatMessageItemContainer::HandleCustomEmojis(Gtk::TextView &tv) {
|
2020-10-24 23:42:06 +00:00
|
|
|
static auto rgx = Glib::Regex::create(R"(<a?:([\w\d_]+):(\d+)>)");
|
|
|
|
|
|
|
|
auto &img = Abaddon::Get().GetImageManager();
|
|
|
|
|
2021-01-01 07:47:03 +00:00
|
|
|
auto buf = tv.get_buffer();
|
2020-10-30 19:54:28 +00:00
|
|
|
auto text = GetText(buf);
|
2020-10-24 23:42:06 +00:00
|
|
|
|
|
|
|
Glib::MatchInfo match;
|
|
|
|
int startpos = 0;
|
|
|
|
while (rgx->match(text, startpos, match)) {
|
|
|
|
int mstart, mend;
|
|
|
|
if (!match.fetch_pos(0, mstart, mend)) break;
|
2020-12-22 06:23:56 +00:00
|
|
|
const bool is_animated = match.fetch(0)[1] == 'a';
|
|
|
|
const bool show_animations = Abaddon::Get().GetSettings().GetShowAnimations();
|
2020-10-30 19:54:28 +00:00
|
|
|
|
|
|
|
const auto chars_start = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mstart);
|
|
|
|
const auto chars_end = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mend);
|
|
|
|
auto start_it = buf->get_iter_at_offset(chars_start);
|
|
|
|
auto end_it = buf->get_iter_at_offset(chars_end);
|
|
|
|
|
2020-10-24 23:42:06 +00:00
|
|
|
startpos = mend;
|
2020-12-22 06:23:56 +00:00
|
|
|
if (is_animated && show_animations) {
|
2021-01-12 05:11:59 +00:00
|
|
|
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<Gdk::PixbufAnimation> &pixbuf) {
|
|
|
|
auto start_it = mark_start->get_iter();
|
|
|
|
auto end_it = mark_end->get_iter();
|
|
|
|
end_it.forward_char();
|
|
|
|
buf->delete_mark(mark_start);
|
|
|
|
buf->delete_mark(mark_end);
|
|
|
|
auto it = buf->erase(start_it, end_it);
|
2020-12-22 06:23:56 +00:00
|
|
|
const auto anchor = buf->create_child_anchor(it);
|
|
|
|
auto img = Gtk::manage(new Gtk::Image(pixbuf));
|
|
|
|
img->show();
|
2021-01-01 07:47:03 +00:00
|
|
|
tv.add_child_at_anchor(*img, anchor);
|
2021-01-12 05:11:59 +00:00
|
|
|
};
|
|
|
|
img.LoadAnimationFromURL(EmojiData::URLFromID(match.fetch(2), "gif"), EmojiSize, EmojiSize, sigc::track_obj(cb, tv));
|
2020-10-24 23:42:06 +00:00
|
|
|
} else {
|
2021-01-12 05:11:59 +00:00
|
|
|
// can't erase before pixbuf is ready or else marks that are in the same pos get mixed up
|
|
|
|
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<Gdk::Pixbuf> &pixbuf) {
|
|
|
|
auto start_it = mark_start->get_iter();
|
|
|
|
auto end_it = mark_end->get_iter();
|
|
|
|
end_it.forward_char();
|
|
|
|
buf->delete_mark(mark_start);
|
|
|
|
buf->delete_mark(mark_end);
|
|
|
|
auto it = buf->erase(start_it, end_it);
|
2020-12-22 06:23:56 +00:00
|
|
|
buf->insert_pixbuf(it, pixbuf->scale_simple(EmojiSize, EmojiSize, Gdk::INTERP_BILINEAR));
|
2021-01-12 05:11:59 +00:00
|
|
|
};
|
|
|
|
img.LoadFromURL(EmojiData::URLFromID(match.fetch(2)), sigc::track_obj(cb, tv));
|
2020-10-24 23:42:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 19:54:28 +00:00
|
|
|
text = GetText(buf);
|
2020-10-24 23:42:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 07:47:03 +00:00
|
|
|
void ChatMessageItemContainer::HandleEmojis(Gtk::TextView &tv) {
|
2020-12-17 06:40:02 +00:00
|
|
|
static bool emojis = Abaddon::Get().GetSettings().GetShowEmojis();
|
2020-10-24 23:42:06 +00:00
|
|
|
if (emojis) {
|
|
|
|
HandleStockEmojis(tv);
|
|
|
|
HandleCustomEmojis(tv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 07:39:07 +00:00
|
|
|
void ChatMessageItemContainer::CleanupEmojis(Glib::RefPtr<Gtk::TextBuffer> buf) {
|
|
|
|
static auto rgx = Glib::Regex::create(R"(<a?:([\w\d_]+):(\d+)>)");
|
2020-10-10 03:14:57 +00:00
|
|
|
|
2021-01-01 07:39:07 +00:00
|
|
|
auto &img = Abaddon::Get().GetImageManager();
|
|
|
|
|
|
|
|
auto text = GetText(buf);
|
|
|
|
|
|
|
|
Glib::MatchInfo match;
|
|
|
|
int startpos = 0;
|
|
|
|
while (rgx->match(text, startpos, match)) {
|
|
|
|
int mstart, mend;
|
|
|
|
if (!match.fetch_pos(0, mstart, mend)) break;
|
|
|
|
|
|
|
|
const auto chars_start = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mstart);
|
|
|
|
const auto chars_end = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mend);
|
|
|
|
auto start_it = buf->get_iter_at_offset(chars_start);
|
|
|
|
auto end_it = buf->get_iter_at_offset(chars_end);
|
|
|
|
|
|
|
|
startpos = mend;
|
|
|
|
const auto it = buf->erase(start_it, end_it);
|
|
|
|
const int alen = text.size();
|
|
|
|
text = GetText(buf);
|
|
|
|
const int blen = text.size();
|
|
|
|
startpos -= (alen - blen);
|
|
|
|
|
|
|
|
buf->insert(it, ":" + match.fetch(1) + ":");
|
|
|
|
|
|
|
|
text = GetText(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatMessageItemContainer::HandleChannelMentions(Glib::RefPtr<Gtk::TextBuffer> buf) {
|
|
|
|
static auto rgx = Glib::Regex::create(R"(<#(\d+)>)");
|
2020-10-10 03:14:57 +00:00
|
|
|
|
2020-10-30 19:54:28 +00:00
|
|
|
Glib::ustring text = GetText(buf);
|
2020-10-11 03:38:58 +00:00
|
|
|
|
|
|
|
const auto &discord = Abaddon::Get().GetDiscordClient();
|
2020-10-10 03:14:57 +00:00
|
|
|
|
2020-10-30 19:54:28 +00:00
|
|
|
int startpos = 0;
|
|
|
|
Glib::MatchInfo match;
|
|
|
|
while (rgx->match(text, startpos, match)) {
|
|
|
|
int mstart, mend;
|
|
|
|
match.fetch_pos(0, mstart, mend);
|
|
|
|
std::string channel_id = match.fetch(1);
|
2020-12-10 08:50:40 +00:00
|
|
|
const auto chan = discord.GetChannel(channel_id);
|
|
|
|
if (!chan.has_value()) {
|
2020-10-30 19:54:28 +00:00
|
|
|
startpos = mend;
|
2020-10-11 03:38:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto tag = buf->create_tag();
|
|
|
|
m_channel_tagmap[tag] = channel_id;
|
|
|
|
tag->property_weight() = Pango::WEIGHT_BOLD;
|
|
|
|
|
2020-10-30 19:54:28 +00:00
|
|
|
const auto chars_start = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mstart);
|
|
|
|
const auto chars_end = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mend);
|
|
|
|
const auto erase_from = buf->get_iter_at_offset(chars_start);
|
|
|
|
const auto erase_to = buf->get_iter_at_offset(chars_end);
|
2020-10-11 03:38:58 +00:00
|
|
|
auto it = buf->erase(erase_from, erase_to);
|
2020-12-10 08:50:40 +00:00
|
|
|
const std::string replacement = "#" + *chan->Name;
|
|
|
|
it = buf->insert_with_tag(it, "#" + *chan->Name, tag);
|
2020-10-11 03:38:58 +00:00
|
|
|
|
|
|
|
// rescan the whole thing so i dont have to deal with fixing match positions
|
2020-10-30 19:54:28 +00:00
|
|
|
text = GetText(buf);
|
|
|
|
startpos = 0;
|
2020-10-11 03:38:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 07:39:07 +00:00
|
|
|
void ChatMessageItemContainer::HandleChannelMentions(Gtk::TextView *tv) {
|
|
|
|
tv->signal_button_press_event().connect(sigc::mem_fun(*this, &ChatMessageItemContainer::OnClickChannel), false);
|
|
|
|
HandleChannelMentions(tv->get_buffer());
|
|
|
|
}
|
|
|
|
|
2020-10-11 03:38:58 +00:00
|
|
|
// a lot of repetition here so there should probably just be one slot for textview's button-press
|
|
|
|
bool ChatMessageItemContainer::OnClickChannel(GdkEventButton *ev) {
|
|
|
|
if (m_text_component == nullptr) return false;
|
2020-12-22 07:35:57 +00:00
|
|
|
if (ev->type != GDK_BUTTON_PRESS) return false;
|
2020-10-11 03:38:58 +00:00
|
|
|
if (ev->button != GDK_BUTTON_PRIMARY) return false;
|
|
|
|
|
|
|
|
auto buf = m_text_component->get_buffer();
|
|
|
|
Gtk::TextBuffer::iterator start, end;
|
|
|
|
buf->get_selection_bounds(start, end); // no open if selection
|
|
|
|
if (start.get_offset() != end.get_offset())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int x, y;
|
|
|
|
m_text_component->window_to_buffer_coords(Gtk::TEXT_WINDOW_WIDGET, ev->x, 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) {
|
|
|
|
const auto it = m_channel_tagmap.find(tag);
|
|
|
|
if (it != m_channel_tagmap.end()) {
|
|
|
|
m_signal_action_channel_click.emit(it->second);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-06 05:20:31 +00:00
|
|
|
void ChatMessageItemContainer::on_link_menu_copy() {
|
|
|
|
Gtk::Clipboard::get()->set_text(m_selected_link);
|
|
|
|
}
|
|
|
|
|
2021-01-01 07:47:03 +00:00
|
|
|
void ChatMessageItemContainer::HandleLinks(Gtk::TextView &tv) {
|
2020-10-30 19:54:28 +00:00
|
|
|
const auto rgx = Glib::Regex::create(R"(\bhttps?:\/\/[^\s]+\.[^\s]+\b)");
|
2020-10-11 03:38:58 +00:00
|
|
|
|
2021-01-01 07:47:03 +00:00
|
|
|
tv.signal_button_press_event().connect(sigc::mem_fun(*this, &ChatMessageItemContainer::OnLinkClick), false);
|
2020-10-11 03:38:58 +00:00
|
|
|
|
2021-01-01 07:47:03 +00:00
|
|
|
auto buf = tv.get_buffer();
|
2020-10-30 19:54:28 +00:00
|
|
|
Glib::ustring text = GetText(buf);
|
2020-10-10 03:14:57 +00:00
|
|
|
|
|
|
|
// i'd like to let this be done thru css like .message-link { color: #bitch; } but idk how
|
2020-12-17 06:40:02 +00:00
|
|
|
static auto link_color = Abaddon::Get().GetSettings().GetLinkColor();
|
2020-10-10 03:14:57 +00:00
|
|
|
|
2020-10-30 19:54:28 +00:00
|
|
|
int startpos = 0;
|
|
|
|
Glib::MatchInfo match;
|
|
|
|
while (rgx->match(text, startpos, match)) {
|
|
|
|
int mstart, mend;
|
|
|
|
match.fetch_pos(0, mstart, mend);
|
|
|
|
std::string link = match.fetch(0);
|
2020-10-10 03:14:57 +00:00
|
|
|
auto tag = buf->create_tag();
|
2020-10-11 03:38:58 +00:00
|
|
|
m_link_tagmap[tag] = link;
|
2020-10-10 03:14:57 +00:00
|
|
|
tag->property_foreground_rgba() = Gdk::RGBA(link_color);
|
2020-11-06 05:20:31 +00:00
|
|
|
tag->set_property("underline", 1); // stupid workaround for vcpkg bug (i think)
|
2020-10-10 03:14:57 +00:00
|
|
|
|
2020-10-30 19:54:28 +00:00
|
|
|
const auto chars_start = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mstart);
|
|
|
|
const auto chars_end = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mend);
|
|
|
|
const auto erase_from = buf->get_iter_at_offset(chars_start);
|
|
|
|
const auto erase_to = buf->get_iter_at_offset(chars_end);
|
2020-10-11 03:38:58 +00:00
|
|
|
auto it = buf->erase(erase_from, erase_to);
|
|
|
|
it = buf->insert_with_tag(it, link, tag);
|
2020-10-10 03:14:57 +00:00
|
|
|
|
2020-10-30 19:54:28 +00:00
|
|
|
startpos = mend;
|
2020-10-10 03:14:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatMessageItemContainer::OnLinkClick(GdkEventButton *ev) {
|
|
|
|
if (m_text_component == nullptr) return false;
|
2020-12-22 07:35:57 +00:00
|
|
|
if (ev->type != GDK_BUTTON_PRESS) return false;
|
2020-11-06 05:20:31 +00:00
|
|
|
if (ev->button != GDK_BUTTON_PRIMARY && ev->button != GDK_BUTTON_SECONDARY) return false;
|
2020-10-10 03:14:57 +00:00
|
|
|
|
|
|
|
auto buf = m_text_component->get_buffer();
|
|
|
|
Gtk::TextBuffer::iterator start, end;
|
|
|
|
buf->get_selection_bounds(start, end); // no open if selection
|
|
|
|
if (start.get_offset() != end.get_offset())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int x, y;
|
|
|
|
m_text_component->window_to_buffer_coords(Gtk::TEXT_WINDOW_WIDGET, ev->x, 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) {
|
2020-10-11 03:38:58 +00:00
|
|
|
const auto it = m_link_tagmap.find(tag);
|
|
|
|
if (it != m_link_tagmap.end()) {
|
2020-11-06 05:20:31 +00:00
|
|
|
if (ev->button == GDK_BUTTON_PRIMARY) {
|
|
|
|
LaunchBrowser(it->second);
|
|
|
|
return true;
|
|
|
|
} else if (ev->button == GDK_BUTTON_SECONDARY) {
|
|
|
|
m_selected_link = it->second;
|
|
|
|
m_link_menu.popup_at_pointer(reinterpret_cast<GdkEvent *>(ev));
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-10 03:14:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
void ChatMessageItemContainer::ShowMenu(GdkEvent *event) {
|
|
|
|
const auto &client = Abaddon::Get().GetDiscordClient();
|
2020-11-24 01:34:09 +00:00
|
|
|
const auto data = client.GetMessage(ID);
|
2020-09-26 03:02:13 +00:00
|
|
|
if (data->IsDeleted()) {
|
|
|
|
m_menu_delete_message->set_sensitive(false);
|
|
|
|
m_menu_edit_message->set_sensitive(false);
|
|
|
|
} else {
|
|
|
|
const bool can_edit = client.GetUserData().ID == data->Author.ID;
|
|
|
|
const bool can_delete = can_edit || client.HasChannelPermission(client.GetUserData().ID, ChannelID, Permission::MANAGE_MESSAGES);
|
|
|
|
m_menu_delete_message->set_sensitive(can_delete);
|
|
|
|
m_menu_edit_message->set_sensitive(can_edit);
|
2020-09-03 05:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
m_menu.popup_at_pointer(event);
|
2020-09-03 05:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
void ChatMessageItemContainer::on_menu_copy_id() {
|
|
|
|
Gtk::Clipboard::get()->set_text(std::to_string(ID));
|
2020-09-03 05:54:40 +00:00
|
|
|
}
|
2020-09-22 01:01:32 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
void ChatMessageItemContainer::on_menu_delete_message() {
|
|
|
|
m_signal_action_delete.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatMessageItemContainer::on_menu_edit_message() {
|
|
|
|
m_signal_action_edit.emit();
|
|
|
|
}
|
|
|
|
|
2020-10-13 03:55:52 +00:00
|
|
|
void ChatMessageItemContainer::on_menu_copy_content() {
|
2020-11-24 01:34:09 +00:00
|
|
|
const auto msg = Abaddon::Get().GetDiscordClient().GetMessage(ID);
|
|
|
|
if (msg.has_value())
|
2020-10-13 03:55:52 +00:00
|
|
|
Gtk::Clipboard::get()->set_text(msg->Content);
|
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
ChatMessageItemContainer::type_signal_action_delete ChatMessageItemContainer::signal_action_delete() {
|
|
|
|
return m_signal_action_delete;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChatMessageItemContainer::type_signal_action_edit ChatMessageItemContainer::signal_action_edit() {
|
|
|
|
return m_signal_action_edit;
|
|
|
|
}
|
|
|
|
|
2020-10-11 03:38:58 +00:00
|
|
|
ChatMessageItemContainer::type_signal_channel_click ChatMessageItemContainer::signal_action_channel_click() {
|
|
|
|
return m_signal_action_channel_click;
|
|
|
|
}
|
|
|
|
|
2020-12-15 06:51:49 +00:00
|
|
|
ChatMessageItemContainer::type_signal_action_reaction_add ChatMessageItemContainer::signal_action_reaction_add() {
|
|
|
|
return m_signal_action_reaction_add;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChatMessageItemContainer::type_signal_action_reaction_remove ChatMessageItemContainer::signal_action_reaction_remove() {
|
|
|
|
return m_signal_action_reaction_remove;
|
|
|
|
}
|
|
|
|
|
2020-09-30 04:12:38 +00:00
|
|
|
ChatMessageItemContainer::type_signal_image_load ChatMessageItemContainer::signal_image_load() {
|
|
|
|
return m_signal_image_load;
|
|
|
|
}
|
|
|
|
|
2021-01-01 07:47:03 +00:00
|
|
|
void ChatMessageItemContainer::AttachEventHandlers(Gtk::Widget &widget) {
|
2020-12-22 07:24:09 +00:00
|
|
|
const auto on_button_press_event = [this](GdkEventButton *event) -> bool {
|
2020-09-26 03:02:13 +00:00
|
|
|
if (event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_SECONDARY) {
|
2020-12-22 07:24:09 +00:00
|
|
|
ShowMenu(reinterpret_cast<GdkEvent *>(event));
|
2020-09-26 03:02:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2020-12-22 07:24:09 +00:00
|
|
|
};
|
2021-01-01 07:47:03 +00:00
|
|
|
widget.signal_button_press_event().connect(on_button_press_event, false);
|
2020-09-26 03:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ChatMessageHeader::ChatMessageHeader(const Message *data) {
|
|
|
|
UserID = data->Author.ID;
|
|
|
|
ChannelID = data->ChannelID;
|
|
|
|
|
|
|
|
m_main_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
|
|
|
|
m_content_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
|
2020-12-25 08:02:40 +00:00
|
|
|
m_content_box_ev = Gtk::manage(new Gtk::EventBox);
|
2020-09-26 03:02:13 +00:00
|
|
|
m_meta_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
|
2020-11-08 05:44:26 +00:00
|
|
|
m_meta_ev = Gtk::manage(new Gtk::EventBox);
|
2020-09-26 03:02:13 +00:00
|
|
|
m_author = Gtk::manage(new Gtk::Label);
|
|
|
|
m_timestamp = Gtk::manage(new Gtk::Label);
|
2020-11-08 05:44:26 +00:00
|
|
|
m_avatar_ev = Gtk::manage(new Gtk::EventBox);
|
2020-09-26 03:02:13 +00:00
|
|
|
|
2020-11-24 01:34:09 +00:00
|
|
|
const auto author = Abaddon::Get().GetDiscordClient().GetUser(UserID);
|
2020-11-18 21:25:10 +00:00
|
|
|
auto &img = Abaddon::Get().GetImageManager();
|
2020-12-22 07:24:09 +00:00
|
|
|
|
2020-12-25 08:02:40 +00:00
|
|
|
m_avatar = Gtk::manage(new Gtk::Image(img.GetPlaceholder(AvatarSize)));
|
|
|
|
if (author->HasAvatar())
|
|
|
|
img.LoadFromURL(author->GetAvatarURL(), sigc::mem_fun(*this, &ChatMessageHeader::OnAvatarLoad));
|
2020-12-22 07:24:09 +00:00
|
|
|
|
2020-12-25 08:02:40 +00:00
|
|
|
if (author->HasAnimatedAvatar())
|
|
|
|
img.LoadAnimationFromURL(author->GetAvatarURL("gif"), AvatarSize, AvatarSize, sigc::mem_fun(*this, &ChatMessageHeader::OnAnimatedAvatarLoad));
|
2020-09-26 03:02:13 +00:00
|
|
|
|
|
|
|
get_style_context()->add_class("message-container");
|
|
|
|
m_author->get_style_context()->add_class("message-container-author");
|
|
|
|
m_timestamp->get_style_context()->add_class("message-container-timestamp");
|
|
|
|
m_avatar->get_style_context()->add_class("message-container-avatar");
|
|
|
|
|
|
|
|
m_avatar->set_valign(Gtk::ALIGN_START);
|
|
|
|
m_avatar->set_margin_right(10);
|
|
|
|
|
|
|
|
m_author->set_markup("<span weight='bold'>" + Glib::Markup::escape_text(data->Author.Username) + "</span>");
|
|
|
|
m_author->set_single_line_mode(true);
|
|
|
|
m_author->set_line_wrap(false);
|
|
|
|
m_author->set_ellipsize(Pango::ELLIPSIZE_END);
|
|
|
|
m_author->set_xalign(0.f);
|
|
|
|
m_author->set_can_focus(false);
|
2020-11-08 05:44:26 +00:00
|
|
|
|
|
|
|
m_meta_ev->signal_button_press_event().connect(sigc::mem_fun(*this, &ChatMessageHeader::on_author_button_press));
|
2020-09-26 03:02:13 +00:00
|
|
|
|
2020-12-23 02:18:39 +00:00
|
|
|
if (author->IsBot || data->WebhookID.has_value()) {
|
2020-10-06 02:46:44 +00:00
|
|
|
m_extra = Gtk::manage(new Gtk::Label);
|
|
|
|
m_extra->get_style_context()->add_class("message-container-extra");
|
|
|
|
m_extra->set_single_line_mode(true);
|
|
|
|
m_extra->set_margin_start(12);
|
|
|
|
m_extra->set_can_focus(false);
|
|
|
|
m_extra->set_use_markup(true);
|
|
|
|
}
|
2020-12-23 02:18:39 +00:00
|
|
|
if (author->IsBot)
|
|
|
|
m_extra->set_markup("<b>BOT</b>");
|
|
|
|
else if (data->WebhookID.has_value())
|
|
|
|
m_extra->set_markup("<b>Webhook</b>");
|
2020-10-06 02:46:44 +00:00
|
|
|
|
2021-01-02 06:02:23 +00:00
|
|
|
m_timestamp->set_text(data->ID.GetLocalTimestamp());
|
|
|
|
m_timestamp->set_hexpand(true);
|
|
|
|
m_timestamp->set_halign(Gtk::ALIGN_END);
|
|
|
|
m_timestamp->set_ellipsize(Pango::ELLIPSIZE_END);
|
2020-09-26 03:02:13 +00:00
|
|
|
m_timestamp->set_opacity(0.5);
|
|
|
|
m_timestamp->set_single_line_mode(true);
|
|
|
|
m_timestamp->set_margin_start(12);
|
|
|
|
m_timestamp->set_can_focus(false);
|
|
|
|
|
|
|
|
m_main_box->set_hexpand(true);
|
|
|
|
m_main_box->set_vexpand(true);
|
|
|
|
m_main_box->set_can_focus(true);
|
|
|
|
|
2021-01-02 06:02:23 +00:00
|
|
|
m_meta_box->set_hexpand(true);
|
2020-09-26 03:02:13 +00:00
|
|
|
m_meta_box->set_can_focus(false);
|
|
|
|
|
|
|
|
m_content_box->set_can_focus(false);
|
|
|
|
|
2020-12-25 08:02:40 +00:00
|
|
|
const auto on_enter_cb = [this](const GdkEventCrossing *event) -> bool {
|
|
|
|
if (m_anim_avatar)
|
|
|
|
m_avatar->property_pixbuf_animation() = m_anim_avatar;
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
const auto on_leave_cb = [this](const GdkEventCrossing *event) -> bool {
|
|
|
|
if (m_anim_avatar)
|
|
|
|
m_avatar->property_pixbuf() = m_static_avatar;
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_content_box_ev->add_events(Gdk::ENTER_NOTIFY_MASK | Gdk::LEAVE_NOTIFY_MASK);
|
|
|
|
m_meta_ev->add_events(Gdk::ENTER_NOTIFY_MASK | Gdk::LEAVE_NOTIFY_MASK);
|
|
|
|
m_avatar_ev->add_events(Gdk::ENTER_NOTIFY_MASK | Gdk::LEAVE_NOTIFY_MASK);
|
|
|
|
if (Abaddon::Get().GetSettings().GetShowAnimations()) {
|
|
|
|
m_content_box_ev->signal_enter_notify_event().connect(on_enter_cb);
|
|
|
|
m_content_box_ev->signal_leave_notify_event().connect(on_leave_cb);
|
|
|
|
m_meta_ev->signal_enter_notify_event().connect(on_enter_cb);
|
|
|
|
m_meta_ev->signal_leave_notify_event().connect(on_leave_cb);
|
|
|
|
m_avatar_ev->signal_enter_notify_event().connect(on_enter_cb);
|
|
|
|
m_avatar_ev->signal_leave_notify_event().connect(on_leave_cb);
|
|
|
|
}
|
|
|
|
|
2020-11-08 05:44:26 +00:00
|
|
|
m_meta_box->add(*m_author);
|
2020-10-06 02:46:44 +00:00
|
|
|
if (m_extra != nullptr)
|
|
|
|
m_meta_box->add(*m_extra);
|
2020-11-08 05:44:26 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
m_meta_box->add(*m_timestamp);
|
2020-11-08 05:44:26 +00:00
|
|
|
m_meta_ev->add(*m_meta_box);
|
|
|
|
m_content_box->add(*m_meta_ev);
|
|
|
|
m_avatar_ev->add(*m_avatar);
|
|
|
|
m_main_box->add(*m_avatar_ev);
|
2020-12-25 08:02:40 +00:00
|
|
|
m_content_box_ev->add(*m_content_box);
|
|
|
|
m_main_box->add(*m_content_box_ev);
|
2020-09-26 03:02:13 +00:00
|
|
|
add(*m_main_box);
|
|
|
|
|
|
|
|
set_margin_bottom(8);
|
|
|
|
|
2020-09-22 01:01:32 +00:00
|
|
|
show_all();
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
UpdateNameColor();
|
2020-11-08 05:44:26 +00:00
|
|
|
AttachUserMenuHandler(*m_meta_ev);
|
|
|
|
AttachUserMenuHandler(*m_avatar_ev);
|
2020-09-22 01:01:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
void ChatMessageHeader::UpdateNameColor() {
|
|
|
|
const auto &discord = Abaddon::Get().GetDiscordClient();
|
|
|
|
const auto guild_id = discord.GetChannel(ChannelID)->GuildID;
|
2020-12-10 08:50:40 +00:00
|
|
|
const auto role_id = discord.GetMemberHoistedRole(*guild_id, UserID, true);
|
2020-11-20 00:18:59 +00:00
|
|
|
const auto user = discord.GetUser(UserID);
|
|
|
|
if (!user.has_value()) return;
|
2020-11-24 01:34:09 +00:00
|
|
|
const auto role = discord.GetRole(role_id);
|
2020-09-26 03:02:13 +00:00
|
|
|
|
|
|
|
std::string md;
|
2020-11-24 01:34:09 +00:00
|
|
|
if (role.has_value())
|
2020-09-26 03:02:13 +00:00
|
|
|
md = "<span weight='bold' color='#" + IntToCSSColor(role->Color) + "'>" + Glib::Markup::escape_text(user->Username) + "</span>";
|
|
|
|
else
|
|
|
|
md = "<span weight='bold' color='#eeeeee'>" + Glib::Markup::escape_text(user->Username) + "</span>";
|
|
|
|
|
|
|
|
m_author->set_markup(md);
|
|
|
|
}
|
|
|
|
|
2020-11-18 21:25:10 +00:00
|
|
|
void ChatMessageHeader::OnAvatarLoad(const Glib::RefPtr<Gdk::Pixbuf> &pixbuf) {
|
2020-12-22 07:24:09 +00:00
|
|
|
m_static_avatar = pixbuf;
|
2020-11-18 21:25:10 +00:00
|
|
|
m_avatar->property_pixbuf() = pixbuf;
|
|
|
|
}
|
|
|
|
|
2020-12-22 07:24:09 +00:00
|
|
|
void ChatMessageHeader::OnAnimatedAvatarLoad(const Glib::RefPtr<Gdk::PixbufAnimation> &pixbuf) {
|
|
|
|
m_anim_avatar = pixbuf;
|
|
|
|
}
|
|
|
|
|
2020-11-08 05:44:26 +00:00
|
|
|
void ChatMessageHeader::AttachUserMenuHandler(Gtk::Widget &widget) {
|
|
|
|
widget.signal_button_press_event().connect([this](GdkEventButton *ev) -> bool {
|
|
|
|
if (ev->type == GDK_BUTTON_PRESS && ev->button == GDK_BUTTON_SECONDARY) {
|
|
|
|
m_signal_action_open_user_menu.emit(reinterpret_cast<GdkEvent *>(ev));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-11 05:56:30 +00:00
|
|
|
bool ChatMessageHeader::on_author_button_press(GdkEventButton *ev) {
|
|
|
|
if (ev->button == GDK_BUTTON_PRIMARY && (ev->state & GDK_SHIFT_MASK)) {
|
2020-11-08 05:44:26 +00:00
|
|
|
m_signal_action_insert_mention.emit();
|
2020-10-11 05:56:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChatMessageHeader::type_signal_action_insert_mention ChatMessageHeader::signal_action_insert_mention() {
|
|
|
|
return m_signal_action_insert_mention;
|
|
|
|
}
|
|
|
|
|
2020-11-08 05:44:26 +00:00
|
|
|
ChatMessageHeader::type_signal_action_open_user_menu ChatMessageHeader::signal_action_open_user_menu() {
|
|
|
|
return m_signal_action_open_user_menu;
|
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
void ChatMessageHeader::AddContent(Gtk::Widget *widget, bool prepend) {
|
|
|
|
m_content_box->add(*widget);
|
|
|
|
if (prepend)
|
|
|
|
m_content_box->reorder_child(*widget, 1);
|
|
|
|
}
|