2007-10-10 [colin] 3.0.2cvs53
[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         if (!boldfont) {
171                 normalfont = pango_font_description_from_string(NORMAL_FONT);
172                 boldfont = pango_font_description_from_string(NORMAL_FONT);
173                 pango_font_description_set_weight(boldfont, PANGO_WEIGHT_BOLD);
174         }
175
176         if (boldfont) {
177                 gtk_widget_modify_font(headerview->from_header_label, boldfont);
178                 gtk_widget_modify_font(headerview->to_header_label, boldfont);
179                 gtk_widget_modify_font(headerview->ng_header_label, boldfont);
180                 gtk_widget_modify_font(headerview->subject_header_label, boldfont);
181                 gtk_widget_modify_font(headerview->tags_header_label, boldfont);
182                 pango_font_description_free(boldfont);
183
184                 gtk_widget_modify_font(headerview->from_body_label, normalfont);
185                 gtk_widget_modify_font(headerview->to_body_label, normalfont);
186                 gtk_widget_modify_font(headerview->ng_body_label, normalfont);
187                 gtk_widget_modify_font(headerview->subject_body_label, normalfont);
188                 gtk_widget_modify_font(headerview->tags_body_label, normalfont);
189                 pango_font_description_free(normalfont);
190         }
191 }
192
193 void headerview_init(HeaderView *headerview)
194 {
195         headerview_set_font(headerview);
196         headerview_clear(headerview);
197         headerview_set_visibility(headerview, prefs_common.display_header_pane);
198
199 #if HAVE_LIBCOMPFACE
200         {
201                 gint i;
202
203                 for (i = 0; i < XPM_XFACE_HEIGHT; i++) {
204                         xpm_xface[i] = g_malloc(WIDTH + 1);
205                         *xpm_xface[i] = '\0';
206                 }
207         }
208 #endif
209 }
210
211 void headerview_show(HeaderView *headerview, MsgInfo *msginfo)
212 {
213         gchar *tags = procmsg_msginfo_get_tags_str(msginfo);
214
215         headerview_clear(headerview);
216
217         gtk_label_set_text(GTK_LABEL(headerview->from_body_label),
218                            msginfo->from ? msginfo->from : _("(No From)"));
219         if (msginfo->to) {
220                 gtk_label_set_text(GTK_LABEL(headerview->to_body_label),
221                                    msginfo->to);
222                 gtk_widget_show(headerview->to_header_label);
223                 gtk_widget_show(headerview->to_body_label);
224         }
225         if (msginfo->newsgroups) {
226                 gtk_label_set_text(GTK_LABEL(headerview->ng_body_label),
227                                    msginfo->newsgroups);
228                 gtk_widget_show(headerview->ng_header_label);
229                 gtk_widget_show(headerview->ng_body_label);
230         }
231         gtk_label_set_text(GTK_LABEL(headerview->subject_body_label),
232                            msginfo->subject ? msginfo->subject :
233                            _("(No Subject)"));
234         if (tags) {
235                 gtk_label_set_text(GTK_LABEL(headerview->tags_body_label),
236                                    tags);
237                 gtk_widget_show(headerview->tags_header_label);
238                 gtk_widget_show(headerview->tags_body_label);
239                 g_free(tags);
240         }
241         if (!headerview_show_face(headerview, msginfo))
242                 return;
243
244 #if HAVE_LIBCOMPFACE
245         if (!headerview_show_xface(headerview, msginfo))
246                 return;
247 #endif
248
249         if (!headerview_show_contact_pic(headerview, msginfo))
250                 return;
251
252 }
253
254 #if HAVE_LIBCOMPFACE
255 static gint headerview_show_xface(HeaderView *headerview, MsgInfo *msginfo)
256 {
257         GtkWidget *hbox = headerview->hbox;
258         GtkWidget *image;
259
260         if (!msginfo->extradata || 
261             !msginfo->extradata->xface || 
262             strlen(msginfo->extradata->xface) < 5) {
263                 if (headerview->image &&
264                     GTK_WIDGET_VISIBLE(headerview->image)) {
265                         gtk_widget_hide(headerview->image);
266                         gtk_widget_queue_resize(hbox);
267                 }
268                 return -1;
269         }
270         if (!GTK_WIDGET_VISIBLE(headerview->hbox)) return -1;
271
272         if (headerview->image) {
273                 gtk_widget_destroy(headerview->image);
274                 headerview->image = NULL;
275         }
276         
277
278         image = xface_get_from_header(msginfo->extradata->xface, &hbox->style->white,
279                                 hbox->window);
280
281         if (image) {
282                 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
283                 gtk_widget_show(image);
284         }
285
286         headerview->image = image;
287         if (image) {
288                 headerview_save_contact_pic(headerview, msginfo);
289         }
290         return 0;
291 }
292 #endif
293
294 static gint headerview_show_face (HeaderView *headerview, MsgInfo *msginfo)
295 {
296         GtkWidget *hbox = headerview->hbox;
297         GtkWidget *image;
298
299         if (!msginfo->extradata || !msginfo->extradata->face) {
300                 if (headerview->image &&
301                     GTK_WIDGET_VISIBLE(headerview->image)) {
302                         gtk_widget_hide(headerview->image);
303                         gtk_widget_queue_resize(hbox);
304                 }
305                 return -1;
306         }
307         if (!GTK_WIDGET_VISIBLE(headerview->hbox)) return -1;
308
309         if (headerview->image) {
310                 gtk_widget_destroy(headerview->image);
311                 headerview->image = NULL;
312         }
313         
314
315         image = face_get_from_header(msginfo->extradata->face);
316
317         if (image) {
318                 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
319                 gtk_widget_show(image);
320         }
321
322         headerview->image = image;
323         if (image == NULL)
324                 return -1;
325         else {
326                 headerview_save_contact_pic(headerview, msginfo);
327                 return 0;
328         }
329 }
330
331 static void headerview_save_contact_pic (HeaderView *headerview, MsgInfo *msginfo)
332 {
333         gchar *filename = NULL;
334         GError *error = NULL;
335         GdkPixbuf *picture = NULL;
336
337         if (!GTK_WIDGET_VISIBLE(headerview->hbox)) return;
338
339         if (headerview->image) {
340                 picture = gtk_image_get_pixbuf(GTK_IMAGE(headerview->image));
341         }
342         
343         filename = addrindex_get_picture_file(msginfo->from);
344         if (!filename)
345                 return;
346         if (!is_file_exist(filename)) {
347                 gdk_pixbuf_save(picture, filename, "png", &error, NULL);
348                 if (error) {
349                         g_warning(_("Failed to save image: \n%s"),
350                                         error->message);
351                         g_error_free(error);
352                 }
353         }
354         g_free(filename);
355 }       
356
357 static gint headerview_show_contact_pic (HeaderView *headerview, MsgInfo *msginfo)
358 {
359         GtkWidget *hbox = headerview->hbox;
360         GtkWidget *image;
361         gchar *filename = NULL;
362         GError *error = NULL;
363         GdkPixbuf *picture = NULL;
364         gint w, h;
365
366         if (!GTK_WIDGET_VISIBLE(headerview->hbox)) return -1;
367
368         if (headerview->image) {
369                 gtk_widget_destroy(headerview->image);
370                 headerview->image = NULL;
371         }
372         
373         filename = addrindex_get_picture_file(msginfo->from);
374         
375         if (!filename)
376                 return -1;
377         if (!is_file_exist(filename)) {
378                 g_free(filename);
379                 return -1;
380         }
381         gdk_pixbuf_get_file_info(filename, &w, &h);
382         
383         if (w > 48 || h > 48)
384                 picture = gdk_pixbuf_new_from_file_at_scale(filename, 
385                                                 48, 48, TRUE, &error);
386         else
387                 picture = gdk_pixbuf_new_from_file(filename, &error);
388
389         g_free(filename);
390         if (error) {
391                 debug_print("Failed to import image: \n%s",
392                                 error->message);
393                 g_error_free(error);
394                 return -1;
395         }
396         if (picture)
397                 image = gtk_image_new_from_pixbuf(picture);
398         else 
399                 return -1;
400
401         g_object_unref(picture);
402         if (image) {
403                 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
404                 gtk_widget_show(image);
405         }
406
407         headerview->image = image;
408         if (image == NULL)
409                 return -1;
410         else 
411                 return 0;
412 }
413
414 void headerview_clear(HeaderView *headerview)
415 {
416         if (headerview == NULL)
417                 return;
418
419         gtk_label_set_text(GTK_LABEL(headerview->from_body_label), "");
420         gtk_label_set_text(GTK_LABEL(headerview->to_body_label), "");
421         gtk_label_set_text(GTK_LABEL(headerview->ng_body_label), "");
422         gtk_label_set_text(GTK_LABEL(headerview->subject_body_label), "");
423         gtk_label_set_text(GTK_LABEL(headerview->tags_body_label), "");
424         gtk_widget_hide(headerview->to_header_label);
425         gtk_widget_hide(headerview->to_body_label);
426         gtk_widget_hide(headerview->ng_header_label);
427         gtk_widget_hide(headerview->ng_body_label);
428         gtk_widget_hide(headerview->tags_header_label);
429         gtk_widget_hide(headerview->tags_body_label);
430
431         if (headerview->image && GTK_WIDGET_VISIBLE(headerview->image)) {
432                 gtk_widget_hide(headerview->image);
433                 gtk_widget_queue_resize(headerview->hbox);
434         }
435 }
436
437 void headerview_set_visibility(HeaderView *headerview, gboolean visibility)
438 {
439         if (visibility)
440                 gtk_widget_show(headerview->hbox);
441         else
442                 gtk_widget_hide(headerview->hbox);
443 }
444
445 void headerview_destroy(HeaderView *headerview)
446 {
447         g_free(headerview);
448 }