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