opticalhelper: Clean up signals
This commit is contained in:
parent
8c320509ed
commit
18d331a241
|
@ -23,12 +23,13 @@ import gobject
|
|||
class vmmMediaDevice(gobject.GObject):
|
||||
__gsignals__ = {}
|
||||
|
||||
def __init__(self, path, key, media_label=None):
|
||||
def __init__(self, path, key, media_label, media_key):
|
||||
self.__gobject_init__()
|
||||
|
||||
self.path = path
|
||||
self.key = key
|
||||
self.media_label = media_label
|
||||
self.media_key = media_key
|
||||
|
||||
def get_path(self):
|
||||
return self.path
|
||||
|
@ -41,9 +42,15 @@ class vmmMediaDevice(gobject.GObject):
|
|||
|
||||
def get_media_label(self):
|
||||
return self.media_label
|
||||
def get_media_key(self):
|
||||
return self.media_key
|
||||
|
||||
def set_media_label(self, media_label):
|
||||
def set_media(self, media_label, media_key):
|
||||
self.media_label = media_label
|
||||
self.media_key = media_key
|
||||
def clear_media(self):
|
||||
self.media_label = None
|
||||
self.media_key = None
|
||||
|
||||
def pretty_label(self):
|
||||
media_label = self.get_media_label()
|
||||
|
|
|
@ -20,7 +20,6 @@
|
|||
import gobject
|
||||
import dbus
|
||||
import logging
|
||||
import gtk
|
||||
|
||||
from virtManager.mediadev import vmmMediaDevice
|
||||
|
||||
|
@ -28,8 +27,10 @@ class vmmOpticalDriveHelper(gobject.GObject):
|
|||
__gsignals__ = {
|
||||
"optical-added" : (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
||||
[object]),
|
||||
"optical-removed": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
||||
[str]),
|
||||
"optical-media-added" : (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
||||
[object]),
|
||||
"device-removed": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
||||
[str]),
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
|
@ -74,64 +75,93 @@ class vmmOpticalDriveHelper(gobject.GObject):
|
|||
def populate_opt_media(self):
|
||||
volinfo = {}
|
||||
|
||||
# Find info about all current present media
|
||||
for path in self.hal_iface.FindDeviceByCapability("volume"):
|
||||
label, devnode = self._fetch_device_info(path)
|
||||
|
||||
if not devnode:
|
||||
# Not an applicable device
|
||||
continue
|
||||
|
||||
volinfo[devnode] = (label, path)
|
||||
|
||||
for path in self.hal_iface.FindDeviceByCapability("storage.cdrom"):
|
||||
# Make sure we only populate CDROM devs
|
||||
dev = self.bus.get_object("org.freedesktop.Hal", path)
|
||||
devif = dbus.Interface(dev, "org.freedesktop.Hal.Device")
|
||||
devnode = devif.GetProperty("block.device")
|
||||
if not self.is_cdrom(path):
|
||||
continue
|
||||
|
||||
if volinfo.has_key(devnode):
|
||||
label, path = volinfo[devnode]
|
||||
else:
|
||||
label, path = None, None
|
||||
|
||||
obj = vmmMediaDevice(str(devnode), str(path), label)
|
||||
devnode, media_label, media_hal_path = self._fetch_cdrom_info(path)
|
||||
obj = vmmMediaDevice(str(devnode), str(path), media_label,
|
||||
media_hal_path)
|
||||
|
||||
self.device_info[str(devnode)] = obj
|
||||
|
||||
def _device_added(self, path):
|
||||
label, devnode = self._fetch_device_info(path)
|
||||
media_label = None
|
||||
media_hal_path = None
|
||||
devpath = None
|
||||
signal = None
|
||||
|
||||
if not devnode:
|
||||
# Not an applicable device
|
||||
if self.is_cdrom_media(path):
|
||||
media_hal_path = path
|
||||
media_label, devpath = self._fetch_media_info(path)
|
||||
signal = "optical-media-added"
|
||||
elif self.is_cdrom(path):
|
||||
devpath, media_label, media_hal_path = self._fetch_cdrom_info(path)
|
||||
signal = "optical-added"
|
||||
else:
|
||||
# Not a relevant device
|
||||
return
|
||||
|
||||
obj = vmmMediaDevice(str(devnode), str(path), str(label))
|
||||
self.device_info[str(devnode)] = obj
|
||||
obj = vmmMediaDevice(devpath, path, media_label, media_hal_path)
|
||||
self.device_info[devpath] = obj
|
||||
|
||||
logging.debug("Optical device added: %s" % obj.pretty_label())
|
||||
self.emit("optical-added", obj)
|
||||
self.emit(signal, obj)
|
||||
|
||||
def _device_removed(self, path):
|
||||
logging.debug("Optical device removed: %s" % str(path))
|
||||
self.emit("optical-removed", str(path))
|
||||
self.emit("device-removed", str(path))
|
||||
|
||||
def _fetch_device_info(self, path):
|
||||
def dbus_dev_lookup(self, halpath):
|
||||
obj = self.bus.get_object("org.freedesktop.Hal", halpath)
|
||||
objif = dbus.Interface(obj, "org.freedesktop.Hal.Device")
|
||||
return objif
|
||||
|
||||
def is_cdrom_media(self, halpath):
|
||||
obj = self.dbus_dev_lookup(halpath)
|
||||
return bool(obj.QueryCapability("volume") and
|
||||
obj.GetPropertyBoolean("volume.is_disc") and
|
||||
obj.GetPropertyBoolean("volume.disc.has_data"))
|
||||
|
||||
def is_cdrom(self, halpath):
|
||||
obj = self.dbus_dev_lookup(halpath)
|
||||
return bool(obj.QueryCapability("storage.cdrom"))
|
||||
|
||||
|
||||
def _fetch_media_info(self, halpath):
|
||||
label = None
|
||||
devnode = None
|
||||
|
||||
vol = self.bus.get_object("org.freedesktop.Hal", path)
|
||||
volif = dbus.Interface(vol, "org.freedesktop.Hal.Device")
|
||||
if volif.QueryCapability("volume"):
|
||||
volif = self.dbus_dev_lookup(halpath)
|
||||
|
||||
if (volif.GetPropertyBoolean("volume.is_disc") and
|
||||
volif.GetPropertyBoolean("volume.disc.has_data")):
|
||||
|
||||
devnode = volif.GetProperty("block.device")
|
||||
label = volif.GetProperty("volume.label")
|
||||
if label == None or len(label) == 0:
|
||||
label = devnode
|
||||
devnode = volif.GetProperty("block.device")
|
||||
label = volif.GetProperty("volume.label")
|
||||
if not label:
|
||||
label = devnode
|
||||
|
||||
return (label and str(label), devnode and str(devnode))
|
||||
|
||||
def _fetch_cdrom_info(self, halpath):
|
||||
devif = self.dbus_dev_lookup(halpath)
|
||||
|
||||
devnode = devif.GetProperty("block.device")
|
||||
media_label = None
|
||||
media_hal_path = None
|
||||
|
||||
if devnode:
|
||||
media_label, media_hal_path = self._find_media_for_devpath(devnode)
|
||||
|
||||
return (devnode and str(devnode), media_label, media_hal_path)
|
||||
|
||||
def _find_media_for_devpath(self, devpath):
|
||||
for path in self.hal_iface.FindDeviceByCapability("volume"):
|
||||
if not self.is_cdrom_media(path):
|
||||
continue
|
||||
|
||||
label, devnode = self._fetch_media_info(path)
|
||||
|
||||
if devnode == devpath:
|
||||
return (label, path)
|
||||
|
||||
return None, None
|
||||
|
||||
gobject.type_register(vmmOpticalDriveHelper)
|
||||
|
|
|
@ -28,11 +28,12 @@ from virtinst import VirtualNetworkInterface
|
|||
from virtManager.opticalhelper import vmmOpticalDriveHelper
|
||||
from virtManager.error import vmmErrorDialog
|
||||
|
||||
OPTICAL_PATH = 0
|
||||
OPTICAL_DEV_PATH = 0
|
||||
OPTICAL_LABEL = 1
|
||||
OPTICAL_IS_MEDIA_PRESENT = 2
|
||||
OPTICAL_HAL_PATH = 3
|
||||
OPTICAL_MEDIADEV = 4
|
||||
OPTICAL_DEV_KEY = 3
|
||||
OPTICAL_MEDIA_KEY = 4
|
||||
OPTICAL_MEDIADEV = 5
|
||||
|
||||
##############################################################
|
||||
# Initialize an error object to use for validation functions #
|
||||
|
@ -251,8 +252,9 @@ def generate_macaddr(conn):
|
|||
##############################################
|
||||
|
||||
def init_optical_combo(widget, empty_sensitive=False):
|
||||
# [Device path, pretty label, has_media?, unique hal path, vmmMediaDevice]
|
||||
model = gtk.ListStore(str, str, bool, str, object)
|
||||
# [Device path, pretty label, has_media?, device key, media key,
|
||||
# vmmMediaDevice]
|
||||
model = gtk.ListStore(str, str, bool, str, str, object)
|
||||
widget.set_model(model)
|
||||
model.clear()
|
||||
|
||||
|
@ -264,48 +266,80 @@ def init_optical_combo(widget, empty_sensitive=False):
|
|||
|
||||
helper = vmmOpticalDriveHelper()
|
||||
helper.connect("optical-added", optical_added, widget)
|
||||
helper.connect("optical-removed", optical_removed, widget)
|
||||
helper.connect("optical-media-added", optical_media_added, widget)
|
||||
helper.connect("device-removed", optical_removed, widget)
|
||||
|
||||
widget.set_active(-1)
|
||||
optical_set_default_selection(widget)
|
||||
|
||||
def set_row_from_object(row):
|
||||
obj = row[OPTICAL_MEDIADEV]
|
||||
row[OPTICAL_PATH] = obj.get_path()
|
||||
row[OPTICAL_DEV_PATH] = obj.get_path()
|
||||
row[OPTICAL_LABEL] = obj.pretty_label()
|
||||
row[OPTICAL_IS_MEDIA_PRESENT] = bool(obj.get_media_label())
|
||||
row[OPTICAL_HAL_PATH] = obj.get_key()
|
||||
row[OPTICAL_DEV_KEY] = obj.get_key()
|
||||
row[OPTICAL_MEDIA_KEY] = obj.get_media_key()
|
||||
|
||||
def optical_removed(ignore_helper, halpath, widget):
|
||||
def optical_removed(ignore_helper, key, widget):
|
||||
model = widget.get_model()
|
||||
active = widget.get_active()
|
||||
idx = 0
|
||||
# Search for the row containing matching HAL volume path
|
||||
# and update (clear) it, de-activating it if its currently
|
||||
# selected
|
||||
|
||||
# Search for the row containing matching media key and update
|
||||
# (clear) it, de-activating it if its currently selected
|
||||
for row in model:
|
||||
if row[OPTICAL_HAL_PATH] == halpath:
|
||||
row[OPTICAL_MEDIADEV].set_media_label(None)
|
||||
if row[OPTICAL_MEDIA_KEY] == key:
|
||||
row[OPTICAL_MEDIADEV].clear_media()
|
||||
set_row_from_object(row)
|
||||
|
||||
if idx == active:
|
||||
widget.set_active(-1)
|
||||
idx = idx + 1
|
||||
|
||||
elif row[OPTICAL_DEV_KEY] == key:
|
||||
# Whole device removed
|
||||
del(model[idx])
|
||||
widget.set_active(-1)
|
||||
|
||||
idx += 1
|
||||
|
||||
optical_set_default_selection(widget)
|
||||
|
||||
def remove_row_from_model(widget, rmrow):
|
||||
active = widget.get_active()
|
||||
model = widget.get_model()
|
||||
newmodel = []
|
||||
|
||||
for row in model:
|
||||
if row != rmrow:
|
||||
newmodel.append(row)
|
||||
|
||||
model.clear()
|
||||
for row in newmodel:
|
||||
print row
|
||||
model.append(row)
|
||||
|
||||
widget.set_active(-1)
|
||||
|
||||
def optical_added(ignore_helper, newobj, widget):
|
||||
model = widget.get_model()
|
||||
active = widget.get_active()
|
||||
idx = 0
|
||||
found = False
|
||||
|
||||
# Brand new device
|
||||
row = [None, None, None, None, None, newobj]
|
||||
set_row_from_object(row)
|
||||
model.append(row)
|
||||
|
||||
def optical_media_added(ignore_helper, newobj, widget):
|
||||
model = widget.get_model()
|
||||
active = widget.get_active()
|
||||
idx = 0
|
||||
|
||||
# Search for the row with matching device node and
|
||||
# fill in info about inserted media. If model has no current
|
||||
# selection, select the new media.
|
||||
for row in model:
|
||||
if row[OPTICAL_PATH] == newobj.get_path():
|
||||
found = True
|
||||
if row[OPTICAL_DEV_PATH] == newobj.get_path():
|
||||
row[OPTICAL_MEDIADEV] = newobj
|
||||
set_row_from_object(row)
|
||||
|
||||
|
@ -314,14 +348,6 @@ def optical_added(ignore_helper, newobj, widget):
|
|||
|
||||
idx = idx + 1
|
||||
|
||||
if not found:
|
||||
# Brand new device
|
||||
row = [None, None, None, None, newobj]
|
||||
set_row_from_object(row)
|
||||
model.append(row)
|
||||
if active == -1:
|
||||
widget.set_active(len(model) - 1)
|
||||
|
||||
def optical_set_default_selection(widget):
|
||||
# Set the first active cdrom device as selected, otherwise none
|
||||
model = widget.get_model()
|
||||
|
|
Loading…
Reference in New Issue