2012-04-01 [colin] 3.8.0cvs36
[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 #ifndef USE_NEW_ADDRBOOK
331         gchar *filename = NULL;
332         GError *error = NULL;
333         GdkPixbuf *picture = NULL;
334
335         if (!gtk_widget_get_visible(headerview->hbox)) return;
336
337         if (headerview->image) {
338                 picture = gtk_image_get_pixbuf(GTK_IMAGE(headerview->image));
339         }
340         
341         filename = addrindex_get_picture_file(msginfo->from);
342         if (!filename)
343                 return;
344         if (!is_file_exist(filename)) {
345                 gdk_pixbuf_save(picture, filename, "png", &error, NULL);
346                 if (error) {
347                         g_warning(_("Failed to save image: \n%s"),
348                                         error->message);
349                         g_error_free(error);
350                 }
351         }
352         g_free(filename);
353 #else
354         /* new address book */
355 #endif
356 }       
357
358 static gint headerview_show_contact_pic (HeaderView *headerview, MsgInfo *msginfo)
359 {
360 #ifndef USE_NEW_ADDRBOOK
361         GtkWidget *hbox = headerview->hbox;
362         GtkWidget *image;
363         gchar *filename = NULL;
364         GError *error = NULL;
365         GdkPixbuf *picture = NULL;
366         gint w, h;
367
368         if (!gtk_widget_get_visible(headerview->hbox)) return -1;
369
370         if (headerview->image) {
371                 gtk_widget_destroy(headerview->image);
372                 headerview->image = NULL;
373         }
374         
375         filename = addrindex_get_picture_file(msginfo->from);
376         
377         if (!filename)
378                 return -1;
379         if (!is_file_exist(filename)) {
380                 g_free(filename);
381                 return -1;
382         }
383         gdk_pixbuf_get_file_info(filename, &w, &h);
384         
385         if (w > 48 || h > 48)
386                 picture = gdk_pixbuf_new_from_file_at_scale(filename, 
387                                                 48, 48, TRUE, &error);
388         else
389                 picture = gdk_pixbuf_new_from_file(filename, &error);
390
391         g_free(filename);
392         if (error) {
393                 debug_print("Failed to import image: \n%s",
394                                 error->message);
395                 g_error_free(error);
396                 return -1;
397         }
398         if (picture)
399                 image = gtk_image_new_from_pixbuf(picture);
400         else 
401                 return -1;
402
403         g_object_unref(picture);
404         if (image) {
405                 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
406                 gtk_widget_show(image);
407         }
408
409         headerview->image = image;
410         if (image == NULL)
411                 return -1;
412         else 
413                 return 0;
414 #else
415         /* new address book */
416         return -1;
417 #endif
418 }
419
420 void headerview_clear(HeaderView *headerview)
421 {
422         if (headerview == NULL)
423                 return;
424
425         gtk_label_set_text(GTK_LABEL(headerview->from_body_label), "");
426         gtk_label_set_text(GTK_LABEL(headerview->to_body_label), "");
427         gtk_label_set_text(GTK_LABEL(headerview->ng_body_label), "");
428         gtk_label_set_text(GTK_LABEL(headerview->subject_body_label), "");
429         gtk_label_set_text(GTK_LABEL(headerview->tags_body_label), "");
430         gtk_widget_hide(headerview->to_header_label);
431         gtk_widget_hide(headerview->to_body_label);
432         gtk_widget_hide(headerview->ng_header_label);
433         gtk_widget_hide(headerview->ng_body_label);
434         gtk_widget_hide(headerview->tags_header_label);
435         gtk_widget_hide(headerview->tags_body_label);
436
437         if (headerview->image && gtk_widget_get_visible(headerview->image)) {
438                 gtk_widget_hide(headerview->image);
439                 gtk_widget_queue_resize(headerview->hbox);
440         }
441 }
442
443 void headerview_set_visibility(HeaderView *headerview, gboolean visibility)
444 {
445         if (visibility)
446                 gtk_widget_show(headerview->hbox);
447         else
448                 gtk_widget_hide(headerview->hbox);
449 }
450
451 void headerview_destroy(HeaderView *headerview)
452 {
453         g_free(headerview);
454 }