Fix warning
[claws.git] / src / headerview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto 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
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <time.h>
33
34 #include <gdk-pixbuf/gdk-pixbuf.h>
35
36 #include "headerview.h"
37 #include "prefs_common.h"
38 #include "codeconv.h"
39 #include "gtkutils.h"
40 #include "utils.h"
41 #include "base64.h"
42 #include "headers.h"
43 #include "addrindex.h"
44 #include "hooks.h"
45 #include "avatars.h"
46
47 static gint headerview_show_avatar      (HeaderView     *headerview,
48                                          MsgInfo        *msginfo);
49 static gint headerview_show_contact_pic (HeaderView     *headerview,
50                                          MsgInfo        *msginfo);
51 static void headerview_save_contact_pic (HeaderView     *headerview,
52                                          MsgInfo        *msginfo);
53
54 HeaderView *headerview_create(void)
55 {
56         HeaderView *headerview;
57         GtkWidget *hbox;
58         GtkWidget *vbox;
59         GtkWidget *hbox1;
60         GtkWidget *hbox2;
61         GtkWidget *hbox3;
62         GtkWidget *from_header_label;
63         GtkWidget *from_body_label;
64         GtkWidget *to_header_label;
65         GtkWidget *to_body_label;
66         GtkWidget *ng_header_label;
67         GtkWidget *ng_body_label;
68         GtkWidget *subject_header_label;
69         GtkWidget *subject_body_label;
70         GtkWidget *tags_header_label;
71         GtkWidget *tags_body_label;
72
73         debug_print("Creating header view...\n");
74         headerview = g_new0(HeaderView, 1);
75
76         hbox = gtk_hbox_new(FALSE, 0);
77         gtk_container_set_border_width(GTK_CONTAINER(hbox), 2);
78         vbox = gtk_vbox_new(FALSE, 2);
79         gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
80
81         hbox1 = gtk_hbox_new(FALSE, 4);
82         gtk_box_pack_start(GTK_BOX(vbox), hbox1, FALSE, FALSE, 0);
83         hbox2 = gtk_hbox_new(FALSE, 4);
84         gtk_box_pack_start(GTK_BOX(vbox), hbox2, FALSE, FALSE, 0);
85         hbox3 = gtk_hbox_new(FALSE, 4);
86         gtk_box_pack_start(GTK_BOX(vbox), hbox3, FALSE, FALSE, 0);
87
88         from_header_label    = gtk_label_new(prefs_common_translated_header_name("From:"));
89         from_body_label      = gtk_label_new("");
90         to_header_label      = gtk_label_new(prefs_common_translated_header_name("To:"));
91         to_body_label        = gtk_label_new("");
92         ng_header_label      = gtk_label_new(prefs_common_translated_header_name("Newsgroups:"));
93         ng_body_label        = gtk_label_new("");
94         subject_header_label = gtk_label_new(prefs_common_translated_header_name("Subject:"));
95         subject_body_label   = gtk_label_new("");
96         tags_header_label = gtk_label_new(_("Tags:"));
97         tags_body_label   = gtk_label_new("");
98
99         gtk_label_set_selectable(GTK_LABEL(from_body_label), TRUE);
100         gtk_label_set_selectable(GTK_LABEL(to_body_label), TRUE);
101         gtk_label_set_selectable(GTK_LABEL(ng_body_label), TRUE);
102         gtk_label_set_selectable(GTK_LABEL(subject_body_label), TRUE);
103         gtk_label_set_selectable(GTK_LABEL(tags_body_label), TRUE);
104
105         gtkut_widget_set_can_focus(from_body_label, FALSE);
106         gtkut_widget_set_can_focus(to_body_label, FALSE);
107         gtkut_widget_set_can_focus(ng_body_label, FALSE);
108         gtkut_widget_set_can_focus(subject_body_label, FALSE);
109         gtkut_widget_set_can_focus(tags_body_label, FALSE);
110
111         gtk_box_pack_start(GTK_BOX(hbox1), from_header_label, FALSE, FALSE, 0);
112         gtk_box_pack_start(GTK_BOX(hbox1), from_body_label, FALSE, FALSE, 0);
113         gtk_box_pack_start(GTK_BOX(hbox1), to_header_label, FALSE, FALSE, 0);
114         gtk_box_pack_start(GTK_BOX(hbox1), to_body_label, TRUE, TRUE, 0);
115         gtk_box_pack_start(GTK_BOX(hbox1), ng_header_label, FALSE, FALSE, 0);
116         gtk_box_pack_start(GTK_BOX(hbox1), ng_body_label, TRUE, TRUE, 0);
117         gtk_box_pack_start(GTK_BOX(hbox2), subject_header_label, FALSE, FALSE, 0);
118         gtk_box_pack_start(GTK_BOX(hbox2), subject_body_label, TRUE, TRUE, 0);
119         gtk_box_pack_start(GTK_BOX(hbox3), tags_header_label, FALSE, FALSE, 0);
120         gtk_box_pack_start(GTK_BOX(hbox3), tags_body_label, TRUE, TRUE, 0);
121
122         gtk_misc_set_alignment(GTK_MISC(to_body_label), 0, 0.5);
123         gtk_misc_set_alignment(GTK_MISC(ng_body_label), 0, 0.5);
124         gtk_misc_set_alignment(GTK_MISC(subject_body_label), 0, 0.5);
125         gtk_misc_set_alignment(GTK_MISC(tags_body_label), 0, 0.5);
126         gtk_label_set_ellipsize(GTK_LABEL(to_body_label), PANGO_ELLIPSIZE_END);
127         gtk_label_set_ellipsize(GTK_LABEL(ng_body_label), PANGO_ELLIPSIZE_END);
128         gtk_label_set_ellipsize(GTK_LABEL(subject_body_label), PANGO_ELLIPSIZE_END);
129         gtk_label_set_ellipsize(GTK_LABEL(tags_body_label), PANGO_ELLIPSIZE_END);
130
131         headerview->hbox = hbox;
132         headerview->from_header_label    = from_header_label;
133         headerview->from_body_label      = from_body_label;
134         headerview->to_header_label      = to_header_label;
135         headerview->to_body_label        = to_body_label;
136         headerview->ng_header_label      = ng_header_label;
137         headerview->ng_body_label        = ng_body_label;
138         headerview->subject_header_label = subject_header_label;
139         headerview->subject_body_label   = subject_body_label;
140         headerview->tags_header_label = tags_header_label;
141         headerview->tags_body_label   = tags_body_label;
142         headerview->image = NULL;
143
144         gtk_widget_show_all(hbox);
145
146         return headerview;
147 }
148
149 void headerview_set_font(HeaderView *headerview)
150 {
151         PangoFontDescription *boldfont = NULL;
152         PangoFontDescription *normalfont = NULL;
153         
154         normalfont = pango_font_description_from_string(NORMAL_FONT);
155         if (normalfont) {
156                 gtk_widget_modify_font(headerview->from_body_label, normalfont);
157                 gtk_widget_modify_font(headerview->to_body_label, normalfont);
158                 gtk_widget_modify_font(headerview->ng_body_label, normalfont);
159                 gtk_widget_modify_font(headerview->subject_body_label, normalfont);
160                 gtk_widget_modify_font(headerview->tags_body_label, normalfont);
161                 pango_font_description_free(normalfont);
162         }
163
164         if (prefs_common.derive_from_normal_font || !BOLD_FONT) {
165                 boldfont = pango_font_description_from_string(NORMAL_FONT);
166                 pango_font_description_set_weight(boldfont, PANGO_WEIGHT_BOLD);
167         } else {
168                 boldfont = pango_font_description_from_string(BOLD_FONT);
169         }
170         if (boldfont) {
171                 gtk_widget_modify_font(headerview->from_header_label, boldfont);
172                 gtk_widget_modify_font(headerview->to_header_label, boldfont);
173                 gtk_widget_modify_font(headerview->ng_header_label, boldfont);
174                 gtk_widget_modify_font(headerview->subject_header_label, boldfont);
175                 gtk_widget_modify_font(headerview->tags_header_label, boldfont);
176                 pango_font_description_free(boldfont);
177         }
178 }
179
180 void headerview_init(HeaderView *headerview)
181 {
182         headerview_set_font(headerview);
183         headerview_clear(headerview);
184         headerview_set_visibility(headerview, prefs_common.display_header_pane);
185 }
186
187 void headerview_show(HeaderView *headerview, MsgInfo *msginfo)
188 {
189         gchar *tags = procmsg_msginfo_get_tags_str(msginfo);
190
191         headerview_clear(headerview);
192
193         gtk_label_set_text(GTK_LABEL(headerview->from_body_label),
194                            msginfo->from ? msginfo->from : _("(No From)"));
195         if (msginfo->to) {
196                 gtk_label_set_text(GTK_LABEL(headerview->to_body_label),
197                                    msginfo->to);
198                 gtk_widget_show(headerview->to_header_label);
199                 gtk_widget_show(headerview->to_body_label);
200         }
201         if (msginfo->newsgroups) {
202                 gtk_label_set_text(GTK_LABEL(headerview->ng_body_label),
203                                    msginfo->newsgroups);
204                 gtk_widget_show(headerview->ng_header_label);
205                 gtk_widget_show(headerview->ng_body_label);
206         }
207         gtk_label_set_text(GTK_LABEL(headerview->subject_body_label),
208                            msginfo->subject ? msginfo->subject :
209                            _("(No Subject)"));
210         if (tags) {
211                 gtk_label_set_text(GTK_LABEL(headerview->tags_body_label),
212                                    tags);
213                 gtk_widget_show(headerview->tags_header_label);
214                 gtk_widget_show(headerview->tags_body_label);
215                 g_free(tags);
216         }
217         if (!headerview_show_avatar(headerview, msginfo))
218                 return;
219
220         if (!headerview_show_contact_pic(headerview, msginfo))
221                 return;
222
223 }
224
225 static gint headerview_show_avatar (HeaderView *headerview, MsgInfo *msginfo)
226 {
227         AvatarRender *avatarr = avatars_avatarrender_new(msginfo);
228         GtkWidget *hbox = headerview->hbox;
229
230         hooks_invoke(AVATAR_IMAGE_RENDER_HOOKLIST, avatarr);
231
232         if (!avatarr->image) {
233                 if (headerview->image
234                                 && gtk_widget_get_visible(headerview->image)) {
235                         gtk_widget_hide(headerview->image);
236                         gtk_widget_queue_resize(hbox);
237                 }
238                 avatars_avatarrender_free(avatarr);
239                 return -1;
240         }
241         if (!gtk_widget_get_visible(hbox)) {
242                 avatars_avatarrender_free(avatarr);
243                 return -1;
244         }
245
246         if (headerview->image) {
247                 gtk_widget_destroy(headerview->image);
248                 headerview->image = NULL;
249         }
250
251         gtk_box_pack_start(GTK_BOX(hbox), avatarr->image, FALSE, FALSE, 0);
252         gtk_widget_show(avatarr->image);
253
254         headerview->image = avatarr->image;
255         avatarr->image = NULL; /* avoid destroying */
256         avatars_avatarrender_free(avatarr);
257
258         headerview_save_contact_pic(headerview, msginfo);
259         return 0;
260 }
261
262 static void headerview_save_contact_pic (HeaderView *headerview, MsgInfo *msginfo)
263 {
264 #ifndef USE_NEW_ADDRBOOK
265         gchar *filename = NULL;
266         GError *error = NULL;
267         GdkPixbuf *picture = NULL;
268
269         if (!gtk_widget_get_visible(headerview->hbox)) return;
270
271         if (headerview->image) {
272                 picture = gtk_image_get_pixbuf(GTK_IMAGE(headerview->image));
273         }
274         
275         filename = addrindex_get_picture_file(msginfo->from);
276         if (!filename)
277                 return;
278         if (!is_file_exist(filename)) {
279                 gdk_pixbuf_save(picture, filename, "png", &error, NULL);
280                 if (error) {
281                         g_warning(_("Failed to save image: \n%s"),
282                                         error->message);
283                         g_error_free(error);
284                 }
285         }
286         g_free(filename);
287 #else
288         /* new address book */
289 #endif
290 }       
291
292 static gint headerview_show_contact_pic (HeaderView *headerview, MsgInfo *msginfo)
293 {
294 #ifndef USE_NEW_ADDRBOOK
295         GtkWidget *hbox = headerview->hbox;
296         GtkWidget *image;
297         gchar *filename = NULL;
298         GError *error = NULL;
299         GdkPixbuf *picture = NULL;
300         gint w, h;
301
302         if (!gtk_widget_get_visible(headerview->hbox)) return -1;
303
304         if (headerview->image) {
305                 gtk_widget_destroy(headerview->image);
306                 headerview->image = NULL;
307         }
308         
309         filename = addrindex_get_picture_file(msginfo->from);
310         
311         if (!filename)
312                 return -1;
313         if (!is_file_exist(filename)) {
314                 g_free(filename);
315                 return -1;
316         }
317         gdk_pixbuf_get_file_info(filename, &w, &h);
318         
319         if (w > 48 || h > 48)
320                 picture = gdk_pixbuf_new_from_file_at_scale(filename, 
321                                                 48, 48, TRUE, &error);
322         else
323                 picture = gdk_pixbuf_new_from_file(filename, &error);
324
325         g_free(filename);
326         if (error) {
327                 debug_print("Failed to import image: \n%s",
328                                 error->message);
329                 g_error_free(error);
330                 return -1;
331         }
332         if (picture)
333                 image = gtk_image_new_from_pixbuf(picture);
334         else 
335                 return -1;
336
337         g_object_unref(picture);
338         if (image) {
339                 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
340                 gtk_widget_show(image);
341         }
342
343         headerview->image = image;
344         if (image == NULL)
345                 return -1;
346         else 
347                 return 0;
348 #else
349         /* new address book */
350         return -1;
351 #endif
352 }
353
354 void headerview_clear(HeaderView *headerview)
355 {
356         if (headerview == NULL)
357                 return;
358
359         gtk_label_set_text(GTK_LABEL(headerview->from_body_label), "");
360         gtk_label_set_text(GTK_LABEL(headerview->to_body_label), "");
361         gtk_label_set_text(GTK_LABEL(headerview->ng_body_label), "");
362         gtk_label_set_text(GTK_LABEL(headerview->subject_body_label), "");
363         gtk_label_set_text(GTK_LABEL(headerview->tags_body_label), "");
364         gtk_widget_hide(headerview->to_header_label);
365         gtk_widget_hide(headerview->to_body_label);
366         gtk_widget_hide(headerview->ng_header_label);
367         gtk_widget_hide(headerview->ng_body_label);
368         gtk_widget_hide(headerview->tags_header_label);
369         gtk_widget_hide(headerview->tags_body_label);
370
371         if (headerview->image && gtk_widget_get_visible(headerview->image)) {
372                 gtk_widget_hide(headerview->image);
373                 gtk_widget_queue_resize(headerview->hbox);
374         }
375 }
376
377 void headerview_set_visibility(HeaderView *headerview, gboolean visibility)
378 {
379         if (visibility)
380                 gtk_widget_show(headerview->hbox);
381         else
382                 gtk_widget_hide(headerview->hbox);
383 }
384
385 void headerview_destroy(HeaderView *headerview)
386 {
387         g_free(headerview);
388 }