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_images.h"
28 static GdkPixbuf
*lh_get_image(const char *url
)
31 GdkPixbuf
*pixbuf
= NULL
;
33 http
* http_loader
= new http();
34 GInputStream
*image
= http_loader
->load_url(url
, &error
);
36 if (error
|| !image
) {
38 g_warning("lh_get_image: Could not load URL for '%s': %s",
40 g_clear_error(&error
);
43 pixbuf
= gdk_pixbuf_new_from_stream(image
, NULL
, &error
);
45 g_warning("lh_get_image: Could not create pixbuf for '%s': %s",
48 g_clear_error(&error
);
57 void get_image_threaded(GTask
*task
, gpointer source
, gpointer task_data
, GCancellable
*cancellable
)
59 struct FetchCtx
*ctx
= (struct FetchCtx
*)task_data
;
60 GdkPixbuf
*pixbuf
= lh_get_image(ctx
->url
);
62 g_task_return_pointer(task
, pixbuf
, NULL
);
65 void get_image_callback(GObject
*source
, GAsyncResult
*res
, gpointer user_data
)
68 struct FetchCtx
*ctx
= (struct FetchCtx
*)user_data
;
70 pixbuf
= GDK_PIXBUF(g_task_propagate_pointer(G_TASK(res
), NULL
));
72 ctx
->container
->update_image_cache(ctx
->url
, pixbuf
);
73 ctx
->container
->rerender();
79 void container_linux::update_image_cache(const gchar
*url
, GdkPixbuf
*image
)
81 g_return_if_fail(url
!= NULL
);
83 debug_print("updating image cache: %p '%s'\n", image
, url
);
85 auto i
= m_images
.find(url
);
86 if(i
== m_images
.end()) {
87 g_warning("image '%s' was not found in pixbuf cache", url
);
88 unlock_images_cache();
92 if(i
->second
.first
!= NULL
&& i
->second
.first
!= image
) {
93 g_warning("pixbuf pointer for image '%s' changed", url
);
94 g_object_unref(i
->second
.first
);
98 /* A null pixbuf pointer presumably means the download failed,
99 * so remove the cache entry to allow for future retries. */
100 debug_print("warning - new pixbuf for '%s' is null\n", url
);
102 unlock_images_cache();
106 i
->second
.first
= image
;
107 unlock_images_cache();
110 void container_linux::lock_images_cache(void)
112 g_rec_mutex_lock(&m_images_lock
);
115 void container_linux::unlock_images_cache(void)
117 g_rec_mutex_unlock(&m_images_lock
);