This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrivelistmodel.cpp
144 lines (124 loc) · 3.56 KB
/
drivelistmodel.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
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2020 Raspberry Pi (Trading) Limited
*/
#include "drivelistmodel.h"
#include "config.h"
#include "dependencies/drivelist/src/drivelist.hpp"
#include <QSet>
#include <QDebug>
DriveListModel::DriveListModel(QObject *parent)
: QAbstractListModel(parent)
{
_rolenames = {
{deviceRole, "device"},
{descriptionRole, "description"},
{sizeRole, "size"},
{isUsbRole, "isUsb"},
{isScsiRole, "isScsi"},
{isReadOnlyRole, "isReadOnly"},
{mountpointsRole, "mountpoints"}};
// Enumerate drives in seperate thread, but process results in UI thread
connect(&_thread, SIGNAL(newDriveList(std::vector<Drivelist::DeviceDescriptor>)), SLOT(processDriveList(std::vector<Drivelist::DeviceDescriptor>)));
}
auto DriveListModel::rowCount(const QModelIndex & /*parent*/) const -> int
{
return _drivelist.count();
}
auto DriveListModel::roleNames() const -> QHash<int, QByteArray>
{
return _rolenames;
}
auto DriveListModel::data(const QModelIndex &index, int role) const -> QVariant
{
int row = index.row();
if (row < 0 || row >= _drivelist.count())
{
return QVariant();
}
QByteArray propertyName = _rolenames.value(role);
if (propertyName.isEmpty())
{
return QVariant();
}
return _drivelist.values().at(row)->property(propertyName);
}
void DriveListModel::processDriveList(const std::vector<Drivelist::DeviceDescriptor> &l)
{
bool changes = false;
bool filterSystemDrives = DRIVELIST_FILTER_SYSTEM_DRIVES;
QSet<QString> drivesInNewList;
for (const auto &i : l)
{
// Convert STL vector<string> to Qt QStringList
QStringList mountpoints;
for (const auto &s : i.mountpoints)
{
mountpoints.append(QString::fromStdString(s));
}
if (filterSystemDrives)
{
if (i.isSystem)
{
continue;
}
}
// Should already be caught by isSystem variable, but just in case...
if (mountpoints.contains("/") || mountpoints.contains("C://"))
{
continue;
}
// Skip zero-sized devices
if (i.size == 0)
{
continue;
}
#ifdef Q_OS_DARWIN
if (i.isVirtual)
continue;
#endif
QString deviceNamePlusSize = QString::fromStdString(i.device) + ":" + QString::number(i.size);
if (i.isReadOnly)
{
deviceNamePlusSize += "ro";
}
drivesInNewList.insert(deviceNamePlusSize);
if (!_drivelist.contains(deviceNamePlusSize))
{
// Found new drive
if (!changes)
{
beginResetModel();
changes = true;
}
_drivelist[deviceNamePlusSize] = new DriveListItem(QString::fromStdString(i.device), QString::fromStdString(i.description), i.size, i.isUSB, i.isSCSI, i.isReadOnly, mountpoints, this);
}
}
// Look for drives removed
QStringList drivesInOldList = _drivelist.keys();
for (auto &device : drivesInOldList)
{
if (!drivesInNewList.contains(device))
{
if (!changes)
{
beginResetModel();
changes = true;
}
_drivelist.value(device)->deleteLater();
_drivelist.remove(device);
}
}
if (changes)
{
endResetModel();
}
}
void DriveListModel::startPolling()
{
_thread.start();
}
void DriveListModel::stopPolling()
{
_thread.stop();
}