mirror of
https://github.com/virt-manager/virt-manager.git
synced 2025-02-25 18:55:27 -06:00
progress: Allow running a thread which calls its own gtk.main()
This commit is contained in:
parent
1879eadf44
commit
320eb9a3e1
@ -27,25 +27,29 @@ import gobject
|
|||||||
|
|
||||||
from virtManager import util
|
from virtManager import util
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
|
||||||
# Displays a progress bar while executing the "callback" method.
|
# Displays a progress bar while executing the "callback" method.
|
||||||
|
|
||||||
class vmmAsyncJob(gobject.GObject):
|
class vmmAsyncJob(gobject.GObject):
|
||||||
# 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)
|
|
||||||
|
|
||||||
def __init__(self, config, callback, args=None,
|
def __init__(self, config, callback, args=None,
|
||||||
text=_("Please wait a few moments..."),
|
text=_("Please wait a few moments..."),
|
||||||
title=_("Operation in progress")):
|
title=_("Operation in progress"),
|
||||||
|
run_main=True):
|
||||||
self.__gobject_init__()
|
self.__gobject_init__()
|
||||||
self.config = config
|
self.config = config
|
||||||
|
self.run_main = bool(run_main)
|
||||||
|
|
||||||
self.window = gtk.glade.XML(config.get_glade_dir() + "/vmm-progress.glade", "vmm-progress", domain="virt-manager")
|
self.window = gtk.glade.XML(config.get_glade_dir() + \
|
||||||
|
"/vmm-progress.glade",
|
||||||
|
"vmm-progress", domain="virt-manager")
|
||||||
self.window.get_widget("pbar-text").set_text(text)
|
self.window.get_widget("pbar-text").set_text(text)
|
||||||
|
|
||||||
self.topwin = self.window.get_widget("vmm-progress")
|
self.topwin = self.window.get_widget("vmm-progress")
|
||||||
@ -58,7 +62,7 @@ class vmmAsyncJob(gobject.GObject):
|
|||||||
self.pbar = self.window.get_widget("pbar")
|
self.pbar = self.window.get_widget("pbar")
|
||||||
|
|
||||||
args.append(self)
|
args.append(self)
|
||||||
self.bg_thread = vmmAsyncJob.asyncJobWorker(callback, args)
|
self.bg_thread = asyncJobWorker(callback, args)
|
||||||
self.bg_thread.setDaemon(True)
|
self.bg_thread.setDaemon(True)
|
||||||
self.is_pulsing = True
|
self.is_pulsing = True
|
||||||
|
|
||||||
@ -66,8 +70,13 @@ class vmmAsyncJob(gobject.GObject):
|
|||||||
timer = util.safe_timeout_add(100, self.exit_if_necessary)
|
timer = util.safe_timeout_add(100, self.exit_if_necessary)
|
||||||
self.topwin.present()
|
self.topwin.present()
|
||||||
self.topwin.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH))
|
self.topwin.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH))
|
||||||
self.bg_thread.start()
|
|
||||||
gtk.main()
|
if self.run_main:
|
||||||
|
self.bg_thread.start()
|
||||||
|
gtk.main()
|
||||||
|
else:
|
||||||
|
self.bg_thread.run()
|
||||||
|
|
||||||
gobject.source_remove(timer)
|
gobject.source_remove(timer)
|
||||||
timer = 0
|
timer = 0
|
||||||
|
|
||||||
@ -135,12 +144,15 @@ class vmmAsyncJob(gobject.GObject):
|
|||||||
return self._error_info
|
return self._error_info
|
||||||
|
|
||||||
def exit_if_necessary(self, force_exit=False):
|
def exit_if_necessary(self, force_exit=False):
|
||||||
if self.bg_thread.isAlive() and not force_exit:
|
thread_active = (self.bg_thread.isAlive() or not self.run_main)
|
||||||
|
|
||||||
|
if thread_active and not force_exit:
|
||||||
if (self.is_pulsing):
|
if (self.is_pulsing):
|
||||||
# Don't call pulse_pbar: this function is thread wrapped
|
# Don't call pulse_pbar: this function is thread wrapped
|
||||||
self.pbar.pulse()
|
self.pbar.pulse()
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
gtk.main_quit()
|
if self.run_main:
|
||||||
|
gtk.main_quit()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user