[geary/wip/geary-inspector: 27/38] Add UI and implement saving details and logs to file



commit 8dc54d38c096cc04eceeed7f4dbedd6f39b3bd2a
Author: Michael Gratton <mike vee net>
Date:   Sat Apr 6 18:49:10 2019 +1100

    Add UI and implement saving details and logs to file

 src/client/components/components-inspector.vala | 63 +++++++++++++++++++++++++
 ui/components-inspector.ui                      | 24 ++++++++--
 2 files changed, 83 insertions(+), 4 deletions(-)
---
diff --git a/src/client/components/components-inspector.vala b/src/client/components/components-inspector.vala
index c25f8f83..dc4bf0d0 100644
--- a/src/client/components/components-inspector.vala
+++ b/src/client/components/components-inspector.vala
@@ -87,6 +87,41 @@ public class Components.Inspector : Gtk.Window {
         this.details = details.str;
     }
 
+    private async void save(string path,
+                            GLib.Cancellable? cancellable)
+        throws GLib.Error {
+        GLib.File dest = GLib.File.new_for_path(path);
+        GLib.FileIOStream dest_io = yield dest.create_readwrite_async(
+            GLib.FileCreateFlags.NONE,
+            GLib.Priority.DEFAULT,
+            cancellable
+        );
+        GLib.DataOutputStream out = new GLib.DataOutputStream(
+            new GLib.BufferedOutputStream(dest_io.get_output_stream())
+        );
+
+        out.put_string(this.details);
+        out.put_byte('\n');
+        out.put_byte('\n');
+
+        Gtk.TreeModel model = this.logs_view.model;
+        Gtk.TreeIter? iter;
+        bool valid = model.get_iter_first(out iter);
+        while (valid && !cancellable.is_cancelled()) {
+            Value value;
+            model.get_value(iter, COL_MESSAGE, out value);
+            string? message = (string) value;
+            if (message != null) {
+                out.put_string(message);
+                out.put_byte('\n');
+            }
+            valid = model.iter_next(ref iter);
+        }
+
+        yield out.close_async();
+        yield dest_io.close_async();
+    }
+
     private void update_ui() {
         bool logs_visible = this.stack.visible_child == this.logs_pane;
         uint logs_selected = this.logs_view.get_selection().count_selected_rows();
@@ -129,6 +164,34 @@ public class Components.Inspector : Gtk.Window {
         }
     }
 
+    [GtkCallback]
+    private void on_save_as_clicked() {
+        Gtk.FileChooserNative chooser = new Gtk.FileChooserNative(
+            _("Save As"),
+            this,
+            Gtk.FileChooserAction.SAVE,
+            _("Save As"),
+            _("Cancel")
+        );
+        chooser.set_current_name(
+            new GLib.DateTime.now_local().format("Geary Inspector - %F %T.txt")
+        );
+
+        if (chooser.run() == Gtk.ResponseType.ACCEPT) {
+            this.save.begin(
+                chooser.get_filename(),
+                null,
+                (obj, res) => {
+                    try {
+                        this.save.end(res);
+                    } catch (GLib.Error err) {
+                        warning("Failed to save inspector data: %s", err.message);
+                    }
+                }
+            );
+        }
+    }
+
     [GtkCallback]
     private void on_search_clicked() {
         this.search_bar.set_search_mode(!this.search_bar.get_search_mode());
diff --git a/ui/components-inspector.ui b/ui/components-inspector.ui
index 098c4d7b..95824f0e 100644
--- a/ui/components-inspector.ui
+++ b/ui/components-inspector.ui
@@ -38,9 +38,6 @@
               </object>
             </child>
           </object>
-          <packing>
-            <property name="position">1</property>
-          </packing>
         </child>
         <child type="title">
           <object class="GtkStackSwitcher">
@@ -49,6 +46,25 @@
             <property name="stack">stack</property>
           </object>
         </child>
+        <child>
+          <object class="GtkButton" id="save_as_button">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="receives_default">True</property>
+            <signal name="clicked" handler="on_save_as_clicked" swapped="no"/>
+            <child>
+              <object class="GtkImage">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="icon_name">document-save-as-symbolic</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="pack_type">end</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
         <child>
           <object class="GtkButton" id="copy_button">
             <property name="visible">True</property>
@@ -66,7 +82,7 @@
           </object>
           <packing>
             <property name="pack_type">end</property>
-            <property name="position">1</property>
+            <property name="position">2</property>
           </packing>
         </child>
       </object>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]