add prefetch (default off)

This commit is contained in:
ouwou 2020-12-18 01:13:31 -05:00
parent 776c350eb6
commit 3916a50bf9
7 changed files with 50 additions and 2 deletions

View File

@ -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() {

View File

@ -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 {

View File

@ -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()();

View File

@ -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);

View File

@ -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);
}

View File

@ -15,6 +15,7 @@ public:
bool GetShowEmojis() const;
std::string GetLinkColor() const;
int GetCacheHTTPConcurrency() const;
bool GetPrefetch() const;
bool IsValid() const;

View File

@ -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;
}