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

kleopatra

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

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