mirror of
https://github.com/uowuo/abaddon.git
synced 2024-11-10 14:10:10 +00:00
add prefetch (default off)
This commit is contained in:
parent
776c350eb6
commit
3916a50bf9
11
abaddon.cpp
11
abaddon.cpp
@ -37,6 +37,17 @@ Abaddon::Abaddon()
|
||||
m_discord.signal_guild_update().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnGuildUpdate));
|
||||
m_discord.signal_reaction_add().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnReactionAdd));
|
||||
m_discord.signal_reaction_remove().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnReactionRemove));
|
||||
if (m_settings.GetPrefetch())
|
||||
m_discord.signal_message_create().connect([this](Snowflake id) {
|
||||
const auto msg = m_discord.GetMessage(id);
|
||||
const auto author = m_discord.GetUser(msg->Author.ID);
|
||||
if (author.has_value() && author->HasAvatar())
|
||||
m_img_mgr.Prefetch(author->GetAvatarURL());
|
||||
for (const auto &attachment : msg->Attachments) {
|
||||
if (IsURLViewableImage(attachment.ProxyURL))
|
||||
m_img_mgr.Prefetch(attachment.ProxyURL);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Abaddon::~Abaddon() {
|
||||
|
@ -56,8 +56,7 @@ ChatMessageItemContainer *ChatMessageItemContainer::FromMessage(Snowflake id) {
|
||||
// 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") {
|
||||
if (IsURLViewableImage(a.ProxyURL)) {
|
||||
auto *widget = container->CreateImageComponent(a);
|
||||
container->m_main->add(*widget);
|
||||
} else {
|
||||
|
@ -41,6 +41,10 @@ void ImageManager::LoadFromURL(std::string url, callback_type cb) {
|
||||
});
|
||||
}
|
||||
|
||||
void ImageManager::Prefetch(std::string url) {
|
||||
m_cache.GetFileFromURL(url, [](const auto &) {});
|
||||
}
|
||||
|
||||
void ImageManager::RunCallbacks() {
|
||||
m_cb_mutex.lock();
|
||||
m_cb_queue.front()();
|
||||
|
@ -14,6 +14,7 @@ public:
|
||||
|
||||
Cache &GetCache();
|
||||
void LoadFromURL(std::string url, callback_type cb);
|
||||
void Prefetch(std::string url);
|
||||
Glib::RefPtr<Gdk::Pixbuf> GetFromURLIfCached(std::string url);
|
||||
Glib::RefPtr<Gdk::Pixbuf> GetPlaceholder(int size);
|
||||
|
||||
|
@ -61,3 +61,7 @@ std::string SettingsManager::GetLinkColor() const {
|
||||
int SettingsManager::GetCacheHTTPConcurrency() const {
|
||||
return GetSettingInt("http", "concurrent", 10);
|
||||
}
|
||||
|
||||
bool SettingsManager::GetPrefetch() const {
|
||||
return GetSettingBool("discord", "prefetch", false);
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ public:
|
||||
bool GetShowEmojis() const;
|
||||
std::string GetLinkColor() const;
|
||||
int GetCacheHTTPConcurrency() const;
|
||||
bool GetPrefetch() const;
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
|
28
util.hpp
28
util.hpp
@ -184,3 +184,31 @@ inline void AddWidgetMenuHandler(Gtk::Widget *widget, Gtk::Menu &menu) {
|
||||
}, false);
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
inline std::vector<std::string> StringSplit(const std::string &str, const char *delim) {
|
||||
std::vector<std::string> parts;
|
||||
char *token = std::strtok(const_cast<char *>(str.c_str()), delim);
|
||||
while (token != nullptr) {
|
||||
parts.push_back(token);
|
||||
token = std::strtok(nullptr, delim);
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
|
||||
inline std::string GetExtension(std::string url) {
|
||||
url = StringSplit(url, "?")[0];
|
||||
url = StringSplit(url, "/").back();
|
||||
return url.find(".") != std::string::npos ? url.substr(url.find_last_of(".")) : "";
|
||||
}
|
||||
|
||||
inline bool IsURLViewableImage(const std::string &url) {
|
||||
const auto ext = GetExtension(url);
|
||||
static const char *exts[] = { ".jpeg",
|
||||
".jpg",
|
||||
".png", nullptr };
|
||||
const char *str = ext.c_str();
|
||||
for (int i = 0; exts[i] != nullptr; i++)
|
||||
if (strcmp(str, exts[i]) == 0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user