summaryrefslogtreecommitdiff
path: root/components/friendslist.cpp
blob: 3896f02218a813939349956692662ed8182ea66a (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
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
#include "friendslist.hpp"
#include "abaddon.hpp"
#include "lazyimage.hpp"

using namespace std::string_literals;

FriendsList::FriendsList()
    : Gtk::Box(Gtk::ORIENTATION_VERTICAL)
    , m_filter_mode(FILTER_FRIENDS) {
    get_style_context()->add_class("friends-list");

    auto &discord = Abaddon::Get().GetDiscordClient();

    discord.signal_relationship_add().connect(sigc::mem_fun(*this, &FriendsList::OnRelationshipAdd));
    discord.signal_relationship_remove().connect(sigc::mem_fun(*this, &FriendsList::OnRelationshipRemove));

    PopulateRelationships();
    signal_map().connect(sigc::mem_fun(*this, &FriendsList::PopulateRelationships));

    constexpr static std::array<const char *, 4> strs = {
        "Friends",
        "Online",
        "Pending",
        "Blocked",
    };
    for (const auto &x : strs) {
        auto *btn = Gtk::manage(new Gtk::RadioButton(m_group, x));
        m_buttons.add(*btn);
        btn->show();
        btn->signal_toggled().connect([this, btn, str = x] {
            if (!btn->get_active()) return;
            switch (str[0]) { // hehe
                case 'F':
                    m_filter_mode = FILTER_FRIENDS;
                    break;
                case 'O':
                    m_filter_mode = FILTER_ONLINE;
                    break;
                case 'P':
                    m_filter_mode = FILTER_PENDING;
                    break;
                case 'B':
                    m_filter_mode = FILTER_BLOCKED;
                    break;
            }
            m_list.invalidate_filter();
        });
    }
    m_buttons.set_homogeneous(true);
    m_buttons.set_halign(Gtk::ALIGN_CENTER);

    m_add.set_halign(Gtk::ALIGN_CENTER);
    m_add.set_margin_top(5);
    m_add.set_margin_bottom(5);

    m_scroll.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);

    m_list.set_sort_func(sigc::mem_fun(*this, &FriendsList::ListSortFunc));
    m_list.set_filter_func(sigc::mem_fun(*this, &FriendsList::ListFilterFunc));
    m_list.set_selection_mode(Gtk::SELECTION_NONE);
    m_list.set_hexpand(true);
    m_list.set_vexpand(true);
    m_scroll.add(m_list);
    add(m_add);
    add(m_buttons);
    add(m_scroll);

    m_add.show();
    m_scroll.show();
    m_buttons.show();
    m_list.show();
}

FriendsListFriendRow *FriendsList::MakeRow(const UserData &user, RelationshipType type) {
    auto *row = Gtk::manage(new FriendsListFriendRow(type, user));
    row->signal_action_remove().connect(sigc::bind(sigc::mem_fun(*this, &FriendsList::OnActionRemove), user.ID));
    row->signal_action_accept().connect(sigc::bind(sigc::mem_fun(*this, &FriendsList::OnActionAccept), user.ID));
    return row;
}

void FriendsList::OnRelationshipAdd(const RelationshipAddData &data) {
    for (auto *row_ : m_list.get_children()) {
        auto *row = dynamic_cast<FriendsListFriendRow *>(row_);
        if (row == nullptr || row->ID != data.ID) continue;
        delete row;
        break;
    }

    auto *row = MakeRow(data.User, data.Type);
    m_list.add(*row);
    row->show();
}

void FriendsList::OnRelationshipRemove(Snowflake id, RelationshipType type) {
    for (auto *row_ : m_list.get_children()) {
        auto *row = dynamic_cast<FriendsListFriendRow *>(row_);
        if (row == nullptr || row->ID != id) continue;
        delete row;
        return;
    }
}

