2009-12-01 00:20:06 +03:00
#
# Copyright (C) 2009 Red Hat, Inc.
# Copyright (C) 2009 Cole Robinson <crobinso@redhat.com>
#
# 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.
#
2013-04-12 01:16:33 +04:00
# pylint: disable=E0611
2012-05-14 17:24:56 +04:00
from gi . repository import GObject
2013-04-12 01:16:33 +04:00
# pylint: enable=E0611
2012-05-14 17:24:56 +04:00
2009-12-01 00:16:43 +03:00
import logging
2013-09-23 02:13:24 +04:00
from virtinst import NodeDevice
2009-12-01 00:16:43 +03:00
2010-12-09 20:37:48 +03:00
from virtManager . baseclass import vmmGObject
2010-02-11 20:32:00 +03:00
2009-12-11 04:04:26 +03:00
MEDIA_FLOPPY = " floppy "
MEDIA_CDROM = " cdrom "
2013-04-13 22:34:52 +04:00
2010-12-09 20:37:48 +03:00
class vmmMediaDevice ( vmmGObject ) :
2012-05-14 17:24:56 +04:00
__gsignals__ = {
" media-added " : ( GObject . SignalFlags . RUN_FIRST , None , [ ] ) ,
" media-removed " : ( GObject . SignalFlags . RUN_FIRST , None , [ ] ) ,
}
2009-12-01 00:16:43 +03:00
@staticmethod
2011-04-10 05:56:05 +04:00
def mediadev_from_nodedev ( dev ) :
2013-09-23 16:34:50 +04:00
nodedev = dev . get_xmlobj ( )
2011-04-10 03:32:30 +04:00
2009-12-01 00:16:43 +03:00
if nodedev . device_type != " storage " :
return None
2010-12-10 19:47:07 +03:00
if nodedev . drive_type not in [ MEDIA_CDROM , MEDIA_FLOPPY ] :
2009-12-01 00:16:43 +03:00
return None
2009-12-11 04:04:26 +03:00
drvtype = nodedev . drive_type
2009-12-01 00:16:43 +03:00
path = nodedev . block
key = nodedev . name
has_media = nodedev . media_available
2009-12-02 00:33:58 +03:00
media_label = nodedev . media_label
2009-12-01 00:16:43 +03:00
media_key = None
obj = vmmMediaDevice ( path , key , has_media , media_label , media_key ,
2011-04-10 03:32:30 +04:00
dev , drvtype )
2012-02-08 23:15:15 +04:00
obj . do_poll = True
2009-12-01 00:16:43 +03:00
return obj
def __init__ ( self , path , key , has_media , media_label , media_key ,
2010-12-10 19:47:07 +03:00
nodedev_obj = None , media_type = MEDIA_CDROM ) :
2010-12-09 20:37:48 +03:00
vmmGObject . __init__ ( self )
2009-12-01 00:20:06 +03:00
self . path = path
self . key = key
2009-12-01 00:16:43 +03:00
self . _has_media = has_media
2009-12-01 00:20:06 +03:00
self . media_label = media_label
2009-12-01 00:22:20 +03:00
self . media_key = media_key
2009-12-11 04:04:26 +03:00
self . media_type = media_type
2009-12-01 00:20:06 +03:00
2009-12-01 00:16:43 +03:00
self . nodedev_obj = nodedev_obj
2012-02-08 23:15:15 +04:00
self . do_poll = False
self . last_tick = 0
2009-12-01 00:16:43 +03:00
2011-07-24 05:16:54 +04:00
def _cleanup ( self ) :
pass
2009-12-01 00:20:06 +03:00
def get_path ( self ) :
return self . path
def get_key ( self ) :
return self . key
2009-12-11 04:04:26 +03:00
def get_media_type ( self ) :
return self . media_type
2009-12-01 00:20:06 +03:00
def has_media ( self ) :
2009-12-01 00:16:43 +03:00
return self . _has_media
2009-12-01 00:20:06 +03:00
def get_media_label ( self ) :
return self . media_label
2009-12-01 00:22:20 +03:00
def get_media_key ( self ) :
return self . media_key
2009-12-01 00:20:06 +03:00
2009-12-01 00:16:43 +03:00
def set_media ( self , has_media , media_label , media_key ) :
self . _has_media = has_media
2009-12-01 00:20:06 +03:00
self . media_label = media_label
2009-12-01 00:22:20 +03:00
self . media_key = media_key
def clear_media ( self ) :
2009-12-01 00:16:43 +03:00
self . set_media ( None , None , None )
2009-12-01 00:20:06 +03:00
def pretty_label ( self ) :
media_label = self . get_media_label ( )
2009-12-01 00:16:43 +03:00
has_media = self . has_media ( )
if not has_media :
2010-02-12 21:52:33 +03:00
media_label = _ ( " No media detected " )
2009-12-02 00:33:58 +03:00
elif not media_label :
2009-12-01 00:16:43 +03:00
media_label = _ ( " Media Unknown " )
2009-12-01 00:20:06 +03:00
return " %s ( %s ) " % ( media_label , self . get_path ( ) )
2009-11-30 18:50:04 +03:00
2009-12-01 00:16:43 +03:00
#########################################
# Nodedev API polling for media updates #
#########################################
2012-02-08 23:15:15 +04:00
def tick ( self ) :
2009-12-01 00:16:43 +03:00
if not self . nodedev_obj :
2012-02-08 23:15:15 +04:00
return
2011-07-23 00:43:26 +04:00
if not self . nodedev_obj . conn . is_active ( ) :
2012-02-08 23:15:15 +04:00
return
2009-12-01 00:16:43 +03:00
try :
2011-04-10 03:32:30 +04:00
self . nodedev_obj . refresh_xml ( )
xml = self . nodedev_obj . get_xml ( )
2009-12-01 00:16:43 +03:00
except :
# Assume the device was removed
2012-02-08 23:15:15 +04:00
return
2009-12-01 00:16:43 +03:00
try :
2013-09-23 02:13:24 +04:00
vobj = NodeDevice . parse ( self . nodedev_obj . conn . get_backend ( ) , xml )
has_media = vobj . media_available or False
2009-12-01 00:16:43 +03:00
except :
logging . exception ( " Node device CDROM polling failed " )
2012-02-08 23:15:15 +04:00
return
if has_media == self . has_media ( ) :
return
2009-12-01 00:16:43 +03:00
2012-02-08 23:15:15 +04:00
self . set_media ( has_media , None , None )
self . idle_emit ( has_media and " media-added " or " media-removed " )