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

kleopatra

  • sources
  • kde-4.12
  • 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 #include <boost/bind.hpp>
47 
48 #include <algorithm>
49 #include <iterator>
50 
51 using namespace GpgME;
52 using namespace Kleo;
53 using namespace boost;
54 
55 namespace {
56 
57  static inline bool is_userid_level( const QModelIndex & idx ) {
58  return idx.isValid() && idx.internalId() < 0 ;
59  }
60 
61  static inline int extract_uid_number( const QModelIndex & idx ) {
62  return idx.internalId();
63  }
64 
65  static inline bool is_signature_level( const QModelIndex & idx ) {
66  return idx.isValid() && idx.internalId() >= 0 ;
67  }
68 
69 }
70 
71 class UserIDListModel::Private {
72  friend class ::Kleo::UserIDListModel;
73  UserIDListModel * const q;
74 public:
75  explicit Private( UserIDListModel * qq )
76  : q( qq ), key() {}
77 
78 private:
79  Key key;
80 };
81 
82 
83 
84 UserIDListModel::UserIDListModel( QObject * p )
85  : QAbstractItemModel( p ), d( new Private( this ) )
86 {
87 
88 }
89 
90 UserIDListModel::~UserIDListModel() {}
91 
92 
93 Key UserIDListModel::key() const {
94  return d->key;
95 }
96 
97 // slot
98 void UserIDListModel::setKey( const Key & key ) {
99 
100  const Key oldKey = d->key;
101 
102  d->key = key;
103 
104  if ( qstricmp( key.primaryFingerprint(), oldKey.primaryFingerprint() ) != 0 ) {
105  // different key -> reset
106  reset();
107  return;
108  }
109 
110  // ### diff them, and signal more fine-grained than this:
111 
112  if ( key.numUserIDs() > 0 && oldKey.numUserIDs() == key.numUserIDs() ) {
113  bool identical = true;
114  for ( unsigned int i = 0, end = key.numUserIDs() ; i != end ; ++i ) {
115  if ( key.userID( i ).numSignatures() != oldKey.userID( i ).numSignatures() ) {
116  identical = false;
117  break;
118  }
119  }
120  if ( identical ) {
121  emit dataChanged( index( 0, 0 ), index( key.numUserIDs() - 1, NumColumns - 1 ) );
122  return;
123  }
124  }
125  emit layoutAboutToBeChanged();
126  emit layoutChanged();
127 }
128 
129 UserID UserIDListModel::userID( const QModelIndex & idx, bool strict ) const {
130  if ( is_userid_level( idx ) )
131  return d->key.userID( idx.row() );
132  if ( !strict && is_signature_level( idx ) )
133  return d->key.userID( extract_uid_number( idx ) );
134  return UserID();
135 }
136 
137 std::vector<UserID> UserIDListModel::userIDs( const QList<QModelIndex> & indexes, bool strict ) const {
138  std::vector<UserID> result;
139  result.reserve( indexes.size() );
140  std::transform( indexes.begin(), indexes.end(),
141  std::back_inserter( result ),
142  boost::bind( &UserIDListModel::userID, this, _1, strict ) );
143  return result;
144 }
145 
146 UserID::Signature UserIDListModel::signature( const QModelIndex & idx ) const {
147  if ( is_signature_level( idx ) )
148  return d->key.userID( extract_uid_number( idx ) ).signature( idx.row() );
149  else
150  return UserID::Signature();
151 }
152 
153 std::vector<UserID::Signature> UserIDListModel::signatures( const QList<QModelIndex> & indexes ) const {
154  std::vector<UserID::Signature> result;
155  result.reserve( indexes.size() );
156  std::transform( indexes.begin(), indexes.end(),
157  std::back_inserter( result ),
158  boost::bind( &UserIDListModel::signature, this, _1 ) );
159  return result;
160 }
161 
162 QModelIndex UserIDListModel::index( const UserID & userID, int col ) const {
163  // O(N), but not sorted, so no better way...
164  for ( unsigned int row = 0, end = d->key.numUserIDs() ; row != end ; ++row )
165  if ( qstricmp( userID.id(), d->key.userID( row ).id() ) == 0 )
166  return createIndex( row, col, -1 );
167  return QModelIndex();
168 }
169 
170 QList<QModelIndex> UserIDListModel::indexes( const std::vector<UserID> & userIDs, int col ) const {
171  // O(N*M), but who cares...?
172  QList<QModelIndex> result;
173  Q_FOREACH( const UserID & uid, userIDs )
174  result.push_back( index( uid, col ) );
175  return result;
176 }
177 
178 QModelIndex UserIDListModel::index( const UserID::Signature & sig, int col ) const {
179  const UserID uid = sig.parent();
180  const QModelIndex pidx = index( uid );
181  if ( !pidx.isValid() )
182  return QModelIndex();
183  const std::vector<UserID::Signature> sigs = uid.signatures();
184  const std::vector<UserID::Signature>::const_iterator it
185  = std::find_if( sigs.begin(), sigs.end(),
186  boost::bind( qstricmp, boost::bind( &UserID::Signature::signerKeyID, _1 ), sig.signerKeyID() ) == 0 );
187  if ( it == sigs.end() )
188  return QModelIndex();
189  return createIndex( std::distance( sigs.begin(), it ), col, pidx.row() );
190 }
191 
192 QList<QModelIndex> UserIDListModel::indexes( const std::vector<UserID::Signature> & signatures, int col ) const {
193  QList<QModelIndex> result;
194  Q_FOREACH( const UserID::Signature & sig, signatures )
195  result.push_back( index( sig, col ) );
196  return result;
197 }
198 
199 void UserIDListModel::clear() {
200  d->key = Key::null;
201  reset();
202 }
203 
204 int UserIDListModel::columnCount( const QModelIndex & ) const {
205  return NumColumns;
206 }
207 
208 int UserIDListModel::rowCount( const QModelIndex & pidx ) const {
209  if ( !pidx.isValid() )
210  return d->key.numUserIDs();
211  if ( is_userid_level( pidx ) )
212  return d->key.userID( pidx.row() ).numSignatures();
213  return 0;
214 }
215 
216 QModelIndex UserIDListModel::index( int row, int col, const QModelIndex & pidx ) const {
217  if ( row < 0 || col < 0 || col >= NumColumns )
218  return QModelIndex();
219 
220  if ( !pidx.isValid() ) {
221  if ( static_cast<unsigned>( row ) < d->key.numUserIDs() )
222  return createIndex( row, col, -1 );
223  else
224  return QModelIndex();
225  }
226 
227  if ( !is_userid_level( pidx ) )
228  return QModelIndex();
229 
230  const int numSigs = userID( pidx ).numSignatures();
231  if ( row < numSigs )
232  return createIndex( row, col, pidx.row() );
233 
234  return QModelIndex();
235 }
236 
237 QModelIndex UserIDListModel::parent( const QModelIndex & idx ) const {
238  if ( is_signature_level( idx ) )
239  return createIndex( idx.internalId(), 0, -1 );
240  else
241  return QModelIndex();
242 }
243 
244 QVariant UserIDListModel::headerData( int section, Qt::Orientation o, int role ) const {
245  if ( o == Qt::Horizontal )
246  if ( role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::ToolTipRole )
247  switch ( section ) {
248  case ID: return i18n( "ID" );
249  case PrettyName: return i18n( "Name" );
250  case PrettyEMail: return i18n( "EMail" );
251  case ValidFrom: return i18n( "Valid From" );
252  case ValidUntil: return i18n( "Valid Until" );
253  case Status: return i18n( "Status" );
254  case NumColumns: ;
255  }
256  return QVariant();
257 }
258 
259 QVariant UserIDListModel::data( const QModelIndex & idx, int role ) const {
260 
261  if ( role != Qt::DisplayRole && role != Qt::EditRole && role != Qt::ToolTipRole )
262  return QVariant();
263 
264  if ( is_userid_level( idx ) ) {
265 
266  const UserID uid = this->userID( idx );
267  if ( uid.isNull() )
268  return QVariant();
269 
270  if ( idx.column() == 0 )
271  // we assume that the top-level items are spanned
272  return Formatting::prettyUserID( uid );
273  else
274  return QVariant();
275 
276  } else if ( is_signature_level( idx ) ) {
277 
278  const UserID::Signature signature = this->signature( idx );
279  if ( signature.isNull() )
280  return QVariant();
281 
282  switch ( idx.column() ) {
283 
284  case ID:
285  return QString::fromLatin1( signature.signerKeyID() );
286  case PrettyName:
287  return Formatting::prettyName( signature );
288  case PrettyEMail:
289  return Formatting::prettyEMail( signature );
290  case ValidFrom:
291  if ( role == Qt::EditRole )
292  return Formatting::creationDate( signature );
293  else
294  return Formatting::creationDateString( signature );
295  case ValidUntil:
296  if ( role == Qt::EditRole )
297  return Formatting::expirationDate( signature );
298  else
299  return Formatting::expirationDateString( signature );
300  case Status:
301  return Formatting::validityShort( signature );
302 #if 0
303  if ( userID.isRevoked() )
304  return i18n("revoked");
305  if ( userID.isExpired() )
306  return i18n("expired");
307  if ( userID.isDisabled() )
308  return i18n("disabled");
309  if ( userID.isInvalid() )
310  return i18n("invalid");
311  return i18n("good");
312 #endif
313  }
314 
315 
316  }
317 
318  return QVariant();
319 }
320 
321 #include "moc_useridlistmodel.cpp"
Kleo::UserIDListModel::setKey
void setKey(const GpgME::Key &key)
Definition: useridlistmodel.cpp:98
Kleo::UserIDListModel::~UserIDListModel
~UserIDListModel()
Definition: useridlistmodel.cpp:90
Kleo::UserIDListModel::signature
GpgME::UserID::Signature signature(const QModelIndex &idx) const
Definition: useridlistmodel.cpp:146
Kleo::UserIDListModel::columnCount
int columnCount(const QModelIndex &pidx=QModelIndex()) const
Definition: useridlistmodel.cpp:204
Kleo::UserIDListModel::parent
QModelIndex parent(const QModelIndex &index) const
Definition: useridlistmodel.cpp:237
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:208
formatting.h
Kleo::UserIDListModel::ValidFrom
Definition: useridlistmodel.h:56
Kleo::Formatting::expirationDateString
QString expirationDateString(const GpgME::Key &key)
Kleo::Formatting::prettyUserID
QString prettyUserID(const GpgME::UserID &uid)
d
#define d
Definition: adduseridcommand.cpp:90
Kleo::Formatting::validityShort
QString validityShort(const GpgME::Subkey &subkey)
Kleo::UserIDListModel::userIDs
std::vector< GpgME::UserID > userIDs(const QList< QModelIndex > &indexes, bool strict=false) const
Definition: useridlistmodel.cpp:137
Kleo::UserIDListModel::clear
void clear()
Definition: useridlistmodel.cpp:199
Kleo::UserIDListModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: useridlistmodel.cpp:259
Kleo::Formatting::expirationDate
QDate expirationDate(const GpgME::Key &key)
Kleo::UserIDListModel::NumColumns
Definition: useridlistmodel.h:61
Kleo::Formatting::prettyEMail
QString prettyEMail(const char *email, const char *id)
Definition: formatting.cpp:184
QAbstractItemModel
Kleo::UserIDListModel::signatures
std::vector< GpgME::UserID::Signature > signatures(const QList< QModelIndex > &indexes) const
Definition: useridlistmodel.cpp:153
Kleo::UserIDListModel::key
GpgME::Key key() const
Definition: useridlistmodel.cpp:93
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:91
Kleo::UserIDListModel::indexes
QList< QModelIndex > indexes(const std::vector< GpgME::UserID > &userIDs, int col=0) const
Kleo::UserIDListModel::PrettyEMail
Definition: useridlistmodel.h:55
Kleo::UserIDListModel::Status
Definition: useridlistmodel.h:58
Kleo::UserIDListModel::userID
GpgME::UserID userID(const QModelIndex &idx, bool strict=false) const
Definition: useridlistmodel.cpp:129
Kleo::UserIDListModel::headerData
QVariant headerData(int section, Qt::Orientation o, int role=Qt::DisplayRole) const
Definition: useridlistmodel.cpp:244
Kleo::UserIDListModel::PrettyName
Definition: useridlistmodel.h:54
QList
Definition: commands/command.h:46
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:42 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

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