summaryrefslogtreecommitdiffstats
path: root/libk3bdevice/k3bhalconnection.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libk3bdevice/k3bhalconnection.cpp')
-rw-r--r--libk3bdevice/k3bhalconnection.cpp303
1 files changed, 302 insertions, 1 deletions
diff --git a/libk3bdevice/k3bhalconnection.cpp b/libk3bdevice/k3bhalconnection.cpp
index ca12457..1877d78 100644
--- a/libk3bdevice/k3bhalconnection.cpp
+++ b/libk3bdevice/k3bhalconnection.cpp
@@ -2,9 +2,11 @@
*
* $Id: sourceheader,v 1.3 2005/01/19 13:03:46 trueg Exp $
* Copyright (C) 2005-2007 Sebastian Trueg <trueg@k3b.org>
+ * Copyright (C) 2013 Timothy Pearson <kb9vqf@pearsoncomputing.net>
*
* This file is part of the K3b project.
* Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
+ * Copyright (C) 2013 Timothy Pearson <kb9vqf@pearsoncomputing.net>
*
* 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
@@ -21,13 +23,17 @@
#include <tqtimer.h>
+#ifdef HAVE_HAL
// We acknowledge the the dbus API is unstable
#define DBUS_API_SUBJECT_TO_CHANGE
#include <dbus/connection.h>
#include <dbus/dbus.h>
#include <hal/libhal.h>
+#else // HAVE_HAL
+#include <tdehardwaredevices.h>
+#endif // HAVE_HAL
-
+#ifdef HAVE_HAL
static char** qstringListToArray( const TQStringList& s )
{
char** a = new char*[s.count()];
@@ -607,4 +613,299 @@ void K3bDevice::HalConnection::setupDBusTQtConnection( DBusConnection* dbusConne
d->dBusTQtConnection->dbus_connection_setup_with_qt_main( dbusConnection );
}
+#else // HAVE_HAL
+
+K3bDevice::HalConnection* K3bDevice::HalConnection::s_instance = 0;
+
+
+class K3bDevice::HalConnection::Private
+{
+ public:
+ Private()
+ : bOpen(false),
+ m_hwdevices(NULL) {
+ //
+ }
+
+ bool bOpen;
+ TDEHardwareDevices *m_hwdevices;
+
+ TQMap<TQString, TQString> udiDeviceMap;
+ TQMap<TQString, TQString> deviceUdiMap;
+
+ TQMap<TQString, bool> deviceMediumUdiMap;
+};
+
+
+K3bDevice::HalConnection* K3bDevice::HalConnection::instance()
+{
+ if (s_instance == 0) {
+ s_instance = new HalConnection(0);
+ }
+
+ if ((!s_instance->isConnected()) && (!s_instance->open())) {
+ k3bDebug() << "(K3bDevice::HalConnection) failed to initialize the TDE hardware backend." << endl;
+ }
+
+ return s_instance;
+}
+
+
+K3bDevice::HalConnection::HalConnection( TQObject* parent, const char* name )
+ : TQObject( parent, name )
+{
+ d = new Private();
+}
+
+
+K3bDevice::HalConnection::~HalConnection()
+{
+ s_instance = 0;
+ close();
+ delete d;
+}
+
+
+bool K3bDevice::HalConnection::isConnected() const
+{
+ return d->bOpen;
+}
+
+
+bool K3bDevice::HalConnection::open()
+{
+ // Initialize the TDE device manager
+ d->m_hwdevices = TDEGlobal::hardwareDevices();
+
+ // Connect device monitoring signals/slots
+ connect(d->m_hwdevices, TQT_SIGNAL(hardwareAdded(TDEGenericDevice*)), this, TQT_SLOT(AddDeviceHandler(TDEGenericDevice*)));
+ connect(d->m_hwdevices, TQT_SIGNAL(hardwareRemoved(TDEGenericDevice*)), this, TQT_SLOT(RemoveDeviceHandler(TDEGenericDevice*)));
+ connect(d->m_hwdevices, TQT_SIGNAL(hardwareUpdated(TDEGenericDevice*)), this, TQT_SLOT(ModifyDeviceHandler(TDEGenericDevice*)));
+
+ d->bOpen = true;
+
+ //
+ // Report all devices
+ //
+ TDEGenericHardwareList hwlist = d->m_hwdevices->listAllPhysicalDevices();
+ TDEGenericDevice *hwdevice;
+ for (hwdevice = hwlist.first(); hwdevice; hwdevice = hwlist.next()) {
+ AddDeviceHandler(hwdevice);
+ }
+
+ return true;
+}
+
+
+void K3bDevice::HalConnection::close()
+{
+ d->bOpen = false;
+}
+
+
+TQStringList K3bDevice::HalConnection::devices() const
+{
+ return TQStringList(d->udiDeviceMap.values());
+}
+
+void K3bDevice::HalConnection::AddDeviceHandler(TDEGenericDevice* hwdevice)
+{
+ if (hwdevice->type() != TDEGenericDeviceType::Disk) {
+ return;
+ }
+ TQString udi = hwdevice->uniqueID();
+ TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
+
+ if (sdevice->diskType() & TDEDiskDeviceType::Optical) {
+ TQString blockDevice = sdevice->deviceNode();
+ if (!blockDevice.isEmpty()) {
+ k3bDebug() << "Mapping udi " << udi << " to device " << blockDevice << endl;
+ d->udiDeviceMap[udi] = blockDevice;
+ d->deviceUdiMap[blockDevice] = udi;
+ emit deviceAdded(blockDevice);
+ // Check for medium
+ if (sdevice->mediaInserted()) {
+ d->deviceMediumUdiMap[blockDevice] = true;
+ emit mediumChanged(blockDevice);
+ }
+ }
+ }
+}
+
+void K3bDevice::HalConnection::RemoveDeviceHandler(TDEGenericDevice* hwdevice)
+{
+ if (hwdevice->type() != TDEGenericDeviceType::Disk) {
+ return;
+ }
+ TQString udi = hwdevice->uniqueID();
+ TQString blockDevice = hwdevice->deviceNode();
+
+ TQMapIterator<TQString, TQString> it = d->udiDeviceMap.find(udi);
+ if (it != d->udiDeviceMap.end()) {
+ k3bDebug() << "Unmapping udi " << udi << " from device " << it.data() << endl;
+ emit deviceRemoved(it.data());
+ d->udiDeviceMap.erase(it);
+ d->deviceUdiMap.erase(it.data());
+
+ if (d->deviceMediumUdiMap[blockDevice]) {
+ d->deviceMediumUdiMap[blockDevice] = false;
+ emit mediumChanged(blockDevice);
+ }
+ }
+}
+
+void K3bDevice::HalConnection::ModifyDeviceHandler(TDEGenericDevice* hwdevice)
+{
+ if (hwdevice->type() != TDEGenericDeviceType::Disk) {
+ return;
+ }
+ TQString udi = hwdevice->uniqueID();
+ TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
+ TQString blockDevice = hwdevice->deviceNode();
+
+ if (d->deviceMediumUdiMap[blockDevice] != sdevice->mediaInserted()) {
+ d->deviceMediumUdiMap[blockDevice] = sdevice->mediaInserted();
+ emit mediumChanged(blockDevice);
+ }
+}
+
+
+int K3bDevice::HalConnection::lock(Device* dev)
+{
+ if (!d->deviceUdiMap.contains(dev->blockDeviceName())) {
+ return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
+ }
+ TQString udi = d->deviceUdiMap[dev->blockDeviceName()];
+ TDEGenericDevice *hwdevice = d->m_hwdevices->findByUniqueID(udi);
+ if (!hwdevice) {
+ return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
+ }
+
+ if (hwdevice->type() != TDEGenericDeviceType::Disk) {
+ return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
+ }
+ TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
+
+ if (sdevice->lockDriveMedia(true)) {
+ return org_freedesktop_Hal_Success;
+ }
+ else {
+ return org_freedesktop_Hal_Device_Volume_InvalidEjectOption;
+ }
+}
+
+
+int K3bDevice::HalConnection::unlock(Device* dev)
+{
+ if (!d->deviceUdiMap.contains(dev->blockDeviceName())) {
+ return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
+ }
+ TQString udi = d->deviceUdiMap[dev->blockDeviceName()];
+ TDEGenericDevice *hwdevice = d->m_hwdevices->findByUniqueID(udi);
+ if (!hwdevice) {
+ return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
+ }
+
+ if (hwdevice->type() != TDEGenericDeviceType::Disk) {
+ return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
+ }
+ TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
+
+ if (sdevice->lockDriveMedia(false)) {
+ return org_freedesktop_Hal_Success;
+ }
+ else {
+ return org_freedesktop_Hal_Device_Volume_InvalidEjectOption;
+ }
+}
+
+
+int K3bDevice::HalConnection::mount( K3bDevice::Device* dev,
+ const TQString& mountPoint,
+ const TQString& fstype,
+ const TQStringList& options )
+{
+ if (!d->deviceUdiMap.contains(dev->blockDeviceName())) {
+ return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
+ }
+ TQString udi = d->deviceUdiMap[dev->blockDeviceName()];
+ TDEGenericDevice *hwdevice = d->m_hwdevices->findByUniqueID(udi);
+ if (!hwdevice) {
+ return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
+ }
+
+ if (hwdevice->type() != TDEGenericDeviceType::Disk) {
+ return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
+ }
+ TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
+
+ // FIXME
+ // Options from 'options' are not currently loaded into 'optionString'
+ TQString optionString;
+ TQString mountedPath = sdevice->mountDevice(mountPoint, optionString);
+ if (mountedPath.isNull()) {
+ return org_freedesktop_Hal_CommunicationError;
+ }
+ else {
+ return org_freedesktop_Hal_Success;
+ }
+}
+
+
+int K3bDevice::HalConnection::unmount(K3bDevice::Device* dev, const TQStringList& options)
+{
+ if (!d->deviceUdiMap.contains(dev->blockDeviceName())) {
+ return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
+ }
+ TQString udi = d->deviceUdiMap[dev->blockDeviceName()];
+ TDEGenericDevice *hwdevice = d->m_hwdevices->findByUniqueID(udi);
+ if (!hwdevice) {
+ return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
+ }
+
+ if (hwdevice->type() != TDEGenericDeviceType::Disk) {
+ return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
+ }
+ TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
+
+ // FIXME
+ // Options from 'options' are not currently loaded into 'optionString'
+ TQString optionString;
+
+ if (!sdevice->unmountDevice(NULL)) {
+ // Unmount failed!
+ return org_freedesktop_Hal_CommunicationError;
+ }
+ else {
+ return org_freedesktop_Hal_Success;
+ }
+}
+
+
+int K3bDevice::HalConnection::eject(K3bDevice::Device* dev, const TQStringList& options)
+{
+ if (!d->deviceUdiMap.contains(dev->blockDeviceName())) {
+ return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
+ }
+ TQString udi = d->deviceUdiMap[dev->blockDeviceName()];
+ TDEGenericDevice *hwdevice = d->m_hwdevices->findByUniqueID(udi);
+ if (!hwdevice) {
+ return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
+ }
+
+ if (hwdevice->type() != TDEGenericDeviceType::Disk) {
+ return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
+ }
+ TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
+
+ if (sdevice->ejectDriveMedia()) {
+ return org_freedesktop_Hal_Success;
+ }
+ else {
+ return org_freedesktop_Hal_Device_Volume_InvalidEjectOption;
+ }
+}
+
+#endif // HAVE_HAL
+
#include "k3bhalconnection.moc"