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