void FriendsList::OnActionAccept(Snowflake id) {
    const auto cb = [this](DiscordError code) {
        if (code != DiscordError::NONE) {
            Gtk::MessageDialog dlg(*dynamic_cast<Gtk::Window *>(get_toplevel()), "Failed to accept", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
            dlg.set_position(Gtk::WIN_POS_CENTER);
            dlg.run();
        }
    };
    Abaddon::Get().GetDiscordClient().PutRelationship(id, sigc::track_obj(cb, *this));
}

void FriendsList::OnActionRemove(Snowflake id) {
    auto &discord = Abaddon::Get().GetDiscordClient();
    const auto user = discord.GetUser(id);
    if (auto *window = dynamic_cast<Gtk::Window *>(get_toplevel())) {
        Glib::ustring str;
        switch (*discord.GetRelationship(id)) {
            case RelationshipType::Blocked:
                str = "Are you sure you want to unblock " + user->Username + "#" + user->Discriminator + "?";
                break;
            case RelationshipType::Friend:
                str = "Are you sure you want to remove " + user->Username + "#" + user->Discriminator + "?";
                break;
            case RelationshipType::PendingIncoming:
                str = "Are you sure you want to ignore " + user->Username + "#" + user->Discriminator + "?";
                break;
            case RelationshipType::PendingOutgoing:
                str = "Are you sure you want to cancel your request to " + user->Username + "#" + user->Discriminator + "?";
                break;
            default:
                break;
        }
        if (Abaddon::Get().ShowConfirm(str, window)) {
            const auto cb = [this, window](DiscordError code) {
                if (code == DiscordError::NONE) return;
                Gtk::MessageDialog dlg(*window, "Failed to remove user", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
                dlg.set_position(Gtk::WIN_POS_CENTER);
                dlg.run();
            };
            discord.RemoveRelationship(id, sigc::track_obj(cb, *this));
        }
    }
}

void FriendsList::PopulateRelationships() {
    for (auto child : m_list.get_children())
        delete child;

    auto &discord = Abaddon::Get().GetDiscordClient();
    for (const auto &[id, type] : discord.GetRelationships()) {
        const auto user = discord.GetUser(id);
        if (!user.has_value()) continue;
        auto *row = MakeRow(*user, type);
        m_list.add(*row);
        row->show();
    }
}

int FriendsList::ListSortFunc(Gtk::ListBoxRow *a_, Gtk::ListBoxRow *b_) {
    auto *a = dynamic_cast<FriendsListFriendRow *>(a_);
    auto *b = dynamic_cast<FriendsListFriendRow *>(b_);
    if (a == nullptr || b == nullptr) return 0;
    return a->Name.compare(b->Name);
}

bool FriendsList::ListFilterFunc(Gtk::ListBoxRow *row_) {
    auto *row = dynamic_cast<FriendsListFriendRow *>(row_);
    if (row == nullptr) return false;
    switch (m_filter_mode) {
        case FILTER_FRIENDS:
            return row->Type == RelationshipType::Friend;
        case FILTER_ONLINE:
            return row->Type == RelationshipType::Friend && row->Status != PresenceStatus::Offline;
        case FILTER_PENDING:
            return row->Type == RelationshipType::PendingIncoming || row->Type == RelationshipType::PendingOutgoing;
        case FILTER_BLOCKED:
            return row->Type == RelationshipType::Blocked;
        default:
            return false;
    }
}

FriendsListAddComponent::FriendsListAddComponent()
    : Gtk::Box(Gtk::ORIENTATION_VERTICAL)
    , m_label("Add a Friend", Gtk::ALIGN_START)
    , m_status("", Gtk::ALIGN_START)
    , m_add("Add")
    , m_box(Gtk::ORIENTATION_HORIZONTAL) {
    m_box.add(m_entry);
    m_box.add(m_add);
    m_box.add(m_status);

    m_add.signal_clicked().connect(sigc::mem_fun(*this, &FriendsListAddComponent::Submit));

    m_label.set_halign(Gtk::ALIGN_CENTER);

    m_entry.set_placeholder_text("Enter a Username#1234");
    m_entry.signal_key_press_event().connect(sigc::mem_fun(*this, &FriendsListAddComponent::OnKeyPress), false);

    add(m_label);
    add(m_box);

    show_all_children();
}

void FriendsListAddComponent::Submit() {
    if (m_requesting) return;

    auto text = m_entry.get_text();
    m_label.set_text("Invalid input"); // cheeky !!
    m_entry.set_text("");
    const auto hashpos = text.find("#");
    if (hashpos == Glib::ustring::npos) return;
    const auto username = text.substr(0, hashpos);
    const auto discriminator = text.substr(hashpos + 1);
    if (username.size() == 0 || discriminator.size() != 4) return;
    if (discriminator.find_first_not_of("0123456789") != Glib::ustring::npos) return;

    m_requesting = true;
    m_label.set_text("Hang on...");

    const auto cb = [this](DiscordError code) {
        m_requesting = false;
        if (code == DiscordError::NONE) {
            m_label.set_text("Success!");
        } else {
            m_label.set_text("Failed: "s + GetDiscordErrorDisplayString(code));
        }
    };
    Abaddon::Get().GetDiscordClient().SendFriendRequest(username, std::stoul(discriminator), sigc::track_obj(cb, *this));
}

bool FriendsListAddComponent::OnKeyPress(GdkEventKey *e) {
    if (e->keyval == GDK_KEY_Return) {
        Submit();
        return true;
    }
    return false;
}

FriendsListFriendRow::FriendsListFriendRow(RelationshipType type, const UserData &data)
    : ID(data.ID)
    , Type(type)
    , Name(data.Username + "#" + data.Discriminator)
    , Status(Abaddon::Get().GetDiscordClient().GetUserStatus(data.ID))
    , m_accept("Accept") {
    auto *ev = Gtk::manage(new Gtk::EventBox);
    auto *box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
    auto *img = Gtk::manage(new LazyImage(32, 32, true));
    auto *namebox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
    auto *namelbl = Gtk::manage(new Gtk::Label("", Gtk::ALIGN_START));
    m_status_lbl = Gtk::manage(new Gtk::Label("", Gtk::ALIGN_START));
    auto *lblbox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));

    auto &discord = Abaddon::Get().GetDiscordClient();
    discord.signal_presence_update().connect(sigc::mem_fun(*this, &FriendsListFriendRow::OnPresenceUpdate));

    static bool show_animations = Abaddon::Get().GetSettings().GetShowAnimations();
    if (data.HasAnimatedAvatar() && show_animations) {
        img->SetAnimated(true);
        img->SetURL(data.GetAvatarURL("gif", "32"));
    } else {
        img->SetURL(data.GetAvatarURL("png", "32"));
    }

    namelbl->set_markup(data.GetEscapedBoldName());

    UpdatePresenceLabel();

    AddWidgetMenuHandler(ev, m_menu, [this] {
        m_accept.set_visible(Type == RelationshipType::PendingIncoming);

        switch (Type) {
            case RelationshipType::Blocked:
            case RelationshipType::Friend:
                m_remove.set_label("Remove");
                break;
            case RelationshipType::PendingIncoming:
                m_remove.set_label("Ignore");
                break;
            case RelationshipType::PendingOutgoing:
                m_remove.set_label("Cancel");
                break;
            default:
                break;
        }
    });

    m_remove.signal_activate().connect([this] {
        m_signal_remove.emit();
    });

    m_accept.signal_activate().connect([this] {
        m_signal_accept.emit();
    });

    m_menu.append(m_accept);
    m_menu.append(m_remove);
    m_menu.show_all();

    lblbox->set_valign(Gtk::ALIGN_CENTER);

    img->set_margin_end(5);

    namebox->add(*namelbl);
    lblbox->add(*namebox);
    lblbox->add(*m_status_lbl);

    box->add(*img);
    box->add(*lblbox);

    ev->add(*box);
    add(*ev);
    show_all_children();
}

void FriendsListFriendRow::UpdatePresenceLabel() {
    switch (Type) {
        case RelationshipType::PendingIncoming:
            m_status_lbl->set_text("Incoming Friend Request");
            break;
        case RelationshipType::PendingOutgoing:
            m_status_lbl->set_text("Outgoing Friend Request");
            break;
        default:
            m_status_lbl->set_text(GetPresenceDisplayString(Status));
            break;
    }
}

void FriendsListFriendRow::OnPresenceUpdate(const UserData &user, PresenceStatus status) {
    if (user.ID != ID) return;
    Status = status;
    UpdatePresenceLabel();
    changed();
}

FriendsListFriendRow::type_signal_remove FriendsListFriendRow::signal_action_remove() {
    return m_signal_remove;
}

FriendsListFriendRow::type_signal_accept FriendsListFriendRow::signal_action_accept() {
    return m_signal_accept;
}

FriendsListWindow::FriendsListWindow() {
    add(m_friends);
    set_default_size(500, 500);
    get_style_context()->add_class("app-window");
    get_style_context()->add_class("app-popup");
    m_friends.show();
}