38bf49562895ec9cd4697136cdb55c0762619314
[claws.git] / src / gtk / logwindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gdk/gdkkeysyms.h>
27 #include <gtk/gtkwidget.h>
28 #include <gtk/gtkwindow.h>
29 #include <gtk/gtksignal.h>
30 #include <gtk/gtkscrolledwindow.h>
31 #include <gtk/gtktextview.h>
32 #include <gtk/gtkstyle.h>
33 #include <gtk/gtkmenuitem.h>
34 #include <gtk/gtkseparatormenuitem.h>
35
36 #include "logwindow.h"
37 #include "utils.h"
38 #include "gtkutils.h"
39 #include "log.h"
40 #include "hooks.h"
41 #include "prefs_common.h"
42
43 static void hide_cb                             (GtkWidget      *widget,
44                                                  LogWindow      *logwin);
45 static gboolean key_pressed                     (GtkWidget      *widget,
46                                                  GdkEventKey    *event,
47                                                  LogWindow      *logwin);
48 static void size_allocate_cb    (GtkWidget *widget,
49                                          GtkAllocation *allocation);
50 static gboolean log_window_append               (gpointer        source,
51                                                  gpointer        data);
52 static void log_window_clip                     (GtkWidget      *text,
53                                                  guint           glip_length);
54 static void log_window_clear                    (GtkWidget      *widget,
55                                                  LogWindow      *logwin);
56 static void log_window_popup_menu_extend        (GtkTextView    *textview,
57                                                  GtkMenu        *menu,
58                                                  LogWindow      *logwin);
59                                          
60 /*!
61  *\brief        Save Gtk object size to prefs dataset
62  */
63 static void size_allocate_cb(GtkWidget *widget,
64                                          GtkAllocation *allocation)
65 {
66         g_return_if_fail(allocation != NULL);
67
68         prefs_common.logwin_width = allocation->width;
69         prefs_common.logwin_height = allocation->height;
70 }
71
72 LogWindow *log_window_create(void)
73 {
74         LogWindow *logwin;
75         GtkWidget *window;
76         GtkWidget *scrolledwin;
77         GtkWidget *text;
78         GtkTextBuffer *buffer;
79         GtkTextIter iter;
80         static GdkGeometry geometry;
81
82         debug_print("Creating log window...\n");
83
84         logwin = g_new0(LogWindow, 1);
85
86         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
87         gtk_window_set_title(GTK_WINDOW(window), _("Protocol log"));
88         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
89         g_signal_connect(G_OBJECT(window), "delete_event",
90                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
91         g_signal_connect(G_OBJECT(window), "key_press_event",
92                          G_CALLBACK(key_pressed), logwin);
93         g_signal_connect(G_OBJECT(window), "hide",
94                          G_CALLBACK(hide_cb), logwin);
95         gtk_widget_realize(window);
96
97         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
98         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
99                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
100         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
101                                             GTK_SHADOW_IN);
102         gtk_container_add(GTK_CONTAINER(window), scrolledwin);
103         gtk_widget_show(scrolledwin);
104
105         text = gtk_text_view_new();
106         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
107         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
108         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
109         gtk_text_buffer_get_start_iter(buffer, &iter);
110         gtk_text_buffer_create_mark(buffer, "end", &iter, FALSE);
111         g_signal_connect(G_OBJECT(text), "populate-popup",
112                          G_CALLBACK(log_window_popup_menu_extend), logwin);
113         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
114         gtk_widget_show(text);
115
116         g_signal_connect(G_OBJECT(window), "size_allocate",
117                          G_CALLBACK(size_allocate_cb), NULL);
118
119         if (!geometry.min_height) {
120                 geometry.min_width = 520;
121                 geometry.min_height = 400;
122         }
123
124         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
125                                       GDK_HINT_MIN_SIZE);
126         gtk_widget_set_size_request(window, prefs_common.logwin_width,
127                                     prefs_common.logwin_height);
128
129         logwin->window = window;
130         logwin->scrolledwin = scrolledwin;
131         logwin->text = text;
132         logwin->hook_id = hooks_register_hook(LOG_APPEND_TEXT_HOOKLIST, log_window_append, logwin);
133
134         return logwin;
135 }
136
137 void log_window_init(LogWindow *logwin)
138 {
139         GtkTextBuffer *buffer;
140         GdkColormap *colormap;
141         GdkColor color[5];
142         gboolean success[5];
143         gint i;
144
145         gtkut_convert_int_to_gdk_color(prefs_common.log_msg_color, &color[0]);
146         gtkut_convert_int_to_gdk_color(prefs_common.log_warn_color, &color[1]);
147         gtkut_convert_int_to_gdk_color(prefs_common.log_error_color, &color[2]);
148         gtkut_convert_int_to_gdk_color(prefs_common.log_in_color, &color[3]);
149         gtkut_convert_int_to_gdk_color(prefs_common.log_out_color, &color[4]);
150
151         logwin->msg_color = color[0];
152         logwin->warn_color = color[1];
153         logwin->error_color = color[2];
154         logwin->in_color = color[3];
155         logwin->out_color = color[4];
156
157         colormap = gdk_drawable_get_colormap(logwin->window->window);
158         gdk_colormap_alloc_colors(colormap, color, 5, FALSE, TRUE, success);
159
160         for (i = 0; i < 5; i++) {
161                 if (success[i] == FALSE) {
162                         GtkStyle *style;
163
164                         g_warning("LogWindow: color allocation failed\n");
165                         style = gtk_widget_get_style(logwin->window);
166                         logwin->msg_color = logwin->warn_color =
167                         logwin->error_color = logwin->in_color =
168                         logwin->out_color = style->black;
169                         break;
170                 }
171         }
172
173         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
174         gtk_text_buffer_create_tag(buffer, "message",
175                                    "foreground-gdk", &logwin->msg_color,
176                                    NULL);
177         gtk_text_buffer_create_tag(buffer, "warn",
178                                    "foreground-gdk", &logwin->warn_color,
179                                    NULL);
180         gtk_text_buffer_create_tag(buffer, "error",
181                                    "foreground-gdk", &logwin->error_color,
182                                    NULL);
183         gtk_text_buffer_create_tag(buffer, "input",
184                                    "foreground-gdk", &logwin->in_color,
185                                    NULL);
186         gtk_text_buffer_create_tag(buffer, "output",
187                                    "foreground-gdk", &logwin->out_color,
188                                    NULL);
189 }
190
191 void log_window_show(LogWindow *logwin)
192 {
193         GtkTextView *text = GTK_TEXT_VIEW(logwin->text);
194         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
195         GtkTextMark *mark;
196
197         mark = gtk_text_buffer_get_mark(buffer, "end");
198         gtk_text_view_scroll_mark_onscreen(text, mark);
199
200         gtk_widget_show(logwin->window);
201 }
202
203 void log_window_set_clipping(LogWindow *logwin, gboolean clip, guint clip_length)
204 {
205         g_return_if_fail(logwin != NULL);
206
207         logwin->clip = clip;
208         logwin->clip_length = clip_length;
209 }
210
211 static gboolean log_window_append(gpointer source, gpointer data)
212 {
213         LogText *logtext = (LogText *) source;
214         LogWindow *logwindow = (LogWindow *) data;
215         GtkTextView *text;
216         GtkTextBuffer *buffer;
217         GtkTextIter iter;
218         gchar *head = NULL;
219         const gchar *tag;
220
221         g_return_val_if_fail(logtext != NULL, TRUE);
222         g_return_val_if_fail(logtext->text != NULL, TRUE);
223         g_return_val_if_fail(logwindow != NULL, FALSE);
224
225         if (logwindow->clip && !logwindow->clip_length)
226                 return FALSE;
227
228         text = GTK_TEXT_VIEW(logwindow->text);
229         buffer = gtk_text_view_get_buffer(text);
230         gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
231
232         switch (logtext->type) {
233         case LOG_MSG:
234                 tag = "message";
235                 head = "* ";
236                 break;
237         case LOG_WARN:
238                 tag = "warn";
239                 head = "** ";
240                 break;
241         case LOG_ERROR:
242                 tag = "error";
243                 head = "*** ";
244                 break;
245         default:
246                 tag = NULL;
247                 break;
248         }
249   
250         if (tag == NULL) {
251                 if (strstr(logtext->text, "] POP3>")
252                 ||  strstr(logtext->text, "] IMAP4>")
253                 ||  strstr(logtext->text, "] SMTP>")
254                 ||  strstr(logtext->text, "] ESMTP>")
255                 ||  strstr(logtext->text, "] NNTP>"))
256                         tag = "output";
257                 if (strstr(logtext->text, "] POP3<")
258                 ||  strstr(logtext->text, "] IMAP4<")
259                 ||  strstr(logtext->text, "] SMTP<")
260                 ||  strstr(logtext->text, "] ESMTP<")
261                 ||  strstr(logtext->text, "] NNTP<"))
262                         tag = "input";
263         }
264
265         if (head)
266                 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, head, -1,
267                                                          tag, NULL);
268         gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, logtext->text, -1,
269                                                  tag, NULL);
270
271         gtk_text_buffer_get_start_iter(buffer, &iter);
272
273         if (logwindow->clip)
274                log_window_clip (GTK_WIDGET (text), logwindow->clip_length);
275
276         gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
277         gtk_text_view_scroll_to_iter(text, &iter, 0, TRUE, 0, 0);
278         gtk_text_buffer_place_cursor(buffer, &iter);
279
280         return FALSE;
281 }
282
283 static void hide_cb(GtkWidget *widget, LogWindow *logwin)
284 {
285 }
286
287 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
288                         LogWindow *logwin)
289 {
290         if (event && event->keyval == GDK_Escape)
291                 gtk_widget_hide(logwin->window);
292         else if (event && event->keyval == GDK_Delete) 
293                 log_window_clear(NULL, logwin);
294
295         return FALSE;
296 }
297
298 static void log_window_clip(GtkWidget *textw, guint clip_length)
299 {
300         guint length;
301         guint point;
302         GtkTextView *textview = GTK_TEXT_VIEW(textw);
303         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(textview);
304         GtkTextIter start_iter, end_iter;
305         
306         length = gtk_text_buffer_get_line_count(textbuf);
307         /* debug_print("Log window length: %u\n", length); */
308         
309         if (length > clip_length) {
310                 /* find the end of the first line after the cut off
311                  * point */
312                 point = length - clip_length;
313                 gtk_text_buffer_get_iter_at_line(textbuf, &end_iter, point);
314                 if (!gtk_text_iter_forward_to_line_end(&end_iter))
315                         return;
316                 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
317                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
318         }
319 }
320
321 static void log_window_clear(GtkWidget *widget, LogWindow *logwin)
322 {
323         GtkTextView *textview = GTK_TEXT_VIEW(logwin->text);
324         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(textview);
325         GtkTextIter start_iter, end_iter;
326         
327         gtk_text_buffer_get_start_iter(textbuf, &start_iter);
328         gtk_text_buffer_get_end_iter(textbuf, &end_iter);
329         gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
330 }
331
332 static void log_window_popup_menu_extend(GtkTextView *textview,
333                         GtkMenu *menu, LogWindow *logwin)
334 {
335         GtkWidget *menuitem;
336         
337         g_return_if_fail(menu != NULL);
338         g_return_if_fail(GTK_IS_MENU_SHELL(menu));
339
340         menuitem = gtk_separator_menu_item_new();
341         gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), menuitem);
342         gtk_widget_show(menuitem);
343         
344         menuitem = gtk_menu_item_new_with_mnemonic(_("Clear _Log"));
345         g_signal_connect(G_OBJECT(menuitem), "activate",
346                          G_CALLBACK(log_window_clear), logwin);
347         gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), menuitem);
348         gtk_widget_show(menuitem);
349 }
350