2006-07-20 10:16:07 -05:00
|
|
|
#
|
|
|
|
# Copyright (C) 2006 Red Hat, Inc.
|
|
|
|
# Copyright (C) 2006 Hugh O. Brock <hbrock@redhat.com>
|
|
|
|
#
|
|
|
|
# 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
|
2006-11-06 14:00:39 -06:00
|
|
|
import gtk.gdk
|
2006-07-20 10:16:07 -05:00
|
|
|
import gtk.glade
|
|
|
|
import gobject
|
|
|
|
|
|
|
|
# Displays a progress bar while executing the "callback" method.
|
|
|
|
|
2006-07-24 12:50:11 -05:00
|
|
|
class vmmAsyncJob(gobject.GObject):
|
2006-07-20 11:52:00 -05:00
|
|
|
# This thin wrapper only exists so we can put debugging
|
|
|
|
# code in the run() method every now & then
|
|
|
|
class asyncJobWorker(threading.Thread):
|
|
|
|
def __init__(self, callback, args):
|
|
|
|
threading.Thread.__init__(self, target=callback, args=args)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
threading.Thread.run(self)
|
|
|
|
|
2007-02-01 13:16:44 -06:00
|
|
|
def __init__(self, config, callback, args=None, text=_("Please wait a few moments..."), title=_("Operation in progress")):
|
2006-07-20 10:16:07 -05:00
|
|
|
self.__gobject_init__()
|
|
|
|
self.config = config
|
2007-02-01 13:16:44 -06:00
|
|
|
|
2007-02-15 14:35:37 -06:00
|
|
|
self.window = gtk.glade.XML(config.get_glade_dir() + "/vmm-progress.glade", "vmm-progress", domain="virt-manager")
|
2007-02-01 13:16:44 -06:00
|
|
|
self.window.get_widget("pbar-text").set_text(text)
|
|
|
|
|
|
|
|
self.topwin = self.window.get_widget("vmm-progress")
|
|
|
|
self.topwin.set_title(title)
|
|
|
|
self.topwin.hide()
|
|
|
|
|
|
|
|
self.stage = self.window.get_widget("pbar-stage")
|
|
|
|
self.pbar = self.window.get_widget("pbar")
|
|
|
|
|
2007-01-12 14:39:18 -06:00
|
|
|
args.append(self)
|
2006-07-24 12:50:11 -05:00
|
|
|
self.bg_thread = vmmAsyncJob.asyncJobWorker(callback, args)
|
2007-01-12 14:39:18 -06:00
|
|
|
self.is_pulsing = True
|
2006-07-20 10:16:07 -05:00
|
|
|
|
|
|
|
def run(self):
|
2007-01-10 15:11:57 -06:00
|
|
|
self.timer = gobject.timeout_add (100, self.exit_if_necessary)
|
2007-02-01 13:16:44 -06:00
|
|
|
self.topwin.present()
|
|
|
|
self.topwin.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH))
|
2006-07-20 10:16:07 -05:00
|
|
|
self.bg_thread.start()
|
2007-03-09 15:22:43 -06:00
|
|
|
gtk.gdk.threads_enter()
|
2006-07-20 10:16:07 -05:00
|
|
|
gtk.main()
|
2007-03-09 15:22:43 -06:00
|
|
|
gtk.gdk.threads_leave()
|
2006-07-20 10:16:07 -05:00
|
|
|
gobject.source_remove(self.timer)
|
|
|
|
self.timer = 0
|
2007-02-01 13:16:44 -06:00
|
|
|
self.topwin.destroy()
|
2006-07-20 10:16:07 -05:00
|
|
|
|
2007-02-01 13:16:44 -06:00
|
|
|
def pulse_pbar(self, progress="", stage=None):
|
2007-01-10 15:11:57 -06:00
|
|
|
self.is_pulsing = True
|
2007-02-01 13:16:44 -06:00
|
|
|
self.pbar.set_text(progress)
|
|
|
|
if stage is not None:
|
|
|
|
self.stage.set_text(stage)
|
|
|
|
else:
|
|
|
|
self.stage.set_text(_("Processing..."))
|
2007-01-10 15:11:57 -06:00
|
|
|
|
2007-02-01 13:16:44 -06:00
|
|
|
def set_pbar_fraction(self, frac, progress, stage=None):
|
2007-01-10 15:11:57 -06:00
|
|
|
# callback for progress meter when file size is known
|
|
|
|
self.is_pulsing=False
|
2007-02-01 13:16:44 -06:00
|
|
|
if stage is not None:
|
|
|
|
self.stage.set_text(stage)
|
|
|
|
else:
|
|
|
|
self.stage.set_text(_("Processing..."))
|
|
|
|
self.pbar.set_text(progress)
|
2007-01-10 15:11:57 -06:00
|
|
|
self.pbar.set_fraction(frac)
|
|
|
|
|
2007-02-01 13:16:44 -06:00
|
|
|
def set_pbar_done(self, progress, stage=None):
|
2007-01-10 15:11:57 -06:00
|
|
|
#callback for progress meter when progress is done
|
|
|
|
self.is_pulsing=False
|
2007-02-01 13:16:44 -06:00
|
|
|
if stage is not None:
|
|
|
|
self.stage.set_text(stage)
|
|
|
|
else:
|
|
|
|
self.stage.set_text(_("Completed"))
|
|
|
|
self.pbar.set_text(progress)
|
2007-01-10 15:11:57 -06:00
|
|
|
self.pbar.set_fraction(1)
|
2007-02-01 13:16:44 -06:00
|
|
|
|
2007-01-10 15:11:57 -06:00
|
|
|
def exit_if_necessary(self):
|
2007-03-09 15:22:43 -06:00
|
|
|
gtk.gdk.threads_enter()
|
|
|
|
try:
|
|
|
|
self._exit_if_necessary(self)
|
|
|
|
finally:
|
|
|
|
gtk.gdk.threads_leave()
|
|
|
|
|
|
|
|
def _exit_if_necessary(self):
|
2007-02-01 13:16:44 -06:00
|
|
|
if self.bg_thread.isAlive():
|
2007-01-10 15:11:57 -06:00
|
|
|
if(self.is_pulsing):
|
|
|
|
self.pbar.pulse()
|
2006-07-20 11:52:00 -05:00
|
|
|
return True
|
2006-07-20 10:16:07 -05:00
|
|
|
else:
|
|
|
|
gtk.main_quit()
|
2006-07-20 11:52:00 -05:00
|
|
|
return False
|
2007-02-01 13:16:44 -06:00
|
|
|
|