changes and bugfixes submitted by Andre Eibel
[claws.git] / src / headerwindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 #include "defs.h"
21
22 #include <glib.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtkwidget.h>
25 #include <gtk/gtkwindow.h>
26 #include <gtk/gtksignal.h>
27 #include <gtk/gtkcontainer.h>
28 #include <gtk/gtkscrolledwindow.h>
29 #include <gtk/gtktext.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "intl.h"
34 #include "main.h"
35 #include "headerwindow.h"
36 #include "mainwindow.h"
37 #include "prefs_common.h"
38 #include "procheader.h"
39 #include "procmsg.h"
40 #include "codeconv.h"
41 #include "utils.h"
42
43 static GdkFont *normalfont;
44 static GdkFont *boldfont;
45
46 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
47                         HeaderWindow *headerwin);
48
49 HeaderWindow *header_window_create(void)
50 {
51         HeaderWindow *headerwin;
52         GtkWidget *window;
53         GtkWidget *scrolledwin;
54         GtkWidget *text;
55
56         debug_print(_("Creating header window...\n"));
57         headerwin = g_new0(HeaderWindow, 1);
58
59         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
60         gtk_window_set_title(GTK_WINDOW(window), _("All header"));
61         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
62         gtk_widget_set_usize(window, 600, 500);
63         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
64                            GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete), NULL);
65         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
66                            GTK_SIGNAL_FUNC(key_pressed), headerwin);
67         gtk_widget_realize(window);
68
69         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
70         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
71                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
72         gtk_container_add(GTK_CONTAINER(window), scrolledwin);
73         gtk_widget_show(scrolledwin);
74
75         text = gtk_text_new(gtk_scrolled_window_get_hadjustment
76                             (GTK_SCROLLED_WINDOW(scrolledwin)),
77                             gtk_scrolled_window_get_vadjustment
78                             (GTK_SCROLLED_WINDOW(scrolledwin)));
79         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
80         gtk_widget_show(text);
81
82         headerwin->window = window;
83         headerwin->scrolledwin = scrolledwin;
84         headerwin->text = text;
85
86         return headerwin;
87 }
88
89 void header_window_init(HeaderWindow *headerwin)
90 {
91         /*
92         if (!normalfont)
93                 normalfont = gdk_fontset_load(NORMAL_FONT);
94         */      
95         if (!boldfont)
96                 boldfont = gdk_fontset_load(BOLD_FONT);
97 }
98
99 void header_window_show(HeaderWindow *headerwin, MsgInfo *msginfo)
100 {
101         gchar *file;
102         FILE *fp;
103         gchar buf[BUFFSIZE], tmp[BUFFSIZE];
104         GtkText *text = GTK_TEXT(headerwin->text);
105         gboolean parse_next;
106
107         g_return_if_fail(msginfo != NULL);
108
109         file = procmsg_get_message_file_path(msginfo);
110         if ((fp = fopen(file, "r")) == NULL) {
111                 FILE_OP_ERROR(file, "fopen");
112                 g_free(file);
113                 return;
114         }
115
116         debug_print(_("Displaying the header of %s ...\n"), file);
117
118         g_snprintf(buf, sizeof(buf), _("%s - All header"), file);
119         gtk_window_set_title(GTK_WINDOW(headerwin->window), buf);
120         g_free(file);
121
122         gtk_text_freeze(text);
123         gtk_text_backward_delete(text, gtk_text_get_length(text));
124         gtk_text_thaw(text);
125         gtk_text_freeze(text);
126
127         parse_next = MSG_IS_QUEUED(msginfo->flags);
128
129         for (;;) {
130                 gint val;
131                 gchar *p;
132
133                 val = procheader_get_one_field(buf, sizeof(buf), fp, NULL);
134                 if (val == -1) {
135                         if (parse_next) {
136                                 parse_next = FALSE;
137                                 gtk_text_insert(text, normalfont, NULL, NULL,
138                                                 "\n", 1);
139                                 continue;
140                         } else
141                                 break;
142                 }
143                 conv_unmime_header(tmp, sizeof(tmp), buf, NULL);
144                 if ((p = strchr(tmp, ':')) != NULL) {
145                         gtk_text_insert(text, boldfont, NULL, NULL,
146                                         tmp, p - tmp + 1);
147                         gtk_text_insert(text, normalfont, NULL, NULL,
148                                         p + 1, -1);
149                 } else
150                         gtk_text_insert(text, normalfont, NULL, NULL, tmp, -1);
151                 gtk_text_insert(text, NULL, NULL, NULL, "\n", 1);
152         }
153
154         fclose(fp);
155         gtk_text_thaw(text);
156 }
157
158 void header_window_show_cb(gpointer data, guint action, GtkWidget *widget)
159 {
160         MainWindow *mainwin = data;
161         GtkCTreeNode *node = mainwin->summaryview->selected;
162         MsgInfo *msginfo;
163
164         if (node) {
165                 msginfo = gtk_ctree_node_get_row_data
166                         (GTK_CTREE(mainwin->summaryview->ctree), node);
167                 header_window_show(mainwin->headerwin, msginfo);
168         }
169
170         gtk_widget_hide(mainwin->headerwin->window);
171         gtk_widget_show(mainwin->headerwin->window);
172 }
173
174 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
175                         HeaderWindow *headerwin)
176 {
177         if (event && event->keyval == GDK_Escape)
178                 gtk_widget_hide(headerwin->window);
179 }