• 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
  • commands
gnupgprocesscommand.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  commands/gnupgprocesscommand.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2008 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 "gnupgprocesscommand.h"
36 
37 #include "command_p.h"
38 
39 #include <utils/kdlogtextwidget.h>
40 
41 #include <KDebug>
42 #include <KProcess>
43 #include <KLocalizedString>
44 #include <KWindowSystem>
45 
46 #include <QString>
47 #include <QStringList>
48 #include <QByteArray>
49 #include <QTimer>
50 #include <QDialog>
51 #include <QDialogButtonBox>
52 #include <QPushButton>
53 #include <QVBoxLayout>
54 #include <QPointer>
55 
56 static const int PROCESS_TERMINATE_TIMEOUT = 5000; // milliseconds
57 
58 using namespace Kleo;
59 using namespace Kleo::Commands;
60 
61 namespace {
62 
63  class OutputDialog : public QDialog {
64  Q_OBJECT
65  public:
66  explicit OutputDialog( QWidget * parent=0 )
67  : QDialog( parent ),
68  vlay( this ),
69  logTextWidget( this ),
70  buttonBox( QDialogButtonBox::Cancel|QDialogButtonBox::Close, Qt::Horizontal, this )
71  {
72  KDAB_SET_OBJECT_NAME( vlay );
73  KDAB_SET_OBJECT_NAME( logTextWidget );
74  KDAB_SET_OBJECT_NAME( buttonBox );
75 
76  logTextWidget.setMinimumVisibleLines( 20 );
77  logTextWidget.setMinimumVisibleColumns( 80 );
78 
79  vlay.addWidget( &logTextWidget, 1 );
80  vlay.addWidget( &buttonBox );
81 
82  connect( closeButton(), SIGNAL(clicked()), this, SLOT(close()) );
83  connect( cancelButton(), SIGNAL(clicked()), this, SLOT(slotCancelClicked()) );
84  }
85 
86  Q_SIGNALS:
87  void cancelRequested();
88 
89  public Q_SLOTS:
90  void message( const QString & s ) {
91  logTextWidget.message( s );
92  }
93  void setComplete( bool complete ) {
94  cancelButton()->setVisible( !complete );
95  }
96 
97  private Q_SLOTS:
98  void slotCancelClicked() {
99  cancelButton()->hide();
100  emit cancelRequested();
101  }
102 
103  private:
104  QAbstractButton * closeButton() const { return buttonBox.button( QDialogButtonBox::Close ); }
105  QAbstractButton * cancelButton() const { return buttonBox.button( QDialogButtonBox::Cancel ); }
106 
107  private:
108  QVBoxLayout vlay;
109  KDLogTextWidget logTextWidget;
110  QDialogButtonBox buttonBox;
111  };
112 
113 }
114 
115 class GnuPGProcessCommand::Private : Command::Private {
116  friend class ::Kleo::Commands::GnuPGProcessCommand;
117  GnuPGProcessCommand * q_func() const { return static_cast<GnuPGProcessCommand*>( q ); }
118 public:
119  explicit Private( GnuPGProcessCommand * qq, KeyListController * c );
120  ~Private();
121 
122 private:
123  void init();
124  void ensureDialogCreated() {
125  if ( !showsOutputWindow )
126  return;
127  if ( !dialog ) {
128  dialog = new OutputDialog;
129  dialog->setAttribute( Qt::WA_DeleteOnClose );
130  applyWindowID( dialog );
131  connect( dialog, SIGNAL(cancelRequested()), q, SLOT(cancel()) );
132  dialog->setWindowTitle( i18n("Subprocess Diagnostics") );
133  }
134  }
135  void ensureDialogVisible() {
136  if ( !showsOutputWindow )
137  return;
138  ensureDialogCreated();
139  if ( dialog->isVisible() )
140  dialog->raise();
141  else
142  dialog->show();
143 #ifdef Q_OS_WIN
144  KWindowSystem::forceActiveWindow( dialog->winId() );
145 #endif
146  }
147  void message( const QString & msg ) {
148  if ( dialog )
149  dialog->message( msg );
150  else
151  kDebug() << msg;
152  }
153 
154 private:
155  void slotProcessFinished( int, QProcess::ExitStatus );
156  void slotProcessReadyReadStandardError();
157 
158 private:
159  KProcess process;
160  QPointer<OutputDialog> dialog;
161  QStringList arguments;
162  QByteArray errorBuffer;
163  bool ignoresSuccessOrFailure;
164  bool showsOutputWindow;
165  bool canceled;
166 };
167 
168 GnuPGProcessCommand::Private * GnuPGProcessCommand::d_func() { return static_cast<Private*>( d.get() ); }
169 const GnuPGProcessCommand::Private * GnuPGProcessCommand::d_func() const { return static_cast<const Private*>( d.get() ); }
170 
171 #define d d_func()
172 #define q q_func()
173 
174 GnuPGProcessCommand::Private::Private( GnuPGProcessCommand * qq, KeyListController * c )
175  : Command::Private( qq, c ),
176  process(),
177  dialog(),
178  errorBuffer(),
179  ignoresSuccessOrFailure( false ),
180  showsOutputWindow( false ),
181  canceled( false )
182 {
183  process.setOutputChannelMode( KProcess::OnlyStderrChannel );
184  process.setReadChannel( KProcess::StandardError );
185 }
186 
187 GnuPGProcessCommand::Private::~Private() {}
188 
189 GnuPGProcessCommand::GnuPGProcessCommand( KeyListController * c )
190  : Command( new Private( this, c ) )
191 {
192  d->init();
193 }
194 
195 GnuPGProcessCommand::GnuPGProcessCommand( QAbstractItemView * v, KeyListController * c )
196  : Command( v, new Private( this, c ) )
197 {
198  d->init();
199 }
200 
201 GnuPGProcessCommand::GnuPGProcessCommand( const GpgME::Key & key )
202  : Command( key, new Private( this, 0 ) )
203 {
204  d->init();
205 }
206 
207 void GnuPGProcessCommand::Private::init() {
208  connect( &process, SIGNAL(finished(int,QProcess::ExitStatus)),
209  q, SLOT(slotProcessFinished(int,QProcess::ExitStatus)) );
210  connect( &process, SIGNAL(readyReadStandardError()),
211  q, SLOT(slotProcessReadyReadStandardError()) );
212 }
213 
214 GnuPGProcessCommand::~GnuPGProcessCommand() {}
215 
216 QDialog * GnuPGProcessCommand::dialog() const {
217  return d->dialog;
218 }
219 
220 bool GnuPGProcessCommand::preStartHook( QWidget * ) const {
221  return true;
222 }
223 
224 void GnuPGProcessCommand::postSuccessHook( QWidget * ) {
225 
226 }
227 
228 void GnuPGProcessCommand::doStart() {
229 
230  if ( !preStartHook( d->parentWidgetOrView() ) ) {
231  d->finished();
232  return;
233  }
234 
235  d->arguments = arguments();
236 
237  d->process << d->arguments;
238 
239  d->process.start();
240 
241  if ( !d->process.waitForStarted() ) {
242  d->error( i18n( "Unable to start process %1. "
243  "Please check your installation.", d->arguments[0] ),
244  errorCaption() );
245  d->finished();
246  } else {
247  d->ensureDialogVisible();
248  d->message( i18n("Starting %1...", d->arguments.join(QLatin1String(" ")) ) );
249  }
250 }
251 
252 void GnuPGProcessCommand::doCancel() {
253  d->canceled = true;
254  if ( d->process.state() != QProcess::NotRunning ) {
255  d->process.terminate();
256  QTimer::singleShot( PROCESS_TERMINATE_TIMEOUT, &d->process, SLOT(kill()) );
257  }
258 }
259 
260 void GnuPGProcessCommand::Private::slotProcessFinished( int code, QProcess::ExitStatus status ) {
261  if ( !canceled )
262  if ( status == QProcess::CrashExit ) {
263  error( q->crashExitMessage( arguments ), q->errorCaption() );
264  } else if ( ignoresSuccessOrFailure ) {
265  if ( dialog ) {
266  message( i18n("Process finished") );
267  } else {
268  ;
269  }
270  } else if ( code ) {
271  error( q->errorExitMessage( arguments ), q->errorCaption() );
272  } else {
273  q->postSuccessHook( parentWidgetOrView() );
274  const QString successMessage = q->successMessage( arguments );
275  if ( dialog ) {
276  message( successMessage );
277  } else {
278  information( successMessage, q->successCaption() );
279  }
280  }
281  if ( dialog )
282  dialog->setComplete( true );
283  finished();
284 }
285 
286 void GnuPGProcessCommand::Private::slotProcessReadyReadStandardError() {
287  while ( process.canReadLine() ) {
288  QByteArray ba = process.readLine();
289  errorBuffer += ba;
290  while ( ba.endsWith('\n') || ba.endsWith('\r') )
291  ba.chop(1);
292  message( QString::fromLocal8Bit( ba.constData(), ba.size() ) );
293  }
294 }
295 
296 QString GnuPGProcessCommand::errorString() const {
297  return QString::fromLocal8Bit( d->errorBuffer );
298 }
299 
300 void GnuPGProcessCommand::setIgnoresSuccessOrFailure( bool ignores ) {
301  d->ignoresSuccessOrFailure = ignores;
302 }
303 
304 bool GnuPGProcessCommand::ignoresSuccessOrFailure() const {
305  return d->ignoresSuccessOrFailure;
306 }
307 
308 void GnuPGProcessCommand::setShowsOutputWindow( bool show ) {
309  if ( show == d->showsOutputWindow )
310  return;
311  d->showsOutputWindow = show;
312  if ( show ) {
313  d->ensureDialogCreated();
314  } else {
315  if ( d->dialog )
316  d->dialog->deleteLater();
317  d->dialog = 0;
318  }
319 }
320 
321 bool GnuPGProcessCommand::showsOutputWindow() const {
322  return d->showsOutputWindow;
323 }
324 
325 #undef d
326 #undef q
327 
328 #include "moc_gnupgprocesscommand.cpp"
329 #include "gnupgprocesscommand.moc"
gnupgprocesscommand.h
Kleo::Commands::GnuPGProcessCommand::GnuPGProcessCommand
GnuPGProcessCommand(QAbstractItemView *view, KeyListController *parent)
Definition: gnupgprocesscommand.cpp:195
QWidget
Kleo::Commands::GnuPGProcessCommand::errorString
QString errorString() const
Definition: gnupgprocesscommand.cpp:296
QAbstractItemView
Kleo::Command::Private
Definition: commands/command_p.h:52
QByteArray
KDLogTextWidget
A high-speed text display widget.
Definition: kdlogtextwidget.h:34
Kleo::KeyListController
Definition: keylistcontroller.h:55
QByteArray::chop
void chop(int n)
QPointer< OutputDialog >
QWidget::isVisible
bool isVisible() const
Kleo::Commands::GnuPGProcessCommand::dialog
QDialog * dialog() const
Definition: gnupgprocesscommand.cpp:216
QWidget::setAttribute
void setAttribute(Qt::WidgetAttribute attribute, bool on)
Kleo::Commands::GnuPGProcessCommand::ignoresSuccessOrFailure
bool ignoresSuccessOrFailure() const
Definition: gnupgprocesscommand.cpp:304
Kleo::Commands::GnuPGProcessCommand::setIgnoresSuccessOrFailure
void setIgnoresSuccessOrFailure(bool ignore)
Definition: gnupgprocesscommand.cpp:300
Kleo::Command::d
kdtools::pimpl_ptr< Private > d
Definition: commands/command.h:129
status
VerifyChecksumsDialog::Status status
Definition: verifychecksumscontroller.cpp:511
Kleo::Commands::GnuPGProcessCommand
Definition: gnupgprocesscommand.h:44
QString::fromLocal8Bit
QString fromLocal8Bit(const char *str, int size)
q
#define q
Definition: gnupgprocesscommand.cpp:172
process
static QString process(const Dir &dir, bool *fatal)
Definition: createchecksumscontroller.cpp:557
QWidget::raise
void raise()
QByteArray::constData
const char * constData() const
KDAB_SET_OBJECT_NAME
#define KDAB_SET_OBJECT_NAME(x)
Definition: kdtoolsglobal.h:66
QVBoxLayout
kdtools::pimpl_ptr::get
T * get()
Definition: pimpl_ptr.h:39
QWidget::winId
WId winId() const
QString
Kleo::Command::cancel
void cancel()
Definition: commands/command.cpp:186
QStringList
Kleo::Commands::GnuPGProcessCommand::showsOutputWindow
bool showsOutputWindow() const
Definition: gnupgprocesscommand.cpp:321
command_p.h
Kleo::Commands::GnuPGProcessCommand::setShowsOutputWindow
void setShowsOutputWindow(bool show)
Definition: gnupgprocesscommand.cpp:308
Kleo::Command::canceled
void canceled()
QAbstractButton
kdlogtextwidget.h
QLatin1String
QDialogButtonBox
QWidget::setWindowTitle
void setWindowTitle(const QString &)
QDialog
QWidget::show
void show()
PROCESS_TERMINATE_TIMEOUT
static const int PROCESS_TERMINATE_TIMEOUT
Definition: gnupgprocesscommand.cpp:56
Kleo::Commands::GnuPGProcessCommand::~GnuPGProcessCommand
~GnuPGProcessCommand()
Definition: gnupgprocesscommand.cpp:214
QByteArray::size
int size() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Kleo::Command
Definition: commands/command.h:58
QByteArray::endsWith
bool endsWith(const QByteArray &ba) const
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