add support for Ayatana indicator to Notification plugin
[claws.git] / src / plugins / litehtml_viewer / container_linux_images.cpp
blob4998a2adbc2c67ea34cc1974245e5b2f297bed4c
1 /*
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.
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #include "claws-features.h"
21 #endif
23 #include "common/utils.h"
25 #include "container_linux_images.h"
26 #include "http.h"
28 static GdkPixbuf *lh_get_image(const char *url)
30 GError *error = NULL;
31 GdkPixbuf *pixbuf = NULL;
33 http* http_loader = new http();
34 GInputStream *image = http_loader->load_url(url, &error);
36 if (error || !image) {
37 if (error) {
38 g_warning("lh_get_image: Could not load URL for '%s': %s",
39 url, error->message);
40 g_clear_error(&error);
42 } else {
43 pixbuf = gdk_pixbuf_new_from_stream(image, NULL, &error);
44 if (error) {
45 g_warning("lh_get_image: Could not create pixbuf for '%s': %s",
46 url, error->message);
47 pixbuf = NULL;
48 g_clear_error(&error);
52 delete http_loader;
54 return pixbuf;
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)
67 GdkPixbuf *pixbuf;
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();
75 g_free(ctx->url);
76 g_free(ctx);
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);
84 lock_images_cache();
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();
89 return;
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);
97 if(image == NULL) {
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);
101 m_images.erase(i);
102 unlock_images_cache();
103 return;
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);