-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisks.cpp
201 lines (180 loc) · 6.15 KB
/
disks.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
# Copyright (c) 2018, Ole-André Rodlie <ole.andre.rodlie@gmail.com> All rights reserved.
#
# Available under the 3-clause BSD license
# See the LICENSE file for full details
*/
#include "disks.h"
#include "udisks2.h"
#include <QDebug>
#include <QTimer>
#include <QtDBus/QDBusConnection>
Device::Device(const QString block, QObject *parent)
: QObject(parent)
, path(block)
, isOptical(false)
, isRemovable(false)
, hasMedia(false)
, opticalDataTracks(0)
, opticalAudioTracks(0)
, isBlankDisc(false)
, hasPartition(false)
, dbus(0)
{
QDBusConnection system = QDBusConnection::systemBus();
dbus = new QDBusInterface(DBUS_SERVICE, path, QString("%1.Block").arg(DBUS_SERVICE), system, parent);
system.connect(dbus->service(), dbus->path(), DBUS_PROPERTIES, "PropertiesChanged", this, SLOT(handlePropertiesChanged(QString,QMap<QString,QVariant>)));
updateDeviceProperties();
}
void Device::mount()
{
if (!dbus->isValid() || !mountpoint.isEmpty()) { return; }
QString reply;
if (isOptical) {
reply = uDisks2::mountOptical(path);
}
else { reply = uDisks2::mountDevice(path); }
if (!reply.isEmpty()) {
emit errorMessage(path, reply);
return;
}
updateDeviceProperties();
}
void Device::unmount()
{
if (!dbus->isValid() || (mountpoint.isEmpty() && !isOptical)) { return; }
QString reply;
if (isOptical) { reply = uDisks2::unmountOptical(path); }
else { reply = uDisks2::unmountDevice(path); }
updateDeviceProperties();
if (!reply.isEmpty() || (!mountpoint.isEmpty())) {
if (reply.isEmpty()) { reply = QObject::tr("Failed to umount %1").arg(name); }
emit errorMessage(path, reply);
return;
}
if (isOptical) { eject(); }
}
void Device::eject()
{
if (!dbus->isValid()) { return; }
QString reply = uDisks2::ejectDevice(drive);
updateDeviceProperties();
if (!reply.isEmpty()/* || hasMedia*/) {
if (reply.isEmpty()) { reply = QObject::tr("Failed to eject %1").arg(name); }
emit errorMessage(path, reply);
}
}
void Device::updateDeviceProperties()
{
if (!dbus->isValid()) { return; }
bool hadMedia = hasMedia;
QString lastMountpoint = mountpoint;
QString lastName = name;
drive = uDisks2::getDrivePath(path);
name = uDisks2::getDeviceName(drive);
dev = path.split("/").takeLast();
isRemovable = uDisks2::isRemovable(drive);
filesystem = uDisks2::getFileSystem(path);
isOptical = uDisks2::isOptical(drive);
if (isOptical) {
mountpoint = uDisks2::getMountPointOptical(path);
} else {
mountpoint = uDisks2::getMountPoint(path);
}
hasMedia = uDisks2::hasMedia(drive);
opticalDataTracks = uDisks2::opticalDataTracks(drive);
opticalAudioTracks = uDisks2::opticalAudioTracks(drive);
isBlankDisc = uDisks2::opticalMediaIsBlank(drive);
hasPartition = uDisks2::hasPartition(path);
if (hadMedia != hasMedia) {
emit mediaChanged(path, hasMedia);
}
if (lastMountpoint != mountpoint) {
emit mountpointChanged(path, mountpoint);
}
if (lastName != name) {
emit nameChanged(path, name);
}
}
void Device::handlePropertiesChanged(const QString &interfaceType, const QMap<QString, QVariant> &changedProperties)
{
Q_UNUSED(interfaceType)
Q_UNUSED(changedProperties)
updateDeviceProperties();
}
Disks::Disks(QObject *parent)
: QObject(parent)
, dbus(0)
{
setupDBus();
timer.setInterval(60000);
connect(&timer, SIGNAL(timeout()), this, SLOT(checkUDisks()));
timer.start();
}
void Disks::setupDBus()
{
QDBusConnection system = QDBusConnection::systemBus();
if (system.isConnected()) {
system.connect(DBUS_SERVICE, DBUS_PATH, DBUS_OBJMANAGER, DBUS_DEVICE_ADDED, this, SLOT(deviceAdded(const QDBusObjectPath&)));
system.connect(DBUS_SERVICE, DBUS_PATH, DBUS_OBJMANAGER, DBUS_DEVICE_REMOVED, this, SLOT(deviceRemoved(const QDBusObjectPath&)));
if (dbus==NULL) { dbus = new QDBusInterface(DBUS_SERVICE, DBUS_PATH, DBUS_OBJMANAGER, system); } // only used to verify the UDisks is running
scanDevices();
}
}
void Disks::scanDevices()
{
if (!dbus->isValid()) { return; }
QStringList foundDevices = uDisks2::getDevices();
for (int i=0;i<foundDevices.size();i++) {
QString foundDevicePath = foundDevices.at(i);
bool hasDevice = devices.contains(foundDevicePath);
if (hasDevice) { continue; }
Device *newDevice = new Device(foundDevicePath, this);
connect(newDevice, SIGNAL(mediaChanged(QString,bool)), this, SLOT(handleDeviceMediaChanged(QString,bool)));
connect(newDevice, SIGNAL(mountpointChanged(QString,QString)), this, SLOT(handleDeviceMountpointChanged(QString,QString)));
connect(newDevice, SIGNAL(errorMessage(QString,QString)), this, SLOT(handleDeviceErrorMessage(QString,QString)));
devices[foundDevicePath] = newDevice;
}
emit updatedDevices();
}
void Disks::deviceAdded(const QDBusObjectPath &obj)
{
if (!dbus->isValid()) { return; }
QString path = obj.path();
if (path.startsWith(QString("%1/jobs").arg(DBUS_PATH))) { return; }
scanDevices();
emit foundNewDevice(path);
}
void Disks::deviceRemoved(const QDBusObjectPath &obj)
{
if (!dbus->isValid()) { return; }
QString path = obj.path();
bool deviceExists = devices.contains(path);
if (path.startsWith(QString("%1/jobs").arg(DBUS_PATH))) { return; }
if (deviceExists) {
if (uDisks2::getDevices().contains(path)) { return; }
delete devices.take(path);
}
scanDevices();
emit removedDevice(path);
}
void Disks::handleDeviceMediaChanged(QString devicePath, bool mediaPresent)
{
emit mediaChanged(devicePath, mediaPresent);
}
void Disks::handleDeviceMountpointChanged(QString devicePath, QString deviceMountpoint)
{
emit mountpointChanged(devicePath, deviceMountpoint);
}
void Disks::handleDeviceErrorMessage(QString devicePath, QString deviceError)
{
emit deviceErrorMessage(devicePath, deviceError);
}
void Disks::checkUDisks()
{
if (!QDBusConnection::systemBus().isConnected()) {
setupDBus();
return;
}
if (!dbus->isValid()) { scanDevices(); }
}