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
|
#include "infopane.hpp"
#include "../../abaddon.hpp"
#include <filesystem>
GuildSettingsInfoPane::GuildSettingsInfoPane(Snowflake id)
: GuildID(id)
, m_guild_name_label("Guild name")
, m_guild_icon_label("Guild icon") {
auto &discord = Abaddon::Get().GetDiscordClient();
const auto guild = *discord.GetGuild(id);
const auto self_id = discord.GetUserData().ID;
const auto can_modify = discord.HasGuildPermission(self_id, id, Permission::MANAGE_GUILD);
set_name("guild-info-pane");
set_margin_top(10);
set_margin_bottom(10);
set_margin_left(10);
set_margin_top(10);
m_guild_name.set_sensitive(can_modify);
m_guild_name.set_text(guild.Name);
m_guild_name.signal_focus_out_event().connect([this](GdkEventFocus *e) -> bool {
UpdateGuildName();
return false;
});
m_guild_name.signal_key_press_event().connect([this](GdkEventKey *e) -> bool {
if (e->keyval == GDK_KEY_Return)
UpdateGuildName();
return false;
// clang-format off
}, false);
// clang-format on
m_guild_name.set_tooltip_text("Press enter or lose focus to submit");
m_guild_name.show();
m_guild_name_label.show();
auto load_icon_cb = [this](const Glib::RefPtr<Gdk::Pixbuf> &pixbuf) {
m_guild_icon.property_pixbuf() = pixbuf->scale_simple(64, 64, Gdk::INTERP_BILINEAR);
};
auto guild_update_cb = [this, load_icon_cb](Snowflake id) {
if (id != GuildID) return;
const auto guild = *Abaddon::Get().GetDiscordClient().GetGuild(id);
if (guild.HasIcon())
Abaddon::Get().GetImageManager().LoadFromURL(guild.GetIconURL("png", "64"), sigc::track_obj(load_icon_cb, *this));
};
discord.signal_guild_update().connect(sigc::track_obj(guild_update_cb, *this));
m_guild_icon.property_pixbuf() = Abaddon::Get().GetImageManager().GetPlaceholder(32);
if (guild.HasIcon()) {
Abaddon::Get().GetImageManager().LoadFromURL(guild.GetIconURL("png", "64"), sigc::track_obj(load_icon_cb, *this));
}
m_guild_icon.set_margin_bottom(10);
if (can_modify) {
m_guild_icon_ev.signal_realize().connect([this]() {
auto window = m_guild_icon_ev.get_window();
auto display = window->get_display();
auto cursor = Gdk::Cursor::create(display, "pointer");
window->set_cursor(cursor);
});
m_guild_icon_ev.signal_button_press_event().connect([this](GdkEventButton *event) -> bool {
if (event->type == GDK_BUTTON_PRESS)
if (event->button == GDK_BUTTON_PRIMARY)
UpdateGuildIconPicker();
else if (event->button == GDK_BUTTON_SECONDARY)
UpdateGuildIconClipboard();
return false;
});
}
m_guild_icon_ev.set_tooltip_text("Click to choose a file, right click to paste");
m_guild_icon.show();
m_guild_icon_ev.show();
//m_guild_icon_label.show();
m_guild_icon_ev.add(m_guild_icon);
attach(m_guild_icon_ev, 0, 0, 1, 1);
attach(m_guild_name_label, 0, 1, 1, 1);
attach_next_to(m_guild_name, m_guild_name_label, Gtk::POS_RIGHT, 1, 1);
//attach(m_guild_icon_label, 0, 1, 1, 1);
//attach_next_to(m_guild_icon, m_guild_icon_label, Gtk::POS_RIGHT, 1, 1);
}
void GuildSettingsInfoPane::UpdateGuildName() {
auto &discord = Abaddon::Get().GetDiscordClient();
if (discord.GetGuild(GuildID)->Name == m_guild_name.get_text()) return;
auto cb = [this](bool success) {
if (!success) {
m_guild_name.set_text(Abaddon::Get().GetDiscordClient().GetGuild(GuildID)->Name);
Gtk::MessageDialog dlg("Failed to set guild name", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
dlg.run();
}
};
discord.SetGuildName(GuildID, m_guild_name.get_text(), sigc::track_obj(cb, *this));
}
void GuildSettingsInfoPane::UpdateGuildIconFromData(const std::vector<uint8_t> &data, const std::string &mime) {
auto encoded = "data:" + mime + ";base64," + Glib::Base64::encode(std::string(data.begin(), data.end()));
auto &discord = Abaddon::Get().GetDiscordClient();
auto cb = [this](bool success) {
if (!success) {
Gtk::MessageDialog dlg("Failed to set guild icon", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
dlg.run();
}
};
discord.SetGuildIcon(GuildID, encoded, sigc::track_obj(cb, *this));
}
void GuildSettingsInfoPane::UpdateGuildIconFromPixbuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf) {
int w = pixbuf->get_width();
int h = pixbuf->get_height();
if (w > 1024 || h > 1024) {
GetImageDimensions(w, h, w, h, 1024, 1024);
pixbuf = pixbuf->scale_simple(w, h, Gdk::INTERP_BILINEAR);
}
gchar *buffer;
gsize buffer_size;
pixbuf->save_to_buffer(buffer, buffer_size, "png");
std::vector<uint8_t> data(buffer_size);
std::memcpy(data.data(), buffer, buffer_size);
UpdateGuildIconFromData(data, "image/png");
}
void GuildSettingsInfoPane::UpdateGuildIconPicker() {
// this picker fucking sucks
Gtk::FileChooserDialog dlg("Choose new guild icon", Gtk::FILE_CHOOSER_ACTION_OPEN);
dlg.get_style_context()->remove_provider(Abaddon::Get().GetStyleProvider());
dlg.set_modal(true);
dlg.signal_response().connect([this, &dlg](int response) {
if (response == Gtk::RESPONSE_OK) {
auto data = ReadWholeFile(dlg.get_filename());
if (GetExtension(dlg.get_filename()) == ".gif")
UpdateGuildIconFromData(data, "image/gif");
else
try {
auto loader = Gdk::PixbufLoader::create();
loader->signal_size_prepared().connect([&loader](int inw, int inh) {
int w, h;
GetImageDimensions(inw, inh, w, h, 1024, 1024);
loader->set_size(w, h);
});
loader->write(data.data(), data.size());
loader->close();
UpdateGuildIconFromPixbuf(loader->get_pixbuf());
} catch (const std::exception &) {};
}
});
dlg.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
dlg.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
auto filter_images = Gtk::FileFilter::create();
if (Abaddon::Get().GetDiscordClient().GetGuild(GuildID)->HasFeature("ANIMATED_ICON")) {
filter_images->set_name("Supported images (*.jpg, *.jpeg, *.png, *.gif)");
filter_images->add_pattern("*.gif");
} else {
filter_images->set_name("Supported images (*.jpg, *.jpeg, *.png)");
}
filter_images->add_pattern("*.jpg");
filter_images->add_pattern("*.jpeg");
filter_images->add_pattern("*.png");
dlg.add_filter(filter_images);
auto filter_all = Gtk::FileFilter::create();
filter_all->set_name("All files (*.*)");
filter_all->add_pattern("*.*");
dlg.add_filter(filter_all);
dlg.run();
}
void GuildSettingsInfoPane::UpdateGuildIconClipboard() {
std::vector<uint8_t> icon_data;
auto cb = Gtk::Clipboard::get();
// query for file path then for actual image
if (cb->wait_is_text_available()) {
auto path = cb->wait_for_text();
if (!std::filesystem::exists(path.c_str())) return;
auto data = ReadWholeFile(path);
try {
auto loader = Gdk::PixbufLoader::create();
loader->signal_size_prepared().connect([&loader](int inw, int inh) {
int w, h;
GetImageDimensions(inw, inh, w, h, 1024, 1024);
loader->set_size(w, h);
});
loader->write(data.data(), data.size());
loader->close();
auto pb = loader->get_pixbuf();
UpdateGuildIconFromPixbuf(pb);
return;
} catch (const std::exception &) {};
}
if (cb->wait_is_image_available()) {
auto pb = cb->wait_for_image();
UpdateGuildIconFromPixbuf(pb);
return;
}
}
|