File indexing completed on 2025-03-09 04:54:30

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "dkimmanagerkeymodel.h"
0008 #include <KLocalizedString>
0009 using namespace MessageViewer;
0010 
0011 DKIMManagerKeyModel::DKIMManagerKeyModel(QObject *parent)
0012     : QAbstractListModel{parent}
0013 {
0014 }
0015 
0016 DKIMManagerKeyModel::~DKIMManagerKeyModel() = default;
0017 
0018 QList<MessageViewer::KeyInfo> DKIMManagerKeyModel::keyInfos() const
0019 {
0020     return mKeyInfos;
0021 }
0022 
0023 void DKIMManagerKeyModel::setKeyInfos(const QList<MessageViewer::KeyInfo> &newKeyInfos)
0024 {
0025     beginResetModel();
0026     mKeyInfos = newKeyInfos;
0027     endResetModel();
0028 }
0029 
0030 int DKIMManagerKeyModel::rowCount(const QModelIndex &parent) const
0031 {
0032     if (parent.isValid()) {
0033         return 0; // flat model
0034     }
0035     return mKeyInfos.count();
0036 }
0037 
0038 int DKIMManagerKeyModel::columnCount(const QModelIndex &parent) const
0039 {
0040     Q_UNUSED(parent)
0041     return static_cast<int>(DKIMManagerKeyRoles::LastColumn) + 1;
0042 }
0043 
0044 QVariant DKIMManagerKeyModel::data(const QModelIndex &index, int role) const
0045 {
0046     if (index.row() < 0 || index.row() >= mKeyInfos.count()) {
0047         return {};
0048     }
0049     const KeyInfo &keyInfo = mKeyInfos.at(index.row());
0050     if (role != Qt::DisplayRole) {
0051         return {};
0052     }
0053     switch (static_cast<DKIMManagerKeyRoles>(index.column())) {
0054     case KeyRole: {
0055         return keyInfo.keyValue;
0056     }
0057     case SelectorRole:
0058         return keyInfo.selector;
0059     case DomainRole:
0060         return keyInfo.domain;
0061     case StoredAtDateTimeRole:
0062         return keyInfo.storedAtDateTime.toString();
0063     case LastUsedDateTimeRole:
0064         return keyInfo.lastUsedDateTime.toString();
0065     }
0066     return {};
0067 }
0068 
0069 QVariant DKIMManagerKeyModel::headerData(int section, Qt::Orientation orientation, int role) const
0070 {
0071     if (orientation != Qt::Horizontal || role != Qt::DisplayRole) {
0072         return {};
0073     }
0074 
0075     switch (static_cast<DKIMManagerKeyRoles>(section)) {
0076     case KeyRole:
0077         return i18n("DKIM Key");
0078     case SelectorRole:
0079         return i18n("Selector");
0080     case DomainRole:
0081         return i18n("SDID");
0082     case StoredAtDateTimeRole:
0083         return i18n("Inserted");
0084     case LastUsedDateTimeRole:
0085         return i18n("Last Used");
0086     }
0087     return {};
0088 }
0089 
0090 void DKIMManagerKeyModel::clear()
0091 {
0092     if (!mKeyInfos.isEmpty()) {
0093         beginResetModel();
0094         mKeyInfos.clear();
0095         endResetModel();
0096     }
0097 }
0098 
0099 bool DKIMManagerKeyModel::insertKeyInfo(const KeyInfo &keyInfo)
0100 {
0101     bool found = false;
0102     auto it = std::find_if(mKeyInfos.cbegin(), mKeyInfos.cend(), [keyInfo](const KeyInfo &key) {
0103         return key == keyInfo;
0104     });
0105     if (it == mKeyInfos.cend()) {
0106         beginInsertRows(QModelIndex(), mKeyInfos.count() - 1, mKeyInfos.count());
0107         mKeyInfos.append(keyInfo);
0108         endInsertRows();
0109     } else {
0110         found = true;
0111     }
0112     return found;
0113 }
0114 
0115 void DKIMManagerKeyModel::removeKeyInfo(const QString &keyValue)
0116 {
0117     auto it = std::find_if(mKeyInfos.cbegin(), mKeyInfos.cend(), [keyValue](const KeyInfo &key) {
0118         return key.keyValue == keyValue;
0119     });
0120     if (it != mKeyInfos.cend()) {
0121         beginResetModel();
0122         mKeyInfos.removeAll(*it);
0123         endResetModel();
0124     }
0125 }
0126 
0127 void DKIMManagerKeyModel::removeKeyInfos(const QStringList &keyInfos)
0128 {
0129     if (keyInfos.isEmpty()) {
0130         return;
0131     }
0132     beginResetModel();
0133     for (const auto &keyInfo : keyInfos) {
0134         auto it = std::find_if(mKeyInfos.cbegin(), mKeyInfos.cend(), [keyInfo](const KeyInfo &key) {
0135             return key.keyValue == keyInfo;
0136         });
0137         if (it != mKeyInfos.cend()) {
0138             mKeyInfos.removeAll(*it);
0139         }
0140     }
0141     endResetModel();
0142 }
0143 
0144 #include "moc_dkimmanagerkeymodel.cpp"