Fix several memory leaks around template parsing.
[claws.git] / src / avatars.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2014-2016 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         ar->type = 0;
43
44         return ar;
45 }
46
47 void avatars_avatarrender_free(AvatarRender *avrender)
48 {
49         if (avrender == NULL)
50                 return;
51
52         if (avrender->image != NULL) {
53                 gtk_widget_destroy(avrender->image);
54         }
55         g_free(avrender);
56 }
57
58 gboolean avatars_internal_rendering_hook(gpointer source, gpointer data)
59 {
60         AvatarRender *avatarr = (AvatarRender *)source;
61         gchar *aface;
62
63         if (!(prefs_common.enable_avatars & AVATARS_ENABLE_RENDER)) {
64                 debug_print("Internal rendering of avatars is disabled\n");
65                 return FALSE;
66         }
67
68         if (avatarr == NULL) {
69                 g_warning("Internal rendering invoked with NULL argument");
70                 return FALSE;
71         }
72
73         if (avatarr->image != NULL) {
74                 g_warning("Memory leak: image widget not destroyed");
75         }
76
77         aface = procmsg_msginfo_get_avatar(avatarr->full_msginfo, AVATAR_FACE);
78         if (aface) {
79                 avatarr->image = face_get_from_header(aface);
80                 avatarr->type  = AVATAR_FACE;
81         }
82 #if HAVE_LIBCOMPFACE
83         else {
84                 aface = procmsg_msginfo_get_avatar(avatarr->full_msginfo, AVATAR_XFACE);
85                 if (aface) {
86                         avatarr->image = xface_get_from_header(aface);
87                         avatarr->type  = AVATAR_XFACE;
88                 }
89         }
90 #endif
91         return FALSE;
92 }
93
94 void avatars_init(void)
95 {
96         if (avatar_render_hook_id != -1) {
97                 g_warning("Internal avatars rendering already initialized");
98                 return;
99         }
100         avatar_render_hook_id = hooks_register_hook(AVATAR_IMAGE_RENDER_HOOKLIST, avatars_internal_rendering_hook, NULL);
101         if (avatar_render_hook_id == -1) {
102                 g_warning("Failed to register avatars internal rendering hook");
103         }
104 }
105
106 void avatars_done(void)
107 {
108         if (avatar_render_hook_id != -1) {
109                 hooks_unregister_hook(AVATAR_IMAGE_RENDER_HOOKLIST, avatar_render_hook_id);
110                 avatar_render_hook_id = -1;
111         }
112 }
113