From dcd07970f0612e8a6d264b71eaee1a8a4811ab37 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Sun, 8 Mar 2009 15:14:00 -0400 Subject: [PATCH] Break all FileBrowser calls out into a util function. --- src/virtManager/addhardware.py | 30 +++------------- src/virtManager/choosecd.py | 29 ++++------------ src/virtManager/createpool.py | 18 +++------- src/virtManager/details.py | 63 +++++++++++++++------------------- src/virtManager/engine.py | 48 +++++++++++++------------- src/virtManager/manager.py | 32 +++++++---------- src/virtManager/util.py | 50 +++++++++++++++++++++++++++ 7 files changed, 128 insertions(+), 142 deletions(-) diff --git a/src/virtManager/addhardware.py b/src/virtManager/addhardware.py index 3b7b842b7..c280ca7b2 100644 --- a/src/virtManager/addhardware.py +++ b/src/virtManager/addhardware.py @@ -645,32 +645,12 @@ class vmmAddHardware(gobject.GObject): def _browse_file(self, dialog_name, folder=None, _type=None, confirm_overwrite=False): - # user wants to browse for an ISO - fcdialog = gtk.FileChooserDialog(dialog_name, - self.window.get_widget("vmm-add-hardware"), - gtk.FILE_CHOOSER_ACTION_OPEN, - (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, - gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT), - None) - fcdialog.set_default_response(gtk.RESPONSE_ACCEPT) - if _type != None: - f = gtk.FileFilter() - f.add_pattern("*." + _type) - fcdialog.set_filter(f) - if folder != None: - fcdialog.set_current_folder(folder) + + confirm_func = None if confirm_overwrite: - fcdialog.set_do_overwrite_confirmation(True) - fcdialog.connect("confirm-overwrite", self.confirm_overwrite_callback) - response = fcdialog.run() - fcdialog.hide() - if(response == gtk.RESPONSE_ACCEPT): - filename = fcdialog.get_filename() - fcdialog.destroy() - return filename - else: - fcdialog.destroy() - return None + confirm_func = self.confirm_overwrite_callback + return vmmutil.browse_local(self.topwin, dialog_name, folder, _type, + confirm_func=confirm_func) def toggle_storage_size(self, ignore1=None, ignore2=None): filename = self.get_config_disk_image() diff --git a/src/virtManager/choosecd.py b/src/virtManager/choosecd.py index 5b62fcc00..7fa0663e9 100644 --- a/src/virtManager/choosecd.py +++ b/src/virtManager/choosecd.py @@ -20,7 +20,10 @@ import gtk.glade import gobject import logging + import virtinst + +from virtManager import util from virtManager.opticalhelper import vmmOpticalDriveHelper from virtManager.error import vmmErrorDialog @@ -141,28 +144,8 @@ class vmmChooseCD(gobject.GObject): self.window.get_widget("physical-media").set_sensitive(False) def _browse_file(self, dialog_name, folder=None, _type=None): - # user wants to browse for an ISO - fcdialog = gtk.FileChooserDialog(dialog_name, - self.window.get_widget("vmm-choose-cd"), - gtk.FILE_CHOOSER_ACTION_OPEN, - (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, - gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT), - None) - fcdialog.set_default_response(gtk.RESPONSE_ACCEPT) - if _type != None: - f = gtk.FileFilter() - f.add_pattern("*." + _type) - fcdialog.set_filter(f) - if folder != None: - fcdialog.set_current_folder(folder) - response = fcdialog.run() - fcdialog.hide() - if(response == gtk.RESPONSE_ACCEPT): - filename = fcdialog.get_filename() - fcdialog.destroy() - return filename - else: - fcdialog.destroy() - return None + return util.browse_local(self.window.get_widget("vmm-choose-cd"), + dialog_name, folder, _type) + gobject.type_register(vmmChooseCD) diff --git a/src/virtManager/createpool.py b/src/virtManager/createpool.py index 2699369f9..d968df529 100644 --- a/src/virtManager/createpool.py +++ b/src/virtManager/createpool.py @@ -26,6 +26,7 @@ import logging import libvirt +from virtManager import util from virtManager.error import vmmErrorDialog from virtManager.asyncjob import vmmAsyncJob from virtManager.createmeter import vmmCreateMeter @@ -407,20 +408,9 @@ class vmmCreatePool(gobject.GObject): mode = gtk.FILE_CHOOSER_ACTION_OPEN if foldermode: mode = gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER - fcdialog = gtk.FileChooserDialog(dialog_name, self.topwin, mode, - (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, - gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT), - None) - fcdialog.set_default_response(gtk.RESPONSE_ACCEPT) - if startfolder != None: - fcdialog.set_current_folder(startfolder) - response = fcdialog.run() - fcdialog.hide() - ret = None - if(response == gtk.RESPONSE_ACCEPT): - ret = fcdialog.get_filename() - fcdialog.destroy() - return ret + return util.browse_local(self.topwin, dialog_name, dialog_type=mode, + start_folder=startfolder, + foldermode=foldermode) gobject.type_register(vmmCreatePool) diff --git a/src/virtManager/details.py b/src/virtManager/details.py index f57c0f0b8..06206c0c0 100644 --- a/src/virtManager/details.py +++ b/src/virtManager/details.py @@ -1359,42 +1359,35 @@ class vmmDetails(gobject.GObject): def control_vm_screenshot(self, src): # If someone feels kind they could extend this code to allow # user to choose what image format they'd like to save in.... - fcdialog = gtk.FileChooserDialog(_("Save Virtual Machine Screenshot"), - self.window.get_widget("vmm-details"), - gtk.FILE_CHOOSER_ACTION_SAVE, - (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, - gtk.STOCK_SAVE, gtk.RESPONSE_ACCEPT), - None) - fcdialog.set_default_response(gtk.RESPONSE_ACCEPT) - png = gtk.FileFilter() - png.set_name("PNG files") - png.add_pattern("*.png") - fcdialog.add_filter(png) - fcdialog.set_do_overwrite_confirmation(True) - if fcdialog.run() == gtk.RESPONSE_ACCEPT: - fcdialog.hide() - filename = fcdialog.get_filename() - if not(filename.endswith(".png")): - filename += ".png" - image = self.vncViewer.get_pixbuf() + path = util.browse_local(self.window.get_widget("vmm-details"), + _("Save Virtual Machine Screenshot"), + _type = ("*.png", "PNG files"), + dialog_type = gtk.FILE_CHOOSER_ACTION_SAVE) + if not path: + return - # Save along with a little metadata about us & the domain - image.save(filename, 'png', - { 'tEXt::Hypervisor URI': self.vm.get_connection().get_uri(), - 'tEXt::Domain Name': self.vm.get_name(), - 'tEXt::Domain UUID': self.vm.get_uuid(), - 'tEXt::Generator App': self.config.get_appname(), - 'tEXt::Generator Version': self.config.get_appversion() }) - msg = gtk.MessageDialog(self.window.get_widget("vmm-details"), - gtk.DIALOG_MODAL, - gtk.MESSAGE_INFO, - gtk.BUTTONS_OK,_("The screenshot has been saved to:\n%s") % file) - msg.set_title(_("Screenshot saved")) - msg.run() - msg.destroy() - else: - fcdialog.hide() - fcdialog.destroy() + filename = path + if not(filename.endswith(".png")): + filename += ".png" + image = self.vncViewer.get_pixbuf() + + # Save along with a little metadata about us & the domain + image.save(filename, 'png', + { 'tEXt::Hypervisor URI': self.vm.get_connection().get_uri(), + 'tEXt::Domain Name': self.vm.get_name(), + 'tEXt::Domain UUID': self.vm.get_uuid(), + 'tEXt::Generator App': self.config.get_appname(), + 'tEXt::Generator Version': self.config.get_appversion() }) + + msg = gtk.MessageDialog(self.window.get_widget("vmm-details"), + gtk.DIALOG_MODAL, + gtk.MESSAGE_INFO, + gtk.BUTTONS_OK, + (_("The screenshot has been saved to:\n%s") % + filename)) + msg.set_title(_("Screenshot saved")) + msg.run() + msg.destroy() # ------------------------------ diff --git a/src/virtManager/engine.py b/src/virtManager/engine.py index a3632831e..ab6e4f4ab 100644 --- a/src/virtManager/engine.py +++ b/src/virtManager/engine.py @@ -393,39 +393,37 @@ class vmmEngine(gobject.GObject): def save_domain(self, src, uri, uuid): con = self.get_connection(uri, False) if con.is_remote(): - self.err.val_err(_("Saving virtual machines over remote connections is not yet supported.")) + # FIXME: This should work with remote storage stuff + self.err.val_err(_("Saving virtual machines over remote " + "connections is not yet supported.")) return - + vm = con.get_vm(uuid) status = vm.status() if status in [ libvirt.VIR_DOMAIN_SHUTDOWN, libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_CRASHED, libvirt.VIR_DOMAIN_PAUSED ]: - logging.warning("Save requested, but machine is shutdown / shutoff / paused") - else: - fcdialog = gtk.FileChooserDialog(_("Save Virtual Machine"), - src.window.get_widget("vmm-details"), - gtk.FILE_CHOOSER_ACTION_SAVE, - (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, - gtk.STOCK_SAVE, gtk.RESPONSE_ACCEPT), - None) - fcdialog.set_default_response(gtk.RESPONSE_ACCEPT) - fcdialog.set_current_folder(self.config.get_default_save_dir(con)) - fcdialog.set_do_overwrite_confirmation(True) - response = fcdialog.run() - fcdialog.hide() - if(response == gtk.RESPONSE_ACCEPT): - file_to_save = fcdialog.get_filename() - progWin = vmmAsyncJob(self.config, self._save_callback, - [vm, file_to_save], - _("Saving Virtual Machine")) - progWin.run() - fcdialog.destroy() + logging.warning("Save requested, but machine is shutdown / " + "shutoff / paused") + return - if self._save_callback_info != []: - self.err.show_err(_("Error saving domain: %s" % self._save_callback_info[0]), self._save_callback_info[1]) - self._save_callback_info = [] + path = util.browse_local(src.window.get_widget("vmm-details"), + _("Save Virtual Machine"), + self.config.get_default_save_dir(con), + dialog_type=gtk.FILE_CHOOSER_ACTION_SAVE) + + if path: + progWin = vmmAsyncJob(self.config, self._save_callback, + [vm, path], + _("Saving Virtual Machine")) + progWin.run() + + if self._save_callback_info != []: + self.err.show_err(_("Error saving domain: %s") % + self._save_callback_info[0], + self._save_callback_info[1]) + self._save_callback_info = [] def _save_callback(self, vm, file_to_save, ignore1=None): try: diff --git a/src/virtManager/manager.py b/src/virtManager/manager.py index 707e7ba06..af816e4b5 100644 --- a/src/virtManager/manager.py +++ b/src/virtManager/manager.py @@ -374,35 +374,27 @@ class vmmManager(gobject.GObject): def restore_saved(self, src=None): conn = self.current_connection() if conn.is_remote(): - self.err.val_err(_("Restoring virtual machines over remote connections is not yet supported")) + self.err.val_err(_("Restoring virtual machines over remote " + "connections is not yet supported")) return - # get filename - 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) - fcdialog.set_default_response(gtk.RESPONSE_ACCEPT) - fcdialog.set_current_folder(self.config.get_default_save_dir(self.current_connection())) - # pop up progress dialog - response = fcdialog.run() - fcdialog.hide() - if(response == gtk.RESPONSE_ACCEPT): - file_to_load = fcdialog.get_filename() - if self.is_valid_saved_image(file_to_load): + path = util.browse_local(self.window.get_widget("vmm-manager"), + _("Restore Virtual Machine"), + self.config.get_default_save_dir(conn)) + + if path: + if self.is_valid_saved_image(path): progWin = vmmAsyncJob(self.config, self.restore_saved_callback, - [file_to_load], + [path], _("Restoring Virtual Machine")) progWin.run() else: - self.err.val_err(_("The file '%s' does not appear to be a valid saved machine image") % file_to_load) + self.err.val_err(_("The file '%s' does not appear to be a " + "valid saved machine image") % path) return - fcdialog.destroy() - if(self.domain_restore_error != ""): + if self.domain_restore_error != "": self.err.val_err(self.domain_restore_error) self.domain_restore_error = "" diff --git a/src/virtManager/util.py b/src/virtManager/util.py index 37741e0b5..40a5b4cd3 100644 --- a/src/virtManager/util.py +++ b/src/virtManager/util.py @@ -19,6 +19,7 @@ # import logging +import gtk import libvirt @@ -62,3 +63,52 @@ def tooltip_wrapper(obj, txt, func="set_tooltip_text"): except: # XXX: Catch a specific error here pass + +def browse_local(parent, dialog_name, start_folder=None, _type=None, + dialog_type=gtk.FILE_CHOOSER_ACTION_OPEN, + foldermode=False, confirm_func=None): + + overwrite_confirm = False + choose_button = gtk.STOCK_OPEN + if dialog_type == gtk.FILE_CHOOSER_ACTION_SAVE: + choose_button = gtk.STOCK_SAVE + overwrite_confirm = True + + fcdialog = gtk.FileChooserDialog(dialog_name, parent, + dialog_type, + (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, + choose_button, gtk.RESPONSE_ACCEPT), + None) + fcdialog.set_default_response(gtk.RESPONSE_ACCEPT) + + if confirm_func: + overwrite_confirm = True + fcdialog.connect("confirm-overwrite", confirm_func) + fcdialog.set_do_overwrite_confirmation(overwrite_confirm) + + if _type != None: + pattern = _type + name = None + if type(_type) is tuple: + pattern = _type[0] + name = _type[1] + + f = gtk.FileFilter() + f.add_pattern("*." + pattern) + if name: + f.set_name(name) + fcdialog.set_filter(f) + + if start_folder != None: + fcdialog.set_current_folder(start_folder) + + response = fcdialog.run() + fcdialog.hide() + if(response == gtk.RESPONSE_ACCEPT): + filename = fcdialog.get_filename() + fcdialog.destroy() + return filename + else: + fcdialog.destroy() + return None +