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