Better wording
[kdepim.git] / kleopatra / uiserver / selectcertificatecommand.cpp
blobc842dac9fc2e6c1a3785ee4d8ccb9003725bd87e
1 /* -*- mode: c++; c-basic-offset:4 -*-
2 uiserver/selectcertificatecommand.cpp
4 This file is part of Kleopatra, the KDE keymanager
5 Copyright (c) 2007 Klarälvdalens Datakonsult AB
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.
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.
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
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.
33 #include <config-kleopatra.h>
35 #include "selectcertificatecommand.h"
37 #include <dialogs/certificateselectiondialog.h>
39 #include <models/keycache.h>
41 #include <boost/mem_fn.hpp>
43 #include <kleo/stl_util.h>
44 #include <kleo/exception.h>
46 #include <gpgme++/key.h>
48 #include <gpg-error.h>
50 #include <KDebug>
51 #include <KLocale>
53 #include <QVariant>
54 #include <QByteArray>
55 #include <QIODevice>
56 #include <QList>
57 #include <QPointer>
59 #include <string>
60 #include <algorithm>
62 using namespace Kleo;
63 using namespace Kleo::Dialogs;
64 using namespace GpgME;
65 using namespace boost;
67 static const char option_prefix[] = "prefix";
69 class SelectCertificateCommand::Private {
70 friend class ::Kleo::SelectCertificateCommand;
71 SelectCertificateCommand * const q;
72 public:
73 Private( SelectCertificateCommand * qq ) :
74 q( qq ),
75 dialog()
80 private:
81 void slotDialogAccepted();
82 void slotDialogRejected();
83 void slotSelectedCertificates( int, const QByteArray & );
85 private:
86 void ensureDialogCreated() {
87 if ( dialog )
88 return;
89 dialog = new CertificateSelectionDialog;
90 q->applyWindowID( dialog );
91 dialog->setAttribute( Qt::WA_DeleteOnClose );
92 //dialog->setWindowTitle( i18nc( "@title", "Certificate Selection" ) );
93 connect( dialog, SIGNAL(accepted()), q, SLOT(slotDialogAccepted()) );
94 connect( dialog, SIGNAL(rejected()), q, SLOT(slotDialogRejected()) );
96 void ensureDialogShown() {
97 ensureDialogCreated();
98 if ( dialog->isVisible() )
99 dialog->raise();
100 else
101 dialog->show();
104 private:
105 QPointer<CertificateSelectionDialog> dialog;
108 SelectCertificateCommand::SelectCertificateCommand()
109 : QObject(), AssuanCommandMixin<SelectCertificateCommand>(), d( new Private( this ) ) {}
111 SelectCertificateCommand::~SelectCertificateCommand() {}
113 static const struct {
114 const char * name;
115 CertificateSelectionDialog::Option option;
116 } option_table[] = {
117 { "multi", CertificateSelectionDialog::MultiSelection },
118 { "sign-only", CertificateSelectionDialog::SignOnly },
119 { "encrypt-only", CertificateSelectionDialog::EncryptOnly },
120 { "openpgp-only", CertificateSelectionDialog::OpenPGPFormat },
121 { "x509-only", CertificateSelectionDialog::CMSFormat },
122 { "secret-only", CertificateSelectionDialog::SecretKeys },
125 int SelectCertificateCommand::doStart() {
127 d->ensureDialogCreated();
129 CertificateSelectionDialog::Options opts;
130 for ( unsigned int i = 0 ; i < sizeof option_table / sizeof *option_table ; ++i )
131 if ( hasOption( option_table[i].name ) )
132 opts |= option_table[i].option;
134 d->dialog->setOptions( opts );
136 if ( const int err = inquire( "SELECTED_CERTIFICATES",
137 this, SLOT(slotSelectedCertificates(int,QByteArray)) ) )
138 return err;
140 d->ensureDialogShown();
142 return 0;
145 void SelectCertificateCommand::Private::slotSelectedCertificates( int err, const QByteArray & data ) {
146 kDebug() << err << ", " << data.constData();
147 if ( err )
148 return;
149 const std::vector<std::string> fprs = kdtools::transform< std::vector<std::string> >( data.split( '\n' ), mem_fn( &QByteArray::constData ) );
150 const std::vector<Key> keys = KeyCache::instance()->findByKeyIDOrFingerprint( fprs );
151 Q_FOREACH( const Key & key, keys )
152 kDebug() << "found key " << key.userID(0).id();
153 if ( dialog )
154 dialog->selectCertificates( keys );
155 else
156 kWarning() << "dialog == NULL in slotSelectedCertificates";
159 void SelectCertificateCommand::doCanceled() {
160 if ( d->dialog )
161 d->dialog->close();
164 void SelectCertificateCommand::Private::slotDialogAccepted() {
165 try {
166 QByteArray data;
167 Q_FOREACH( const Key & key, dialog->selectedCertificates() ) {
168 data += key.primaryFingerprint();
169 data += '\n';
171 q->sendData( data );
172 q->done();
173 } catch ( const Exception & e ) {
174 q->done( e.error(), e.message() );
175 } catch ( const std::exception & e ) {
176 q->done( makeError( GPG_ERR_UNEXPECTED ),
177 i18n("Caught unexpected exception in SelectCertificateCommand::Private::slotDialogAccepted: %1",
178 QString::fromLocal8Bit( e.what() ) ) );
179 } catch ( ... ) {
180 q->done( makeError( GPG_ERR_UNEXPECTED ),
181 i18n("Caught unknown exception in SelectCertificateCommand::Private::slotDialogAccepted") );
185 void SelectCertificateCommand::Private::slotDialogRejected() {
186 dialog = 0;
187 q->done( makeError( GPG_ERR_CANCELED ) );
190 #include "moc_selectcertificatecommand.cpp"