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