[gnome-boxes] libvirt: provide a menu entry to force shutdown



commit d7c57d82d47c5bdec0a675770c22bcfc177aebed
Author: Marc-Andrà Lureau <marcandre lureau gmail com>
Date:   Sun Jun 10 19:30:26 2012 +0200

    libvirt: provide a menu entry to force shutdown
    
    I would prefer to only show the force shutdown entry when the Box
    can be shutdown, via libvirt. But it doesn't seem to be easy to
    remove or hide an item from the global menu. Also, I wonder if
    from a UI POV this is very desirable, since it may or may not be
    in the menu.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=677690

 src/app.vala             |    7 +++++++
 src/libvirt-machine.vala |   26 ++++++++++++++++++++------
 2 files changed, 27 insertions(+), 6 deletions(-)
---
diff --git a/src/app.vala b/src/app.vala
index 012e44c..b7913b2 100644
--- a/src/app.vala
+++ b/src/app.vala
@@ -44,6 +44,7 @@ private class Boxes.App: Boxes.UI {
     public Collection collection;
     public GLib.SimpleAction action_properties;
     public GLib.SimpleAction action_fullscreen;
+    public GLib.SimpleAction action_shutdown;
 
     public signal void ready (bool first_time);
     public signal void item_selected (CollectionItem item);
@@ -81,6 +82,10 @@ private class Boxes.App: Boxes.UI {
         action_properties.activate.connect (() => { ui_state = UIState.PROPERTIES; });
         application.add_action (action_properties);
 
+        action_shutdown = new GLib.SimpleAction ("display.shutdown", null);
+        action_shutdown.activate.connect (() => { (current_item as LibvirtMachine).force_shutdown (); });
+        application.add_action (action_shutdown);
+
         action = new GLib.SimpleAction ("about", null);
         action.activate.connect (() => {
             string[] authors = {
@@ -113,6 +118,7 @@ private class Boxes.App: Boxes.UI {
             var display_section = new GLib.Menu ();
             display_section.append (_("Properties"), "app.display.properties");
             display_section.append (_("Fullscreen"), "app.display.fullscreen");
+            display_section.append (_("Force shutdown"), "app.display.shutdown");
             menu.append_section (null, display_section);
 
             menu.append (_("About Boxes"), "app.about");
@@ -479,6 +485,7 @@ private class Boxes.App: Boxes.UI {
     public override void ui_state_changed () {
         action_fullscreen.set_enabled (ui_state == UIState.DISPLAY);
         action_properties.set_enabled (ui_state == UIState.DISPLAY);
+        action_shutdown.set_enabled (ui_state == UIState.DISPLAY && current_item is LibvirtMachine);
 
         foreach (var ui in new Boxes.UI[] { sidebar, topbar, view, wizard, properties }) {
             ui.ui_state = ui_state;
diff --git a/src/libvirt-machine.vala b/src/libvirt-machine.vala
index 82bf3ac..7599d6e 100644
--- a/src/libvirt-machine.vala
+++ b/src/libvirt-machine.vala
@@ -375,12 +375,7 @@ private class Boxes.LibvirtMachine: Boxes.Machine {
         set_stats_enable (false);
 
         if (by_user) {
-            try {
-                if (is_running ())
-                    domain.stop (0);
-            } catch (GLib.Error err) {
-                // ignore stop error
-            }
+            force_shutdown (false);
 
             try {
                 if (connection == App.app.default_connection) {
@@ -399,6 +394,25 @@ private class Boxes.LibvirtMachine: Boxes.Machine {
         (save_on_quit) ? yield domain.save_async (0, null) : domain.suspend ();
     }
 
+    public void force_shutdown (bool confirm = true) {
+        if (confirm) {
+            var dialog = new Gtk.MessageDialog (App.app.window, Gtk.DialogFlags.DESTROY_WITH_PARENT, Gtk.MessageType.QUESTION, Gtk.ButtonsType.OK_CANCEL, _("When you force shutdown, the box may lose data."));
+            var response = dialog.run ();
+            dialog.destroy ();
+
+            if (response != Gtk.ResponseType.OK)
+                return;
+        }
+
+        debug ("Force shutdown '%s'..", name);
+        try {
+            if (is_running ())
+                domain.stop (0);
+        } catch (GLib.Error error) {
+            warning ("Failed to shutdown '%s': %s", domain.get_name (), error.message);
+        }
+    }
+
     private GVir.DomainDisk? get_domain_disk () throws GLib.Error {
         var disk = null as GVir.DomainDisk;
 



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