File indexing completed on 2025-03-23 04:28:11
0001 /* 0002 SPDX-FileCopyrightText: 2005-2009 Sebastian Trueg <trueg@k3b.org> 0003 SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "k3bhalconnection.h" 0009 #include "k3bdevice.h" 0010 #include "k3bdevice_i18n.h" 0011 0012 #include <Solid/Device> 0013 0014 #include <QDebug> 0015 #include <QDBusConnection> 0016 #include <QDBusInterface> 0017 0018 0019 Q_GLOBAL_STATIC( K3b::Device::HalConnection, s_instance ) 0020 0021 0022 namespace { 0023 0024 K3b::Device::HalConnection::ErrorCode toErrorCode( const QString& errorName ) 0025 { 0026 if( errorName == QLatin1String( "org.freedesktop.Hal.NoSuchDevice" ) ) { 0027 return K3b::Device::HalConnection::org_freedesktop_Hal_NoSuchDevice; 0028 } 0029 else if( errorName == QLatin1String( "org.freedesktop.Hal.DeviceAlreadyLocked" ) ) { 0030 return K3b::Device::HalConnection::org_freedesktop_Hal_DeviceAlreadyLocked; 0031 } 0032 else if( errorName == QLatin1String( "org.freedesktop.Hal.DeviceNotLocked" ) ) { 0033 return K3b::Device::HalConnection::org_freedesktop_Hal_DeviceNotLocked; 0034 } 0035 else if( errorName == QLatin1String( "org.freedesktop.Hal.Device.InterfaceAlreadyLocked" ) ) { 0036 return K3b::Device::HalConnection::org_freedesktop_Hal_Device_InterfaceAlreadyLocked; 0037 } 0038 else if( errorName == QLatin1String( "org.freedesktop.Hal.Device.InterfaceNotLocked" ) ) { 0039 return K3b::Device::HalConnection::org_freedesktop_Hal_Device_InterfaceNotLocked; 0040 } 0041 else if( errorName == QLatin1String( "org.freedesktop.Hal.PermissionDenied" ) ) { 0042 return K3b::Device::HalConnection::org_freedesktop_Hal_PermissionDenied; 0043 } 0044 else { 0045 qDebug() << "Unknown HAL error:" << errorName; 0046 return K3b::Device::HalConnection::org_freedesktop_Hal_Unknown; 0047 } 0048 } 0049 0050 } // namespace 0051 0052 0053 class K3b::Device::HalConnection::Private 0054 { 0055 public: 0056 }; 0057 0058 0059 K3b::Device::HalConnection* K3b::Device::HalConnection::instance() 0060 { 0061 return s_instance(); 0062 } 0063 0064 0065 K3b::Device::HalConnection::HalConnection( QObject* parent ) 0066 : QObject( parent ) 0067 { 0068 d = new Private(); 0069 } 0070 0071 0072 K3b::Device::HalConnection::~HalConnection() 0073 { 0074 delete d; 0075 } 0076 0077 0078 K3b::Device::HalConnection::ErrorCode K3b::Device::HalConnection::lock( Device* dev ) 0079 { 0080 qDebug() << dev->blockDeviceName(); 0081 0082 QDBusInterface halIface( "org.freedesktop.Hal", 0083 dev->solidDevice().udi(), 0084 "org.freedesktop.Hal.Device", 0085 QDBusConnection::systemBus() ); 0086 0087 if ( halIface.isValid() ) { 0088 0089 QDBusMessage msg = halIface.call( QLatin1String( "Lock" ), QLatin1String( "Locked by the K3b libraries" ) ); 0090 if ( msg.type() == QDBusMessage::ErrorMessage ) { 0091 qDebug() << "Failed to lock device through HAL:" << msg.errorMessage(); 0092 return toErrorCode( msg.errorName() ); 0093 } 0094 0095 bool exclusive = true; 0096 msg = halIface.call( QLatin1String( "AcquireInterfaceLock" ), QLatin1String( "org.freedesktop.Hal.Device.Storage" ), exclusive ); 0097 if ( msg.type() == QDBusMessage::ErrorMessage ) { 0098 qDebug() << "Failed to acquire storage interface lock through HAL:" << msg.errorMessage(); 0099 return toErrorCode( msg.errorName() ); 0100 } 0101 0102 return org_freedesktop_Hal_Success; 0103 } 0104 else { 0105 qDebug() << "Could not connect to device object:" << halIface.path(); 0106 return org_freedesktop_Hal_CommunicationError; 0107 } 0108 } 0109 0110 0111 K3b::Device::HalConnection::ErrorCode K3b::Device::HalConnection::unlock( Device* dev ) 0112 { 0113 qDebug() << dev->blockDeviceName(); 0114 0115 QDBusInterface halIface( "org.freedesktop.Hal", 0116 dev->solidDevice().udi(), 0117 "org.freedesktop.Hal.Device", 0118 QDBusConnection::systemBus() ); 0119 0120 if ( halIface.isValid() ) { 0121 0122 QDBusMessage msg = halIface.call( QLatin1String( "ReleaseInterfaceLock" ), QLatin1String( "org.freedesktop.Hal.Device.Storage" ) ); 0123 if ( msg.type() == QDBusMessage::ErrorMessage ) { 0124 qDebug() << "Failed to release storage interface lock through HAL:" << msg.errorMessage(); 0125 return toErrorCode( msg.errorName() ); 0126 } 0127 0128 msg = halIface.call( QLatin1String( "Unlock" ) ); 0129 if ( msg.type() == QDBusMessage::ErrorMessage ) { 0130 qDebug() << "Failed to unlock device through HAL:" << msg.errorMessage(); 0131 return toErrorCode( msg.errorName() ); 0132 } 0133 0134 return org_freedesktop_Hal_Success; 0135 } 0136 else { 0137 qDebug() << "Could not connect to device object:" << halIface.path(); 0138 return org_freedesktop_Hal_CommunicationError; 0139 } 0140 } 0141 0142 #include "moc_k3bhalconnection.cpp"