Skip to content

Latest commit

 

History

History
361 lines (312 loc) · 11.6 KB

partitionmodel.cpp

File metadata and controls

361 lines (312 loc) · 11.6 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"
Aug 21, 2018
Aug 21, 2018
35
36
37
38
39
40
#include "udisks2monitor_p.h"
#include "logging_p.h"
#include <QDir>
#include <QFileInfo>
41
42
43
44
45
46
47
48
49
50
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);
Aug 21, 2018
Aug 21, 2018
51
52
53
connect(m_manager.data(), &PartitionManagerPrivate::errorMessage, this, &PartitionModel::errorMessage);
Aug 29, 2018
Aug 29, 2018
54
55
56
57
58
59
60
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));
});
Aug 21, 2018
Aug 21, 2018
61
62
63
64
65
66
67
68
69
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));
});
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
}
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();
}
}
Aug 21, 2018
Aug 21, 2018
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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;
}
110
111
112
113
114
115
116
117
118
119
120
121
void PartitionModel::refresh()
{
m_manager->refresh();
}
void PartitionModel::refresh(int index)
{
if (index >= 0 && index < m_partitions.count()) {
m_partitions[index].refresh();
}
}
Aug 29, 2018
Aug 29, 2018
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
void PartitionModel::lock(const QString &deviceName)
{
qCInfo(lcMemoryCardLog) << Q_FUNC_INFO << deviceName << m_partitions.count();
bool found = false;
for (const Partition &partition : m_partitions) {
if (deviceName == partition.deviceName()) {
found = true;
m_manager->lock(partition);
break;
}
}
if (!found) {
qCWarning(lcMemoryCardLog) << "Unable to lock unknown device:" << deviceName;
}
}
void PartitionModel::unlock(const QString &deviceName, const QString &passphrase)
{
qCInfo(lcMemoryCardLog) << Q_FUNC_INFO << deviceName << m_partitions.count();
bool found = false;
for (const Partition &partition : m_partitions) {
if (deviceName == partition.deviceName()) {
found = true;
m_manager->unlock(partition, passphrase);
break;
}
}
if (!found) {
qCWarning(lcMemoryCardLog) << "Unable to unlock unknown device:" << deviceName;
}
}
Aug 21, 2018
Aug 21, 2018
158
159
160
161
void PartitionModel::mount(const QString &deviceName)
{
qCInfo(lcMemoryCardLog) << Q_FUNC_INFO << deviceName << m_partitions.count();
Aug 29, 2018
Aug 29, 2018
162
bool found = false;
Aug 21, 2018
Aug 21, 2018
163
164
for (const Partition &partition : m_partitions) {
if (deviceName == partition.deviceName()) {
Aug 29, 2018
Aug 29, 2018
165
found = true;
Aug 21, 2018
Aug 21, 2018
166
167
168
169
m_manager->mount(partition);
break;
}
}
Aug 29, 2018
Aug 29, 2018
170
171
172
173
if (!found) {
qCWarning(lcMemoryCardLog) << "Unable to mount unknown device:" << deviceName;
}
Aug 21, 2018
Aug 21, 2018
174
175
176
177
178
179
}
void PartitionModel::unmount(const QString &deviceName)
{
qCInfo(lcMemoryCardLog) << Q_FUNC_INFO << deviceName << m_partitions.count();
Aug 29, 2018
Aug 29, 2018
180
bool found = false;
Aug 21, 2018
Aug 21, 2018
181
182
for (const Partition &partition : m_partitions) {
if (deviceName == partition.deviceName()) {
Aug 29, 2018
Aug 29, 2018
183
found = true;
Aug 21, 2018
Aug 21, 2018
184
185
186
187
m_manager->unmount(partition);
break;
}
}
Aug 29, 2018
Aug 29, 2018
188
189
190
191
if (!found) {
qCWarning(lcMemoryCardLog) << "Unable to unmount unknown device:" << deviceName;
}
Aug 21, 2018
Aug 21, 2018
192
193
}
Aug 24, 2018
Aug 24, 2018
194
void PartitionModel::format(const QString &deviceName, const QString &type, const QString &label, const QString &passphrase)
Aug 21, 2018
Aug 21, 2018
195
196
197
{
qCInfo(lcMemoryCardLog) << Q_FUNC_INFO << deviceName << type << label << m_partitions.count();
Aug 29, 2018
Aug 29, 2018
198
bool found = false;
Aug 21, 2018
Aug 21, 2018
199
200
for (const Partition &partition : m_partitions) {
if (deviceName == partition.deviceName()) {
Aug 29, 2018
Aug 29, 2018
201
found = true;
Aug 24, 2018
Aug 24, 2018
202
m_manager->format(partition, type, label, passphrase);
Aug 21, 2018
Aug 21, 2018
203
204
205
break;
}
}
Aug 29, 2018
Aug 29, 2018
206
207
208
209
if (!found) {
qCWarning(lcMemoryCardLog) << "Unable to format unknown device:" << deviceName;
}
Aug 21, 2018
Aug 21, 2018
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
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();
}
}
QHash<int, QByteArray> PartitionModel::roleNames() const
{
static const QHash<int, QByteArray> roleNames = {
{ ReadOnlyRole, "readOnly" },
{ StatusRole, "status" },
{ CanMountRole, "canMount" },
{ MountFailedRole, "mountFailed" },
{ StorageTypeRole, "storageType" },
{ FilesystemTypeRole, "filesystemType" },
Aug 21, 2018
Aug 21, 2018
263
{ DeviceLabelRole, "deviceLabel" },
264
{ DevicePathRole, "devicePath" },
Feb 6, 2018
Feb 6, 2018
265
{ DeviceNameRole, "deviceName" },
266
267
268
{ MountPathRole, "mountPath" },
{ BytesAvailableRole, "bytesAvailable" },
{ BytesTotalRole, "bytesTotal" },
Dec 9, 2016
Dec 9, 2016
269
{ BytesFreeRole, "bytesFree" },
Aug 31, 2018
Aug 31, 2018
270
271
{ PartitionModelRole, "partitionModel" },
{ IsCryptoDeviceRoles, "isCryptoDevice"},
Sep 4, 2018
Sep 4, 2018
272
{ IsSupportedFileSystemType, "isSupportedFileSystemType"},
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
};
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();
Aug 21, 2018
Aug 21, 2018
303
304
case DeviceLabelRole:
return partition.deviceLabel();
305
306
case DevicePathRole:
return partition.devicePath();
Feb 6, 2018
Feb 6, 2018
307
308
case DeviceNameRole:
return partition.deviceName();
309
310
311
312
313
314
315
316
case MountPathRole:
return partition.mountPath();
case BytesAvailableRole:
return partition.bytesAvailable();
case BytesTotalRole:
return partition.bytesTotal();
case BytesFreeRole:
return partition.bytesFree();
Dec 9, 2016
Dec 9, 2016
317
318
case PartitionModelRole:
return QVariant::fromValue(static_cast<QObject*>(const_cast<PartitionModel*>((this))));
Aug 31, 2018
Aug 31, 2018
319
320
case IsCryptoDeviceRoles:
return partition.isCryptoDevice();
Sep 4, 2018
Sep 4, 2018
321
322
case IsSupportedFileSystemType:
return partition.isSupportedFileSystemType();
323
324
325
326
327
328
329
330
331
default:
return QVariant();
}
}
}
void PartitionModel::partitionChanged(const Partition &partition)
{
for (int i = 0; i < m_partitions.count(); ++i) {
Aug 21, 2018
Aug 21, 2018
332
qCInfo(lcMemoryCardLog) << "partition changed:" << partition.status() << partition.mountPath();;
333
if (m_partitions.at(i) == partition) {
Feb 6, 2018
Feb 6, 2018
334
335
QModelIndex index = createIndex(i, 0);
emit dataChanged(index, index);
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
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;
}
}
}