• 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
  • ui
messagebox.cpp
Go to the documentation of this file.
1 /*
2  messagebox.cpp
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  Copyright (c) 2004 Klarälvdalens 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
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 "messagebox.h"
34 #include "messagebox_p.h"
35 
36 #include "kleo/job.h"
37 
38 #include <gpgme++/signingresult.h>
39 #include <gpgme++/encryptionresult.h>
40 
41 
42 #ifndef KDEPIM_ONLY_KLEO
43 # include <kfiledialog.h>
44 #else
45 # include <QFileDialog>
46 #endif
47 
48 #include <kdialog.h>
49 #include <klocale.h>
50 #include <ksavefile.h>
51 #include <kguiitem.h>
52 #include <kdebug.h>
53 #include <ktextedit.h>
54 
55 #include <qtextstream.h>
56 
57 #include <gpg-error.h>
58 
59 using namespace Kleo;
60 using namespace Kleo::Private;
61 using namespace GpgME;
62 
63 namespace {
64 
65 #ifndef QT_NO_FILEDIALOG
66 static KGuiItem KGuiItem_save() {
67  return KGuiItem( i18n("&Save to Disk..."), QLatin1String("document-save-as") );
68 }
69 #endif
70 
71 #ifndef QT_NO_CLIPBOARD
72 static KGuiItem KGuiItem_copy() {
73  return KGuiItem( i18n("&Copy to Clipboard"), QLatin1String("edit-copy"), i18n("Copy Audit Log to Clipboard") );
74 }
75 #endif
76 
77 static KGuiItem KGuiItem_showAuditLog() {
78  return KGuiItem( i18n("&Show Audit Log") ); // "view_log"?
79 }
80 
81 } // anon namespace
82 
83 AuditLogViewer::AuditLogViewer( const QString & log, QWidget * parent, Qt::WindowFlags f )
84  : KDialog( parent, f ),
85  m_log( /* sic */ ),
86  m_textEdit( new KTextEdit( this ) )
87 {
88  setCaption( i18n("View GnuPG Audit Log") );
89  setButtons( Close
90 #ifndef QT_NO_FILEDIALOG
91  |User1
92 #endif
93 #ifndef QT_NO_CLIPBOARD
94  |User2
95 #endif
96  );
97  setDefaultButton( Close );
98 #ifndef QT_NO_FILEDIALOG
99  setButtonGuiItem( User1, KGuiItem_save() );
100 #endif
101 #ifndef QT_NO_CLIPBOARD
102  setButtonGuiItem( User2, KGuiItem_copy() );
103 #endif
104  showButtonSeparator( false );
105  setModal( false );
106  setMainWidget( m_textEdit );
107  m_textEdit->setObjectName( QLatin1String("m_textEdit") );
108  m_textEdit->setReadOnly( true );
109  setAuditLog( log );
110 
111 #ifndef QT_NO_FILEDIALOG
112  connect( this, SIGNAL(user1Clicked()), SLOT(slotUser1()) );
113 #endif
114 #ifndef QT_NO_CLIPBOARD
115  connect( this, SIGNAL(user2Clicked()), SLOT(slotUser2()) );
116 #endif
117  readConfig();
118 }
119 
120 AuditLogViewer::~AuditLogViewer()
121 {
122  writeConfig();
123 }
124 
125 void AuditLogViewer::setAuditLog( const QString & log ) {
126  if ( log == m_log )
127  return;
128  m_log = log;
129  m_textEdit->setHtml( QLatin1String("<qt>") + log + QLatin1String("</qt>") );
130 }
131 
132 #ifndef QT_NO_FILEDIALOG
133 void AuditLogViewer::slotUser1() {
134 #ifndef KDEPIM_ONLY_KLEO
135  const QString fileName = KFileDialog::getSaveFileName( QString(), QString(),
136  this, i18n("Choose File to Save GnuPG Audit Log to") );
137 #else
138  const QString fileName = QFileDialog::getSaveFileName( this, i18n("Choose File to Save GnuPG Audit Log to") );
139 #endif
140  if ( fileName.isEmpty() )
141  return;
142 
143  KSaveFile file( fileName );
144 
145  if ( file.open() ) {
146  QTextStream s( &file );
147  s << "<html><head>";
148  if ( !windowTitle().isEmpty() ) {
149  s << "\n<title>"
150  << Qt::escape( windowTitle() )
151  << "</title>\n";
152  }
153  s << "</head><body>\n"
154  << m_log
155  << "\n</body></html>" << endl;
156  s.flush();
157  file.finalize();
158  }
159 
160  if ( const int err = file.error() )
161  KMessageBox::error( this, i18n("Could not save to file \"%1\": %2",
162  file.fileName(), QString::fromLocal8Bit( strerror( err ) ) ),
163  i18n("File Save Error") );
164 }
165 #endif // QT_NO_FILEDIALOG
166 
167 #ifndef QT_NO_CLIPBOARD
168 void AuditLogViewer::slotUser2() {
169  m_textEdit->selectAll();
170  m_textEdit->copy();
171  m_textEdit->textCursor().clearSelection();
172 }
173 #endif // QT_NO_CLIPBOARD
174 
175 void AuditLogViewer::readConfig()
176 {
177  KConfigGroup group( KGlobal::config(), "AuditLogViewer" );
178  const QSize size = group.readEntry( "Size", QSize() );
179  if ( size.isValid() ) {
180  resize( size );
181  } else {
182  resize( 600, 400 );
183  }
184 }
185 
186 void AuditLogViewer::writeConfig()
187 {
188  KConfigGroup group( KGlobal::config(), "AuditLogViewer" );
189  group.writeEntry( "Size", size() );
190  group.sync();
191 }
192 
193 
194 // static
195 void MessageBox::auditLog( QWidget * parent, const Job * job, const QString & caption ) {
196 
197  if ( !job )
198  return;
199 
200  if ( !GpgME::hasFeature( AuditLogFeature ) || !job->isAuditLogSupported() ) {
201  KMessageBox::information( parent, i18n("Your system does not have support for GnuPG Audit Logs"),
202  i18n("System Error") );
203  return;
204  }
205 
206  const GpgME::Error err = job->auditLogError();
207 
208  if ( err && err.code() != GPG_ERR_NO_DATA ) {
209  KMessageBox::information( parent, i18n("An error occurred while trying to retrieve the GnuPG Audit Log:\n%1",
210  QString::fromLocal8Bit( err.asString() ) ),
211  i18n("GnuPG Audit Log Error") );
212  return;
213  }
214 
215  const QString log = job->auditLogAsHtml();
216 
217  if ( log.isEmpty() ) {
218  KMessageBox::information( parent, i18n("No GnuPG Audit Log available for this operation."),
219  i18n("No GnuPG Audit Log") );
220  return;
221  }
222 
223  auditLog( parent, log, caption );
224 }
225 
226 // static
227 void MessageBox::auditLog( QWidget * parent, const QString & log, const QString & caption ) {
228  AuditLogViewer * const alv = new AuditLogViewer( log, parent );
229  alv->setAttribute( Qt::WA_DeleteOnClose );
230  alv->setObjectName( QLatin1String("alv") );
231  alv->setCaption( caption );
232  alv->show();
233 }
234 
235 // static
236 void MessageBox::auditLog( QWidget * parent, const Job * job ) {
237  auditLog( parent, job, i18n("GnuPG Audit Log Viewer") );
238 }
239 
240 // static
241 void MessageBox::auditLog( QWidget * parent, const QString & log ) {
242  auditLog( parent, log, i18n("GnuPG Audit Log Viewer") );
243 }
244 
245 static QString to_information_string( const SigningResult & result ) {
246  return result.error()
247  ? i18n("Signing failed: %1", QString::fromLocal8Bit( result.error().asString() ) )
248  : i18n("Signing successful") ;
249 }
250 
251 static QString to_error_string( const SigningResult & result ) {
252  return to_information_string( result );
253 }
254 
255 static QString to_information_string( const EncryptionResult & result ) {
256  return result.error()
257  ? i18n("Encryption failed: %1", QString::fromLocal8Bit( result.error().asString() ) )
258  : i18n("Encryption successful") ;
259 }
260 
261 static QString to_error_string( const EncryptionResult & result ) {
262  return to_information_string( result );
263 }
264 
265 static QString to_information_string( const SigningResult & sresult, const EncryptionResult & eresult ) {
266  return to_information_string( sresult ) + QLatin1Char('\n') + to_information_string( eresult );
267 }
268 
269 static QString to_error_string( const SigningResult & sresult, const EncryptionResult & eresult ) {
270  return to_information_string( sresult, eresult );
271 }
272 
273 // static
274 void MessageBox::information( QWidget * parent, const SigningResult & result, const Job * job, KMessageBox::Options options ) {
275  information( parent, result, job, i18n("Signing Result"), options );
276 }
277 
278 // static
279 void MessageBox::information( QWidget * parent, const SigningResult & result, const Job * job, const QString & caption, KMessageBox::Options options ) {
280  make( parent, QMessageBox::Information, to_information_string( result ), job, caption, options );
281 }
282 
283 // static
284 void MessageBox::error( QWidget * parent, const SigningResult & result, const Job * job, KMessageBox::Options options ) {
285  error( parent, result, job, i18n("Signing Error"), options );
286 }
287 
288 // static
289 void MessageBox::error( QWidget * parent, const SigningResult & result, const Job * job, const QString & caption, KMessageBox::Options options ) {
290  make( parent, QMessageBox::Critical, to_error_string( result ), job, caption, options );
291 }
292 
293 // static
294 void MessageBox::information( QWidget * parent, const EncryptionResult & result, const Job * job, KMessageBox::Options options ) {
295  information( parent, result, job, i18n("Encryption Result"), options );
296 }
297 
298 // static
299 void MessageBox::information( QWidget * parent, const EncryptionResult & result, const Job * job, const QString & caption, KMessageBox::Options options ) {
300  make( parent, QMessageBox::Information, to_information_string( result ), job, caption, options );
301 }
302 
303 // static
304 void MessageBox::error( QWidget * parent, const EncryptionResult & result, const Job * job, KMessageBox::Options options ) {
305  error( parent, result, job, i18n("Encryption Error"), options );
306 }
307 
308 // static
309 void MessageBox::error( QWidget * parent, const EncryptionResult & result, const Job * job, const QString & caption, KMessageBox::Options options ) {
310  make( parent, QMessageBox::Critical, to_error_string( result ), job, caption, options );
311 }
312 
313 // static
314 void MessageBox::information( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, KMessageBox::Options options ) {
315  information( parent, sresult, eresult, job, i18n("Encryption Result"), options );
316 }
317 
318 // static
319 void MessageBox::information( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, const QString & caption, KMessageBox::Options options ) {
320  make( parent, QMessageBox::Information, to_information_string( sresult, eresult ), job, caption, options );
321 }
322 
323 // static
324 void MessageBox::error( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, KMessageBox::Options options ) {
325  error( parent, sresult, eresult, job, i18n("Encryption Error"), options );
326 }
327 
328 // static
329 void MessageBox::error( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, const QString & caption, KMessageBox::Options options ) {
330  make( parent, QMessageBox::Critical, to_error_string( sresult, eresult ), job, caption, options );
331 }
332 
333 // static
334 bool MessageBox::showAuditLogButton( const Kleo::Job * job ) {
335  if ( !job ) {
336  kDebug(5150) << "not showing audit log button (no job instance)";
337  return false;
338  }
339  if ( !GpgME::hasFeature( GpgME::AuditLogFeature ) ) {
340  kDebug(5150) << "not showing audit log button (gpgme too old)";
341  return false;
342  }
343  if ( !job->isAuditLogSupported() ) {
344  kDebug(5150) << "not showing audit log button (not supported)";
345  return false;
346  }
347  if ( job->auditLogError().code() == GPG_ERR_NO_DATA ) {
348  kDebug(5150) << "not showing audit log button (GPG_ERR_NO_DATA)";
349  return false;
350  }
351  if ( !job->auditLogError() && job->auditLogAsHtml().isEmpty() ) {
352  kDebug(5150) << "not showing audit log button (success, but result empty)";
353  return false;
354  }
355  return true;
356 }
357 
358 
359 // static
360 void MessageBox::make( QWidget * parent, QMessageBox::Icon icon, const QString & text, const Job * job, const QString & caption, KMessageBox::Options options ) {
361  KDialog * dialog = new KDialog( parent );
362  dialog->setCaption( caption );
363  dialog->setButtons( showAuditLogButton( job ) ? ( KDialog::Yes | KDialog::No ) : KDialog::Yes );
364  dialog->setDefaultButton( KDialog::Yes );
365  dialog->setEscapeButton( KDialog::Yes );
366  dialog->setObjectName( QLatin1String("error") );
367  dialog->setModal( true );
368  dialog->showButtonSeparator( true );
369  dialog->setButtonGuiItem( KDialog::Yes, KStandardGuiItem::ok() );
370  if ( GpgME::hasFeature( GpgME::AuditLogFeature ) )
371  dialog->setButtonGuiItem( KDialog::No, KGuiItem_showAuditLog() );
372 
373  if ( options & KMessageBox::PlainCaption )
374  dialog->setPlainCaption( caption );
375 
376  if ( KDialog::No == KMessageBox::createKMessageBox( dialog, icon, text, QStringList(), QString::null, 0, options ) )
377  auditLog( 0, job );
378 }
379 
380 #include "moc_messagebox_p.cpp"
messagebox_p.h
QWidget
Kleo::MessageBox::error
static void error(QWidget *parent, const GpgME::SigningResult &result, const Kleo::Job *job, const QString &caption, KMessageBox::Options options=KMessageBox::Notify)
KDialog
QString
Kleo::MessageBox::showAuditLogButton
static bool showAuditLogButton(const Kleo::Job *job)
Definition: messagebox.cpp:334
Kleo::Private::AuditLogViewer::setAuditLog
void setAuditLog(const QString &log)
Definition: messagebox.cpp:125
Kleo::Private::AuditLogViewer::AuditLogViewer
AuditLogViewer(const QString &log, QWidget *parent=0, Qt::WindowFlags f=0)
Definition: messagebox.cpp:83
Kleo::MessageBox::information
static void information(QWidget *parent, const GpgME::SigningResult &result, const Kleo::Job *job, const QString &caption, KMessageBox::Options options=KMessageBox::Notify)
to_information_string
static QString to_information_string(const SigningResult &result)
Definition: messagebox.cpp:245
messagebox.h
Kleo::Private::AuditLogViewer
Definition: messagebox_p.h:44
Kleo::Job
An abstract base class for asynchronous crypto operations.
Definition: job.h:66
job.h
Kleo::Job::auditLogAsHtml
virtual QString auditLogAsHtml() const
Definition: job.cpp:81
Kleo::Job::isAuditLogSupported
bool isAuditLogSupported() const
Definition: job.cpp:91
Kleo::Private::AuditLogViewer::~AuditLogViewer
~AuditLogViewer()
Definition: messagebox.cpp:120
Kleo::Job::auditLogError
virtual GpgME::Error auditLogError() const
Definition: job.cpp:86
to_error_string
static QString to_error_string(const SigningResult &result)
Definition: messagebox.cpp:251
Kleo::MessageBox::auditLog
static void auditLog(QWidget *parent, const Kleo::Job *job, const QString &caption)
Definition: messagebox.cpp:195
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