Build with non-standard boost locations.
[kdepim.git] / messagecomposer / encryptjob.cpp
blob315a324a591d45db13e69f5272d1c98a80799030
1 /*
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
18 02110-1301, USA.
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"
28 #include "util.h"
30 #include <kdebug.h>
32 #include <kmime/kmime_message.h>
33 #include <kmime/kmime_content.h>
34 #include <QBuffer>
36 #include <gpgme++/global.h>
37 #include <gpgme++/signingresult.h>
38 #include <gpgme++/encryptionresult.h>
39 #include <sstream>
41 using namespace Message;
43 class Message::EncryptJobPrivate : public ContentJobBasePrivate
45 public:
46 EncryptJobPrivate( EncryptJob *qq )
47 : ContentJobBasePrivate( qq )
48 , content( 0 )
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 )
60 switch ( f ) {
61 case Kleo::SMIMEFormat:
62 case Kleo::SMIMEOpaqueFormat:
63 return true;
64 default:
65 case Kleo::OpenPGPMIMEFormat:
66 case Kleo::InlineOpenPGPFormat:
67 return false;
72 GpgME::SignatureMode signingMode( Kleo::CryptoMessageFormat f )
74 switch ( f ) {
75 case Kleo::SMIMEOpaqueFormat:
76 return GpgME::NormalSignatureMode;
77 case Kleo::InlineOpenPGPFormat:
78 return GpgME::Clearsigned;
79 default:
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 )
102 Q_D( EncryptJob );
104 d->content = content;
105 d->content->assemble();
108 void EncryptJob::setCryptoMessageFormat( Kleo::CryptoMessageFormat format)
110 Q_D( EncryptJob );
112 d->format = format;
115 void EncryptJob::setEncryptionKeys( std::vector<GpgME::Key> keys )
117 Q_D( EncryptJob );
119 d->keys = keys;
122 void EncryptJob::setRecipients( QStringList recipients ) {
123 Q_D( EncryptJob );
125 d->recipients = recipients;
128 QStringList EncryptJob::recipients() {
129 Q_D( EncryptJob );
131 return d->recipients;
134 std::vector<GpgME::Key> EncryptJob::encryptionKeys() {
135 Q_D( EncryptJob );
137 return d->keys;
140 void EncryptJob::process()
142 Q_D( EncryptJob );
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.";
148 return;
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();
167 Q_ASSERT( proto );
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;
174 QByteArray content;
175 if( d->format & Kleo::InlineOpenPGPFormat ) {
176 content = d->content->body();
177 } else {
178 content = d->content->encodedContent();
180 const GpgME::EncryptionResult res = seJob->exec( d->keys,
181 content,
182 false,
183 encryptedBody );
185 if ( res.error() ) {
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();
194 emitResult();
195 return;
199 #include "encryptjob.moc"