uihelpers: Move browse_local to error.py

It doesn't fit any better, but now it's accessible to all UI classes
more easily.
This commit is contained in:
Cole Robinson 2014-01-26 17:59:09 -05:00
parent 58d5e0b799
commit 2a34b353da
6 changed files with 102 additions and 111 deletions

View File

@ -536,6 +536,5 @@ class vmmCreatePool(vmmGObjectUI):
if foldermode:
mode = Gtk.FileChooserAction.SELECT_FOLDER
return uihelpers.browse_local(self.topwin, dialog_name, self.conn,
dialog_type=mode,
start_folder=startfolder)
return self.err.browse_local(self.conn, dialog_name,
dialog_type=mode, start_folder=startfolder)

View File

@ -1494,14 +1494,12 @@ class vmmDetails(vmmGObjectUI):
now = str(datetime.datetime.now()).split(".")[0].replace(" ", "_")
default = "Screenshot_%s_%s.png" % (self.vm.get_name(), now)
path = uihelpers.browse_local(
self.topwin,
_("Save Virtual Machine Screenshot"),
self.vm.conn,
_type=("png", "PNG files"),
dialog_type=Gtk.FileChooserAction.SAVE,
browse_reason=self.config.CONFIG_DIR_SCREENSHOT,
default_name=default)
path = self.err.browse_local(
self.vm.conn, _("Save Virtual Machine Screenshot"),
_type=("png", "PNG files"),
dialog_type=Gtk.FileChooserAction.SAVE,
browse_reason=self.config.CONFIG_DIR_SCREENSHOT,
default_name=default)
if not path:
logging.debug("No screenshot path given, skipping save.")
return

View File

@ -33,7 +33,6 @@ import libvirt
from virtinst import util
from virtManager import packageutils
from virtManager import uihelpers
from virtManager.about import vmmAbout
from virtManager.baseclass import vmmGObject
from virtManager.clone import vmmCloneVM
@ -908,11 +907,10 @@ class vmmEngine(vmmGObject):
path = None
if not managed:
path = uihelpers.browse_local(src.topwin,
_("Save Virtual Machine"),
conn,
dialog_type=Gtk.FileChooserAction.SAVE,
browse_reason=self.config.CONFIG_DIR_SAVE)
path = src.err.browse_local(
conn, _("Save Virtual Machine"),
dialog_type=Gtk.FileChooserAction.SAVE,
browse_reason=self.config.CONFIG_DIR_SAVE)
if not path:
return
@ -956,10 +954,9 @@ class vmmEngine(vmmGObject):
"connections is not yet supported"))
return
path = uihelpers.browse_local(src.topwin,
_("Restore Virtual Machine"),
conn,
browse_reason=self.config.CONFIG_DIR_RESTORE)
path = src.err.browse_local(
conn, _("Restore Virtual Machine"),
browse_reason=self.config.CONFIG_DIR_RESTORE)
if not path:
return

View File

