summaryrefslogtreecommitdiff
path: root/src/components/channellist/classic/guildlistguilditem.cpp
blob: 5acad02f34c79209ca1a21778421f624b212a57e (plain)
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
#include "guildlistguilditem.hpp"

#include "abaddon.hpp"

GuildListGuildItem::GuildListGuildItem(const GuildData &guild)
    : ID(guild.ID) {
    get_style_context()->add_class("classic-guild-list-guild");

    m_image.property_pixbuf() = Abaddon::Get().GetImageManager().GetPlaceholder(48);

    add(m_box);
    m_box.pack_start(m_image);
    show_all_children();

    set_tooltip_text(guild.Name);

    UpdateIcon();

    Abaddon::Get().GetDiscordClient().signal_message_create().connect(sigc::mem_fun(*this, &GuildListGuildItem::OnMessageCreate));
    Abaddon::Get().GetDiscordClient().signal_message_ack().connect(sigc::mem_fun(*this, &GuildListGuildItem::OnMessageAck));
}

void GuildListGuildItem::UpdateIcon() {
    const auto guild = Abaddon::Get().GetDiscordClient().GetGuild(ID);
    if (!guild.has_value() || !guild->HasIcon()) return;
    Abaddon::Get().GetImageManager().LoadFromURL(guild->GetIconURL("png", "64"), sigc::mem_fun(*this, &GuildListGuildItem::OnIconFetched));
}

void GuildListGuildItem::OnIconFetched(const Glib::RefPtr<Gdk::Pixbuf> &pb) {
    m_image.property_pixbuf() = pb->scale_simple(48, 48, Gdk::INTERP_BILINEAR);
}

void GuildListGuildItem::OnMessageCreate(const Message &msg) {
    if (msg.GuildID.has_value() && *msg.GuildID == ID) CheckUnreadStatus();
}

void GuildListGuildItem::OnMessageAck(const MessageAckData &data) {
    CheckUnreadStatus();
}

void GuildListGuildItem::CheckUnreadStatus() {
    auto &discord = Abaddon::Get().GetDiscordClient();
    if (!Abaddon::Get().GetSettings().Unreads) return;
    int mentions;
    if (!discord.IsGuildMuted(ID) && discord.GetUnreadStateForGuild(ID, mentions)) {
        get_style_context()->add_class("has-unread");
    } else {
        get_style_context()->remove_class("has-unread");
    }
}