~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to messagecomposer/signencryptjob.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2009 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.net
 
3
  Copyright (c) 2009 Leo Franchi <lfranchi@kde.org>
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
19
*/
 
20
 
 
21
#include "signencryptjob.h"
 
22
 
 
23
#include "contentjobbase_p.h"
 
24
#include "kleo/cryptobackendfactory.h"
 
25
#include "kleo/cryptobackend.h"
 
26
#include "kleo/enum.h"
 
27
#include "kleo/signencryptjob.h"
 
28
#include "util.h"
 
29
 
 
30
#include <kdebug.h>
 
31
#include <kmime/kmime_message.h>
 
32
#include <kmime/kmime_content.h>
 
33
#include <kmime/kmime_util.h>
 
34
#include <QBuffer>
 
35
 
 
36
#include <gpgme++/global.h>
 
37
#include <gpgme++/signingresult.h>
 
38
#include <gpgme++/encryptionresult.h>
 
39
#include <sstream>
 
40
 
 
41
using namespace Message;
 
42
 
 
43
class Message::SignEncryptJobPrivate : public ContentJobBasePrivate
 
44
{
 
45
  public:
 
46
    SignEncryptJobPrivate( SignEncryptJob *qq )
 
47
      : ContentJobBasePrivate( qq )
 
48
      , content( 0 )
 
49
    {
 
50
    }
 
51
 
 
52
    KMime::Content* content;
 
53
    std::vector<GpgME::Key> signers;
 
54
    Kleo::CryptoMessageFormat format;
 
55
 
 
56
    std::vector<GpgME::Key> encKeys;
 
57
    QStringList recipients;
 
58
    
 
59
    // copied from messagecomposer.cpp
 
60
    bool binaryHint( Kleo::CryptoMessageFormat f )
 
61
    {
 
62
      switch ( f ) {
 
63
      case Kleo::SMIMEFormat:
 
64
      case Kleo::SMIMEOpaqueFormat:
 
65
        return true;
 
66
      default:
 
67
      case Kleo::OpenPGPMIMEFormat:
 
68
      case Kleo::InlineOpenPGPFormat:
 
69
        return false;
 
70
      }
 
71
    }
 
72
 
 
73
    Q_DECLARE_PUBLIC( SignEncryptJob )
 
74
};
 
75
 
 
76
SignEncryptJob::SignEncryptJob( QObject *parent )
 
77
  : ContentJobBase( *new SignEncryptJobPrivate( this ), parent )
 
78
{
 
79
}
 
80
 
 
81
SignEncryptJob::~SignEncryptJob()
 
82
{
 
83
}
 
84
 
 
85
void SignEncryptJob::setContent( KMime::Content* content )
 
86
{
 
87
  Q_D( SignEncryptJob );
 
88
 
 
89
  Q_ASSERT( content );
 
90
 
 
91
  d->content = content;
 
92
}
 
93
 
 
94
void SignEncryptJob::setCryptoMessageFormat( Kleo::CryptoMessageFormat format)
 
95
{
 
96
  Q_D( SignEncryptJob );
 
97
 
 
98
  // There *must* be a concrete format set at this point.
 
99
  Q_ASSERT( format == Kleo::OpenPGPMIMEFormat
 
100
            || format == Kleo::InlineOpenPGPFormat
 
101
            || format == Kleo::SMIMEFormat
 
102
            || format == Kleo::SMIMEOpaqueFormat );
 
103
  d->format = format;
 
104
}
 
105
 
 
106
void SignEncryptJob::setSigningKeys( std::vector<GpgME::Key>& signers )
 
107
{
 
108
  Q_D( SignEncryptJob );
 
109
 
 
110
  d->signers = signers;
 
111
}
 
112
 
 
113
KMime::Content* SignEncryptJob::origContent()
 
114
{
 
115
  Q_D( SignEncryptJob );
 
116
 
 
117
  return d->content;
 
118
 
 
119
}
 
120
 
 
121
 
 
122
void SignEncryptJob::setEncryptionKeys( std::vector<GpgME::Key> keys )
 
123
{
 
124
  Q_D( SignEncryptJob );
 
125
 
 
126
  d->encKeys = keys;
 
127
}
 
128
 
 
129
void SignEncryptJob::setRecipients( QStringList recipients ) {
 
130
  Q_D( SignEncryptJob );
 
131
 
 
132
  d->recipients = recipients;
 
133
}
 
134
 
 
135
QStringList SignEncryptJob::recipients() {
 
136
  Q_D( SignEncryptJob );
 
137
 
 
138
  return d->recipients;
 
139
}
 
140
 
 
141
std::vector<GpgME::Key> SignEncryptJob::encryptionKeys() {
 
142
  Q_D( SignEncryptJob );
 
143
 
 
144
  return d->encKeys;
 
145
}
 
146
 
 
147
 
 
148
void SignEncryptJob::process()
 
149
{
 
150
  Q_D( SignEncryptJob );
 
151
  Q_ASSERT( d->resultContent == 0 ); // Not processed before.
 
152
 
 
153
  // if setContent hasn't been called, we assume that a subjob was added
 
154
  // and we want to use that
 
155
  if( !d->content || !d->content->hasContent() ) {
 
156
    Q_ASSERT( d->subjobContents.size() == 1 );
 
157
    d->content = d->subjobContents.first();
 
158
  }
 
159
 
 
160
  d->resultContent = new KMime::Content;
 
161
 
 
162
  const Kleo::CryptoBackend::Protocol *proto = 0;
 
163
  if( d->format & Kleo::AnyOpenPGP ) {
 
164
    proto = Kleo::CryptoBackendFactory::instance()->openpgp();
 
165
  } else if( d->format & Kleo::AnySMIME ) {
 
166
    proto = Kleo::CryptoBackendFactory::instance()->smime();
 
167
  }
 
168
 
 
169
  Q_ASSERT( proto );
 
170
 
 
171
 
 
172
  kDebug() << "creating signencrypt from:" << proto->name() << proto->displayName();
 
173
  std::auto_ptr<Kleo::SignEncryptJob> job( proto->signEncryptJob( !d->binaryHint( d->format ), d->format == Kleo::InlineOpenPGPFormat ) );
 
174
  QByteArray encBody;
 
175
 
 
176
  // replace simple LFs by CRLFs for all MIME supporting CryptPlugs
 
177
  // according to RfC 2633, 3.1.1 Canonicalization
 
178
  d->content->assemble();
 
179
  QByteArray content;
 
180
  if( d->format & Kleo::InlineOpenPGPFormat ) {
 
181
    content = KMime::LFtoCRLF( d->content->body() );
 
182
  } else {
 
183
    content = KMime::LFtoCRLF( d->content->encodedContent() );
 
184
  }
 
185
 
 
186
  // FIXME: Make this async
 
187
  const std::pair<GpgME::SigningResult,GpgME::EncryptionResult> res = job->exec( d->signers, d->encKeys,
 
188
                                        content,
 
189
                                        false,
 
190
                                        encBody );
 
191
 
 
192
  if ( res.first.error() ) {
 
193
    kDebug() << "signing failed:" << res.first.error().asString();
 
194
    setError( res.first.error().code() );
 
195
    setErrorText( QString::fromLocal8Bit( res.first.error().asString() ) );
 
196
  }
 
197
  if ( res.second.error() ) {
 
198
    kDebug() << "encrypyting failed:" << res.second.error().asString();
 
199
    setError( res.second.error().code() );
 
200
    setErrorText( QString::fromLocal8Bit( res.second.error().asString() ) );
 
201
  }
 
202
 
 
203
  // exec'ed jobs don't delete themselves
 
204
  job->deleteLater();
 
205
  QByteArray signatureHashAlgo =  res.first.createdSignature( 0 ).hashAlgorithmAsString();
 
206
 
 
207
  d->resultContent = Message::Util::composeHeadersAndBody( d->content, encBody, d->format, true, signatureHashAlgo );
 
208
//  d->resultContent->setBody( signature );
 
209
  emitResult();
 
210
}
 
211
 
 
212
#include "signencryptjob.moc"