comparison gtk/gtkdebug.c @ 14191:009db0b357b5

This is a hand-crafted commit to migrate across subversion revisions 16854:16861, due to some vagaries of the way the original renames were done. Witness that monotone can do in one revision what svn had to spread across several.
author Ethan Blanton <elb@pidgin.im>
date Sat, 16 Dec 2006 04:59:55 +0000
parents
children aca9a7b62a23
comparison
equal deleted inserted replaced
14190:366be2ce35a7 14191:009db0b357b5
1 /**
2 * @file gtkdebug.c GTK+ Debug API
3 * @ingroup gtkui
4 *
5 * gaim
6 *
7 * Gaim is the legal property of its developers, whose names are too numerous
8 * to list here. Please refer to the COPYRIGHT file distributed with this
9 * source distribution.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25 #include "internal.h"
26 #include "gtkgaim.h"
27
28 #include "notify.h"
29 #include "prefs.h"
30 #include "request.h"
31 #include "util.h"
32
33 #include "gtkdebug.h"
34 #include "gtkdialogs.h"
35 #include "gtkimhtml.h"
36 #include "gtkutils.h"
37 #include "gaimstock.h"
38
39 #ifdef HAVE_REGEX_H
40 # include <regex.h>
41 #endif /* HAVE_REGEX_H */
42
43 #include <gdk/gdkkeysyms.h>
44
45 typedef struct
46 {
47 GtkWidget *window;
48 GtkWidget *text;
49
50 GtkListStore *store;
51
52 gboolean timestamps;
53 gboolean paused;
54
55 #ifdef HAVE_REGEX_H
56 GtkWidget *filter;
57 GtkWidget *expression;
58
59 gboolean invert;
60 gboolean highlight;
61
62 guint timer;
63
64 regex_t regex;
65 #else
66 GtkWidget *find;
67 #endif /* HAVE_REGEX_H */
68 } DebugWindow;
69
70 static char debug_fg_colors[][8] = {
71 "#000000", /**< All debug levels. */
72 "#666666", /**< Misc. */
73 "#000000", /**< Information. */
74 "#660000", /**< Warnings. */
75 "#FF0000", /**< Errors. */
76 "#FF0000", /**< Fatal errors. */
77 };
78
79 static DebugWindow *debug_win = NULL;
80
81 #ifdef HAVE_REGEX_H
82 static void regex_filter_all(DebugWindow *win);
83 static void regex_show_all(DebugWindow *win);
84 #endif /* HAVE_REGEX_H */
85
86 static gint
87 debug_window_destroy(GtkWidget *w, GdkEvent *event, void *unused)
88 {
89 gaim_prefs_disconnect_by_handle(gaim_gtk_debug_get_handle());
90
91 #ifdef HAVE_REGEX_H
92 if(debug_win->timer != 0) {
93 const gchar *text;
94
95 g_source_remove(debug_win->timer);
96
97 text = gtk_entry_get_text(GTK_ENTRY(debug_win->expression));
98 gaim_prefs_set_string("/gaim/gtk/debug/regex", text);
99 }
100
101 regfree(&debug_win->regex);
102 #endif
103
104 /* If the "Save Log" dialog is open then close it */
105 gaim_request_close_with_handle(debug_win);
106
107 g_free(debug_win);
108 debug_win = NULL;
109
110 gaim_prefs_set_bool("/gaim/gtk/debug/enabled", FALSE);
111
112 return FALSE;
113 }
114
115 static gboolean
116 configure_cb(GtkWidget *w, GdkEventConfigure *event, DebugWindow *win)
117 {
118 if (GTK_WIDGET_VISIBLE(w)) {
119 gaim_prefs_set_int("/gaim/gtk/debug/width", event->width);
120 gaim_prefs_set_int("/gaim/gtk/debug/height", event->height);
121 }
122
123 return FALSE;
124 }
125
126 #ifndef HAVE_REGEX_H
127 struct _find {
128 DebugWindow *window;
129 GtkWidget *entry;
130 };
131
132 static void
133 do_find_cb(GtkWidget *widget, gint response, struct _find *f)
134 {
135 switch (response) {
136 case GTK_RESPONSE_OK:
137 gtk_imhtml_search_find(GTK_IMHTML(f->window->text),
138 gtk_entry_get_text(GTK_ENTRY(f->entry)));
139 break;
140
141 case GTK_RESPONSE_DELETE_EVENT:
142 case GTK_RESPONSE_CLOSE:
143 gtk_imhtml_search_clear(GTK_IMHTML(f->window->text));
144 gtk_widget_destroy(f->window->find);
145 f->window->find = NULL;
146 g_free(f);
147 break;
148 }
149 }
150
151 static void
152 find_cb(GtkWidget *w, DebugWindow *win)
153 {
154 GtkWidget *hbox, *img, *label;
155 struct _find *f;
156
157 if(win->find)
158 {
159 gtk_window_present(GTK_WINDOW(win->find));
160 return;
161 }
162
163 f = g_malloc(sizeof(struct _find));
164 f->window = win;
165 win->find = gtk_dialog_new_with_buttons(_("Find"),
166 GTK_WINDOW(win->window), GTK_DIALOG_DESTROY_WITH_PARENT,
167 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
168 GTK_STOCK_FIND, GTK_RESPONSE_OK, NULL);
169 gtk_dialog_set_default_response(GTK_DIALOG(win->find),
170 GTK_RESPONSE_OK);
171 g_signal_connect(G_OBJECT(win->find), "response",
172 G_CALLBACK(do_find_cb), f);
173
174 gtk_container_set_border_width(GTK_CONTAINER(win->find), GAIM_HIG_BOX_SPACE);
175 gtk_window_set_resizable(GTK_WINDOW(win->find), FALSE);
176 gtk_dialog_set_has_separator(GTK_DIALOG(win->find), FALSE);
177 gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(win->find)->vbox), GAIM_HIG_BORDER);
178 gtk_container_set_border_width(
179 GTK_CONTAINER(GTK_DIALOG(win->find)->vbox), GAIM_HIG_BOX_SPACE);
180
181 hbox = gtk_hbox_new(FALSE, GAIM_HIG_BORDER);
182 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(win->find)->vbox),
183 hbox);
184 img = gtk_image_new_from_stock(GAIM_STOCK_DIALOG_QUESTION,
185 GTK_ICON_SIZE_DIALOG);
186 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 0);
187
188 gtk_misc_set_alignment(GTK_MISC(img), 0, 0);
189 gtk_dialog_set_response_sensitive(GTK_DIALOG(win->find),
190 GTK_RESPONSE_OK, FALSE);
191
192 label = gtk_label_new(NULL);
193 gtk_label_set_markup_with_mnemonic(GTK_LABEL(label), _("_Search for:"));
194 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
195
196 f->entry = gtk_entry_new();
197 gtk_entry_set_activates_default(GTK_ENTRY(f->entry), TRUE);
198 gtk_label_set_mnemonic_widget(GTK_LABEL(label), GTK_WIDGET(f->entry));
199 g_signal_connect(G_OBJECT(f->entry), "changed",
200 G_CALLBACK(gaim_gtk_set_sensitive_if_input),
201 win->find);
202 gtk_box_pack_start(GTK_BOX(hbox), f->entry, FALSE, FALSE, 0);
203
204 gtk_widget_show_all(win->find);
205 gtk_widget_grab_focus(f->entry);
206 }
207 #endif /* HAVE_REGEX_H */
208
209 static void
210 save_writefile_cb(void *user_data, const char *filename)
211 {
212 DebugWindow *win = (DebugWindow *)user_data;
213 FILE *fp;
214 char *tmp;
215
216 if ((fp = g_fopen(filename, "w+")) == NULL) {
217 gaim_notify_error(win, NULL, _("Unable to open file."), NULL);
218 return;
219 }
220
221 tmp = gtk_imhtml_get_text(GTK_IMHTML(win->text), NULL, NULL);
222 fprintf(fp, "Gaim Debug Log : %s\n", gaim_date_format_full(NULL));
223 fprintf(fp, "%s", tmp);
224 g_free(tmp);
225
226 fclose(fp);
227 }
228
229 static void
230 save_cb(GtkWidget *w, DebugWindow *win)
231 {
232 gaim_request_file(win, _("Save Debug Log"), "gaim-debug.log", TRUE,
233 G_CALLBACK(save_writefile_cb), NULL, win);
234 }
235
236 static void
237 clear_cb(GtkWidget *w, DebugWindow *win)
238 {
239 gtk_imhtml_clear(GTK_IMHTML(win->text));
240
241 #ifdef HAVE_REGEX_H
242 gtk_list_store_clear(win->store);
243 #endif /* HAVE_REGEX_H */
244 }
245
246 static void
247 pause_cb(GtkWidget *w, DebugWindow *win)
248 {
249 win->paused = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w));
250
251 #ifdef HAVE_REGEX_H
252 if(!win->paused) {
253 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(win->filter)))
254 regex_filter_all(win);
255 else
256 regex_show_all(win);
257 }
258 #endif /* HAVE_REGEX_H */
259 }
260
261 static void
262 timestamps_cb(GtkWidget *w, DebugWindow *win)
263 {
264 win->timestamps = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w));
265
266 gaim_prefs_set_bool("/core/debug/timestamps", win->timestamps);
267 }
268
269 static void
270 timestamps_pref_cb(const char *name, GaimPrefType type,
271 gconstpointer value, gpointer data)
272 {
273 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(data), GPOINTER_TO_INT(value));
274 }
275
276 /******************************************************************************
277 * regex stuff
278 *****************************************************************************/
279 #ifdef HAVE_REGEX_H
280 static void
281 regex_clear_color(GtkWidget *w) {
282 gtk_widget_modify_base(w, GTK_STATE_NORMAL, NULL);
283 }
284
285 static void
286 regex_change_color(GtkWidget *w, guint16 r, guint16 g, guint16 b) {
287 GdkColor color;
288
289 color.red = r;
290 color.green = g;
291 color.blue = b;
292
293 gtk_widget_modify_base(w, GTK_STATE_NORMAL, &color);
294 }
295
296 static void
297 regex_highlight_clear(DebugWindow *win) {
298 GtkIMHtml *imhtml = GTK_IMHTML(win->text);
299 GtkTextIter s, e;
300
301 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &s);
302 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &e);
303 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "regex", &s, &e);
304 }
305
306 static void
307 regex_match(DebugWindow *win, const gchar *text) {
308 GtkIMHtml *imhtml = GTK_IMHTML(win->text);
309 regmatch_t matches[4]; /* adjust if necessary */
310 size_t n_matches = sizeof(matches) / sizeof(matches[0]);
311 gchar *plaintext;
312 gint inverted;
313
314 if(!text)
315 return;
316
317 inverted = (win->invert) ? REG_NOMATCH : 0;
318
319 /* I don't like having to do this, but we need it for highlighting. Plus
320 * it makes the ^ and $ operators work :)
321 */
322 plaintext = gaim_markup_strip_html(text);
323
324 /* we do a first pass to see if it matches at all. If it does we append
325 * it, and work out the offsets to highlight.
326 */
327 if(regexec(&win->regex, plaintext, n_matches, matches, 0) == inverted) {
328 GtkTextIter ins;
329 gchar *p = plaintext;
330 gint i, offset = 0;
331
332 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &ins,
333 gtk_text_buffer_get_insert(imhtml->text_buffer));
334 i = gtk_text_iter_get_offset(&ins);
335
336 gtk_imhtml_append_text(imhtml, text, 0);
337
338 /* If we're not highlighting or the expression is inverted, we're
339 * done and move on.
340 */
341 if(!win->highlight || inverted == REG_NOMATCH) {
342 g_free(plaintext);
343 return;
344 }
345
346 /* we use a do-while to highlight the first match, and then continue
347 * if necessary...
348 */
349 do {
350 size_t m;
351
352 for(m = 0; m < n_matches; m++) {
353 GtkTextIter ms, me;
354
355 if(matches[m].rm_eo == -1)
356 break;
357
358 i += offset;
359
360 gtk_text_buffer_get_iter_at_offset(imhtml->text_buffer, &ms,
361 i + matches[m].rm_so);
362 gtk_text_buffer_get_iter_at_offset(imhtml->text_buffer, &me,
363 i + matches[m].rm_eo);
364 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "regex",
365 &ms, &me);
366 offset = matches[m].rm_eo;
367 }
368
369 p += offset;
370 } while(regexec(&win->regex, p, n_matches, matches, REG_NOTBOL) == inverted);
371 }
372
373 g_free(plaintext);
374 }
375
376 static gboolean
377 regex_filter_all_cb(GtkTreeModel *m, GtkTreePath *p, GtkTreeIter *iter,
378 gpointer data)
379 {
380 DebugWindow *win = (DebugWindow *)data;
381 gchar *text;
382
383 gtk_tree_model_get(m, iter, 0, &text, -1);
384
385 regex_match(win, text);
386
387 g_free(text);
388
389 return FALSE;
390 }
391
392 static void
393 regex_filter_all(DebugWindow *win) {
394 gtk_imhtml_clear(GTK_IMHTML(win->text));
395
396 if(win->highlight)
397 regex_highlight_clear(win);
398
399 gtk_tree_model_foreach(GTK_TREE_MODEL(win->store), regex_filter_all_cb,
400 win);
401 }
402
403 static gboolean
404 regex_show_all_cb(GtkTreeModel *m, GtkTreePath *p, GtkTreeIter *iter,
405 gpointer data)
406 {
407 DebugWindow *win = (DebugWindow *)data;
408 gchar *text;
409
410 gtk_tree_model_get(m, iter, 0, &text, -1);
411 gtk_imhtml_append_text(GTK_IMHTML(win->text), text, 0);
412 g_free(text);
413
414 return FALSE;
415 }
416
417 static void
418 regex_show_all(DebugWindow *win) {
419 gtk_imhtml_clear(GTK_IMHTML(win->text));
420
421 if(win->highlight)
422 regex_highlight_clear(win);
423
424 gtk_tree_model_foreach(GTK_TREE_MODEL(win->store), regex_show_all_cb,
425 win);
426 }
427
428 static void
429 regex_compile(DebugWindow *win) {
430 const gchar *text;
431
432 text = gtk_entry_get_text(GTK_ENTRY(win->expression));
433
434 if(text == NULL || *text == '\0') {
435 regex_clear_color(win->expression);
436 gtk_widget_set_sensitive(win->filter, FALSE);
437 return;
438 }
439
440 regfree(&win->regex);
441
442 if(regcomp(&win->regex, text, REG_EXTENDED | REG_ICASE) != 0) {
443 /* failed to compile */
444 regex_change_color(win->expression, 0xFFFF, 0xAFFF, 0xAFFF);
445 gtk_widget_set_sensitive(win->filter, FALSE);
446 } else {
447 /* compiled successfully */
448 regex_change_color(win->expression, 0xAFFF, 0xFFFF, 0xAFFF);
449 gtk_widget_set_sensitive(win->filter, TRUE);
450 }
451
452 /* we check if the filter is on in case it was only of the options that
453 * got changed, and not the expression.
454 */
455 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(win->filter)))
456 regex_filter_all(win);
457 }
458
459 static void
460 regex_pref_filter_cb(const gchar *name, GaimPrefType type,
461 gconstpointer val, gpointer data)
462 {
463 DebugWindow *win = (DebugWindow *)data;
464 gboolean active = GPOINTER_TO_INT(val), current;
465
466 if(!win || !win->window)
467 return;
468
469 current = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(win->filter));
470 if(active != current)
471 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(win->filter), active);
472 }
473
474 static void
475 regex_pref_expression_cb(const gchar *name, GaimPrefType type,
476 gconstpointer val, gpointer data)
477 {
478 DebugWindow *win = (DebugWindow *)data;
479 const gchar *exp = (const gchar *)val;
480
481 gtk_entry_set_text(GTK_ENTRY(win->expression), exp);
482 }
483
484 static void
485 regex_pref_invert_cb(const gchar *name, GaimPrefType type,
486 gconstpointer val, gpointer data)
487 {
488 DebugWindow *win = (DebugWindow *)data;
489 gboolean active = GPOINTER_TO_INT(val);
490
491 win->invert = active;
492
493 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(win->filter)))
494 regex_filter_all(win);
495 }
496
497 static void
498 regex_pref_highlight_cb(const gchar *name, GaimPrefType type,
499 gconstpointer val, gpointer data)
500 {
501 DebugWindow *win = (DebugWindow *)data;
502 gboolean active = GPOINTER_TO_INT(val);
503
504 win->highlight = active;
505
506 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(win->filter)))
507 regex_filter_all(win);
508 }
509
510 static void
511 regex_row_changed_cb(GtkTreeModel *model, GtkTreePath *path,
512 GtkTreeIter *iter, DebugWindow *win)
513 {
514 gchar *text;
515
516 if(!win || !win->window)
517 return;
518
519 /* If the debug window is paused, we just return since it's in the store.
520 * We don't call regex_match because it doesn't make sense to check the
521 * string if it's paused. When we unpause we clear the imhtml and
522 * reiterate over the store to handle matches that were outputted when
523 * we were paused.
524 */
525 if(win->paused)
526 return;
527
528 gtk_tree_model_get(model, iter, 0, &text, -1);
529
530 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(win->filter))) {
531 regex_match(win, text);
532 } else {
533 gtk_imhtml_append_text(GTK_IMHTML(win->text), text, 0);
534 }
535
536 g_free(text);
537 }
538
539 static gboolean
540 regex_timer_cb(DebugWindow *win) {
541 const gchar *text;
542
543 text = gtk_entry_get_text(GTK_ENTRY(win->expression));
544 gaim_prefs_set_string("/gaim/gtk/debug/regex", text);
545
546 win->timer = 0;
547
548 return FALSE;
549 }
550
551 static void
552 regex_changed_cb(GtkWidget *w, DebugWindow *win) {
553 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(win->filter))) {
554 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(win->filter),
555 FALSE);
556 }
557
558 if(win->timer == 0)
559 win->timer = gaim_timeout_add(5000, (GSourceFunc)regex_timer_cb, win);
560
561 regex_compile(win);
562 }
563
564 static void
565 regex_key_release_cb(GtkWidget *w, GdkEventKey *e, DebugWindow *win) {
566 if(e->keyval == GDK_Return &&
567 GTK_WIDGET_IS_SENSITIVE(win->filter) &&
568 !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(win->filter)))
569 {
570 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(win->filter), TRUE);
571 }
572 }
573
574 static void
575 regex_menu_cb(GtkWidget *item, const gchar *pref) {
576 gboolean active;
577
578 active = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(item));
579
580 gaim_prefs_set_bool(pref, active);
581 }
582
583 static void
584 regex_popup_cb(GtkEntry *entry, GtkWidget *menu, DebugWindow *win) {
585 gaim_separator(menu);
586 gaim_new_check_item(menu, _("Invert"),
587 G_CALLBACK(regex_menu_cb),
588 "/gaim/gtk/debug/invert", win->invert);
589 gaim_new_check_item(menu, _("Highlight matches"),
590 G_CALLBACK(regex_menu_cb),
591 "/gaim/gtk/debug/highlight", win->highlight);
592 }
593
594 static void
595 regex_filter_toggled_cb(GtkToggleButton *button, DebugWindow *win) {
596 gboolean active;
597
598 active = gtk_toggle_button_get_active(button);
599
600 gaim_prefs_set_bool("/gaim/gtk/debug/filter", active);
601
602 if(!GTK_IS_IMHTML(win->text))
603 return;
604
605 if(active)
606 regex_filter_all(win);
607 else
608 regex_show_all(win);
609 }
610
611 #endif /* HAVE_REGEX_H */
612
613 static DebugWindow *
614 debug_window_new(void)
615 {
616 DebugWindow *win;
617 GtkWidget *vbox;
618 GtkWidget *toolbar;
619 GtkWidget *frame;
620 GtkWidget *button;
621 GtkWidget *image;
622 gint width, height;
623 void *handle;
624
625 win = g_new0(DebugWindow, 1);
626
627 width = gaim_prefs_get_int("/gaim/gtk/debug/width");
628 height = gaim_prefs_get_int("/gaim/gtk/debug/height");
629
630 GAIM_DIALOG(win->window);
631 gaim_debug_info("gtkdebug", "Setting dimensions to %d, %d\n",
632 width, height);
633
634 gtk_window_set_default_size(GTK_WINDOW(win->window), width, height);
635 gtk_window_set_role(GTK_WINDOW(win->window), "debug");
636 gtk_window_set_title(GTK_WINDOW(win->window), _("Debug Window"));
637
638 g_signal_connect(G_OBJECT(win->window), "delete_event",
639 G_CALLBACK(debug_window_destroy), NULL);
640 g_signal_connect(G_OBJECT(win->window), "configure_event",
641 G_CALLBACK(configure_cb), win);
642
643 handle = gaim_gtk_debug_get_handle();
644
645 #ifdef HAVE_REGEX_H
646 /* the list store for all the messages */
647 win->store = gtk_list_store_new(1, G_TYPE_STRING);
648
649 /* row-changed gets called when we do gtk_list_store_set, and row-inserted
650 * gets called with gtk_list_store_append, which is a
651 * completely empty row. So we just ignore row-inserted, and deal with row
652 * changed. -Gary
653 */
654 g_signal_connect(G_OBJECT(win->store), "row-changed",
655 G_CALLBACK(regex_row_changed_cb), win);
656
657 #endif /* HAVE_REGEX_H */
658
659 /* Setup the vbox */
660 vbox = gtk_vbox_new(FALSE, 0);
661 gtk_container_add(GTK_CONTAINER(win->window), vbox);
662
663 if (gaim_prefs_get_bool("/gaim/gtk/debug/toolbar")) {
664 /* Setup our top button bar thingie. */
665 toolbar = gtk_toolbar_new();
666 gtk_toolbar_set_tooltips(GTK_TOOLBAR(toolbar), TRUE);
667
668 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar),
669 GTK_TOOLBAR_BOTH_HORIZ);
670 gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar),
671 GTK_ICON_SIZE_SMALL_TOOLBAR);
672
673 gtk_box_pack_start(GTK_BOX(vbox), toolbar, FALSE, FALSE, 0);
674
675 #ifndef HAVE_REGEX_H
676 /* Find button */
677 gtk_toolbar_insert_stock(GTK_TOOLBAR(toolbar), GTK_STOCK_FIND,
678 _("Find"), NULL, G_CALLBACK(find_cb),
679 win, -1);
680 #endif /* HAVE_REGEX_H */
681
682 /* Save */
683 gtk_toolbar_insert_stock(GTK_TOOLBAR(toolbar), GTK_STOCK_SAVE,
684 _("Save"), NULL, G_CALLBACK(save_cb),
685 win, -1);
686
687 /* Clear button */
688 gtk_toolbar_insert_stock(GTK_TOOLBAR(toolbar), GTK_STOCK_CLEAR,
689 _("Clear"), NULL, G_CALLBACK(clear_cb),
690 win, -1);
691
692 gtk_toolbar_insert_space(GTK_TOOLBAR(toolbar), -1);
693
694 /* Pause */
695 image = gtk_image_new_from_stock(GAIM_STOCK_PAUSE, GTK_ICON_SIZE_MENU);
696 gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
697 GTK_TOOLBAR_CHILD_TOGGLEBUTTON,
698 NULL, _("Pause"), _("Pause"),
699 NULL, image,
700 G_CALLBACK(pause_cb), win);
701
702 /* Timestamps */
703 button = gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
704 GTK_TOOLBAR_CHILD_TOGGLEBUTTON,
705 NULL, _("Timestamps"),
706 _("Timestamps"), NULL, NULL,
707 G_CALLBACK(timestamps_cb),
708 win);
709
710 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),
711 gaim_prefs_get_bool("/core/debug/timestamps"));
712
713 gaim_prefs_connect_callback(handle, "/core/debug/timestamps",
714 timestamps_pref_cb, button);
715
716 #ifdef HAVE_REGEX_H
717 /* regex stuff */
718 gtk_toolbar_insert_space(GTK_TOOLBAR(toolbar), -1);
719
720 /* regex toggle button */
721 win->filter =
722 gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
723 GTK_TOOLBAR_CHILD_TOGGLEBUTTON,
724 NULL, _("Filter"), _("Filter"),
725 NULL, NULL,
726 G_CALLBACK(regex_filter_toggled_cb),
727 win);
728 /* we purposely disable the toggle button here in case
729 * /gaim/gtk/debug/expression has an empty string. If it does not have
730 * an empty string, the change signal will get called and make the
731 * toggle button sensitive.
732 */
733 gtk_widget_set_sensitive(win->filter, FALSE);
734 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(win->filter),
735 gaim_prefs_get_bool("/gaim/gtk/debug/filter"));
736 gaim_prefs_connect_callback(handle, "/gaim/gtk/debug/filter",
737 regex_pref_filter_cb, win);
738
739 /* regex entry */
740 win->expression = gtk_entry_new();
741 gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
742 GTK_TOOLBAR_CHILD_WIDGET, win->expression,
743 NULL, _("Right click for more options."),
744 NULL, NULL, NULL, NULL);
745 /* this needs to be before the text is set from the pref if we want it
746 * to colorize a stored expression.
747 */
748 g_signal_connect(G_OBJECT(win->expression), "changed",
749 G_CALLBACK(regex_changed_cb), win);
750 gtk_entry_set_text(GTK_ENTRY(win->expression),
751 gaim_prefs_get_string("/gaim/gtk/debug/regex"));
752 g_signal_connect(G_OBJECT(win->expression), "populate-popup",
753 G_CALLBACK(regex_popup_cb), win);
754 g_signal_connect(G_OBJECT(win->expression), "key-release-event",
755 G_CALLBACK(regex_key_release_cb), win);
756 gaim_prefs_connect_callback(handle, "/gaim/gtk/debug/regex",
757 regex_pref_expression_cb, win);
758
759 /* connect the rest of our pref callbacks */
760 win->invert = gaim_prefs_get_bool("/gaim/gtk/debug/invert");
761 gaim_prefs_connect_callback(handle, "/gaim/gtk/debug/invert",
762 regex_pref_invert_cb, win);
763
764 win->highlight = gaim_prefs_get_bool("/gaim/gtk/debug/highlight");
765 gaim_prefs_connect_callback(handle, "/gaim/gtk/debug/highlight",
766 regex_pref_highlight_cb, win);
767
768 #endif /* HAVE_REGEX_H */
769 }
770
771 /* Add the gtkimhtml */
772 frame = gaim_gtk_create_imhtml(FALSE, &win->text, NULL, NULL);
773 gtk_imhtml_set_format_functions(GTK_IMHTML(win->text),
774 GTK_IMHTML_ALL ^ GTK_IMHTML_SMILEY ^ GTK_IMHTML_IMAGE);
775 gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0);
776 gtk_widget_show(frame);
777
778 #ifdef HAVE_REGEX_H
779 /* add the tag for regex highlighting */
780 gtk_text_buffer_create_tag(GTK_IMHTML(win->text)->text_buffer, "regex",
781 "background", "#FFAFAF",
782 "weight", "bold",
783 NULL);
784 #endif /* HAVE_REGEX_H */
785
786 gtk_widget_show_all(win->window);
787
788 return win;
789 }
790
791 static void
792 debug_enabled_cb(const char *name, GaimPrefType type,
793 gconstpointer value, gpointer data)
794 {
795 if (value)
796 gaim_gtk_debug_window_show();
797 else
798 gaim_gtk_debug_window_hide();
799 }
800
801 static void
802 gaim_glib_log_handler(const gchar *domain, GLogLevelFlags flags,
803 const gchar *msg, gpointer user_data)
804 {
805 GaimDebugLevel level;
806 char *new_msg = NULL;
807 char *new_domain = NULL;
808
809 if ((flags & G_LOG_LEVEL_ERROR) == G_LOG_LEVEL_ERROR)
810 level = GAIM_DEBUG_ERROR;
811 else if ((flags & G_LOG_LEVEL_CRITICAL) == G_LOG_LEVEL_CRITICAL)
812 level = GAIM_DEBUG_FATAL;
813 else if ((flags & G_LOG_LEVEL_WARNING) == G_LOG_LEVEL_WARNING)
814 level = GAIM_DEBUG_WARNING;
815 else if ((flags & G_LOG_LEVEL_MESSAGE) == G_LOG_LEVEL_MESSAGE)
816 level = GAIM_DEBUG_INFO;
817 else if ((flags & G_LOG_LEVEL_INFO) == G_LOG_LEVEL_INFO)
818 level = GAIM_DEBUG_INFO;
819 else if ((flags & G_LOG_LEVEL_DEBUG) == G_LOG_LEVEL_DEBUG)
820 level = GAIM_DEBUG_MISC;
821 else
822 {
823 gaim_debug_warning("gtkdebug",
824 "Unknown glib logging level in %d\n", flags);
825
826 level = GAIM_DEBUG_MISC; /* This will never happen. */
827 }
828
829 if (msg != NULL)
830 new_msg = gaim_utf8_try_convert(msg);
831
832 if (domain != NULL)
833 new_domain = gaim_utf8_try_convert(domain);
834
835 if (new_msg != NULL)
836 {
837 gaim_debug(level, (new_domain != NULL ? new_domain : "g_log"),
838 "%s\n", new_msg);
839
840 g_free(new_msg);
841 }
842
843 g_free(new_domain);
844 }
845
846 #ifdef _WIN32
847 static void
848 gaim_glib_dummy_print_handler(const gchar *string)
849 {
850 }
851 #endif
852
853 void
854 gaim_gtk_debug_init(void)
855 {
856 /* Debug window preferences. */
857 /*
858 * NOTE: This must be set before prefs are loaded, and the callbacks
859 * set after they are loaded, since prefs sets the enabled
860 * preference here and that loads the window, which calls the
861 * configure event, which overrides the width and height! :P
862 */
863
864 gaim_prefs_add_none("/gaim/gtk/debug");
865
866 /* Controls printing to the debug window */
867 gaim_prefs_add_bool("/gaim/gtk/debug/enabled", FALSE);
868
869 gaim_prefs_add_bool("/gaim/gtk/debug/toolbar", TRUE);
870 gaim_prefs_add_int("/gaim/gtk/debug/width", 450);
871 gaim_prefs_add_int("/gaim/gtk/debug/height", 250);
872
873 #ifdef HAVE_REGEX_H
874 gaim_prefs_add_string("/gaim/gtk/debug/regex", "");
875 gaim_prefs_add_bool("/gaim/gtk/debug/filter", FALSE);
876 gaim_prefs_add_bool("/gaim/gtk/debug/invert", FALSE);
877 gaim_prefs_add_bool("/gaim/gtk/debug/case_insensitive", FALSE);
878 gaim_prefs_add_bool("/gaim/gtk/debug/highlight", FALSE);
879 #endif /* HAVE_REGEX_H */
880
881 gaim_prefs_connect_callback(NULL, "/gaim/gtk/debug/enabled",
882 debug_enabled_cb, NULL);
883
884 #define REGISTER_G_LOG_HANDLER(name) \
885 g_log_set_handler((name), G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL \
886 | G_LOG_FLAG_RECURSION, \
887 gaim_glib_log_handler, NULL)
888
889 /* Register the glib/gtk log handlers. */
890 REGISTER_G_LOG_HANDLER(NULL);
891 REGISTER_G_LOG_HANDLER("Gdk");
892 REGISTER_G_LOG_HANDLER("Gtk");
893 REGISTER_G_LOG_HANDLER("GdkPixbuf");
894 REGISTER_G_LOG_HANDLER("GLib");
895 REGISTER_G_LOG_HANDLER("GModule");
896 REGISTER_G_LOG_HANDLER("GLib-GObject");
897 REGISTER_G_LOG_HANDLER("GThread");
898
899 #ifdef _WIN32
900 if (!gaim_debug_is_enabled())
901 g_set_print_handler(gaim_glib_dummy_print_handler);
902 #endif
903 }
904
905 void
906 gaim_gtk_debug_uninit(void)
907 {
908 gaim_debug_set_ui_ops(NULL);
909 }
910
911 void
912 gaim_gtk_debug_window_show(void)
913 {
914 if (debug_win == NULL)
915 debug_win = debug_window_new();
916
917 gtk_widget_show(debug_win->window);
918
919 gaim_prefs_set_bool("/gaim/gtk/debug/enabled", TRUE);
920 }
921
922 void
923 gaim_gtk_debug_window_hide(void)
924 {
925 if (debug_win != NULL) {
926 gtk_widget_destroy(debug_win->window);
927 debug_window_destroy(NULL, NULL, NULL);
928 }
929 }
930
931 static void
932 gaim_gtk_debug_print(GaimDebugLevel level, const char *category,
933 const char *arg_s)
934 {
935 #ifdef HAVE_REGEX_H
936 GtkTreeIter iter;
937 #endif /* HAVE_REGEX_H */
938 gboolean timestamps;
939 gchar *ts_s;
940 gchar *esc_s, *cat_s, *tmp, *s;
941
942 if (!gaim_prefs_get_bool("/gaim/gtk/debug/enabled") ||
943 (debug_win == NULL))
944 {
945 return;
946 }
947
948 timestamps = gaim_prefs_get_bool("/core/debug/timestamps");
949
950 /*
951 * For some reason we only print the timestamp if category is
952 * not NULL. Why the hell do we do that? --Mark
953 */
954 if ((category != NULL) && (timestamps)) {
955 const char *mdate;
956
957 time_t mtime = time(NULL);
958 mdate = gaim_utf8_strftime("%H:%M:%S", localtime(&mtime));
959 ts_s = g_strdup_printf("(%s) ", mdate);
960 } else {
961 ts_s = g_strdup("");
962 }
963
964 if (category == NULL)
965 cat_s = g_strdup("");
966 else
967 cat_s = g_strdup_printf("<b>%s:</b> ", category);
968
969 esc_s = g_markup_escape_text(arg_s, -1);
970
971 s = g_strdup_printf("<font color=\"%s\">%s%s%s</font>",
972 debug_fg_colors[level], ts_s, cat_s, esc_s);
973
974 g_free(ts_s);
975 g_free(cat_s);
976 g_free(esc_s);
977
978 tmp = gaim_utf8_try_convert(s);
979 g_free(s);
980 s = tmp;
981
982 if (level == GAIM_DEBUG_FATAL) {
983 tmp = g_strdup_printf("<b>%s</b>", s);
984 g_free(s);
985 s = tmp;
986 }
987
988 #ifdef HAVE_REGEX_H
989 /* add the text to the list store */
990 gtk_list_store_append(debug_win->store, &iter);
991 gtk_list_store_set(debug_win->store, &iter, 0, s, -1);
992 #else /* HAVE_REGEX_H */
993 if(!debug_win->paused)
994 gtk_imhtml_append_text(GTK_IMHTML(debug_win->text), s, 0);
995 #endif /* !HAVE_REGEX_H */
996
997 g_free(s);
998 }
999
1000 static GaimDebugUiOps ops =
1001 {
1002 gaim_gtk_debug_print,
1003 };
1004
1005 GaimDebugUiOps *
1006 gaim_gtk_debug_get_ui_ops(void)
1007 {
1008 return &ops;
1009 }
1010
1011 void *
1012 gaim_gtk_debug_get_handle() {
1013 static int handle;
1014
1015 return &handle;
1016 }