2014-01-20 17:09:13 +01:00
# Copyright (C) 2009, 2013, 2014 Red Hat, Inc.
2009-03-09 16:19:39 -04:00
# Copyright (C) 2009 Cole Robinson <crobinso@redhat.com>
#
2018-04-04 14:35:41 +01:00
# This work is licensed under the GNU GPLv2 or later.
2018-03-20 15:00:02 -04:00
# See the COPYING file in the top-level directory.
2009-03-09 16:19:39 -04:00
2022-12-13 15:09:35 -05:00
import os
from gi . repository import Gtk
2019-06-16 21:12:39 -04:00
from virtinst import log
2009-03-09 16:19:39 -04:00
2019-06-16 22:19:17 -04:00
from . lib import uiutil
2015-04-08 18:29:48 -04:00
from . baseclass import vmmGObjectUI
2019-06-16 21:59:14 -04:00
from . hoststorage import vmmHostStorage
2009-03-09 16:19:39 -04:00
2013-04-13 14:34:52 -04:00
2022-12-13 15:09:35 -05:00
class _BrowseReasonMetadata :
def __init__ ( self , browse_reason ) :
self . enable_create = False
self . storage_title = None
self . local_title = None
self . gsettings_key = None
self . dialog_type = None
2024-04-07 15:12:49 -04:00
self . choose_label = None
2022-12-13 15:09:35 -05:00
if browse_reason == vmmStorageBrowser . REASON_IMAGE :
self . enable_create = True
self . local_title = _ ( " Locate existing storage " )
self . storage_title = _ ( " Locate or create storage volume " )
self . dialog_type = Gtk . FileChooserAction . SAVE
2024-04-07 15:12:49 -04:00
self . choose_label = _ ( " _Open " )
2022-12-13 15:09:35 -05:00
self . gsettings_key = " image "
if browse_reason == vmmStorageBrowser . REASON_ISO_MEDIA :
self . local_title = _ ( " Locate ISO media " )
self . storage_title = _ ( " Locate ISO media volume " )
self . gsettings_key = " media "
if browse_reason == vmmStorageBrowser . REASON_FLOPPY_MEDIA :
self . local_title = _ ( " Locate floppy media " )
self . storage_title = _ ( " Locate floppy media volume " )
self . gsettings_key = " media "
if browse_reason == vmmStorageBrowser . REASON_FS :
self . local_title = _ ( " Locate directory volume " )
self . storage_title = _ ( " Locate directory volume " )
self . dialog_type = Gtk . FileChooserAction . SELECT_FOLDER
if browse_reason is None :
self . enable_create = True
self . storage_title = _ ( " Choose Storage Volume " )
2010-12-08 17:26:19 -05:00
class vmmStorageBrowser ( vmmGObjectUI ) :
2022-12-13 15:09:35 -05:00
REASON_IMAGE = " image "
REASON_ISO_MEDIA = " isomedia "
REASON_FLOPPY_MEDIA = " floppymedia "
REASON_FS = " fs "
2010-12-08 17:26:19 -05:00
def __init__ ( self , conn ) :
2013-09-22 16:10:16 -04:00
vmmGObjectUI . __init__ ( self , " storagebrowse.ui " , " vmm-storage-browse " )
2010-11-30 14:33:21 -05:00
self . conn = conn
2013-10-05 14:15:11 -04:00
self . _first_run = False
2014-12-09 18:37:00 -05:00
self . _finish_cb = None
2015-04-08 18:29:48 -04:00
self . _browse_reason = None
2009-03-09 16:19:39 -04:00
2019-06-16 21:59:14 -04:00
self . storagelist = vmmHostStorage ( self . conn , self . builder , self . topwin ,
2015-04-08 18:29:48 -04:00
self . _vol_sensitive_cb )
self . _init_ui ( )
2014-01-20 17:09:13 +01:00
2013-02-16 13:31:46 -05:00
self . builder . connect_signals ( {
2017-08-05 14:39:32 +08:00
" on_vmm_storage_browse_delete_event " : self . close ,
2009-03-09 16:19:39 -04:00
} )
2011-04-18 11:25:28 -04:00
self . bind_escape_key_close ( )
2009-11-17 15:06:15 -05:00
2009-03-09 16:19:39 -04:00
2015-04-08 18:29:48 -04:00
def show ( self , parent ) :
2019-06-16 21:12:39 -04:00
log . debug ( " Showing storage browser " )
2015-04-08 18:29:48 -04:00
if not self . _first_run :
self . _first_run = True
pool = self . conn . get_default_pool ( )
2020-09-01 12:35:26 -04:00
uiutil . set_list_selection (
self . storagelist . widget ( " pool-list " ) , pool )
2015-04-08 18:29:48 -04:00
2011-04-14 08:47:42 -04:00
self . topwin . set_transient_for ( parent )
2009-03-09 16:19:39 -04:00
self . topwin . present ( )
2013-07-07 11:06:15 -04:00
self . conn . schedule_priority_tick ( pollpool = True )
2009-03-09 16:19:39 -04:00
def close ( self , ignore1 = None , ignore2 = None ) :
2019-04-13 17:03:16 -04:00
if self . is_visible ( ) :
2019-06-16 21:12:39 -04:00
log . debug ( " Closing storage browser " )
2015-05-03 16:56:45 -04:00
self . topwin . hide ( )
2015-04-08 18:29:48 -04:00
self . storagelist . close ( )
2009-03-09 16:19:39 -04:00
return 1
2011-07-23 21:16:54 -04:00
def _cleanup ( self ) :
self . conn = None
2011-04-11 18:35:21 -04:00
2015-04-08 18:29:48 -04:00
self . storagelist . cleanup ( )
self . storagelist = None
2011-04-11 18:35:21 -04:00
2020-08-22 15:05:30 -04:00
###########
# UI init #
###########
2009-03-09 16:19:39 -04:00
2015-04-08 18:29:48 -04:00
def _init_ui ( self ) :
self . storagelist . connect ( " browse-clicked " , self . _browse_clicked )
self . storagelist . connect ( " volume-chosen " , self . _volume_chosen )
self . storagelist . connect ( " cancel-clicked " , self . close )
2014-06-02 17:17:47 -04:00
2015-04-08 18:29:48 -04:00
self . widget ( " storage-align " ) . add ( self . storagelist . top_box )
self . err . set_modal_default ( True )
self . storagelist . err . set_modal_default ( True )
2009-03-09 16:19:39 -04:00
2012-05-14 14:24:56 +01:00
tooltip = " "
2009-03-09 16:19:39 -04:00
is_remote = self . conn . is_remote ( )
2015-04-08 18:29:48 -04:00
self . storagelist . widget ( " browse-local " ) . set_sensitive ( not is_remote )
2009-03-09 16:19:39 -04:00
if is_remote :
tooltip = _ ( " Cannot use local storage on remote connection. " )
2015-04-08 18:29:48 -04:00
self . storagelist . widget ( " browse-local " ) . set_tooltip_text ( tooltip )
uiutil . set_grid_row_visible (
self . storagelist . widget ( " pool-autostart " ) , False )
uiutil . set_grid_row_visible (
self . storagelist . widget ( " pool-name-entry " ) , False )
uiutil . set_grid_row_visible (
self . storagelist . widget ( " pool-state-box " ) , False )
self . storagelist . widget ( " browse-local " ) . set_visible ( True )
self . storagelist . widget ( " browse-cancel " ) . set_visible ( True )
self . storagelist . widget ( " choose-volume " ) . set_visible ( True )
self . storagelist . widget ( " choose-volume " ) . set_sensitive ( False )
self . storagelist . widget ( " pool-apply " ) . set_visible ( False )
2020-08-22 15:05:30 -04:00
self . set_browse_reason ( self . _browse_reason )
##############
# Public API #
##############
def set_finish_cb ( self , callback ) :
self . _finish_cb = callback
def set_vm_name ( self , name ) :
self . storagelist . set_name_hint ( name )
def set_browse_reason ( self , reason ) :
self . _browse_reason = reason
2022-12-13 15:09:35 -05:00
data = _BrowseReasonMetadata ( self . _browse_reason )
2014-06-02 17:17:47 -04:00
2022-12-13 15:09:35 -05:00
self . topwin . set_title ( data . storage_title )
self . storagelist . widget ( " vol-add " ) . set_sensitive ( data . enable_create )
2009-03-09 16:19:39 -04:00
2015-04-08 18:29:48 -04:00
#############
# Listeners #
#############
def _browse_clicked ( self , src ) :
2014-06-02 17:17:47 -04:00
ignore = src
2015-04-08 18:29:48 -04:00
return self . _browse_local ( )
2009-03-09 16:19:39 -04:00
2015-04-08 18:29:48 -04:00
def _volume_chosen ( self , src , volume ) :
ignore = src
2019-06-16 21:12:39 -04:00
log . debug ( " Chosen volume XML: \n %s " , volume . xmlobj . get_xml ( ) )
2015-04-08 18:29:48 -04:00
self . _finish ( volume . get_target_path ( ) )
def _vol_sensitive_cb ( self , fmt ) :
2022-12-13 15:09:35 -05:00
if ( ( self . _browse_reason == vmmStorageBrowser . REASON_FS ) and
2015-07-10 14:07:02 +02:00
fmt != ' dir ' ) :
2015-04-08 18:29:48 -04:00
return False
return True
2009-03-09 16:19:39 -04:00
2015-04-08 18:29:48 -04:00
####################
# Internal helpers #
####################
2010-03-03 17:48:22 -05:00
2015-04-08 18:29:48 -04:00
def _browse_local ( self ) :
2022-12-13 15:09:35 -05:00
data = _BrowseReasonMetadata ( self . _browse_reason )
gsettings_key = data . gsettings_key
if gsettings_key :
start_folder = self . config . get_default_directory ( gsettings_key )
filename = self . err . browse_local (
dialog_type = data . dialog_type ,
dialog_name = data . local_title ,
2024-04-07 15:12:49 -04:00
start_folder = start_folder ,
choose_label = data . choose_label )
2022-12-13 15:09:35 -05:00
if not filename :
return
log . debug ( " Browse local chose path= %s " , filename )
if gsettings_key :
self . config . set_default_directory (
gsettings_key , os . path . dirname ( filename ) )
self . _finish ( filename )
2009-03-09 16:19:39 -04:00
2015-04-08 18:29:48 -04:00
def _finish ( self , path ) :
if self . _finish_cb :
self . _finish_cb ( self , path )
self . close ( )