• 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
  • uiserver
encryptcommand.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  uiserver/encryptcommand.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 "encryptcommand.h"
36 
37 #include <crypto/newsignencryptemailcontroller.h>
38 
39 #include <utils/kleo_assert.h>
40 #include <utils/input.h>
41 #include <utils/output.h>
42 
43 #include <kleo/exception.h>
44 
45 #include <KLocale>
46 
47 #include <QTimer>
48 
49 using namespace Kleo;
50 using namespace Kleo::Crypto;
51 using namespace boost;
52 
53 class EncryptCommand::Private : public QObject {
54  Q_OBJECT
55 private:
56  friend class ::Kleo::EncryptCommand;
57  EncryptCommand * const q;
58 public:
59  explicit Private( EncryptCommand * qq )
60  : q( qq ),
61  controller()
62  {
63 
64  }
65 
66 private:
67  void checkForErrors() const;
68 
69 private Q_SLOTS:
70  void slotDone();
71  void slotError( int, const QString & );
72  void slotRecipientsResolved();
73 
74 private:
75  shared_ptr<NewSignEncryptEMailController> controller;
76 };
77 
78 EncryptCommand::EncryptCommand()
79  : AssuanCommandMixin<EncryptCommand>(), d( new Private( this ) )
80 {
81 
82 }
83 
84 EncryptCommand::~EncryptCommand() {}
85 
86 void EncryptCommand::Private::checkForErrors() const {
87 
88  if ( q->numFiles() )
89  throw Exception( makeError( GPG_ERR_CONFLICT ),
90  i18n( "ENCRYPT is an email mode command, connection seems to be in filmanager mode" ) );
91 
92  if ( !q->senders().empty() && !q->informativeSenders() )
93  throw Exception( makeError( GPG_ERR_CONFLICT ),
94  i18n( "SENDER may not be given prior to ENCRYPT, except with --info" ) );
95 
96  if ( q->inputs().empty() )
97  throw Exception( makeError( GPG_ERR_ASS_NO_INPUT ),
98  i18n( "At least one INPUT must be present" ) );
99 
100  if ( q->outputs().empty() )
101  throw Exception( makeError( GPG_ERR_ASS_NO_OUTPUT ),
102  i18n( "At least one OUTPUT must be present" ) );
103 
104  if ( q->outputs().size() != q->inputs().size() )
105  throw Exception( makeError( GPG_ERR_CONFLICT ),
106  i18n( "INPUT/OUTPUT count mismatch" ) );
107 
108  if ( !q->messages().empty() )
109  throw Exception( makeError( GPG_ERR_INV_VALUE ),
110  i18n( "MESSAGE command is not allowed before ENCRYPT" ) );
111 
112  const shared_ptr<NewSignEncryptEMailController> m = q->mementoContent< shared_ptr<NewSignEncryptEMailController> >( NewSignEncryptEMailController::mementoName() );
113  kleo_assert( m );
114 
115  if ( m && m->isEncrypting() ) {
116 
117  if ( m->protocol() != q->checkProtocol( EMail ) )
118  throw Exception( makeError( GPG_ERR_CONFLICT ),
119  i18n( "Protocol given conflicts with protocol determined by PREP_ENCRYPT" ) );
120 
121  if ( !q->recipients().empty() )
122  throw Exception( makeError( GPG_ERR_CONFLICT ),
123  i18n( "New recipients added after PREP_ENCRYPT command" ) );
124  if ( !q->senders().empty() )
125  throw Exception( makeError( GPG_ERR_CONFLICT ),
126  i18n( "New senders added after PREP_ENCRYPT command" ) );
127 
128  } else {
129 
130  if ( q->recipients().empty() || q->informativeRecipients() )
131  throw Exception( makeError( GPG_ERR_MISSING_VALUE ),
132  i18n( "No recipients given, or only with --info" ) );
133 
134  }
135 
136 }
137 
138 static void connectController( const QObject * controller, const QObject * d ) {
139 
140  QObject::connect( controller, SIGNAL(certificatesResolved()), d, SLOT(slotRecipientsResolved()) );
141  QObject::connect( controller, SIGNAL(done()), d, SLOT(slotDone()) );
142  QObject::connect( controller, SIGNAL(error(int,QString)), d, SLOT(slotError(int,QString)) );
143 
144 }
145 
146 int EncryptCommand::doStart() {
147 
148  d->checkForErrors();
149 
150  const shared_ptr<NewSignEncryptEMailController> seec = mementoContent< shared_ptr<NewSignEncryptEMailController> >( NewSignEncryptEMailController::mementoName() );
151 
152  if ( seec && seec->isEncrypting() ) {
153  // reuse the controller from a previous PREP_ENCRYPT, if available:
154  d->controller = seec;
155  connectController( seec.get(), d.get() );
156  removeMemento( NewSignEncryptEMailController::mementoName() );
157  d->controller->setExecutionContext( shared_from_this() );
158  if ( seec->areCertificatesResolved() )
159  QTimer::singleShot( 0, d.get(), SLOT(slotRecipientsResolved()) );
160  else
161  kleo_assert( seec->isResolvingInProgress() );
162  } else {
163  // use a new controller
164  d->controller.reset( new NewSignEncryptEMailController( shared_from_this() ) );
165 
166  const QString session = sessionTitle();
167  if ( !session.isEmpty() )
168  d->controller->setSubject( session );
169 
170  d->controller->setEncrypting( true );
171  d->controller->setSigning( false );
172  d->controller->setProtocol( checkProtocol( EMail ) );
173  connectController( d->controller.get(), d.get() );
174  d->controller->startResolveCertificates( recipients(), senders() );
175  }
176 
177 
178  return 0;
179 }
180 
181 void EncryptCommand::Private::slotRecipientsResolved() {
182  //hold local shared_ptr to member as q->done() deletes *this
183  const shared_ptr<NewSignEncryptEMailController> cont( controller );
184 
185  try {
186  const QString sessionTitle = q->sessionTitle();
187  if ( !sessionTitle.isEmpty() )
188  Q_FOREACH ( const shared_ptr<Input> & i, q->inputs() )
189  i->setLabel( sessionTitle );
190 
191  cont->startEncryption( q->inputs(), q->outputs() );
192 
193  return;
194 
195  } catch ( const Exception & e ) {
196  q->done( e.error(), e.message() );
197  } catch ( const std::exception & e ) {
198  q->done( makeError( GPG_ERR_UNEXPECTED ),
199  i18n("Caught unexpected exception in EncryptCommand::Private::slotRecipientsResolved: %1",
200  QString::fromLocal8Bit( e.what() ) ) );
201  } catch ( ... ) {
202  q->done( makeError( GPG_ERR_UNEXPECTED ),
203  i18n("Caught unknown exception in EncryptCommand::Private::slotRecipientsResolved") );
204  }
205  cont->cancel();
206 }
207 
208 void EncryptCommand::Private::slotDone() {
209  q->done();
210 }
211 
212 void EncryptCommand::Private::slotError( int err, const QString & details ) {
213  q->done( err, details );
214 }
215 
216 void EncryptCommand::doCanceled() {
217  if ( d->controller )
218  d->controller->cancel();
219 }
220 
221 #include "encryptcommand.moc"
output.h
Kleo::EncryptCommand
Definition: encryptcommand.h:42
input.h
Kleo::EncryptCommand::~EncryptCommand
virtual ~EncryptCommand()
Definition: encryptcommand.cpp:84
Kleo::AssuanCommandMixin
Definition: assuancommand.h:367
kleo_assert.h
boost::shared_ptr< NewSignEncryptEMailController >
d
#define d
Definition: adduseridcommand.cpp:90
Kleo::EncryptCommand::EncryptCommand
EncryptCommand()
Definition: encryptcommand.cpp:78
newsignencryptemailcontroller.h
kdtools::pimpl_ptr::get
T * get()
Definition: pimpl_ptr.h:39
kleo_assert
#define kleo_assert(cond)
Definition: kleo_assert.h:84
encryptcommand.h
q
#define q
Definition: adduseridcommand.cpp:91
connectController
static void connectController(const QObject *controller, const QObject *d)
Definition: encryptcommand.cpp:138
Kleo::Crypto::NewSignEncryptEMailController
Definition: newsignencryptemailcontroller.h:68
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