syncthingtray/model/syncthingdevicemodel.cpp

339 lines
12 KiB
C++
Raw Normal View History

2016-08-25 00:45:32 +02:00
#include "./syncthingdevicemodel.h"
2016-11-08 19:42:50 +01:00
#include "./colors.h"
2017-05-01 03:34:43 +02:00
#include "./syncthingicons.h"
#include "../connector/syncthingconnection.h"
#include "../connector/utils.h"
2016-09-01 16:34:30 +02:00
using namespace ChronoUtilities;
2016-08-25 00:45:32 +02:00
namespace Data {
2017-05-01 03:34:43 +02:00
SyncthingDeviceModel::SyncthingDeviceModel(SyncthingConnection &connection, QObject *parent)
: SyncthingModel(connection, parent)
, m_devs(connection.devInfo())
2016-08-25 00:45:32 +02:00
{
2016-08-26 16:43:53 +02:00
connect(&m_connection, &SyncthingConnection::newConfig, this, &SyncthingDeviceModel::newConfig);
connect(&m_connection, &SyncthingConnection::newDevices, this, &SyncthingDeviceModel::newDevices);
connect(&m_connection, &SyncthingConnection::devStatusChanged, this, &SyncthingDeviceModel::devStatusChanged);
2016-08-25 00:45:32 +02:00
}
QHash<int, QByteArray> SyncthingDeviceModel::initRoleNames()
{
QHash<int, QByteArray> roles;
roles[Qt::DisplayRole] = "name";
roles[DeviceStatus] = "status";
roles[Qt::DecorationRole] = "statusIcon";
roles[DevicePaused] = "paused";
roles[DeviceStatusString] = "statusString";
roles[DeviceStatusColor] = "statusColor";
roles[DeviceId] = "devId";
roles[DeviceDetail] = "detail";
return roles;
}
QHash<int, QByteArray> SyncthingDeviceModel::roleNames() const
{
const static QHash<int, QByteArray> roles(initRoleNames());
return roles;
}
2016-08-25 00:45:32 +02:00
/*!
* \brief Returns the device info for the spcified \a index. The returned object is not persistent.
*/
const SyncthingDev *SyncthingDeviceModel::devInfo(const QModelIndex &index) const
{
2017-05-01 03:34:43 +02:00
return (index.parent().isValid() ? devInfo(index.parent())
: (static_cast<size_t>(index.row()) < m_devs.size() ? &m_devs[static_cast<size_t>(index.row())] : nullptr));
2016-08-25 00:45:32 +02:00
}
QModelIndex SyncthingDeviceModel::index(int row, int column, const QModelIndex &parent) const
{
2017-05-01 03:34:43 +02:00
if (!parent.isValid()) {
2016-08-25 00:45:32 +02:00
// top-level: all dev IDs
2017-05-01 03:34:43 +02:00
if (row < rowCount(parent)) {
return createIndex(row, column, static_cast<quintptr>(-1));
2016-08-25 00:45:32 +02:00
}
2017-05-01 03:34:43 +02:00
} else if (!parent.parent().isValid()) {
2016-08-25 00:45:32 +02:00
// dev-level: dev attributes
2017-05-01 03:34:43 +02:00
if (row < rowCount(parent)) {
return createIndex(row, column, static_cast<quintptr>(parent.row()));
2016-08-25 00:45:32 +02:00
}
}
return QModelIndex();
}
QModelIndex SyncthingDeviceModel::parent(const QModelIndex &child) const
{
return child.internalId() != static_cast<quintptr>(-1) ? index(static_cast<int>(child.internalId()), 0, QModelIndex()) : QModelIndex();
2016-08-25 00:45:32 +02:00
}
QVariant SyncthingDeviceModel::headerData(int section, Qt::Orientation orientation, int role) const
{
2017-05-01 03:34:43 +02:00
switch (orientation) {
2016-08-25 00:45:32 +02:00
case Qt::Horizontal:
2017-05-01 03:34:43 +02:00
switch (role) {
2016-08-25 00:45:32 +02:00
case Qt::DisplayRole:
2017-05-01 03:34:43 +02:00
switch (section) {
case 0:
return tr("ID");
case 1:
return tr("Status");
2016-08-25 00:45:32 +02:00
}
break;
2017-05-01 03:34:43 +02:00
default:;
2016-08-25 00:45:32 +02:00
}
break;
2017-05-01 03:34:43 +02:00
default:;
2016-08-25 00:45:32 +02:00
}
return QVariant();
}
QVariant SyncthingDeviceModel::data(const QModelIndex &index, int role) const
{
2017-05-01 03:34:43 +02:00
if (index.isValid()) {
if (index.parent().isValid()) {
2016-08-25 00:45:32 +02:00
// dir attributes
2017-05-01 03:34:43 +02:00
if (static_cast<size_t>(index.parent().row()) < m_devs.size()) {
switch (role) {
2016-08-25 00:45:32 +02:00
case Qt::DisplayRole:
case Qt::EditRole:
if (index.column() == 0) {
// attribute names
2017-05-01 03:34:43 +02:00
switch (index.row()) {
case 0:
return tr("ID");
case 1:
return tr("Addresses");
case 2:
return tr("Last seen");
case 3:
return tr("Compression");
case 4:
return tr("Certificate");
case 5:
return tr("Introducer");
2016-08-25 00:45:32 +02:00
}
break;
}
FALLTHROUGH;
case DeviceDetail:
if (index.column() == 1 || role == DeviceDetail) {
// attribute values
const SyncthingDev &dev = m_devs[static_cast<size_t>(index.parent().row())];
2017-05-01 03:34:43 +02:00
switch (index.row()) {
case 0:
return dev.id;
case 1:
return dev.addresses.join(QStringLiteral(", "));
case 2:
return dev.lastSeen.isNull() ? tr("unknown or own device")
: QString::fromLatin1(dev.lastSeen.toString(DateTimeOutputFormat::DateAndTime, true).data());
case 3:
return dev.compression;
case 4:
return dev.certName.isEmpty() ? tr("none") : dev.certName;
case 5:
return dev.introducer ? tr("yes") : tr("no");
2016-08-25 00:45:32 +02:00
}
}
break;
2016-09-01 16:34:30 +02:00
case Qt::ForegroundRole:
2017-05-01 03:34:43 +02:00
switch (index.column()) {
2016-09-01 16:34:30 +02:00
case 1:
2016-09-21 21:09:12 +02:00
const SyncthingDev &dev = m_devs[static_cast<size_t>(index.parent().row())];
2017-05-01 03:34:43 +02:00
switch (index.row()) {
2016-09-01 16:34:30 +02:00
case 2:
2017-05-01 03:34:43 +02:00
if (dev.lastSeen.isNull()) {
2016-11-08 19:42:50 +01:00
return Colors::gray(m_brightColors);
2016-09-01 16:34:30 +02:00
}
break;
case 4:
2017-05-01 03:34:43 +02:00
if (dev.certName.isEmpty()) {
2016-11-08 19:42:50 +01:00
return Colors::gray(m_brightColors);
2016-09-01 16:34:30 +02:00
}
break;
}
}
break;
case Qt::ToolTipRole:
2017-05-01 03:34:43 +02:00
switch (index.column()) {
2016-09-01 16:34:30 +02:00
case 1:
2017-05-01 03:34:43 +02:00
switch (index.row()) {
2016-09-01 16:34:30 +02:00
case 2:
2016-09-21 21:09:12 +02:00
const SyncthingDev &dev = m_devs[static_cast<size_t>(index.parent().row())];
2017-05-01 03:34:43 +02:00
if (!dev.lastSeen.isNull()) {
2016-09-01 16:34:30 +02:00
return agoString(dev.lastSeen);
}
break;
}
}
break;
2017-05-01 03:34:43 +02:00
default:;
2016-08-25 00:45:32 +02:00
}
}
2017-05-01 03:34:43 +02:00
} else if (static_cast<size_t>(index.row()) < m_devs.size()) {
2016-08-25 00:45:32 +02:00
// dir IDs and status
const SyncthingDev &dev = m_devs[static_cast<size_t>(index.row())];
2017-05-01 03:34:43 +02:00
switch (role) {
2016-08-25 00:45:32 +02:00
case Qt::DisplayRole:
case Qt::EditRole:
2017-05-01 03:34:43 +02:00
switch (index.column()) {
case 0:
return dev.name.isEmpty() ? dev.id : dev.name;
2016-08-25 00:45:32 +02:00
case 1:
return devStatusString(dev);
2016-08-25 00:45:32 +02:00
}
case Qt::DecorationRole:
2017-05-01 03:34:43 +02:00
switch (index.column()) {
2016-08-25 00:45:32 +02:00
case 0:
2017-05-01 03:34:43 +02:00
if (dev.paused) {
2017-02-20 18:03:20 +01:00
return statusIcons().pause;
2016-08-26 16:43:53 +02:00
} else {
2017-05-01 03:34:43 +02:00
switch (dev.status) {
case SyncthingDevStatus::Unknown:
2017-05-01 03:34:43 +02:00
case SyncthingDevStatus::Disconnected:
return statusIcons().disconnected;
case SyncthingDevStatus::OwnDevice:
2017-05-01 03:34:43 +02:00
case SyncthingDevStatus::Idle:
return statusIcons().idling;
case SyncthingDevStatus::Synchronizing:
return statusIcons().sync;
case SyncthingDevStatus::OutOfSync:
2017-05-01 03:34:43 +02:00
case SyncthingDevStatus::Rejected:
return statusIcons().error;
2016-08-26 16:43:53 +02:00
}
2016-08-25 00:45:32 +02:00
}
break;
}
break;
case Qt::TextAlignmentRole:
2017-05-01 03:34:43 +02:00
switch (index.column()) {
case 0:
break;
case 1:
return static_cast<int>(Qt::AlignRight | Qt::AlignVCenter);
2016-08-25 00:45:32 +02:00
}
break;
case Qt::ForegroundRole:
2017-05-01 03:34:43 +02:00
switch (index.column()) {
case 0:
break;
2016-08-25 00:45:32 +02:00
case 1:
return devStatusColor(dev);
2016-08-25 00:45:32 +02:00
}
break;
2016-08-26 16:43:53 +02:00
case DeviceStatus:
return static_cast<int>(dev.status);
case DevicePaused:
return dev.paused;
case IsOwnDevice:
return dev.status == SyncthingDevStatus::OwnDevice;
case DeviceStatusString:
return devStatusString(dev);
case DeviceStatusColor:
return devStatusColor(dev);
case DeviceId:
return dev.id;
2017-05-01 03:34:43 +02:00
default:;
2016-08-25 00:45:32 +02:00
}
}
}
return QVariant();
}
bool SyncthingDeviceModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
2017-05-01 03:34:43 +02:00
Q_UNUSED(index) Q_UNUSED(value) Q_UNUSED(role) return false;
2016-08-25 00:45:32 +02:00
}
int SyncthingDeviceModel::rowCount(const QModelIndex &parent) const
{
2017-05-01 03:34:43 +02:00
if (!parent.isValid()) {
return static_cast<int>(m_devs.size());
2017-05-01 03:34:43 +02:00
} else if (!parent.parent().isValid()) {
2016-09-01 16:34:30 +02:00
return 6;
2016-08-25 00:45:32 +02:00
} else {
return 0;
}
}
int SyncthingDeviceModel::columnCount(const QModelIndex &parent) const
{
2017-05-01 03:34:43 +02:00
if (!parent.isValid()) {
2016-08-25 00:45:32 +02:00
return 2; // name/id, status
2017-05-01 03:34:43 +02:00
} else if (!parent.parent().isValid()) {
2016-08-25 00:45:32 +02:00
return 2; // field name and value
} else {
return 0;
}
}
2016-08-26 16:43:53 +02:00
void SyncthingDeviceModel::newConfig()
{
beginResetModel();
}
void SyncthingDeviceModel::newDevices()
{
endResetModel();
}
void SyncthingDeviceModel::devStatusChanged(const SyncthingDev &, int index)
{
const QModelIndex modelIndex1(this->index(index, 0, QModelIndex()));
static const QVector<int> modelRoles1({ Qt::DecorationRole, DevicePaused, DeviceStatus, DeviceStatusString, DeviceStatusColor, DeviceId });
emit dataChanged(modelIndex1, modelIndex1, modelRoles1);
2016-08-26 16:43:53 +02:00
const QModelIndex modelIndex2(this->index(index, 1, QModelIndex()));
static const QVector<int> modelRoles2({ Qt::DisplayRole, Qt::ForegroundRole });
emit dataChanged(modelIndex2, modelIndex2, modelRoles2);
}
QString SyncthingDeviceModel::devStatusString(const SyncthingDev &dev)
{
if (dev.paused) {
return tr("Paused");
}
switch (dev.status) {
case SyncthingDevStatus::Unknown:
return tr("Unknown status");
case SyncthingDevStatus::OwnDevice:
return tr("Own device");
case SyncthingDevStatus::Idle:
return tr("Idle");
case SyncthingDevStatus::Disconnected:
return tr("Disconnected");
case SyncthingDevStatus::Synchronizing:
return dev.progressPercentage > 0 ? tr("Synchronizing (%1 %)").arg(dev.progressPercentage) : tr("Synchronizing");
case SyncthingDevStatus::OutOfSync:
return tr("Out of sync");
case SyncthingDevStatus::Rejected:
return tr("Rejected");
}
return QString();
}
2017-09-01 17:13:01 +02:00
QVariant SyncthingDeviceModel::devStatusColor(const SyncthingDev &dev) const
{
if (dev.paused) {
2017-09-01 17:13:01 +02:00
return QVariant();
}
switch (dev.status) {
case SyncthingDevStatus::Unknown:
break;
case SyncthingDevStatus::Disconnected:
break;
case SyncthingDevStatus::OwnDevice:
case SyncthingDevStatus::Idle:
return Colors::green(m_brightColors);
case SyncthingDevStatus::Synchronizing:
return Colors::blue(m_brightColors);
case SyncthingDevStatus::OutOfSync:
case SyncthingDevStatus::Rejected:
return Colors::red(m_brightColors);
}
2017-09-01 17:13:01 +02:00
return QVariant();
2016-08-26 16:43:53 +02:00
}
2016-08-25 00:45:32 +02:00
} // namespace Data