2014-01-28 22:51:59 +04:00
# Copyright (C) 2014 Red Hat, Inc.
#
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.
2014-01-28 22:51:59 +04:00
from gi . repository import Gtk
2019-06-17 05:19:17 +03:00
from . . lib import uiutil
2019-06-17 04:45:19 +03:00
from . . baseclass import vmmGObject , vmmGObjectUI
2014-01-28 22:51:59 +04:00
class vmmMediaCombo ( vmmGObjectUI ) :
2018-10-06 21:26:31 +03:00
__gsignals__ = {
" changed " : ( vmmGObject . RUN_FIRST , None , [ object ] ) ,
" activate " : ( vmmGObject . RUN_FIRST , None , [ object ] ) ,
}
MEDIA_TYPE_FLOPPY = " floppy "
MEDIA_TYPE_CDROM = " cdrom "
2015-04-07 20:56:48 +03:00
2018-10-06 21:26:31 +03:00
MEDIA_FIELDS_NUM = 4
( MEDIA_FIELD_PATH ,
MEDIA_FIELD_LABEL ,
MEDIA_FIELD_HAS_MEDIA ,
MEDIA_FIELD_KEY ) = range ( MEDIA_FIELDS_NUM )
2014-01-28 22:51:59 +04:00
2018-10-06 21:26:31 +03:00
def __init__ ( self , conn , builder , topwin ) :
2014-01-28 22:51:59 +04:00
vmmGObjectUI . __init__ ( self , None , None , builder = builder , topwin = topwin )
self . conn = conn
self . top_box = None
2018-10-06 21:26:31 +03:00
self . _combo = None
2014-01-28 22:51:59 +04:00
self . _populated = False
self . _init_ui ( )
2018-10-06 21:26:31 +03:00
self . _iso_rows = [ ]
self . _cdrom_rows = [ ]
self . _floppy_rows = [ ]
self . _rows_inited = False
self . add_gsettings_handle (
self . config . on_iso_paths_changed ( self . _iso_paths_changed_cb ) )
2014-01-28 22:51:59 +04:00
def _cleanup ( self ) :
self . conn = None
self . top_box . destroy ( )
self . top_box = None
2015-04-07 20:56:48 +03:00
2014-01-28 22:51:59 +04:00
##########################
# Initialization methods #
##########################
def _init_ui ( self ) :
self . top_box = Gtk . Box ( )
self . top_box . set_spacing ( 6 )
self . top_box . set_orientation ( Gtk . Orientation . HORIZONTAL )
2018-10-06 21:26:31 +03:00
self . _combo = Gtk . ComboBox ( has_entry = True )
self . _combo . set_entry_text_column ( self . MEDIA_FIELD_LABEL )
self . _combo . get_accessible ( ) . set_name ( " media-combo " )
def separator_cb ( _model , _iter ) :
return _model [ _iter ] [ self . MEDIA_FIELD_PATH ] is None
self . _combo . set_row_separator_func ( separator_cb )
self . _entry = self . _combo . get_child ( )
self . _entry . set_placeholder_text ( _ ( " No media selected " ) )
self . _entry . set_hexpand ( True )
self . _entry . get_accessible ( ) . set_name ( " media-entry " )
self . _entry . connect ( " changed " , self . _on_entry_changed_cb )
self . _entry . connect ( " activate " , self . _on_entry_activated_cb )
self . _entry . connect ( " icon-press " , self . _on_entry_icon_press_cb )
self . _browse = Gtk . Button ( )
self . top_box . add ( self . _combo )
2014-01-28 22:51:59 +04:00
self . top_box . show_all ( )
2018-10-06 21:26:31 +03:00
# [path, label, has_media?, device key]
store = Gtk . ListStore ( str , str , bool , str )
self . _combo . set_model ( store )
2014-01-28 22:51:59 +04:00
2018-10-06 21:26:31 +03:00
def _make_row ( self , path , label , has_media , key ) :
row = [ None ] * self . MEDIA_FIELDS_NUM
row [ self . MEDIA_FIELD_PATH ] = path
row [ self . MEDIA_FIELD_LABEL ] = label
row [ self . MEDIA_FIELD_HAS_MEDIA ] = has_media
row [ self . MEDIA_FIELD_KEY ] = key
return row
2014-01-28 22:51:59 +04:00
2018-10-06 21:26:31 +03:00
def _make_nodedev_rows ( self , media_type ) :
rows = [ ]
2019-06-05 21:45:48 +03:00
for nodedev in self . conn . filter_nodedevs ( " storage " ) :
2015-04-07 21:12:00 +03:00
if not ( nodedev . xmlobj . device_type == " storage " and
nodedev . xmlobj . drive_type in [ " cdrom " , " floppy " ] ) :
2015-04-07 20:56:48 +03:00
continue
2018-10-06 21:26:31 +03:00
if nodedev . xmlobj . drive_type != media_type :
2015-04-07 20:56:48 +03:00
continue
2020-08-20 20:34:01 +03:00
media_label = nodedev . xmlobj . media_label or _ ( " Media Unknown " )
2018-10-06 21:26:31 +03:00
if not nodedev . xmlobj . media_available :
media_label = _ ( " No media detected " )
label = " %s ( %s ) " % ( media_label , nodedev . xmlobj . block )
row = self . _make_row ( nodedev . xmlobj . block , label ,
nodedev . xmlobj . media_available ,
nodedev . xmlobj . name )
rows . append ( row )
return rows
def _make_iso_rows ( self ) :
rows = [ ]
for path in self . config . get_iso_paths ( ) :
row = self . _make_row ( path , path , True , path )
rows . append ( row )
return rows
def _init_rows ( self ) :
self . _cdrom_rows = self . _make_nodedev_rows ( " cdrom " )
self . _floppy_rows = self . _make_nodedev_rows ( " floppy " )
self . _iso_rows = self . _make_iso_rows ( )
self . _rows_inited = True
2015-04-07 20:56:48 +03:00
2014-01-28 22:51:59 +04:00
2018-10-06 21:26:31 +03:00
################
# UI callbacks #
################
def _on_entry_changed_cb ( self , src ) :
self . emit ( " changed " , self . _entry )
def _on_entry_activated_cb ( self , src ) :
self . emit ( " activate " , self . _entry )
def _on_entry_icon_press_cb ( self , src , icon_pos , event ) :
self . _entry . set_text ( " " )
def _iso_paths_changed_cb ( self ) :
self . _iso_rows = self . _make_iso_rows ( )
2014-01-28 22:51:59 +04:00
##############
# Public API #
##############
2018-10-06 21:26:31 +03:00
def set_conn ( self , conn ) :
if conn == self . conn :
return
self . conn = conn
self . _init_rows ( )
def reset_state ( self , is_floppy = False ) :
if not self . _rows_inited :
self . _init_rows ( )
2020-09-30 17:33:18 +03:00
self . _entry . set_text ( " " )
2018-10-06 21:26:31 +03:00
model = self . _combo . get_model ( )
model . clear ( )
for row in self . _iso_rows :
model . append ( row )
nodedev_rows = self . _cdrom_rows
if is_floppy :
nodedev_rows = self . _floppy_rows
if len ( model ) and nodedev_rows :
model . append ( self . _make_row ( None , None , False , None ) )
for row in nodedev_rows :
model . append ( row )
self . _combo . set_active ( - 1 )
def get_path ( self , store_media = True ) :
ret = uiutil . get_list_selection (
self . _combo , column = self . MEDIA_FIELD_PATH )
if store_media and not ret . startswith ( " /dev " ) :
self . config . add_iso_path ( ret )
return ret
def set_path ( self , path ) :
uiutil . set_list_selection (
self . _combo , path , column = self . MEDIA_FIELD_PATH )
self . _entry . set_position ( - 1 )
2014-01-28 22:51:59 +04:00
2018-10-06 21:26:31 +03:00
def set_mnemonic_label ( self , label ) :
label . set_mnemonic_widget ( self . _entry )
2014-01-28 22:51:59 +04:00
2018-10-06 21:26:31 +03:00
def show_clear_icon ( self ) :
pos = Gtk . EntryIconPosition . SECONDARY
self . _entry . set_icon_from_icon_name ( pos , " edit-clear-symbolic " )
self . _entry . set_icon_activatable ( pos , True )