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