diff --git a/syncthingconnector/syncthingconnection_requests.cpp b/syncthingconnector/syncthingconnection_requests.cpp index 068796b..4ff963c 100644 --- a/syncthingconnector/syncthingconnection_requests.cpp +++ b/syncthingconnector/syncthingconnection_requests.cpp @@ -709,12 +709,18 @@ void SyncthingConnection::readDirs(const QJsonArray &dirs) void SyncthingConnection::readDevs(const QJsonArray &devs) { // store the new devs in a temporary list which is assigned to m_devs later - vector newDevs; - newDevs.reserve(static_cast(devs.size())); + auto newDevs = std::vector(); + newDevs.reserve(static_cast(devs.size())); + auto *const thisDevice = addDevInfo(newDevs, m_myId); + thisDevice->id = m_myId; + thisDevice->status = SyncthingDevStatus::ThisDevice; + thisDevice->paused = false; - for (const QJsonValue &devVal : devs) { - const QJsonObject devObj(devVal.toObject()); - SyncthingDev *const devItem = addDevInfo(newDevs, devObj.value(QLatin1String("deviceID")).toString()); + for (const auto &devVal : devs) { + const auto devObj = devVal.toObject(); + const auto deviceId = devObj.value(QLatin1String("deviceID")).toString(); + const auto isThisDevice = deviceId == m_myId; + auto *const devItem = isThisDevice ? thisDevice : addDevInfo(newDevs, deviceId); if (!devItem) { continue; } @@ -724,10 +730,7 @@ void SyncthingConnection::readDevs(const QJsonArray &devs) devItem->compression = devObj.value(QLatin1String("compression")).toString(); devItem->certName = devObj.value(QLatin1String("certName")).toString(); devItem->introducer = devObj.value(QLatin1String("introducer")).toBool(false); - if (devItem->id == m_myId) { - devItem->status = SyncthingDevStatus::ThisDevice; - devItem->paused = false; - } else { + if (!isThisDevice) { devItem->status = SyncthingDevStatus::Unknown; devItem->paused = devObj.value(QLatin1String("paused")).toBool(devItem->paused); } diff --git a/syncthingmodel/syncthingdevicemodel.cpp b/syncthingmodel/syncthingdevicemodel.cpp index ce25f3e..5c79077 100644 --- a/syncthingmodel/syncthingdevicemodel.cpp +++ b/syncthingmodel/syncthingdevicemodel.cpp @@ -29,6 +29,7 @@ QHash SyncthingDeviceModel::roleNames() const { Qt::DecorationRole, "statusIcon" }, { DevicePaused, "paused" }, { IsOwnDevice, "isOwnDevice" }, + { IsPinned, "isPinned" }, { DeviceStatusString, "statusString" }, { DeviceStatusColor, "statusColor" }, { DeviceId, "devId" }, @@ -298,12 +299,13 @@ QVariant SyncthingDeviceModel::data(const QModelIndex &index, int role) const return devStatusColor(dev); } break; + case IsPinned: + case IsOwnDevice: + return dev.status == SyncthingDevStatus::ThisDevice; case DeviceStatus: return static_cast(dev.status); case DevicePaused: return dev.paused; - case IsOwnDevice: - return dev.status == SyncthingDevStatus::ThisDevice; case DeviceStatusString: return devStatusString(dev); case DeviceStatusColor: diff --git a/syncthingmodel/syncthingdevicemodel.h b/syncthingmodel/syncthingdevicemodel.h index 9af0140..43a5aec 100644 --- a/syncthingmodel/syncthingdevicemodel.h +++ b/syncthingmodel/syncthingdevicemodel.h @@ -15,7 +15,7 @@ class LIB_SYNCTHING_MODEL_EXPORT SyncthingDeviceModel : public SyncthingModel { Q_OBJECT public: enum SyncthingDeviceModelRole { - DeviceStatus = Qt::UserRole + 1, + DeviceStatus = SyncthingModelUserRole + 1, DevicePaused, IsOwnDevice, DeviceStatusString, diff --git a/syncthingmodel/syncthingdirectorymodel.h b/syncthingmodel/syncthingdirectorymodel.h index 5fb79c6..5939067 100644 --- a/syncthingmodel/syncthingdirectorymodel.h +++ b/syncthingmodel/syncthingdirectorymodel.h @@ -15,7 +15,7 @@ class LIB_SYNCTHING_MODEL_EXPORT SyncthingDirectoryModel : public SyncthingModel Q_OBJECT public: enum SyncthingDirectoryModelRole { - DirectoryStatus = Qt::UserRole + 1, + DirectoryStatus = SyncthingModelUserRole + 1, DirectoryPaused, DirectoryStatusString, DirectoryStatusColor, diff --git a/syncthingmodel/syncthingdownloadmodel.h b/syncthingmodel/syncthingdownloadmodel.h index 3f59fcf..fc5de6c 100644 --- a/syncthingmodel/syncthingdownloadmodel.h +++ b/syncthingmodel/syncthingdownloadmodel.h @@ -20,7 +20,7 @@ class LIB_SYNCTHING_MODEL_EXPORT SyncthingDownloadModel : public SyncthingModel public: explicit SyncthingDownloadModel(SyncthingConnection &connection, QObject *parent = nullptr); - enum SyncthingDownloadModelRole { ItemPercentage = Qt::UserRole + 1, ItemProgressLabel, ItemPath }; + enum SyncthingDownloadModelRole { ItemPercentage = SyncthingModelUserRole + 1, ItemProgressLabel, ItemPath }; public Q_SLOTS: QHash roleNames() const override; diff --git a/syncthingmodel/syncthingmodel.h b/syncthingmodel/syncthingmodel.h index 9290ffb..867f546 100644 --- a/syncthingmodel/syncthingmodel.h +++ b/syncthingmodel/syncthingmodel.h @@ -19,6 +19,11 @@ class LIB_SYNCTHING_MODEL_EXPORT SyncthingModel : public QAbstractItemModel { Q_PROPERTY(bool brightColors READ brightColors WRITE setBrightColors) public: + enum SyncthingModelRole { + IsPinned = Qt::UserRole + 1, + SyncthingModelUserRole = Qt::UserRole + 100, + }; + explicit SyncthingModel(SyncthingConnection &connection, QObject *parent = nullptr); Data::SyncthingConnection *connection(); const Data::SyncthingConnection *connection() const; diff --git a/syncthingmodel/syncthingrecentchangesmodel.h b/syncthingmodel/syncthingrecentchangesmodel.h index b13339c..d08371b 100644 --- a/syncthingmodel/syncthingrecentchangesmodel.h +++ b/syncthingmodel/syncthingrecentchangesmodel.h @@ -23,7 +23,7 @@ class LIB_SYNCTHING_MODEL_EXPORT SyncthingRecentChangesModel : public SyncthingM Q_PROPERTY(int maxRows READ maxRows WRITE setMaxRows) public: enum SyncthingRecentChangesModelRole { - Action = Qt::UserRole + 1, + Action = SyncthingModelUserRole + 1, ActionIcon, ModifiedBy, DirectoryId, diff --git a/syncthingmodel/syncthingsortfiltermodel.cpp b/syncthingmodel/syncthingsortfiltermodel.cpp index 0f6c98a..d98e17f 100644 --- a/syncthingmodel/syncthingsortfiltermodel.cpp +++ b/syncthingmodel/syncthingsortfiltermodel.cpp @@ -1,4 +1,5 @@ #include "./syncthingsortfiltermodel.h" +#include "./syncthingmodel.h" #include @@ -20,6 +21,12 @@ bool SyncthingSortFilterModel::lessThan(const QModelIndex &left, const QModelInd if (m_behavior == SyncthingSortBehavior::KeepRawOrder || left.parent().isValid() || right.parent().isValid()) { return left.row() < right.row(); } + // show pinned items before all other items + const auto leftPinned = left.data(SyncthingModel::IsPinned).toBool(); + const auto rightPinned = right.data(SyncthingModel::IsPinned).toBool(); + if (leftPinned != rightPinned) { + return leftPinned; + } // use the default sorting for the top-level return QSortFilterProxyModel::lessThan(left, right); }