2 Copyright (C) 2009 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.net
3 Copyright (c) 2009 Leo Franchi <lfranchi@kde.org>
5 This library is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or (at your
8 option) any later version.
10 This library is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "encryptjob.h"
23 #include "contentjobbase_p.h"
24 #include "kleo/cryptobackendfactory.h"
25 #include "kleo/cryptobackend.h"
26 #include "kleo/encryptjob.h"
27 #include "kleo/enum.h"
32 #include <kmime/kmime_message.h>
33 #include <kmime/kmime_content.h>
36 #include <gpgme++/global.h>
37 #include <gpgme++/signingresult.h>
38 #include <gpgme++/encryptionresult.h>
41 using namespace Message
;
43 class Message::EncryptJobPrivate
: public ContentJobBasePrivate
46 EncryptJobPrivate( EncryptJob
*qq
)
47 : ContentJobBasePrivate( qq
)
52 KMime::Content
* content
;
53 std::vector
<GpgME::Key
> keys
;
54 Kleo::CryptoMessageFormat format
;
55 QStringList recipients
;
57 // copied from messagecomposer.cpp
58 bool binaryHint( Kleo::CryptoMessageFormat f
)
61 case Kleo::SMIMEFormat
:
62 case Kleo::SMIMEOpaqueFormat
:
65 case Kleo::OpenPGPMIMEFormat
:
66 case Kleo::InlineOpenPGPFormat
:
72 GpgME::SignatureMode
signingMode( Kleo::CryptoMessageFormat f
)
75 case Kleo::SMIMEOpaqueFormat
:
76 return GpgME::NormalSignatureMode
;
77 case Kleo::InlineOpenPGPFormat
:
78 return GpgME::Clearsigned
;
80 case Kleo::SMIMEFormat
:
81 case Kleo::OpenPGPMIMEFormat
:
82 return GpgME::Detached
;
87 Q_DECLARE_PUBLIC( EncryptJob
)
90 EncryptJob::EncryptJob( QObject
*parent
)
91 : ContentJobBase( *new EncryptJobPrivate( this ), parent
)
95 EncryptJob::~EncryptJob()
100 void EncryptJob::setContent( KMime::Content
* content
)
104 d
->content
= content
;
105 d
->content
->assemble();
108 void EncryptJob::setCryptoMessageFormat( Kleo::CryptoMessageFormat format
)
115 void EncryptJob::setEncryptionKeys( std::vector
<GpgME::Key
> keys
)
122 void EncryptJob::setRecipients( QStringList recipients
) {
125 d
->recipients
= recipients
;
128 QStringList
EncryptJob::recipients() {
131 return d
->recipients
;
134 std::vector
<GpgME::Key
> EncryptJob::encryptionKeys() {
140 void EncryptJob::process()
143 Q_ASSERT( d
->resultContent
== 0 ); // Not processed before.
145 kDebug() << "starting encryption job";
146 if( d
->keys
.size() == 0 ) { // should not happen---resolver should have dealt with it earlier
147 kDebug() << "HELP! Encrypt job but have no keys to encrypt with.";
151 // if setContent hasn't been called, we assume that a subjob was added
152 // and we want to use that
153 if( !d
->content
|| !d
->content
->hasContent() ) {
154 Q_ASSERT( d
->subjobContents
.size() == 1 );
155 d
->content
= d
->subjobContents
.first();
158 d
->resultContent
= new KMime::Content
;
160 const Kleo::CryptoBackend::Protocol
*proto
= 0;
161 if( d
->format
& Kleo::AnyOpenPGP
) {
162 proto
= Kleo::CryptoBackendFactory::instance()->openpgp();
163 } else if( d
->format
& Kleo::AnySMIME
) {
164 proto
= Kleo::CryptoBackendFactory::instance()->smime();
169 kDebug() << "got backend, starting job";
170 Kleo::EncryptJob
* seJob
= proto
->encryptJob( !d
->binaryHint( d
->format
), d
->format
== Kleo::InlineOpenPGPFormat
);
172 // for now just do the main recipients
173 QByteArray encryptedBody
;
175 if( d
->format
& Kleo::InlineOpenPGPFormat
) {
176 content
= d
->content
->body();
178 content
= d
->content
->encodedContent();
180 const GpgME::EncryptionResult res
= seJob
->exec( d
->keys
,
186 setError( res
.error().code() );
187 setErrorText( QString::fromLocal8Bit( res
.error().asString() ) );
189 d
->resultContent
= Message::Util::composeHeadersAndBody( d
->content
, encryptedBody
, d
->format
, false );
191 // exec'ed jobs don't delete themselves
192 seJob
->deleteLater();
199 #include "encryptjob.moc"