|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +#include "governancelabelslistmodel.h" |
| 7 | + |
| 8 | +#include <QLoggingCategory> |
| 9 | +#include <QJsonDocument> |
| 10 | +#include <QJsonObject> |
| 11 | +#include <QJsonArray> |
| 12 | + |
| 13 | +using namespace Qt::StringLiterals; |
| 14 | + |
| 15 | +namespace OCC |
| 16 | +{ |
| 17 | + |
| 18 | +Q_LOGGING_CATEGORY(lcGovernanceLabelsListModel, "nextcloud.gui.governance.labelslistmodel", QtInfoMsg) |
| 19 | + |
| 20 | +GovernanceLabelsListModel::GovernanceLabelsListModel(QObject *parent) |
| 21 | + : QAbstractListModel(parent) |
| 22 | +{ |
| 23 | +} |
| 24 | + |
| 25 | +int GovernanceLabelsListModel::rowCount(const QModelIndex &parent) const |
| 26 | +{ |
| 27 | + auto result = 0; |
| 28 | + if (parent.isValid()) { |
| 29 | + return result; |
| 30 | + } |
| 31 | + |
| 32 | + result = _data.count(); |
| 33 | + return result; |
| 34 | +} |
| 35 | + |
| 36 | +QVariant GovernanceLabelsListModel::data(const QModelIndex &index, int role) const |
| 37 | +{ |
| 38 | + auto result = QVariant{}; |
| 39 | + |
| 40 | + if (!index.isValid()) { |
| 41 | + return result; |
| 42 | + } |
| 43 | + |
| 44 | + if (index.column() != 0) { |
| 45 | + return result; |
| 46 | + } |
| 47 | + |
| 48 | + if (index.row() < 0 || index.row() >= _data.count()) { |
| 49 | + return result; |
| 50 | + } |
| 51 | + |
| 52 | + if (role >= Qt::UserRole + 1) { |
| 53 | + auto convertedRole = static_cast<LabelsListModelRoles>(role); |
| 54 | + |
| 55 | + switch (convertedRole) |
| 56 | + { |
| 57 | + case LabelsListModelRoles::IdRole: |
| 58 | + result = _data[index.row()]._id; |
| 59 | + break; |
| 60 | + case LabelsListModelRoles::NameRole: |
| 61 | + result = _data[index.row()]._name; |
| 62 | + break; |
| 63 | + case LabelsListModelRoles::PriorityRole: |
| 64 | + result = _data[index.row()]._priority; |
| 65 | + break; |
| 66 | + case LabelsListModelRoles::DescriptionRole: |
| 67 | + result = _data[index.row()]._description; |
| 68 | + break; |
| 69 | + case LabelsListModelRoles::ColorRole: |
| 70 | + result = _data[index.row()]._color; |
| 71 | + break; |
| 72 | + case LabelsListModelRoles::ScopesRole: |
| 73 | + result = _data[index.row()]._scopes; |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return result; |
| 79 | +} |
| 80 | + |
| 81 | +QHash<int, QByteArray> GovernanceLabelsListModel::roleNames() const |
| 82 | +{ |
| 83 | + auto result = QHash<int, QByteArray>{ |
| 84 | + {static_cast<int>(LabelsListModelRoles::IdRole), "id"_ba}, |
| 85 | + {static_cast<int>(LabelsListModelRoles::NameRole), "name"_ba}, |
| 86 | + {static_cast<int>(LabelsListModelRoles::PriorityRole), "priority"_ba}, |
| 87 | + {static_cast<int>(LabelsListModelRoles::DescriptionRole), "description"_ba}, |
| 88 | + {static_cast<int>(LabelsListModelRoles::ColorRole), "color"_ba}, |
| 89 | + {static_cast<int>(LabelsListModelRoles::ScopesRole), "scopes"_ba}, |
| 90 | + }; |
| 91 | + |
| 92 | + return result; |
| 93 | +} |
| 94 | + |
| 95 | +Governance::LabelType GovernanceLabelsListModel::labelType() const |
| 96 | +{ |
| 97 | + return _labelType; |
| 98 | +} |
| 99 | + |
| 100 | +void GovernanceLabelsListModel::setLabelType(Governance::LabelType newLabelType) |
| 101 | +{ |
| 102 | + if (_labelType == newLabelType) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + _labelType = newLabelType; |
| 107 | + Q_EMIT labelTypeChanged(); |
| 108 | + |
| 109 | + emitRefreshData(); |
| 110 | +} |
| 111 | + |
| 112 | +QString GovernanceLabelsListModel::entityId() const |
| 113 | +{ |
| 114 | + return _entityId; |
| 115 | +} |
| 116 | + |
| 117 | +void GovernanceLabelsListModel::setEntityId(const QString &newEntityId) |
| 118 | +{ |
| 119 | + if (_entityId == newEntityId) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + _entityId = newEntityId; |
| 124 | + Q_EMIT entityIdChanged(); |
| 125 | + |
| 126 | + emitRefreshData(); |
| 127 | +} |
| 128 | + |
| 129 | +void GovernanceLabelsListModel::setAvailableLabelsJsonData(const QJsonDocument &reply) |
| 130 | +{ |
| 131 | + const auto replyObject = reply.object(); |
| 132 | + |
| 133 | + if (!replyObject.contains(u"ocs"_s)) { |
| 134 | + qCWarning(lcGovernanceLabelsListModel()) << "wrong format for reply" << reply.toJson(QJsonDocument::JsonFormat::Compact); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + const auto ocsObject = replyObject.value(u"ocs"_s).toObject(); |
| 139 | + |
| 140 | + if (!ocsObject.contains(u"data"_s)) { |
| 141 | + qCWarning(lcGovernanceLabelsListModel()) << "wrong format for reply" << ocsObject; |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + const auto dataArray = ocsObject.value(u"data"_s).toArray(); |
| 146 | + |
| 147 | + const auto convertToStringList = [] (const QJsonArray &scopesList) -> QStringList |
| 148 | + { |
| 149 | + auto result = QStringList{}; |
| 150 | + |
| 151 | + for (const auto &oneScope : scopesList) { |
| 152 | + result << oneScope.toString(); |
| 153 | + } |
| 154 | + |
| 155 | + return result; |
| 156 | + }; |
| 157 | + |
| 158 | + beginResetModel(); |
| 159 | + _data.clear(); |
| 160 | + for (const auto oneLabel : dataArray) { |
| 161 | + const auto oneLabelObject = oneLabel.toObject(); |
| 162 | + _data.emplaceBack(oneLabelObject.value(u"id"_s).toString(), |
| 163 | + oneLabelObject.value(u"name"_s).toString(), |
| 164 | + oneLabelObject.value(u"priority"_s).toInt(), |
| 165 | + oneLabelObject.value(u"description"_s).toString(), |
| 166 | + oneLabelObject.value(u"color"_s).toString(), |
| 167 | + convertToStringList(oneLabelObject.value(u"scopes"_s).toArray()) |
| 168 | + ); |
| 169 | + } |
| 170 | + endResetModel(); |
| 171 | +} |
| 172 | + |
| 173 | +void GovernanceLabelsListModel::setExistingLabelsJsonData(const QJsonDocument &data) |
| 174 | +{ |
| 175 | + qCInfo(lcGovernanceLabelsListModel()) << data.toJson(QJsonDocument::JsonFormat::Compact); |
| 176 | +} |
| 177 | + |
| 178 | +void OCC::GovernanceLabelsListModel::etagChanged() |
| 179 | +{ |
| 180 | + Q_EMIT refreshData(_labelType, _entityId); |
| 181 | +} |
| 182 | + |
| 183 | +void GovernanceLabelsListModel::emitRefreshData() |
| 184 | +{ |
| 185 | + if (_entityId.isEmpty() || _labelType == Governance::LabelType::Invalid) { |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + Q_EMIT refreshData(_labelType, _entityId); |
| 190 | +} |
| 191 | + |
| 192 | +} // namespace OCC |
0 commit comments