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