-
Notifications
You must be signed in to change notification settings - Fork 0
/
udisks2.cpp
350 lines (312 loc) · 11 KB
/
udisks2.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "udisks2.h"
// Custom type for unmarhsalling byte arrays
typedef QList<unsigned char> dbus_ay;
QString dbus_ay_toString(const dbus_ay &data) {
QString output;
foreach(unsigned char c, data)
if (c) output.append(c);
return output;
}
Q_DECLARE_METATYPE(dbus_ay)
UDisks2::UDisks2(QObject *parent) :
QObject(parent)
{
auto system = QDBusConnection::systemBus();
system.connect("org.freedesktop.UDisks2",
"/org/freedesktop/UDisks2",
"org.freedesktop.DBus.ObjectManager",
"InterfacesAdded",
this,
SLOT(dbus_interfaceAdded(QDBusObjectPath,QMap<QString,QVariant>)));
system.connect("org.freedesktop.UDisks2",
"/org/freedesktop/UDisks2",
"org.freedesktop.DBus.ObjectManager",
"InterfacesRemoved",
this,
SLOT(dbus_interfaceRemoved(QDBusObjectPath,QStringList)));
foreach (QString block, blockDevices()) {
addBlock(block);
}
foreach (QString drive, drives()) {
addDrive(drive);
}
}
static QString lastPart(const QString &path) {
return path.split('/').last();
}
UDisks2::~UDisks2()
{
}
QStringList UDisks2::blockDevices()
{
QDBusInterface ud2("org.freedesktop.UDisks2",
"/org/freedesktop/UDisks2/block_devices",
"org.freedesktop.DBus.Introspectable",
QDBusConnection::systemBus());
QDBusReply<QString> reply = ud2.call("Introspect");
if (!reply.isValid())
return QStringList();
QXmlStreamReader xml(reply.value());
QStringList response;
while (!xml.atEnd()) {
xml.readNext();
if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name().toString() == "node") {
QString name = xml.attributes().value("name").toString();
if (!name.isEmpty())
response << name;
}
}
return response;
}
UDisks2Block *UDisks2::blockDevice(const QString &node)
{
return blocks_.contains(node) ? blocks_[node] : NULL;
}
QStringList UDisks2::drives()
{
QDBusInterface ud2("org.freedesktop.UDisks2",
"/org/freedesktop/UDisks2/drives",
"org.freedesktop.DBus.Introspectable",
QDBusConnection::systemBus());
if (!ud2.isValid())
return QStringList();
QDBusReply<QString> reply = ud2.call("Introspect");
if (!reply.isValid())
return QStringList();
QXmlStreamReader xml(reply.value());
QStringList response;
while (!xml.atEnd()) {
xml.readNext();
if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name().toString() == "node") {
QString name = xml.attributes().value("name").toString();
if (!name.isEmpty())
response << name;
}
}
return response;
}
UDisks2Drive *UDisks2::drive(const QString &node)
{
return drives_.contains(node) ? drives_[node] : NULL;
}
void UDisks2::addDrive(const QString &node)
{
if (drives_.contains(node))
drives_.value(node)->update();
else {
auto drive = new UDisks2Drive(node, this);
connect(drive, &UDisks2Drive::changed,
this, &UDisks2::driveChanged);
drives_.insert(node, drive);
emit driveAdded(node);
}
}
void UDisks2::addBlock(const QString &node)
{
if (blocks_.contains(node))
blocks_.value(node)->update();
else {
auto block = new UDisks2Block(node, this);
connect(block, &UDisks2Block::filesystemAdded,
this, &UDisks2::filesystemAdded);
connect(block, &UDisks2Block::filesystemRemoved,
this, &UDisks2::filesystemRemoved);
connect(block, &UDisks2Block::filesystemChanged,
this, &UDisks2::filesystemChanged);
connect(block, &UDisks2Block::changed,
this, &UDisks2::blockDeviceChanged);
blocks_.insert(node, block);
emit blockDeviceAdded(node);
}
}
void UDisks2::removeDrive(const QString &node)
{
if (drives_.contains(node))
delete drives_.take(node);
}
void UDisks2::removeBlock(const QString &node)
{
if (blocks_.contains(node))
delete blocks_.take(node);
}
void UDisks2::dbus_interfaceAdded(const QDBusObjectPath &path, const QMap<QString, QVariant> &interfaces)
{
// path: o [path]
// nodes: a{sa{sv} [dict of strings dict of string variants]
QString node = lastPart(path.path());
if (path.path().startsWith("/org/freedesktop/UDisks2/block_devices")) {
if (interfaces.contains("org.freedesktop.UDisks2.Block"))
addBlock(node);
if (interfaces.contains("org.freedesktop.UDisks2.Filesystem")
&& blocks_.contains(node))
blocks_[node]->addFilesystem();
} else if (path.path().startsWith("/org/freedesktop/UDisks2/drives")) {
if (interfaces.contains("org.freedesktop.UDisks2.Drive"))
addDrive(node);
}
}
void UDisks2::dbus_interfaceRemoved(const QDBusObjectPath &path, const QStringList &interfaces)
{
// path: o [path]
// interfaces: as [list of strings]
QString node = lastPart(path.path());
if (path.path().startsWith("/org/freedesktop/UDisks2/block_devices")) {
if (interfaces.contains("org.freedesktop.UDisks2.Block")) {
removeBlock(node);
emit blockDeviceRemoved(node);
}
if (interfaces.contains("org.freedesktop.UDisks2.Filesystem")
&& blocks_.contains(node)) {
blocks_[node]->removeFilesystem();
}
} else if (path.path().startsWith("/org/freedesktop/UDisks2/drives")) {
removeDrive(node);
emit driveRemoved(node);
}
}
UDisks2Block::UDisks2Block(const QString &node, QObject *parent) :
QObject(parent), name(node), fs(NULL)
{
QDBusConnection system = QDBusConnection::systemBus();
dbus = new QDBusInterface("org.freedesktop.UDisks2",
"/org/freedesktop/UDisks2/block_devices/" + node,
"org.freedesktop.UDisks2.Block",
system, parent);
system.connect(dbus->service(), dbus->path(),
"org.freedesktop.DBus.Properties", "PropertiesChanged",
this, SLOT(self_propertiesChanged(QString,QVariantMap,QStringList)));
update();
if (!type.isEmpty())
addFilesystem();
}
QString UDisks2Block::toString()
{
return QString("name: %1\ndev: %2\nid: %3\ndrive: %4\nsize: %5\n"
"readonly: %6\nusage: %7\ntype: %8")
.arg(name).arg(dev).arg(id).arg(drive).arg(size).arg(readonly)
.arg(usage).arg(type);
}
void UDisks2Block::update()
{
dev = dbus->property("Device").toString();
id = dbus->property("Id").toString();
drive = lastPart(dbus->property("Drive").value<QDBusObjectPath>().path());
size = dbus->property("Size").toULongLong();
readonly = dbus->property("ReadOnly").toBool();
usage = dbus->property("IdUsage").toString();
type = dbus->property("IdType").toString();
}
void UDisks2Block::updateFilesystem()
{
if (fs)
fs->update();
}
void UDisks2Block::addFilesystem()
{
if (fs && !fs->isValid()) {
delete fs;
fs = NULL;
}
if (!fs)
fs = new UDisks2Filesystem(name);
emit filesystemAdded(name);
}
void UDisks2Block::removeFilesystem()
{
emit filesystemRemoved(name);
if (fs) delete fs;
fs = NULL;
}
UDisks2Filesystem *UDisks2Block::fileSystem()
{
return fs;
}
void UDisks2Block::self_propertiesChanged(const QString &interface, const QVariantMap &changedProp, const QStringList &invalidatedProp)
{
if (interface == "org.freedesktop.UDisks2.Block") {
update();
emit changed(name);
}
if (interface == "org.freedesktop.UDisks2.Filesystem") {
updateFilesystem();
emit filesystemChanged(name);
}
}
UDisks2Drive::UDisks2Drive(const QString &node, QObject *parent) :
QObject(parent), name(node)
{
QDBusConnection system = QDBusConnection::systemBus();
dbus = new QDBusInterface("org.freedesktop.UDisks2",
"/org/freedesktop/UDisks2/drives/" + node,
"org.freedesktop.UDisks2.Drive",
system, parent);
system.connect(dbus->service(), dbus->path(),
"org.freedesktop.DBus.Properties", "PropertiesChanged",
this, SLOT(self_propertiesChanged(QString,QVariantMap,QStringList)));
update();
}
QString UDisks2Drive::toString()
{
return QString("name: %1\nsize: %2\nvendor: %3\nmodel: %4\nserial: %5\n"
"id: %6\nmedia: %7\noptical: %8\nremovable: %9\navailable: %10")
.arg(name).arg(size).arg(vendor).arg(model).arg(serial).arg(id)
.arg(media).arg(optical).arg(removable).arg(available);
}
void UDisks2Drive::update()
{
size = dbus->property("Size").toULongLong();
vendor = dbus->property("Vendor").toString();
model = dbus->property("Model").toString();
serial = dbus->property("Serial").toString();
id = dbus->property("Id").toString();
media = dbus->property("Media").toString();
optical = dbus->property("Optical").toBool();
removable = dbus->property("MediaRemovable").toBool();
available = dbus->property("MediaAvailable").toBool();
}
void UDisks2Drive::self_propertiesChanged(const QString &interface, const QVariantMap &changedProp, const QStringList &invalidatedProp)
{
update();
emit this->changed(name);
}
UDisks2Filesystem::UDisks2Filesystem(const QString &node, QObject *parent)
: QObject(parent)
{
name = node;
QDBusConnection system = QDBusConnection::systemBus();
dbus = new QDBusInterface("org.freedesktop.UDisks2",
"/org/freedesktop/UDisks2/block_devices/" + node,
"org.freedesktop.UDisks2.Filesystem",
system, parent);
dbusProp = new QDBusInterface("org.freedesktop.UDisks2",
"/org/freedesktop/UDisks2/block_devices/" + node,
"org.freedesktop.DBus.Properties",
system, parent);
emit update();
}
QStringList UDisks2Filesystem::mountPoints() const { return mountPoints_; }
QString UDisks2Filesystem::mount()
{
QDBusMessage reply = dbus->call("Mount", QVariantMap());
return reply.arguments().first().toString();
}
void UDisks2Filesystem::unmount()
{ dbus->call("Unmount", QVariantMap());
}
void UDisks2Filesystem::update()
{
mountPoints_.clear();
QDBusMessage reply = dbusProp->call("Get", "org.freedesktop.UDisks2.Filesystem", "MountPoints");
QVariant v = reply.arguments().first();
QDBusArgument arg = v.value<QDBusVariant>().variant().value<QDBusArgument>();
arg.beginArray();
while (!arg.atEnd()) {
dbus_ay data;
arg >> data;
mountPoints_.append(dbus_ay_toString(data));
}
}
bool UDisks2Filesystem::isValid()
{
return dbus->isValid();
}