From 28e267794474a1ccc6c994a358a41dca0bbbfbb5 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Thu, 14 Apr 2011 16:43:17 -0400 Subject: [PATCH] Break out helper for prompting user before performing an op --- src/virtManager/details.py | 15 ++----- src/virtManager/engine.py | 88 +++++++++++--------------------------- src/virtManager/host.py | 36 +++++----------- src/virtManager/util.py | 19 ++++++++ 4 files changed, 59 insertions(+), 99 deletions(-) diff --git a/src/virtManager/details.py b/src/virtManager/details.py index 6bdd7e9b7..3ee53fd6d 100644 --- a/src/virtManager/details.py +++ b/src/virtManager/details.py @@ -1961,18 +1961,11 @@ class vmmDetails(vmmGObjectUI): # Device removal def remove_device(self, dev_type, dev_id_info): logging.debug("Removing device: %s %s" % (dev_type, dev_id_info)) - do_prompt = self.config.get_confirm_removedev() - if do_prompt: - res = self.err.warn_chkbox( - text1=(_("Are you sure you want to remove this device?")), - chktext=_("Don't ask me again."), - buttons=gtk.BUTTONS_YES_NO) - - response, skip_prompt = res - if not response: - return - self.config.set_confirm_removedev(not skip_prompt) + if util.chkbox_helper(self, self.config.get_confirm_removedev, + self.config.set_confirm_removedev, + text1=(_("Are you sure you want to remove this device?"))): + return # Define the change try: diff --git a/src/virtManager/engine.py b/src/virtManager/engine.py index 66a54c6d1..cba585b97 100644 --- a/src/virtManager/engine.py +++ b/src/virtManager/engine.py @@ -856,7 +856,6 @@ class vmmEngine(vmmGObject): conn = self._lookup_connection(uri) vm = conn.get_vm(uuid) managed = bool(vm.managedsave_supported) - do_prompt = self.config.get_confirm_poweroff() if not managed and conn.is_remote(): src.err.val_err(_("Saving virtual machines over remote " @@ -864,17 +863,10 @@ class vmmEngine(vmmGObject): "libvirt version or hypervisor.")) return - if do_prompt: - res = src.err.warn_chkbox( - text1=_("Are you sure you want to save " - "'%s'?" % vm.get_name()), - chktext=_("Don't ask me again."), - buttons=gtk.BUTTONS_YES_NO) - - response, skip_prompt = res - if not response: - return - self.config.set_confirm_poweroff(not skip_prompt) + if util.chkbox_helper(src, self.config.get_confirm_poweroff, + self.config.set_confirm_poweroff, + text1=_("Are you sure you want to save '%s'?" % vm.get_name())): + return path = None if not managed: @@ -964,21 +956,14 @@ class vmmEngine(vmmGObject): def _do_destroy_domain(self, src, uri, uuid): conn = self._lookup_connection(uri) vm = conn.get_vm(uuid) - do_prompt = self.config.get_confirm_forcepoweroff() - if do_prompt: - res = src.err.warn_chkbox( - text1=(_("Are you sure you want to force poweroff '%s'?") % - vm.get_name()), - text2=_("This will immediately poweroff the VM without " - "shutting down the OS and may cause data loss."), - chktext=_("Don't ask me again."), - buttons=gtk.BUTTONS_YES_NO) - - response, skip_prompt = res - if not response: - return - self.config.set_confirm_forcepoweroff(not skip_prompt) + if util.chkbox_helper(src, self.config.get_confirm_forcepoweroff, + self.config.set_confirm_forcepoweroff, + text1=_("Are you sure you want to force poweroff '%s'?" % + vm.get_name()), + text2=_("This will immediately poweroff the VM without " + "shutting down the OS and may cause data loss.")): + return logging.debug("Destroying vm '%s'." % vm.get_name()) try: @@ -989,19 +974,12 @@ class vmmEngine(vmmGObject): def _do_suspend_domain(self, src, uri, uuid): conn = self._lookup_connection(uri) vm = conn.get_vm(uuid) - do_prompt = self.config.get_confirm_pause() - if do_prompt: - res = src.err.warn_chkbox( - text1=_("Are you sure you want to pause " - "'%s'?" % vm.get_name()), - chktext=_("Don't ask me again."), - buttons=gtk.BUTTONS_YES_NO) - - response, skip_prompt = res - if not response: - return - self.config.set_confirm_pause(not skip_prompt) + if util.chkbox_helper(src, self.config.get_confirm_pause, + self.config.set_confirm_pause, + text1=_("Are you sure you want to pause '%s'?" % + vm.get_name())): + return logging.debug("Pausing vm '%s'." % vm.get_name()) try: @@ -1043,19 +1021,12 @@ class vmmEngine(vmmGObject): def _do_shutdown_domain(self, src, uri, uuid): conn = self._lookup_connection(uri) vm = conn.get_vm(uuid) - do_prompt = self.config.get_confirm_poweroff() - if do_prompt: - res = src.err.warn_chkbox( - text1=_("Are you sure you want to poweroff " - "'%s'?" % vm.get_name()), - chktext=_("Don't ask me again."), - buttons=gtk.BUTTONS_YES_NO) - - response, skip_prompt = res - if not response: - return - self.config.set_confirm_poweroff(not skip_prompt) + if util.chkbox_helper(src, self.config.get_confirm_poweroff, + self.config.set_confirm_poweroff, + text1=_("Are you sure you want to poweroff '%s'?" % + vm.get_name())): + return logging.debug("Shutting down vm '%s'." % vm.get_name()) try: @@ -1066,19 +1037,12 @@ class vmmEngine(vmmGObject): def _do_reboot_domain(self, src, uri, uuid): conn = self._lookup_connection(uri) vm = conn.get_vm(uuid) - do_prompt = self.config.get_confirm_poweroff() - if do_prompt: - res = src.err.warn_chkbox( - text1=_("Are you sure you want to reboot " - "'%s'?" % vm.get_name()), - chktext=_("Don't ask me again."), - buttons=gtk.BUTTONS_YES_NO) - - response, skip_prompt = res - if not response: - return - self.config.set_confirm_poweroff(not skip_prompt) + if util.chkbox_helper(src, self.config.get_confirm_poweroff, + self.config.set_confirm_poweroff, + text1=_("Are you sure you want to reboot '%s'?" % + vm.get_name())): + return logging.debug("Rebooting vm '%s'." % vm.get_name()) no_support = False diff --git a/src/virtManager/host.py b/src/virtManager/host.py index 7dba81ab5..56e1840ec 100644 --- a/src/virtManager/host.py +++ b/src/virtManager/host.py @@ -906,19 +906,11 @@ class vmmHost(vmmGObjectUI): if interface is None: return - do_prompt = self.config.get_confirm_interface() - - if do_prompt: - res = self.err.warn_chkbox( - text1=_("Are you sure you want to stop the interface " - "'%s'?" % interface.get_name()), - chktext=_("Don't ask me again for interface start/stop."), - buttons=gtk.BUTTONS_YES_NO) - - response, skip_prompt = res - if not response: - return - self.config.set_confirm_interface(not skip_prompt) + if util.chkbox_helper(self, self.config.get_confirm_interface, + self.config.set_confirm_interface, + text1=_("Are you sure you want to stop the interface " + "'%s'?" % interface.get_name())): + return logging.debug("Stopping interface '%s'" % interface.get_name()) try: @@ -932,19 +924,11 @@ class vmmHost(vmmGObjectUI): if interface is None: return - do_prompt = self.config.get_confirm_interface() - - if do_prompt: - res = self.err.warn_chkbox( - text1=_("Are you sure you want to start the interface " - "'%s'?" % interface.get_name()), - chktext=_("Don't ask me again for interface start/stop."), - buttons=gtk.BUTTONS_YES_NO) - - response, skip_prompt = res - if not response: - return - self.config.set_confirm_interface(not skip_prompt) + if util.chkbox_helper(self, self.config.get_confirm_interface, + self.config.set_confirm_interface, + text1=_("Are you sure you want to start the interface " + "'%s'?" % interface.get_name())): + return logging.debug("Starting interface '%s'" % interface.get_name()) try: diff --git a/src/virtManager/util.py b/src/virtManager/util.py index e5a2686f7..947110493 100644 --- a/src/virtManager/util.py +++ b/src/virtManager/util.py @@ -416,3 +416,22 @@ def pretty_bytes(val): return "%2.2f MB" % (val / (1024.0 * 1024.0)) xpath = virtinst.util.get_xml_path + +def chkbox_helper(src, getcb, setcb, text1, text2=None): + """ + Helper to prompt user about proceeding with an operation + Returns True if operation should be cancelled + """ + do_prompt = getcb() + if not do_prompt: + return False + + res = src.err.warn_chkbox(text1=text1, text2=text2, + chktext=_("Don't ask me again."), + buttons=gtk.BUTTONS_YES_NO) + response, skip_prompt = res + if not response: + return True + + setcb(not skip_prompt) + return False