From a5c0597604331f599710536db47e988b07c0bdfd Mon Sep 17 00:00:00 2001 From: "Hugh O. Brock" Date: Thu, 20 Jul 2006 11:16:07 -0400 Subject: [PATCH] Make the progress bar threaded so that it will bounce back and forth in the background (still doesn't work) and abstract the progress bar functionality --- src/virt-manager.glade | 36 +++++++++++++++++++---- src/virt-manager.py.in | 1 + src/virtManager/asyncjob.py | 54 +++++++++++++++++++++++++++++++++++ src/virtManager/connection.py | 2 ++ src/virtManager/engine.py | 24 ++++------------ src/virtManager/manager.py | 23 +++++++++++++++ 6 files changed, 116 insertions(+), 24 deletions(-) create mode 100644 src/virtManager/asyncjob.py diff --git a/src/virt-manager.glade b/src/virt-manager.glade index 0c7070b06..ec0a31d7f 100644 --- a/src/virt-manager.glade +++ b/src/virt-manager.glade @@ -49,9 +49,10 @@ New machine... True + - + True gtk-new 1 @@ -64,6 +65,29 @@ + + + True + Restore a saved machine from a filesystem image + Restore saved machine... + True + + + + + + True + gtk-open + 1 + 0.5 + 0.5 + 0 + 0 + + + + + True @@ -78,7 +102,7 @@ - + True gtk-connect 1 @@ -136,7 +160,7 @@ - + True gtk-properties 1 @@ -157,7 +181,7 @@ - + True gtk-delete 1 @@ -3265,7 +3289,7 @@ Inactive virtual machines - + True Saving VM Image GTK_WINDOW_TOPLEVEL @@ -3290,7 +3314,7 @@ Inactive virtual machines True - Please wait while the VM image saves... + Please wait... False False GTK_JUSTIFY_LEFT diff --git a/src/virt-manager.py.in b/src/virt-manager.py.in index 8b4dc98d1..077ceb774 100755 --- a/src/virt-manager.py.in +++ b/src/virt-manager.py.in @@ -20,6 +20,7 @@ # import gtk +gtk.threads_init() import dbus import dbus.glib diff --git a/src/virtManager/asyncjob.py b/src/virtManager/asyncjob.py new file mode 100644 index 000000000..4975c5334 --- /dev/null +++ b/src/virtManager/asyncjob.py @@ -0,0 +1,54 @@ +# +# Copyright (C) 2006 Red Hat, Inc. +# Copyright (C) 2006 Hugh O. Brock +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +# + +import threading +import gtk +import gtk.glade +import gobject + +# Displays a progress bar while executing the "callback" method. + +class asyncJob(gobject.GObject): + def __init__(self, config, callback, args=None, title="Progress"): + self.__gobject_init__() + self.config = config + self.callback = callback + self.args = args + self.pbar_glade = gtk.glade.XML(self.config.get_glade_file(), "vmm-progress") + self.pbar_win = self.pbar_glade.get_widget("vmm-progress") + self.pbar = self.pbar_glade.get_widget("pbar") + self.pbar_win.set_title(title) + self.pbar_win.hide() + self.bg_thread = threading.Thread(target=self.callback, args=self.args) + + def run(self): + self.timer = gobject.timeout_add (100, self.pulse_pbar) + self.pbar_win.present() + self.bg_thread.start() + gtk.main() + gobject.source_remove(self.timer) + self.timer = 0 + self.pbar_win.destroy() + + def pulse_pbar(self): + print "About to frobnicate the pbar" + if(self.bg_thread.isAlive()): + self.pbar.pulse() + else: + gtk.main_quit() diff --git a/src/virtManager/connection.py b/src/virtManager/connection.py index 223da795c..d3d1443c3 100644 --- a/src/virtManager/connection.py +++ b/src/virtManager/connection.py @@ -85,6 +85,8 @@ class vmmConnection(gobject.GObject): def host_maximum_processor_count(self): return self.hostinfo[4] * self.hostinfo[5] * self.hostinfo[6] * self.hostinfo[7] + def restore(self, frm): + self.vmm.restore(frm) def tick(self): if self.vmm == None: diff --git a/src/virtManager/engine.py b/src/virtManager/engine.py index 361e651c6..51edc474c 100644 --- a/src/virtManager/engine.py +++ b/src/virtManager/engine.py @@ -28,6 +28,7 @@ from virtManager.preferences import vmmPreferences from virtManager.manager import vmmManager from virtManager.details import vmmDetails from virtManager.console import vmmConsole +from virtManager.asyncjob import asyncJob class vmmEngine: def __init__(self, config): @@ -216,24 +217,11 @@ class vmmEngine: gtk.STOCK_SAVE, gtk.RESPONSE_ACCEPT), None) self.fcdialog.set_do_overwrite_confirmation(True) - # also set up the progress bar now - self.pbar_glade = gtk.glade.XML(config.get_glade_file(), "vmm-save-progress") - self.pbar_win = self.pbar_glade.get_widget("vmm-save-progress") - self.pbar_win.hide() - response = self.fcdialog.run() self.fcdialog.hide() if(response == gtk.RESPONSE_ACCEPT): - uri_to_save = self.fcdialog.get_filename() - # show a lovely bouncing progress bar until the vm actually saves - self.timer = gobject.timeout_add (100, - self.pbar_glade.get_widget("pbar").pulse) - self.pbar_win.present() - - # actually save the vm - vm.save( uri_to_save ) - gobject.source_remove(self.timer) - self.timer = 0 - self.pbar_win.hide() - self.fcdialog.destroy() - self.pbar_win.destroy() + file_to_save = self.fcdialog.get_filename() + progWin = asyncJob(self.config, vm.save, + [file_to_save], "Saving Virtual Machine") + progWin.run() + self.fcdialog.destroy() diff --git a/src/virtManager/manager.py b/src/virtManager/manager.py index e57f7436b..e8eaa4b5c 100644 --- a/src/virtManager/manager.py +++ b/src/virtManager/manager.py @@ -20,9 +20,12 @@ import gobject import gtk import gtk.glade +import threading import sparkline +from virtManager.asyncjob import asyncJob + VMLIST_SORT_ID = 1 VMLIST_SORT_NAME = 2 VMLIST_SORT_CPU_USAGE = 3 @@ -104,6 +107,7 @@ class vmmManager(gobject.GObject): "on_menu_file_open_connection_activate": self.open_connection, "on_menu_file_quit_activate": self.exit_app, "on_menu_file_close_activate": self.close, + "on_menu_restore_saved_activate": self.restore_saved, "on_vmm_close_clicked": self.close, "on_vm_details_clicked": self.show_vm_details, "on_vm_open_clicked": self.open_vm_console, @@ -138,6 +142,25 @@ class vmmManager(gobject.GObject): def open_connection(self, src=None): self.emit("action-show-connect") + def restore_saved(self, src=None): + # get filename + self.fcdialog = gtk.FileChooserDialog("Restore Virtual Machine", + self.window.get_widget("vmm-manager"), + gtk.FILE_CHOOSER_ACTION_OPEN, + (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, + gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT), + None) + # pop up progress dialog + response = self.fcdialog.run() + self.fcdialog.hide() + if(response == gtk.RESPONSE_ACCEPT): + file_to_load = self.fcdialog.get_filename() + progWin = asyncJob(self.config, self.connection.restore, + [file_to_load], "Restoring Virtual Machine") + progWin.run() + + self.fcdialog.destroy() + def vm_added(self, connection, uri, vmuuid): vmlist = self.window.get_widget("vm-list") model = vmlist.get_model()