Fix memory leak when freeing RSSyl's FeedItem struct.
[claws.git] / src / sourcewindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <gtk/gtk.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "sourcewindow.h"
30 #include "utils.h"
31 #include "gtkutils.h"
32 #include "prefs_common.h"
33
34 static void source_window_size_alloc_cb (GtkWidget      *widget,
35                                          GtkAllocation  *allocation);
36 static gint source_window_delete_cb     (GtkWidget      *widget,
37                                          GdkEventAny    *event,
38                                          SourceWindow   *sourcewin);
39 static gboolean key_pressed             (GtkWidget      *widget,
40                                          GdkEventKey    *event,
41                                          SourceWindow   *sourcewin);
42 static void source_window_append        (SourceWindow   *sourcewin,
43                                          const gchar    *str);
44 static void source_window_destroy       (SourceWindow   *sourcewin);
45
46 static void source_window_init()
47 {
48 }
49
50 SourceWindow *source_window_create(void)
51 {
52         SourceWindow *sourcewin;
53         GtkWidget *window;
54         GtkWidget *scrolledwin;
55         GtkWidget *text;
56         static PangoFontDescription *font_desc = NULL;
57
58         static GdkGeometry geometry;
59         
60         debug_print("Creating source window...\n");
61         sourcewin = g_new0(SourceWindow, 1);
62
63         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "sourcewindow");
64         gtk_window_set_title(GTK_WINDOW(window), _("Source of the message"));
65         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
66         gtk_widget_set_size_request(window, prefs_common.sourcewin_width,
67                                     prefs_common.sourcewin_height);
68         
69         if (!geometry.min_height) {
70                 geometry.min_width = 400;
71                 geometry.min_height = 320;
72         }
73         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
74                                       GDK_HINT_MIN_SIZE);
75
76         g_signal_connect(G_OBJECT(window), "size_allocate",
77                          G_CALLBACK(source_window_size_alloc_cb),
78                          sourcewin);
79         g_signal_connect(G_OBJECT(window), "delete_event",
80                          G_CALLBACK(source_window_delete_cb), sourcewin);
81         g_signal_connect(G_OBJECT(window), "key_press_event",
82                          G_CALLBACK(key_pressed), sourcewin);
83         gtk_widget_realize(window);
84
85         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
86         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
87                                        GTK_POLICY_AUTOMATIC,
88                                        GTK_POLICY_AUTOMATIC);
89         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
90                                             GTK_SHADOW_IN);
91         gtk_container_add(GTK_CONTAINER(window), scrolledwin);
92         gtk_widget_show(scrolledwin);
93
94         text = gtk_text_view_new();
95         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD_CHAR);
96         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
97         if (!font_desc && prefs_common.textfont)
98                 font_desc = pango_font_description_from_string
99                                         (prefs_common.textfont);
100         if (font_desc)
101                 gtk_widget_modify_font(text, font_desc);
102         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
103         gtk_widget_show(text);
104
105         sourcewin->window = window;
106         sourcewin->scrolledwin = scrolledwin;
107         sourcewin->text = text;
108
109         source_window_init();
110
111         return sourcewin;
112 }
113
114 void source_window_show(SourceWindow *sourcewin)
115 {
116         gtk_widget_show_all(sourcewin->window);
117 }
118
119 static void source_window_destroy(SourceWindow *sourcewin)
120 {
121         if (sourcewin->updating) {
122                 debug_print("deferring destroy\n");
123                 sourcewin->deferred_destroy = TRUE;
124                 return;
125         }
126         gtk_widget_destroy(sourcewin->window);
127         g_free(sourcewin);
128 }
129
130 void source_window_show_msg(SourceWindow *sourcewin, MsgInfo *msginfo)
131 {
132         gchar *file;
133         gchar *title;
134         FILE *fp;
135         gchar buf[BUFFSIZE];
136
137         cm_return_if_fail(msginfo != NULL);
138
139         sourcewin->updating = TRUE;
140         file = procmsg_get_message_file(msginfo);
141         sourcewin->updating = FALSE;
142         
143         if (sourcewin->deferred_destroy) {
144                 g_free(file);
145                 source_window_destroy(sourcewin);
146                 return;
147         }
148
149         cm_return_if_fail(file != NULL);
150
151         if ((fp = g_fopen(file, "rb")) == NULL) {
152                 FILE_OP_ERROR(file, "fopen");
153                 g_free(file);
154                 return;
155         }
156
157         debug_print("Displaying the source of %s ...\n", file);
158
159         title = g_strdup_printf(_("%s - Source"), file);
160         gtk_window_set_title(GTK_WINDOW(sourcewin->window), title);
161         g_free(title);
162         g_free(file);
163
164         while (fgets(buf, sizeof(buf), fp) != NULL)
165                 source_window_append(sourcewin, buf);
166
167         fclose(fp);
168 }
169
170 static void source_window_append(SourceWindow *sourcewin, const gchar *str)
171 {
172         GtkTextView *text = GTK_TEXT_VIEW(sourcewin->text);
173         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
174         GtkTextIter iter;
175         gchar *out;
176         gint len;
177
178         len = strlen(str) + 1;
179         Xalloca(out, len, return);
180         conv_utf8todisp(out, len, str);
181
182         gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
183         gtk_text_buffer_insert(buffer, &iter, out, -1);
184 }
185
186 static void source_window_size_alloc_cb(GtkWidget *widget,
187                                         GtkAllocation *allocation)
188 {
189         cm_return_if_fail(allocation != NULL);
190
191         prefs_common.sourcewin_width  = allocation->width;
192         prefs_common.sourcewin_height = allocation->height;
193 }
194
195 static gint source_window_delete_cb(GtkWidget *widget, GdkEventAny *event,
196                                     SourceWindow *sourcewin)
197 {
198         source_window_destroy(sourcewin);
199         return TRUE;
200 }
201
202 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
203                             SourceWindow *sourcewin)
204 {
205
206         if (!event || !sourcewin) return FALSE;
207         
208         switch (event->keyval) {
209         case GDK_KEY_A:
210         case GDK_KEY_a:
211                 if ((event->state & GDK_CONTROL_MASK) != 0)
212                         gtk_editable_select_region(GTK_EDITABLE(sourcewin->text), 0, -1);
213                 break;
214         case GDK_KEY_W:
215         case GDK_KEY_w:
216                 if ((event->state & GDK_CONTROL_MASK) != 0)
217                         gtk_widget_destroy(sourcewin->window);
218                 break;
219         case GDK_KEY_Escape:
220                 source_window_destroy(sourcewin);
221                 return TRUE;
222                 break;
223         }
224
225         return FALSE;
226 }