2014-01-20 20:09:13 +04:00
# Copyright (C) 2009, 2013, 2014 Red Hat, Inc.
2009-03-09 23:19:39 +03:00
# Copyright (C) 2009 Cole Robinson <crobinso@redhat.com>
#
2018-04-04 16:35:41 +03:00
# This work is licensed under the GNU GPLv2 or later.
2018-03-20 22:00:02 +03:00
# See the COPYING file in the top-level directory.
2009-03-09 23:19:39 +03:00
import logging
2014-09-13 00:10:45 +04:00
from . import uiutil
2015-04-09 01:29:48 +03:00
from . baseclass import vmmGObjectUI
from . storagelist import vmmStorageList
2009-03-09 23:19:39 +03:00
2013-04-13 22:34:52 +04:00
2010-12-09 01:26:19 +03:00
class vmmStorageBrowser ( vmmGObjectUI ) :
def __init__ ( self , conn ) :
2013-09-23 00:10:16 +04:00
vmmGObjectUI . __init__ ( self , " storagebrowse.ui " , " vmm-storage-browse " )
2010-11-30 22:33:21 +03:00
self . conn = conn
2013-10-05 22:15:11 +04:00
self . _first_run = False
2014-12-10 02:37:00 +03:00
self . _finish_cb = None
2009-03-09 23:19:39 +03:00
2015-04-09 01:29:48 +03:00
# Passed to browse_local
self . _browse_reason = None
2009-03-09 23:19:39 +03:00
2015-04-09 01:29:48 +03:00
self . storagelist = vmmStorageList ( self . conn , self . builder , self . topwin ,
self . _vol_sensitive_cb )
self . _init_ui ( )
2014-01-20 20:09:13 +04:00
2013-02-16 22:31:46 +04:00
self . builder . connect_signals ( {
2017-08-05 09:39:32 +03:00
" on_vmm_storage_browse_delete_event " : self . close ,
2009-03-09 23:19:39 +03:00
} )
2011-04-18 19:25:28 +04:00
self . bind_escape_key_close ( )
2009-11-17 23:06:15 +03:00
2009-03-09 23:19:39 +03:00
2015-04-09 01:29:48 +03:00
def show ( self , parent ) :
2012-02-01 03:16:54 +04:00
logging . debug ( " Showing storage browser " )
2015-04-09 01:29:48 +03:00
if not self . _first_run :
self . _first_run = True
pool = self . conn . get_default_pool ( )
2015-05-20 01:13:33 +03:00
uiutil . set_list_selection ( self . storagelist . widget ( " pool-list " ) ,
2015-04-09 01:29:48 +03:00
pool and pool . get_connkey ( ) or None )
2011-04-14 16:47:42 +04:00
self . topwin . set_transient_for ( parent )
2009-03-09 23:19:39 +03:00
self . topwin . present ( )
2013-07-07 19:06:15 +04:00
self . conn . schedule_priority_tick ( pollpool = True )
2009-03-09 23:19:39 +03:00
def close ( self , ignore1 = None , ignore2 = None ) :
2015-05-03 23:56:45 +03:00
if self . topwin . is_visible ( ) :
logging . debug ( " Closing storage browser " )
self . topwin . hide ( )
2015-04-09 01:29:48 +03:00
self . storagelist . close ( )
2009-03-09 23:19:39 +03:00
return 1
2011-07-24 05:16:54 +04:00
def _cleanup ( self ) :
self . conn = None
2011-04-12 02:35:21 +04:00
2015-04-09 01:29:48 +03:00
self . storagelist . cleanup ( )
self . storagelist = None
2011-04-12 02:35:21 +04:00
2015-04-09 01:29:48 +03:00
##############
# Public API #
##############
2011-04-12 02:35:21 +04:00
2009-09-14 18:39:57 +04:00
def set_finish_cb ( self , callback ) :
2014-12-10 02:37:00 +03:00
self . _finish_cb = callback
2009-11-10 22:30:51 +03:00
def set_browse_reason ( self , reason ) :
2015-04-09 01:29:48 +03:00
self . _browse_reason = reason
2009-11-15 23:57:38 +03:00
def set_vm_name ( self , name ) :
2015-04-09 01:29:48 +03:00
self . storagelist . set_name_hint ( name )
2009-03-09 23:19:39 +03:00
2015-04-09 01:29:48 +03: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-03 01:17:47 +04:00
2015-04-09 01:29:48 +03: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 23:19:39 +03:00
2012-05-14 17:24:56 +04:00
tooltip = " "
2009-03-09 23:19:39 +03:00
is_remote = self . conn . is_remote ( )
2015-04-09 01:29:48 +03:00
self . storagelist . widget ( " browse-local " ) . set_sensitive ( not is_remote )
2009-03-09 23:19:39 +03:00
if is_remote :
tooltip = _ ( " Cannot use local storage on remote connection. " )
2015-04-09 01:29:48 +03: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 )
data = self . config . browse_reason_data . get ( self . _browse_reason )
allow_create = True
2010-12-09 22:33:04 +03:00
if data :
self . topwin . set_title ( data [ " storage_title " ] )
2015-04-09 01:29:48 +03:00
allow_create = data [ " enable_create " ]
2014-06-03 01:17:47 +04:00
2015-04-09 01:29:48 +03:00
self . storagelist . widget ( " vol-add " ) . set_sensitive ( allow_create )
2009-03-09 23:19:39 +03:00
2015-04-09 01:29:48 +03:00
#############
# Listeners #
#############
def _browse_clicked ( self , src ) :
2014-06-03 01:17:47 +04:00
ignore = src
2015-04-09 01:29:48 +03:00
return self . _browse_local ( )
2009-03-09 23:19:39 +03:00
2015-04-09 01:29:48 +03:00
def _volume_chosen ( self , src , volume ) :
ignore = src
2018-08-31 23:52:02 +03:00
logging . debug ( " Chosen volume XML: \n %s " , volume . xmlobj . get_xml ( ) )
2015-04-09 01:29:48 +03:00
self . _finish ( volume . get_target_path ( ) )
def _vol_sensitive_cb ( self , fmt ) :
2015-07-10 15:07:02 +03:00
if ( ( self . _browse_reason == self . config . CONFIG_DIR_FS ) and
fmt != ' dir ' ) :
2015-04-09 01:29:48 +03:00
return False
return True
2009-03-09 23:19:39 +03:00
2015-04-09 01:29:48 +03:00
####################
# Internal helpers #
####################
2010-03-04 01:48:22 +03:00
2015-04-09 01:29:48 +03:00
def _browse_local ( self ) :
dialog_type = None
dialog_name = None
choose_button = None
2009-03-09 23:19:39 +03:00
2015-04-09 01:29:48 +03:00
data = self . config . browse_reason_data . get ( self . _browse_reason )
if data :
dialog_name = data [ " local_title " ] or None
dialog_type = data . get ( " dialog_type " )
choose_button = data . get ( " choose_button " )
2011-06-09 02:37:35 +04:00
2015-04-09 01:29:48 +03:00
filename = self . err . browse_local ( self . conn ,
dialog_type = dialog_type , browse_reason = self . _browse_reason ,
dialog_name = dialog_name , choose_button = choose_button )
if filename :
2015-08-10 23:45:44 +03:00
logging . debug ( " Browse local chose path= %s " , filename )
2015-04-09 01:29:48 +03:00
self . _finish ( filename )
2009-03-09 23:19:39 +03:00
2015-04-09 01:29:48 +03:00
def _finish ( self , path ) :
if self . _finish_cb :
self . _finish_cb ( self , path )
self . close ( )