bugfix-try: always try to show msginfo in headerwindow.c
[claws.git] / src / headerwindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 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         //if (!normalfont)
92         //      normalfont = gdk_fontset_load(NORMAL_FONT);
93         if (!boldfont)
94                 boldfont = gdk_fontset_load(BOLD_FONT);
95 }
96
97 void header_window_show(HeaderWindow *headerwin, MsgInfo *msginfo)
98 {
99         gchar *file;
100         FILE *fp;
101         gchar buf[BUFFSIZE], tmp[BUFFSIZE];
102         GtkText *text = GTK_TEXT(headerwin->text);
103         gboolean parse_next;
104
105         g_return_if_fail(msginfo != NULL);
106
107         file = procmsg_get_message_file_path(msginfo);
108         if ((fp = fopen(file, "r")) == NULL) {
109                 FILE_OP_ERROR(file, "fopen");
110                 g_free(file);
111                 return;
112         }
113
114         debug_print(_("Displaying the header of %s ...\n"), file);
115
116         g_snprintf(buf, sizeof(buf), _("%s - All header"), file);
117         gtk_window_set_title(GTK_WINDOW(headerwin->window), buf);
118         g_free(file);
119
120         gtk_text_freeze(text);
121         gtk_text_backward_delete(text, gtk_text_get_length(text));
122         gtk_text_thaw(text);
123         gtk_text_freeze(text);
124
125         parse_next = MSG_IS_QUEUED(msginfo->flags);
126
127         for (;;) {
128                 gint val;
129                 gchar *p;
130
131                 val = procheader_get_one_field(buf, sizeof(buf), fp, NULL);
132                 if (val == -1) {
133                         if (parse_next) {
134                                 parse_next = FALSE;
135                                 gtk_text_insert(text, normalfont, NULL, NULL,
136                                                 "\n", 1);
137                                 continue;
138                         } else
139                                 break;
140                 }
141                 conv_unmime_header(tmp, sizeof(tmp), buf, NULL);
142                 if ((p = strchr(tmp, ':')) != NULL) {
143                         gtk_text_insert(text, boldfont, NULL, NULL,
144                                         tmp, p - tmp + 1);
145                         gtk_text_insert(text, normalfont, NULL, NULL,
146                                         p + 1, -1);
147                 } else
148                         gtk_text_insert(text, normalfont, NULL, NULL, tmp, -1);
149                 gtk_text_insert(text, NULL, NULL, NULL, "\n", 1);
150         }
151
152         fclose(fp);
153         gtk_text_thaw(text);
154 }
155
156 void header_window_show_cb(gpointer data, guint action, GtkWidget *widget)
157 {
158         MainWindow *mainwin = data;
159         GtkCTreeNode *node = mainwin->summaryview->displayed;
160         MsgInfo *msginfo;
161
162         g_return_if_fail(node != NULL);
163
164         msginfo = gtk_ctree_node_get_row_data
165                 (GTK_CTREE(mainwin->summaryview->ctree), node);
166         header_window_show(mainwin->headerwin, msginfo);
167
168         gtk_widget_hide(mainwin->headerwin->window);
169         gtk_widget_show(mainwin->headerwin->window);
170 }
171
172 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
173                         HeaderWindow *headerwin)
174 {
175         if (event && event->keyval == GDK_Escape)
176                 gtk_widget_hide(headerwin->window);
177 }