1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
|
#include "chatmessage.hpp"
#include "../abaddon.hpp"
#include "../util.hpp"
ChatMessageItemContainer::ChatMessageItemContainer() {
m_main = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
add(*m_main);
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));
m_menu.append(*m_menu_copy_id);
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));
m_menu.append(*m_menu_delete_message);
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));
m_menu.append(*m_menu_edit_message);
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);
m_menu.show_all();
}
ChatMessageItemContainer *ChatMessageItemContainer::FromMessage(Snowflake id) {
const auto *data = Abaddon::Get().GetDiscordClient().GetMessage(id);
if (data == nullptr) return nullptr;
auto *container = Gtk::manage(new ChatMessageItemContainer);
container->ID = data->ID;
container->ChannelID = data->ChannelID;
if (data->Content.size() > 0 || data->Type != MessageType::DEFAULT) {
container->m_text_component = container->CreateTextComponent(data);
container->AttachGuildMenuHandler(container->m_text_component);
container->m_main->add(*container->m_text_component);
}
// there should only ever be 1 embed (i think?)
if (data->Embeds.size() == 1) {
container->m_embed_component = container->CreateEmbedComponent(data);
container->AttachGuildMenuHandler(container->m_embed_component);
container->m_main->add(*container->m_embed_component);
}
// 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->AttachGuildMenuHandler(ev);
container->AddClickHandler(ev, a.URL);
container->m_main->add(*ev);
container->HandleImage(a, widget, a.ProxyURL);
} else {
auto *widget = container->CreateAttachmentComponent(a);
container->AttachGuildMenuHandler(widget);
container->AddClickHandler(widget, a.URL);
container->m_main->add(*widget);
}
}
container->UpdateAttributes();
return container;
}
// this doesnt rly make sense
void ChatMessageItemContainer::UpdateContent() {
const auto *data = Abaddon::Get().GetDiscordClient().GetMessage(ID);
if (m_text_component != nullptr)
UpdateTextComponent(m_text_component);
if (m_embed_component != nullptr)
delete m_embed_component;
if (data->Embeds.size() == 1) {
m_embed_component = CreateEmbedComponent(data);
if (m_embed_imgurl.size() > 0) {
m_signal_image_load.emit(m_embed_imgurl);
}
AttachGuildMenuHandler(m_embed_component);
m_main->add(*m_embed_component);
}
}
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;
}
auto it = m_img_loadmap.find(url);
if (it != m_img_loadmap.end()) {
int w, h;
GetImageDimensions(it->second.second.Width, it->second.second.Height, w, h);
it->second.first->property_pixbuf() = buf->scale_simple(w, h, Gdk::INTERP_BILINEAR);
}
}
void ChatMessageItemContainer::UpdateAttributes() {
const auto *data = Abaddon::Get().GetDiscordClient().GetMessage(ID);
if (data == nullptr) return;
const bool deleted = data->IsDeleted();
const bool edited = data->IsEdited();
if (!deleted && !edited) return;
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)
}
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>");
}
bool ChatMessageItemContainer::EmitImageLoad(std::string url) {
m_signal_image_load.emit(url);
return false;
}
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
}
Gtk::TextView *ChatMessageItemContainer::CreateTextComponent(const Message *data) {
auto *tv = Gtk::manage(new Gtk::TextView);
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);
UpdateTextComponent(tv);
return tv;
}
void ChatMessageItemContainer::UpdateTextComponent(Gtk::TextView *tv) {
const auto *data = Abaddon::Get().GetDiscordClient().GetMessage(ID);
if (data == nullptr)
return;
auto b = tv->get_buffer();
b->set_text("");
Gtk::TextBuffer::iterator s, e;
b->get_bounds(s, e);
switch (data->Type) {
case MessageType::DEFAULT:
b->insert_markup(s, Glib::Markup::escape_text(data->Content));
HandleUserMentions(tv);
HandleLinks(tv);
HandleChannelMentions(tv);
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;
}
}
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];
if (embed.Author.Name.length() > 0) {
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);
author_lbl->set_text(embed.Author.Name);
author_lbl->get_style_context()->add_class("embed-author");
main->pack_start(*author_lbl);
}
if (embed.Title.length() > 0) {
auto *title_label = Gtk::manage(new Gtk::Label);
title_label->set_use_markup(true);
title_label->set_markup("<b>" + Glib::Markup::escape_text(embed.Title) + "</b>");
title_label->set_halign(Gtk::ALIGN_CENTER);
title_label->set_hexpand(false);
title_label->get_style_context()->add_class("embed-title");
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);
}
if (embed.Description.length() > 0) {
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);
}
// todo: handle inline fields
if (embed.Fields.size() > 0) {
auto *flow = Gtk::manage(new Gtk::FlowBox);
flow->set_orientation(Gtk::ORIENTATION_HORIZONTAL);
flow->set_min_children_per_line(3);
flow->set_max_children_per_line(3);
flow->set_halign(Gtk::ALIGN_START);
flow->set_hexpand(false);
flow->set_column_spacing(10);
flow->set_selection_mode(Gtk::SELECTION_NONE);
main->pack_start(*flow);
for (const auto &field : embed.Fields) {
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);
field_box->set_valign(Gtk::ALIGN_START);
field_lbl->set_hexpand(false);
field_lbl->set_halign(Gtk::ALIGN_START);
field_lbl->set_valign(Gtk::ALIGN_START);
field_val->set_hexpand(false);
field_val->set_halign(Gtk::ALIGN_START);
field_val->set_valign(Gtk::ALIGN_START);
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);
field_lbl->get_style_context()->add_class("embed-field-title");
field_val->get_style_context()->add_class("embed-field-value");
flow->insert(*field_box, -1);
}
}
bool is_img = embed.Image.URL.size() > 0;
bool is_thumb = embed.Thumbnail.URL.size() > 0;
if (is_img || is_thumb) {
auto *img = Gtk::manage(new Gtk::Image);
img->set_halign(Gtk::ALIGN_CENTER);
int w, h;
if (is_img)
GetImageDimensions(embed.Image.Width, embed.Image.Height, w, h, 200, 150);
else
GetImageDimensions(embed.Thumbnail.Width, embed.Thumbnail.Height, w, h, 200, 150);
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;
Glib::signal_idle().connect(sigc::bind(sigc::mem_fun(*this, &ChatMessageItemContainer::EmitImageLoad), m_embed_imgurl));
}
if (embed.Footer.Text.length() > 0) {
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);
footer_lbl->set_text(embed.Footer.Text);
footer_lbl->get_style_context()->add_class("embed-footer");
main->pack_start(*footer_lbl);
}
auto style = main->get_style_context();
if (embed.Color != -1) {
auto provider = Gtk::CssProvider::create(); // this seems wrong
std::string css = ".embed { border-left: 2px solid #" + IntToCSSColor(embed.Color) + "; }";
provider->load_from_data(css);
style->add_provider(provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
style->add_class("embed");
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);
ev->add(*main);
ev->show_all();
return ev;
}
Gtk::Image *ChatMessageItemContainer::CreateImageComponent(const AttachmentData &data) {
int w, h;
GetImageDimensions(data.Width, data.Height, w, h);
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;
}
void ChatMessageItemContainer::HandleImage(const AttachmentData &data, Gtk::Image *img, std::string url) {
m_img_loadmap[url] = std::make_pair(img, data);
// ask the chatwindow to call UpdateImage because dealing with lifetimes sucks
Glib::signal_idle().connect(sigc::bind(sigc::mem_fun(*this, &ChatMessageItemContainer::EmitImageLoad), url));
}
void ChatMessageItemContainer::HandleUserMentions(Gtk::TextView *tv) {
constexpr static const auto mentions_regex = R"(<@!?(\d+)>)";
std::regex rgx(mentions_regex, std::regex_constants::ECMAScript);
auto buf = tv->get_buffer();
std::string text = buf->get_text();
const auto &discord = Abaddon::Get().GetDiscordClient();
std::string::const_iterator sstart(text.begin());
std::smatch match;
while (std::regex_search(sstart, text.cend(), match, rgx)) {
const std::string user_id = match.str(1);
const auto *user = discord.GetUser(user_id);
const auto *channel = discord.GetChannel(ChannelID);
if (user == nullptr || channel == nullptr) {
sstart = match.suffix().first;
continue;
}
std::string replacement;
if (channel->Type == ChannelType::DM || channel->Type == ChannelType::GROUP_DM)
replacement = "<b>@" + Glib::Markup::escape_text(user->Username) + "#" + user->Discriminator + "</b>";
else {
const auto role_id = user->GetHoistedRole(channel->GuildID, true);
const auto *role = discord.GetRole(role_id);
if (role == nullptr)
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>";
}
const auto start = std::distance(text.cbegin(), sstart) + match.position();
auto erase_from = buf->get_iter_at_offset(start);
auto erase_to = buf->get_iter_at_offset(start + match.length());
auto it = buf->erase(erase_from, erase_to);
buf->insert_markup(it, replacement);
text = buf->get_text();
sstart = text.begin();
}
}
void ChatMessageItemContainer::HandleChannelMentions(Gtk::TextView *tv) {
constexpr static const auto chan_regex = R"(<#(\d+)>)";
std::regex rgx(chan_regex, std::regex_constants::ECMAScript);
tv->signal_button_press_event().connect(sigc::mem_fun(*this, &ChatMessageItemContainer::OnClickChannel), false);
auto buf = tv->get_buffer();
std::string text = buf->get_text();
const auto &discord = Abaddon::Get().GetDiscordClient();
std::string::const_iterator sstart(text.begin());
std::smatch match;
while (std::regex_search(sstart, text.cend(), match, rgx)) {
std::string channel_id = match.str(1);
const auto *chan = discord.GetChannel(channel_id);
if (chan == nullptr) {
sstart = match.suffix().first;
continue;
}
auto tag = buf->create_tag();
m_channel_tagmap[tag] = channel_id;
tag->property_weight() = Pango::WEIGHT_BOLD;
const auto start = std::distance(text.cbegin(), sstart) + match.position();
auto erase_from = buf->get_iter_at_offset(start);
auto erase_to = buf->get_iter_at_offset(start + match.length());
auto it = buf->erase(erase_from, erase_to);
const std::string replacement = "#" + chan->Name;
it = buf->insert_with_tag(it, "#" + chan->Name, tag);
// rescan the whole thing so i dont have to deal with fixing match positions
text = buf->get_text();
sstart = text.begin();
}
}
// 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;
if (ev->type != Gdk::BUTTON_PRESS) return false;
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;
}
void ChatMessageItemContainer::HandleLinks(Gtk::TextView *tv) {
constexpr static const auto links_regex = R"(\bhttps?:\/\/[^\s]+\.[^\s]+\b)";
std::regex rgx(links_regex, std::regex_constants::ECMAScript);
tv->signal_button_press_event().connect(sigc::mem_fun(*this, &ChatMessageItemContainer::OnLinkClick), false);
auto buf = tv->get_buffer();
Gtk::TextBuffer::iterator start, end;
buf->get_bounds(start, end);
std::string text = buf->get_slice(start, end);
// i'd like to let this be done thru css like .message-link { color: #bitch; } but idk how
auto &settings = Abaddon::Get().GetSettings();
auto link_color = settings.GetSettingString("misc", "linkcolor", "rgba(40, 200, 180, 255)");
std::string::const_iterator sstart(text.begin());
std::smatch match;
while (std::regex_search(sstart, text.cend(), match, rgx)) {
std::string link = match.str();
auto tag = buf->create_tag();
m_link_tagmap[tag] = link;
tag->property_foreground_rgba() = Gdk::RGBA(link_color);
const auto start = std::distance(text.cbegin(), sstart) + match.position();
auto erase_from = buf->get_iter_at_offset(start);
auto erase_to = buf->get_iter_at_offset(start + match.length());
auto it = buf->erase(erase_from, erase_to);
it = buf->insert_with_tag(it, link, tag);
sstart = match.suffix().first;
}
}
bool ChatMessageItemContainer::OnLinkClick(GdkEventButton *ev) {
if (m_text_component == nullptr) return false;
if (ev->type != Gdk::BUTTON_PRESS) return false;
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_link_tagmap.find(tag);
if (it != m_link_tagmap.end()) {
LaunchBrowser(it->second);
return true;
}
}
return false;
}
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);
}
m_menu.popup_at_pointer(event);
}
void ChatMessageItemContainer::on_menu_copy_id() {
Gtk::Clipboard::get()->set_text(std::to_string(ID));
}
void ChatMessageItemContainer::on_menu_delete_message() {
m_signal_action_delete.emit();
}
void ChatMessageItemContainer::on_menu_edit_message() {
m_signal_action_edit.emit();
}
void ChatMessageItemContainer::on_menu_copy_content() {
const auto *msg = Abaddon::Get().GetDiscordClient().GetMessage(ID);
if (msg != nullptr)
Gtk::Clipboard::get()->set_text(msg->Content);
}
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;
}
ChatMessageItemContainer::type_signal_channel_click ChatMessageItemContainer::signal_action_channel_click() {
return m_signal_action_channel_click;
}
ChatMessageItemContainer::type_signal_image_load ChatMessageItemContainer::signal_image_load() {
return m_signal_image_load;
}
// clang-format off
void ChatMessageItemContainer::AttachGuildMenuHandler(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_ev = Gtk::manage(new Gtk::EventBox);
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_author_ev->signal_button_press_event().connect(sigc::mem_fun(*this, &ChatMessageHeader::on_author_button_press));
if (data->WebhookID.IsValid()) {
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);
m_extra->set_markup("<b>Webhook</b>");
} else if (data->Author.IsBot) {
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);
m_extra->set_markup("<b>BOT</b>");
}
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_author_ev->add(*m_author);
m_meta_box->add(*m_author_ev);
if (m_extra != nullptr)
m_meta_box->add(*m_extra);
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);
show_all();
UpdateNameColor();
}
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);
}
bool ChatMessageHeader::on_author_button_press(GdkEventButton *ev) {
if (ev->button == GDK_BUTTON_PRIMARY && (ev->state & GDK_SHIFT_MASK)) {
m_signal_action_insert_mention.emit(UserID);
return true;
}
return false;
}
ChatMessageHeader::type_signal_action_insert_mention ChatMessageHeader::signal_action_insert_mention() {
return m_signal_action_insert_mention;
}
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;
}
|