/
partitionmodel.cpp
359 lines (314 loc) · 12.3 KB
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
/*
* Copyright (C) 2016 Jolla Ltd. <andrew.den.exter@jolla.com>
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Nemo Mobile nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/
#include "partitionmodel.h"
#include "partitionmanager_p.h"
35
36
37
38
39
#include "udisks2monitor_p.h"
#include "logging_p.h"
#include <QDir>
#include <QFileInfo>
40
#include <QtQml/qqmlinfo.h>
41
42
43
44
45
46
47
48
49
50
51
PartitionModel::PartitionModel(QObject *parent)
: QAbstractListModel(parent)
, m_manager(PartitionManagerPrivate::instance())
, m_storageTypes(Any | ExcludeParents)
{
m_partitions = m_manager->partitions(Partition::Any | Partition::ExcludeParents);
connect(m_manager.data(), &PartitionManagerPrivate::partitionChanged, this, &PartitionModel::partitionChanged);
connect(m_manager.data(), &PartitionManagerPrivate::partitionAdded, this, &PartitionModel::partitionAdded);
connect(m_manager.data(), &PartitionManagerPrivate::partitionRemoved, this, &PartitionModel::partitionRemoved);
52
53
54
connect(m_manager.data(), &PartitionManagerPrivate::errorMessage, this, &PartitionModel::errorMessage);
55
56
57
58
59
60
61
connect(m_manager.data(), &PartitionManagerPrivate::lockError, this, [this](Partition::Error error) {
emit lockError(static_cast<PartitionModel::Error>(error));
});
connect(m_manager.data(), &PartitionManagerPrivate::unlockError, this, [this](Partition::Error error) {
emit unlockError(static_cast<PartitionModel::Error>(error));
});
62
63
64
65
66
67
68
69
70
connect(m_manager.data(), &PartitionManagerPrivate::mountError, this, [this](Partition::Error error) {
emit mountError(static_cast<PartitionModel::Error>(error));
});
connect(m_manager.data(), &PartitionManagerPrivate::unmountError, this, [this](Partition::Error error) {
emit unmountError(static_cast<PartitionModel::Error>(error));
});
connect(m_manager.data(), &PartitionManagerPrivate::formatError, this, [this](Partition::Error error) {
emit formatError(static_cast<PartitionModel::Error>(error));
});
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
}
PartitionModel::~PartitionModel()
{
}
PartitionModel::StorageTypes PartitionModel::storageTypes() const
{
return m_storageTypes;
}
void PartitionModel::setStorageTypes(StorageTypes types)
{
if (m_storageTypes != types) {
m_storageTypes = types;
update();
emit storageTypesChanged();
}
}
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
QStringList PartitionModel::supportedFormatTypes() const
{
QStringList types;
QDir dir("/sbin/");
QStringList entries = dir.entryList(QStringList() << QString("mkfs.*"));
for (const QString &entry : entries) {
QFileInfo info(QString("/sbin/%1").arg(entry));
if (info.exists() && info.isExecutable()) {
QStringList parts = entry.split('.');
if (!parts.isEmpty()) {
types << parts.takeLast();
}
}
}
return types;
}
111
112
113
114
115
116
117
118
119
120
121
122
void PartitionModel::refresh()
{
m_manager->refresh();
}
void PartitionModel::refresh(int index)
{
if (index >= 0 && index < m_partitions.count()) {
m_partitions[index].refresh();
}
}
123
void PartitionModel::lock(const QString &devicePath)
124
{
125
qCInfo(lcMemoryCardLog) << Q_FUNC_INFO << devicePath << m_partitions.count();
126
m_manager->lock(devicePath);
127
128
}
129
void PartitionModel::unlock(const QString &devicePath, const QString &passphrase)
130
{
131
132
qCInfo(lcMemoryCardLog) << Q_FUNC_INFO << devicePath << m_partitions.count();
if (const Partition *partition = getPartition(devicePath)) {
133
134
m_manager->unlock(*partition, passphrase);
} else {
135
qCWarning(lcMemoryCardLog) << "Unable to unlock unknown device:" << devicePath;
136
137
138
}
}
139
void PartitionModel::mount(const QString &devicePath)
140
{
141
142
qCInfo(lcMemoryCardLog) << Q_FUNC_INFO << devicePath << m_partitions.count();
if (const Partition *partition = getPartition(devicePath)) {
143
144
m_manager->mount(*partition);
} else {
145
qCWarning(lcMemoryCardLog) << "Unable to mount unknown device:" << devicePath;
146
}
147
148
}
149
void PartitionModel::unmount(const QString &devicePath)
150
{
151
152
qCInfo(lcMemoryCardLog) << Q_FUNC_INFO << devicePath << m_partitions.count();
if (const Partition *partition = getPartition(devicePath)) {
153
154
m_manager->unmount(*partition);
} else {
155
qCWarning(lcMemoryCardLog) << "Unable to unmount unknown device:" << devicePath;
156
}
157
158
}
159
void PartitionModel::format(const QString &devicePath, const QVariantMap &arguments)
160
{
161
162
QString filesystemType = arguments.value(QLatin1String("filesystemType"), QString()).toString();
if (filesystemType.isEmpty()) {
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
qmlInfo(this) << "Missing or empty filesystemType argument, cannot format.";
return;
}
// Only fixing invalid args would be enough. Udisks don't care if key is unknown like auto-mount.
QVariantMap args;
args.insert(QLatin1String("label"), arguments.value(QLatin1String("label"), QString()).toString());
args.insert(QLatin1String("no-block"), true);
args.insert(QLatin1String("take-ownership"), true);
args.insert(QLatin1String("update-partition-type"), true);
args.insert(QLatin1String("auto-mount"), arguments.value(QLatin1String("auto-mount"), false).toBool());
QString passphrase = arguments.value(QLatin1String("encrypt-passphrase"), QString()).toString();
if (!passphrase.isEmpty()) {
args.insert(QLatin1String("encrypt.passphrase"), passphrase);
}
180
181
qCInfo(lcMemoryCardLog) << Q_FUNC_INFO << devicePath << filesystemType << args << m_partitions.count();
m_manager->format(devicePath, filesystemType, args);
182
183
}
184
QString PartitionModel::objectPath(const QString &devicePath) const
185
{
186
qCInfo(lcMemoryCardLog) << Q_FUNC_INFO << devicePath;
187
return m_manager->objectPath(devicePath);
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
void PartitionModel::update()
{
const int count = m_partitions.count();
const auto partitions = m_manager->partitions(Partition::StorageTypes(int(m_storageTypes)));
int index = 0;
for (const auto partition : partitions) {
const int existingIndex = [this, index, partition]() {
for (int i = index; i < m_partitions.count(); ++i) {
if (m_partitions.at(i) == partition) {
return i;
}
}
return -1;
}();
if (existingIndex == -1) {
beginInsertRows(QModelIndex(), index, index);
m_partitions.insert(index, partition);
endInsertRows();
} else if (existingIndex > index) {
beginMoveRows(QModelIndex(), existingIndex, existingIndex, QModelIndex(), index);
const auto partition = m_partitions.takeAt(existingIndex);
m_partitions.insert(index, partition);
endMoveRows();
}
++index;
}
if (index < m_partitions.count()) {
beginRemoveRows(QModelIndex(), index, m_partitions.count() - 1);
m_partitions.resize(index);
endRemoveRows();
}
if (count != m_partitions.count()) {
emit countChanged();
}
}
232
const Partition *PartitionModel::getPartition(const QString &devicePath) const
233
234
{
for (const Partition &partition : m_partitions) {
235
if (devicePath == partition.devicePath()) {
236
237
238
239
240
241
242
return &partition;
}
}
return nullptr;
}
243
244
245
246
247
248
249
250
251
QHash<int, QByteArray> PartitionModel::roleNames() const
{
static const QHash<int, QByteArray> roleNames = {
{ ReadOnlyRole, "readOnly" },
{ StatusRole, "status" },
{ CanMountRole, "canMount" },
{ MountFailedRole, "mountFailed" },
{ StorageTypeRole, "storageType" },
{ FilesystemTypeRole, "filesystemType" },
252
{ DeviceLabelRole, "deviceLabel" },
253
{ DevicePathRole, "devicePath" },
254
{ DeviceNameRole, "deviceName" },
255
256
257
{ MountPathRole, "mountPath" },
{ BytesAvailableRole, "bytesAvailable" },
{ BytesTotalRole, "bytesTotal" },
258
{ BytesFreeRole, "bytesFree" },
259
260
{ PartitionModelRole, "partitionModel" },
{ IsCryptoDeviceRoles, "isCryptoDevice"},
261
{ IsSupportedFileSystemType, "isSupportedFileSystemType"},
262
{ IsEncryptedRoles, "isEncrypted"},
263
{ CryptoBackingDevicePath, "cryptoBackingDevicePath"},
264
{ DriveRole, "drive"},
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
};
return roleNames;
}
int PartitionModel::rowCount(const QModelIndex &parent) const
{
return !parent.isValid() ? m_partitions.count() : 0;
}
QVariant PartitionModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= m_partitions.count() || index.column() != 0) {
return QVariant();
} else {
const Partition &partition = m_partitions.at(index.row());
switch (role) {
case ReadOnlyRole:
return partition.isReadOnly();
case StatusRole:
return partition.status();
case CanMountRole:
return partition.canMount();
case MountFailedRole:
return partition.mountFailed();
case StorageTypeRole:
return partition.storageType();
case FilesystemTypeRole:
return partition.filesystemType();
295
296
case DeviceLabelRole:
return partition.deviceLabel();
297
298
case DevicePathRole:
return partition.devicePath();
299
300
case DeviceNameRole:
return partition.deviceName();
301
302
303
304
305
306
307
308
case MountPathRole:
return partition.mountPath();
case BytesAvailableRole:
return partition.bytesAvailable();
case BytesTotalRole:
return partition.bytesTotal();
case BytesFreeRole:
return partition.bytesFree();
309
310
case PartitionModelRole:
return QVariant::fromValue(static_cast<QObject*>(const_cast<PartitionModel*>((this))));
311
312
case IsCryptoDeviceRoles:
return partition.isCryptoDevice();
313
314
case IsSupportedFileSystemType:
return partition.isSupportedFileSystemType();
315
316
case IsEncryptedRoles:
return partition.isEncrypted();
317
318
case CryptoBackingDevicePath:
return partition.cryptoBackingDevicePath();
319
320
case DriveRole:
return partition.drive();
321
322
323
324
325
326
327
328
329
default:
return QVariant();
}
}
}
void PartitionModel::partitionChanged(const Partition &partition)
{
for (int i = 0; i < m_partitions.count(); ++i) {
330
qCInfo(lcMemoryCardLog) << "partition changed:" << partition.status() << partition.mountPath();;
331
if (m_partitions.at(i) == partition) {
332
333
QModelIndex index = createIndex(i, 0);
emit dataChanged(index, index);
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
return;
}
}
}
void PartitionModel::partitionAdded(const Partition &partition)
{
if (partition.storageType() & m_storageTypes) {
update();
}
}
void PartitionModel::partitionRemoved(const Partition &partition)
{
for (int i = 0; i < m_partitions.count(); ++i) {
if (m_partitions.at(i) == partition) {
beginRemoveRows(QModelIndex(), i, i);
m_partitions.removeAt(i);
endRemoveRows();
emit countChanged();
return;
}
}
}