mirror of
https://github.com/virt-manager/virt-manager.git
synced 2024-12-23 17:34:21 +03:00
Break all FileBrowser calls out into a util function.
This commit is contained in:
parent
2e3b9b7758
commit
dcd07970f0
@ -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()
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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()
|
||||
|
||||
|
||||
# ------------------------------
|
||||
|
@ -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:
|
||||
|
@ -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 = ""
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user