00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifdef HAVE_CONFIG_H
00034 #include <config.h>
00035 #endif
00036
00037 #include "messagebox.h"
00038
00039 #include "kleo/job.h"
00040
00041 #include <gpgmepp/signingresult.h>
00042 #include <gpgmepp/encryptionresult.h>
00043
00044 #include <kfiledialog.h>
00045 #include <kdialogbase.h>
00046 #include <klocale.h>
00047 #include <ksavefile.h>
00048 #include <kguiitem.h>
00049 #include <kdebug.h>
00050
00051 #include <qtextedit.h>
00052 #include <qtextstream.h>
00053 #include <qvbox.h>
00054 #include <qapplication.h>
00055
00056 #include <gpg-error.h>
00057
00058 using namespace Kleo;
00059 using namespace GpgME;
00060
00061 namespace {
00062
00063 static KGuiItem KGuiItem_save() {
00064 return KGuiItem( i18n("&Save to Disk..."), "filesaveas" );
00065 }
00066
00067 static KGuiItem KGuiItem_copy() {
00068 return KGuiItem( i18n("&Copy to Clipboard"), "editcopy", i18n("Copy Audit Log to Clipboard") );
00069 }
00070
00071 static KGuiItem KGuiItem_showAuditLog() {
00072 return KGuiItem( i18n("&Show Audit Log") );
00073 }
00074
00075 class AuditLogViewer : public KDialogBase {
00076
00077 public:
00078 explicit AuditLogViewer( const QString & log, QWidget * parent=0, const char * name=0, WFlags f=0 )
00079 : KDialogBase( parent, name, false, i18n("View GnuPG Audit Log"),
00080 Close|User1|User2, Close, false, KGuiItem_save(), KGuiItem_copy() ),
00081 m_textEdit( new QTextEdit( this, "m_textEdit" ) )
00082 {
00083 setWFlags( f );
00084 setMainWidget( m_textEdit );
00085 m_textEdit->setTextFormat( QTextEdit::RichText );
00086 m_textEdit->setReadOnly( true );
00087 setAuditLog( log );
00088 }
00089 ~AuditLogViewer() {}
00090
00091 void setAuditLog( const QString & log ) {
00092 m_textEdit->setText( log );
00093 const QRect rect = m_textEdit->paragraphRect( 0 );
00094 kdDebug() << "setAuditLog: rect = " << rect;
00095 if ( !rect.isValid() )
00096 return;
00097 QSize maxSize = qApp->desktop()->screenGeometry( this ).size() * 2 / 3 ;
00098 if ( !maxSize.isValid() )
00099 maxSize = QSize( 640, 480 );
00100 m_textEdit->setMinimumSize( rect.size().boundedTo( maxSize ) );
00101 }
00102
00103 private:
00104 void slotUser1() {
00105 const QString fileName = KFileDialog::getSaveFileName( QString(), QString(),
00106 this, i18n("Choose File to Save GnuPG Audit Log to") );
00107 if ( fileName.isEmpty() )
00108 return;
00109
00110 KSaveFile file( fileName );
00111
00112 if ( QTextStream * const s = file.textStream() ) {
00113 *s << m_textEdit->text() << endl;
00114 file.close();
00115 }
00116
00117 if ( const int err = file.status() )
00118 KMessageBox::error( this, i18n("Couldn't save to file \"%1\": %2")
00119 .arg( file.name(), QString::fromLocal8Bit( strerror( err ) ) ),
00120 i18n("File Save Error") );
00121 }
00122 void slotUser2() {
00123 m_textEdit->selectAll();
00124 m_textEdit->copy();
00125 m_textEdit->selectAll( false );
00126 }
00127
00128 private:
00129 QTextEdit * m_textEdit;
00130 };
00131
00132 }
00133
00134
00135 void MessageBox::auditLog( QWidget * parent, const Job * job, const QString & caption ) {
00136
00137 if ( !job )
00138 return;
00139
00140 if ( !GpgME::hasFeature( AuditLogFeature ) || !job->isAuditLogSupported() ) {
00141 KMessageBox::information( parent, i18n("Your system does not have support for GnuPG Audit Logs"),
00142 i18n("System Error") );
00143 return;
00144 }
00145
00146 const GpgME::Error err = job->auditLogError();
00147
00148 if ( err.code() != GPG_ERR_NO_DATA ) {
00149 KMessageBox::information( parent, i18n("An error occurred while trying to retrieve the GnuPG Audit Log:\n%1")
00150 .arg( QString::fromLocal8Bit( err.asString() ) ),
00151 i18n("GnuPG Audit Log Error") );
00152 return;
00153 }
00154
00155 const QString log = job->auditLogAsHtml();
00156
00157 if ( log.isEmpty() ) {
00158 KMessageBox::information( parent, i18n("No GnuPG Audit Log available for this operation."),
00159 i18n("No GnuPG Audit Log") );
00160 return;
00161 }
00162
00163 auditLog( parent, log, caption );
00164 }
00165
00166
00167 void MessageBox::auditLog( QWidget * parent, const QString & log, const QString & caption ) {
00168 AuditLogViewer * const alv = new AuditLogViewer( "<qt>" + log + "</qt>", parent, "alv", Qt::WDestructiveClose );
00169 alv->setCaption( caption );
00170 alv->show();
00171 }
00172
00173
00174 void MessageBox::auditLog( QWidget * parent, const Job * job ) {
00175 auditLog( parent, job, i18n("GnuPG Audit Log Viewer") );
00176 }
00177
00178
00179 void MessageBox::auditLog( QWidget * parent, const QString & log ) {
00180 auditLog( parent, log, i18n("GnuPG Audit Log Viewer") );
00181 }
00182
00183 static QString to_information_string( const SigningResult & result ) {
00184 return result.error()
00185 ? i18n("Signing failed: %1").arg( QString::fromLocal8Bit( result.error().asString() ) )
00186 : i18n("Signing successful") ;
00187 }
00188
00189 static QString to_error_string( const SigningResult & result ) {
00190 return to_information_string( result );
00191 }
00192
00193 static QString to_information_string( const EncryptionResult & result ) {
00194 return result.error()
00195 ? i18n("Encryption failed: %1").arg( QString::fromLocal8Bit( result.error().asString() ) )
00196 : i18n("Encryption successful") ;
00197 }
00198
00199 static QString to_error_string( const EncryptionResult & result ) {
00200 return to_information_string( result );
00201 }
00202
00203 static QString to_information_string( const SigningResult & sresult, const EncryptionResult & eresult ) {
00204 return to_information_string( sresult ) + '\n' + to_information_string( eresult );
00205 }
00206
00207 static QString to_error_string( const SigningResult & sresult, const EncryptionResult & eresult ) {
00208 return to_information_string( sresult, eresult );
00209 }
00210
00211
00212 void MessageBox::information( QWidget * parent, const SigningResult & result, const Job * job, int options ) {
00213 information( parent, result, job, i18n("Signing Result"), options );
00214 }
00215
00216
00217 void MessageBox::information( QWidget * parent, const SigningResult & result, const Job * job, const QString & caption, int options ) {
00218 make( parent, QMessageBox::Information, to_information_string( result ), job, caption, options );
00219 }
00220
00221
00222 void MessageBox::error( QWidget * parent, const SigningResult & result, const Job * job, int options ) {
00223 error( parent, result, job, i18n("Signing Error"), options );
00224 }
00225
00226
00227 void MessageBox::error( QWidget * parent, const SigningResult & result, const Job * job, const QString & caption, int options ) {
00228 make( parent, QMessageBox::Critical, to_error_string( result ), job, caption, options );
00229 }
00230
00231
00232 void MessageBox::information( QWidget * parent, const EncryptionResult & result, const Job * job, int options ) {
00233 information( parent, result, job, i18n("Encryption Result"), options );
00234 }
00235
00236
00237 void MessageBox::information( QWidget * parent, const EncryptionResult & result, const Job * job, const QString & caption, int options ) {
00238 make( parent, QMessageBox::Information, to_information_string( result ), job, caption, options );
00239 }
00240
00241
00242 void MessageBox::error( QWidget * parent, const EncryptionResult & result, const Job * job, int options ) {
00243 error( parent, result, job, i18n("Encryption Error"), options );
00244 }
00245
00246
00247 void MessageBox::error( QWidget * parent, const EncryptionResult & result, const Job * job, const QString & caption, int options ) {
00248 make( parent, QMessageBox::Critical, to_error_string( result ), job, caption, options );
00249 }
00250
00251
00252 void MessageBox::information( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, int options ) {
00253 information( parent, sresult, eresult, job, i18n("Encryption Result"), options );
00254 }
00255
00256
00257 void MessageBox::information( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, const QString & caption, int options ) {
00258 make( parent, QMessageBox::Information, to_information_string( sresult, eresult ), job, caption, options );
00259 }
00260
00261
00262 void MessageBox::error( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, int options ) {
00263 error( parent, sresult, eresult, job, i18n("Encryption Error"), options );
00264 }
00265
00266
00267 void MessageBox::error( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, const QString & caption, int options ) {
00268 make( parent, QMessageBox::Critical, to_error_string( sresult, eresult ), job, caption, options );
00269 }
00270
00271
00272 bool MessageBox::showAuditLogButton( const Kleo::Job * job ) {
00273 if ( !job ) {
00274 kdDebug() << "not showing audit log button (no job instance)" << endl;
00275 return false;
00276 }
00277 if ( !GpgME::hasFeature( GpgME::AuditLogFeature ) ) {
00278 kdDebug() << "not showing audit log button (gpgme too old)" << endl;
00279 return false;
00280 }
00281 if ( !job->isAuditLogSupported() ) {
00282 kdDebug() << "not showing audit log button (not supported)" << endl;
00283 return false;
00284 }
00285 if ( job->auditLogError().code() == GPG_ERR_NO_DATA ) {
00286 kdDebug() << "not showing audit log button (GPG_ERR_NO_DATA)" << endl;
00287 return false;
00288 }
00289 if ( !job->auditLogError() && job->auditLogAsHtml().isEmpty() ) {
00290 kdDebug() << "not showing audit log button (success, but result empty)" << endl;
00291 return false;
00292 }
00293 return true;
00294 }
00295
00296
00297
00298 void MessageBox::make( QWidget * parent, QMessageBox::Icon icon, const QString & text, const Job * job, const QString & caption, int options ) {
00299 KDialogBase * dialog = showAuditLogButton( job )
00300 ? new KDialogBase( caption, KDialogBase::Yes | KDialogBase::No,
00301 KDialogBase::Yes, KDialogBase::Yes,
00302 parent, "error", true, true,
00303 KStdGuiItem::ok(), KGuiItem_showAuditLog() )
00304 : new KDialogBase( caption, KDialogBase::Yes,
00305 KDialogBase::Yes, KDialogBase::Yes,
00306 parent, "error", true, true,
00307 KStdGuiItem::ok() ) ;
00308 if ( options & KMessageBox::PlainCaption )
00309 dialog->setPlainCaption( caption );
00310
00311 if ( KDialogBase::No == KMessageBox::createKMessageBox( dialog, icon, text, QStringList(), QString::null, 0, options ) )
00312 auditLog( 0, job );
00313 }