talons

Fork of Claws Mail https://www.claws-mail
Log | Files | Refs | README | LICENSE

commit 99db9602c30d19ba5e68b5c87b45c41166c74d46
parent c556ff93f99831391382fc3c6687cfccbb0cb1a5
Author: Jonathan Boeing <jonathan@claws-mail.org>
Date:   Sun,  8 Oct 2023 21:10:21 -0700

Move functions back to container_linux.cpp

Move load_image(), get_image_size(), and clear_images() from container_linux_images.cpp
to container_linux.cpp

Diffstat:
Msrc/plugins/litehtml_viewer/container_linux.cpp | 164+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/plugins/litehtml_viewer/container_linux_images.cpp | 163-------------------------------------------------------------------------------
2 files changed, 164 insertions(+), 163 deletions(-)

diff --git a/src/plugins/litehtml_viewer/container_linux.cpp b/src/plugins/litehtml_viewer/container_linux.cpp @@ -26,6 +26,8 @@ #define _USE_MATH_DEFINES #include <math.h> +#include "lh_prefs.h" +#include "utils.h" #ifndef M_PI # define M_PI 3.14159265358979323846 @@ -106,6 +108,81 @@ void container_linux::draw_list_marker( litehtml::uint_ptr hdc, const litehtml:: } } +void container_linux::load_image( const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready ) +{ + litehtml::tstring url; + make_url(src, baseurl, url); + bool request = false; + struct timeval last; + + gettimeofday(&last, NULL); + + lock_images_cache(); + + auto i = m_images.find(url); + if(i == m_images.end()) { + /* Attached images can be loaded into cache right here. */ + if (!strncmp(src, "cid:", 4)) { + GdkPixbuf *pixbuf = get_local_image(src); + + if (pixbuf != NULL) + m_images.insert(std::make_pair(src, std::make_pair(pixbuf, last))); + + unlock_images_cache(); + return; + } else { + if (!lh_prefs_get()->enable_remote_content) { + debug_print("blocking download of image from '%s'\n", src); + unlock_images_cache(); + return; + } + + request = true; + m_images.insert(std::make_pair(url, std::make_pair((GdkPixbuf *)NULL, last))); + } + } else { + debug_print("found image cache entry: %p '%s'\n", i->second.first, url.c_str()); + i->second.second = last; + } + + unlock_images_cache(); + + if (request) { + struct FetchCtx *ctx; + + debug_print("allowing download of image from '%s'\n", src); + + ctx = g_new(struct FetchCtx, 1); + ctx->url = g_strdup(url.c_str()); + ctx->container = this; + + GTask *task = g_task_new(NULL, NULL, get_image_callback, ctx); + g_task_set_task_data(task, ctx, NULL); + g_task_run_in_thread(task, get_image_threaded); + } +} + +void container_linux::get_image_size( const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz ) +{ + litehtml::tstring url; + make_url(src, baseurl, url); + const GdkPixbuf *img = NULL; + + lock_images_cache(); + + auto i = m_images.find(url); + if (i != m_images.end() && i->second.first) { + img = i->second.first; + sz.width = gdk_pixbuf_get_width(img); + sz.height = gdk_pixbuf_get_height(img); + } else { + sz.width = 0; + sz.height = 0; + } + + unlock_images_cache(); +} + void container_linux::draw_background( litehtml::uint_ptr hdc, const litehtml::background_paint& bg ) { cairo_t* cr = (cairo_t*) hdc; @@ -589,6 +666,93 @@ void container_linux::fill_ellipse( cairo_t* cr, int x, int y, int width, int he cairo_restore(cr); } +void container_linux::clear_images() +{ + lock_images_cache(); + + for(auto i = m_images.begin(); i != m_images.end(); ++i) { + if (i->second.first) { + g_object_unref(i->second.first); + } + } + + m_images.clear(); + + unlock_images_cache(); +} + +gint container_linux::clear_images(gsize desired_size) +{ + gsize size = 0; + gint num = 0; + + lock_images_cache(); + + /* First, remove all local images - the ones with "cid:" URL. */ + for (auto i = m_images.begin(); i != m_images.end(); ) { + if (!strncmp(i->first.c_str(), "cid:", 4)) { + g_object_unref(i->second.first); + i = m_images.erase(i); + num++; + } else { + ++i; + } + } + + /* Second, build an LRU list */ + auto lru_comp_func = [](const lru_entry& l1, const lru_entry& l2) { + return timercmp(&l1.second, &l2.second, <); + }; + std::set<lru_entry, decltype(lru_comp_func)> lru(lru_comp_func); + + for (auto i = m_images.begin(); i != m_images.end(); ++i) { + lru.insert(std::make_pair(i->first, i->second.second)); + } + + /* + for (auto l = lru.begin(); l != lru.end(); l++) { + debug_print("lru dump: %d %d %s\n", l->second.tv_sec, l->second.tv_usec, l->first.c_str()); + } + */ + + /* Last, walk the LRU list and remove the oldest entries that push it over + * the desired size limit */ + for (auto l = lru.rbegin(); l != lru.rend(); ++l) { + gsize cursize; + + auto i = m_images.find(l->first); + + if(i == m_images.end()) { + g_warning("failed to find '%s' in m_images", l->first.c_str()); + continue; + } + + if(i->second.first == NULL) { + /* This should mean that the download is still in progress */ + debug_print("warning - trying to prune a null pixbuf for %s\n", i->first.c_str()); + continue; + } + + cursize = gdk_pixbuf_get_byte_length(i->second.first); + /* + debug_print("clear_images: desired_size %d - size %d - cursize %d - %d %d %s\n", + desired_size, size, cursize, l->second.tv_sec, l->second.tv_usec, l->first.c_str()); + */ + if (size + cursize > desired_size) { + debug_print("pruning %s from image cache\n", i->first.c_str()); + g_object_unref(i->second.first); + m_images.erase(i); + num++; + } else { + size += cursize; + } + } + + unlock_images_cache(); + + return num; +} + std::shared_ptr<litehtml::element> container_linux::create_element(const litehtml::tchar_t *tag_name, const litehtml::string_map &attributes, const std::shared_ptr<litehtml::document> &doc) diff --git a/src/plugins/litehtml_viewer/container_linux_images.cpp b/src/plugins/litehtml_viewer/container_linux_images.cpp @@ -25,7 +25,6 @@ #include "container_linux.h" #include "http.h" -#include "lh_prefs.h" typedef std::pair<litehtml::tstring, struct timeval> lru_entry; @@ -87,81 +86,6 @@ static void get_image_callback(GObject *source, GAsyncResult *res, gpointer user g_free(ctx); } -void container_linux::load_image( const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready ) -{ - litehtml::tstring url; - make_url(src, baseurl, url); - bool request = false; - struct timeval last; - - gettimeofday(&last, NULL); - - lock_images_cache(); - - auto i = m_images.find(url); - if(i == m_images.end()) { - /* Attached images can be loaded into cache right here. */ - if (!strncmp(src, "cid:", 4)) { - GdkPixbuf *pixbuf = get_local_image(src); - - if (pixbuf != NULL) - m_images.insert(std::make_pair(src, std::make_pair(pixbuf, last))); - - unlock_images_cache(); - return; - } else { - if (!lh_prefs_get()->enable_remote_content) { - debug_print("blocking download of image from '%s'\n", src); - unlock_images_cache(); - return; - } - - request = true; - m_images.insert(std::make_pair(url, std::make_pair((GdkPixbuf *)NULL, last))); - } - } else { - debug_print("found image cache entry: %p '%s'\n", i->second.first, url.c_str()); - i->second.second = last; - } - - unlock_images_cache(); - - if (request) { - struct FetchCtx *ctx; - - debug_print("allowing download of image from '%s'\n", src); - - ctx = g_new(struct FetchCtx, 1); - ctx->url = g_strdup(url.c_str()); - ctx->container = this; - - GTask *task = g_task_new(NULL, NULL, get_image_callback, ctx); - g_task_set_task_data(task, ctx, NULL); - g_task_run_in_thread(task, get_image_threaded); - } -} - -void container_linux::get_image_size( const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz ) -{ - litehtml::tstring url; - make_url(src, baseurl, url); - const GdkPixbuf *img = NULL; - - lock_images_cache(); - - auto i = m_images.find(url); - if (i != m_images.end() && i->second.first) { - img = i->second.first; - sz.width = gdk_pixbuf_get_width(img); - sz.height = gdk_pixbuf_get_height(img); - } else { - sz.width = 0; - sz.height = 0; - } - - unlock_images_cache(); -} - void container_linux::update_image_cache(const gchar *url, GdkPixbuf *image) { g_return_if_fail(url != NULL); @@ -202,90 +126,3 @@ void container_linux::unlock_images_cache(void) { g_rec_mutex_unlock(&m_images_lock); } - -void container_linux::clear_images() -{ - lock_images_cache(); - - for(auto i = m_images.begin(); i != m_images.end(); ++i) { - if (i->second.first) { - g_object_unref(i->second.first); - } - } - - m_images.clear(); - - unlock_images_cache(); -} - -gint container_linux::clear_images(gsize desired_size) -{ - gsize size = 0; - gint num = 0; - - lock_images_cache(); - - /* First, remove all local images - the ones with "cid:" URL. */ - for (auto i = m_images.begin(); i != m_images.end(); ) { - if (!strncmp(i->first.c_str(), "cid:", 4)) { - g_object_unref(i->second.first); - i = m_images.erase(i); - num++; - } else { - ++i; - } - } - - /* Second, build an LRU list */ - auto lru_comp_func = [](const lru_entry& l1, const lru_entry& l2) { - return timercmp(&l1.second, &l2.second, <); - }; - std::set<lru_entry, decltype(lru_comp_func)> lru(lru_comp_func); - - for (auto i = m_images.begin(); i != m_images.end(); ++i) { - lru.insert(std::make_pair(i->first, i->second.second)); - } - - /* - for (auto l = lru.begin(); l != lru.end(); l++) { - debug_print("lru dump: %d %d %s\n", l->second.tv_sec, l->second.tv_usec, l->first.c_str()); - } - */ - - /* Last, walk the LRU list and remove the oldest entries that push it over - * the desired size limit */ - for (auto l = lru.rbegin(); l != lru.rend(); ++l) { - gsize cursize; - - auto i = m_images.find(l->first); - - if(i == m_images.end()) { - g_warning("failed to find '%s' in m_images", l->first.c_str()); - continue; - } - - if(i->second.first == NULL) { - /* This should mean that the download is still in progress */ - debug_print("warning - trying to prune a null pixbuf for %s\n", i->first.c_str()); - continue; - } - - cursize = gdk_pixbuf_get_byte_length(i->second.first); - /* - debug_print("clear_images: desired_size %d - size %d - cursize %d - %d %d %s\n", - desired_size, size, cursize, l->second.tv_sec, l->second.tv_usec, l->first.c_str()); - */ - if (size + cursize > desired_size) { - debug_print("pruning %s from image cache\n", i->first.c_str()); - g_object_unref(i->second.first); - m_images.erase(i); - num++; - } else { - size += cursize; - } - } - - unlock_images_cache(); - - return num; -}