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