• 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
  • dialogs
certificateselectiondialog.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  dialogs/certificateselectiondialog.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2008 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 "certificateselectiondialog.h"
36 
37 #include <view/searchbar.h>
38 #include <view/tabwidget.h>
39 
40 #include <models/keylistmodel.h>
41 #include <models/keycache.h>
42 
43 #include <commands/reloadkeyscommand.h>
44 #include <commands/lookupcertificatescommand.h>
45 #include <commands/newcertificatecommand.h>
46 
47 #include <gpgme++/key.h>
48 
49 #include <KLocalizedString>
50 #include <KConfigGroup>
51 #include <KSharedConfig>
52 #include <KDebug>
53 
54 #include <QLabel>
55 #include <QPushButton>
56 #include <QDialogButtonBox>
57 #include <QItemSelectionModel>
58 #include <QAbstractItemView>
59 #include <QPointer>
60 #include <QVBoxLayout>
61 
62 #ifndef Q_MOC_RUN
63 #include <boost/bind.hpp>
64 #endif
65 
66 #include <algorithm>
67 
68 using namespace Kleo;
69 using namespace Kleo::Dialogs;
70 using namespace Kleo::Commands;
71 using namespace boost;
72 using namespace GpgME;
73 
74 class CertificateSelectionDialog::Private {
75  friend class ::Kleo::Dialogs::CertificateSelectionDialog;
76  CertificateSelectionDialog * const q;
77 public:
78  explicit Private( CertificateSelectionDialog * qq );
79 
80 
81 private:
82  void reload() {
83  Command * const cmd = new ReloadKeysCommand( 0 );
84  cmd->setParentWidget( q );
85  cmd->start();
86  }
87  void create() {
88  NewCertificateCommand * cmd = new NewCertificateCommand( 0 );
89  cmd->setParentWidget( q );
90  if ( ( options & AnyFormat ) != AnyFormat )
91  cmd->setProtocol( (options & OpenPGPFormat) ? OpenPGP : CMS );
92  cmd->start();
93  }
94  void lookup() {
95  Command * const cmd = new LookupCertificatesCommand( 0 );
96  cmd->setParentWidget( q );
97  cmd->start();
98  }
99  void slotKeysMayHaveChanged();
100  void slotCurrentViewChanged( QAbstractItemView * newView );
101  void slotSelectionChanged();
102  void slotDoubleClicked( const QModelIndex & idx );
103 
104 private:
105  bool acceptable( const std::vector<Key> & keys ) {
106  return !keys.empty();
107  }
108  void filterAllowedKeys( std::vector<Key> & keys );
109  void updateLabelText() {
110  ui.label.setText( !customLabelText.isEmpty() ? customLabelText :
111  (options & MultiSelection)
112  ? i18n( "Please select one or more of the following certificates:" )
113  : i18n( "Please select one of the following certificates:" ) );
114  }
115 
116 private:
117  QPointer<QAbstractItemView> lastView;
118  QString customLabelText;
119  Options options;
120 
121  struct UI {
122  QLabel label;
123  SearchBar searchBar;
124  TabWidget tabWidget;
125  QDialogButtonBox buttonBox;
126  QVBoxLayout vlay;
127 
128  explicit UI( CertificateSelectionDialog * q )
129  : label( q ),
130  searchBar( q ),
131  tabWidget( q ),
132  buttonBox( q ),
133  vlay( q )
134  {
135  KDAB_SET_OBJECT_NAME( label );
136  KDAB_SET_OBJECT_NAME( searchBar );
137  KDAB_SET_OBJECT_NAME( tabWidget );
138  KDAB_SET_OBJECT_NAME( buttonBox );
139  KDAB_SET_OBJECT_NAME( vlay );
140 
141  vlay.addWidget( &label );
142  vlay.addWidget( &searchBar );
143  vlay.addWidget( &tabWidget, 1 );
144  vlay.addWidget( &buttonBox );
145 
146  QPushButton * const ok = buttonBox.addButton( QDialogButtonBox::Ok );
147  ok->setEnabled( false );
148  QPushButton * const cancel = buttonBox.addButton( QDialogButtonBox::Close );
149  Q_UNUSED( cancel );
150  QPushButton * const reload = buttonBox.addButton( i18n("Reload"), QDialogButtonBox::ActionRole );
151  QPushButton * const lookup = buttonBox.addButton( i18n("Lookup..."), QDialogButtonBox::ActionRole );
152  QPushButton * const create = buttonBox.addButton( i18n("New..."), QDialogButtonBox::ActionRole );
153 
154  lookup->setToolTip( i18nc("@info:tooltip","Lookup certificates on server") );
155  reload->setToolTip( i18nc("@info:tooltip","Refresh certificate list") );
156  create->setToolTip( i18nc("@info:tooltip","Create a new certificate") );
157 
158  connect( &buttonBox, SIGNAL(accepted()), q, SLOT(accept()) );
159  connect( &buttonBox, SIGNAL(rejected()), q, SLOT(reject()) );
160  connect( reload, SIGNAL(clicked()), q, SLOT(reload()) );
161  connect( lookup, SIGNAL(clicked()), q, SLOT(lookup()) );
162  connect( create, SIGNAL(clicked()), q, SLOT(create()) );
163  connect( KeyCache::instance().get(), SIGNAL(keysMayHaveChanged()),
164  q, SLOT(slotKeysMayHaveChanged()) );
165  }
166  } ui;
167 };
168 
169 
170 CertificateSelectionDialog::Private::Private( CertificateSelectionDialog * qq )
171  : q( qq ),
172  ui( q )
173 {
174  ui.tabWidget.setFlatModel( AbstractKeyListModel::createFlatKeyListModel() );
175  ui.tabWidget.setHierarchicalModel( AbstractKeyListModel::createHierarchicalKeyListModel() );
176  ui.tabWidget.connectSearchBar( &ui.searchBar );
177 
178  connect( &ui.tabWidget, SIGNAL(currentViewChanged(QAbstractItemView*)),
179  q, SLOT(slotCurrentViewChanged(QAbstractItemView*)) );
180 
181  updateLabelText();
182  q->setWindowTitle( i18n( "Certificate Selection" ) );
183 }
184 
185 CertificateSelectionDialog::CertificateSelectionDialog( QWidget * parent, Qt::WindowFlags f )
186  : QDialog( parent, f ), d( new Private( this ) )
187 {
188  const KSharedConfig::Ptr config = KSharedConfig::openConfig( QLatin1String("kleopatracertificateselectiondialogrc") );
189  d->ui.tabWidget.loadViews( config.data() );
190  const KConfigGroup geometry( config, "Geometry" );
191  resize( geometry.readEntry( "size", size() ) );
192  d->slotKeysMayHaveChanged();
193 }
194 
195 CertificateSelectionDialog::~CertificateSelectionDialog() {}
196 
197 void CertificateSelectionDialog::setCustomLabelText( const QString & txt ) {
198  if ( txt == d->customLabelText )
199  return;
200  d->customLabelText = txt;
201  d->updateLabelText();
202 }
203 
204 QString CertificateSelectionDialog::customLabelText() const {
205  return d->customLabelText;
206 }
207 
208 void CertificateSelectionDialog::setOptions( Options options ) {
209  if ( d->options == options )
210  return;
211  d->options = options;
212 
213  d->ui.tabWidget.setMultiSelection( options & MultiSelection );
214 
215  d->slotKeysMayHaveChanged();
216 }
217 
218 CertificateSelectionDialog::Options CertificateSelectionDialog::options() const {
219  return d->options;
220 }
221 
222 void CertificateSelectionDialog::setStringFilter( const QString & filter ) {
223  d->ui.tabWidget.setStringFilter( filter );
224 }
225 
226 void CertificateSelectionDialog::setKeyFilter( const shared_ptr<KeyFilter> & filter ) {
227  d->ui.tabWidget.setKeyFilter( filter );
228 }
229 
230 void CertificateSelectionDialog::selectCertificates( const std::vector<Key> & keys ) {
231  const QAbstractItemView * const view = d->ui.tabWidget.currentView();
232  if ( !view )
233  return;
234  const KeyListModelInterface * const model = dynamic_cast<KeyListModelInterface*>( view->model() );
235  assert( model );
236  QItemSelectionModel * const sm = view->selectionModel();
237  assert( sm );
238 
239  Q_FOREACH( const QModelIndex & idx, model->indexes( keys ) )
240  if ( idx.isValid() )
241  sm->select( idx, QItemSelectionModel::Select | QItemSelectionModel::Rows );
242 }
243 
244 void CertificateSelectionDialog::selectCertificate( const Key & key ) {
245  selectCertificates( std::vector<Key>( 1, key ) );
246 }
247 
248 std::vector<Key> CertificateSelectionDialog::selectedCertificates() const {
249  const QAbstractItemView * const view = d->ui.tabWidget.currentView();
250  if ( !view )
251  return std::vector<Key>();
252  const KeyListModelInterface * const model = dynamic_cast<KeyListModelInterface*>( view->model() );
253  assert( model );
254  const QItemSelectionModel * const sm = view->selectionModel();
255  assert( sm );
256  return model->keys( sm->selectedRows() );
257 }
258 
259 Key CertificateSelectionDialog::selectedCertificate() const {
260  const std::vector<Key> keys = selectedCertificates();
261  return keys.empty() ? Key() : keys.front() ;
262 }
263 
264 void CertificateSelectionDialog::hideEvent( QHideEvent * e ) {
265  KSharedConfig::Ptr config = KSharedConfig::openConfig( QLatin1String("kleopatracertificateselectiondialogrc") );
266  d->ui.tabWidget.saveViews( config.data() );
267  KConfigGroup geometry( config, "Geometry" );
268  geometry.writeEntry( "size", size() );
269  QDialog::hideEvent( e );
270 }
271 
272 void CertificateSelectionDialog::Private::slotKeysMayHaveChanged() {
273  q->setEnabled( true );
274  std::vector<Key> keys = (options & SecretKeys) ? KeyCache::instance()->secretKeys() : KeyCache::instance()->keys() ;
275  filterAllowedKeys( keys );
276  const std::vector<Key> selected = q->selectedCertificates();
277  if ( AbstractKeyListModel * const model = ui.tabWidget.flatModel() )
278  model->setKeys( keys );
279  if ( AbstractKeyListModel * const model = ui.tabWidget.hierarchicalModel() )
280  model->setKeys( keys );
281  q->selectCertificates( selected );
282 }
283 
284 void CertificateSelectionDialog::Private::filterAllowedKeys( std::vector<Key> & keys ) {
285  std::vector<Key>::iterator end = keys.end();
286 
287  switch ( options & AnyFormat ) {
288  case OpenPGPFormat:
289  end = std::remove_if( keys.begin(), end, boost::bind( &Key::protocol, _1 ) != GpgME::OpenPGP );
290  break;
291  case CMSFormat:
292  end = std::remove_if( keys.begin(), end, boost::bind( &Key::protocol, _1 ) != GpgME::CMS );
293  break;
294  default:
295  case AnyFormat:
296  ;
297  }
298 
299  switch ( options & AnyCertificate ) {
300  case SignOnly:
301  end = std::remove_if( keys.begin(), end, !boost::bind( &Key::canReallySign, _1 ) );
302  break;
303  case EncryptOnly:
304  end = std::remove_if( keys.begin(), end, !boost::bind( &Key::canEncrypt, _1 ) );
305  break;
306  default:
307  case AnyCertificate:
308  ;
309  }
310 
311  if ( options & SecretKeys )
312  end = std::remove_if( keys.begin(), end, !boost::bind( &Key::hasSecret, _1 ) );
313 
314  keys.erase( end, keys.end() );
315 }
316 
317 void CertificateSelectionDialog::Private::slotCurrentViewChanged( QAbstractItemView * newView ) {
318  if ( lastView ) {
319  disconnect( lastView, SIGNAL(doubleClicked(QModelIndex)),
320  q, SLOT(slotDoubleClicked(QModelIndex)) );
321  assert( lastView->selectionModel() );
322  disconnect( lastView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
323  q, SLOT(slotSelectionChanged()) );
324  }
325  lastView = newView;
326  if ( newView ) {
327  connect( newView, SIGNAL(doubleClicked(QModelIndex)),
328  q, SLOT(slotDoubleClicked(QModelIndex)) );
329  assert( newView->selectionModel() );
330  connect( newView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
331  q, SLOT(slotSelectionChanged()) );
332  }
333  slotSelectionChanged();
334 }
335 
336 void CertificateSelectionDialog::Private::slotSelectionChanged() {
337  if ( QPushButton * const pb = ui.buttonBox.button( QDialogButtonBox::Ok ) )
338  pb->setEnabled( acceptable( q->selectedCertificates() ) );
339 }
340 
341 void CertificateSelectionDialog::Private::slotDoubleClicked( const QModelIndex & idx ) {
342  QAbstractItemView * const view = ui.tabWidget.currentView();
343  assert( view );
344  const KeyListModelInterface * const model = dynamic_cast<KeyListModelInterface*>( view->model() );
345  assert( model );
346  Q_UNUSED( model );
347  QItemSelectionModel * const sm = view->selectionModel();
348  assert( sm );
349  sm->select( idx, QItemSelectionModel::ClearAndSelect|QItemSelectionModel::Rows );
350  QMetaObject::invokeMethod( q, "accept", Qt::QueuedConnection );
351 }
352 
353 void CertificateSelectionDialog::accept() {
354  if ( d->acceptable( selectedCertificates() ) )
355  QDialog::accept();
356 }
357 
358 #include "moc_certificateselectiondialog.cpp"
Kleo::Dialogs::CertificateSelectionDialog::setOptions
void setOptions(Options options)
Definition: certificateselectiondialog.cpp:208
QHideEvent
Kleo::Dialogs::CertificateSelectionDialog::selectCertificates
void selectCertificates(const std::vector< GpgME::Key > &certs)
Definition: certificateselectiondialog.cpp:230
QModelIndex
Kleo::Commands::NewCertificateCommand
Definition: newcertificatecommand.h:43
QWidget
newcertificatecommand.h
QAbstractItemView
Kleo::Dialogs::CertificateSelectionDialog::setCustomLabelText
void setCustomLabelText(const QString &text)
Definition: certificateselectiondialog.cpp:197
Kleo::TabWidget
Definition: tabwidget.h:58
QAbstractItemView::selectionModel
QItemSelectionModel * selectionModel() const
QPointer< QAbstractItemView >
Kleo::SearchBar
Definition: searchbar.h:50
Kleo::Command::start
void start()
Definition: commands/command.cpp:182
Kleo::Commands::LookupCertificatesCommand
Definition: lookupcertificatescommand.h:41
QWidget::geometry
const QRect & geometry() const
Kleo::ReloadKeysCommand
Definition: reloadkeyscommand.h:41
Kleo::KeyListModelInterface::indexes
virtual QList< QModelIndex > indexes(const std::vector< GpgME::Key > &keys) const =0
Kleo::Dialogs::CertificateSelectionDialog::~CertificateSelectionDialog
~CertificateSelectionDialog()
Definition: certificateselectiondialog.cpp:195
Kleo::AbstractKeyListModel::createHierarchicalKeyListModel
static AbstractKeyListModel * createHierarchicalKeyListModel(QObject *parent=0)
Definition: keylistmodel.cpp:880
Kleo::Dialogs::CertificateSelectionDialog::options
Options options() const
Definition: certificateselectiondialog.cpp:218
QWidget::resize
void resize(int w, int h)
lookupcertificatescommand.h
QModelIndex::isValid
bool isValid() const
boost::shared_ptr< KeyFilter >
Kleo::Dialogs::CertificateSelectionDialog::MultiSelection
Definition: certificateselectiondialog.h:62
QWidget::setEnabled
void setEnabled(bool)
d
#define d
Definition: adduseridcommand.cpp:89
tabwidget.h
Kleo::Dialogs::CertificateSelectionDialog
Definition: certificateselectiondialog.h:56
certificateselectiondialog.h
Kleo::Dialogs::CertificateSelectionDialog::setKeyFilter
void setKeyFilter(const boost::shared_ptr< Kleo::KeyFilter > &filter)
Definition: certificateselectiondialog.cpp:226
Kleo::Class::OpenPGP
Definition: classify.h:49
Kleo::Dialogs::CertificateSelectionDialog::accept
void accept()
Definition: certificateselectiondialog.cpp:353
QWidget::hideEvent
virtual void hideEvent(QHideEvent *event)
QItemSelectionModel::select
virtual void select(const QModelIndex &index, QFlags< QItemSelectionModel::SelectionFlag > command)
Kleo::Dialogs::CertificateSelectionDialog::customLabelText
QString customLabelText() const
Definition: certificateselectiondialog.cpp:204
QItemSelectionModel::selectedRows
QModelIndexList selectedRows(int column) const
Kleo::Dialogs::CertificateSelectionDialog::selectedCertificate
GpgME::Key selectedCertificate() const
Definition: certificateselectiondialog.cpp:259
Kleo::Dialogs::CertificateSelectionDialog::selectedCertificates
std::vector< GpgME::Key > selectedCertificates() const
Definition: certificateselectiondialog.cpp:248
KDAB_SET_OBJECT_NAME
#define KDAB_SET_OBJECT_NAME(x)
Definition: kdtoolsglobal.h:66
QVBoxLayout
Kleo::Class::CMS
Definition: classify.h:48
reloadkeyscommand.h
QString
QDialog::accept
virtual void accept()
Kleo::AbstractKeyListModel
Definition: keylistmodel.h:49
Kleo::KeyListModelInterface
Definition: keylistmodelinterface.h:47
QMetaObject::invokeMethod
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
keylistmodel.h
QItemSelection
Kleo::Dialogs::CertificateSelectionDialog::selectCertificate
void selectCertificate(const GpgME::Key &key)
Definition: certificateselectiondialog.cpp:244
QLatin1String
Ok
Definition: setinitialpindialog.cpp:61
searchbar.h
QDialogButtonBox
QWidget::setWindowTitle
void setWindowTitle(const QString &)
Kleo::AbstractKeyListModel::createFlatKeyListModel
static AbstractKeyListModel * createFlatKeyListModel(QObject *parent=0)
Definition: keylistmodel.cpp:871
q
#define q
Definition: adduseridcommand.cpp:90
Kleo::KeyCache::instance
static boost::shared_ptr< const KeyCache > instance()
Definition: keycache.cpp:190
QDialog
QPushButton
Qt::WindowFlags
typedef WindowFlags
QWidget::setToolTip
void setToolTip(const QString &)
QAbstractItemView::model
QAbstractItemModel * model() const
keycache.h
Kleo::Commands::NewCertificateCommand::setProtocol
void setProtocol(GpgME::Protocol proto)
Definition: newcertificatecommand.cpp:100
QItemSelectionModel
QLabel
Kleo::Command::setParentWidget
void setParentWidget(QWidget *widget)
Definition: commands/command.cpp:136
Kleo::Dialogs::CertificateSelectionDialog::setStringFilter
void setStringFilter(const QString &text)
Definition: certificateselectiondialog.cpp:222
Kleo::Command
Definition: commands/command.h:58
Kleo::Dialogs::CertificateSelectionDialog::hideEvent
void hideEvent(QHideEvent *)
Definition: certificateselectiondialog.cpp:264
Kleo::Class::AnyFormat
Definition: classify.h:58
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:10 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