* src/common/plugin.[ch]
[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/gtkpixmap.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 static GdkFont *boldfont;
50
51 #define TR(str) (prefs_common.trans_hdr ? gettext(str) : str)
52
53 #if 0
54         _("From:");
55         _("To:");
56         _("Newsgroups:");
57         _("Subject:");
58 #endif
59
60 #if HAVE_LIBCOMPFACE
61 #define XPM_XFACE_HEIGHT        (HEIGHT + 3)  /* 3 = 1 header + 2 colors */
62
63 static gchar *xpm_xface[XPM_XFACE_HEIGHT];
64
65 static void headerview_show_xface       (HeaderView     *headerview,
66                                          MsgInfo        *msginfo);
67 static gint create_xpm_from_xface       (gchar          *xpm[],
68                                          const gchar    *xface);
69 #endif
70
71 HeaderView *headerview_create(void)
72 {
73         HeaderView *headerview;
74         GtkWidget *hbox;
75         GtkWidget *vbox;
76         GtkWidget *hbox1;
77         GtkWidget *hbox2;
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
87         debug_print("Creating header view...\n");
88         headerview = g_new0(HeaderView, 1);
89
90         hbox = gtk_hbox_new(FALSE, 0);
91         gtk_container_set_border_width(GTK_CONTAINER(hbox), 2);
92         vbox = gtk_vbox_new(FALSE, 0);
93         gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
94
95         hbox1 = gtk_hbox_new(FALSE, 4);
96         gtk_box_pack_start(GTK_BOX(vbox), hbox1, FALSE, FALSE, 0);
97         hbox2 = gtk_hbox_new(FALSE, 4);
98         gtk_box_pack_start(GTK_BOX(vbox), hbox2, FALSE, FALSE, 0);
99
100         from_header_label    = gtk_label_new(TR("From:"));
101         from_body_label      = gtk_label_new("");
102         to_header_label      = gtk_label_new(TR("To:"));
103         to_body_label        = gtk_label_new("");
104         ng_header_label      = gtk_label_new(TR("Newsgroups:"));
105         ng_body_label        = gtk_label_new("");
106         subject_header_label = gtk_label_new(TR("Subject:"));
107         subject_body_label   = gtk_label_new("");
108
109         gtk_box_pack_start(GTK_BOX(hbox1), from_header_label, FALSE, FALSE, 0);
110         gtk_box_pack_start(GTK_BOX(hbox1), from_body_label, FALSE, FALSE, 0);
111         gtk_box_pack_start(GTK_BOX(hbox1), to_header_label, FALSE, FALSE, 0);
112         gtk_box_pack_start(GTK_BOX(hbox1), to_body_label, FALSE, FALSE, 0);
113         gtk_box_pack_start(GTK_BOX(hbox1), ng_header_label, FALSE, FALSE, 0);
114         gtk_box_pack_start(GTK_BOX(hbox1), ng_body_label, FALSE, FALSE, 0);
115         gtk_box_pack_start(GTK_BOX(hbox2), subject_header_label, FALSE, FALSE, 0);
116         gtk_box_pack_start(GTK_BOX(hbox2), subject_body_label, FALSE, FALSE, 0);
117
118         headerview->hbox = hbox;
119         headerview->from_header_label    = from_header_label;
120         headerview->from_body_label      = from_body_label;
121         headerview->to_header_label      = to_header_label;
122         headerview->to_body_label        = to_body_label;
123         headerview->ng_header_label      = ng_header_label;
124         headerview->ng_body_label        = ng_body_label;
125         headerview->subject_header_label = subject_header_label;
126         headerview->subject_body_label   = subject_body_label;
127         headerview->image = NULL;
128
129         gtk_widget_show_all(hbox);
130
131         return headerview;
132 }
133
134 void headerview_init(HeaderView *headerview)
135 {
136         if (!boldfont)
137                 boldfont = gtkut_font_load(BOLD_FONT);
138
139 #define SET_FONT_STYLE(wid) \
140 { \
141         GtkStyle *style; \
142  \
143         style = gtk_style_copy(gtk_widget_get_style(headerview->wid)); \
144         if (boldfont) \
145                 style->font = boldfont; \
146         gtk_widget_set_style(headerview->wid, style); \
147 }
148
149         SET_FONT_STYLE(from_header_label);
150         SET_FONT_STYLE(to_header_label);
151         SET_FONT_STYLE(ng_header_label);
152         SET_FONT_STYLE(subject_header_label);
153
154         headerview_clear(headerview);
155         headerview_set_visibility(headerview, prefs_common.display_header_pane);
156
157 #if HAVE_LIBCOMPFACE
158         {
159                 gint i;
160
161                 for (i = 0; i < XPM_XFACE_HEIGHT; i++) {
162                         xpm_xface[i] = g_malloc(WIDTH + 1);
163                         *xpm_xface[i] = '\0';
164                 }
165         }
166 #endif
167 }
168
169 void headerview_show(HeaderView *headerview, MsgInfo *msginfo)
170 {
171         gchar *str;
172
173         headerview_clear(headerview);
174
175         if (msginfo->from) {
176                 Xstrdup_a(str, msginfo->from, return);
177                 conv_unreadable_locale(str);
178         } else
179                 str = NULL;
180         gtk_label_set_text(GTK_LABEL(headerview->from_body_label),
181                            str ? str : _("(No From)"));
182         if (msginfo->to) {
183                 Xstrdup_a(str, msginfo->to, return);
184                 conv_unreadable_locale(str);
185                 gtk_label_set_text(GTK_LABEL(headerview->to_body_label), str);
186                 gtk_widget_show(headerview->to_header_label);
187                 gtk_widget_show(headerview->to_body_label);
188         }
189         if (msginfo->newsgroups) {
190                 Xstrdup_a(str, msginfo->newsgroups, return);
191                 conv_unreadable_locale(str);
192                 gtk_label_set_text(GTK_LABEL(headerview->ng_body_label), str);
193                 gtk_widget_show(headerview->ng_header_label);
194                 gtk_widget_show(headerview->ng_body_label);
195         }
196         if (msginfo->subject) {
197                 Xstrdup_a(str, msginfo->subject, return);
198                 conv_unreadable_locale(str);
199         } else
200                 str = NULL;
201         gtk_label_set_text(GTK_LABEL(headerview->subject_body_label),
202                            str ? str : _("(No Subject)"));
203
204 #if HAVE_LIBCOMPFACE
205         headerview_show_xface(headerview, msginfo);
206 #endif
207 }
208
209 #if HAVE_LIBCOMPFACE
210 static void headerview_show_xface(HeaderView *headerview, MsgInfo *msginfo)
211 {
212         gchar xface[2048];
213         GdkPixmap *pixmap;
214         GdkBitmap *mask;
215         GtkWidget *hbox = headerview->hbox;
216
217         if (!msginfo->xface || strlen(msginfo->xface) < 5) {
218                 if (headerview->image &&
219                     GTK_WIDGET_VISIBLE(headerview->image)) {
220                         gtk_widget_hide(headerview->image);
221                         gtk_widget_queue_resize(hbox);
222                 }
223                 return;
224         }
225         if (!GTK_WIDGET_VISIBLE(headerview->hbox)) return;
226
227         strncpy(xface, msginfo->xface, sizeof(xface));
228
229         if (uncompface(xface) < 0) {
230                 g_warning("uncompface failed\n");
231                 if (headerview->image)
232                         gtk_widget_hide(headerview->image);
233                 return;
234         }
235
236         create_xpm_from_xface(xpm_xface, xface);
237
238         pixmap = gdk_pixmap_create_from_xpm_d
239                 (hbox->window, &mask, &hbox->style->white, xpm_xface);
240
241         if (!headerview->image) {
242                 GtkWidget *image;
243
244                 image = gtk_pixmap_new(pixmap, mask);
245                 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
246                 gtk_widget_show(image);
247                 headerview->image = image;
248         } else {
249                 gtk_pixmap_set(GTK_PIXMAP(headerview->image), pixmap, mask);
250                 gtk_widget_show(headerview->image);
251         }
252
253         gdk_pixmap_unref(pixmap);
254 }
255 #endif
256
257 void headerview_clear(HeaderView *headerview)
258 {
259         gtk_label_set_text(GTK_LABEL(headerview->from_body_label), "");
260         gtk_label_set_text(GTK_LABEL(headerview->to_body_label), "");
261         gtk_label_set_text(GTK_LABEL(headerview->ng_body_label), "");
262         gtk_label_set_text(GTK_LABEL(headerview->subject_body_label), "");
263         gtk_widget_hide(headerview->to_header_label);
264         gtk_widget_hide(headerview->to_body_label);
265         gtk_widget_hide(headerview->ng_header_label);
266         gtk_widget_hide(headerview->ng_body_label);
267
268         if (headerview->image && GTK_WIDGET_VISIBLE(headerview->image)) {
269                 gtk_widget_hide(headerview->image);
270                 gtk_widget_queue_resize(headerview->hbox);
271         }
272 }
273
274 void headerview_set_visibility(HeaderView *headerview, gboolean visibility)
275 {
276         if (visibility)
277                 gtk_widget_show(headerview->hbox);
278         else
279                 gtk_widget_hide(headerview->hbox);
280 }
281
282 void headerview_destroy(HeaderView *headerview)
283 {
284         g_free(headerview);
285 }
286
287 #if HAVE_LIBCOMPFACE
288 static gint create_xpm_from_xface(gchar *xpm[], const gchar *xface)
289 {
290         static gchar *bit_pattern[] = {
291                 "....",
292                 "...#",
293                 "..#.",
294                 "..##",
295                 ".#..",
296                 ".#.#",
297                 ".##.",
298                 ".###",
299                 "#...",
300                 "#..#",
301                 "#.#.",
302                 "#.##",
303                 "##..",
304                 "##.#",
305                 "###.",
306                 "####"
307         };
308
309         static gchar *xface_header = "48 48 2 1";
310         static gchar *xface_black  = "# c #000000";
311         static gchar *xface_white  = ". c #ffffff";
312
313         gint i, line = 0;
314         const guchar *p;
315         gchar buf[WIDTH * 4 + 1];  /* 4 = strlen("0x0000") */
316
317         p = xface;
318
319         strcpy(xpm[line++], xface_header);
320         strcpy(xpm[line++], xface_black);
321         strcpy(xpm[line++], xface_white);
322
323         for (i = 0; i < HEIGHT; i++) {
324                 gint col;
325
326                 buf[0] = '\0';
327      
328                 for (col = 0; col < 3; col++) {
329                         gint figure;
330
331                         p += 2;  /* skip '0x' */
332
333                         for (figure = 0; figure < 4; figure++) {
334                                 gint n = 0;
335
336                                 if ('0' <= *p && *p <= '9') {
337                                         n = *p - '0';
338                                 } else if ('a' <= *p && *p <= 'f') {
339                                         n = *p - 'a' + 10;
340                                 } else if ('A' <= *p && *p <= 'F') {
341                                         n = *p - 'A' + 10;
342                                 }
343
344                                 strcat(buf, bit_pattern[n]);
345                                 p++;  /* skip ',' */
346                         }
347
348                         p++;  /* skip '\n' */
349                 }
350
351                 strcpy(xpm[line++], buf);
352                 p++;
353         }
354
355         return 0;
356 }
357 #endif