• 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
  • crypto
  • gui
signingcertificateselectionwidget.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  crypto/gui/signingcertificateselectionwidget.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2007, 2009 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 "signingcertificateselectionwidget.h"
36 
37 #include "ui_signingcertificateselectionwidget.h"
38 
39 #include <models/keycache.h>
40 
41 #include <utils/formatting.h>
42 
43 #include <kleo/stl_util.h>
44 
45 #include <QByteArray>
46 #include <QMap>
47 
48 #ifndef Q_MOC_RUN
49 #include <boost/bind.hpp>
50 #endif
51 
52 #include <cassert>
53 
54 using namespace Kleo;
55 using namespace Kleo::Crypto::Gui;
56 
57 class SigningCertificateSelectionWidget::Private {
58  friend class ::SigningCertificateSelectionWidget;
59  SigningCertificateSelectionWidget * const q;
60 public:
61  explicit Private( SigningCertificateSelectionWidget * qq );
62  ~Private();
63  static std::vector<GpgME::Key> candidates( GpgME::Protocol prot );
64  static void addCandidates( GpgME::Protocol prot, QComboBox* combo );
65 
66 private:
67  Ui::SigningCertificateSelectionWidget ui;
68 };
69 
70 
71 static GpgME::Key current_cert( const QComboBox & cb ) {
72  const QByteArray fpr = cb.itemData( cb.currentIndex() ).toByteArray();
73  return KeyCache::instance()->findByFingerprint( fpr.constData() );
74 }
75 
76 static void select_cert( QComboBox & cb, const GpgME::Key & key ) {
77  const QByteArray fpr = key.primaryFingerprint();
78  if ( !fpr.isEmpty() )
79  cb.setCurrentIndex( cb.findData( fpr ) );
80 }
81 
82 static void add_cert( QComboBox & cb, const GpgME::Key & key ) {
83  cb.addItem( Formatting::formatForComboBox( key ),
84  QVariant( QByteArray( key.primaryFingerprint() ) ) );
85 
86 }
87 
88 SigningCertificateSelectionWidget::Private::Private( SigningCertificateSelectionWidget * qq )
89  : q( qq ), ui()
90 {
91  ui.setupUi( q );
92  addCandidates( GpgME::CMS, ui.cmsCombo );
93  addCandidates( GpgME::OpenPGP, ui.pgpCombo );
94  ui.rememberCO->setChecked( true );
95 }
96 
97 SigningCertificateSelectionWidget::Private::~Private() {}
98 
99 
100 
101 SigningCertificateSelectionWidget::SigningCertificateSelectionWidget( QWidget * parent, Qt::WindowFlags f )
102  : QWidget( parent, f ), d( new Private( this ) )
103 {
104 
105 }
106 
107 SigningCertificateSelectionWidget::~SigningCertificateSelectionWidget() {}
108 
109 
110 void SigningCertificateSelectionWidget::setSelectedCertificates( const QMap<GpgME::Protocol, GpgME::Key>& certificates )
111 {
112  setSelectedCertificates( certificates[GpgME::OpenPGP], certificates[GpgME::CMS] );
113 }
114 
115 void SigningCertificateSelectionWidget::setSelectedCertificates( const GpgME::Key & pgp, const GpgME::Key & cms ) {
116  select_cert( *d->ui.pgpCombo, pgp );
117  select_cert( *d->ui.cmsCombo, cms );
118 }
119 
120 std::vector<GpgME::Key> SigningCertificateSelectionWidget::Private::candidates( GpgME::Protocol prot )
121 {
122  assert( prot != GpgME::UnknownProtocol );
123  std::vector<GpgME::Key> keys = KeyCache::instance()->keys();
124  std::vector<GpgME::Key>::iterator end = keys.end();
125 
126  end = std::remove_if( keys.begin(), end, boost::bind( &GpgME::Key::protocol, _1 ) != prot );
127  end = std::remove_if( keys.begin(), end, !boost::bind( &GpgME::Key::hasSecret, _1 ) );
128  assert( kdtools::all( keys.begin(), end, boost::bind( &GpgME::Key::hasSecret, _1 ) ) );
129  end = std::remove_if( keys.begin(), end, !boost::bind( &GpgME::Key::canReallySign, _1 ) );
130  end = std::remove_if( keys.begin(), end, boost::bind( &GpgME::Key::isExpired, _1 ) );
131  end = std::remove_if( keys.begin(), end, boost::bind( &GpgME::Key::isRevoked, _1 ) );
132  keys.erase( end, keys.end() );
133  return keys;
134 }
135 
136 void SigningCertificateSelectionWidget::Private::addCandidates( GpgME::Protocol prot, QComboBox* combo )
137 {
138  const std::vector<GpgME::Key> keys = candidates( prot );
139  Q_FOREACH( const GpgME::Key& i, keys )
140  add_cert( *combo, i );
141 }
142 
143 
144 QMap<GpgME::Protocol, GpgME::Key> SigningCertificateSelectionWidget::selectedCertificates() const
145 {
146  QMap<GpgME::Protocol, GpgME::Key> res;
147 
148  res.insert( GpgME::OpenPGP, current_cert( *d->ui.pgpCombo ) );
149  res.insert( GpgME::CMS, current_cert( *d->ui.cmsCombo ) );
150 
151  return res;
152 }
153 
154 bool SigningCertificateSelectionWidget::rememberAsDefault() const
155 {
156  return d->ui.rememberCO->isChecked();
157 }
158 
159 void SigningCertificateSelectionWidget::setAllowedProtocols( const QVector<GpgME::Protocol>& allowedProtocols )
160 {
161  setAllowedProtocols( allowedProtocols.contains( GpgME::OpenPGP ),
162  allowedProtocols.contains( GpgME::CMS ) );
163 }
164 
165 void SigningCertificateSelectionWidget::setAllowedProtocols( bool pgp, bool cms )
166 {
167  d->ui.pgpLabel->setVisible( pgp );
168  d->ui.pgpCombo->setVisible( pgp );
169 
170  d->ui.cmsLabel->setVisible( cms );
171  d->ui.cmsCombo->setVisible( cms );
172 }
173 
QWidget
QByteArray
select_cert
static void select_cert(QComboBox &cb, const GpgME::Key &key)
Definition: signingcertificateselectionwidget.cpp:76
QMap< GpgME::Protocol, GpgME::Key >
QByteArray::isEmpty
bool isEmpty() const
formatting.h
Kleo::Crypto::Gui::SigningCertificateSelectionWidget::setSelectedCertificates
void setSelectedCertificates(const QMap< GpgME::Protocol, GpgME::Key > &certificates)
Definition: signingcertificateselectionwidget.cpp:110
QComboBox::addItem
void addItem(const QString &text, const QVariant &userData)
Kleo::Formatting::formatForComboBox
QString formatForComboBox(const GpgME::Key &key)
Definition: formatting.cpp:497
d
#define d
Definition: adduseridcommand.cpp:89
QVector::contains
bool contains(const T &value) const
Kleo::Crypto::Gui::SigningCertificateSelectionWidget::rememberAsDefault
bool rememberAsDefault() const
Definition: signingcertificateselectionwidget.cpp:154
Kleo::Class::OpenPGP
Definition: classify.h:49
QByteArray::constData
const char * constData() const
Kleo::Class::CMS
Definition: classify.h:48
QComboBox::itemData
QVariant itemData(int index, int role) const
Kleo::Crypto::Gui::SigningCertificateSelectionWidget
Definition: signingcertificateselectionwidget.h:51
QComboBox::findData
int findData(const QVariant &data, int role, QFlags< Qt::MatchFlag > flags) const
QVector
q
#define q
Definition: adduseridcommand.cpp:90
QComboBox::currentIndex
currentIndex
Kleo::KeyCache::instance
static boost::shared_ptr< const KeyCache > instance()
Definition: keycache.cpp:190
signingcertificateselectionwidget.h
Qt::WindowFlags
typedef WindowFlags
Kleo::Crypto::Gui::SigningCertificateSelectionWidget::setAllowedProtocols
void setAllowedProtocols(const QVector< GpgME::Protocol > &allowedProtocols)
Definition: signingcertificateselectionwidget.cpp:159
QMap::insert
iterator insert(const Key &key, const T &value)
add_cert
static void add_cert(QComboBox &cb, const GpgME::Key &key)
Definition: signingcertificateselectionwidget.cpp:82
keycache.h
Kleo::Crypto::Gui::SigningCertificateSelectionWidget::~SigningCertificateSelectionWidget
~SigningCertificateSelectionWidget()
Definition: signingcertificateselectionwidget.cpp:107
current_cert
static GpgME::Key current_cert(const QComboBox &cb)
Definition: signingcertificateselectionwidget.cpp:71
QVariant
QComboBox
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