37 #include "messagebox.h"
41 #include <gpgmepp/signingresult.h>
42 #include <gpgmepp/encryptionresult.h>
44 #include <tdefiledialog.h>
45 #include <kdialogbase.h>
46 #include <tdelocale.h>
47 #include <ksavefile.h>
51 #include <tqtextedit.h>
52 #include <tqtextstream.h>
54 #include <tqapplication.h>
55 #include <tqstylesheet.h>
57 #include <gpg-error.h>
60 using namespace GpgME;
64 static KGuiItem KGuiItem_save() {
65 return KGuiItem( i18n(
"&Save to Disk..."),
"document-save-as" );
68 static KGuiItem KGuiItem_copy() {
69 return KGuiItem( i18n(
"&Copy to Clipboard"),
"edit-copy", i18n(
"Copy Audit Log to Clipboard") );
72 static KGuiItem KGuiItem_showAuditLog() {
73 return KGuiItem( i18n(
"&Show Audit Log") );
76 class AuditLogViewer :
public KDialogBase {
80 explicit AuditLogViewer(
const TQString & log, TQWidget * parent=0,
const char * name=0, WFlags f=0 )
81 : KDialogBase( parent, name, false, i18n(
"View GnuPG Audit Log"),
82 Close|User1|User2, Close, false, KGuiItem_save(), KGuiItem_copy() ),
84 m_textEdit( new TQTextEdit( this,
"m_textEdit" ) )
87 setMainWidget( m_textEdit );
88 m_textEdit->setTextFormat( TQTextEdit::RichText );
89 m_textEdit->setReadOnly(
true );
94 void setAuditLog(
const TQString & log ) {
98 m_textEdit->setText(
"<qt>" + log +
"</qt>" );
99 const TQRect rect = m_textEdit->paragraphRect( 0 );
100 kdDebug() <<
"setAuditLog: rect = " << rect << endl;
101 if ( !rect.isValid() )
103 TQSize maxSize = tqApp->desktop()->screenGeometry(
this ).size() * 2 / 3 ;
104 if ( !maxSize.isValid() )
105 maxSize = TQSize( 640, 480 );
106 m_textEdit->setMinimumSize( rect.size().boundedTo( maxSize ) );
111 const TQString fileName = KFileDialog::getSaveFileName( TQString(), TQString(),
112 this, i18n(
"Choose File to Save GnuPG Audit Log to") );
113 if ( fileName.isEmpty() )
116 KSaveFile file( fileName );
118 if ( TQTextStream *
const s = file.textStream() ) {
119 *s <<
"<html><head>";
120 if ( !caption().isEmpty() )
121 *s <<
"\n<title>" << TQStyleSheet::escape( caption() ) <<
"</title>\n";
122 *s <<
"</head><body>\n"
124 <<
"\n</body></html>" << endl;
128 if (
const int err = file.status() )
129 KMessageBox::error(
this, i18n(
"Couldn't save to file \"%1\": %2")
130 .arg( file.name(), TQString::fromLocal8Bit( strerror( err ) ) ),
131 i18n(
"File Save Error") );
134 m_textEdit->selectAll();
136 m_textEdit->selectAll(
false );
141 TQTextEdit * m_textEdit;
147 void MessageBox::auditLog( TQWidget * parent,
const Job * job,
const TQString & caption ) {
152 if ( !GpgME::hasFeature( AuditLogFeature ) || !job->isAuditLogSupported() ) {
153 KMessageBox::information( parent, i18n(
"Your system does not have support for GnuPG Audit Logs"),
154 i18n(
"System Error") );
158 const GpgME::Error err = job->auditLogError();
160 if ( err.code() != GPG_ERR_NO_DATA ) {
161 KMessageBox::information( parent, i18n(
"An error occurred while trying to retrieve the GnuPG Audit Log:\n%1")
162 .arg( TQString::fromLocal8Bit( err.asString() ) ),
163 i18n(
"GnuPG Audit Log Error") );
167 const TQString log = job->auditLogAsHtml();
169 if ( log.isEmpty() ) {
170 KMessageBox::information( parent, i18n(
"No GnuPG Audit Log available for this operation."),
171 i18n(
"No GnuPG Audit Log") );
175 auditLog( parent, log, caption );
179 void MessageBox::auditLog( TQWidget * parent,
const TQString & log,
const TQString & caption ) {
180 AuditLogViewer *
const alv =
new AuditLogViewer( log, parent,
"alv", TQt::WDestructiveClose );
181 alv->setCaption( caption );
186 void MessageBox::auditLog( TQWidget * parent,
const Job * job ) {
187 auditLog( parent, job, i18n(
"GnuPG Audit Log Viewer") );
191 void MessageBox::auditLog( TQWidget * parent,
const TQString & log ) {
192 auditLog( parent, log, i18n(
"GnuPG Audit Log Viewer") );
195 static TQString to_information_string(
const SigningResult & result ) {
196 return result.error()
197 ? i18n(
"Signing failed: %1").arg( TQString::fromLocal8Bit( result.error().asString() ) )
198 : i18n(
"Signing successful") ;
201 static TQString to_error_string(
const SigningResult & result ) {
202 return to_information_string( result );
205 static TQString to_information_string(
const EncryptionResult & result ) {
206 return result.error()
207 ? i18n(
"Encryption failed: %1").arg( TQString::fromLocal8Bit( result.error().asString() ) )
208 : i18n(
"Encryption successful") ;
211 static TQString to_error_string(
const EncryptionResult & result ) {
212 return to_information_string( result );
215 static TQString to_information_string(
const SigningResult & sresult,
const EncryptionResult & eresult ) {
216 return to_information_string( sresult ) +
'\n' + to_information_string( eresult );
219 static TQString to_error_string(
const SigningResult & sresult,
const EncryptionResult & eresult ) {
220 return to_information_string( sresult, eresult );
224 void MessageBox::information( TQWidget * parent,
const SigningResult & result,
const Job * job,
int options ) {
225 information( parent, result, job, i18n(
"Signing Result"), options );
229 void MessageBox::information( TQWidget * parent,
const SigningResult & result,
const Job * job,
const TQString & caption,
int options ) {
230 make( parent, TQMessageBox::Information, to_information_string( result ), job, caption, options );
234 void MessageBox::error( TQWidget * parent,
const SigningResult & result,
const Job * job,
int options ) {
235 error( parent, result, job, i18n(
"Signing Error"), options );
239 void MessageBox::error( TQWidget * parent,
const SigningResult & result,
const Job * job,
const TQString & caption,
int options ) {
240 make( parent, TQMessageBox::Critical, to_error_string( result ), job, caption, options );
244 void MessageBox::information( TQWidget * parent,
const EncryptionResult & result,
const Job * job,
int options ) {
245 information( parent, result, job, i18n(
"Encryption Result"), options );
249 void MessageBox::information( TQWidget * parent,
const EncryptionResult & result,
const Job * job,
const TQString & caption,
int options ) {
250 make( parent, TQMessageBox::Information, to_information_string( result ), job, caption, options );
254 void MessageBox::error( TQWidget * parent,
const EncryptionResult & result,
const Job * job,
int options ) {
255 error( parent, result, job, i18n(
"Encryption Error"), options );
259 void MessageBox::error( TQWidget * parent,
const EncryptionResult & result,
const Job * job,
const TQString & caption,
int options ) {
260 make( parent, TQMessageBox::Critical, to_error_string( result ), job, caption, options );
264 void MessageBox::information( TQWidget * parent,
const SigningResult & sresult,
const EncryptionResult & eresult,
const Job * job,
int options ) {
265 information( parent, sresult, eresult, job, i18n(
"Encryption Result"), options );
269 void MessageBox::information( TQWidget * parent,
const SigningResult & sresult,
const EncryptionResult & eresult,
const Job * job,
const TQString & caption,
int options ) {
270 make( parent, TQMessageBox::Information, to_information_string( sresult, eresult ), job, caption, options );
274 void MessageBox::error( TQWidget * parent,
const SigningResult & sresult,
const EncryptionResult & eresult,
const Job * job,
int options ) {
275 error( parent, sresult, eresult, job, i18n(
"Encryption Error"), options );
279 void MessageBox::error( TQWidget * parent,
const SigningResult & sresult,
const EncryptionResult & eresult,
const Job * job,
const TQString & caption,
int options ) {
280 make( parent, TQMessageBox::Critical, to_error_string( sresult, eresult ), job, caption, options );
284 bool MessageBox::showAuditLogButton(
const Kleo::Job * job ) {
286 kdDebug() <<
"not showing audit log button (no job instance)" << endl;
289 if ( !GpgME::hasFeature( GpgME::AuditLogFeature ) ) {
290 kdDebug() <<
"not showing audit log button (gpgme too old)" << endl;
293 if ( !job->isAuditLogSupported() ) {
294 kdDebug() <<
"not showing audit log button (not supported)" << endl;
297 if ( job->auditLogError().code() == GPG_ERR_NO_DATA ) {
298 kdDebug() <<
"not showing audit log button (GPG_ERR_NO_DATA)" << endl;
301 if ( !job->auditLogError() && job->auditLogAsHtml().isEmpty() ) {
302 kdDebug() <<
"not showing audit log button (success, but result empty)" << endl;
310 void MessageBox::make( TQWidget * parent, TQMessageBox::Icon icon,
const TQString & text,
const Job * job,
const TQString & caption,
int options ) {
311 KDialogBase * dialog = showAuditLogButton( job )
312 ?
new KDialogBase( caption, KDialogBase::Yes | KDialogBase::No,
313 KDialogBase::Yes, KDialogBase::Yes,
314 parent,
"error",
true,
true,
315 KStdGuiItem::ok(), KGuiItem_showAuditLog() )
316 : new KDialogBase( caption, KDialogBase::Yes,
317 KDialogBase::Yes, KDialogBase::Yes,
318 parent,
"error", true, true,
319 KStdGuiItem::ok() ) ;
320 if ( options & KMessageBox::PlainCaption )
321 dialog->setPlainCaption( caption );
323 if ( KDialogBase::No == KMessageBox::createKMessageBox( dialog, icon, text, TQStringList(), TQString(), 0, options ) )
An abstract base class for asynchronous crypto operations.