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>
9 using namespace MessageViewer;
10 
11 DKIMManagerKeyModel::DKIMManagerKeyModel(QObject *parent)
13 {
14 }
15 
16 DKIMManagerKeyModel::~DKIMManagerKeyModel() = default;
17 
18 QList<MessageViewer::KeyInfo> DKIMManagerKeyModel::keyInfos() const
19 {
20  return mKeyInfos;
21 }
22 
23 void DKIMManagerKeyModel::setKeyInfos(const QList<MessageViewer::KeyInfo> &newKeyInfos)
24 {
25  beginResetModel();
26  mKeyInfos = newKeyInfos;
27  endResetModel();
28 }
29 
30 int DKIMManagerKeyModel::rowCount(const QModelIndex &parent) const
31 {
32  if (parent.isValid()) {
33  return 0; // flat model
34  }
35  return mKeyInfos.count();
36 }
37 
38 int DKIMManagerKeyModel::columnCount(const QModelIndex &parent) const
39 {
40  Q_UNUSED(parent)
41  return static_cast<int>(DKIMManagerKeyRoles::LastColumn) + 1;
42 }
43 
44 QVariant 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 keyInfo.storedAtDateTime.toString();
63  case LastUsedDateTimeRole:
64  return keyInfo.lastUsedDateTime.toString();
65  }
66  return {};
67 }
68 
69 QVariant 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 
90 void DKIMManagerKeyModel::clear()
91 {
92  if (!mKeyInfos.isEmpty()) {
93  beginResetModel();
94  mKeyInfos.clear();
95  endResetModel();
96  }
97 }
98 
99 bool 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);
108  endInsertRows();
109  } else {
110  found = true;
111  }
112  return found;
113 }
114 
115 void 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()) {
121  beginResetModel();
122  mKeyInfos.removeAll(*it);
123  endResetModel();
124  }
125 }
126 
127 void DKIMManagerKeyModel::removeKeyInfos(const QStringList &keyInfos)
128 {
129  if (keyInfos.isEmpty()) {
130  return;
131  }
132  beginResetModel();
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  }
141  endResetModel();
142 }
143 
144 #include "moc_dkimmanagerkeymodel.cpp"
DisplayRole
The KeyInfo struct.
int column() const const
QString i18n(const char *text, const TYPE &arg...)
Orientation
bool isEmpty() const const
bool isValid() const const
int row() const const
QString toString(Qt::DateFormat format) const const
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:55:20 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.