@ -203,6 +203,91 @@ class vmmErrorDialog(vmmGObject):
return response
def browse_local(self, conn, dialog_name, start_folder=None,
_type=None, dialog_type=None,
confirm_func=None, browse_reason=None,
choose_button=None, default_name=None):
"""
Helper function for launching a filechooser
@dialog_name: String to use in the title bar of the filechooser.
@conn: vmmConnection used by calling class
@start_folder: Folder the filechooser is viewing at startup
@_type: File extension to filter by (e.g. "iso", "png")
@dialog_type: Maps to FileChooserDialog 'action'
@confirm_func: Optional callback function if file is chosen.
@browse_reason: The vmmConfig.CONFIG_DIR* reason we are browsing.
If set, this will override the 'folder' parameter with the gconf
value, and store the user chosen path.
"""
import os
# Initial setup
overwrite_confirm = False
if dialog_type is None:
dialog_type = Gtk.FileChooserAction.OPEN
if dialog_type == Gtk.FileChooserAction.SAVE:
if choose_button is None:
choose_button = Gtk.STOCK_SAVE
overwrite_confirm = True
if choose_button is None:
choose_button = Gtk.STOCK_OPEN
fcdialog = Gtk.FileChooserDialog(title=dialog_name,
parent=self._parent,
action=dialog_type,
buttons=(Gtk.STOCK_CANCEL,
Gtk.ResponseType.CANCEL,
choose_button,
Gtk.ResponseType.ACCEPT))
fcdialog.set_default_response(Gtk.ResponseType.ACCEPT)
if default_name:
fcdialog.set_current_name(default_name)
# If confirm is set, warn about a file overwrite
if confirm_func:
overwrite_confirm = True
fcdialog.connect("confirm-overwrite", confirm_func)
fcdialog.set_do_overwrite_confirmation(overwrite_confirm)
# Set file match pattern (ex. *.png)
if _type is not 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)
# Set initial dialog folder
if browse_reason:
start_folder = self.config.get_default_directory(
conn, browse_reason)
if start_folder is not None:
if os.access(start_folder, os.R_OK):
fcdialog.set_current_folder(start_folder)
# Run the dialog and parse the response
ret = None
if fcdialog.run() == Gtk.ResponseType.ACCEPT:
ret = fcdialog.get_filename()
fcdialog.destroy()
# Store the chosen directory in gconf if necessary
if ret and browse_reason and not ret.startswith("/dev"):
self.config.set_default_directory(
os.path.dirname(ret), browse_reason)
return ret
class _errorDialog (Gtk.MessageDialog):
"""

View File

@ -308,9 +308,8 @@ class vmmStorageBrowser(vmmGObjectUI):
if not self.local_args.get("dialog_name"):
self.local_args["dialog_name"] = None
filename = uihelpers.browse_local(parent=self.topwin,
conn=self.conn,
**self.local_args)
filename = self.err.browse_local(
self.conn, **self.local_args)
if filename:
self._do_finish(path=filename)

View File

@ -823,93 +823,6 @@ def spin_get_helper(widget):
return adj.get_value()
def browse_local(parent, dialog_name, conn, start_folder=None,
_type=None, dialog_type=None,
confirm_func=None, browse_reason=None,
choose_button=None, default_name=None):
"""
Helper function for launching a filechooser
@param parent: Parent window for the filechooser
@param dialog_name: String to use in the title bar of the filechooser.
@param conn: vmmConnection used by calling class
@param start_folder: Folder the filechooser is viewing at startup
@param _type: File extension to filter by (e.g. "iso", "png")
@param dialog_type: Maps to FileChooserDialog 'action'
@param confirm_func: Optional callback function if file is chosen.
@param browse_reason: The vmmConfig.CONFIG_DIR* reason we are browsing.
If set, this will override the 'folder' parameter with the gconf
value, and store the user chosen path.
"""
# Initial setup
overwrite_confirm = False
if dialog_type is None:
dialog_type = Gtk.FileChooserAction.OPEN
if dialog_type == Gtk.FileChooserAction.SAVE:
if choose_button is None:
choose_button = Gtk.STOCK_SAVE
overwrite_confirm = True
if choose_button is None:
choose_button = Gtk.STOCK_OPEN
fcdialog = Gtk.FileChooserDialog(title=dialog_name,
parent=parent,
action=dialog_type,
buttons=(Gtk.STOCK_CANCEL,
Gtk.ResponseType.CANCEL,
choose_button,
Gtk.ResponseType.ACCEPT))
fcdialog.set_default_response(Gtk.ResponseType.ACCEPT)
if default_name:
fcdialog.set_current_name(default_name)
# If confirm is set, warn about a file overwrite
if confirm_func:
overwrite_confirm = True
fcdialog.connect("confirm-overwrite", confirm_func)
fcdialog.set_do_overwrite_confirmation(overwrite_confirm)
# Set file match pattern (ex. *.png)
if _type is not 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)
# Set initial dialog folder
if browse_reason:
start_folder = config.running_config.get_default_directory(conn,
browse_reason)
if start_folder is not None:
if os.access(start_folder, os.R_OK):
fcdialog.set_current_folder(start_folder)
# Run the dialog and parse the response
ret = None
if fcdialog.run() == Gtk.ResponseType.ACCEPT:
ret = fcdialog.get_filename()
fcdialog.destroy()
# Store the chosen directory in gconf if necessary
if ret and browse_reason and not ret.startswith("/dev"):
config.running_config.set_default_directory(os.path.dirname(ret),
browse_reason)
return ret
def get_list_selection(widget):
selection = widget.get_selection()
active = selection.get_selected()