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

libkleo

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