Mercurial > pidgin.yaz
annotate finch/gntdebug.c @ 16105:1983ecd15174
Remember the collapsed state of groups in the blist
author | Richard Nelson <wabz@pidgin.im> |
---|---|
date | Sat, 14 Apr 2007 12:39:33 +0000 |
parents | 6dc5dc83a61b |
children | ab3f93232a2d |
rev | line source |
---|---|
15818 | 1 /** |
2 * @file gntdebug.c GNT Debug API | |
3 * @ingroup gntui | |
4 * | |
15871
66dff3dfdea6
Re-sed the copyright notices so they don't all talk about Purple.
Richard Laager <rlaager@wiktel.com>
parents:
15823
diff
changeset
|
5 * finch |
15818 | 6 * |
15871
66dff3dfdea6
Re-sed the copyright notices so they don't all talk about Purple.
Richard Laager <rlaager@wiktel.com>
parents:
15823
diff
changeset
|
7 * Finch is the legal property of its developers, whose names are too numerous |
15818 | 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 <gnt.h> | |
26 #include <gntbox.h> | |
27 #include <gnttextview.h> | |
28 #include <gntbutton.h> | |
29 #include <gntcheckbox.h> | |
30 #include <gntline.h> | |
31 | |
32 #include "gntdebug.h" | |
15823 | 33 #include "finch.h" |
15818 | 34 #include "util.h" |
35 | |
36 #include <stdio.h> | |
37 #include <string.h> | |
38 | |
15823 | 39 #define PREF_ROOT "/purple/gnt/debug" |
15818 | 40 |
41 static struct | |
42 { | |
43 GntWidget *window; | |
44 GntWidget *tview; | |
45 gboolean paused; | |
46 gboolean timestamps; | |
47 } debug; | |
48 | |
49 static gboolean | |
50 debug_window_kpress_cb(GntWidget *wid, const char *key, GntTextView *view) | |
51 { | |
52 if (key[0] == 27) | |
53 { | |
54 if (strcmp(key, GNT_KEY_DOWN) == 0) | |
55 gnt_text_view_scroll(view, 1); | |
56 else if (strcmp(key, GNT_KEY_UP) == 0) | |
57 gnt_text_view_scroll(view, -1); | |
58 else if (strcmp(key, GNT_KEY_PGDOWN) == 0) | |
59 gnt_text_view_scroll(view, wid->priv.height - 2); | |
60 else if (strcmp(key, GNT_KEY_PGUP) == 0) | |
61 gnt_text_view_scroll(view, -(wid->priv.height - 2)); | |
62 else | |
63 return FALSE; | |
64 return TRUE; | |
65 } | |
66 return FALSE; | |
67 } | |
68 | |
69 static void | |
15823 | 70 finch_debug_print(PurpleDebugLevel level, const char *category, |
15818 | 71 const char *args) |
72 { | |
73 if (debug.window && !debug.paused) | |
74 { | |
75 int pos = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(debug.tview)); | |
76 GntTextFormatFlags flag = GNT_TEXT_FLAG_NORMAL; | |
77 | |
78 if (debug.timestamps) { | |
79 const char *mdate; | |
80 time_t mtime = time(NULL); | |
15823 | 81 mdate = purple_utf8_strftime("%H:%M:%S ", localtime(&mtime)); |
15818 | 82 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(debug.tview), |
83 mdate, flag); | |
84 } | |
85 | |
86 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(debug.tview), | |
87 category, GNT_TEXT_FLAG_BOLD); | |
88 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(debug.tview), | |
89 ": ", GNT_TEXT_FLAG_BOLD); | |
90 | |
91 switch (level) | |
92 { | |
15823 | 93 case PURPLE_DEBUG_WARNING: |
15818 | 94 flag |= GNT_TEXT_FLAG_UNDERLINE; |
15823 | 95 case PURPLE_DEBUG_ERROR: |
96 case PURPLE_DEBUG_FATAL: | |
15818 | 97 flag |= GNT_TEXT_FLAG_BOLD; |
98 break; | |
99 default: | |
100 break; | |
101 } | |
102 | |
103 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(debug.tview), args, flag); | |
104 if (pos <= 1) | |
105 gnt_text_view_scroll(GNT_TEXT_VIEW(debug.tview), 0); | |
106 } | |
107 } | |
108 | |
15985
6dc5dc83a61b
Add a whimpy ui op to the debug API that returns TRUE if debugging
Mark Doliner <mark@kingant.net>
parents:
15871
diff
changeset
|
109 static gboolean |
6dc5dc83a61b
Add a whimpy ui op to the debug API that returns TRUE if debugging
Mark Doliner <mark@kingant.net>
parents:
15871
diff
changeset
|
110 finch_debug_is_enabled(PurpleDebugLevel level, const char *category) |
6dc5dc83a61b
Add a whimpy ui op to the debug API that returns TRUE if debugging
Mark Doliner <mark@kingant.net>
parents:
15871
diff
changeset
|
111 { |
6dc5dc83a61b
Add a whimpy ui op to the debug API that returns TRUE if debugging
Mark Doliner <mark@kingant.net>
parents:
15871
diff
changeset
|
112 return debug.window && !debug.paused; |
6dc5dc83a61b
Add a whimpy ui op to the debug API that returns TRUE if debugging
Mark Doliner <mark@kingant.net>
parents:
15871
diff
changeset
|
113 } |
6dc5dc83a61b
Add a whimpy ui op to the debug API that returns TRUE if debugging
Mark Doliner <mark@kingant.net>
parents:
15871
diff
changeset
|
114 |
15823 | 115 static PurpleDebugUiOps uiops = |
15818 | 116 { |
117 finch_debug_print, | |
15985
6dc5dc83a61b
Add a whimpy ui op to the debug API that returns TRUE if debugging
Mark Doliner <mark@kingant.net>
parents:
15871
diff
changeset
|
118 finch_debug_is_enabled |
15818 | 119 }; |
120 | |
15823 | 121 PurpleDebugUiOps *finch_debug_get_ui_ops() |
15818 | 122 { |
123 return &uiops; | |
124 } | |
125 | |
126 static void | |
127 reset_debug_win(GntWidget *w, gpointer null) | |
128 { | |
129 debug.window = debug.tview = NULL; | |
130 } | |
131 | |
132 static void | |
133 clear_debug_win(GntWidget *w, GntTextView *tv) | |
134 { | |
135 gnt_text_view_clear(tv); | |
136 } | |
137 | |
138 static void | |
139 print_stderr(const char *string) | |
140 { | |
141 g_printerr("%s", string); | |
142 } | |
143 | |
144 static void | |
145 suppress_error_messages(const char *message) | |
146 {} | |
147 | |
148 static void | |
149 toggle_pause(GntWidget *w, gpointer n) | |
150 { | |
151 debug.paused = !debug.paused; | |
152 } | |
153 | |
154 static void | |
155 toggle_timestamps(GntWidget *w, gpointer n) | |
156 { | |
157 debug.timestamps = !debug.timestamps; | |
15823 | 158 purple_prefs_set_bool("/core/debug/timestamps", debug.timestamps); |
15818 | 159 } |
160 | |
161 /* Xerox */ | |
162 static void | |
15823 | 163 purple_glib_log_handler(const gchar *domain, GLogLevelFlags flags, |
15818 | 164 const gchar *msg, gpointer user_data) |
165 { | |
15823 | 166 PurpleDebugLevel level; |
15818 | 167 char *new_msg = NULL; |
168 char *new_domain = NULL; | |
169 | |
170 if ((flags & G_LOG_LEVEL_ERROR) == G_LOG_LEVEL_ERROR) | |
15823 | 171 level = PURPLE_DEBUG_ERROR; |
15818 | 172 else if ((flags & G_LOG_LEVEL_CRITICAL) == G_LOG_LEVEL_CRITICAL) |
15823 | 173 level = PURPLE_DEBUG_FATAL; |
15818 | 174 else if ((flags & G_LOG_LEVEL_WARNING) == G_LOG_LEVEL_WARNING) |
15823 | 175 level = PURPLE_DEBUG_WARNING; |
15818 | 176 else if ((flags & G_LOG_LEVEL_MESSAGE) == G_LOG_LEVEL_MESSAGE) |
15823 | 177 level = PURPLE_DEBUG_INFO; |
15818 | 178 else if ((flags & G_LOG_LEVEL_INFO) == G_LOG_LEVEL_INFO) |
15823 | 179 level = PURPLE_DEBUG_INFO; |
15818 | 180 else if ((flags & G_LOG_LEVEL_DEBUG) == G_LOG_LEVEL_DEBUG) |
15823 | 181 level = PURPLE_DEBUG_MISC; |
15818 | 182 else |
183 { | |
15823 | 184 purple_debug_warning("gntdebug", |
15818 | 185 "Unknown glib logging level in %d\n", flags); |
186 | |
15823 | 187 level = PURPLE_DEBUG_MISC; /* This will never happen. */ |
15818 | 188 } |
189 | |
190 if (msg != NULL) | |
15823 | 191 new_msg = purple_utf8_try_convert(msg); |
15818 | 192 |
193 if (domain != NULL) | |
15823 | 194 new_domain = purple_utf8_try_convert(domain); |
15818 | 195 |
196 if (new_msg != NULL) | |
197 { | |
15823 | 198 purple_debug(level, (new_domain != NULL ? new_domain : "g_log"), |
15818 | 199 "%s\n", new_msg); |
200 | |
201 g_free(new_msg); | |
202 } | |
203 | |
204 g_free(new_domain); | |
205 } | |
206 | |
207 static void | |
208 size_changed_cb(GntWidget *widget, int oldw, int oldh) | |
209 { | |
210 int w, h; | |
211 gnt_widget_get_size(widget, &w, &h); | |
15823 | 212 purple_prefs_set_int(PREF_ROOT "/size/width", w); |
213 purple_prefs_set_int(PREF_ROOT "/size/height", h); | |
15818 | 214 } |
215 | |
216 void finch_debug_window_show() | |
217 { | |
218 debug.paused = FALSE; | |
15823 | 219 debug.timestamps = purple_prefs_get_bool("/core/debug/timestamps"); |
15818 | 220 if (debug.window == NULL) |
221 { | |
222 GntWidget *wid, *box; | |
223 debug.window = gnt_vbox_new(FALSE); | |
224 gnt_box_set_toplevel(GNT_BOX(debug.window), TRUE); | |
225 gnt_box_set_title(GNT_BOX(debug.window), _("Debug Window")); | |
226 gnt_box_set_pad(GNT_BOX(debug.window), 0); | |
227 gnt_box_set_alignment(GNT_BOX(debug.window), GNT_ALIGN_MID); | |
228 | |
229 debug.tview = gnt_text_view_new(); | |
230 gnt_box_add_widget(GNT_BOX(debug.window), debug.tview); | |
231 gnt_widget_set_size(debug.tview, | |
15823 | 232 purple_prefs_get_int(PREF_ROOT "/size/width"), |
233 purple_prefs_get_int(PREF_ROOT "/size/height")); | |
15818 | 234 g_signal_connect(G_OBJECT(debug.tview), "size_changed", G_CALLBACK(size_changed_cb), NULL); |
235 | |
236 gnt_box_add_widget(GNT_BOX(debug.window), gnt_line_new(FALSE)); | |
237 | |
238 box = gnt_hbox_new(FALSE); | |
239 gnt_box_set_alignment(GNT_BOX(box), GNT_ALIGN_MID); | |
240 gnt_box_set_fill(GNT_BOX(box), FALSE); | |
241 | |
242 /* XXX: Setting the GROW_Y for the following widgets don't make sense. But right now | |
243 * it's necessary to make the width of the debug window resizable ... like I said, | |
244 * it doesn't make sense. The bug is likely in the packing in gntbox.c. | |
245 */ | |
246 wid = gnt_button_new(_("Clear")); | |
247 g_signal_connect(G_OBJECT(wid), "activate", G_CALLBACK(clear_debug_win), debug.tview); | |
248 GNT_WIDGET_SET_FLAGS(wid, GNT_WIDGET_GROW_Y); | |
249 gnt_box_add_widget(GNT_BOX(box), wid); | |
250 | |
251 wid = gnt_check_box_new(_("Pause")); | |
252 g_signal_connect(G_OBJECT(wid), "toggled", G_CALLBACK(toggle_pause), NULL); | |
253 GNT_WIDGET_SET_FLAGS(wid, GNT_WIDGET_GROW_Y); | |
254 gnt_box_add_widget(GNT_BOX(box), wid); | |
255 | |
256 wid = gnt_check_box_new(_("Timestamps")); | |
257 gnt_check_box_set_checked(GNT_CHECK_BOX(wid), debug.timestamps); | |
258 g_signal_connect(G_OBJECT(wid), "toggled", G_CALLBACK(toggle_timestamps), NULL); | |
259 GNT_WIDGET_SET_FLAGS(wid, GNT_WIDGET_GROW_Y); | |
260 gnt_box_add_widget(GNT_BOX(box), wid); | |
261 | |
262 gnt_box_add_widget(GNT_BOX(debug.window), box); | |
263 GNT_WIDGET_SET_FLAGS(box, GNT_WIDGET_GROW_Y); | |
264 | |
265 gnt_widget_set_name(debug.window, "debug-window"); | |
266 | |
267 g_signal_connect(G_OBJECT(debug.window), "destroy", G_CALLBACK(reset_debug_win), NULL); | |
268 g_signal_connect(G_OBJECT(debug.window), "key_pressed", G_CALLBACK(debug_window_kpress_cb), debug.tview); | |
269 } | |
270 | |
271 gnt_widget_show(debug.window); | |
272 } | |
273 | |
274 static gboolean | |
275 start_with_debugwin(gpointer null) | |
276 { | |
277 finch_debug_window_show(); | |
278 return FALSE; | |
279 } | |
280 | |
281 void finch_debug_init() | |
282 { | |
283 /* Xerox */ | |
284 #define REGISTER_G_LOG_HANDLER(name) \ | |
285 g_log_set_handler((name), G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL \ | |
286 | G_LOG_FLAG_RECURSION, \ | |
15823 | 287 purple_glib_log_handler, NULL) |
15818 | 288 |
289 /* Register the glib log handlers. */ | |
290 REGISTER_G_LOG_HANDLER(NULL); | |
291 REGISTER_G_LOG_HANDLER("GLib"); | |
292 REGISTER_G_LOG_HANDLER("GModule"); | |
293 REGISTER_G_LOG_HANDLER("GLib-GObject"); | |
294 REGISTER_G_LOG_HANDLER("GThread"); | |
295 | |
296 g_set_print_handler(print_stderr); /* Redirect the debug messages to stderr */ | |
297 g_set_printerr_handler(suppress_error_messages); | |
298 | |
15823 | 299 purple_prefs_add_none(PREF_ROOT); |
300 purple_prefs_add_none(PREF_ROOT "/size"); | |
301 purple_prefs_add_int(PREF_ROOT "/size/width", 60); | |
302 purple_prefs_add_int(PREF_ROOT "/size/height", 15); | |
15818 | 303 |
15823 | 304 if (purple_debug_is_enabled()) |
15818 | 305 g_timeout_add(0, start_with_debugwin, NULL); |
306 } | |
307 | |
308 void finch_debug_uninit() | |
309 { | |
310 } | |
311 |