docs update, needs proofreading
[kdepim.git] / libkleo / ui / messagebox.cpp
blob9cea2b6bfc67387365c051b7130426b2f6613833
1 /*
2 messagebox.cpp
4 This file is part of libkleopatra, the KDE keymanagement library
5 Copyright (c) 2004 Klarälvdalens Datakonsult AB
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.
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.
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
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.
33 #include "messagebox.h"
34 #include "messagebox_p.h"
36 #include "kleo/job.h"
38 #include <gpgme++/signingresult.h>
39 #include <gpgme++/encryptionresult.h>
42 #ifndef KDEPIM_ONLY_KLEO
43 # include <kfiledialog.h>
44 #else
45 # include <QFileDialog>
46 #endif
48 #include <kdialog.h>
49 #include <klocale.h>
50 #include <ksavefile.h>
51 #include <kguiitem.h>
52 #include <kdebug.h>
54 #include <qtextedit.h>
55 #include <qtextstream.h>
57 #include <gpg-error.h>
59 using namespace Kleo;
60 using namespace Kleo::Private;
61 using namespace GpgME;
63 namespace {
65 static KGuiItem KGuiItem_save() {
66 return KGuiItem( i18n("&Save to Disk..."), "document-save-as" );
69 static KGuiItem KGuiItem_copy() {
70 return KGuiItem( i18n("&Copy to Clipboard"), "edit-copy", i18n("Copy Audit Log to Clipboard") );
73 static KGuiItem KGuiItem_showAuditLog() {
74 return KGuiItem( i18n("&Show Audit Log") ); // "view_log"?
77 } // anon namespace
79 AuditLogViewer::AuditLogViewer( const QString & log, QWidget * parent, Qt::WindowFlags f )
80 : KDialog( parent, f ),
81 m_textEdit( new QTextEdit( this ) )
83 setCaption( i18n("View GnuPG Audit Log") );
84 setButtons( Close|User1|User2 );
85 setDefaultButton( Close );
86 setButtonGuiItem( User1, KGuiItem_save() );
87 setButtonGuiItem( User2, KGuiItem_copy() );
88 showButtonSeparator( false );
89 setModal( false );
90 setMainWidget( m_textEdit );
91 m_textEdit->setObjectName( "m_textEdit" );
92 m_textEdit->setReadOnly( true );
93 setAuditLog( log );
95 connect( this, SIGNAL(user1Clicked()), SLOT(slotUser1()) );
96 connect( this, SIGNAL(user2Clicked()), SLOT(slotUser2()) );
99 AuditLogViewer::~AuditLogViewer() {}
101 void AuditLogViewer::setAuditLog( const QString & log ) {
102 m_textEdit->setHtml( log );
105 void AuditLogViewer::slotUser1() {
106 #ifndef KDEPIM_ONLY_KLEO
107 const QString fileName = KFileDialog::getSaveFileName( QString(), QString(),
108 this, i18n("Choose File to Save GnuPG Audit Log to") );
109 #else
110 const QString fileName = QFileDialog::getSaveFileName( this, i18n("Choose File to Save GnuPG Audit Log to") );
111 #endif
112 if ( fileName.isEmpty() )
113 return;
115 KSaveFile file( fileName );
117 if ( file.open() ) {
118 QTextStream s( &file );
119 s << m_textEdit->toPlainText() << endl;
120 s.flush();
121 file.finalize();
124 if ( const int err = file.error() )
125 KMessageBox::error( this, i18n("Could not save to file \"%1\": %2",
126 file.fileName(), QString::fromLocal8Bit( strerror( err ) ) ),
127 i18n("File Save Error") );
130 void AuditLogViewer::slotUser2() {
131 m_textEdit->selectAll();
132 m_textEdit->copy();
133 m_textEdit->textCursor().clearSelection();
136 // static
137 void MessageBox::auditLog( QWidget * parent, const Job * job, const QString & caption ) {
139 if ( !job )
140 return;
142 if ( !GpgME::hasFeature( AuditLogFeature ) || !job->isAuditLogSupported() ) {
143 KMessageBox::information( parent, i18n("Your system does not have support for GnuPG Audit Logs"),
144 i18n("System Error") );
145 return;
148 const GpgME::Error err = job->auditLogError();
150 if ( err.code() != GPG_ERR_NO_DATA ) {
151 KMessageBox::information( parent, i18n("An error occurred while trying to retrieve the GnuPG Audit Log:\n%1",
152 QString::fromLocal8Bit( err.asString() ) ),
153 i18n("GnuPG Audit Log Error") );
154 return;
157 const QString log = job->auditLogAsHtml();
159 if ( log.isEmpty() ) {
160 KMessageBox::information( parent, i18n("No GnuPG Audit Log available for this operation."),
161 i18n("No GnuPG Audit Log") );
162 return;
165 auditLog( parent, log, caption );
168 // static
169 void MessageBox::auditLog( QWidget * parent, const QString & log, const QString & caption ) {
170 AuditLogViewer * const alv = new AuditLogViewer( "<qt>" + log + "</qt>", parent, Qt::WDestructiveClose );
171 alv->setObjectName( "alv" );
172 alv->setCaption( caption );
173 alv->show();
176 // static
177 void MessageBox::auditLog( QWidget * parent, const Job * job ) {
178 auditLog( parent, job, i18n("GnuPG Audit Log Viewer") );
181 // static
182 void MessageBox::auditLog( QWidget * parent, const QString & log ) {
183 auditLog( parent, log, i18n("GnuPG Audit Log Viewer") );
186 static QString to_information_string( const SigningResult & result ) {
187 return result.error()
188 ? i18n("Signing failed: %1", QString::fromLocal8Bit( result.error().asString() ) )
189 : i18n("Signing successful") ;
192 static QString to_error_string( const SigningResult & result ) {
193 return to_information_string( result );
196 static QString to_information_string( const EncryptionResult & result ) {
197 return result.error()
198 ? i18n("Encryption failed: %1", QString::fromLocal8Bit( result.error().asString() ) )
199 : i18n("Encryption successful") ;
202 static QString to_error_string( const EncryptionResult & result ) {
203 return to_information_string( result );
206 static QString to_information_string( const SigningResult & sresult, const EncryptionResult & eresult ) {
207 return to_information_string( sresult ) + '\n' + to_information_string( eresult );
210 static QString to_error_string( const SigningResult & sresult, const EncryptionResult & eresult ) {
211 return to_information_string( sresult, eresult );
214 // static
215 void MessageBox::information( QWidget * parent, const SigningResult & result, const Job * job, KMessageBox::Options options ) {
216 information( parent, result, job, i18n("Signing Result"), options );
219 // static
220 void MessageBox::information( QWidget * parent, const SigningResult & result, const Job * job, const QString & caption, KMessageBox::Options options ) {
221 make( parent, QMessageBox::Information, to_information_string( result ), job, caption, options );
224 // static
225 void MessageBox::error( QWidget * parent, const SigningResult & result, const Job * job, KMessageBox::Options options ) {
226 error( parent, result, job, i18n("Signing Error"), options );
229 // static
230 void MessageBox::error( QWidget * parent, const SigningResult & result, const Job * job, const QString & caption, KMessageBox::Options options ) {
231 make( parent, QMessageBox::Critical, to_error_string( result ), job, caption, options );
234 // static
235 void MessageBox::information( QWidget * parent, const EncryptionResult & result, const Job * job, KMessageBox::Options options ) {
236 information( parent, result, job, i18n("Encryption Result"), options );
239 // static
240 void MessageBox::information( QWidget * parent, const EncryptionResult & result, const Job * job, const QString & caption, KMessageBox::Options options ) {
241 make( parent, QMessageBox::Information, to_information_string( result ), job, caption, options );
244 // static
245 void MessageBox::error( QWidget * parent, const EncryptionResult & result, const Job * job, KMessageBox::Options options ) {
246 error( parent, result, job, i18n("Encryption Error"), options );
249 // static
250 void MessageBox::error( QWidget * parent, const EncryptionResult & result, const Job * job, const QString & caption, KMessageBox::Options options ) {
251 make( parent, QMessageBox::Critical, to_error_string( result ), job, caption, options );
254 // static
255 void MessageBox::information( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, KMessageBox::Options options ) {
256 information( parent, sresult, eresult, job, i18n("Encryption Result"), options );
259 // static
260 void MessageBox::information( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, const QString & caption, KMessageBox::Options options ) {
261 make( parent, QMessageBox::Information, to_information_string( sresult, eresult ), job, caption, options );
264 // static
265 void MessageBox::error( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, KMessageBox::Options options ) {
266 error( parent, sresult, eresult, job, i18n("Encryption Error"), options );
269 // static
270 void MessageBox::error( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, const QString & caption, KMessageBox::Options options ) {
271 make( parent, QMessageBox::Critical, to_error_string( sresult, eresult ), job, caption, options );
274 // static
275 bool MessageBox::showAuditLogButton( const Kleo::Job * job ) {
276 if ( !job ) {
277 kDebug() << "not showing audit log button (no job instance)";
278 return false;
280 if ( !GpgME::hasFeature( GpgME::AuditLogFeature ) ) {
281 kDebug() << "not showing audit log button (gpgme too old)";
282 return false;
284 if ( !job->isAuditLogSupported() ) {
285 kDebug() << "not showing audit log button (not supported)";
286 return false;
288 if ( job->auditLogError().code() == GPG_ERR_NO_DATA ) {
289 kDebug() << "not showing audit log button (GPG_ERR_NO_DATA)";
290 return false;
292 if ( !job->auditLogError() && job->auditLogAsHtml().isEmpty() ) {
293 kDebug() << "not showing audit log button (success, but result empty)";
294 return false;
296 return true;
300 // static
301 void MessageBox::make( QWidget * parent, QMessageBox::Icon icon, const QString & text, const Job * job, const QString & caption, KMessageBox::Options options ) {
302 KDialog * dialog = new KDialog( parent );
303 dialog->setCaption( caption );
304 dialog->setButtons( showAuditLogButton( job ) ? ( KDialog::Yes | KDialog::No ) : KDialog::Yes );
305 dialog->setDefaultButton( KDialog::Yes );
306 dialog->setEscapeButton( KDialog::Yes );
307 dialog->setObjectName( "error" );
308 dialog->setModal( true );
309 dialog->showButtonSeparator( true );
310 dialog->setButtonGuiItem( KDialog::Yes, KStandardGuiItem::ok() );
311 if ( GpgME::hasFeature( GpgME::AuditLogFeature ) )
312 dialog->setButtonGuiItem( KDialog::No, KGuiItem_showAuditLog() );
314 if ( options & KMessageBox::PlainCaption )
315 dialog->setPlainCaption( caption );
317 if ( KDialog::No == KMessageBox::createKMessageBox( dialog, icon, text, QStringList(), QString::null, 0, options ) )
318 auditLog( 0, job );
321 #include "moc_messagebox_p.cpp"