[gnome-boxes] Add MachineState enum & property to Machine



commit 5f2425dc12a784e844dc50d6bffe0ed818642c67
Author: Alexander Larsson <alexl redhat com>
Date:   Tue May 8 17:03:04 2012 +0200

    Add MachineState enum & property to Machine
    
    Also implement Machine.is_running() using it
    
    This means we can track the state without having to do sync calls.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=675663

 src/libvirt-machine.vala |   46 +++++++++++++++++++++++++++++++---------------
 src/machine.vala         |   20 +++++++++++++++++++-
 src/remote-machine.vala  |    8 +++-----
 3 files changed, 53 insertions(+), 21 deletions(-)
---
diff --git a/src/libvirt-machine.vala b/src/libvirt-machine.vala
index 163b0fa..58d4930 100644
--- a/src/libvirt-machine.vala
+++ b/src/libvirt-machine.vala
@@ -8,14 +8,6 @@ private class Boxes.LibvirtMachine: Boxes.Machine {
     public GVir.Connection connection;
     private string? storage_volume_path;
 
-    private DomainState get_state_sync () {
-        try {
-            return domain.get_info ().state;
-        } catch (GLib.Error error) {
-            return DomainState.NONE;
-        }
-    }
-
     public bool save_on_quit {
         get { return source.get_boolean ("source", "save-on-quit"); }
         set { source.set_boolean ("source", "save-on-quit", value); }
@@ -35,12 +27,40 @@ private class Boxes.LibvirtMachine: Boxes.Machine {
         if (display != null)
             return;
 
-        var state = get_state_sync ();
-        if (state != DomainState.RUNNING) {
+        try {
+            var s = domain.get_info ().state;
+            switch (s) {
+            case DomainState.RUNNING:
+            case DomainState.BLOCKED:
+                state = MachineState.RUNNING;
+                break;
+            case DomainState.PAUSED:
+                state = MachineState.PAUSED;
+                break;
+            case DomainState.SHUTDOWN:
+            case DomainState.SHUTOFF:
+            case DomainState.CRASHED:
+                state = MachineState.STOPPED;
+                break;
+            default:
+            case DomainState.NONE:
+                state = MachineState.UNKNOWN;
+                break;
+            }
+        } catch (GLib.Error error) {
+            state = MachineState.UNKNOWN;
+        }
+
+        domain.started.connect (() => { state = MachineState.RUNNING; });
+        domain.suspended.connect (() => { state = MachineState.PAUSED; });
+        domain.resumed.connect (() => { state = MachineState.RUNNING; });
+        domain.stopped.connect (() => { state = MachineState.STOPPED; });
+
+        if (state != MachineState.RUNNING) {
             if (started_id != 0)
                 return;
 
-            if (state == DomainState.PAUSED) {
+            if (state == MachineState.PAUSED) {
                 started_id = domain.resumed.connect (() => {
                     domain.disconnect (started_id);
                     started_id = 0;
@@ -320,10 +340,6 @@ private class Boxes.LibvirtMachine: Boxes.Machine {
         return domain.get_uuid ();
     }
 
-    public override bool is_running () {
-        return get_state_sync () == DomainState.RUNNING;
-    }
-
     public override async bool take_screenshot () throws GLib.Error {
         var state = DomainState.NONE;
         try {
diff --git a/src/machine.vala b/src/machine.vala
index 2045369..fe2f0ce 100644
--- a/src/machine.vala
+++ b/src/machine.vala
@@ -21,6 +21,21 @@ private abstract class Boxes.Machine: Boxes.CollectionItem, Boxes.IPropertiesPro
     public static const int SCREENSHOT_WIDTH = 180;
     public static const int SCREENSHOT_HEIGHT = 134;
 
+    public enum MachineState {
+        UNKNOWN,
+        STOPPED,
+        RUNNING,
+        PAUSED
+    }
+
+    private MachineState _state;
+    public MachineState state { get { return _state; }
+        protected set {
+            _state = value;
+            debug ("State of '%s' changed to %s", name, state.to_string ());
+        }
+    }
+
     private Display? _display;
     public Display? display {
         get { return _display; }
@@ -122,12 +137,15 @@ private abstract class Boxes.Machine: Boxes.CollectionItem, Boxes.IPropertiesPro
 
     public abstract List<Pair<string, Widget>> get_properties (Boxes.PropertiesPage page);
 
-    public abstract bool is_running ();
     public abstract string get_screenshot_prefix ();
 
     public abstract void connect_display ();
     public abstract void disconnect_display ();
 
+    public bool is_running () {
+        return state == MachineState.RUNNING;
+    }
+
     public async void update_screenshot (int width = SCREENSHOT_WIDTH, int height = SCREENSHOT_HEIGHT) {
         try {
             yield take_screenshot ();
diff --git a/src/remote-machine.vala b/src/remote-machine.vala
index 3757090..2282946 100644
--- a/src/remote-machine.vala
+++ b/src/remote-machine.vala
@@ -6,6 +6,9 @@ private class Boxes.RemoteMachine: Boxes.Machine, Boxes.IPropertiesProvider {
     public RemoteMachine (CollectionSource source, Boxes.App app) {
         base (source, app, source.name);
 
+        // assume the remote is running for now
+        state = MachineState.RUNNING;
+
         config = new DisplayConfig (source);
         source.bind_property ("name", this, "name", BindingFlags.DEFAULT);
         update_screenshot.begin ();
@@ -78,11 +81,6 @@ private class Boxes.RemoteMachine: Boxes.Machine, Boxes.IPropertiesProvider {
         return source.filename;
     }
 
-    public override bool is_running () {
-        // assume the remote is running for now
-        return true;
-    }
-
     public override void delete (bool by_user = true) {
         return_if_fail (by_user);
 



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