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