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