• 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
dumpcertificatecommand.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  commands/dumpcertificatecommand.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 "dumpcertificatecommand.h"
36 
37 #include "command_p.h"
38 
39 #include <utils/kdlogtextwidget.h>
40 #include <utils/gnupg-helper.h>
41 
42 #include <gpgme++/key.h>
43 
44 #include <KProcess>
45 #include <KMessageBox>
46 #include <KLocale>
47 #include <KPushButton>
48 #include <KStandardGuiItem>
49 #include <KGlobalSettings>
50 
51 #include <QString>
52 #include <QByteArray>
53 #include <QTimer>
54 #include <QLayout>
55 #include <QPointer>
56 
57 
58 static const int PROCESS_TERMINATE_TIMEOUT = 5000; // milliseconds
59 
60 namespace {
61  class DumpCertificateDialog : public QDialog {
62  Q_OBJECT
63  public:
64  explicit DumpCertificateDialog( QWidget * parent=0 )
65  : QDialog( parent ), ui( this )
66  {
67 
68  }
69 
70  Q_SIGNALS:
71  void updateRequested();
72 
73  public Q_SLOTS:
74  void append( const QString & line ) {
75  ui.logTextWidget.message( line );
76  }
77  void clear() {
78  ui.logTextWidget.clear();
79  }
80 
81  private:
82  struct Ui {
83  KDLogTextWidget logTextWidget;
84  KPushButton updateButton, closeButton;
85  QVBoxLayout vlay;
86  QHBoxLayout hlay;
87 
88  explicit Ui( DumpCertificateDialog * q )
89  : logTextWidget( q ),
90  updateButton( i18nc("@action:button Update the log text widget", "&Update"), q ),
91  closeButton( KStandardGuiItem::close(), q ),
92  vlay( q ),
93  hlay()
94  {
95  KDAB_SET_OBJECT_NAME( logTextWidget );
96  KDAB_SET_OBJECT_NAME( updateButton );
97  KDAB_SET_OBJECT_NAME( closeButton );
98  KDAB_SET_OBJECT_NAME( vlay );
99  KDAB_SET_OBJECT_NAME( hlay );
100 
101  logTextWidget.setFont( KGlobalSettings::fixedFont() );
102  logTextWidget.setMinimumVisibleLines( 25 );
103  logTextWidget.setMinimumVisibleColumns( 80 );
104 
105  vlay.addWidget( &logTextWidget, 1 );
106  vlay.addLayout( &hlay );
107 
108  hlay.addWidget( &updateButton );
109  hlay.addStretch( 1 );
110  hlay.addWidget( &closeButton );
111 
112  connect( &updateButton, SIGNAL(clicked()),
113  q, SIGNAL(updateRequested()) );
114  connect( &closeButton, SIGNAL(clicked()),
115  q, SLOT(close()) );
116  }
117  } ui;
118  };
119 }
120 
121 using namespace Kleo;
122 using namespace Kleo::Commands;
123 
124 static QByteArray chomped( QByteArray ba ) {
125  while ( ba.endsWith( '\n' ) || ba.endsWith( '\r' ) )
126  ba.chop( 1 );
127  return ba;
128 }
129 
130 class DumpCertificateCommand::Private : Command::Private {
131  friend class ::Kleo::Commands::DumpCertificateCommand;
132  DumpCertificateCommand * q_func() const { return static_cast<DumpCertificateCommand*>( q ); }
133 public:
134  explicit Private( DumpCertificateCommand * qq, KeyListController * c );
135  ~Private();
136 
137  QString errorString() const {
138  return QString::fromLocal8Bit( errorBuffer );
139  }
140 
141 private:
142  void init();
143  void refreshView();
144 
145 private:
146  void slotProcessFinished( int, QProcess::ExitStatus );
147 
148  void slotProcessReadyReadStandardOutput() {
149  while ( process.canReadLine() ) {
150  const QString line = QString::fromUtf8( chomped( process.readLine() ) );
151  if ( dialog )
152  dialog->append( line );
153  outputBuffer.push_back( line );
154  }
155  }
156 
157  void slotProcessReadyReadStandardError() {
158  errorBuffer += process.readAllStandardError();
159  }
160 
161  void slotUpdateRequested() {
162  if ( process.state() == QProcess::NotRunning )
163  refreshView();
164  }
165 
166  void slotDialogDestroyed() {
167  dialog = 0;
168  if ( process.state() != QProcess::NotRunning )
169  q->cancel();
170  else
171  finished();
172  }
173 
174 private:
175  QPointer<DumpCertificateDialog> dialog;
176  KProcess process;
177  QByteArray errorBuffer;
178  QStringList outputBuffer;
179  bool useDialog;
180  bool canceled;
181 };
182 
183 DumpCertificateCommand::Private * DumpCertificateCommand::d_func() { return static_cast<Private*>( d.get() ); }
184 const DumpCertificateCommand::Private * DumpCertificateCommand::d_func() const { return static_cast<const Private*>( d.get() ); }
185 
186 #define d d_func()
187 #define q q_func()
188 
189 DumpCertificateCommand::Private::Private( DumpCertificateCommand * qq, KeyListController * c )
190  : Command::Private( qq, c ),
191  process(),
192  errorBuffer(),
193  outputBuffer(),
194  useDialog( true ),
195  canceled( false )
196 {
197  process.setOutputChannelMode( KProcess::SeparateChannels );
198  process.setReadChannel( KProcess::StandardOutput );
199 }
200 
201 DumpCertificateCommand::Private::~Private() {
202  if ( dialog && !dialog->isVisible() )
203  delete dialog;
204 }
205 
206 DumpCertificateCommand::DumpCertificateCommand( KeyListController * c )
207  : Command( new Private( this, c ) )
208 {
209  d->init();
210 }
211 
212 DumpCertificateCommand::DumpCertificateCommand( QAbstractItemView * v, KeyListController * c )
213  : Command( v, new Private( this, c ) )
214 {
215  d->init();
216 }
217 
218 DumpCertificateCommand::DumpCertificateCommand( const GpgME::Key & k )
219  : Command( k, new Private( this, 0 ) )
220 {
221  d->init();
222 }
223 
224 void DumpCertificateCommand::Private::init() {
225  connect( &process, SIGNAL(finished(int,QProcess::ExitStatus)),
226  q, SLOT(slotProcessFinished(int,QProcess::ExitStatus)) );
227  connect( &process, SIGNAL(readyReadStandardError()),
228  q, SLOT(slotProcessReadyReadStandardError()) );
229  connect( &process, SIGNAL(readyReadStandardOutput()),
230  q, SLOT(slotProcessReadyReadStandardOutput()) );
231  if ( !key().isNull() )
232  process << gpgSmPath() << QLatin1String("--dump-cert") << QLatin1String(key().primaryFingerprint());
233 }
234 
235 DumpCertificateCommand::~DumpCertificateCommand() {}
236 
237 void DumpCertificateCommand::setUseDialog( bool use ) {
238  d->useDialog = use;
239 }
240 
241 bool DumpCertificateCommand::useDialog() const {
242  return d->useDialog;
243 }
244 
245 QStringList DumpCertificateCommand::output() const {
246  return d->outputBuffer;
247 }
248 
249 void DumpCertificateCommand::doStart() {
250 
251  const std::vector<GpgME::Key> keys = d->keys();
252  if ( keys.size() != 1 || keys.front().protocol() != GpgME::CMS ) {
253  d->finished();
254  return;
255  }
256 
257  if ( d->useDialog ) {
258  d->dialog = new DumpCertificateDialog;
259  d->dialog->setAttribute( Qt::WA_DeleteOnClose );
260  d->dialog->setWindowTitle( i18n("Certificate Dump") );
261 
262  connect( d->dialog, SIGNAL(updateRequested()),
263  this, SLOT(slotUpdateRequested()) );
264  connect( d->dialog, SIGNAL(destroyed()),
265  this, SLOT(slotDialogDestroyed()) );
266  }
267 
268  d->refreshView();
269 }
270 
271 void DumpCertificateCommand::Private::refreshView() {
272 
273  if ( dialog )
274  dialog->clear();
275  errorBuffer.clear();
276  outputBuffer.clear();
277 
278  process.start();
279 
280  if ( process.waitForStarted() ) {
281  if ( dialog )
282  dialog->show();
283  } else {
284  KMessageBox::error( dialog ? static_cast<QWidget*>( dialog ) : parentWidgetOrView(),
285  i18n( "Unable to start process gpgsm. "
286  "Please check your installation." ),
287  i18n( "Dump Certificate Error" ) );
288  finished();
289  }
290 }
291 
292 void DumpCertificateCommand::doCancel() {
293  d->canceled = true;
294  if ( d->process.state() != QProcess::NotRunning ) {
295  d->process.terminate();
296  QTimer::singleShot( PROCESS_TERMINATE_TIMEOUT, &d->process, SLOT(kill()) );
297  }
298  if ( d->dialog )
299  d->dialog->close();
300  d->dialog = 0;
301 }
302 
303 void DumpCertificateCommand::Private::slotProcessFinished( int code, QProcess::ExitStatus status ) {
304  if ( !canceled ) {
305  if ( status == QProcess::CrashExit )
306  KMessageBox::error( dialog,
307  i18n( "The GpgSM process that tried to dump the certificate "
308  "ended prematurely because of an unexpected error. "
309  "Please check the output of gpgsm --dump-cert %1 for details.",
310  QLatin1String(key().primaryFingerprint()) ),
311  i18n( "Dump Certificate Error" ) );
312  else if ( code )
313  KMessageBox::error( dialog,
314  i18n( "An error occurred while trying to dump the certificate. "
315  "The output from GpgSM was:\n%1", errorString() ),
316  i18n( "Dump Certificate Error" ) );
317  }
318  if ( !useDialog )
319  slotDialogDestroyed();
320 }
321 
322 #undef d
323 #undef q
324 
325 #include "moc_dumpcertificatecommand.cpp"
326 #include "dumpcertificatecommand.moc"
Kleo::Command::finished
void finished()
Kleo::Commands::DumpCertificateCommand::DumpCertificateCommand
DumpCertificateCommand(QAbstractItemView *view, KeyListController *parent)
Definition: dumpcertificatecommand.cpp:212
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::DumpCertificateCommand::useDialog
bool useDialog() const
Definition: dumpcertificatecommand.cpp:241
Kleo::Command::d
kdtools::pimpl_ptr< Private > d
Definition: commands/command.h:129
status
VerifyChecksumsDialog::Status status
Definition: verifychecksumscontroller.cpp:512
Kleo::Commands::DumpCertificateCommand::setUseDialog
void setUseDialog(bool on)
Definition: dumpcertificatecommand.cpp:237
PROCESS_TERMINATE_TIMEOUT
static const int PROCESS_TERMINATE_TIMEOUT
Definition: dumpcertificatecommand.cpp:58
process
static QString process(const Dir &dir, bool *fatal)
Definition: createchecksumscontroller.cpp:532
Kleo::gpgSmPath
QString gpgSmPath()
Definition: gnupg-helper.cpp:82
KDAB_SET_OBJECT_NAME
#define KDAB_SET_OBJECT_NAME(x)
Definition: kdtoolsglobal.h:66
q
#define q
Definition: dumpcertificatecommand.cpp:187
kdtools::pimpl_ptr::get
T * get()
Definition: pimpl_ptr.h:39
Kleo::Class::CMS
Definition: classify.h:48
Kleo::Commands::DumpCertificateCommand::~DumpCertificateCommand
~DumpCertificateCommand()
Definition: dumpcertificatecommand.cpp:235
Kleo::Commands::DumpCertificateCommand::output
QStringList output() const
Definition: dumpcertificatecommand.cpp:245
gnupg-helper.h
command_p.h
Kleo::Command::canceled
void canceled()
kdlogtextwidget.h
dumpcertificatecommand.h
Kleo::Commands::DumpCertificateCommand
Definition: dumpcertificatecommand.h:41
chomped
static QByteArray chomped(QByteArray ba)
Definition: dumpcertificatecommand.cpp:124
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