From 6dc8b278f0c71b8fddaf3bf09f93980caf808af0 Mon Sep 17 00:00:00 2001 From: Michael Gratton Date: Sun, 7 Apr 2019 11:32:38 +1000 Subject: [PATCH] Append new log records to the inspector and scroll to them --- .../components/components-inspector.vala | 58 +++++++++++++++++-- src/engine/imap-db/imap-db-attachment.vala | 12 ++-- ui/components-inspector.ui | 8 ++- 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/src/client/components/components-inspector.vala b/src/client/components/components-inspector.vala index 321176be..bee621b3 100644 --- a/src/client/components/components-inspector.vala +++ b/src/client/components/components-inspector.vala @@ -36,6 +36,9 @@ public class Components.Inspector : Gtk.Window { [GtkChild] private Gtk.SearchEntry search_entry; + [GtkChild] + private Gtk.ScrolledWindow logs_scroller; + [GtkChild] private Gtk.TreeView logs_view; @@ -58,6 +61,8 @@ public class Components.Inspector : Gtk.Window { private string details; + private bool autoscroll = true; + public Inspector(GearyApplication app) { this.title = this.header_bar.title = _("Inspector"); @@ -81,15 +86,14 @@ public class Components.Inspector : Gtk.Window { } this.details = details.str; - // Log a marker for when the inspector was opened - debug("---- 8< ---- %s ---- 8< ----", this.header_bar.title); + enable_log_updates(true); Gtk.ListStore logs_store = this.logs_store; Geary.Logging.Record? logs = Geary.Logging.get_logs(); int index = 0; while (logs != null) { Gtk.TreeIter iter; - logs_store.append(out iter); + logs_store.insert(out iter, index++); logs_store.set_value(iter, COL_MESSAGE, logs.format()); logs = logs.get_next(); } @@ -117,6 +121,13 @@ public class Components.Inspector : Gtk.Window { this.logs_view.set_model(this.logs_filter); } + public override void destroy() { + // Don't use enable_log_updates() here because we don't want a + // marker logged. + Geary.Logging.set_log_listener(null); + base.destroy(); + } + public override bool key_press_event(Gdk.EventKey event) { bool ret = this.search_bar.handle_event(event); if (ret == Gdk.EVENT_PROPAGATE) { @@ -125,6 +136,16 @@ public class Components.Inspector : Gtk.Window { return ret; } + private void enable_log_updates(bool enabled) { + // Log a marker it indicate when it was toggled + debug("---- 8< ---- %s ---- 8< ----", this.header_bar.title); + if (enabled) { + Geary.Logging.set_log_listener(this.on_log_record); + } else { + Geary.Logging.set_log_listener(null); + } + } + private async void save(string path, GLib.Cancellable? cancellable) throws GLib.Error { @@ -167,11 +188,22 @@ public class Components.Inspector : Gtk.Window { this.search_button.set_visible(logs_visible); } + private void update_scrollbar() { + Gtk.Adjustment adj = this.logs_scroller.get_vadjustment(); + adj.set_value(adj.upper - adj.page_size); + } + private void update_logs_filter() { this.logs_filter_terms = this.search_entry.text.split(" "); this.logs_filter.refilter(); } + private void append_record(Geary.Logging.Record record) { + Gtk.TreeIter inserted_iter; + this.logs_store.append(out inserted_iter); + this.logs_store.set_value(inserted_iter, COL_MESSAGE, record.format()); + } + [GtkCallback] private void on_visible_child_changed() { update_ui(); @@ -240,6 +272,13 @@ public class Components.Inspector : Gtk.Window { this.search_bar.set_search_mode(!this.search_bar.get_search_mode()); } + [GtkCallback] + private void on_logs_size_allocate() { + if (this.autoscroll) { + update_scrollbar(); + } + } + [GtkCallback] private void on_logs_selection_changed() { update_ui(); @@ -250,9 +289,16 @@ public class Components.Inspector : Gtk.Window { update_logs_filter(); } - [GtkCallback] - private void on_destroy() { - destroy(); + private void on_log_record(Geary.Logging.Record record) { + if (GLib.MainContext.default() == + GLib.MainContext.get_thread_default()) { + append_record(record); + } else { + GLib.Idle.add(() => { + append_record(record); + return GLib.Source.REMOVE; + }); + } } } diff --git a/src/engine/imap-db/imap-db-attachment.vala b/src/engine/imap-db/imap-db-attachment.vala index f840aeb8..60c7028c 100644 --- a/src/engine/imap-db/imap-db-attachment.vala +++ b/src/engine/imap-db/imap-db-attachment.vala @@ -95,9 +95,9 @@ private class Geary.ImapDB.Attachment : Geary.Attachment { save_file(part, attachments_dir, cancellable); update_db(cx, cancellable); } catch (Error err) { - // Don't honour the cancellable here, we need to delete - // it. - this.delete(cx, cancellable); + // Don't honour the cancellable here, it needs to be + // deleted + this.delete(cx, null); throw err; } } @@ -161,10 +161,8 @@ private class Geary.ImapDB.Attachment : Geary.Attachment { // create directory, but don't throw exception if already exists try { target.get_parent().make_directory_with_parents(cancellable); - } catch (IOError ioe) { - // fall through if already exists - if (!(ioe is IOError.EXISTS)) - throw ioe; + } catch (IOError.EXISTS err) { + // All good } // Delete any existing file now since we might not be creating diff --git a/ui/components-inspector.ui b/ui/components-inspector.ui index d62575b1..732b2845 100644 --- a/ui/components-inspector.ui +++ b/ui/components-inspector.ui @@ -18,7 +18,6 @@ False 750 500 - True @@ -50,6 +49,8 @@ True True + Save logs and details to a file True @@ -69,13 +70,13 @@ True True + Copy selected rows True True False - Copy selected rows edit-copy-symbolic @@ -119,7 +120,7 @@ - + True True True @@ -132,6 +133,7 @@ False False False + multiple