Probably fix bug #3050, "Claws mail closes when one attempts to delete a tag"
[claws.git] / src / avatars.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2014 Ricardo Mones and the Claws Mail team
4  *
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  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27
28 #include "defs.h"
29 #include "hooks.h"
30 #include "gtkutils.h"
31 #include "procmsg.h"
32 #include "prefs_common.h"
33 #include "avatars.h"
34
35 static guint avatar_render_hook_id = -1;
36
37 AvatarRender *avatars_avatarrender_new(MsgInfo *msginfo)
38 {
39         AvatarRender *ar = g_new0(AvatarRender, 1);
40         ar->full_msginfo = msginfo;
41         ar->image = NULL;
42
43         return ar;
44 }
45
46 void avatars_avatarrender_free(AvatarRender *avrender)
47 {
48         if (avrender == NULL)
49                 return;
50
51         if (avrender->image != NULL) {
52                 gtk_widget_destroy(avrender->image);
53         }
54         g_free(avrender);
55 }
56
57 gboolean avatars_internal_rendering_hook(gpointer source, gpointer data)
58 {
59         AvatarRender *avatarr = (AvatarRender *)source;
60         gchar *aface;
61
62         if (!(prefs_common.enable_avatars | AVATARS_ENABLE_RENDER)) {
63                 debug_print("Internal rendering of avatars is disabled");
64                 return FALSE;
65         }
66
67         if (avatarr == NULL) {
68                 g_warning("Internal rendering invoked with NULL argument");
69                 return FALSE;
70         }
71
72         if (avatarr->image != NULL) {
73                 g_warning("Memory leak: image widget not destroyed");
74         }
75
76         aface = procmsg_msginfo_get_avatar(avatarr->full_msginfo, AVATAR_FACE);
77         if (aface) {
78                 avatarr->image = face_get_from_header(aface);
79         }
80 #if HAVE_LIBCOMPFACE
81         else {
82                 aface = procmsg_msginfo_get_avatar(avatarr->full_msginfo, AVATAR_XFACE);
83                 if (aface) {
84                         avatarr->image = xface_get_from_header(aface);
85                 }
86         }
87 #endif
88         return FALSE;
89 }
90
91 void avatars_init(void)
92 {
93         if (avatar_render_hook_id != -1) {
94                 g_warning(_("Internal avatars rendering already initialized"));
95                 return;
96         }
97         avatar_render_hook_id = hooks_register_hook(AVATAR_IMAGE_RENDER_HOOKLIST, avatars_internal_rendering_hook, NULL);
98         if (avatar_render_hook_id == -1) {
99                 g_warning(_("Failed to register avatars internal rendering hook"));
100         }
101 }
102
103 void avatars_done(void)
104 {
105         if (avatar_render_hook_id != -1) {
106                 hooks_unregister_hook(AVATAR_IMAGE_RENDER_HOOKLIST, avatar_render_hook_id);
107                 avatar_render_hook_id = -1;
108         }
109 }
110