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

kleopatra

  • sources
  • kde-4.14
  • kdepim
  • kleopatra
  • models
useridlistmodel.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  models/useridlistmodel.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2007 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "useridlistmodel.h"
36 
37 #include <utils/formatting.h>
38 
39 #include <gpgme++/key.h>
40 
41 #include <KLocalizedString>
42 
43 #include <QVariant>
44 #include <QDate>
45 
46 #ifndef Q_MOC_RUN
47 #include <boost/bind.hpp>
48 #endif
49 
50 #include <algorithm>
51 #include <iterator>
52 
53 using namespace GpgME;
54 using namespace Kleo;
55 using namespace boost;
56 
57 namespace {
58 
59  static inline bool is_userid_level( const QModelIndex & idx ) {
60  return idx.isValid() && idx.internalId() < 0 ;
61  }
62 
63  static inline int extract_uid_number( const QModelIndex & idx ) {
64  return idx.internalId();
65  }
66 
67  static inline bool is_signature_level( const QModelIndex & idx ) {
68  return idx.isValid() && idx.internalId() >= 0 ;
69  }
70 
71 }
72 
73 class UserIDListModel::Private {
74  friend class ::Kleo::UserIDListModel;
75  UserIDListModel * const q;
76 public:
77  explicit Private( UserIDListModel * qq )
78  : q( qq ), key() {}
79 
80 private:
81  Key key;
82 };
83 
84 
85 
86 UserIDListModel::UserIDListModel( QObject * p )
87  : QAbstractItemModel( p ), d( new Private( this ) )
88 {
89 
90 }
91 
92 UserIDListModel::~UserIDListModel() {}
93 
94 
95 Key UserIDListModel::key() const {
96  return d->key;
97 }
98 
99 // slot
100 void UserIDListModel::setKey( const Key & key ) {
101 
102  const Key oldKey = d->key;
103 
104  d->key = key;
105 
106  if ( qstricmp( key.primaryFingerprint(), oldKey.primaryFingerprint() ) != 0 ) {
107  // different key -> reset
108  reset();
109  return;
110  }
111 
112  // ### diff them, and signal more fine-grained than this:
113 
114  if ( key.numUserIDs() > 0 && oldKey.numUserIDs() == key.numUserIDs() ) {
115  bool identical = true;
116  for ( unsigned int i = 0, end = key.numUserIDs() ; i != end ; ++i ) {
117  if ( key.userID( i ).numSignatures() != oldKey.userID( i ).numSignatures() ) {
118  identical = false;
119  break;
120  }
121  }
122  if ( identical ) {
123  emit dataChanged( index( 0, 0 ), index( key.numUserIDs() - 1, NumColumns - 1 ) );
124  return;
125  }
126  }
127  emit layoutAboutToBeChanged();
128  emit layoutChanged();
129 }
130 
131 UserID UserIDListModel::userID( const QModelIndex & idx, bool strict ) const {
132  if ( is_userid_level( idx ) )
133  return d->key.userID( idx.row() );
134  if ( !strict && is_signature_level( idx ) )
135  return d->key.userID( extract_uid_number( idx ) );
136  return UserID();
137 }
138 
139 std::vector<UserID> UserIDListModel::userIDs( const QList<QModelIndex> & indexes, bool strict ) const {
140  std::vector<UserID> result;
141  result.reserve( indexes.size() );
142  std::transform( indexes.begin(), indexes.end(),
143  std::back_inserter( result ),
144  boost::bind( &UserIDListModel::userID, this, _1, strict ) );
145  return result;
146 }
147 
148 UserID::Signature UserIDListModel::signature( const QModelIndex & idx ) const {
149  if ( is_signature_level( idx ) )
150  return d->key.userID( extract_uid_number( idx ) ).signature( idx.row() );
151  else
152  return UserID::Signature();
153 }
154 
155 std::vector<UserID::Signature> UserIDListModel::signatures( const QList<QModelIndex> & indexes ) const {
156  std::vector<UserID::Signature> result;
157  result.reserve( indexes.size() );
158  std::transform( indexes.begin(), indexes.end(),
159  std::back_inserter( result ),
160  boost::bind( &UserIDListModel::signature, this, _1 ) );
161  return result;
162 }
163 
164 QModelIndex UserIDListModel::index( const UserID & userID, int col ) const {
165  // O(N), but not sorted, so no better way...
166  for ( unsigned int row = 0, end = d->key.numUserIDs() ; row != end ; ++row )
167  if ( qstricmp( userID.id(), d->key.userID( row ).id() ) == 0 )
168  return createIndex( row, col, -1 );
169  return QModelIndex();
170 }
171 
172 QList<QModelIndex> UserIDListModel::indexes( const std::vector<UserID> & userIDs, int col ) const {
173  // O(N*M), but who cares...?
174  QList<QModelIndex> result;
175  Q_FOREACH( const UserID & uid, userIDs )
176  result.push_back( index( uid, col ) );
177  return result;
178 }
179 
180 QModelIndex UserIDListModel::index( const UserID::Signature & sig, int col ) const {
181  const UserID uid = sig.parent();
182  const QModelIndex pidx = index( uid );
183  if ( !pidx.isValid() )
184  return QModelIndex();
185  const std::vector<UserID::Signature> sigs = uid.signatures();
186  const std::vector<UserID::Signature>::const_iterator it
187  = std::find_if( sigs.begin(), sigs.end(),
188  boost::bind( qstricmp, boost::bind( &UserID::Signature::signerKeyID, _1 ), sig.signerKeyID() ) == 0 );
189  if ( it == sigs.end() )
190  return QModelIndex();
191  return createIndex( std::distance( sigs.begin(), it ), col, pidx.row() );
192 }
193 
194 QList<QModelIndex> UserIDListModel::indexes( const std::vector<UserID::Signature> & signatures, int col ) const {
195  QList<QModelIndex> result;
196  Q_FOREACH( const UserID::Signature & sig, signatures )
197  result.push_back( index( sig, col ) );
198  return result;
199 }
200 
201 void UserIDListModel::clear() {
202  d->key = Key::null;
203  reset();
204 }
205 
206 int UserIDListModel::columnCount( const QModelIndex & ) const {
207  return NumColumns;
208 }
209 
210 int UserIDListModel::rowCount( const QModelIndex & pidx ) const {
211  if ( !pidx.isValid() )
212  return d->key.numUserIDs();
213  if ( is_userid_level( pidx ) )
214  return d->key.userID( pidx.row() ).numSignatures();
215  return 0;
216 }
217 
218 QModelIndex UserIDListModel::index( int row, int col, const QModelIndex & pidx ) const {
219  if ( row < 0 || col < 0 || col >= NumColumns )
220  return QModelIndex();
221 
222  if ( !pidx.isValid() ) {
223  if ( static_cast<unsigned>( row ) < d->key.numUserIDs() )
224  return createIndex( row, col, -1 );
225  else
226  return QModelIndex();
227  }
228 
229  if ( !is_userid_level( pidx ) )
230  return QModelIndex();
231 
232  const int numSigs = userID( pidx ).numSignatures();
233  if ( row < numSigs )
234  return createIndex( row, col, pidx.row() );
235 
236  return QModelIndex();
237 }
238 
239 QModelIndex UserIDListModel::parent( const QModelIndex & idx ) const {
240  if ( is_signature_level( idx ) )
241  return createIndex( idx.internalId(), 0, -1 );
242  else
243  return QModelIndex();
244 }
245 
246 QVariant UserIDListModel::headerData( int section, Qt::Orientation o, int role ) const {
247  if ( o == Qt::Horizontal )
248  if ( role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::ToolTipRole )
249  switch ( section ) {
250  case ID: return i18n( "ID" );
251  case PrettyName: return i18n( "Name" );
252  case PrettyEMail: return i18n( "EMail" );
253  case ValidFrom: return i18n( "Valid From" );
254  case ValidUntil: return i18n( "Valid Until" );
255  case Status: return i18n( "Status" );
256  case NumColumns: ;
257  }
258  return QVariant();
259 }
260 
261 QVariant UserIDListModel::data( const QModelIndex & idx, int role ) const {
262 
263  if ( role != Qt::DisplayRole && role != Qt::EditRole && role != Qt::ToolTipRole )
264  return QVariant();
265 
266  if ( is_userid_level( idx ) ) {
267 
268  const UserID uid = this->userID( idx );
269  if ( uid.isNull() )
270  return QVariant();
271 
272  if ( idx.column() == 0 )
273  // we assume that the top-level items are spanned
274  return Formatting::prettyUserID( uid );
275  else
276  return QVariant();
277 
278  } else if ( is_signature_level( idx ) ) {
279 
280  const UserID::Signature signature = this->signature( idx );
281  if ( signature.isNull() )
282  return QVariant();
283 
284  switch ( idx.column() ) {
285 
286  case ID:
287  return QString::fromLatin1( signature.signerKeyID() );
288  case PrettyName:
289  return Formatting::prettyName( signature );
290  case PrettyEMail:
291  return Formatting::prettyEMail( signature );
292  case ValidFrom:
293  if ( role == Qt::EditRole )
294  return Formatting::creationDate( signature );
295  else
296  return Formatting::creationDateString( signature );
297  case ValidUntil:
298  if ( role == Qt::EditRole )
299  return Formatting::expirationDate( signature );
300  else
301  return Formatting::expirationDateString( signature );
302  case Status:
303  return Formatting::validityShort( signature );
304 #if 0
305  if ( userID.isRevoked() )
306  return i18n("revoked");
307  if ( userID.isExpired() )
308  return i18n("expired");
309  if ( userID.isDisabled() )
310  return i18n("disabled");
311  if ( userID.isInvalid() )
312  return i18n("invalid");
313  return i18n("good");
314 #endif
315  }
316 
317 
318  }
319 
320  return QVariant();
321 }
322 
Kleo::UserIDListModel::setKey
void setKey(const GpgME::Key &key)
Definition: useridlistmodel.cpp:100
QModelIndex
Kleo::UserIDListModel::~UserIDListModel
~UserIDListModel()
Definition: useridlistmodel.cpp:92
Kleo::UserIDListModel::signature
GpgME::UserID::Signature signature(const QModelIndex &idx) const
Definition: useridlistmodel.cpp:148
Kleo::UserIDListModel::columnCount
int columnCount(const QModelIndex &pidx=QModelIndex()) const
Definition: useridlistmodel.cpp:206
QAbstractItemModel::layoutChanged
void layoutChanged()
Kleo::UserIDListModel::ID
Definition: useridlistmodel.h:59
useridlistmodel.h
Kleo::Formatting::creationDateString
QString creationDateString(const GpgME::Key &key)
Kleo::UserIDListModel::rowCount
int rowCount(const QModelIndex &pidx=QModelIndex()) const
Definition: useridlistmodel.cpp:210
formatting.h
Kleo::UserIDListModel::ValidFrom
Definition: useridlistmodel.h:56
QModelIndex::internalId
qint64 internalId() const
Kleo::Formatting::expirationDateString
QString expirationDateString(const GpgME::Key &key)
QList::size
int size() const
Kleo::Formatting::prettyUserID
QString prettyUserID(const GpgME::UserID &uid)
QAbstractItemModel::layoutAboutToBeChanged
void layoutAboutToBeChanged()
QAbstractItemModel::reset
void reset()
QModelIndex::isValid
bool isValid() const
d
#define d
Definition: adduseridcommand.cpp:89
QAbstractItemModel::dataChanged
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
QObject
Kleo::Formatting::validityShort
QString validityShort(const GpgME::Subkey &subkey)
QModelIndex::row
int row() const
Kleo::UserIDListModel::userIDs
std::vector< GpgME::UserID > userIDs(const QList< QModelIndex > &indexes, bool strict=false) const
Definition: useridlistmodel.cpp:139
Kleo::UserIDListModel::clear
void clear()
Definition: useridlistmodel.cpp:201
Kleo::UserIDListModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: useridlistmodel.cpp:261
Kleo::Formatting::expirationDate
QDate expirationDate(const GpgME::Key &key)
QList
Definition: commands/command.h:46
QModelIndex::parent
QModelIndex parent() const
Kleo::UserIDListModel::NumColumns
Definition: useridlistmodel.h:61
Kleo::Formatting::prettyEMail
QString prettyEMail(const char *email, const char *id)
Definition: formatting.cpp:184
QAbstractItemModel::createIndex
QModelIndex createIndex(int row, int column, void *ptr) const
QList::end
iterator end()
Kleo::UserIDListModel::signatures
std::vector< GpgME::UserID::Signature > signatures(const QList< QModelIndex > &indexes) const
Definition: useridlistmodel.cpp:155
Kleo::UserIDListModel::key
GpgME::Key key() const
Definition: useridlistmodel.cpp:95
Kleo::UserIDListModel::index
QModelIndex index(const GpgME::UserID &userID, int col=0) const
Kleo::Formatting::creationDate
QDate creationDate(const GpgME::Key &key)
Kleo::UserIDListModel::ValidUntil
Definition: useridlistmodel.h:57
Kleo::UserIDListModel
Definition: useridlistmodel.h:45
Kleo::Formatting::prettyName
QString prettyName(int proto, const char *id, const char *name, const char *comment)
Definition: formatting.cpp:64
q
#define q
Definition: adduseridcommand.cpp:90
Kleo::UserIDListModel::indexes
QList< QModelIndex > indexes(const std::vector< GpgME::UserID > &userIDs, int col=0) const
QModelIndex::column
int column() const
QAbstractItemModel
Kleo::UserIDListModel::PrettyEMail
Definition: useridlistmodel.h:55
QString::fromLatin1
QString fromLatin1(const char *str, int size)
Kleo::UserIDListModel::Status
Definition: useridlistmodel.h:58
Kleo::UserIDListModel::userID
GpgME::UserID userID(const QModelIndex &idx, bool strict=false) const
Definition: useridlistmodel.cpp:131
Kleo::UserIDListModel::headerData
QVariant headerData(int section, Qt::Orientation o, int role=Qt::DisplayRole) const
Definition: useridlistmodel.cpp:246
Kleo::UserIDListModel::PrettyName
Definition: useridlistmodel.h:54
QObject::parent
QObject * parent() const
QList::begin
iterator begin()
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:11 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

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