2014-01-28 22:51:59 +04:00
#
# Copyright (C) 2014 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
#
2015-04-13 22:44:46 +03:00
import logging
2014-01-28 22:51:59 +04:00
from gi . repository import Gtk
2014-09-13 00:10:45 +04:00
from . import uiutil
from . baseclass import vmmGObjectUI
2014-01-28 22:51:59 +04:00
class vmmMediaCombo ( vmmGObjectUI ) :
2015-04-07 20:56:48 +03:00
MEDIA_FLOPPY = " floppy "
MEDIA_CDROM = " cdrom "
2014-01-28 22:51:59 +04: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 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 )
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 20:56:48 +03:00
# [Device path, pretty label, has_media?, device key]
2014-01-28 22:51:59 +04: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-20 01:34:32 +03:00
self . combo . get_accessible ( ) . set_name ( " physical-device-combo " )
2014-01-28 22:51:59 +04:00
2015-04-07 20:56:48 +03:00
error = None
if not self . conn . is_nodedev_capable ( ) :
error = _ ( " Libvirt version does not support media listing. " )
2014-01-28 22:51:59 +04:00
self . _warn_icon . set_tooltip_text ( error )
2015-04-07 20:56:48 +03:00
self . _warn_icon . set_visible ( bool ( error ) )
2014-01-28 22:51:59 +04: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 20:56:48 +03:00
def _pretty_label ( self , nodedev ) :
2015-04-07 21:12:00 +03:00
media_label = nodedev . xmlobj . media_label
if not nodedev . xmlobj . media_available :
2015-04-07 20:56:48 +03:00
media_label = _ ( " No media detected " )
2015-04-07 21:12:00 +03:00
elif not nodedev . xmlobj . media_label :
2015-04-07 20:56:48 +03:00
media_label = _ ( " Media Unknown " )
2015-04-07 21:12:00 +03:00
return " %s ( %s ) " % ( media_label , nodedev . xmlobj . block )
2014-01-28 22:51:59 +04: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 16:37:03 +03:00
for nodedev in self . conn . filter_nodedevs ( devtype = " 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
2015-04-07 21:12:00 +03:00
if nodedev . xmlobj . drive_type != self . media_type :
2015-04-07 20:56:48 +03:00
continue
row = [ None ] * self . OPTICAL_FIELDS
2015-04-07 21:12:00 +03:00
row [ self . OPTICAL_DEV_PATH ] = nodedev . xmlobj . block
2015-04-07 20:56:48 +03:00
row [ self . OPTICAL_LABEL ] = self . _pretty_label ( nodedev )
2015-04-07 21:12:00 +03:00
row [ self . OPTICAL_HAS_MEDIA ] = nodedev . xmlobj . media_available
row [ self . OPTICAL_DEV_KEY ] = nodedev . xmlobj . name
2015-04-07 20:56:48 +03:00
model . append ( row )
self . _set_mediadev_default ( )
2014-01-28 22:51:59 +04:00
widget . set_active ( - 1 )
self . _mediadev_set_default_selection ( )
self . _populated = True
##############
# Public API #
##############
def reset_state ( self ) :
2015-04-13 22:44:46 +03:00
try :
self . _populate_media ( )
2017-07-24 11:26:48 +03:00
except Exception :
2015-04-13 22:44:46 +03:00
logging . debug ( " Error populating mediadev combo " , exc_info = True )
2014-01-28 22:51:59 +04:00
def get_path ( self ) :
2015-05-20 00:17:53 +03:00
return uiutil . get_list_selection (
self . combo , column = self . OPTICAL_DEV_PATH )
2014-01-28 22:51:59 +04:00
def has_media ( self ) :
2015-05-20 00:17:53 +03:00
return uiutil . get_list_selection (
2015-06-06 21:23:23 +03:00
self . combo , column = self . OPTICAL_HAS_MEDIA ) or False