2 * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
3 * Copyright(C) 2019 the Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write tothe Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "claws-features.h"
23 #include "common/utils.h"
25 #include "container_linux.h"
29 static GdkPixbuf *lh_get_image(const litehtml::tchar_t* url)
32 GdkPixbuf *pixbuf = NULL;
33 http* http_loader = NULL;
35 http_loader = new http();
36 GInputStream *image = http_loader->load_url(url, &error);
38 if (error || !image) {
40 g_warning("lh_get_image: Could not create pixbuf %s",
42 g_clear_error(&error);
47 pixbuf = gdk_pixbuf_new_from_stream(image, NULL, &error);
49 g_warning("lh_get_image: Could not create pixbuf %s",
52 g_clear_error(&error);
64 container_linux *container;
68 static void get_image_threaded(GTask *task, gpointer source, gpointer task_data, GCancellable *cancellable)
70 struct FetchCtx *ctx = (struct FetchCtx *)task_data;
71 GdkPixbuf *pixbuf = lh_get_image(ctx->url);
73 g_task_return_pointer(task, pixbuf, NULL);
76 static void get_image_callback(GObject *source, GAsyncResult *res, gpointer user_data)
79 struct FetchCtx *ctx = (struct FetchCtx *)user_data;
81 pixbuf = GDK_PIXBUF(g_task_propagate_pointer(G_TASK(res), NULL));
84 ctx->container->add_image_to_cache(ctx->url, pixbuf);
85 ctx->container->redraw(true);
92 void container_linux::load_image( const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready )
94 litehtml::tstring url;
95 make_url(src, baseurl, url);
100 for (auto ii = m_images.cbegin(); ii != m_images.cend(); ++ii) {
101 const image *i = &(*ii);
103 if (!strcmp(i->first.c_str(), url.c_str())) {
109 unlock_images_cache();
112 struct FetchCtx *ctx = g_new(struct FetchCtx, 1);
114 /* Attached images can be loaded into cache right here. */
115 if (!strncmp(src, "cid:", 4)) {
116 GdkPixbuf *pixbuf = get_local_image(src);
119 add_image_to_cache(src, pixbuf);
124 if (!lh_prefs_get()->enable_remote_content) {
125 debug_print("blocking download of image from '%s'\n", src);
129 debug_print("allowing download of image from '%s'\n", src);
131 ctx->url = g_strdup(url.c_str());
132 ctx->container = this;
134 GTask *task = g_task_new(this, NULL, get_image_callback, ctx);
135 g_task_set_task_data(task, ctx, NULL);
136 g_task_run_in_thread(task, get_image_threaded);
138 debug_print("found image in cache: '%s'\n", url.c_str());
142 void container_linux::get_image_size( const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz )
144 litehtml::tstring url;
145 make_url(src, baseurl, url);
147 const image *img = NULL;
151 for (auto ii = m_images.cbegin(); ii != m_images.cend(); ++ii) {
152 const image *i = &(*ii);
153 if (i->first == url) {
162 sz.width = gdk_pixbuf_get_width(img->second);
163 sz.height = gdk_pixbuf_get_height(img->second);
170 unlock_images_cache();
173 void container_linux::add_image_to_cache(const gchar *url, GdkPixbuf *image)
175 g_return_if_fail(url != NULL);
176 g_return_if_fail(image != NULL);
178 debug_print("adding image to cache: '%s'\n", url);
180 m_images.push_back(std::make_pair(url, image));
181 unlock_images_cache();
183 void container_linux::lock_images_cache(void)
185 g_rec_mutex_lock(&m_images_lock);
188 void container_linux::unlock_images_cache(void)
190 g_rec_mutex_unlock(&m_images_lock);
193 void container_linux::clear_images()
197 for(auto i = m_images.begin(); i != m_images.end(); ++i) {
201 g_object_unref(img->second);
207 unlock_images_cache();
210 gint container_linux::clear_images(gint desired_size)
217 /* First, tally up size of all the stored GdkPixbufs and
218 * deallocate those which make the total size be above
219 * the desired_size limit. We will remove their list
221 for (auto i = m_images.rbegin(); i != m_images.rend(); ++i) {
225 if (img->second == NULL)
228 cursize = gdk_pixbuf_get_byte_length(img->second);
230 if (size + cursize > desired_size) {
231 g_object_unref(img->second);
239 /* Remove elements whose GdkPixbuf pointers point to NULL. */
240 m_images.remove_if([&](image _img) -> bool {
241 if (_img.second == NULL)
246 unlock_images_cache();