• 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
encryptemailcontroller.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  crypto/encryptemailcontroller.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 "encryptemailcontroller.h"
36 
37 #include "encryptemailtask.h"
38 #include "taskcollection.h"
39 
40 #include <crypto/gui/encryptemailwizard.h>
41 
42 
43 #include <utils/input.h>
44 #include <utils/output.h>
45 #include <utils/kleo_assert.h>
46 
47 #include <kleo/stl_util.h>
48 #include <kleo/exception.h>
49 
50 #include "emailoperationspreferences.h"
51 
52 #include <gpgme++/key.h>
53 
54 #include <kmime/kmime_header_parsing.h>
55 
56 #include <KLocalizedString>
57 
58 #include <QPointer>
59 #include <QTimer>
60 
61 #ifndef Q_MOC_RUN
62 #include <boost/bind.hpp>
63 #include <boost/shared_ptr.hpp>
64 #endif
65 
66 using namespace Kleo;
67 using namespace Kleo::Crypto;
68 using namespace Kleo::Crypto::Gui;
69 using namespace boost;
70 using namespace GpgME;
71 using namespace KMime::Types;
72 
73 class EncryptEMailController::Private {
74  friend class ::Kleo::Crypto::EncryptEMailController;
75  EncryptEMailController * const q;
76 public:
77  explicit Private( Mode mode, EncryptEMailController * qq );
78 
79 private:
80  void slotWizardRecipientsResolved();
81  void slotWizardCanceled();
82 
83 private:
84  void ensureWizardCreated() const;
85  void ensureWizardVisible();
86  void cancelAllTasks();
87 
88  void schedule();
89  shared_ptr<EncryptEMailTask> takeRunnable( GpgME::Protocol proto );
90 
91 private:
92  const Mode mode;
93  std::vector< shared_ptr<EncryptEMailTask> > runnable, completed;
94  shared_ptr<EncryptEMailTask> cms, openpgp;
95  mutable QPointer<EncryptEMailWizard> wizard;
96 };
97 
98 EncryptEMailController::Private::Private( Mode m, EncryptEMailController * qq )
99  : q( qq ),
100  mode( m ),
101  runnable(),
102  cms(),
103  openpgp(),
104  wizard()
105 {
106 
107 }
108 
109 EncryptEMailController::EncryptEMailController( const shared_ptr<ExecutionContext> & xc, Mode mode, QObject * p )
110  : Controller( xc, p ), d( new Private( mode, this ) )
111 {
112 
113 }
114 
115 EncryptEMailController::EncryptEMailController( Mode mode, QObject * p )
116  : Controller( p ), d( new Private( mode, this ) )
117 {
118 
119 }
120 
121 EncryptEMailController::~EncryptEMailController() {
122  if ( d->wizard && !d->wizard->isVisible() )
123  delete d->wizard;
124  //d->wizard->close(); ### ?
125 }
126 
127 EncryptEMailController::Mode EncryptEMailController::mode() const {
128  return d->mode;
129 }
130 
131 void EncryptEMailController::setProtocol( Protocol proto ) {
132  d->ensureWizardCreated();
133  const Protocol protocol = d->wizard->presetProtocol();
134  kleo_assert( protocol == UnknownProtocol ||
135  protocol == proto );
136 
137  d->wizard->setPresetProtocol( proto );
138 }
139 
140 Protocol EncryptEMailController::protocol() const {
141  d->ensureWizardCreated();
142  return d->wizard->selectedProtocol();
143 }
144 
145 
146 const char * EncryptEMailController::protocolAsString() const {
147  switch ( protocol() ) {
148  case OpenPGP: return "OpenPGP";
149  case CMS: return "CMS";
150  default:
151  throw Kleo::Exception( gpg_error( GPG_ERR_INTERNAL ),
152  i18n("Call to EncryptEMailController::protocolAsString() is ambiguous.") );
153  }
154 }
155 
156 void EncryptEMailController::startResolveRecipients() {
157  startResolveRecipients( std::vector<Mailbox>(), std::vector<Mailbox>() );
158 }
159 
160 void EncryptEMailController::startResolveRecipients( const std::vector<Mailbox> & recipients, const std::vector<Mailbox> & senders ) {
161  d->ensureWizardCreated();
162  d->wizard->setRecipients( recipients, senders );
163  d->ensureWizardVisible();
164 }
165 
166 void EncryptEMailController::Private::slotWizardRecipientsResolved() {
167  emit q->recipientsResolved();
168 }
169 
170 void EncryptEMailController::Private::slotWizardCanceled() {
171  q->setLastError( gpg_error( GPG_ERR_CANCELED ), i18n("User cancel") );
172  q->emitDoneOrError();
173 }
174 
175 void EncryptEMailController::setInputAndOutput( const shared_ptr<Input> & input, const shared_ptr<Output> & output ) {
176  setInputsAndOutputs( std::vector< shared_ptr<Input> >( 1, input ), std::vector< shared_ptr<Output> >( 1, output ) );
177 }
178 
179 void EncryptEMailController::setInputsAndOutputs( const std::vector< shared_ptr<Input> > & inputs, const std::vector< shared_ptr<Output> > & outputs ) {
180 
181  kleo_assert( !inputs.empty() );
182  kleo_assert( outputs.size() == inputs.size() );
183 
184  std::vector< shared_ptr<EncryptEMailTask> > tasks;
185  tasks.reserve( inputs.size() );
186 
187  d->ensureWizardCreated();
188 
189  const std::vector<Key> keys = d->wizard->resolvedCertificates();
190  kleo_assert( !keys.empty() );
191 
192  for ( unsigned int i = 0, end = inputs.size() ; i < end ; ++i ) {
193 
194  const shared_ptr<EncryptEMailTask> task( new EncryptEMailTask );
195  task->setInput( inputs[i] );
196  task->setOutput( outputs[i] );
197  if ( d->mode == ClipboardMode )
198  task->setAsciiArmor( true );
199  task->setRecipients( keys );
200 
201  tasks.push_back( task );
202  }
203 
204  d->runnable.swap( tasks );
205 }
206 
207 void EncryptEMailController::start() {
208  shared_ptr<TaskCollection> coll( new TaskCollection );
209  std::vector<shared_ptr<Task> > tmp;
210  std::copy( d->runnable.begin(), d->runnable.end(), std::back_inserter( tmp ) );
211  coll->setTasks( tmp );
212  d->ensureWizardCreated();
213  d->wizard->setTaskCollection( coll );
214  Q_FOREACH( const shared_ptr<Task> & t, tmp )
215  connectTask( t );
216  d->schedule();
217 }
218 
219 void EncryptEMailController::Private::schedule() {
220 
221  if ( !cms )
222  if ( const shared_ptr<EncryptEMailTask> t = takeRunnable( CMS ) ) {
223  t->start();
224  cms = t;
225  }
226 
227  if ( !openpgp )
228  if ( const shared_ptr<EncryptEMailTask> t = takeRunnable( OpenPGP ) ) {
229  t->start();
230  openpgp = t;
231  }
232 
233  if ( cms || openpgp )
234  return;
235  kleo_assert( runnable.empty() );
236  q->emitDoneOrError();
237 }
238 
239 shared_ptr<EncryptEMailTask> EncryptEMailController::Private::takeRunnable( GpgME::Protocol proto ) {
240  const std::vector< shared_ptr<EncryptEMailTask> >::iterator it
241  = std::find_if( runnable.begin(), runnable.end(),
242  boost::bind( &Task::protocol, _1 ) == proto );
243  if ( it == runnable.end() )
244  return shared_ptr<EncryptEMailTask>();
245 
246  const shared_ptr<EncryptEMailTask> result = *it;
247  runnable.erase( it );
248  return result;
249 }
250 
251 void EncryptEMailController::doTaskDone( const Task * task, const shared_ptr<const Task::Result> & result )
252 {
253  Q_UNUSED( result );
254  assert( task );
255 
256  // We could just delete the tasks here, but we can't use
257  // Qt::QueuedConnection here (we need sender()) and other slots
258  // might not yet have executed. Therefore, we push completed tasks
259  // into a burial container
260 
261  if ( task == d->cms.get() ) {
262  d->completed.push_back( d->cms );
263  d->cms.reset();
264  } else if ( task == d->openpgp.get() ) {
265  d->completed.push_back( d->openpgp );
266  d->openpgp.reset();
267  }
268 
269  QTimer::singleShot( 0, this, SLOT(schedule()) );
270 }
271 
272 void EncryptEMailController::cancel() {
273  try {
274  if ( d->wizard )
275  d->wizard->close();
276  d->cancelAllTasks();
277  } catch ( const std::exception & e ) {
278  kDebug() << "Caught exception: " << e.what();
279  }
280 }
281 
282 void EncryptEMailController::Private::cancelAllTasks() {
283 
284  // we just kill all runnable tasks - this will not result in
285  // signal emissions.
286  runnable.clear();
287 
288  // a cancel() will result in a call to
289  if ( cms )
290  cms->cancel();
291  if ( openpgp )
292  openpgp->cancel();
293 }
294 
295 void EncryptEMailController::Private::ensureWizardCreated() const {
296  if ( wizard )
297  return;
298 
299  std::auto_ptr<EncryptEMailWizard> w( new EncryptEMailWizard );
300  w->setAttribute( Qt::WA_DeleteOnClose );
301  Kleo::EMailOperationsPreferences prefs;
302  w->setQuickMode( prefs.quickEncryptEMail() );
303  connect( w.get(), SIGNAL(recipientsResolved()), q, SLOT(slotWizardRecipientsResolved()), Qt::QueuedConnection );
304  connect( w.get(), SIGNAL(canceled()), q, SLOT(slotWizardCanceled()), Qt::QueuedConnection );
305 
306  wizard = w.release();
307 }
308 
309 void EncryptEMailController::Private::ensureWizardVisible() {
310  ensureWizardCreated();
311  q->bringToForeground( wizard );
312 }
313 
314 #include "moc_encryptemailcontroller.cpp"
315 
316 
encryptemailcontroller.h
output.h
input.h
Kleo::Crypto::EncryptEMailController::ClipboardMode
Definition: encryptemailcontroller.h:68
Kleo::Crypto::EncryptEMailController::start
void start()
Definition: encryptemailcontroller.cpp:207
Kleo::Crypto::EncryptEMailController::cancel
void cancel()
Definition: encryptemailcontroller.cpp:272
Kleo::Crypto::Task
Definition: task.h:57
QPointer< EncryptEMailWizard >
Kleo::Crypto::EncryptEMailController::protocolAsString
const char * protocolAsString() const
Definition: encryptemailcontroller.cpp:146
Kleo::Crypto::EncryptEMailController::setInputsAndOutputs
void setInputsAndOutputs(const std::vector< boost::shared_ptr< Kleo::Input > > &inputs, const std::vector< boost::shared_ptr< Kleo::Output > > &outputs)
Definition: encryptemailcontroller.cpp:179
kleo_assert.h
boost::shared_ptr
Definition: encryptemailcontroller.h:51
encryptemailtask.h
d
#define d
Definition: adduseridcommand.cpp:89
QObject
Kleo::Class::OpenPGP
Definition: classify.h:49
Kleo::Crypto::EncryptEMailController::Mode
Mode
Definition: encryptemailcontroller.h:66
Kleo::Crypto::EncryptEMailController::~EncryptEMailController
~EncryptEMailController()
Definition: encryptemailcontroller.cpp:121
kdtools::pimpl_ptr::get
T * get()
Definition: pimpl_ptr.h:39
Kleo::Class::CMS
Definition: classify.h:48
Kleo::Crypto::EncryptEMailController::EncryptEMailController
EncryptEMailController(Mode mode, QObject *parent=0)
Definition: encryptemailcontroller.cpp:115
Kleo::Crypto::EncryptEMailController::protocol
GpgME::Protocol protocol() const
Definition: encryptemailcontroller.cpp:140
Kleo::Crypto::Gui::EncryptEMailWizard
Definition: encryptemailwizard.h:42
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::TaskCollection
Definition: taskcollection.h:51
q
#define q
Definition: adduseridcommand.cpp:90
Kleo::Crypto::EncryptEMailController::setInputAndOutput
void setInputAndOutput(const boost::shared_ptr< Kleo::Input > &input, const boost::shared_ptr< Kleo::Output > &output)
Definition: encryptemailcontroller.cpp:175
Kleo::Crypto::EncryptEMailController::startResolveRecipients
void startResolveRecipients()
Definition: encryptemailcontroller.cpp:156
encryptemailwizard.h
Kleo::Crypto::EncryptEMailController::mode
Mode mode() const
Definition: encryptemailcontroller.cpp:127
Kleo::Crypto::EncryptEMailTask
Definition: encryptemailtask.h:60
Kleo::Crypto::EncryptEMailController::setProtocol
void setProtocol(GpgME::Protocol proto)
Definition: encryptemailcontroller.cpp:131
Kleo::Crypto::Task::protocol
virtual GpgME::Protocol protocol() const =0
Kleo::Crypto::Controller
Definition: controller.h:52
Kleo::Crypto::EncryptEMailController
Definition: encryptemailcontroller.h:63
taskcollection.h
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:10 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