2020-03-04 16:55:40 +01:00
|
|
|
/*
|
|
|
|
* 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"},
|
|
|
|
{mountpointsRole, "mountpoints"}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
int DriveListModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return _drivelist.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> DriveListModel::roleNames() const
|
|
|
|
{
|
|
|
|
return _rolenames;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DriveListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
int row = index.row();
|
|
|
|
if (row < 0 || row >= _drivelist.count())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
QByteArray propertyName = _rolenames.value(role);
|
|
|
|
if (propertyName.isEmpty())
|
|
|
|
return QVariant();
|
|
|
|
else
|
|
|
|
return _drivelist.values().at(row)->property(propertyName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DriveListModel::refreshDriveList()
|
|
|
|
{
|
|
|
|
bool changes = false;
|
|
|
|
bool filterSystemDrives = DRIVELIST_FILTER_SYSTEM_DRIVES;
|
|
|
|
auto l = Drivelist::ListStorageDevices();
|
|
|
|
QSet<QString> drivesInNewList;
|
|
|
|
|
|
|
|
for (auto &i: l)
|
|
|
|
{
|
|
|
|
// Convert STL vector<string> to Qt QStringList
|
|
|
|
QStringList mountpoints;
|
|
|
|
for (auto &s: i.mountpoints)
|
|
|
|
{
|
|
|
|
mountpoints.append(QString::fromStdString(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filterSystemDrives)
|
|
|
|
{
|
|
|
|
if (i.isSystem || i.isReadOnly)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Should already be caught by isSystem variable, but just in case...
|
|
|
|
if (mountpoints.contains("/") || mountpoints.contains("C://"))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-23 12:37:31 +02:00
|
|
|
// Skip zero-sized and virtual devices
|
|
|
|
if (i.size == 0 || i.isVirtual)
|
|
|
|
continue;
|
|
|
|
|
2020-03-04 16:55:40 +01:00
|
|
|
QString device = QString::fromStdString(i.device);
|
|
|
|
drivesInNewList.insert(device);
|
|
|
|
|
|
|
|
if (!_drivelist.contains(device))
|
|
|
|
{
|
|
|
|
// Found new drive
|
|
|
|
if (!changes)
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
changes = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_drivelist[device] = new DriveListItem(device, QString::fromStdString(i.description), i.size, i.isUSB, i.isSCSI, 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();
|
|
|
|
}
|