pidgin/pidgin

Fix several leaks in tests

14 months ago, Elliott Sales de Andrade
4e1bf25f5575
Fix several leaks in tests

All of these are specific to tests, not the library code.

For the moment, `protocol_xfer` still leaks connections (and anything they hold on to) because it is very difficult to disentangle them from the connection manager in the partially implemented state they are in.

This fixes leaks of options in the account option test (these two leaks occur for every test since they all leak the option):
```
61 (48 direct, 13 indirect) bytes in 1 blocks are definitely lost in loss record 133 of 276
at 0x4848464: calloc (vg_replace_malloc.c:1340)
by 0x49F75F0: g_malloc0 (gmem.c:163)
by 0x48C3B2E: purple_account_option_new (purpleaccountoption.c:78)
by 0x4014AF: test_purple_account_option_copy_int (test_account_option.c:67)
by 0x4A1CC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A1CC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1D181: g_test_run_suite (gtestutils.c:3115)
by 0x4A156EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A156EC: g_test_run (gtestutils.c:2221)
by 0x401721: main (test_account_option.c:157)

61 (48 direct, 13 indirect) bytes in 1 blocks are definitely lost in loss record 134 of 276
at 0x4848464: calloc (vg_replace_malloc.c:1340)
by 0x49F75F0: g_malloc0 (gmem.c:163)
by 0x48C3BC7: purple_account_option_copy (purpleaccountoption.c:93)
by 0x4014BF: test_purple_account_option_copy_int (test_account_option.c:68)
by 0x4A1CC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A1CC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1D181: g_test_run_suite (gtestutils.c:3115)
by 0x4A156EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A156EC: g_test_run (gtestutils.c:2221)
by 0x401721: main (test_account_option.c:157)
```
leaks in the credential manager test (times 3 for read/write/cancel tests):
```
69 (16 direct, 53 indirect) bytes in 1 blocks are definitely lost in loss record 2,427 of 3,503
at 0x484386F: malloc (vg_replace_malloc.c:393)
by 0x4A58168: g_malloc (gmem.c:130)
by 0x4A6FAB5: g_slice_alloc (gslice.c:1074)
by 0x4A700EC: g_slice_alloc0 (gslice.c:1100)
by 0x4A3BECB: g_error_allocate (gerror.c:710)
by 0x4A3C93F: UnknownInlinedFun (gerror.c:724)
by 0x4A3C93F: g_error_new_valist (gerror.c:766)
by 0x4BEE558: g_task_return_new_error (gtask.c:1941)
by 0x48D82C0: purple_credential_manager_read_password_async (purplecredentialmanager.c:492)
by 0x403634: test_purple_credential_manager_no_provider_read_password_idle (test_credential_manager.c:329)
by 0x4A4ECB1: g_idle_dispatch (gmain.c:6124)
by 0x4A4FCBE: UnknownInlinedFun (gmain.c:3444)
by 0x4A4FCBE: g_main_context_dispatch (gmain.c:4162)
by 0x4AA5597: g_main_context_iterate.constprop.0 (gmain.c:4238)
by 0x4A4F28E: g_main_loop_run (gmain.c:4438)
by 0x40369F: test_purple_credential_manager_no_provider_read_password_async (test_credential_manager.c:345)
by 0x4A7DC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A7DC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A7D9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A7D9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A7E181: g_test_run_suite (gtestutils.c:3115)
by 0x4A766EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A766EC: g_test_run (gtestutils.c:2221)
by 0x4048F6: main (test_credential_manager.c:695)
```
a leak in the image test:
```
161 bytes in 1 blocks are definitely lost in loss record 260 of 274
at 0x484386F: malloc (vg_replace_malloc.c:393)
by 0x4A55363: g_try_malloc (gmem.c:286)
by 0x4A3D630: UnknownInlinedFun (gfileutils.c:819)
by 0x4A3D630: UnknownInlinedFun (gfileutils.c:924)
by 0x4A3D630: g_file_get_contents (gfileutils.c:1027)
by 0x401890: test_image_new_from_file (test_image.c:144)
by 0x4A7DC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A7DC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A7D9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A7E181: g_test_run_suite (gtestutils.c:3115)
by 0x4A766EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A766EC: g_test_run (gtestutils.c:2221)
by 0x40195D: main (test_image.c:172)
```
a leak in queued output stream test:
```
72 (40 direct, 32 indirect) bytes in 1 blocks are definitely lost in loss record 219 of 396
at 0x49D51EF: g_type_create_instance (gtype.c:1909)
by 0x49BAC1F: g_object_new_internal (gobject.c:2228)
by 0x49BC247: g_object_new_with_properties (gobject.c:2391)
by 0x49BCFF0: g_object_new (gobject.c:2037)
by 0x402003: test_queued_output_stream_push_bytes_async_error (test_queued_output_stream.c:219)
by 0x4A7DC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A7DC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A7D9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A7E181: g_test_run_suite (gtestutils.c:3115)
by 0x4A766EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A766EC: g_test_run (gtestutils.c:2221)
by 0x402429: main (test_queued_output_stream.c:280)
```
and protocol xfer tests (times 3 for each test that creates a test protocol object):
```
112 (48 direct, 64 indirect) bytes in 1 blocks are definitely lost in loss record 3,430 of 3,698
at 0x49D51EF: g_type_create_instance (gtype.c:1909)
by 0x49BAC1F: g_object_new_internal (gobject.c:2228)
by 0x49BC247: g_object_new_with_properties (gobject.c:2391)
by 0x49BCFF0: g_object_new (gobject.c:2037)
by 0x40291C: test_purple_protocol_xfer_send_file_func (test_protocol_xfer.c:146)
by 0x4A7DC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A7DC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A7D9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A7E181: g_test_run_suite (gtestutils.c:3115)
by 0x4A766EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A766EC: g_test_run (gtestutils.c:2221)
by 0x402B64: main (test_protocol_xfer.c:195)
```
and util tests (times 3 for each call to `purple_text_strip_mnemonic` in the test):
```
5 bytes in 1 blocks are definitely lost in loss record 5 of 247
at 0x484386F: malloc (vg_replace_malloc.c:393)
by 0x49F7168: g_malloc (gmem.c:130)
by 0x491975B: purple_text_strip_mnemonic (util.c:895)
by 0x4015B0: test_util_text_strip_mnemonic (test_util.c:49)
by 0x4A1CC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A1CC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1D181: g_test_run_suite (gtestutils.c:3115)
by 0x4A156EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A156EC: g_test_run (gtestutils.c:2221)
by 0x401901: main (test_util.c:224)
```
and these leaks in any test that initializes the test UI:
```
4,104 bytes in 1 blocks are possibly lost in loss record 3,451 of 3,457
at 0x484386F: malloc (vg_replace_malloc.c:393)
by 0x5235B67: sqlite3MemMalloc.lto_priv.0 (sqlite3.c:25493)
by 0x5232797: UnknownInlinedFun (sqlite3.c:29181)
by 0x5232797: UnknownInlinedFun (sqlite3.c:29227)
by 0x5232797: sqlite3Malloc.lto_priv.0 (sqlite3.c:29221)
by 0x523BD8B: pcache1Alloc.lto_priv.0 (sqlite3.c:53546)
by 0x5249A8B: UnknownInlinedFun (sqlite3.c:53675)
by 0x5249A8B: allocateTempSpace (sqlite3.c:70848)
by 0x52625A6: sqlite3VdbeExec.lto_priv.0 (sqlite3.c:93857)
by 0x525CBEE: UnknownInlinedFun (sqlite3.c:87995)
by 0x525CBEE: UnknownInlinedFun (sqlite3.c:88056)
by 0x525CBEE: sqlite3_step (sqlite3.c:88045)
by 0x529B324: sqlite3_exec (sqlite3.c:131002)
by 0x48FD558: purple_sqlite3_run_migration (purplesqlite3.c:37)
by 0x48FDBB4: purple_sqlite3_run_migrations_from_resources (purplesqlite3.c:195)
by 0x48FDED9: purple_sqlite_history_adapter_run_migrations (purplesqlitehistoryadapter.c:69)
by 0x48FE7F0: purple_sqlite_history_adapter_activate (purplesqlitehistoryadapter.c:287)
by 0x48DB656: purple_history_adapter_activate (purplehistoryadapter.c:181)
by 0x48DC9BC: purple_history_manager_set_active (purplehistorymanager.c:308)
by 0x402BA8: test_ui_init_history (test_ui.c:132)
by 0x402C80: test_ui_purple_init (test_ui.c:167)
by 0x4027BB: main (test_contact.c:88)

4,368 bytes in 1 blocks are possibly lost in loss record 3,453 of 3,457
at 0x484386F: malloc (vg_replace_malloc.c:393)
by 0x5235B67: sqlite3MemMalloc.lto_priv.0 (sqlite3.c:25493)
by 0x5232797: UnknownInlinedFun (sqlite3.c:29181)
by 0x5232797: UnknownInlinedFun (sqlite3.c:29227)
by 0x5232797: sqlite3Malloc.lto_priv.0 (sqlite3.c:29221)
by 0x523BD8B: pcache1Alloc.lto_priv.0 (sqlite3.c:53546)
by 0x5240077: UnknownInlinedFun (sqlite3.c:53634)
by 0x5240077: pcache1FetchStage2 (sqlite3.c:54104)
by 0x5243E9C: UnknownInlinedFun (sqlite3.c:52671)
by 0x5243E9C: getPageNormal.lto_priv.0 (sqlite3.c:60628)
by 0x524A510: UnknownInlinedFun (sqlite3.c:60805)
by 0x524A510: btreeGetPage.lto_priv.0 (sqlite3.c:70289)
by 0x524C2F6: UnknownInlinedFun (sqlite3.c:71257)
by 0x524C2F6: sqlite3BtreeBeginTrans.lto_priv.0 (sqlite3.c:71647)
by 0x5266B3A: sqlite3VdbeExec.lto_priv.0 (sqlite3.c:93532)
by 0x525CBEE: UnknownInlinedFun (sqlite3.c:87995)
by 0x525CBEE: UnknownInlinedFun (sqlite3.c:88056)
by 0x525CBEE: sqlite3_step (sqlite3.c:88045)
by 0x48FD715: purple_sqlite3_get_schema_version (purplesqlite3.c:79)
by 0x48FD9DD: purple_sqlite3_run_migrations_from_resources (purplesqlite3.c:146)
by 0x48FDED9: purple_sqlite_history_adapter_run_migrations (purplesqlitehistoryadapter.c:69)
by 0x48FE7F0: purple_sqlite_history_adapter_activate (purplesqlitehistoryadapter.c:287)
by 0x48DB656: purple_history_adapter_activate (purplehistoryadapter.c:181)
by 0x48DC9BC: purple_history_manager_set_active (purplehistorymanager.c:308)
by 0x402BA8: test_ui_init_history (test_ui.c:132)
by 0x402C80: test_ui_purple_init (test_ui.c:167)
by 0x4027BB: main (test_contact.c:88)

4,368 bytes in 1 blocks are possibly lost in loss record 3,454 of 3,457
at 0x484386F: malloc (vg_replace_malloc.c:393)
by 0x5235B67: sqlite3MemMalloc.lto_priv.0 (sqlite3.c:25493)
by 0x5232797: UnknownInlinedFun (sqlite3.c:29181)
by 0x5232797: UnknownInlinedFun (sqlite3.c:29227)
by 0x5232797: sqlite3Malloc.lto_priv.0 (sqlite3.c:29221)
by 0x523BD8B: pcache1Alloc.lto_priv.0 (sqlite3.c:53546)
by 0x5240077: UnknownInlinedFun (sqlite3.c:53634)
by 0x5240077: pcache1FetchStage2 (sqlite3.c:54104)
by 0x5243E9C: UnknownInlinedFun (sqlite3.c:52671)
by 0x5243E9C: getPageNormal.lto_priv.0 (sqlite3.c:60628)
by 0x52499BC: UnknownInlinedFun (sqlite3.c:60805)
by 0x52499BC: UnknownInlinedFun (sqlite3.c:70289)
by 0x52499BC: btreeGetUnusedPage (sqlite3.c:70432)
by 0x524F504: allocateBtreePage.lto_priv.0 (sqlite3.c:74604)
by 0x5256209: btreeCreateTable.lto_priv.0 (sqlite3.c:77830)
by 0x5265EB2: UnknownInlinedFun (sqlite3.c:77849)
by 0x5265EB2: sqlite3VdbeExec.lto_priv.0 (sqlite3.c:96382)
by 0x525CBEE: UnknownInlinedFun (sqlite3.c:87995)
by 0x525CBEE: UnknownInlinedFun (sqlite3.c:88056)
by 0x525CBEE: sqlite3_step (sqlite3.c:88045)
by 0x529B324: sqlite3_exec (sqlite3.c:131002)
by 0x48FD558: purple_sqlite3_run_migration (purplesqlite3.c:37)
by 0x48FDBB4: purple_sqlite3_run_migrations_from_resources (purplesqlite3.c:195)
by 0x48FDED9: purple_sqlite_history_adapter_run_migrations (purplesqlitehistoryadapter.c:69)
by 0x48FE7F0: purple_sqlite_history_adapter_activate (purplesqlitehistoryadapter.c:287)
by 0x48DB656: purple_history_adapter_activate (purplehistoryadapter.c:181)
by 0x48DC9BC: purple_history_manager_set_active (purplehistorymanager.c:308)
by 0x402BA8: test_ui_init_history (test_ui.c:132)
by 0x402C80: test_ui_purple_init (test_ui.c:167)
by 0x4027BB: main (test_contact.c:88)
```

