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-08-26 05:46:43 +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-08-30 06:00:56 +00:00
|
|
|
m_menu.show_all();
|
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
ChatMessageItemContainer *ChatMessageItemContainer::FromMessage(Snowflake id) {
|
|
|
|
const auto *data = Abaddon::Get().GetDiscordClient().GetMessage(id);
|
|
|
|
if (data == nullptr) 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-09-30 04:12:38 +00:00
|
|
|
container->m_text_component = container->CreateTextComponent(data);
|
2020-09-26 03:02:13 +00:00
|
|
|
container->AttachMenuHandler(container->m_text_component);
|
|
|
|
container->m_main->add(*container->m_text_component);
|
2020-09-24 06:43:27 +00:00
|
|
|
}
|
2020-08-30 06:00:56 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
// there should only ever be 1 embed (i think?)
|
|
|
|
if (data->Embeds.size() == 1) {
|
2020-09-30 04:12:38 +00:00
|
|
|
container->m_embed_component = container->CreateEmbedComponent(data);
|
2020-09-26 03:02:13 +00:00
|
|
|
container->AttachMenuHandler(container->m_embed_component);
|
|
|
|
container->m_main->add(*container->m_embed_component);
|
|
|
|
}
|
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) {
|
|
|
|
const auto last3 = a.ProxyURL.substr(a.ProxyURL.length() - 3);
|
|
|
|
if (last3 == "png" || last3 == "jpg") {
|
|
|
|
auto *widget = container->CreateImageComponent(a);
|
|
|
|
auto *ev = Gtk::manage(new Gtk::EventBox);
|
|
|
|
ev->add(*widget);
|
|
|
|
container->AttachMenuHandler(ev);
|
|
|
|
container->AddClickHandler(ev, a.URL);
|
|
|
|
container->m_main->add(*ev);
|
|
|
|
container->HandleImage(a, widget, a.ProxyURL);
|
|
|
|
} else {
|
|
|
|
auto *widget = container->CreateAttachmentComponent(a);
|
|
|
|
container->AttachMenuHandler(widget);
|
|
|
|
container->AddClickHandler(widget, a.URL);
|
|
|
|
container->m_main->add(*widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
const auto *data = Abaddon::Get().GetDiscordClient().GetMessage(ID);
|
|
|
|
if (m_text_component != nullptr)
|
|
|
|
m_text_component->get_buffer()->set_text(data->Content);
|
2020-08-31 00:24:02 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
if (m_embed_component != nullptr) {
|
|
|
|
// easier to delete and remake than really update it
|
|
|
|
delete m_embed_component;
|
2020-08-29 05:14:07 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
if (data->Embeds.size() == 1) {
|
|
|
|
m_embed_component = CreateEmbedComponent(data);
|
2020-09-30 04:12:38 +00:00
|
|
|
if (m_embed_imgurl.size() > 0) {
|
|
|
|
m_signal_image_load.emit(m_embed_imgurl);
|
|
|
|
}
|
2020-09-26 03:02:13 +00:00
|
|
|
AttachMenuHandler(m_embed_component);
|
|
|
|
m_main->add(*m_embed_component);
|
|
|
|
}
|
|
|
|
}
|
2020-08-31 00:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 04:12:38 +00:00
|
|
|
void ChatMessageItemContainer::UpdateImage() {
|
|
|
|
for (auto it = m_img_loadmap.cbegin(); it != m_img_loadmap.cend();) {
|
|
|
|
auto buf = Abaddon::Get().GetImageManager().GetFromURLIfCached(it->first);
|
|
|
|
if (buf) {
|
|
|
|
int w, h;
|
|
|
|
std::tie(w, h) = GetImageDimensions(it->second.second.Width, it->second.second.Height);
|
|
|
|
it->second.first->property_pixbuf() = buf->scale_simple(w, h, Gdk::INTERP_BILINEAR);
|
|
|
|
it = m_img_loadmap.erase(it);
|
|
|
|
} else
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_embed_img != nullptr) {
|
|
|
|
auto buf = Abaddon::Get().GetImageManager().GetFromURLIfCached(m_embed_imgurl);
|
|
|
|
if (buf) {
|
|
|
|
int w, h;
|
|
|
|
m_embed_img->get_size_request(w, h);
|
|
|
|
m_embed_img->property_pixbuf() = buf->scale_simple(w, h, Gdk::INTERP_BILINEAR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
void ChatMessageItemContainer::UpdateAttributes() {
|
2020-09-11 03:57:36 +00:00
|
|
|
const auto *data = Abaddon::Get().GetDiscordClient().GetMessage(ID);
|
|
|
|
if (data == nullptr) 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 {
|
|
|
|
if (event->type == Gdk::BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY) {
|
|
|
|
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
|
|
|
|
|
|
|
auto b = tv->get_buffer();
|
|
|
|
Gtk::TextBuffer::iterator s, e; // lame
|
|
|
|
b->get_bounds(s, e);
|
|
|
|
switch (data->Type) {
|
|
|
|
case MessageType::DEFAULT:
|
|
|
|
b->set_text(data->Content);
|
|
|
|
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;
|
|
|
|
default: break;
|
|
|
|
}
|
2020-09-03 05:54:40 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
return tv;
|
|
|
|
}
|
|
|
|
|
|
|
|
Gtk::EventBox *ChatMessageItemContainer::CreateEmbedComponent(const Message *data) {
|
|
|
|
Gtk::EventBox *ev = Gtk::manage(new Gtk::EventBox);
|
|
|
|
ev->set_can_focus(true);
|
|
|
|
Gtk::Box *main = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
|
|
|
|
const auto &embed = data->Embeds[0];
|
2020-09-03 05:54:40 +00:00
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
if (embed.Author.Name.length() > 0) {
|
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-09-26 03:02:13 +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-09-26 03:02:13 +00:00
|
|
|
if (embed.Title.length() > 0) {
|
2020-09-03 05:54:40 +00:00
|
|
|
auto *title_label = Gtk::manage(new Gtk::Label);
|
|
|
|
title_label->set_use_markup(true);
|
2020-09-26 03:02:13 +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-09-26 03:02:13 +00:00
|
|
|
if (embed.Description.length() > 0) {
|
2020-09-03 05:54:40 +00:00
|
|
|
auto *desc_label = Gtk::manage(new Gtk::Label);
|
2020-09-26 03:02:13 +00:00
|
|
|
desc_label->set_text(embed.Description);
|
2020-09-03 05:54:40 +00:00
|
|
|
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);
|
2020-09-03 21:44:23 +00:00
|
|
|
desc_label->set_hexpand(false);
|
2020-09-09 22:32:45 +00:00
|
|
|
desc_label->get_style_context()->add_class("embed-description");
|
2020-09-26 03:02:13 +00:00
|
|
|
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
|
|
|
|
if (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-09-26 03:02:13 +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-09-30 19:12:52 +00:00
|
|
|
bool is_img = embed.Image.URL.size() > 0;
|
|
|
|
bool is_thumb = embed.Thumbnail.URL.size() > 0;
|
|
|
|
if (is_img || is_thumb) {
|
2020-09-30 04:12:38 +00:00
|
|
|
auto *img = Gtk::manage(new Gtk::Image);
|
|
|
|
img->set_halign(Gtk::ALIGN_CENTER);
|
|
|
|
int w, h;
|
2020-09-30 19:12:52 +00:00
|
|
|
if (is_img)
|
2020-09-30 04:12:38 +00:00
|
|
|
std::tie(w, h) = GetImageDimensions(embed.Image.Width, embed.Image.Height, 200, 150);
|
|
|
|
else
|
|
|
|
std::tie(w, h) = GetImageDimensions(embed.Thumbnail.Width, embed.Thumbnail.Height, 200, 150);
|
|
|
|
img->set_size_request(w, h);
|
|
|
|
main->pack_start(*img);
|
|
|
|
m_embed_img = img;
|
2020-09-30 19:12:52 +00:00
|
|
|
if (is_img)
|
2020-09-30 04:12:38 +00:00
|
|
|
m_embed_imgurl = embed.Image.ProxyURL;
|
|
|
|
else
|
|
|
|
m_embed_imgurl = embed.Thumbnail.ProxyURL;
|
2020-09-30 19:12:52 +00:00
|
|
|
Glib::signal_idle().connect(sigc::bind(sigc::mem_fun(*this, &ChatMessageItemContainer::EmitImageLoad), m_embed_imgurl));
|
2020-09-30 04:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
if (embed.Footer.Text.length() > 0) {
|
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-09-26 03:02:13 +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-09-26 03:02:13 +00:00
|
|
|
if (embed.Color != -1) {
|
2020-09-03 05:54:40 +00:00
|
|
|
auto provider = Gtk::CssProvider::create(); // this seems wrong
|
2020-09-26 03:02:13 +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-09-30 04:12:38 +00:00
|
|
|
Gtk::Image *ChatMessageItemContainer::CreateImageComponent(const AttachmentData &data) {
|
|
|
|
int w, h;
|
|
|
|
std::tie(w, h) = GetImageDimensions(data.Width, data.Height);
|
|
|
|
|
|
|
|
auto &im = Abaddon::Get().GetImageManager();
|
|
|
|
Gtk::Image *widget = Gtk::manage(new Gtk::Image);
|
|
|
|
widget->set_halign(Gtk::ALIGN_START);
|
|
|
|
widget->set_size_request(w, h);
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
const auto url = data.URL;
|
|
|
|
widget->signal_button_press_event().connect([url](GdkEventButton *event) -> bool {
|
|
|
|
if (event->type == Gdk::BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY) {
|
|
|
|
LaunchBrowser(url);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}, false);
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
Gtk::Box *ChatMessageItemContainer::CreateAttachmentComponent(const AttachmentData &data) {
|
|
|
|
auto *box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
|
|
|
|
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
|
|
|
|
box->get_style_context()->add_class("message-attachment-box");
|
|
|
|
ev->add(*btn);
|
|
|
|
box->add(*ev);
|
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<int, int> ChatMessageItemContainer::GetImageDimensions(int width, int height, int clampw, int clamph) {
|
|
|
|
const auto frac = static_cast<float>(width) / height;
|
|
|
|
|
|
|
|
if (width > clampw) {
|
|
|
|
width = clampw;
|
|
|
|
height = clampw / frac;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (height > clamph) {
|
|
|
|
height = clamph;
|
|
|
|
width = clamph * frac;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_pair(static_cast<int>(width), static_cast<int>(height));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatMessageItemContainer::HandleImage(const AttachmentData &data, Gtk::Image *img, std::string url) {
|
|
|
|
m_img_loadmap[url] = std::make_pair(img, data);
|
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-09-26 03:02:13 +00:00
|
|
|
void ChatMessageItemContainer::ShowMenu(GdkEvent *event) {
|
|
|
|
const auto &client = Abaddon::Get().GetDiscordClient();
|
|
|
|
const auto *data = client.GetMessage(ID);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-30 04:12:38 +00:00
|
|
|
ChatMessageItemContainer::type_signal_image_load ChatMessageItemContainer::signal_image_load() {
|
|
|
|
return m_signal_image_load;
|
|
|
|
}
|
|
|
|
|
2020-09-26 03:02:13 +00:00
|
|
|
// clang-format off
|
|
|
|
void ChatMessageItemContainer::AttachMenuHandler(Gtk::Widget *widget) {
|
|
|
|
widget->signal_button_press_event().connect([this](GdkEventButton *event) -> bool {
|
|
|
|
if (event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_SECONDARY) {
|
|
|
|
ShowMenu(reinterpret_cast<GdkEvent*>(event));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
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));
|
|
|
|
m_meta_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
|
|
|
|
m_author = Gtk::manage(new Gtk::Label);
|
|
|
|
m_timestamp = Gtk::manage(new Gtk::Label);
|
|
|
|
|
|
|
|
auto buf = Abaddon::Get().GetImageManager().GetFromURLIfCached(data->Author.GetAvatarURL());
|
|
|
|
if (buf)
|
|
|
|
m_avatar = Gtk::manage(new Gtk::Image(buf));
|
|
|
|
else
|
|
|
|
m_avatar = Gtk::manage(new Gtk::Image(Abaddon::Get().GetImageManager().GetPlaceholder(32)));
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
m_timestamp->set_text(data->Timestamp);
|
|
|
|
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);
|
|
|
|
|
|
|
|
m_meta_box->set_can_focus(false);
|
|
|
|
|
|
|
|
m_content_box->set_can_focus(false);
|
|
|
|
|
|
|
|
m_meta_box->add(*m_author);
|
|
|
|
m_meta_box->add(*m_timestamp);
|
|
|
|
m_content_box->add(*m_meta_box);
|
|
|
|
m_main_box->add(*m_avatar);
|
|
|
|
m_main_box->add(*m_content_box);
|
|
|
|
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-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;
|
|
|
|
const auto role_id = discord.GetMemberHoistedRole(guild_id, UserID, true);
|
|
|
|
const auto *user = discord.GetUser(UserID);
|
|
|
|
if (user == nullptr) return;
|
|
|
|
const auto *role = discord.GetRole(role_id);
|
|
|
|
|
|
|
|
std::string md;
|
|
|
|
if (role != nullptr)
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatMessageHeader::AddContent(Gtk::Widget *widget, bool prepend) {
|
|
|
|
m_content_box->add(*widget);
|
|
|
|
if (prepend)
|
|
|
|
m_content_box->reorder_child(*widget, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatMessageHeader::SetAvatarFromPixbuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf) {
|
|
|
|
m_avatar->property_pixbuf() = pixbuf;
|
|
|
|
}
|