Messagelib

dkimmanagerkeymodel.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2025 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "dkimmanagerkeymodel.h"
8#include <KLocalizedString>
9using namespace MessageViewer;
10
11DKIMManagerKeyModel::DKIMManagerKeyModel(QObject *parent)
12 : QAbstractListModel{parent}
13{
14}
15
16DKIMManagerKeyModel::~DKIMManagerKeyModel() = default;
17
18QList<MessageViewer::KeyInfo> DKIMManagerKeyModel::keyInfos() const
19{
20 return mKeyInfos;
21}
22
23void DKIMManagerKeyModel::setKeyInfos(const QList<MessageViewer::KeyInfo> &newKeyInfos)
24{
26 mKeyInfos = newKeyInfos;
28}
29
30int DKIMManagerKeyModel::rowCount(const QModelIndex &parent) const
31{
32 if (parent.isValid()) {
33 return 0; // flat model
34 }
35 return mKeyInfos.count();
36}
37
38int DKIMManagerKeyModel::columnCount(const QModelIndex &parent) const
39{
40 Q_UNUSED(parent)
41 return static_cast<int>(DKIMManagerKeyRoles::LastColumn) + 1;
42}
43
44QVariant DKIMManagerKeyModel::data(const QModelIndex &index, int role) const
45{
46 if (index.row() < 0 || index.row() >= mKeyInfos.count()) {
47 return {};
48 }
49 const KeyInfo &keyInfo = mKeyInfos.at(index.row());
50 if (role != Qt::DisplayRole) {
51 return {};
52 }
53 switch (static_cast<DKIMManagerKeyRoles>(index.column())) {
54 case KeyRole: {
55 return keyInfo.keyValue;
56 }
57 case SelectorRole:
58 return keyInfo.selector;
59 case DomainRole:
60 return keyInfo.domain;
61 case StoredAtDateTimeRoleStr:
62 return QLocale().toString(keyInfo.storedAtDateTime);
63 case StoredAtDateTimeRole:
64 return keyInfo.storedAtDateTime;
65 case LastUsedDateTimeRole:
66 return keyInfo.lastUsedDateTime;
67 case LastUsedDateTimeRoleStr:
68 return QLocale().toString(keyInfo.lastUsedDateTime);
69 }
70 return {};
71}
72
73QVariant DKIMManagerKeyModel::headerData(int section, Qt::Orientation orientation, int role) const
74{
75 if (orientation != Qt::Horizontal || role != Qt::DisplayRole) {
76 return {};
77 }
78
79 switch (static_cast<DKIMManagerKeyRoles>(section)) {
80 case KeyRole:
81 return i18n("DKIM Key");
82 case SelectorRole:
83 return i18n("Selector");
84 case DomainRole:
85 return i18n("SDID");
86 case StoredAtDateTimeRoleStr:
87 return i18n("Inserted");
88 case LastUsedDateTimeRoleStr:
89 return i18n("Last Used");
90 case StoredAtDateTimeRole:
91 case LastUsedDateTimeRole:
92 break;
93 }
94 return {};
95}
96
97void DKIMManagerKeyModel::clear()
98{
99 if (!mKeyInfos.isEmpty()) {
101 mKeyInfos.clear();
103 }
104}
105
106bool DKIMManagerKeyModel::insertKeyInfo(const KeyInfo &keyInfo)
107{
108 bool found = false;
109 auto it = std::find_if(mKeyInfos.cbegin(), mKeyInfos.cend(), [keyInfo](const KeyInfo &key) {
110 return key == keyInfo;
111 });
112 if (it == mKeyInfos.cend()) {
113 beginInsertRows(QModelIndex(), mKeyInfos.count() - 1, mKeyInfos.count());
114 mKeyInfos.append(keyInfo);
116 } else {
117 found = true;
118 }
119 return found;
120}
121
122void DKIMManagerKeyModel::removeKeyInfo(const QString &keyValue)
123{
124 auto it = std::find_if(mKeyInfos.cbegin(), mKeyInfos.cend(), [keyValue](const KeyInfo &key) {
125 return key.keyValue == keyValue;
126 });
127 if (it != mKeyInfos.cend()) {
129 mKeyInfos.removeAll(*it);
131 }
132}
133
134void DKIMManagerKeyModel::removeKeyInfos(const QStringList &keyInfos)
135{
136 if (keyInfos.isEmpty()) {
137 return;
138 }
140 for (const auto &keyInfo : keyInfos) {
141 auto it = std::find_if(mKeyInfos.cbegin(), mKeyInfos.cend(), [keyInfo](const KeyInfo &key) {
142 return key.keyValue == keyInfo;
143 });
144 if (it != mKeyInfos.cend()) {
145 mKeyInfos.removeAll(*it);
146 }
147 }
149}
150
151#include "moc_dkimmanagerkeymodel.cpp"
QString i18n(const char *text, const TYPE &arg...)
void beginInsertRows(const QModelIndex &parent, int first, int last)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual QModelIndex parent(const QModelIndex &index) const const=0
QString toString(QStringView format, QCalendar cal) const const
DisplayRole
Orientation
The KeyInfo struct.
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 11:50:07 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.