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

libkleo

  • sources
  • kde-4.12
  • kdepim
  • libkleo
  • backends
  • chiasmus
chiasmusjob.cpp
Go to the documentation of this file.
1 /*
2  chiasmusjob.cpp
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  Copyright (c) 2005 Klar�vdalens Datakonsult AB
6 
7  Libkleopatra is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of the
10  License, or (at your option) any later version.
11 
12  Libkleopatra 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 along
18  with this program; if not, write to the Free Software Foundation, Inc.,
19  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 "chiasmusjob.h"
34 #include "chiasmusbackend.h"
35 #include "symcryptrunprocessbase.h"
36 
37 #include "kleo/cryptoconfig.h"
38 
39 #include <gpgme++/error.h>
40 
41 #include <kshell.h>
42 #include <klocale.h>
43 #include <kdebug.h>
44 #include <kmessagebox.h>
45 
46 #include <QTimer>
47 #include <QFileInfo>
48 #include <QVariant>
49 
50 #include <memory>
51 
52 #include <cassert>
53 
54 using namespace GpgME;
55 
56 Kleo::ChiasmusJob::ChiasmusJob( Mode mode )
57  : Kleo::SpecialJob( 0 ),
58  mSymCryptRun( 0 ),
59  mError( 0 ),
60  mCanceled( false ),
61  mTimeout( false ),
62  mMode( mode )
63 {
64 
65 }
66 
67 Kleo::ChiasmusJob::~ChiasmusJob() {}
68 
69 GpgME::Error Kleo::ChiasmusJob::setup() {
70  if ( !checkPreconditions() )
71  return mError = Error::fromCode( GPG_ERR_INV_VALUE );
72 
73  const Kleo::CryptoConfigEntry * class_
74  = ChiasmusBackend::instance()->config()->entry( QLatin1String("Chiasmus"), QLatin1String("General"), QLatin1String("symcryptrun-class") );
75  const Kleo::CryptoConfigEntry * chiasmus
76  = ChiasmusBackend::instance()->config()->entry( QLatin1String("Chiasmus"), QLatin1String("General"), QLatin1String("path") );
77  const Kleo::CryptoConfigEntry * timeoutEntry
78  = ChiasmusBackend::instance()->config()->entry( QLatin1String("Chiasmus"), QLatin1String("General"), QLatin1String("timeout") );
79  if ( !class_ || !chiasmus || !timeoutEntry )
80  return mError = Error::fromCode( GPG_ERR_INTERNAL );
81 
82  mSymCryptRun = new SymCryptRunProcessBase( class_->stringValue(),
83  KShell::tildeExpand( chiasmus->urlValue().path() ),
84  mKey, mOptions,
85  mMode == Encrypt
86  ? SymCryptRunProcessBase::Encrypt
87  : SymCryptRunProcessBase::Decrypt,
88  this );
89  mSymCryptRun->setObjectName( QLatin1String("symcryptrun") );
90  QTimer::singleShot( timeoutEntry->uintValue() * 1000, this,
91  SLOT(slotTimeout()) );
92  return GpgME::Error();
93 }
94 
95 namespace {
96  struct LaterDeleter {
97  QObject * _this;
98  LaterDeleter( QObject * o ) : _this( o ) {}
99  ~LaterDeleter() { if ( _this ) _this->deleteLater(); }
100  void disable() { _this = 0; }
101  };
102 }
103 
104 GpgME::Error Kleo::ChiasmusJob::start() {
105 
106  LaterDeleter d( this );
107 
108  if ( const GpgME::Error err = setup() )
109  return mError = err;
110 
111  connect( mSymCryptRun, SIGNAL(finished(int,QProcess::ExitStatus)),
112  this, SLOT(finished()) );
113 
114  if ( !mSymCryptRun->launch( mInput ) )
115  return mError = Error::fromCode( GPG_ERR_ENOENT ); // what else?
116 
117  d.disable();
118  return mError = GpgME::Error();
119 }
120 
121 GpgME::Error Kleo::ChiasmusJob::finished() {
122  if ( !mSymCryptRun )
123  mError = Error::fromCode( GPG_ERR_INTERNAL );
124  else if ( mCanceled )
125  mError = Error::fromCode( GPG_ERR_CANCELED );
126  else if ( mTimeout )
127  mError = Error::fromCode( GPG_ERR_TIMEOUT );
128  else if ( mSymCryptRun->exitStatus() != QProcess::NormalExit )
129  mError = Error::fromCode( GPG_ERR_GENERAL );
130  else
131  switch ( mSymCryptRun->exitCode() ) {
132  case 0: // success
133  mOutput = mSymCryptRun->output();
134  mError = GpgME::Error();
135  break;
136  default:
137  case 1: // Some error occurred
138  mStderr = mSymCryptRun->stdErr();
139  mError = Error::fromCode( GPG_ERR_GENERAL );
140  break;
141  case 2: // No valid passphrase was provided
142  mError = Error::fromCode( GPG_ERR_INV_PASSPHRASE );
143  break;
144  case 3: // Canceled
145  mError = Error::fromCode( GPG_ERR_CANCELED );
146  break;
147  }
148 
149  const Kleo::CryptoConfigEntry * showOutput
150  = ChiasmusBackend::instance()->config()->entry( QLatin1String("Chiasmus"), QLatin1String("General"), QLatin1String("show-output") );
151  if ( showOutput && showOutput->boolValue() ) {
152  showChiasmusOutput();
153  }
154 
155  emit done();
156  emit SpecialJob::result( mError, QVariant( mOutput ) );
157  return mError;
158 }
159 
160 void Kleo::ChiasmusJob::showChiasmusOutput() {
161  kDebug(5150) ;
162  if ( mStderr.isEmpty() )
163  return;
164  KMessageBox::information( 0 /*how to get a parent widget?*/,
165  mStderr,
166  i18n( "Output from chiasmus" ) );
167 }
168 
169 GpgME::Error Kleo::ChiasmusJob::exec() {
170  if ( const GpgME::Error err = setup() )
171  return mError = err;
172 
173  if ( !mSymCryptRun->launch( mInput, true ) ) {
174  delete mSymCryptRun; mSymCryptRun = 0;
175  return mError = Error::fromCode( GPG_ERR_ENOENT ); // what else?
176  }
177 
178  const GpgME::Error err = finished();
179  delete mSymCryptRun; mSymCryptRun = 0;
180  return err;
181 }
182 
183 bool Kleo::ChiasmusJob::checkPreconditions() const {
184  return !mKey.isEmpty();
185 }
186 
187 void Kleo::ChiasmusJob::slotCancel() {
188  if ( mSymCryptRun )
189  mSymCryptRun->kill();
190  mCanceled = true;
191 }
192 
193 void Kleo::ChiasmusJob::slotTimeout() {
194  if ( !mSymCryptRun )
195  return;
196  mSymCryptRun->kill();
197  mTimeout = true;
198 }
199 
200 
201 void Kleo::ChiasmusJob::showErrorDialog( QWidget * parent, const QString & caption ) const {
202  if ( !mError )
203  return;
204  if ( mError.isCanceled() )
205  return;
206  const QString reason = QString::fromLocal8Bit( mError.asString() );
207  const QString msg = ( mMode == Encrypt
208  ? i18n( "Encryption failed: %1", reason )
209  : i18n( "Decryption failed: %1", reason ) );
210  if ( !mStderr.isEmpty() ) {
211  const QString details = i18n( "The following was received on stderr:\n%1", mStderr );
212  KMessageBox::detailedError( parent, msg, details, caption );
213  } else {
214  KMessageBox::error( parent, msg, caption );
215  }
216 }
217 
218 #include "chiasmusjob.moc"
Kleo::ChiasmusBackend::instance
static const ChiasmusBackend * instance()
Definition: chiasmusbackend.h:52
Kleo::SymCryptRunProcessBase
Definition: symcryptrunprocessbase.h:42
QWidget
Kleo::SpecialJob
An abstract base class for protocol-specific jobs.
Definition: specialjob.h:65
Kleo::ChiasmusJob::start
GpgME::Error start()
Definition: chiasmusjob.cpp:104
QString
QObject
Kleo::ChiasmusJob::slotCancel
void slotCancel()
Definition: chiasmusjob.cpp:187
Kleo::SymCryptRunProcessBase::Encrypt
Definition: symcryptrunprocessbase.h:46
Kleo::ChiasmusBackend::config
Kleo::CryptoConfig * config() const
Definition: chiasmusbackend.cpp:443
Kleo::ChiasmusJob::exec
GpgME::Error exec()
Definition: chiasmusjob.cpp:169
Kleo::ChiasmusJob::showErrorDialog
void showErrorDialog(QWidget *, const QString &) const
Definition: chiasmusjob.cpp:201
cryptoconfig.h
chiasmusjob.h
symcryptrunprocessbase.h
Kleo::ChiasmusJob::~ChiasmusJob
~ChiasmusJob()
Definition: chiasmusjob.cpp:67
chiasmusbackend.h
Kleo::ChiasmusJob::Mode
Mode
Definition: chiasmusjob.h:60
Kleo::SymCryptRunProcessBase::Decrypt
Definition: symcryptrunprocessbase.h:46
Kleo::SpecialJob::result
void result(const GpgME::Error &result, const QVariant &data)
Kleo::ChiasmusJob::ChiasmusJob
ChiasmusJob(Mode op)
Definition: chiasmusjob.cpp:56
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:57:48 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkleo

Skip menu "libkleo"
  • 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