Testing Done:
Ran tests in valgrind, and all above leaks were gone except for noted `protocol_xfer` issues, and a bunch of leaks of the `PurpleBuddy`-`PurpleContact` compatibility bindings, which will go away in their entirety eventually.

Reviewed at https://reviews.imfreedom.org/r/2385/
/* pidgin
*
* Pidgin is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
#include <purple.h>
#include "gtkdialogs.h"
#include "gtkutils.h"
#include "pidginapplication.h"
#include "pidgincore.h"
#include "pidgindebug.h"
#include <gdk/gdkkeysyms.h>
struct _PidginDebugWindow {
GtkWindow parent;
GtkWidget *textview;
GtkTextBuffer *buffer;
GtkTextMark *start_mark;
GtkTextMark *end_mark;
struct {
GtkTextTag *level[PURPLE_DEBUG_FATAL + 1];
GtkTextTag *category;
GtkTextTag *filtered_invisible;
GtkTextTag *filtered_visible;
GtkTextTag *match;
GtkTextTag *paused;
} tags;
GtkWidget *filter;
GtkWidget *expression;
GtkWidget *filterlevel;
gboolean paused;
GtkWidget *popover;
GtkWidget *popover_invert;
GtkWidget *popover_highlight;
gboolean invert;
gboolean highlight;
GRegex *regex;
};
typedef struct {
GDateTime *timestamp;
PurpleDebugLevel level;
gchar *domain;
gchar *message;
} PidginDebugMessage;
static gboolean debug_print_enabled = FALSE;
static PidginDebugWindow *debug_win = NULL;
static guint pref_callback_id = 0;
static guint debug_enabled_timer = 0;
G_DEFINE_TYPE(PidginDebugWindow, pidgin_debug_window, GTK_TYPE_WINDOW);
static gboolean
save_default_size_cb(GObject *gobject, G_GNUC_UNUSED GParamSpec *pspec,
G_GNUC_UNUSED gpointer data)
{
if (gtk_widget_get_visible(GTK_WIDGET(gobject))) {
gint width, height;
gtk_window_get_default_size(GTK_WINDOW(gobject), &width, &height);
purple_prefs_set_int(PIDGIN_PREFS_ROOT "/debug/width", width);
purple_prefs_set_int(PIDGIN_PREFS_ROOT "/debug/height", height);
}
return FALSE;
}
static gboolean
view_near_bottom(PidginDebugWindow *win)
{
GtkAdjustment *adj = gtk_scrollable_get_vadjustment(
GTK_SCROLLABLE(win->textview));
return (gtk_adjustment_get_value(adj) >=
(gtk_adjustment_get_upper(adj) -
gtk_adjustment_get_page_size(adj) * 1.5));
}
static void
save_response_cb(GtkNativeDialog *self, gint response_id, gpointer data)
{
PidginDebugWindow *win = (PidginDebugWindow *)data;
if(response_id == GTK_RESPONSE_ACCEPT) {
GFile *file = NULL;
GFileOutputStream *output = NULL;
GtkTextIter start, end;
GDateTime *date = NULL;
gchar *date_str = NULL;
gchar *tmp = NULL;
GError *error = NULL;
file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(self));
output = g_file_replace(file, NULL, FALSE, G_FILE_CREATE_NONE, NULL,
&error);
g_object_unref(file);
if(output == NULL) {
purple_debug_error("debug",
"Unable to open file to save debug log: %s",
error->message);
g_error_free(error);
g_object_unref(self);
return;
}
date = g_date_time_new_now_local();
date_str = g_date_time_format(date, "%c");
g_date_time_unref(date);
tmp = g_strdup_printf("Pidgin Debug Log : %s\n", date_str);
g_output_stream_write_all(G_OUTPUT_STREAM(output), tmp, strlen(tmp),
NULL, NULL, &error);
g_free(tmp);
g_free(date_str);
if(error != NULL) {
purple_debug_error("debug", "Unable to save debug log: %s",
error->message);
g_error_free(error);
g_object_unref(output);
g_object_unref(self);
return;
}
gtk_text_buffer_get_bounds(win->buffer, &start, &end);
tmp = gtk_text_buffer_get_text(win->buffer, &start, &end, TRUE);
g_output_stream_write_all(G_OUTPUT_STREAM(output), tmp, strlen(tmp),
NULL, NULL, &error);
g_free(tmp);
if(error != NULL) {
purple_debug_error("debug", "Unable to save debug log: %s",
error->message);
g_error_free(error);
g_object_unref(output);
g_object_unref(self);
return;
}
g_object_unref(output);
}
g_object_unref(self);
}
static void
save_cb(G_GNUC_UNUSED GtkWidget *w, PidginDebugWindow *win)
{
GtkFileChooserNative *filesel;
filesel = gtk_file_chooser_native_new(_("Save Debug Log"), GTK_WINDOW(win),
GTK_FILE_CHOOSER_ACTION_SAVE,
_("_Save"), _("_Cancel"));
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(filesel),
"purple-debug.log");
g_signal_connect(filesel, "response", G_CALLBACK(save_response_cb), win);
gtk_native_dialog_set_modal(GTK_NATIVE_DIALOG(filesel), TRUE);
gtk_native_dialog_show(GTK_NATIVE_DIALOG(filesel));
}
static void
clear_cb(G_GNUC_UNUSED GtkWidget *w, PidginDebugWindow *win)
{
gtk_text_buffer_set_text(win->buffer, "", 0);
}
static void
pause_cb(GtkWidget *w, PidginDebugWindow *win)
{
win->paused = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w));
if (!win->paused) {
GtkTextIter start, end;
gtk_text_buffer_get_bounds(win->buffer, &start, &end);
gtk_text_buffer_remove_tag(win->buffer, win->tags.paused,
&start, &end);
gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(win->textview),
win->end_mark, 0, TRUE, 0, 1);
}
}
/******************************************************************************
* regex stuff
*****************************************************************************/
static void
regex_clear_color(GtkWidget *w) {
gtk_widget_remove_css_class(w, "error");
gtk_widget_remove_css_class(w, "success");
}
static void
regex_change_color(GtkWidget *w, gboolean success) {
if (success) {
gtk_widget_remove_css_class(w, "error");
gtk_widget_add_css_class(w, "success");
} else {
gtk_widget_remove_css_class(w, "success");
gtk_widget_add_css_class(w, "error");
}
}
static void
do_regex(PidginDebugWindow *win, GtkTextIter *start, GtkTextIter *end)
{
GMatchInfo *match;
gint initial_position;
gint start_pos, end_pos;
GtkTextIter match_start, match_end;
gchar *text;
if (!win->regex) {
return;
}
initial_position = gtk_text_iter_get_offset(start);
if (!win->invert) {
/* First hide everything. */
gtk_text_buffer_apply_tag(win->buffer,
win->tags.filtered_invisible, start, end);
}
text = gtk_text_buffer_get_text(win->buffer, start, end, TRUE);
g_regex_match(win->regex, text, 0, &match);
while (g_match_info_matches(match)) {
g_match_info_fetch_pos(match, 0, &start_pos, &end_pos);
start_pos += initial_position;
end_pos += initial_position;
/* Expand match to full line of message. */
gtk_text_buffer_get_iter_at_offset(win->buffer,
&match_start, start_pos);
gtk_text_iter_set_line_index(&match_start, 0);
gtk_text_buffer_get_iter_at_offset(win->buffer,
&match_end, end_pos);
gtk_text_iter_forward_line(&match_end);
if (win->invert) {
/* Make invisible. */
gtk_text_buffer_apply_tag(win->buffer,
win->tags.filtered_invisible,
&match_start, &match_end);
} else {
/* Make visible again (with higher priority.) */
gtk_text_buffer_apply_tag(win->buffer,
win->tags.filtered_visible,
&match_start, &match_end);
if (win->highlight) {
gtk_text_buffer_get_iter_at_offset(
win->buffer,
&match_start,
start_pos);
gtk_text_buffer_get_iter_at_offset(
win->buffer,
&match_end,
end_pos);
gtk_text_buffer_apply_tag(win->buffer,
win->tags.match,
&match_start,
&match_end);
}
}
g_match_info_next(match, NULL);
}
g_match_info_free(match);
g_free(text);
}
static void
regex_toggle_filter(PidginDebugWindow *win, gboolean filter)
{
GtkTextIter start, end;
gtk_text_buffer_get_bounds(win->buffer, &start, &end);
gtk_text_buffer_remove_tag(win->buffer, win->tags.match, &start, &end);
gtk_text_buffer_remove_tag(win->buffer, win->tags.filtered_invisible,
&start, &end);
gtk_text_buffer_remove_tag(win->buffer, win->tags.filtered_visible,
&start, &end);
if (filter) {
do_regex(win, &start, &end);
}
}
static void
regex_pref_filter_cb(G_GNUC_UNUSED const gchar *name,
G_GNUC_UNUSED PurplePrefType type,
gconstpointer val, gpointer data)
{
PidginDebugWindow *win = (PidginDebugWindow *)data;
gboolean active = GPOINTER_TO_INT(val), current;
if (!win) {
return;
}
current = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(win->filter));
if (active != current) {
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(win->filter), active);
}
}
static void
regex_pref_invert_cb(G_GNUC_UNUSED const gchar *name,
G_GNUC_UNUSED PurplePrefType type,
gconstpointer val, gpointer data)
{
PidginDebugWindow *win = (PidginDebugWindow *)data;
gboolean active = GPOINTER_TO_INT(val);
win->invert = active;
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(win->filter))) {
regex_toggle_filter(win, TRUE);
}
}
static void
regex_pref_highlight_cb(G_GNUC_UNUSED const gchar *name,
G_GNUC_UNUSED PurplePrefType type,
gconstpointer val, gpointer data)
{
PidginDebugWindow *win = (PidginDebugWindow *)data;
gboolean active = GPOINTER_TO_INT(val);
win->highlight = active;
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(win->filter))) {
regex_toggle_filter(win, TRUE);
}
}
static void
regex_changed_cb(G_GNUC_UNUSED GtkWidget *w, PidginDebugWindow *win) {
const gchar *text;
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(win->filter))) {
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(win->filter), FALSE);
}
text = gtk_editable_get_text(GTK_EDITABLE(win->expression));
purple_prefs_set_string(PIDGIN_PREFS_ROOT "/debug/regex", text);
if (text == NULL || *text == '\0') {
regex_clear_color(win->expression);
gtk_widget_set_sensitive(win->filter, FALSE);
return;
}
g_clear_pointer(&win->regex, g_regex_unref);
win->regex = g_regex_new(text, G_REGEX_CASELESS|G_REGEX_JAVASCRIPT_COMPAT, 0, NULL);
if (win->regex == NULL) {
/* failed to compile */
regex_change_color(win->expression, FALSE);
gtk_widget_set_sensitive(win->filter, FALSE);
} else {
/* compiled successfully */
regex_change_color(win->expression, TRUE);
gtk_widget_set_sensitive(win->filter, TRUE);
}
}
static void
regex_key_released_cb(G_GNUC_UNUSED GtkEventControllerKey *controller,
guint keyval, G_GNUC_UNUSED guint keycode,
G_GNUC_UNUSED GdkModifierType state, gpointer data)
{
PidginDebugWindow *win = data;
if (gtk_widget_is_sensitive(win->filter)) {
GtkToggleButton *tb = GTK_TOGGLE_BUTTON(win->filter);
if ((keyval == GDK_KEY_Return || keyval == GDK_KEY_KP_Enter) &&
!gtk_toggle_button_get_active(tb))
{
gtk_toggle_button_set_active(tb, TRUE);
}
if (keyval == GDK_KEY_Escape && gtk_toggle_button_get_active(tb)) {
gtk_toggle_button_set_active(tb, FALSE);
}
}
}
static void
regex_menu_cb(GtkWidget *item, PidginDebugWindow *win)
{
gboolean active;
active = gtk_check_button_get_active(GTK_CHECK_BUTTON(item));
if (item == win->popover_highlight) {
purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/debug/highlight", active);
} else if (item == win->popover_invert) {
purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/debug/invert", active);
}
}
static void
regex_popup_cb(G_GNUC_UNUSED GtkGestureClick* self, G_GNUC_UNUSED gint n_press,
gdouble x, gdouble y, gpointer data)
{
PidginDebugWindow *win = data;
gtk_popover_set_pointing_to(GTK_POPOVER(win->popover),
&(const GdkRectangle){(int)x, (int)y, 0, 0});
gtk_popover_popup(GTK_POPOVER(win->popover));
}
static void
regex_filter_toggled_cb(GtkToggleButton *button, PidginDebugWindow *win)
{
gboolean active;
active = gtk_toggle_button_get_active(button);
purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/debug/filter", active);
regex_toggle_filter(win, active);
}
static void
debug_window_set_filter_level(PidginDebugWindow *win, int level)
{
gboolean scroll;
int i;
if (level != (int)gtk_drop_down_get_selected(GTK_DROP_DOWN(win->filterlevel))) {
gtk_drop_down_set_selected(GTK_DROP_DOWN(win->filterlevel), level);
}
scroll = view_near_bottom(win);
for (i = 0; i <= PURPLE_DEBUG_FATAL; i++) {
g_object_set(G_OBJECT(win->tags.level[i]),
"invisible", i < level,
NULL);
}
if (scroll) {
gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(win->textview),
win->end_mark, 0, TRUE, 0, 1);
}
}
static void
filter_level_pref_changed(G_GNUC_UNUSED const char *name,
G_GNUC_UNUSED PurplePrefType type,
gconstpointer value, gpointer data)
{
PidginDebugWindow *win = data;
int level = GPOINTER_TO_INT(value);
debug_window_set_filter_level(win, level);
}
static void
filter_level_changed_cb(GObject *obj, G_GNUC_UNUSED GParamSpec *pspec)
{
GtkDropDown *dropdown = GTK_DROP_DOWN(obj);
purple_prefs_set_int(PIDGIN_PREFS_ROOT "/debug/filterlevel",
gtk_drop_down_get_selected(dropdown));
}
static void
pidgin_debug_window_dispose(GObject *object)
{
PidginDebugWindow *win = PIDGIN_DEBUG_WINDOW(object);
gtk_widget_unparent(win->popover);
G_OBJECT_CLASS(pidgin_debug_window_parent_class)->dispose(object);
}
static void
pidgin_debug_window_finalize(GObject *object)
{
PidginDebugWindow *win = PIDGIN_DEBUG_WINDOW(object);
purple_prefs_disconnect_by_handle(pidgin_debug_get_handle());
g_clear_pointer(&win->regex, g_regex_unref);
debug_win = NULL;
purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/debug/enabled", FALSE);
G_OBJECT_CLASS(pidgin_debug_window_parent_class)->finalize(object);
}
static void
pidgin_debug_window_class_init(PidginDebugWindowClass *klass) {
GObjectClass *obj_class = G_OBJECT_CLASS(klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
obj_class->dispose = pidgin_debug_window_dispose;
obj_class->finalize = pidgin_debug_window_finalize;
gtk_widget_class_set_template_from_resource(
widget_class,
"/im/pidgin/Pidgin3/Debug/debug.ui"
);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, textview);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, buffer);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, tags.category);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, tags.filtered_invisible);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, tags.filtered_visible);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, tags.level[0]);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, tags.level[1]);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, tags.level[2]);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, tags.level[3]);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, tags.level[4]);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, tags.level[5]);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, tags.paused);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, filter);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, filterlevel);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, expression);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, tags.match);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, popover);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, popover_invert);
gtk_widget_class_bind_template_child(
widget_class, PidginDebugWindow, popover_highlight);
gtk_widget_class_bind_template_callback(widget_class, save_cb);
gtk_widget_class_bind_template_callback(widget_class, clear_cb);
gtk_widget_class_bind_template_callback(widget_class, pause_cb);
gtk_widget_class_bind_template_callback(widget_class,
regex_filter_toggled_cb);
gtk_widget_class_bind_template_callback(widget_class,
regex_changed_cb);
gtk_widget_class_bind_template_callback(widget_class, regex_popup_cb);
gtk_widget_class_bind_template_callback(widget_class, regex_menu_cb);
gtk_widget_class_bind_template_callback(widget_class,
regex_key_released_cb);
gtk_widget_class_bind_template_callback(widget_class,
filter_level_changed_cb);
}
static void
pidgin_debug_window_init(PidginDebugWindow *win)
{
gint width, height;
void *handle;
GtkTextIter end;
gtk_widget_init_template(GTK_WIDGET(win));
gtk_widget_set_parent(win->popover, win->filter);
width = purple_prefs_get_int(PIDGIN_PREFS_ROOT "/debug/width");
height = purple_prefs_get_int(PIDGIN_PREFS_ROOT "/debug/height");
purple_debug_info("pidgindebug", "Setting dimensions to %d, %d\n",
width, height);
gtk_window_set_default_size(GTK_WINDOW(win), width, height);
g_signal_connect(G_OBJECT(win), "notify::default-width",
G_CALLBACK(save_default_size_cb), NULL);
g_signal_connect(G_OBJECT(win), "notify::default-height",
G_CALLBACK(save_default_size_cb), NULL);
handle = pidgin_debug_get_handle();
/* we purposely disable the toggle button here in case
* /purple/gtk/debug/expression has an empty string. If it does not have
* an empty string, the change signal will get called and make the
* toggle button sensitive.
*/
gtk_widget_set_sensitive(win->filter, FALSE);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(win->filter),
purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/debug/filter"));
purple_prefs_connect_callback(handle, PIDGIN_PREFS_ROOT "/debug/filter",
regex_pref_filter_cb, win);
/* regex entry */
gtk_editable_set_text(GTK_EDITABLE(win->expression),
purple_prefs_get_string(PIDGIN_PREFS_ROOT "/debug/regex"));
/* connect the rest of our pref callbacks */
win->invert = purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/debug/invert");
purple_prefs_connect_callback(handle, PIDGIN_PREFS_ROOT "/debug/invert",
regex_pref_invert_cb, win);
win->highlight = purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/debug/highlight");
purple_prefs_connect_callback(handle, PIDGIN_PREFS_ROOT "/debug/highlight",
regex_pref_highlight_cb, win);
gtk_drop_down_set_selected(GTK_DROP_DOWN(win->filterlevel),
purple_prefs_get_int(PIDGIN_PREFS_ROOT "/debug/filterlevel"));
purple_prefs_connect_callback(handle, PIDGIN_PREFS_ROOT "/debug/filterlevel",
filter_level_pref_changed, win);
gtk_check_button_set_active(GTK_CHECK_BUTTON(win->popover_invert),
win->invert);
gtk_check_button_set_active(GTK_CHECK_BUTTON(win->popover_highlight),
win->highlight);
/* The *start* and *end* marks bound the beginning and end of an
insertion, used for filtering. The *end* mark is also used for
auto-scrolling. */
gtk_text_buffer_get_end_iter(win->buffer, &end);
win->start_mark = gtk_text_buffer_create_mark(win->buffer,
"start", &end, TRUE);
win->end_mark = gtk_text_buffer_create_mark(win->buffer,
"end", &end, FALSE);
/* Set active filter level in textview */
debug_window_set_filter_level(win,
purple_prefs_get_int(PIDGIN_PREFS_ROOT "/debug/filterlevel"));
clear_cb(NULL, win);
}
static gboolean
debug_enabled_timeout_cb(gpointer data)
{
gboolean enabled = GPOINTER_TO_INT(data);
debug_enabled_timer = 0;
if (enabled) {
pidgin_debug_window_show();
} else {
pidgin_debug_window_hide();
}
return FALSE;
}
static void
debug_enabled_cb(G_GNUC_UNUSED const gchar *name,
G_GNUC_UNUSED PurplePrefType type,
gconstpointer value,
G_GNUC_UNUSED gpointer data)
{
debug_enabled_timer = g_timeout_add(0, debug_enabled_timeout_cb,
(gpointer)value);
}
static gboolean
pidgin_debug_g_log_handler_cb(gpointer data)
{
PidginDebugMessage *message = data;
GtkTextTag *level_tag = NULL;
gchar *local_time = NULL;
GtkTextIter end;
gboolean scroll;
if (debug_win == NULL ||
!purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/debug/enabled")) {
/* The Debug Window may have been closed/disabled after the thread that
* sent this message. */
g_date_time_unref(message->timestamp);
g_free(message->domain);
g_free(message->message);
g_free(message);
return FALSE;
}
scroll = view_near_bottom(debug_win);
gtk_text_buffer_get_end_iter(debug_win->buffer, &end);
gtk_text_buffer_move_mark(debug_win->buffer, debug_win->start_mark, &end);
level_tag = debug_win->tags.level[message->level];
local_time = g_date_time_format(message->timestamp, "(%H:%M:%S) ");
gtk_text_buffer_insert_with_tags(
debug_win->buffer,
&end,
local_time,
-1,
level_tag,
debug_win->paused ? debug_win->tags.paused : NULL,
NULL);
if (message->domain != NULL && *message->domain != '\0') {
gtk_text_buffer_insert_with_tags(
debug_win->buffer,
&end,
message->domain,
-1,
level_tag,
debug_win->tags.category,
debug_win->paused ? debug_win->tags.paused : NULL,
NULL);
gtk_text_buffer_insert_with_tags(
debug_win->buffer,
&end,
": ",
2,
level_tag,
debug_win->tags.category,
debug_win->paused ? debug_win->tags.paused : NULL,
NULL);
}
gtk_text_buffer_insert_with_tags(
debug_win->buffer,
&end,
message->message,
-1,
level_tag,
debug_win->paused ? debug_win->tags.paused : NULL,
NULL);
gtk_text_buffer_insert_with_tags(
debug_win->buffer,
&end,
"\n",
1,
level_tag,
debug_win->paused ? debug_win->tags.paused : NULL,
NULL);
if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/debug/filter") &&
debug_win->regex) {
/* Filter out any new messages. */
GtkTextIter start;
gtk_text_buffer_get_iter_at_mark(debug_win->buffer, &start,
debug_win->start_mark);
gtk_text_buffer_get_iter_at_mark(debug_win->buffer, &end,
debug_win->end_mark);
do_regex(debug_win, &start, &end);
}
if (scroll) {
gtk_text_view_scroll_to_mark(
GTK_TEXT_VIEW(debug_win->textview),
debug_win->end_mark, 0, TRUE, 0, 1);
}
g_free(local_time);
g_date_time_unref(message->timestamp);
g_free(message->domain);
g_free(message->message);
g_free(message);
return FALSE;
}
static GLogWriterOutput
pidgin_debug_g_log_handler(GLogLevelFlags log_level, const GLogField *fields,
gsize n_fields, G_GNUC_UNUSED gpointer user_data)
{
PidginDebugMessage *message = NULL;
gsize i;
if (debug_win == NULL) {
if (debug_print_enabled) {
return g_log_writer_default(log_level, fields, n_fields, user_data);
} else {
return G_LOG_WRITER_UNHANDLED;
}
}
message = g_new0(PidginDebugMessage, 1);
message->timestamp = g_date_time_new_now_local();
for (i = 0; i < n_fields; i++) {
if (purple_strequal(fields[i].key, "GLIB_DOMAIN")) {
message->domain = g_strdup(fields[i].value);
} else if (purple_strequal(fields[i].key, "MESSAGE")) {
message->message = g_strdup(fields[i].value);
}
}
if((log_level & G_LOG_LEVEL_ERROR) != 0) {
message->level = PURPLE_DEBUG_ERROR;
} else if((log_level & G_LOG_LEVEL_CRITICAL) != 0) {
message->level = PURPLE_DEBUG_FATAL;
} else if((log_level & G_LOG_LEVEL_WARNING) != 0) {
message->level = PURPLE_DEBUG_WARNING;
} else if((log_level & G_LOG_LEVEL_MESSAGE) != 0) {
message->level = PURPLE_DEBUG_INFO;
} else if((log_level & G_LOG_LEVEL_INFO) != 0) {
message->level = PURPLE_DEBUG_INFO;
} else if((log_level & G_LOG_LEVEL_DEBUG) != 0) {
message->level = PURPLE_DEBUG_MISC;
} else {
message->level = PURPLE_DEBUG_MISC;
}
g_timeout_add(0, pidgin_debug_g_log_handler_cb, message);
if (debug_print_enabled) {
return g_log_writer_default(log_level, fields, n_fields, user_data);
} else {
return G_LOG_WRITER_HANDLED;
}
}
void
pidgin_debug_window_show(void)
{
if (debug_win == NULL) {
GApplication *application = NULL;
PidginApplication *pidgin_application = NULL;
GtkWindow *parent = NULL;
application = g_application_get_default();
pidgin_application = PIDGIN_APPLICATION(application);
parent = pidgin_application_get_active_window(pidgin_application);
debug_win = PIDGIN_DEBUG_WINDOW(
g_object_new(PIDGIN_TYPE_DEBUG_WINDOW, NULL));
gtk_window_set_transient_for(GTK_WINDOW(debug_win), parent);
}
gtk_window_present_with_time(GTK_WINDOW(debug_win), GDK_CURRENT_TIME);
purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/debug/enabled", TRUE);
}
void
pidgin_debug_window_hide(void)
{
if (debug_win != NULL) {
gtk_window_destroy(GTK_WINDOW(debug_win));
}
}
void
pidgin_debug_init_handler(void)
{
g_log_set_writer_func(pidgin_debug_g_log_handler, NULL, NULL);
}
void
pidgin_debug_set_print_enabled(gboolean enable)
{
debug_print_enabled = enable;
}
void
pidgin_debug_init(void)
{
/* Debug window preferences. */
/*
* NOTE: This must be set before prefs are loaded, and the callbacks
* set after they are loaded, since prefs sets the enabled
* preference here and that loads the window, which calls the
* configure event, which overrides the width and height! :P
*/
purple_prefs_add_none(PIDGIN_PREFS_ROOT "/debug");
/* Controls printing to the debug window */
purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/debug/enabled", FALSE);
purple_prefs_add_int(PIDGIN_PREFS_ROOT "/debug/filterlevel",
PURPLE_DEBUG_ALL);
purple_prefs_add_int(PIDGIN_PREFS_ROOT "/debug/width", 450);
purple_prefs_add_int(PIDGIN_PREFS_ROOT "/debug/height", 250);
purple_prefs_add_string(PIDGIN_PREFS_ROOT "/debug/regex", "");
purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/debug/filter", FALSE);
purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/debug/invert", FALSE);
purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/debug/case_insensitive", FALSE);
purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/debug/highlight", FALSE);
pref_callback_id = purple_prefs_connect_callback(NULL,
PIDGIN_PREFS_ROOT "/debug/enabled",
debug_enabled_cb, NULL);
}
void
pidgin_debug_uninit(void)
{
g_clear_handle_id(&pref_callback_id, purple_prefs_disconnect_callback);
g_clear_handle_id(&debug_enabled_timer, g_source_remove);
}
void *
pidgin_debug_get_handle(void) {
static int handle;
return &handle;
}