0.8.5claws173
[claws.git] / src / sourcewindow.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/gtkscrolledwindow.h>
28 #include <gtk/gtktext.h>
29 #include <gtk/gtkstyle.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "intl.h"
34 #include "sourcewindow.h"
35 #include "procmsg.h"
36 #include "utils.h"
37 #include "gtkutils.h"
38 #include "prefs_common.h"
39
40 static void source_window_destroy_cb    (GtkWidget      *widget,
41                                          SourceWindow   *sourcewin);
42 static void key_pressed                 (GtkWidget      *widget,
43                                          GdkEventKey    *event,
44                                          SourceWindow   *sourcewin);
45
46 static GdkFont *msgfont = NULL;
47
48 static void source_window_init()
49 {
50         if (!msgfont && prefs_common.textfont)
51                 msgfont = gtkut_font_load(prefs_common.textfont);
52 }
53
54 SourceWindow *source_window_create(void)
55 {
56         SourceWindow *sourcewin;
57         GtkWidget *window;
58         GtkWidget *scrolledwin;
59         GtkWidget *text;
60
61         debug_print("Creating source window...\n");
62         sourcewin = g_new0(SourceWindow, 1);
63
64         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
65         gtk_window_set_title(GTK_WINDOW(window), _("Source of the message"));
66         gtk_window_set_wmclass(GTK_WINDOW(window), "source_window", "Sylpheed");
67         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
68         gtk_widget_set_usize(window, 600, 500);
69         gtk_signal_connect(GTK_OBJECT(window), "destroy",
70                            GTK_SIGNAL_FUNC(source_window_destroy_cb),
71                            sourcewin);
72         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
73                            GTK_SIGNAL_FUNC(key_pressed), sourcewin);
74         gtk_widget_realize(window);
75
76         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
77         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
78                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
79         gtk_container_add(GTK_CONTAINER(window), scrolledwin);
80         gtk_widget_show(scrolledwin);
81
82         text = gtk_text_new(gtk_scrolled_window_get_hadjustment
83                             (GTK_SCROLLED_WINDOW(scrolledwin)),
84                             gtk_scrolled_window_get_vadjustment
85                             (GTK_SCROLLED_WINDOW(scrolledwin)));
86         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
87         gtk_widget_show(text);
88
89         sourcewin->window = window;
90         sourcewin->scrolledwin = scrolledwin;
91         sourcewin->text = text;
92
93         source_window_init();
94
95         return sourcewin;
96 }
97
98 void source_window_show(SourceWindow *sourcewin)
99 {
100         gtk_widget_show_all(sourcewin->window);
101 }
102
103 void source_window_destroy(SourceWindow *sourcewin)
104 {
105         g_free(sourcewin);
106 }
107
108 void source_window_show_msg(SourceWindow *sourcewin, MsgInfo *msginfo)
109 {
110         gchar *file;
111         gchar *title;
112         FILE *fp;
113         gchar buf[BUFFSIZE];
114
115         g_return_if_fail(msginfo != NULL);
116
117         file = procmsg_get_message_file(msginfo);
118         g_return_if_fail(file != NULL);
119
120         if ((fp = fopen(file, "rb")) == NULL) {
121                 FILE_OP_ERROR(file, "fopen");
122                 g_free(file);
123                 return;
124         }
125
126         debug_print("Displaying the source of %s ...\n", file);
127
128         title = g_strdup_printf(_("%s - Source"), file);
129         gtk_window_set_title(GTK_WINDOW(sourcewin->window), title);
130         g_free(title);
131         g_free(file);
132
133         gtk_text_freeze(GTK_TEXT(sourcewin->text));
134
135         while (fgets(buf, sizeof(buf), fp) != NULL)
136                 source_window_append(sourcewin, buf);
137
138         gtk_text_thaw(GTK_TEXT(sourcewin->text));
139
140         fclose(fp);
141 }
142
143 void source_window_append(SourceWindow *sourcewin, const gchar *str)
144 {
145         gtk_text_insert(GTK_TEXT(sourcewin->text), msgfont, NULL, NULL,
146                         str, -1);
147 }
148
149 static void source_window_destroy_cb(GtkWidget *widget,
150                                      SourceWindow *sourcewin)
151 {
152         source_window_destroy(sourcewin);
153 }
154
155 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
156                         SourceWindow *sourcewin)
157 {
158         if (event && event->keyval == GDK_Escape)
159                 gtk_widget_destroy(sourcewin->window);
160 }