[gnome-boxes] display-config: learn to save various access information



commit c6576d7b6f0606d4bad73d4be9072613cc30c6d6
Author: Marc-Andrà Lureau <marcandre lureau gmail com>
Date:   Fri Aug 3 04:10:30 2012 +0200

    display-config: learn to save various access information
    
    We will save a few useful informations for sorting / statistics:
    - time spent on a box
    - number of time using a box
    - last time access
    - first time access
    
    I wanted to implement the base class as a mixin, but vala/gobject is a
    bit too limited for that sadly. Not a big deal though.
    
    In the future, I would like to extend this approach to support
    Zeitgeist informations too. Also, it might be interesting that we save
    these information in libvirt XML too, although it's perhpas preferable
    to have a Boxes level of thing, that can work with direct or ovirt
    connections too.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=681104

 src/display.vala       |   92 +++++++++++++++++++++++++++++++++++++----------
 src/spice-display.vala |    4 +-
 src/vnc-display.vala   |    2 +-
 3 files changed, 75 insertions(+), 23 deletions(-)
---
diff --git a/src/display.vala b/src/display.vala
index a60989d..9f707c5 100644
--- a/src/display.vala
+++ b/src/display.vala
@@ -1,38 +1,61 @@
 // This file is part of GNOME Boxes. License: LGPLv2+
 using Gtk;
 
-private abstract class Boxes.Display: GLib.Object, Boxes.IPropertiesProvider {
+// too bad we can't make it just a mixin
+private abstract class Boxes.DisplayProperties: GLib.Object, Boxes.IPropertiesProvider {
     protected struct SavedProperty {
         string name;
         Value default_value;
     }
 
-    public abstract string protocol { get; }
-    public abstract string uri { owned get; }
+    public abstract List<Boxes.Property> get_properties (Boxes.PropertiesPage page);
 
-    public bool can_grab_mouse { get; protected set; }
-    public bool mouse_grabbed { get; protected set; }
-    public bool need_password { get; protected set; }
-    public bool need_username { get; protected set; }
-    public string? password { get; set; }
-    public string? username { get; set; }
+    private int64 started_time;
+    protected void access_start () {
+        if (started_time != 0)
+            return;
 
-    public signal void show (int display_id);
-    public signal void hide (int display_id);
-    public signal void disconnected ();
+        started_time = get_monotonic_time ();
+        access_last_time = get_real_time ();
+        access_ntimes += 1;
 
-    public abstract Gtk.Widget get_display (int n) throws Boxes.Error;
-    public abstract Gdk.Pixbuf? get_pixbuf (int n) throws Boxes.Error;
-    public abstract void set_enable_inputs (Gtk.Widget widget, bool enable);
+        if (access_first_time == 0)
+            access_first_time = access_last_time;
+    }
 
-    public abstract void connect_it ();
-    public abstract void disconnect_it ();
+    protected void access_finish () {
+        if (started_time == 0)
+            return;
 
-    public abstract List<Boxes.Property> get_properties (Boxes.PropertiesPage page);
+        var duration = get_monotonic_time () - started_time;
+        duration /= 1000000; // convert to seconds
+        access_total_time += duration;
+
+        started_time = 0;
+    }
+
+
+    public int64 access_last_time { set; get; }
+    public int64 access_first_time { set; get; }
+    public int64 access_total_time { set; get; } // in seconds
+    public int64 access_ntimes { set; get; }
+    private SavedProperty[] access_saved_properties;
 
-    protected HashTable<int, Gtk.Widget?> displays;
     construct {
-        displays = new HashTable<int, Gtk.Widget> (direct_hash, direct_equal);
+        access_saved_properties = {
+            SavedProperty () { name = "access-last-time", default_value = (int64)(-1) },
+            SavedProperty () { name = "access-first-time", default_value = (int64)(-1) },
+            SavedProperty () { name = "access-total-time", default_value = (int64)(-1) },
+            SavedProperty () { name = "access-ntimes", default_value = (uint64)0 }
+        };
+
+        this.notify["config"].connect (() => {
+            sync_config_with_display (this, access_saved_properties);
+        });
+    }
+
+    ~DisplayProperties () {
+        access_finish ();
     }
 
     public DisplayConfig? config { get; set; }
@@ -53,3 +76,32 @@ private abstract class Boxes.Display: GLib.Object, Boxes.IPropertiesProvider {
         });
     }
 }
+
+private abstract class Boxes.Display: Boxes.DisplayProperties {
+    public abstract string protocol { get; }
+    public abstract string uri { owned get; }
+
+    public bool can_grab_mouse { get; protected set; }
+    public bool mouse_grabbed { get; protected set; }
+    public bool need_password { get; protected set; }
+    public bool need_username { get; protected set; }
+    public string? password { get; set; }
+    public string? username { get; set; }
+
+    public signal void show (int display_id);
+    public signal void hide (int display_id);
+    public signal void disconnected ();
+
+    public abstract Gtk.Widget get_display (int n) throws Boxes.Error;
+    public abstract Gdk.Pixbuf? get_pixbuf (int n) throws Boxes.Error;
+    public abstract void set_enable_inputs (Gtk.Widget widget, bool enable);
+
+    public abstract void connect_it ();
+    public abstract void disconnect_it ();
+
+    protected HashTable<int, Gtk.Widget?> displays;
+
+    construct {
+        displays = new HashTable<int, Gtk.Widget> (direct_hash, direct_equal);
+    }
+}
diff --git a/src/spice-display.vala b/src/spice-display.vala
index a38a138..f19912b 100644
--- a/src/spice-display.vala
+++ b/src/spice-display.vala
@@ -11,8 +11,8 @@ private class Boxes.SpiceDisplay: Boxes.Display, Boxes.IPropertiesProvider {
     private unowned Spice.Audio audio;
     private ulong channel_new_id;
     private ulong channel_destroy_id;
-    private Display.SavedProperty[] display_saved_properties;
-    private Display.SavedProperty[] gtk_session_saved_properties;
+    private DisplayProperties.SavedProperty[] display_saved_properties;
+    private DisplayProperties.SavedProperty[] gtk_session_saved_properties;
 
     public bool resize_guest { get; set; }
     private void ui_state_changed () {
diff --git a/src/vnc-display.vala b/src/vnc-display.vala
index ea3ab64..b9bb48d 100644
--- a/src/vnc-display.vala
+++ b/src/vnc-display.vala
@@ -9,7 +9,7 @@ private class Boxes.VncDisplay: Boxes.Display {
     private string host;
     private int port;
     private Gtk.Window window;
-    private Display.SavedProperty[] saved_properties;
+    private DisplayProperties.SavedProperty[] saved_properties;
 
     construct {
         saved_properties = {



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