-
Notifications
You must be signed in to change notification settings - Fork 39
/
deviceenumerator_udisks2.cpp
213 lines (151 loc) · 7.21 KB
/
deviceenumerator_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
////////////////////////////////////////////////////////////////////////////////
// This file is part of LibreELEC - http://www.libreelec.tv
// Copyright (C) 2016 Team LibreELEC
//
// LibreELEC is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// LibreELEC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LibreELEC. If not, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////////////
#include "deviceenumerator_udisks2.h"
namespace
{
constexpr auto UDISKS2_SERVICE{"org.freedesktop.UDisks2"};
constexpr auto UDISKS2_MANAGER_PATH{"/org/freedesktop/UDisks2/Manager"};
constexpr auto UDISKS2_MANAGER_INTERFACE{"org.freedesktop.UDisks2.Manager"};
constexpr auto UDISKS2_BLOCK_INTERFACE{"org.freedesktop.UDisks2.Block"};
constexpr auto UDISKS2_DRIVE_INTERFACE{"org.freedesktop.UDisks2.Drive"};
constexpr auto UDISKS2_PARTITIONTABLE_INTERFACE{"org.freedesktop.UDisks2.PartitionTable"};
constexpr auto UDISKS2_FILESYSTEM_INTERFACE{"org.freedesktop.UDisks2.Filesystem"};
constexpr auto DBUS_PROPERTIES_INTERFACE{"org.freedesktop.DBus.Properties"};
}
DeviceEnumerator_udisks2::DeviceEnumerator_udisks2() :
connection(QDBusConnection::systemBus())
{}
QStringList DeviceEnumerator_udisks2::getRemovableDeviceNames() const
{
QDBusInterface managerInterface{UDISKS2_SERVICE, UDISKS2_MANAGER_PATH, UDISKS2_MANAGER_INTERFACE, connection};
if (!managerInterface.isValid())
return {};
QMap<QString, QVariant> options{};
QDBusReply<QList<QDBusObjectPath>> reply = managerInterface.call("GetBlockDevices", options);
if (!reply.isValid())
return {};
QStringList deviceNames;
for (const auto& blockDevice : reply.value())
{
QDBusInterface blockDevicePropertiesInterface{UDISKS2_SERVICE, blockDevice.path(), DBUS_PROPERTIES_INTERFACE, connection};
if (!blockDevicePropertiesInterface.isValid())
continue;
QDBusReply<QVariant> replyType = blockDevicePropertiesInterface.call("Get", UDISKS2_PARTITIONTABLE_INTERFACE, "Type");
if (!replyType.isValid())
continue;
QDBusReply<QVariant> replyDrive = blockDevicePropertiesInterface.call("Get", UDISKS2_BLOCK_INTERFACE, "Drive");
if (!replyDrive.isValid())
continue;
QDBusInterface drivePropertiesInterface{UDISKS2_SERVICE, replyDrive.value().value<QDBusObjectPath>().path(), DBUS_PROPERTIES_INTERFACE, connection};
if (!drivePropertiesInterface.isValid())
continue;
QDBusReply<QVariant> replyRemovable = drivePropertiesInterface.call("Get", UDISKS2_DRIVE_INTERFACE, "Removable");
if (!replyRemovable.isValid())
continue;
if (replyRemovable.value().toBool())
deviceNames.append(blockDevice.path());
}
return deviceNames;
}
QStringList DeviceEnumerator_udisks2::getUserFriendlyNames(const QStringList &devices) const
{
QStringList friendlyNames;
for (const auto& device : devices)
{
QDBusInterface blockPropertiesInterface{UDISKS2_SERVICE, device, DBUS_PROPERTIES_INTERFACE, connection};
if (!blockPropertiesInterface.isValid())
continue;
QDBusReply<QVariant> replyDrive = blockPropertiesInterface.call("Get", UDISKS2_BLOCK_INTERFACE, "Drive");
if (!replyDrive.isValid())
continue;
QDBusInterface drivePropertiesInterface{UDISKS2_SERVICE, replyDrive.value().value<QDBusObjectPath>().path(), DBUS_PROPERTIES_INTERFACE, connection};
if (!drivePropertiesInterface.isValid())
continue;
QDBusReply<QVariant> replyVendor = drivePropertiesInterface.call("Get", UDISKS2_DRIVE_INTERFACE, "Vendor");
if (!replyVendor.isValid())
continue;
QDBusReply<QVariant> replyModel = drivePropertiesInterface.call("Get", UDISKS2_DRIVE_INTERFACE,"Model");
if (!replyModel.isValid())
continue;
qint64 size = getSizeOfDevice(device);
QString name = QString("%1 %2 [%3]").arg(replyVendor.value().toString(), replyModel.value().toString(), sizeToHuman(size)).trimmed();
friendlyNames.append(name);
}
return friendlyNames;
}
bool DeviceEnumerator_udisks2::unmountDevicePartitions(const QString &device) const
{
QDBusInterface blockPropertiesInterface{UDISKS2_SERVICE, device, DBUS_PROPERTIES_INTERFACE, connection};
if (!blockPropertiesInterface.isValid())
return false;
QDBusReply<QVariant> replyPartitions = blockPropertiesInterface.call("Get", UDISKS2_PARTITIONTABLE_INTERFACE, "Partitions");
if (!replyPartitions.isValid())
return false;
QDBusArgument dbusArgs = replyPartitions.value().value<QDBusArgument>();
QList<QDBusObjectPath> partitions;
dbusArgs >> partitions;
QMap<QDBusObjectPath, bool> partitionsUnmounted;
for (const auto& partition : partitions)
{
partitionsUnmounted[partition] = false;
QDBusInterface partitionPropertiesInterface{UDISKS2_SERVICE, partition.path(), DBUS_PROPERTIES_INTERFACE, connection};
if (!partitionPropertiesInterface.isValid())
continue;
QDBusReply<QVariant> replyMountPoints = partitionPropertiesInterface.call("Get", UDISKS2_FILESYSTEM_INTERFACE, "MountPoints");
if (!replyMountPoints.isValid())
continue;
QDBusArgument dbusArgs = replyMountPoints.value().value<QDBusArgument>();
QByteArrayList mountPoints;
dbusArgs >> mountPoints;
if (mountPoints.empty())
{
partitionsUnmounted[partition] = true;
continue;
}
else
{
QDBusInterface filesystemInterface{UDISKS2_SERVICE, partition.path(), UDISKS2_FILESYSTEM_INTERFACE, connection};
if (!filesystemInterface.isValid())
continue;
QMap<QString, QVariant> options{};
QDBusReply<void> reply = filesystemInterface.call("Unmount", options);
partitionsUnmounted[partition] = reply.isValid();
}
}
return std::all_of(partitionsUnmounted.keyValueBegin(), partitionsUnmounted.keyValueEnd(), [](const auto& it) {
return it.second;
});
}
qint64 DeviceEnumerator_udisks2::getSizeOfDevice(const QString& device) const
{
QDBusInterface blockPropertiesInterface{UDISKS2_SERVICE, device, DBUS_PROPERTIES_INTERFACE, connection};
if (!blockPropertiesInterface.isValid())
return 0;
QDBusReply<QVariant> replySize = blockPropertiesInterface.call("Get", UDISKS2_BLOCK_INTERFACE, "Size");
if (!replySize.isValid())
return 0;
return replySize.value().toULongLong();
}
int DeviceEnumerator_udisks2::loadEjectDrive(const QString &device, const loadEject action) const
{
return 0;
}
int DeviceEnumerator_udisks2::removeDrive(const QString &device) const
{
return 0;
}