• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

kgpg

  • sources
  • kde-4.14
  • kdeutils
  • kgpg
  • model
selectkeyproxymodel.cpp
Go to the documentation of this file.
1 /* Copyright 2008,2010,2012,2013 Rolf Eike Beer <kde@opensource.sf-tec.de>
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public License as
5  * published by the Free Software Foundation; either version 2 of
6  * the License or (at your option) version 3 or any later version
7  * accepted by the membership of KDE e.V. (or its successor approved
8  * by the membership of KDE e.V.), which shall act as a proxy
9  * defined in Section 14 of version 3 of the license.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "selectkeyproxymodel.h"
20 #include "model/kgpgitemnode.h"
21 #include "kgpgitemmodel.h"
22 #include "core/kgpgkey.h"
23 
24 #include <KGlobal>
25 #include <KLocale>
26 
27 using namespace KgpgCore;
28 
29 SelectKeyProxyModel::SelectKeyProxyModel(QObject *parent)
30  : QSortFilterProxyModel(parent),
31  m_model(NULL),
32  m_showUntrusted(false)
33 {
34 }
35 
36 void
37 SelectKeyProxyModel::setKeyModel(KGpgItemModel *md)
38 {
39  m_model = md;
40  setSourceModel(md);
41 }
42 
43 bool
44 SelectKeyProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
45 {
46  QModelIndex idx = m_model->index(source_row, 0, source_parent);
47  KGpgNode *l = m_model->nodeForIndex(idx);
48 
49  switch (l->getType()) {
50  case ITYPE_GROUP:
51  break;
52  case ITYPE_PAIR:
53  case ITYPE_PUBLIC:
54  if (!l->toKeyNode()->canEncrypt())
55  return false;
56  break;
57  default:
58  return false;
59  }
60 
61  if (!m_showUntrusted && ((l->getTrust() != TRUST_FULL) && (l->getTrust() != TRUST_ULTIMATE)))
62  return false;
63 
64  // there is probably a better place to do this
65  QRegExp rx = filterRegExp();
66  rx.setCaseSensitivity(Qt::CaseInsensitive);
67 
68  if (l->getName().contains(rx))
69  return true;
70 
71  if (l->getEmail().contains(rx))
72  return true;
73 
74  if (l->getId().contains(rx))
75  return true;
76 
77  return false;
78 }
79 
80 KGpgNode *
81 SelectKeyProxyModel::nodeForIndex(const QModelIndex &index) const
82 {
83  return m_model->nodeForIndex(mapToSource(index));
84 }
85 
86 int
87 SelectKeyProxyModel::columnCount(const QModelIndex &) const
88 {
89  return 3;
90 }
91 
92 int
93 SelectKeyProxyModel::rowCount(const QModelIndex &parent) const
94 {
95  if (parent.column() > 0)
96  return 0;
97  if (parent.isValid())
98  return 0;
99  if (m_model == NULL)
100  return 0;
101  return QSortFilterProxyModel::rowCount(parent);
102 }
103 
104 QVariant
105 SelectKeyProxyModel::data(const QModelIndex &index, int role) const
106 {
107  if (index.column() >= 3)
108  return QVariant();
109 
110  QModelIndex sidx = mapToSource(index);
111  KGpgNode *nd = m_model->nodeForIndex(sidx);
112 
113  if ((index.column() == 2) && (role == Qt::ToolTipRole))
114  return nd->getId();
115 
116  if ((role != Qt::DisplayRole) && (index.column() <= 1))
117  return m_model->data(sidx, role);
118 
119  if (role != Qt::DisplayRole)
120  return QVariant();
121 
122  switch (index.column()) {
123  case 0: return nd->getName();
124  case 1: return nd->getEmail();
125  case 2: return nd->getId().right(8);
126  }
127 
128  return QVariant();
129 }
130 
131 bool
132 SelectKeyProxyModel::hasChildren(const QModelIndex &parent) const
133 {
134  if (m_model == NULL)
135  return false;
136  return !parent.isValid();
137 }
138 
139 QVariant
140 SelectKeyProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
141 {
142  if (role != Qt::DisplayRole)
143  return QVariant();
144 
145  if (orientation != Qt::Horizontal)
146  return QVariant();
147 
148  if (m_model == NULL)
149  return QVariant();
150 
151  switch (section) {
152  case 0:
153  return m_model->headerData(KEYCOLUMN_NAME, orientation, role);
154  case 1:
155  return m_model->headerData(KEYCOLUMN_EMAIL, orientation, role);
156  case 2:
157  return m_model->headerData(KEYCOLUMN_ID, orientation, role);
158  default:
159  return QVariant();
160  }
161 }
162 
163 bool
164 SelectKeyProxyModel::showUntrusted() const
165 {
166  return m_showUntrusted;
167 }
168 
169 void
170 SelectKeyProxyModel::setShowUntrusted(const bool b)
171 {
172  m_showUntrusted = b;
173  invalidate();
174 }
175 
176 SelectSecretKeyProxyModel::SelectSecretKeyProxyModel(QObject *parent)
177  : SelectKeyProxyModel(parent)
178 {
179 }
180 
181 bool
182 SelectSecretKeyProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
183 {
184  QModelIndex idx = m_model->index(source_row, 0, source_parent);
185  KGpgNode *l = m_model->nodeForIndex(idx);
186 
187  return ((l->getType() == ITYPE_PAIR) && !(l->getTrust() == TRUST_EXPIRED) && !(l->getTrust() == TRUST_DISABLED));
188 }
189 
190 int
191 SelectSecretKeyProxyModel::columnCount(const QModelIndex &) const
192 {
193  return 4;
194 }
195 
196 int
197 SelectSecretKeyProxyModel::rowCount(const QModelIndex &parent) const
198 {
199  if (parent.column() > 0)
200  return 0;
201  if (parent.isValid())
202  return 0;
203  if (m_model == NULL)
204  return 0;
205  return QSortFilterProxyModel::rowCount(parent);
206 }
207 
208 QVariant
209 SelectSecretKeyProxyModel::data(const QModelIndex &index, int role) const
210 {
211  if (index.column() >= 4)
212  return QVariant();
213 
214  QModelIndex sidx = mapToSource(index);
215  KGpgNode *nd = m_model->nodeForIndex(sidx);
216 
217  if ((index.column() == 3) && (role == Qt::ToolTipRole))
218  return nd->getId();
219 
220  if ((role != Qt::DisplayRole) && (index.column() <= 1))
221  return m_model->data(sidx, role);
222 
223  if (role != Qt::DisplayRole)
224  return QVariant();
225 
226  switch (index.column()) {
227  case 0:
228  return nd->getName();
229  case 1:
230  return nd->getEmail();
231  case 2:
232  return KGlobal::locale()->formatDate(nd->getExpiration().date(), KLocale::ShortDate);
233  case 3:
234  return nd->getId().right(8);
235  }
236 
237  return QVariant();
238 }
239 
240 bool
241 SelectSecretKeyProxyModel::hasChildren(const QModelIndex &parent) const
242 {
243  if (m_model == NULL)
244  return false;
245  return !parent.isValid();
246 }
247 
248 QVariant
249 SelectSecretKeyProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
250 {
251  if (role != Qt::DisplayRole)
252  return QVariant();
253 
254  if (orientation != Qt::Horizontal)
255  return QVariant();
256 
257  if (m_model == NULL)
258  return QVariant();
259 
260  switch (section) {
261  case 0:
262  return m_model->headerData(KEYCOLUMN_NAME, orientation, role);
263  case 1:
264  return m_model->headerData(KEYCOLUMN_EMAIL, orientation, role);
265  case 2:
266  return m_model->headerData(KEYCOLUMN_EXPIR, orientation, role);
267  case 3:
268  return m_model->headerData(KEYCOLUMN_ID, orientation, role);
269  default:
270  return QVariant();
271  }
272 }
QSortFilterProxyModel::invalidate
void invalidate()
SelectKeyProxyModel::nodeForIndex
KGpgNode * nodeForIndex(const QModelIndex &index) const
Definition: selectkeyproxymodel.cpp:81
KGpgItemModel
Definition: kgpgitemmodel.h:44
QModelIndex
QSortFilterProxyModel::setSourceModel
virtual void setSourceModel(QAbstractItemModel *sourceModel)
SelectKeyProxyModel
filter model to select a public key for encryption
Definition: selectkeyproxymodel.h:30
SelectSecretKeyProxyModel::filterAcceptsRow
virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
Definition: selectkeyproxymodel.cpp:182
SelectKeyProxyModel::rowCount
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: selectkeyproxymodel.cpp:93
kgpgkey.h
SelectKeyProxyModel::SelectKeyProxyModel
SelectKeyProxyModel(QObject *parent)
Definition: selectkeyproxymodel.cpp:29
SelectSecretKeyProxyModel::rowCount
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: selectkeyproxymodel.cpp:197
SelectSecretKeyProxyModel::data
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: selectkeyproxymodel.cpp:209
QSortFilterProxyModel::rowCount
virtual int rowCount(const QModelIndex &parent) const
SelectKeyProxyModel::showUntrusted
bool showUntrusted() const
QRegExp
KGpgNode::getType
virtual KgpgCore::KgpgItemType getType() const =0
Returns the item type of this object.
QModelIndex::isValid
bool isValid() const
KGpgNode::getExpiration
virtual QDateTime getExpiration() const
Definition: KGpgNode.cpp:306
SelectSecretKeyProxyModel::SelectSecretKeyProxyModel
SelectSecretKeyProxyModel(QObject *parent)
Definition: selectkeyproxymodel.cpp:176
KgpgCore::TRUST_DISABLED
key is disabled by user (not owner)
Definition: kgpgkey.h:56
QObject
kgpgitemmodel.h
KGpgKeyNode::canEncrypt
bool canEncrypt() const
return if this key can be used for encryption
Definition: KGpgKeyNode.cpp:355
KgpgCore::ITYPE_PUBLIC
public key
Definition: kgpgkey.h:93
SelectSecretKeyProxyModel::hasChildren
virtual bool hasChildren(const QModelIndex &parent) const
Definition: selectkeyproxymodel.cpp:241
SelectKeyProxyModel::setKeyModel
void setKeyModel(KGpgItemModel *)
Definition: selectkeyproxymodel.cpp:37
KGpgItemModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Definition: kgpgitemmodel.cpp:44
KGpgItemModel::data
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: kgpgitemmodel.cpp:95
SelectKeyProxyModel::setShowUntrusted
void setShowUntrusted(const bool b)
Definition: selectkeyproxymodel.cpp:170
KgpgCore::TRUST_FULL
you can fully trust this key
Definition: kgpgkey.h:63
KgpgCore::ITYPE_GROUP
the element is a GnuPG key group
Definition: kgpgkey.h:91
SelectKeyProxyModel::filterAcceptsRow
virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
Definition: selectkeyproxymodel.cpp:44
kgpgitemnode.h
QString::right
QString right(int n) const
QRegExp::setCaseSensitivity
void setCaseSensitivity(Qt::CaseSensitivity cs)
KGpgNode
The abstract base class for all classes representing keyring data.
Definition: KGpgNode.h:42
KgpgCore::ITYPE_PAIR
key pair
Definition: kgpgkey.h:94
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
KgpgCore::TRUST_EXPIRED
key is beyond it's expiry date
Definition: kgpgkey.h:58
QSortFilterProxyModel
SelectKeyProxyModel::m_model
KGpgItemModel * m_model
Definition: selectkeyproxymodel.h:54
KEYCOLUMN_EXPIR
#define KEYCOLUMN_EXPIR
Definition: kgpgitemmodel.h:33
KGpgItemModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: kgpgitemmodel.cpp:363
QSortFilterProxyModel::mapToSource
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const
KGpgNode::toKeyNode
KGpgKeyNode * toKeyNode()
Definition: KGpgNode.cpp:94
QDateTime::date
QDate date() const
SelectKeyProxyModel::data
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: selectkeyproxymodel.cpp:105
SelectSecretKeyProxyModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: selectkeyproxymodel.cpp:249
KGpgNode::getId
virtual QString getId() const
Definition: KGpgNode.cpp:318
KGpgNode::getEmail
virtual QString getEmail() const
Definition: KGpgNode.cpp:300
QModelIndex::column
int column() const
KGpgItemModel::nodeForIndex
KGpgNode * nodeForIndex(const QModelIndex &index) const
Definition: kgpgitemmodel.cpp:230
KGpgNode::getName
virtual QString getName() const
Definition: KGpgNode.cpp:294
KgpgCore::TRUST_ULTIMATE
this key has highest possible level of trust (e.g. your own secret keys)
Definition: kgpgkey.h:64
SelectKeyProxyModel::columnCount
virtual int columnCount(const QModelIndex &) const
Definition: selectkeyproxymodel.cpp:87
SelectKeyProxyModel::hasChildren
virtual bool hasChildren(const QModelIndex &parent) const
Definition: selectkeyproxymodel.cpp:132
KGpgNode::getTrust
virtual KgpgCore::KgpgKeyTrust getTrust() const
Definition: KGpgNode.cpp:282
KEYCOLUMN_EMAIL
#define KEYCOLUMN_EMAIL
Definition: kgpgitemmodel.h:31
SelectKeyProxyModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: selectkeyproxymodel.cpp:140
selectkeyproxymodel.h
KEYCOLUMN_NAME
#define KEYCOLUMN_NAME
Definition: kgpgitemmodel.h:30
SelectSecretKeyProxyModel::columnCount
virtual int columnCount(const QModelIndex &) const
Definition: selectkeyproxymodel.cpp:191
QSortFilterProxyModel::filterRegExp
QRegExp filterRegExp() const
QVariant
KEYCOLUMN_ID
#define KEYCOLUMN_ID
Definition: kgpgitemmodel.h:36
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kgpg

Skip menu "kgpg"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal