• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kleopatra

  • sources
  • kde-4.12
  • kdepim
  • kleopatra
  • crypto
signemailcontroller.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  crypto/signemailcontroller.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2007 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 "signemailcontroller.h"
36 
37 #include "signemailtask.h"
38 #include "certificateresolver.h"
39 #include "taskcollection.h"
40 
41 #include <crypto/gui/signemailwizard.h>
42 
43 #include <utils/input.h>
44 #include <utils/output.h>
45 #include <utils/kleo_assert.h>
46 
47 #include "emailoperationspreferences.h"
48 
49 #include <kleo/stl_util.h>
50 
51 #include <kmime/kmime_header_parsing.h>
52 
53 #include <KLocale>
54 
55 #include <QPointer>
56 #include <QTimer>
57 
58 #include <boost/bind.hpp>
59 #include <boost/shared_ptr.hpp>
60 
61 using namespace Kleo;
62 using namespace Kleo::Crypto;
63 using namespace Kleo::Crypto::Gui;
64 using namespace boost;
65 using namespace GpgME;
66 using namespace KMime::Types;
67 
68 class SignEMailController::Private {
69  friend class ::Kleo::Crypto::SignEMailController;
70  SignEMailController * const q;
71 public:
72  explicit Private( Mode m, SignEMailController * qq );
73  ~Private();
74 
75 private:
76  void slotWizardSignersResolved();
77  void slotWizardCanceled(); // ### extract to base
78 
79 private:
80  void ensureWizardCreated(); // ### extract to base
81  void ensureWizardVisible(); // ### extract to base
82  void cancelAllJobs(); // ### extract to base
83 
84  void schedule(); // ### extract to base
85  shared_ptr<SignEMailTask> takeRunnable( GpgME::Protocol proto ); // ### extract to base
86 
87 private:
88  const Mode mode;
89  std::vector< shared_ptr<SignEMailTask> > runnable, completed; // ### extract to base
90  shared_ptr<SignEMailTask> cms, openpgp; // ### extract to base
91  QPointer<SignEMailWizard> wizard; // ### extract to base
92  Protocol protocol; // ### extract to base
93  bool detached : 1;
94 };
95 
96 SignEMailController::Private::Private( Mode m, SignEMailController * qq )
97  : q( qq ),
98  mode( m ),
99  runnable(),
100  cms(),
101  openpgp(),
102  wizard(),
103  protocol( UnknownProtocol ),
104  detached( false )
105 {
106 
107 }
108 
109 SignEMailController::Private::~Private() {}
110 
111 SignEMailController::SignEMailController( Mode mode, QObject * p )
112  : Controller( p ), d( new Private( mode, this ) )
113 {
114 
115 }
116 
117 SignEMailController::SignEMailController( const boost::shared_ptr<ExecutionContext> & xc, Mode mode, QObject * p )
118  : Controller( xc, p ), d( new Private( mode, this ) )
119 {
120 
121 }
122 
123 SignEMailController::~SignEMailController() {
125  if ( d->wizard && !d->wizard->isVisible() )
126  delete d->wizard;
127  //d->wizard->close(); ### ?
128 }
129 
130 SignEMailController::Mode SignEMailController::mode() const {
131  return d->mode;
132 }
133 
134 // ### extract to base
135 void SignEMailController::setProtocol( Protocol proto ) {
136  kleo_assert( d->protocol == UnknownProtocol ||
137  d->protocol == proto );
138  d->protocol = proto;
139  d->ensureWizardCreated();
140  d->wizard->setPresetProtocol( proto );
141 }
142 
143 Protocol SignEMailController::protocol() const {
144  return d->protocol;
145 }
146 
147 void SignEMailController::startResolveSigners() {
148  startResolveSigners( std::vector<Mailbox>() );
149 }
150 
151 void SignEMailController::startResolveSigners( const std::vector<Mailbox> & signers ) {
152  const std::vector< std::vector<Key> > keys = CertificateResolver::resolveSigners( signers, d->protocol );
153 
154  if ( !signers.empty() ) {
155  kleo_assert( keys.size() == static_cast<size_t>( signers.size() ) );
156  }
157 
158  d->ensureWizardCreated();
159 
160  d->wizard->setSignersAndCandidates( signers, keys );
161 
162  d->ensureWizardVisible();
163 }
164 
165 void SignEMailController::setDetachedSignature( bool detached ) {
166  kleo_assert( !d->openpgp );
167  kleo_assert( !d->cms );
168  kleo_assert( d->completed.empty() );
169  kleo_assert( d->runnable.empty() );
170 
171  d->detached = detached;
172 }
173 
174 void SignEMailController::Private::slotWizardSignersResolved() {
175  emit q->signersResolved();
176 }
177 
178 // ### extract to base
179 void SignEMailController::Private::slotWizardCanceled() {
180  q->setLastError( gpg_error( GPG_ERR_CANCELED ), i18n("User cancel") );
181  q->emitDoneOrError();
182 }
183 
184 void SignEMailController::setInputAndOutput( const shared_ptr<Input> & input, const shared_ptr<Output> & output ) {
185  setInputsAndOutputs( std::vector< shared_ptr<Input> >( 1, input ), std::vector< shared_ptr<Output> >( 1, output ) );
186 }
187 
188 // ### extract to base
189 void SignEMailController::setInputsAndOutputs( const std::vector< shared_ptr<Input> > & inputs, const std::vector< shared_ptr<Output> > & outputs ) {
190  kleo_assert( !inputs.empty() );
191  kleo_assert( !outputs.empty() );
192 
193  std::vector< shared_ptr<SignEMailTask> > tasks;
194  tasks.reserve( inputs.size() );
195 
196  d->ensureWizardCreated();
197 
198  const std::vector<Key> keys = d->wizard->resolvedSigners();
199  kleo_assert( !keys.empty() );
200 
201  for ( unsigned int i = 0, end = inputs.size() ; i < end ; ++i ) {
202 
203  const shared_ptr<SignEMailTask> task( new SignEMailTask );
204  task->setInput( inputs[i] );
205  task->setOutput( outputs[i] );
206  task->setSigners( keys );
207  task->setDetachedSignature( d->detached );
208  if ( d->mode == ClipboardMode ) {
209  if ( d->protocol == OpenPGP )
210  task->setClearsign( true );
211  else
212  task->setAsciiArmor( true );
213  }
214 
215  tasks.push_back( task );
216  }
217 
218  d->runnable.swap( tasks );
219 }
220 
221 // ### extract to base
222 void SignEMailController::start() {
223  shared_ptr<TaskCollection> coll( new TaskCollection );
224  std::vector<shared_ptr<Task> > tmp;
225  std::copy( d->runnable.begin(), d->runnable.end(), std::back_inserter( tmp ) );
226  coll->setTasks( tmp );
227  d->ensureWizardCreated();
228  d->wizard->setTaskCollection( coll );
229  Q_FOREACH( const shared_ptr<Task> & t, tmp )
230  connectTask( t );
231 
232  d->schedule();
233 }
234 
235 // ### extract to base
236 void SignEMailController::Private::schedule() {
237 
238  if ( !cms )
239  if ( const shared_ptr<SignEMailTask> t = takeRunnable( CMS ) ) {
240  t->start();
241  cms = t;
242  }
243 
244  if ( !openpgp )
245  if ( const shared_ptr<SignEMailTask> t = takeRunnable( OpenPGP ) ) {
246  t->start();
247  openpgp = t;
248  }
249 
250  if ( !cms && !openpgp ) {
251  kleo_assert( runnable.empty() );
252  QPointer<QObject> Q = q;
253  Q_FOREACH( const shared_ptr<SignEMailTask> t, completed ) {
254  emit q->reportMicAlg( t->micAlg() );
255  if ( !Q )
256  return;
257  }
258  q->emitDoneOrError();
259  }
260 }
261 
262 // ### extract to base
263 shared_ptr<SignEMailTask> SignEMailController::Private::takeRunnable( GpgME::Protocol proto ) {
264  const std::vector< shared_ptr<SignEMailTask> >::iterator it
265  = std::find_if( runnable.begin(), runnable.end(),
266  boost::bind( &Task::protocol, _1 ) == proto );
267  if ( it == runnable.end() )
268  return shared_ptr<SignEMailTask>();
269 
270  const shared_ptr<SignEMailTask> result = *it;
271  runnable.erase( it );
272  return result;
273 }
274 
275 // ### extract to base
276 void SignEMailController::doTaskDone( const Task * task, const shared_ptr<const Task::Result> & result )
277 {
278  Q_UNUSED( result );
279  assert( task );
280 
281  // We could just delete the tasks here, but we can't use
282  // Qt::QueuedConnection here (we need sender()) and other slots
283  // might not yet have executed. Therefore, we push completed tasks
284  // into a burial container
285 
286  if ( task == d->cms.get() ) {
287  d->completed.push_back( d->cms );
288  d->cms.reset();
289  } else if ( task == d->openpgp.get() ) {
290  d->completed.push_back( d->openpgp );
291  d->openpgp.reset();
292  }
293 
294  QTimer::singleShot( 0, this, SLOT(schedule()) );
295 }
296 
297 // ### extract to base
298 void SignEMailController::cancel() {
299  try {
300  if ( d->wizard )
301  d->wizard->close();
302  d->cancelAllJobs();
303  } catch ( const std::exception & e ) {
304  kDebug() << "Caught exception: " << e.what();
305  }
306 }
307 
308 // ### extract to base
309 void SignEMailController::Private::cancelAllJobs() {
310 
311  // we just kill all runnable tasks - this will not result in
312  // signal emissions.
313  runnable.clear();
314 
315  // a cancel() will result in a call to
316  if ( cms )
317  cms->cancel();
318  if ( openpgp )
319  openpgp->cancel();
320 }
321 
322 // ### extract to base
323 void SignEMailController::Private::ensureWizardCreated() {
324  if ( wizard )
325  return;
326 
327  std::auto_ptr<SignEMailWizard> w( new SignEMailWizard );
328  w->setAttribute( Qt::WA_DeleteOnClose );
329  connect( w.get(), SIGNAL(signersResolved()), q, SLOT(slotWizardSignersResolved()), Qt::QueuedConnection );
330  connect( w.get(), SIGNAL(canceled()), q, SLOT(slotWizardCanceled()), Qt::QueuedConnection );
331  w->setPresetProtocol( protocol );
332  EMailOperationsPreferences prefs;
333  w->setQuickMode( prefs.quickSignEMail() );
334  wizard = w.release();
335 }
336 
337 // ### extract to base
338 void SignEMailController::Private::ensureWizardVisible() {
339  ensureWizardCreated();
340  q->bringToForeground( wizard );
341 }
342 
343 #include "moc_signemailcontroller.cpp"
344 
345 
signemailwizard.h
emailoperationspreferences.h
output.h
input.h
Kleo::Crypto::SignEMailController::mode
Mode mode() const
Definition: signemailcontroller.cpp:130
Kleo::Crypto::SignEMailController::protocol
GpgME::Protocol protocol() const
Definition: signemailcontroller.cpp:143
Kleo::Crypto::Task
Definition: task.h:55
Kleo::Crypto::SignEMailController::setProtocol
void setProtocol(GpgME::Protocol proto)
Definition: signemailcontroller.cpp:135
signemailcontroller.h
Kleo::Crypto::SignEMailController::setInputAndOutput
void setInputAndOutput(const boost::shared_ptr< Kleo::Input > &input, const boost::shared_ptr< Kleo::Output > &output)
Definition: signemailcontroller.cpp:184
kleo_assert.h
boost::shared_ptr
Definition: encryptemailcontroller.h:51
d
#define d
Definition: adduseridcommand.cpp:90
Kleo::Crypto::CertificateResolver::resolveSigners
static std::vector< std::vector< GpgME::Key > > resolveSigners(const std::vector< KMime::Types::Mailbox > &signers, GpgME::Protocol proto)
Definition: certificateresolver.cpp:80
Kleo::Crypto::Gui::SignEMailWizard
Definition: signemailwizard.h:44
Kleo::Class::OpenPGP
Definition: classify.h:49
Kleo::Crypto::SignEMailTask
Definition: signemailtask.h:58
Kleo::EMailOperationsPreferences
Definition: emailoperationspreferences.h:12
kdtools::pimpl_ptr::get
T * get()
Definition: pimpl_ptr.h:39
Kleo::Class::CMS
Definition: classify.h:48
Kleo::EMailOperationsPreferences::quickSignEMail
bool quickSignEMail() const
Get Quick Sign EMail.
Definition: emailoperationspreferences.h:31
Kleo::Crypto::SignEMailController::SignEMailController
SignEMailController(Mode mode, QObject *parent=0)
Definition: signemailcontroller.cpp:111
Kleo::Crypto::SignEMailController::setInputsAndOutputs
void setInputsAndOutputs(const std::vector< boost::shared_ptr< Kleo::Input > > &inputs, const std::vector< boost::shared_ptr< Kleo::Output > > &outputs)
Definition: signemailcontroller.cpp:189
Kleo::Crypto::SignEMailController::~SignEMailController
~SignEMailController()
Definition: signemailcontroller.cpp:123
Kleo::Crypto::Controller::connectTask
void connectTask(const boost::shared_ptr< Task > &task)
Definition: controller.cpp:86
kleo_assert
#define kleo_assert(cond)
Definition: kleo_assert.h:84
Kleo::Crypto::SignEMailController::startResolveSigners
void startResolveSigners()
Definition: signemailcontroller.cpp:147
Kleo::Crypto::TaskCollection
Definition: taskcollection.h:49
Kleo::Crypto::SignEMailController::ClipboardMode
Definition: signemailcontroller.h:66
q
#define q
Definition: adduseridcommand.cpp:91
Kleo::Crypto::SignEMailController::setDetachedSignature
void setDetachedSignature(bool detached)
Definition: signemailcontroller.cpp:165
Kleo::Crypto::SignEMailController::cancel
void cancel()
Definition: signemailcontroller.cpp:298
certificateresolver.h
Kleo::Crypto::SignEMailController::Mode
Mode
Definition: signemailcontroller.h:64
signemailtask.h
Kleo::Crypto::Task::protocol
virtual GpgME::Protocol protocol() const =0
Kleo::Crypto::Controller
Definition: controller.h:50
taskcollection.h
Kleo::Crypto::SignEMailController::start
void start()
Definition: signemailcontroller.cpp:222
Kleo::Crypto::SignEMailController
Definition: signemailcontroller.h:61
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:42 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

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