2007-03-14 [colin] 2.8.1cvs10
[claws.git] / src / gtk / logwindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 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 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                     (LogWindow      *logwin,
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 = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "logwindow");
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_after(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         logwin->buffer = buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
109
110         g_object_ref(G_OBJECT(logwin->buffer));
111         gtk_text_view_set_buffer(GTK_TEXT_VIEW(text), NULL);
112         logwin->hidden = TRUE;
113         logwin->never_shown = TRUE;
114
115         gtk_text_buffer_get_start_iter(buffer, &iter);
116         logwin->end_mark = gtk_text_buffer_create_mark(buffer, "end", &iter, FALSE);
117
118         g_signal_connect(G_OBJECT(text), "populate-popup",
119                          G_CALLBACK(log_window_popup_menu_extend), logwin);
120         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
121         gtk_widget_show(text);
122
123         g_signal_connect(G_OBJECT(window), "size_allocate",
124                          G_CALLBACK(size_allocate_cb), NULL);
125
126         if (!geometry.min_height) {
127                 geometry.min_width = 520;
128                 geometry.min_height = 400;
129         }
130
131         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
132                                       GDK_HINT_MIN_SIZE);
133         gtk_widget_set_size_request(window, prefs_common.logwin_width,
134                                     prefs_common.logwin_height);
135
136         logwin->window = window;
137         logwin->scrolledwin = scrolledwin;
138         logwin->text = text;
139         logwin->hook_id = hooks_register_hook(LOG_APPEND_TEXT_HOOKLIST, log_window_append, logwin);
140
141         gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(text), logwin->end_mark);
142
143         return logwin;
144 }
145
146 void log_window_init(LogWindow *logwin)
147 {
148         GtkTextBuffer *buffer;
149         GdkColormap *colormap;
150         GdkColor color[5];
151         gboolean success[5];
152         gint i;
153
154         gtkut_convert_int_to_gdk_color(prefs_common.log_msg_color, &color[0]);
155         gtkut_convert_int_to_gdk_color(prefs_common.log_warn_color, &color[1]);
156         gtkut_convert_int_to_gdk_color(prefs_common.log_error_color, &color[2]);
157         gtkut_convert_int_to_gdk_color(prefs_common.log_in_color, &color[3]);
158         gtkut_convert_int_to_gdk_color(prefs_common.log_out_color, &color[4]);
159
160         logwin->msg_color = color[0];
161         logwin->warn_color = color[1];
162         logwin->error_color = color[2];
163         logwin->in_color = color[3];
164         logwin->out_color = color[4];
165
166         colormap = gdk_drawable_get_colormap(logwin->window->window);
167         gdk_colormap_alloc_colors(colormap, color, 5, FALSE, TRUE, success);
168
169         for (i = 0; i < 5; i++) {
170                 if (success[i] == FALSE) {
171                         GtkStyle *style;
172
173                         g_warning("LogWindow: color allocation failed\n");
174                         style = gtk_widget_get_style(logwin->window);
175                         logwin->msg_color = logwin->warn_color =
176                         logwin->error_color = logwin->in_color =
177                         logwin->out_color = style->black;
178                         break;
179                 }
180         }
181
182         buffer = logwin->buffer;
183         gtk_text_buffer_create_tag(buffer, "message",
184                                    "foreground-gdk", &logwin->msg_color,
185                                    NULL);
186         gtk_text_buffer_create_tag(buffer, "warn",
187                                    "foreground-gdk", &logwin->warn_color,
188                                    NULL);
189         logwin->error_tag = gtk_text_buffer_create_tag(buffer, "error",
190                                    "foreground-gdk", &logwin->error_color,
191                                    NULL);
192         gtk_text_buffer_create_tag(buffer, "input",
193                                    "foreground-gdk", &logwin->in_color,
194                                    NULL);
195         gtk_text_buffer_create_tag(buffer, "output",
196                                    "foreground-gdk", &logwin->out_color,
197                                    NULL);
198 }
199
200 void log_window_show(LogWindow *logwin)
201 {
202         GtkTextView *text = GTK_TEXT_VIEW(logwin->text);
203         GtkTextBuffer *buffer = logwin->buffer;
204         GtkTextMark *mark;
205
206         logwin->hidden = FALSE;
207
208         if (logwin->never_shown)
209                 gtk_text_view_set_buffer(GTK_TEXT_VIEW(logwin->text), logwin->buffer);
210
211         logwin->never_shown = FALSE;
212
213         mark = gtk_text_buffer_get_mark(buffer, "end");
214         gtk_text_view_scroll_mark_onscreen(text, mark);
215
216         gtk_widget_show(logwin->window);
217 }
218
219 static void log_window_jump_to_error(LogWindow *logwin)
220 {
221         GtkTextIter iter;
222         gtk_text_buffer_get_end_iter(logwin->buffer, &iter);
223         if (!gtk_text_iter_backward_to_tag_toggle(&iter, logwin->error_tag))
224                 return;
225
226         gtk_text_iter_backward_line(&iter);
227         gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(logwin->text), &iter, 0, TRUE, 0, 0);
228 }
229
230 void log_window_show_error(LogWindow *logwin)
231 {
232         log_window_show(logwin);
233         log_window_jump_to_error(logwin);
234 }
235
236 void log_window_set_clipping(LogWindow *logwin, gboolean clip, guint clip_length)
237 {
238         g_return_if_fail(logwin != NULL);
239
240         logwin->clip = clip;
241         logwin->clip_length = clip_length;
242 }
243
244 static gboolean log_window_append(gpointer source, gpointer data)
245 {
246         LogText *logtext = (LogText *) source;
247         LogWindow *logwindow = (LogWindow *) data;
248         GtkTextView *text;
249         GtkTextBuffer *buffer;
250         GtkTextIter iter;
251         gchar *head = NULL;
252         const gchar *tag;
253
254         g_return_val_if_fail(logtext != NULL, TRUE);
255         g_return_val_if_fail(logtext->text != NULL, TRUE);
256         g_return_val_if_fail(logwindow != NULL, FALSE);
257
258         if (logwindow->clip && !logwindow->clip_length)
259                 return FALSE;
260
261         text = GTK_TEXT_VIEW(logwindow->text);
262         buffer = logwindow->buffer;
263         gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
264
265         switch (logtext->type) {
266         case LOG_MSG:
267                 tag = "message";
268                 head = "* ";
269                 break;
270         case LOG_WARN:
271                 tag = "warn";
272                 head = "** ";
273                 break;
274         case LOG_ERROR:
275                 tag = "error";
276                 head = "*** ";
277                 break;
278         default:
279                 tag = NULL;
280                 break;
281         }
282   
283         if (tag == NULL) {
284                 if (strstr(logtext->text, "] POP3>")
285                 ||  strstr(logtext->text, "] IMAP4>")
286                 ||  strstr(logtext->text, "] SMTP>")
287                 ||  strstr(logtext->text, "] ESMTP>")
288                 ||  strstr(logtext->text, "] NNTP>"))
289                         tag = "output";
290                 if (strstr(logtext->text, "] POP3<")
291                 ||  strstr(logtext->text, "] IMAP4<")
292                 ||  strstr(logtext->text, "] SMTP<")
293                 ||  strstr(logtext->text, "] ESMTP<")
294                 ||  strstr(logtext->text, "] NNTP<"))
295                         tag = "input";
296         }
297
298         if (head)
299                 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, head, -1,
300                                                          tag, NULL);
301
302         if (!g_utf8_validate(logtext->text, -1, NULL)) {
303                 gchar * mybuf = g_malloc(strlen(logtext->text)*2 +1);
304                 conv_localetodisp(mybuf, strlen(logtext->text)*2 +1, logtext->text);
305                 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, mybuf, -1,
306                                                          tag, NULL);
307                 g_free(mybuf);
308         } else {
309                 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, logtext->text, -1,
310                                                          tag, NULL);
311         }
312         gtk_text_buffer_get_start_iter(buffer, &iter);
313
314         if (logwindow->clip)
315                log_window_clip (logwindow, logwindow->clip_length);
316
317         if (!logwindow->hidden) {
318                 GtkAdjustment *vadj = text->vadjustment;
319                 gfloat upper = vadj->upper - vadj->page_size;
320                 if (vadj->value == upper || 
321                     (upper - vadj->value < 16 && vadj->value < 8))
322                         gtk_text_view_scroll_mark_onscreen(text, logwindow->end_mark);
323         }
324
325         return FALSE;
326 }
327
328 static void hide_cb(GtkWidget *widget, LogWindow *logwin)
329 {
330         logwin->hidden = TRUE;
331 }
332
333 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
334                         LogWindow *logwin)
335 {
336         if (event && event->keyval == GDK_Escape)
337                 gtk_widget_hide(logwin->window);
338         else if (event && event->keyval == GDK_Delete) 
339                 log_window_clear(NULL, logwin);
340
341         return FALSE;
342 }
343
344 static void log_window_clip(LogWindow *logwin, guint clip_length)
345 {
346         guint length;
347         guint point;
348         GtkTextBuffer *textbuf = logwin->buffer;
349         GtkTextIter start_iter, end_iter;
350         
351         length = gtk_text_buffer_get_line_count(textbuf);
352         /* debug_print("Log window length: %u\n", length); */
353         
354         if (length > clip_length) {
355                 /* find the end of the first line after the cut off
356                  * point */
357                 point = length - clip_length;
358                 gtk_text_buffer_get_iter_at_line(textbuf, &end_iter, point);
359                 if (!gtk_text_iter_forward_to_line_end(&end_iter))
360                         return;
361                 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
362                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
363         }
364 }
365
366 static void log_window_clear(GtkWidget *widget, LogWindow *logwin)
367 {
368         GtkTextBuffer *textbuf = logwin->buffer;
369         GtkTextIter start_iter, end_iter;
370         
371         gtk_text_buffer_get_start_iter(textbuf, &start_iter);
372         gtk_text_buffer_get_end_iter(textbuf, &end_iter);
373         gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
374 }
375
376 static void log_window_popup_menu_extend(GtkTextView *textview,
377                         GtkMenu *menu, LogWindow *logwin)
378 {
379         GtkWidget *menuitem;
380         
381         g_return_if_fail(menu != NULL);
382         g_return_if_fail(GTK_IS_MENU_SHELL(menu));
383
384         menuitem = gtk_separator_menu_item_new();
385         gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), menuitem);
386         gtk_widget_show(menuitem);
387         
388         menuitem = gtk_menu_item_new_with_mnemonic(_("Clear _Log"));
389         g_signal_connect(G_OBJECT(menuitem), "activate",
390                          G_CALLBACK(log_window_clear), logwin);
391         gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), menuitem);
392         gtk_widget_show(menuitem);
393 }
394