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