Messagelib

dkimmanagerkeymodel.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.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 StoredAtDateTimeRole:
62 return QLocale().toString(keyInfo.storedAtDateTime);
63 case LastUsedDateTimeRole:
64 return QLocale().toString(keyInfo.lastUsedDateTime);
65 }
66 return {};
67}
68
69QVariant DKIMManagerKeyModel::headerData(int section, Qt::Orientation orientation, int role) const
70{
71 if (orientation != Qt::Horizontal || role != Qt::DisplayRole) {
72 return {};
73 }
74
75 switch (static_cast<DKIMManagerKeyRoles>(section)) {
76 case KeyRole:
77 return i18n("DKIM Key");
78 case SelectorRole:
79 return i18n("Selector");
80 case DomainRole:
81 return i18n("SDID");
82 case StoredAtDateTimeRole:
83 return i18n("Inserted");
84 case LastUsedDateTimeRole:
85 return i18n("Last Used");
86 }
87 return {};
88}
89
90void DKIMManagerKeyModel::clear()
91{
92 if (!mKeyInfos.isEmpty()) {
94 mKeyInfos.clear();
96 }
97}
98
99bool DKIMManagerKeyModel::insertKeyInfo(const KeyInfo &keyInfo)
100{
101 bool found = false;
102 auto it = std::find_if(mKeyInfos.cbegin(), mKeyInfos.cend(), [keyInfo](const KeyInfo &key) {
103 return key == keyInfo;
104 });
105 if (it == mKeyInfos.cend()) {
106 beginInsertRows(QModelIndex(), mKeyInfos.count() - 1, mKeyInfos.count());
107 mKeyInfos.append(keyInfo);
109 } else {
110 found = true;
111 }
112 return found;
113}
114
115void DKIMManagerKeyModel::removeKeyInfo(const QString &keyValue)
116{
117 auto it = std::find_if(mKeyInfos.cbegin(), mKeyInfos.cend(), [keyValue](const KeyInfo &key) {
118 return key.keyValue == keyValue;
119 });
120 if (it != mKeyInfos.cend()) {
122 mKeyInfos.removeAll(*it);
124 }
125}
126
127void DKIMManagerKeyModel::removeKeyInfos(const QStringList &keyInfos)
128{
129 if (keyInfos.isEmpty()) {
130 return;
131 }
133 for (const auto &keyInfo : keyInfos) {
134 auto it = std::find_if(mKeyInfos.cbegin(), mKeyInfos.cend(), [keyInfo](const KeyInfo &key) {
135 return key.keyValue == keyInfo;
136 });
137 if (it != mKeyInfos.cend()) {
138 mKeyInfos.removeAll(*it);
139 }
140 }
142}
143
144#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 override
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
const_iterator cbegin() const const
const_iterator cend() const const
void clear()
qsizetype count() const const
bool isEmpty() const const
qsizetype removeAll(const AT &t)
QString toString(QDate date, FormatType format) const const
int column() const const
int row() const const
QObject * parent() const const
DisplayRole
Orientation
The KeyInfo struct.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.