2014-01-28 13:51:59 -05:00
# Copyright (C) 2014 Red Hat, Inc.
#
2018-03-20 15:00:02 -04:00
# This work is licensed under the GNU GPLv2.
# See the COPYING file in the top-level directory.
2014-01-28 13:51:59 -05:00
2015-04-13 15:44:46 -04:00
import logging
2014-01-28 13:51:59 -05:00
from gi . repository import Gtk
2014-09-12 16:10:45 -04:00
from . import uiutil
from . baseclass import vmmGObjectUI
2014-01-28 13:51:59 -05:00
class vmmMediaCombo ( vmmGObjectUI ) :
2015-04-07 13:56:48 -04:00
MEDIA_FLOPPY = " floppy "
MEDIA_CDROM = " cdrom "
2014-01-28 13:51:59 -05:00
OPTICAL_FIELDS = 4
( OPTICAL_DEV_PATH ,
OPTICAL_LABEL ,
OPTICAL_HAS_MEDIA ,
OPTICAL_DEV_KEY ) = range ( OPTICAL_FIELDS )
def __init__ ( self , conn , builder , topwin , media_type ) :
vmmGObjectUI . __init__ ( self , None , None , builder = builder , topwin = topwin )
self . conn = conn
self . media_type = media_type
self . top_box = None
self . combo = None
self . _warn_icon = None
self . _populated = False
self . _init_ui ( )
def _cleanup ( self ) :
self . conn = None
self . top_box . destroy ( )
self . top_box = None
2015-04-07 13:56:48 -04:00
2014-01-28 13:51:59 -05: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 )
self . _warn_icon = Gtk . Image ( )
self . _warn_icon . set_from_stock (
Gtk . STOCK_DIALOG_WARNING , Gtk . IconSize . MENU )
self . combo = Gtk . ComboBox ( )
self . top_box . add ( self . combo )
self . top_box . add ( self . _warn_icon )
self . top_box . show_all ( )
2015-04-07 13:56:48 -04:00
# [Device path, pretty label, has_media?, device key]
2014-01-28 13:51:59 -05:00
fields = [ ]
fields . insert ( self . OPTICAL_DEV_PATH , str )
fields . insert ( self . OPTICAL_LABEL , str )
fields . insert ( self . OPTICAL_HAS_MEDIA , bool )
fields . insert ( self . OPTICAL_DEV_KEY , str )
self . combo . set_model ( Gtk . ListStore ( * fields ) )
text = Gtk . CellRendererText ( )
self . combo . pack_start ( text , True )
self . combo . add_attribute ( text , ' text ' , self . OPTICAL_LABEL )
2018-01-19 17:34:32 -05:00
self . combo . get_accessible ( ) . set_name ( " physical-device-combo " )
2014-01-28 13:51:59 -05:00
2015-04-07 13:56:48 -04:00
error = None
if not self . conn . is_nodedev_capable ( ) :
error = _ ( " Libvirt version does not support media listing. " )
2014-01-28 13:51:59 -05:00
self . _warn_icon . set_tooltip_text ( error )
2015-04-07 13:56:48 -04:00
self . _warn_icon . set_visible ( bool ( error ) )
2014-01-28 13:51:59 -05:00
def _set_mediadev_default ( self ) :
model = self . combo . get_model ( )
if len ( model ) != 0 :
return
row = [ None ] * self . OPTICAL_FIELDS
row [ self . OPTICAL_DEV_PATH ] = None
row [ self . OPTICAL_LABEL ] = _ ( " No device present " )
row [ self . OPTICAL_HAS_MEDIA ] = False
row [ self . OPTICAL_DEV_KEY ] = None
model . append ( row )
2015-04-07 13:56:48 -04:00
def _pretty_label ( self , nodedev ) :
2015-04-07 14:12:00 -04:00
media_label = nodedev . xmlobj . media_label
if not nodedev . xmlobj . media_available :
2015-04-07 13:56:48 -04:00
media_label = _ ( " No media detected " )
2015-04-07 14:12:00 -04:00
elif not nodedev . xmlobj . media_label :
2015-04-07 13:56:48 -04:00
media_label = _ ( " Media Unknown " )
2015-04-07 14:12:00 -04:00
return " %s ( %s ) " % ( media_label , nodedev . xmlobj . block )
2014-01-28 13:51:59 -05:00
def _mediadev_set_default_selection ( self ) :
# Set the first active cdrom device as selected, otherwise none
widget = self . combo
model = widget . get_model ( )
idx = 0
active = widget . get_active ( )
if active != - 1 :
# already a selection, don't change it
return
for row in model :
if row [ self . OPTICAL_HAS_MEDIA ] is True :
widget . set_active ( idx )
return
idx + = 1
widget . set_active ( 0 )
def _populate_media ( self ) :
if self . _populated :
return
widget = self . combo
model = widget . get_model ( )
model . clear ( )
2015-04-10 09:37:03 -04:00
for nodedev in self . conn . filter_nodedevs ( devtype = " storage " ) :
2015-04-07 14:12:00 -04:00
if not ( nodedev . xmlobj . device_type == " storage " and
nodedev . xmlobj . drive_type in [ " cdrom " , " floppy " ] ) :
2015-04-07 13:56:48 -04:00
continue
2015-04-07 14:12:00 -04:00
if nodedev . xmlobj . drive_type != self . media_type :
2015-04-07 13:56:48 -04:00
continue
row = [ None ] * self . OPTICAL_FIELDS
2015-04-07 14:12:00 -04:00
row [ self . OPTICAL_DEV_PATH ] = nodedev . xmlobj . block
2015-04-07 13:56:48 -04:00
row [ self . OPTICAL_LABEL ] = self . _pretty_label ( nodedev )
2015-04-07 14:12:00 -04:00
row [ self . OPTICAL_HAS_MEDIA ] = nodedev . xmlobj . media_available
row [ self . OPTICAL_DEV_KEY ] = nodedev . xmlobj . name
2015-04-07 13:56:48 -04:00
model . append ( row )
self . _set_mediadev_default ( )
2014-01-28 13:51:59 -05:00
widget . set_active ( - 1 )
self . _mediadev_set_default_selection ( )
self . _populated = True
##############
# Public API #
##############
def reset_state ( self ) :
2015-04-13 15:44:46 -04:00
try :
self . _populate_media ( )
2017-07-24 09:26:48 +01:00
except Exception :
2015-04-13 15:44:46 -04:00
logging . debug ( " Error populating mediadev combo " , exc_info = True )
2014-01-28 13:51:59 -05:00
def get_path ( self ) :
2015-05-19 17:17:53 -04:00
return uiutil . get_list_selection (
self . combo , column = self . OPTICAL_DEV_PATH )
2014-01-28 13:51:59 -05:00
def has_media ( self ) :
2015-05-19 17:17:53 -04:00
return uiutil . get_list_selection (
2015-06-06 14:23:23 -04:00
self . combo , column = self . OPTICAL_HAS_MEDIA ) or False