2005-02-03 [paul] 1.0.0cvs25.3
[claws.git] / src / headerview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 <gtk/gtkwidget.h>
28 #include <gtk/gtkstyle.h>
29 #include <gtk/gtkscrolledwindow.h>
30 #include <gtk/gtkhbox.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtklabel.h>
33 #include <gtk/gtkimage.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <time.h>
37
38 #if HAVE_LIBCOMPFACE
39 #  include <compface.h>
40 #endif
41
42 #include "intl.h"
43 #include "headerview.h"
44 #include "prefs_common.h"
45 #include "codeconv.h"
46 #include "gtkutils.h"
47 #include "utils.h"
48
49 #define TR(str) (prefs_common.trans_hdr ? gettext(str) : str)
50
51 #if 0
52         _("From:");
53         _("To:");
54         _("Newsgroups:");
55         _("Subject:");
56 #endif
57
58 #if HAVE_LIBCOMPFACE
59 #define XPM_XFACE_HEIGHT        (HEIGHT + 3)  /* 3 = 1 header + 2 colors */
60
61 static gchar *xpm_xface[XPM_XFACE_HEIGHT];
62
63 static void headerview_show_xface       (HeaderView     *headerview,
64                                          MsgInfo        *msginfo);
65 static gint create_xpm_from_xface       (gchar          *xpm[],
66                                          const gchar    *xface);
67 #endif
68
69 HeaderView *headerview_create(void)
70 {
71         HeaderView *headerview;
72         GtkWidget *hbox;
73         GtkWidget *vbox;
74         GtkWidget *hbox1;
75         GtkWidget *hbox2;
76         GtkWidget *from_header_label;
77         GtkWidget *from_body_label;
78         GtkWidget *to_header_label;
79         GtkWidget *to_body_label;
80         GtkWidget *ng_header_label;
81         GtkWidget *ng_body_label;
82         GtkWidget *subject_header_label;
83         GtkWidget *subject_body_label;
84
85         debug_print("Creating header view...\n");
86         headerview = g_new0(HeaderView, 1);
87
88         hbox = gtk_hbox_new(FALSE, 0);
89         gtk_container_set_border_width(GTK_CONTAINER(hbox), 2);
90         vbox = gtk_vbox_new(FALSE, 0);
91         gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
92
93         hbox1 = gtk_hbox_new(FALSE, 4);
94         gtk_box_pack_start(GTK_BOX(vbox), hbox1, FALSE, FALSE, 0);
95         hbox2 = gtk_hbox_new(FALSE, 4);
96         gtk_box_pack_start(GTK_BOX(vbox), hbox2, FALSE, FALSE, 0);
97
98         from_header_label    = gtk_label_new(TR("From:"));
99         from_body_label      = gtk_label_new("");
100         to_header_label      = gtk_label_new(TR("To:"));
101         to_body_label        = gtk_label_new("");
102         ng_header_label      = gtk_label_new(TR("Newsgroups:"));
103         ng_body_label        = gtk_label_new("");
104         subject_header_label = gtk_label_new(TR("Subject:"));
105         subject_body_label   = gtk_label_new("");
106
107         gtk_box_pack_start(GTK_BOX(hbox1), from_header_label, FALSE, FALSE, 0);
108         gtk_box_pack_start(GTK_BOX(hbox1), from_body_label, FALSE, FALSE, 0);
109         gtk_box_pack_start(GTK_BOX(hbox1), to_header_label, FALSE, FALSE, 0);
110         gtk_box_pack_start(GTK_BOX(hbox1), to_body_label, FALSE, FALSE, 0);
111         gtk_box_pack_start(GTK_BOX(hbox1), ng_header_label, FALSE, FALSE, 0);
112         gtk_box_pack_start(GTK_BOX(hbox1), ng_body_label, FALSE, FALSE, 0);
113         gtk_box_pack_start(GTK_BOX(hbox2), subject_header_label, FALSE, FALSE, 0);
114         gtk_box_pack_start(GTK_BOX(hbox2), subject_body_label, FALSE, FALSE, 0);
115
116         headerview->hbox = hbox;
117         headerview->from_header_label    = from_header_label;
118         headerview->from_body_label      = from_body_label;
119         headerview->to_header_label      = to_header_label;
120         headerview->to_body_label        = to_body_label;
121         headerview->ng_header_label      = ng_header_label;
122         headerview->ng_body_label        = ng_body_label;
123         headerview->subject_header_label = subject_header_label;
124         headerview->subject_body_label   = subject_body_label;
125         headerview->image = NULL;
126
127         gtk_widget_show_all(hbox);
128
129         return headerview;
130 }
131
132 void headerview_init(HeaderView *headerview)
133 {
134         static PangoFontDescription *boldfont = NULL;
135
136         if (!boldfont) {
137                 boldfont = pango_font_description_from_string(BOLD_FONT);
138                 pango_font_description_set_weight(boldfont, PANGO_WEIGHT_BOLD);
139         }
140
141         if (boldfont) {
142                 gtk_widget_modify_font(headerview->from_header_label, boldfont);
143                 gtk_widget_modify_font(headerview->to_header_label, boldfont);
144                 gtk_widget_modify_font(headerview->ng_header_label, boldfont);
145                 gtk_widget_modify_font(headerview->subject_header_label, boldfont);
146         }
147
148         headerview_clear(headerview);
149         headerview_set_visibility(headerview, prefs_common.display_header_pane);
150
151 #if HAVE_LIBCOMPFACE
152         {
153                 gint i;
154
155                 for (i = 0; i < XPM_XFACE_HEIGHT; i++) {
156                         xpm_xface[i] = g_malloc(WIDTH + 1);
157                         *xpm_xface[i] = '\0';
158                 }
159         }
160 #endif
161 }
162
163 void headerview_show(HeaderView *headerview, MsgInfo *msginfo)
164 {
165         headerview_clear(headerview);
166
167         gtk_label_set_text(GTK_LABEL(headerview->from_body_label),
168                            msginfo->from ? msginfo->from : _("(No From)"));
169         if (msginfo->to) {
170                 gtk_label_set_text(GTK_LABEL(headerview->to_body_label),
171                                    msginfo->to);
172                 gtk_widget_show(headerview->to_header_label);
173                 gtk_widget_show(headerview->to_body_label);
174         }
175         if (msginfo->newsgroups) {
176                 gtk_label_set_text(GTK_LABEL(headerview->ng_body_label),
177                                    msginfo->newsgroups);
178                 gtk_widget_show(headerview->ng_header_label);
179                 gtk_widget_show(headerview->ng_body_label);
180         }
181         gtk_label_set_text(GTK_LABEL(headerview->subject_body_label),
182                            msginfo->subject ? msginfo->subject :
183                            _("(No Subject)"));
184
185 #if HAVE_LIBCOMPFACE
186         headerview_show_xface(headerview, msginfo);
187 #endif
188 }
189
190 #if HAVE_LIBCOMPFACE
191 static void headerview_show_xface(HeaderView *headerview, MsgInfo *msginfo)
192 {
193         gchar xface[2048];
194         GdkPixmap *pixmap;
195         GdkBitmap *mask;
196         GtkWidget *hbox = headerview->hbox;
197
198         if (!msginfo->xface || strlen(msginfo->xface) < 5) {
199                 if (headerview->image &&
200                     GTK_WIDGET_VISIBLE(headerview->image)) {
201                         gtk_widget_hide(headerview->image);
202                         gtk_widget_queue_resize(hbox);
203                 }
204                 return;
205         }
206         if (!GTK_WIDGET_VISIBLE(headerview->hbox)) return;
207
208         strncpy(xface, msginfo->xface, sizeof(xface));
209
210         if (uncompface(xface) < 0) {
211                 g_warning("uncompface failed\n");
212                 if (headerview->image)
213                         gtk_widget_hide(headerview->image);
214                 return;
215         }
216
217         create_xpm_from_xface(xpm_xface, xface);
218
219         pixmap = gdk_pixmap_create_from_xpm_d
220                 (hbox->window, &mask, &hbox->style->white, xpm_xface);
221
222         if (!headerview->image) {
223                 GtkWidget *image;
224
225                 image = gtk_image_new_from_pixmap(pixmap, mask);
226                 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
227                 gtk_widget_show(image);
228                 headerview->image = image;
229         } else {
230                 gtk_image_set_from_pixmap(GTK_IMAGE(headerview->image), pixmap, mask);
231                 gtk_widget_show(headerview->image);
232         }
233
234         g_object_unref(pixmap);
235 }
236 #endif
237
238 void headerview_clear(HeaderView *headerview)
239 {
240         gtk_label_set_text(GTK_LABEL(headerview->from_body_label), "");
241         gtk_label_set_text(GTK_LABEL(headerview->to_body_label), "");
242         gtk_label_set_text(GTK_LABEL(headerview->ng_body_label), "");
243         gtk_label_set_text(GTK_LABEL(headerview->subject_body_label), "");
244         gtk_widget_hide(headerview->to_header_label);
245         gtk_widget_hide(headerview->to_body_label);
246         gtk_widget_hide(headerview->ng_header_label);
247         gtk_widget_hide(headerview->ng_body_label);
248
249         if (headerview->image && GTK_WIDGET_VISIBLE(headerview->image)) {
250                 gtk_widget_hide(headerview->image);
251                 gtk_widget_queue_resize(headerview->hbox);
252         }
253 }
254
255 void headerview_set_visibility(HeaderView *headerview, gboolean visibility)
256 {
257         if (visibility)
258                 gtk_widget_show(headerview->hbox);
259         else
260                 gtk_widget_hide(headerview->hbox);
261 }
262
263 void headerview_destroy(HeaderView *headerview)
264 {
265         g_free(headerview);
266 }
267
268 #if HAVE_LIBCOMPFACE
269 static gint create_xpm_from_xface(gchar *xpm[], const gchar *xface)
270 {
271         static gchar *bit_pattern[] = {
272                 "....",
273                 "...#",
274                 "..#.",
275                 "..##",
276                 ".#..",
277                 ".#.#",
278                 ".##.",
279                 ".###",
280                 "#...",
281                 "#..#",
282                 "#.#.",
283                 "#.##",
284                 "##..",
285                 "##.#",
286                 "###.",
287                 "####"
288         };
289
290         static gchar *xface_header = "48 48 2 1";
291         static gchar *xface_black  = "# c #000000";
292         static gchar *xface_white  = ". c #ffffff";
293
294         gint i, line = 0;
295         const guchar *p;
296         gchar buf[WIDTH * 4 + 1];  /* 4 = strlen("0x0000") */
297
298         p = xface;
299
300         strcpy(xpm[line++], xface_header);
301         strcpy(xpm[line++], xface_black);
302         strcpy(xpm[line++], xface_white);
303
304         for (i = 0; i < HEIGHT; i++) {
305                 gint col;
306
307                 buf[0] = '\0';
308      
309                 for (col = 0; col < 3; col++) {
310                         gint figure;
311
312                         p += 2;  /* skip '0x' */
313
314                         for (figure = 0; figure < 4; figure++) {
315                                 gint n = 0;
316
317                                 if ('0' <= *p && *p <= '9') {
318                                         n = *p - '0';
319                                 } else if ('a' <= *p && *p <= 'f') {
320                                         n = *p - 'a' + 10;
321                                 } else if ('A' <= *p && *p <= 'F') {
322                                         n = *p - 'A' + 10;
323                                 }
324
325                                 strcat(buf, bit_pattern[n]);
326                                 p++;  /* skip ',' */
327                         }
328
329                         p++;  /* skip '\n' */
330                 }
331
332                 strcpy(xpm[line++], buf);
333                 p++;
334         }
335
336         return 0;
337 }
338 #endif