e7c763fa4fab2d3508d2528310141b2927c8cb57
[claws.git] / src / gtk / logwindow.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2018 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 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gdk/gdkkeysyms.h>
27 #include <gtk/gtk.h>
28
29 #include "logwindow.h"
30 #include "utils.h"
31 #include "gtkutils.h"
32 #include "log.h"
33 #include "hooks.h"
34 #include "prefs_common.h"
35
36 static void hide_cb                             (GtkWidget      *widget,
37                                                  LogWindow      *logwin);
38 static gboolean key_pressed                     (GtkWidget      *widget,
39                                                  GdkEventKey    *event,
40                                                  LogWindow      *logwin);
41 static void size_allocate_cb    (GtkWidget *widget,
42                                          GtkAllocation *allocation,
43                                          gpointer data);
44 static gboolean log_window_append               (gpointer        source,
45                                                  gpointer        data);
46 static void log_window_clip                     (LogWindow      *logwin,
47                                                  guint           glip_length);
48 static void log_window_clear                    (GtkWidget      *widget,
49                                                  LogWindow      *logwin);
50 static void log_window_popup_menu_extend        (GtkTextView    *textview,
51                                                  GtkMenu        *menu,
52                                                  LogWindow      *logwin);
53                                          
54 /*!
55  *\brief        Save Gtk object size to prefs dataset
56  */
57 static void size_allocate_cb(GtkWidget *widget,
58                                          GtkAllocation *allocation,
59                                          gpointer data)
60 {
61         gint *prefs_logwin_width = NULL;
62         gint *prefs_logwin_height = NULL;
63         LogInstance instance = GPOINTER_TO_INT(data);
64
65         cm_return_if_fail(allocation != NULL);
66
67         get_log_prefs(instance, &prefs_logwin_width, &prefs_logwin_height);
68         cm_return_if_fail(prefs_logwin_width != NULL);
69         cm_return_if_fail(prefs_logwin_height != NULL);
70
71         *prefs_logwin_width = allocation->width;
72         *prefs_logwin_height = allocation->height;
73 }
74
75 LogWindow *log_window_create(LogInstance instance)
76 {
77         LogWindow *logwin;
78         GtkWidget *window;
79         GtkWidget *scrolledwin;
80         GtkWidget *text;
81         GtkTextBuffer *buffer;
82         GtkTextIter iter;
83         static GdkGeometry geometry;
84         gint *prefs_logwin_width = NULL;
85         gint *prefs_logwin_height = NULL;
86
87         debug_print("Creating log window...\n");
88
89         get_log_prefs(instance, &prefs_logwin_width, &prefs_logwin_height);
90         cm_return_val_if_fail(prefs_logwin_width != NULL, NULL);
91         cm_return_val_if_fail(prefs_logwin_height != NULL, NULL);
92
93         logwin = g_new0(LogWindow, 1);
94
95         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "logwindow");
96         gtk_window_set_title(GTK_WINDOW(window), get_log_title(instance));
97         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
98         g_signal_connect(G_OBJECT(window), "delete_event",
99                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
100         g_signal_connect(G_OBJECT(window), "key_press_event",
101                          G_CALLBACK(key_pressed), logwin);
102         g_signal_connect_after(G_OBJECT(window), "hide",
103                          G_CALLBACK(hide_cb), logwin);
104         gtk_widget_realize(window);
105
106         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
107         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
108                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
109         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
110                                             GTK_SHADOW_IN);
111         gtk_container_add(GTK_CONTAINER(window), scrolledwin);
112         gtk_widget_show(scrolledwin);
113
114         text = gtk_text_view_new();
115         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
116         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
117         logwin->buffer = buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
118
119         g_object_ref(G_OBJECT(logwin->buffer));
120         gtk_text_view_set_buffer(GTK_TEXT_VIEW(text), NULL);
121         logwin->hidden = TRUE;
122         logwin->never_shown = TRUE;
123
124         gtk_text_buffer_get_start_iter(buffer, &iter);
125         logwin->end_mark = gtk_text_buffer_create_mark(buffer, "end", &iter, FALSE);
126
127         g_signal_connect(G_OBJECT(text), "populate-popup",
128                          G_CALLBACK(log_window_popup_menu_extend), logwin);
129         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
130         gtk_widget_show(text);
131
132         g_signal_connect(G_OBJECT(window), "size_allocate",
133                          G_CALLBACK(size_allocate_cb), GINT_TO_POINTER(instance));
134
135         if (!geometry.min_height) {
136                 geometry.min_width = 520;
137                 geometry.min_height = 400;
138         }
139
140         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
141                                       GDK_HINT_MIN_SIZE);
142         gtk_widget_set_size_request(window, *prefs_logwin_width,
143                                     *prefs_logwin_height);
144
145         logwin->window = window;
146         logwin->scrolledwin = scrolledwin;
147         logwin->text = text;
148         logwin->hook_id = hooks_register_hook(get_log_hook(instance), log_window_append, logwin);
149         logwin->has_error_capability = get_log_error_capability(instance);
150
151         return logwin;
152 }
153
154 #define LOG_COLORS 8
155
156 void log_window_init(LogWindow *logwin)
157 {
158         GtkTextBuffer *buffer;
159         GdkColormap *colormap;
160         gboolean success[LOG_COLORS];
161         GdkColor color[LOG_COLORS];
162         gint i;
163
164         gtkut_convert_int_to_gdk_color(prefs_common.color[COL_LOG_MSG], &color[0]);
165         gtkut_convert_int_to_gdk_color(prefs_common.color[COL_LOG_WARN], &color[1]);
166         gtkut_convert_int_to_gdk_color(prefs_common.color[COL_LOG_ERROR], &color[2]);
167         gtkut_convert_int_to_gdk_color(prefs_common.color[COL_LOG_IN], &color[3]);
168         gtkut_convert_int_to_gdk_color(prefs_common.color[COL_LOG_OUT], &color[4]);
169         gtkut_convert_int_to_gdk_color(prefs_common.color[COL_LOG_STATUS_OK], &color[5]);
170         gtkut_convert_int_to_gdk_color(prefs_common.color[COL_LOG_STATUS_NOK], &color[6]);
171         gtkut_convert_int_to_gdk_color(prefs_common.color[COL_LOG_STATUS_SKIP], &color[7]);
172
173         logwin->msg_color = color[0];
174         logwin->warn_color = color[1];
175         logwin->error_color = color[2];
176         logwin->in_color = color[3];
177         logwin->out_color = color[4];
178         logwin->status_ok_color = color[5];
179         logwin->status_nok_color = color[6];
180         logwin->status_skip_color = color[7];
181
182         colormap = gdk_drawable_get_colormap(gtk_widget_get_window(logwin->window));
183         gdk_colormap_alloc_colors(colormap, color, LOG_COLORS, FALSE, TRUE, success);
184
185         for (i = 0; i < LOG_COLORS; i++) {
186                 if (success[i] == FALSE) {
187                         GtkStyle *style;
188
189                         g_warning("LogWindow: color allocation failed");
190                         style = gtk_widget_get_style(logwin->window);
191                         logwin->msg_color = logwin->warn_color =
192                                         logwin->error_color = logwin->in_color =
193                                         logwin->out_color = logwin->status_ok_color =
194                                         logwin->status_nok_color = logwin->status_skip_color =
195                                         style->black;
196                         break;
197                 }
198         }
199
200         buffer = logwin->buffer;
201         gtk_text_buffer_create_tag(buffer, "message",
202                                    "foreground-gdk", &logwin->msg_color,
203                                    NULL);
204         gtk_text_buffer_create_tag(buffer, "warn",
205                                    "foreground-gdk", &logwin->warn_color,
206                                    NULL);
207         logwin->error_tag = gtk_text_buffer_create_tag(buffer, "error",
208                                    "foreground-gdk", &logwin->error_color,
209                                    NULL);
210         gtk_text_buffer_create_tag(buffer, "input",
211                                    "foreground-gdk", &logwin->in_color,
212                                    NULL);
213         gtk_text_buffer_create_tag(buffer, "output",
214                                    "foreground-gdk", &logwin->out_color,
215                                    NULL);
216         gtk_text_buffer_create_tag(buffer, "status_ok",
217                                    "foreground-gdk", &logwin->status_ok_color,
218                                    NULL);
219         gtk_text_buffer_create_tag(buffer, "status_nok",
220                                    "foreground-gdk", &logwin->status_nok_color,
221                                    NULL);
222         gtk_text_buffer_create_tag(buffer, "status_skip",
223                                    "foreground-gdk", &logwin->status_skip_color,
224                                    NULL);
225 }
226
227 #undef LOG_COLORS
228
229 void log_window_show(LogWindow *logwin)
230 {
231         GtkTextView *text = GTK_TEXT_VIEW(logwin->text);
232         GtkTextBuffer *buffer = logwin->buffer;
233         GtkTextMark *mark;
234
235         logwin->hidden = FALSE;
236
237         if (logwin->never_shown)
238                 gtk_text_view_set_buffer(GTK_TEXT_VIEW(logwin->text), logwin->buffer);
239
240         logwin->never_shown = FALSE;
241
242         mark = gtk_text_buffer_get_mark(buffer, "end");
243         gtk_text_view_scroll_mark_onscreen(text, mark);
244
245         gtk_window_deiconify(GTK_WINDOW(logwin->window));
246         gtk_widget_show(logwin->window);
247         gtk_window_present(GTK_WINDOW(logwin->window));
248 }
249
250 static void log_window_jump_to_error(LogWindow *logwin)
251 {
252         GtkTextIter iter;
253         gtk_text_buffer_get_end_iter(logwin->buffer, &iter);
254         if (!gtk_text_iter_backward_to_tag_toggle(&iter, logwin->error_tag))
255                 return;
256
257         gtk_text_iter_backward_line(&iter);
258         gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(logwin->text), &iter, 0, TRUE, 0, 0);
259 }
260
261 void log_window_show_error(LogWindow *logwin)
262 {
263         log_window_show(logwin);
264         log_window_jump_to_error(logwin);
265 }
266
267 void log_window_set_clipping(LogWindow *logwin, gboolean clip, guint clip_length)
268 {
269         cm_return_if_fail(logwin != NULL);
270
271         logwin->clip = clip;
272         logwin->clip_length = clip_length;
273 }
274
275 static gboolean log_window_append(gpointer source, gpointer data)
276 {
277         LogText *logtext = (LogText *) source;
278         LogWindow *logwindow = (LogWindow *) data;
279         GtkTextView *text;
280         GtkTextBuffer *buffer;
281         GtkTextIter iter;
282         gchar *head = NULL;
283         const gchar *tag;
284
285         cm_return_val_if_fail(logtext != NULL, TRUE);
286         cm_return_val_if_fail(logtext->text != NULL, TRUE);
287         cm_return_val_if_fail(logwindow != NULL, FALSE);
288
289         if (logwindow->clip && !logwindow->clip_length)
290                 return FALSE;
291
292         text = GTK_TEXT_VIEW(logwindow->text);
293         buffer = logwindow->buffer;
294         gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
295
296         switch (logtext->type) {
297         case LOG_MSG:
298                 tag = "message";
299                 head = "* ";
300                 break;
301         case LOG_WARN:
302                 tag = "warn";
303                 head = "** ";
304                 break;
305         case LOG_ERROR:
306                 tag = "error";
307                 head = "*** ";
308                 logwindow->has_error = TRUE;
309                 break;
310         case LOG_STATUS_OK:
311                 tag = "status_ok";
312                 head = "> ";
313                 break;
314         case LOG_STATUS_NOK:
315                 tag = "status_nok";
316                 head = "> ";
317                 break;
318         case LOG_STATUS_SKIP:
319                 tag = "status_skip";
320                 head = "> skipped: ";
321                 break;
322         default:
323                 tag = NULL;
324                 break;
325         }
326
327         if (logtext->instance == LOG_PROTOCOL) {
328                 if (tag == NULL) {
329                         if (strstr(logtext->text, "] POP>")
330                         ||  strstr(logtext->text, "] IMAP>")
331                         ||  strstr(logtext->text, "] SMTP>")
332                         ||  strstr(logtext->text, "] ESMTP>")
333                         ||  strstr(logtext->text, "] NNTP>"))
334                                 tag = "output";
335                         if (strstr(logtext->text, "] POP<")
336                         ||  strstr(logtext->text, "] IMAP<")
337                         ||  strstr(logtext->text, "] SMTP<")
338                         ||  strstr(logtext->text, "] ESMTP<")
339                         ||  strstr(logtext->text, "] NNTP<"))
340                                 tag = "input";
341                 }
342         }
343
344         if (head)
345                 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, head, -1,
346                                                          tag, NULL);
347
348         if (!g_utf8_validate(logtext->text, -1, NULL)) {
349                 gchar * mybuf = g_malloc(strlen(logtext->text)*2 +1);
350                 conv_localetodisp(mybuf, strlen(logtext->text)*2 +1, logtext->text);
351                 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, mybuf, -1,
352                                                          tag, NULL);
353                 g_free(mybuf);
354         } else {
355                 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, logtext->text, -1,
356                                                          tag, NULL);
357         }
358         gtk_text_buffer_get_start_iter(buffer, &iter);
359
360         if (logwindow->clip)
361                log_window_clip (logwindow, logwindow->clip_length);
362
363         if (!logwindow->hidden) {
364                 GtkAdjustment *vadj = gtk_text_view_get_vadjustment(text);
365                 gfloat upper = gtk_adjustment_get_upper(vadj) -
366                     gtk_adjustment_get_page_size(vadj);
367                 gfloat value = gtk_adjustment_get_value(vadj);
368                 if (value == upper || 
369                     (upper - value < 16 && value < 8))
370                         gtk_text_view_scroll_mark_onscreen(text, logwindow->end_mark);
371         }
372
373         return FALSE;
374 }
375
376 static void hide_cb(GtkWidget *widget, LogWindow *logwin)
377 {
378         logwin->hidden = TRUE;
379 }
380
381 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
382                         LogWindow *logwin)
383 {
384         if (event && event->keyval == GDK_KEY_Escape)
385                 gtk_widget_hide(logwin->window);
386         else if (event && event->keyval == GDK_KEY_Delete) 
387                 log_window_clear(NULL, logwin);
388
389         return FALSE;
390 }
391
392 static void log_window_clip(LogWindow *logwin, guint clip_length)
393 {
394         guint length;
395         guint point;
396         GtkTextBuffer *textbuf = logwin->buffer;
397         GtkTextIter start_iter, end_iter;
398         
399         length = gtk_text_buffer_get_line_count(textbuf);
400         /* debug_print("Log window length: %u\n", length); */
401         
402         if (length > clip_length) {
403                 /* find the end of the first line after the cut off
404                  * point */
405                 point = length - clip_length;
406                 gtk_text_buffer_get_iter_at_line(textbuf, &end_iter, point);
407                 if (!gtk_text_iter_forward_to_line_end(&end_iter))
408                         return;
409                 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
410                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
411                 if (logwin->has_error) {
412                         gtk_text_buffer_get_start_iter(textbuf, &start_iter);
413                         if (mainwindow_get_mainwindow() && !gtk_text_iter_forward_to_tag_toggle(&start_iter, logwin->error_tag)) {
414                                 mainwindow_clear_error(mainwindow_get_mainwindow());
415                                 logwin->has_error = FALSE;
416                         }
417                 }
418         }
419 }
420
421 static void log_window_clear(GtkWidget *widget, LogWindow *logwin)
422 {
423         GtkTextBuffer *textbuf = logwin->buffer;
424         GtkTextIter start_iter, end_iter;
425         
426         gtk_text_buffer_get_start_iter(textbuf, &start_iter);
427         gtk_text_buffer_get_end_iter(textbuf, &end_iter);
428         gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
429 }
430
431 static void log_window_go_to_last_error(GtkWidget *widget, LogWindow *logwin)
432 {
433         log_window_jump_to_error(logwin);
434 }
435
436 static void log_window_popup_menu_extend(GtkTextView *textview,
437                         GtkMenu *menu, LogWindow *logwin)
438 {
439         GtkWidget *menuitem;
440         
441         cm_return_if_fail(menu != NULL);
442         cm_return_if_fail(GTK_IS_MENU_SHELL(menu));
443
444         menuitem = gtk_separator_menu_item_new();
445         gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), menuitem);
446         gtk_widget_show(menuitem);
447
448         if (logwin->has_error_capability) {
449                 menuitem = gtk_menu_item_new_with_mnemonic(_("_Go to last error"));
450                 g_signal_connect(G_OBJECT(menuitem), "activate",
451                                  G_CALLBACK(log_window_go_to_last_error), logwin);
452                 gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), menuitem);
453                 gtk_widget_show(menuitem);
454         }
455
456         menuitem = gtk_menu_item_new_with_mnemonic(_("Clear _Log"));
457         g_signal_connect(G_OBJECT(menuitem), "activate",
458                          G_CALLBACK(log_window_clear), logwin);
459         gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), menuitem);
460         gtk_widget_show(menuitem);
461 }
462