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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 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 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 263 264 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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
|
#include "./syncthingdevicemodel.h"
#include "./colors.h"
#include "./syncthingicons.h"
#include <syncthingconnector/syncthingconnection.h>
#include <syncthingconnector/utils.h>
#include <c++utilities/conversion/stringconversion.h>
#include <QStringBuilder>
using namespace std;
using namespace CppUtilities;
namespace Data {
static int computeDeviceRowCount(const SyncthingDev &dev)
{
// hide connection type, last seen and everything after introducer (eg. traffic) unless connected
return dev.isConnected() ? 11 : 6;
}
SyncthingDeviceModel::SyncthingDeviceModel(SyncthingConnection &connection, QObject *parent)
: SyncthingModel(connection, parent)
, m_devs(connection.devInfo())
{
updateRowCount();
connect(&m_connection, &SyncthingConnection::devStatusChanged, this, &SyncthingDeviceModel::devStatusChanged);
}
QHash<int, QByteArray> SyncthingDeviceModel::roleNames() const
{
const static QHash<int, QByteArray> roles{
{ Qt::DisplayRole, "name" },
{ DeviceStatus, "status" },
{ Qt::DecorationRole, "statusIcon" },
{ DevicePaused, "paused" },
{ IsThisDevice, "isThisDevice" },
{ IsPinned, "isPinned" },
{ DeviceStatusString, "statusString" },
{ DeviceStatusColor, "statusColor" },
{ DeviceId, "devId" },
{ DeviceDetail, "detail" },
{ DeviceDetailIcon, "detailIcon" },
{ DeviceDetailTooltip, "detailTooltip" },
{ DeviceNeededItemsCount, "neededItemsCount" },
};
return roles;
}
const QVector<int> &SyncthingDeviceModel::colorRoles() const
{
static const QVector<int> colorRoles({ Qt::DecorationRole, Qt::ForegroundRole, DeviceStatusColor, DeviceDetailIcon });
return colorRoles;
}
/*!
* \brief Returns the device info for the specified \a index. The returned object is not persistent.
*/
const SyncthingDev *SyncthingDeviceModel::devInfo(const QModelIndex &index) const
{
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));
}
QModelIndex SyncthingDeviceModel::index(int row, int column, const QModelIndex &parent) const
{
if (!parent.isValid()) {
// top-level: all dev IDs
if (row < rowCount(parent)) {
return createIndex(row, column, static_cast<quintptr>(-1));
}
} else if (!parent.parent().isValid()) {
// dev-level: dev attributes
if (row < rowCount(parent)) {
return createIndex(row, column, static_cast<quintptr>(parent.row()));
}
}
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();
}
QVariant SyncthingDeviceModel::headerData(int section, Qt::Orientation orientation, int role) const
{
switch (orientation) {
case Qt::Horizontal:
switch (role) {
case Qt::DisplayRole:
switch (section) {
case 0:
return tr("ID");
case 1:
return tr("Status");
}
break;
default:;
}
break;
default:;
}
return QVariant();
}
QVariant SyncthingDeviceModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
if (index.parent().isValid()) {
// dir attributes
if (static_cast<size_t>(index.parent().row()) >= m_devs.size()) {
return QVariant();
}
const auto &dev = m_devs[static_cast<size_t>(index.parent().row())];
const auto skipRows = !dev.isConnected();
auto row = skipRows && index.row() >= 2 ? index.row() + 2 : index.row();
if (skipRows && row >= 5) {
row += 2;
}
switch (role) {
case Qt::DisplayRole:
case Qt::EditRole:
if (index.column() == 0) {
// attribute names
switch (row) {
case 0:
return tr("ID");
case 1:
return tr("Out of Sync items");
case 2:
return tr("Incoming traffic");
case 3:
return tr("Outgoing traffic");
case 4:
return tr("Address");
case 5:
return tr("Connection type");
case 6:
return tr("Last seen");
case 7:
return tr("Compression");
case 8:
return tr("Certificate");
case 9:
return tr("Introducer");
case 10:
return tr("Version");
}
break;
}
[[fallthrough]];
case DeviceDetail:
if (index.column() == 1 || role == DeviceDetail) {
// attribute values
switch (row) {
case 0:
return dev.id;
case 1:
if (dev.overallCompletion.needed.isNull()) {
return tr("none");
}
return tr("%1 item(s), ~ %2", nullptr, trQuandity(dev.overallCompletion.needed.items))
.arg(dev.overallCompletion.needed.items)
.arg(dataSizeToString(dev.overallCompletion.needed.bytes).data());
case 2:
return QString::fromStdString(dataSizeToString(dev.totalIncomingTraffic));
case 3:
return QString::fromStdString(dataSizeToString(dev.totalOutgoingTraffic));
case 4:
if (dev.connectionAddress.isEmpty()) {
return dev.addresses.join(QStringLiteral(", "));
} else {
return QVariant(
dev.connectionAddress % QStringLiteral(" (") % dev.addresses.join(QStringLiteral(", ")) % QStringLiteral(")"));
}
case 5:
if (!dev.connectionType.isEmpty()) {
return QVariant(
dev.connectionType % QStringLiteral(" (") % (dev.connectionLocal ? tr("local") : tr("remote")) % QStringLiteral(")"));
} else {
return QVariant();
}
case 6:
return dev.lastSeen.isNull() ? tr("unknown or this device")
: QString::fromLatin1(dev.lastSeen.toString(DateTimeOutputFormat::DateAndTime, true).data());
case 7:
return dev.compression;
case 8:
return dev.certName.isEmpty() ? tr("none") : dev.certName;
case 9:
return dev.introducer ? tr("yes") : tr("no");
case 10:
return dev.clientVersion;
}
}
break;
case Qt::DecorationRole:
case DeviceDetailIcon:
if (index.column() == 0) {
// attribute icons
const auto &icons = commonForkAwesomeIcons();
switch (row) {
case 0:
return icons.hashtag;
case 1:
return icons.exchange;
case 2:
return icons.cloudDownload;
case 3:
return icons.cloudUpload;
case 4:
return icons.link;
case 5:
return icons.signal;
case 6:
return icons.eye;
case 7:
return icons.fileArchive;
case 8:
return icons.certificate;
case 9:
return icons.networkWired;
case 10:
return icons.tag;
}
}
break;
case Qt::ForegroundRole:
switch (index.column()) {
case 1:
switch (row) {
case 1:
if (dev.overallCompletion.needed.isNull()) {
return Colors::gray(m_brightColors);
}
break;
case 6:
if (dev.lastSeen.isNull()) {
return Colors::gray(m_brightColors);
}
break;
case 8:
if (dev.certName.isEmpty()) {
return Colors::gray(m_brightColors);
}
break;
}
}
break;
case Qt::ToolTipRole:
case DeviceDetailTooltip:
switch ((m_singleColumnMode || role == DeviceDetailTooltip) ? 1 : index.column()) {
case 1:
switch (row) {
case 4:
if (!dev.connectionType.isEmpty()) {
return dev.connectionType;
}
break;
case 6:
if (!dev.lastSeen.isNull()) {
return agoString(dev.lastSeen);
}
break;
}
}
break;
default:;
}
return QVariant();
}
if (static_cast<size_t>(index.row()) >= m_devs.size()) {
return QVariant();
}
// dir IDs and status
const SyncthingDev &dev = m_devs[static_cast<size_t>(index.row())];
switch (role) {
case Qt::DisplayRole:
case Qt::EditRole:
switch (index.column()) {
case 0:
return dev.name.isEmpty() ? dev.id : dev.name;
case 1:
return dev.statusString();
}
break;
case Qt::DecorationRole:
switch (index.column()) {
case 0:
if (dev.paused) {
return statusIcons().pause;
} else {
switch (dev.status) {
case SyncthingDevStatus::Unknown:
case SyncthingDevStatus::Disconnected:
return statusIcons().disconnected;
case SyncthingDevStatus::ThisDevice:
case SyncthingDevStatus::Idle:
return statusIcons().idling;
case SyncthingDevStatus::Synchronizing:
return statusIcons().sync;
case SyncthingDevStatus::OutOfSync:
case SyncthingDevStatus::Rejected:
return statusIcons().error;
}
}
break;
}
break;
case Qt::TextAlignmentRole:
switch (index.column()) {
case 0:
break;
case 1:
return static_cast<int>(Qt::AlignRight | Qt::AlignVCenter);
}
break;
case Qt::ForegroundRole:
switch (index.column()) {
case 0:
break;
case 1:
return devStatusColor(dev);
}
break;
case IsPinned:
case IsThisDevice:
return dev.status == SyncthingDevStatus::ThisDevice;
case DeviceStatus:
return static_cast<int>(dev.status);
case DevicePaused:
return dev.paused;
case DeviceStatusString:
return dev.statusString();
case DeviceStatusColor:
return devStatusColor(dev);
case DeviceId:
return dev.id;
case DeviceNeededItemsCount:
return dev.overallCompletion.needed.items;
default:;
}
return QVariant();
}
bool SyncthingDeviceModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
Q_UNUSED(index)
Q_UNUSED(value)
Q_UNUSED(role)
return false;
}
int SyncthingDeviceModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid()) {
return static_cast<int>(m_devs.size());
} else if (!parent.parent().isValid() && static_cast<std::size_t>(parent.row()) < m_rowCount.size()) {
return m_rowCount[static_cast<std::size_t>(parent.row())];
} else {
return 0;
}
}
void SyncthingDeviceModel::devStatusChanged(const SyncthingDev &dev, int index)
{
if (index < 0 || static_cast<std::size_t>(index) >= m_rowCount.size()) {
return;
}
// update top-level indices
const QModelIndex modelIndex1(this->index(index, 0, QModelIndex()));
static const QVector<int> modelRoles1({ Qt::DisplayRole, Qt::EditRole, Qt::DecorationRole, Qt::ForegroundRole, DevicePaused, DeviceStatus,
DeviceStatusString, DeviceStatusColor, DeviceId, IsThisDevice, IsPinned, DeviceNeededItemsCount });
emit dataChanged(modelIndex1, modelIndex1, modelRoles1);
const QModelIndex modelIndex2(this->index(index, 1, QModelIndex()));
static const QVector<int> modelRoles2({ Qt::DisplayRole, Qt::EditRole, Qt::ForegroundRole });
emit dataChanged(modelIndex2, modelIndex2, modelRoles2);
// remove/insert detail rows
const auto oldRowCount = m_rowCount[static_cast<std::size_t>(index)];
const auto newRowCount = computeDeviceRowCount(dev);
const auto newLastRow = newRowCount - 1;
if (oldRowCount > newRowCount) {
// begin removing rows for statistics
beginRemoveRows(modelIndex1, 2, 3);
m_rowCount[static_cast<std::size_t>(index)] = newRowCount;
endRemoveRows();
} else if (newRowCount > oldRowCount) {
// begin inserting rows for statistics
beginInsertRows(modelIndex1, 2, 3);
m_rowCount[static_cast<std::size_t>(index)] = newRowCount;
endInsertRows();
}
// update detail rows
static const QVector<int> modelRoles3({ Qt::DisplayRole, Qt::EditRole, Qt::ToolTipRole, DeviceDetailTooltip });
emit dataChanged(this->index(0, 1, modelIndex1), this->index(newLastRow, 1, modelIndex1), modelRoles3);
static const QVector<int> modelRoles4({ Qt::DisplayRole, Qt::EditRole, DeviceDetail, DeviceDetailIcon });
emit dataChanged(this->index(0, 0, modelIndex1), this->index(newLastRow, 0, modelIndex1), modelRoles4);
}
void SyncthingDeviceModel::handleConfigInvalidated()
{
beginResetModel();
}
void SyncthingDeviceModel::handleNewConfigAvailable()
{
updateRowCount();
endResetModel();
}
void SyncthingDeviceModel::handleStatusIconsChanged()
{
invalidateTopLevelIndicies(QVector<int>({ Qt::DecorationRole }));
}
void SyncthingDeviceModel::handleForkAwesomeIconsChanged()
{
invalidateNestedIndicies(QVector<int>({ Qt::DecorationRole, DeviceDetailIcon }));
}
QVariant SyncthingDeviceModel::devStatusColor(const SyncthingDev &dev) const
{
if (dev.paused) {
return QVariant();
}
switch (dev.status) {
case SyncthingDevStatus::Unknown:
break;
case SyncthingDevStatus::Disconnected:
break;
case SyncthingDevStatus::ThisDevice:
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);
}
return QVariant();
}
void SyncthingDeviceModel::updateRowCount()
{
m_rowCount.clear();
m_rowCount.reserve(m_devs.size());
for (const auto &dev : m_devs) {
m_rowCount.emplace_back(computeDeviceRowCount(dev));
}
}
} // namespace Data
|