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