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