File: k3bhalconnection.cpp

package info (click to toggle)
k3b 25.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,840 kB
  • sloc: cpp: 99,202; xml: 372; sh: 84; makefile: 10
file content (142 lines) | stat: -rw-r--r-- 4,819 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
    SPDX-FileCopyrightText: 2005-2009 Sebastian Trueg <trueg@k3b.org>
    SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "k3bhalconnection.h"
#include "k3bdevice.h"
#include "k3bdevice_i18n.h"

#include <Solid/Device>

#include <QDebug>
#include <QDBusConnection>
#include <QDBusInterface>


Q_GLOBAL_STATIC( K3b::Device::HalConnection, s_instance )


namespace {
    
    K3b::Device::HalConnection::ErrorCode toErrorCode( const QString& errorName )
    {
        if( errorName == QLatin1String( "org.freedesktop.Hal.NoSuchDevice" ) ) {
            return K3b::Device::HalConnection::org_freedesktop_Hal_NoSuchDevice;
        }
        else if( errorName == QLatin1String( "org.freedesktop.Hal.DeviceAlreadyLocked" ) ) {
            return K3b::Device::HalConnection::org_freedesktop_Hal_DeviceAlreadyLocked;
        }
        else if( errorName == QLatin1String( "org.freedesktop.Hal.DeviceNotLocked" ) ) {
            return K3b::Device::HalConnection::org_freedesktop_Hal_DeviceNotLocked;
        }
        else if( errorName == QLatin1String( "org.freedesktop.Hal.Device.InterfaceAlreadyLocked" ) ) {
            return K3b::Device::HalConnection::org_freedesktop_Hal_Device_InterfaceAlreadyLocked;
        }
        else if( errorName == QLatin1String( "org.freedesktop.Hal.Device.InterfaceNotLocked" ) ) {
            return K3b::Device::HalConnection::org_freedesktop_Hal_Device_InterfaceNotLocked;
        }
        else if( errorName == QLatin1String( "org.freedesktop.Hal.PermissionDenied" ) ) {
            return K3b::Device::HalConnection::org_freedesktop_Hal_PermissionDenied;
        }
        else {
            qDebug() << "Unknown HAL error:" << errorName;
            return K3b::Device::HalConnection::org_freedesktop_Hal_Unknown;
        }
    }
    
} // namespace


class K3b::Device::HalConnection::Private
{
public:
};


K3b::Device::HalConnection* K3b::Device::HalConnection::instance()
{
    return s_instance();
}


K3b::Device::HalConnection::HalConnection( QObject* parent )
    : QObject( parent )
{
    d = new Private();
}


K3b::Device::HalConnection::~HalConnection()
{
    delete d;
}


K3b::Device::HalConnection::ErrorCode K3b::Device::HalConnection::lock( Device* dev )
{
    qDebug() << dev->blockDeviceName();

    QDBusInterface halIface( "org.freedesktop.Hal",
                             dev->solidDevice().udi(),
                             "org.freedesktop.Hal.Device",
                             QDBusConnection::systemBus() );

    if ( halIface.isValid() ) {
        
        QDBusMessage msg = halIface.call( QLatin1String( "Lock" ), QLatin1String( "Locked by the K3b libraries" ) );
        if ( msg.type() == QDBusMessage::ErrorMessage ) {
            qDebug() << "Failed to lock device through HAL:" << msg.errorMessage();
            return toErrorCode( msg.errorName() );
        }
        
        bool exclusive = true;
        msg = halIface.call( QLatin1String( "AcquireInterfaceLock" ), QLatin1String( "org.freedesktop.Hal.Device.Storage" ), exclusive );
        if ( msg.type() == QDBusMessage::ErrorMessage ) {
            qDebug() << "Failed to acquire storage interface lock through HAL:" << msg.errorMessage();
            return toErrorCode( msg.errorName() );
        }
        
        return org_freedesktop_Hal_Success;
    }
    else {
        qDebug() << "Could not connect to device object:" << halIface.path();
        return org_freedesktop_Hal_CommunicationError;
    }
}


K3b::Device::HalConnection::ErrorCode K3b::Device::HalConnection::unlock( Device* dev )
{
    qDebug() << dev->blockDeviceName();

    QDBusInterface halIface( "org.freedesktop.Hal",
                             dev->solidDevice().udi(),
                             "org.freedesktop.Hal.Device",
                             QDBusConnection::systemBus() );

    if ( halIface.isValid() ) {
        
        QDBusMessage msg = halIface.call( QLatin1String( "ReleaseInterfaceLock" ), QLatin1String( "org.freedesktop.Hal.Device.Storage" ) );
        if ( msg.type() == QDBusMessage::ErrorMessage ) {
            qDebug() << "Failed to release storage interface lock through HAL:" << msg.errorMessage();
            return toErrorCode( msg.errorName() );
        }
        
        msg = halIface.call( QLatin1String( "Unlock" ) );
        if ( msg.type() == QDBusMessage::ErrorMessage ) {
            qDebug() << "Failed to unlock device through HAL:" << msg.errorMessage();
            return toErrorCode( msg.errorName() );
        }
        
        return org_freedesktop_Hal_Success;
    }
    else {
        qDebug() << "Could not connect to device object:" << halIface.path();
        return org_freedesktop_Hal_CommunicationError;
    }
}

#include "moc_k3bhalconnection.cpp"