certmanager/lib

messagebox.cpp
1/*
2 messagebox.cpp
3
4 This file is part of libkleopatra, the KDE keymanagement library
5 Copyright (c) 2004 Klarälvdalens Datakonsult AB
6
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.
11
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.
16
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
20
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 TQt library by Trolltech AS, Norway (or with modified versions
24 of TQt that use the same license as TQt), 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 TQt. 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.
31*/
32
33#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37#include "messagebox.h"
38
39#include "kleo/job.h"
40
41#include <gpgmepp/signingresult.h>
42#include <gpgmepp/encryptionresult.h>
43
44#include <tdefiledialog.h>
45#include <kdialogbase.h>
46#include <tdelocale.h>
47#include <ksavefile.h>
48#include <kguiitem.h>
49#include <kdebug.h>
50
51#include <tqtextedit.h>
52#include <tqtextstream.h>
53#include <tqvbox.h>
54#include <tqapplication.h>
55#include <tqstylesheet.h>
56
57#include <gpg-error.h>
58
59using namespace Kleo;
60using namespace GpgME;
61
62namespace {
63
64static KGuiItem KGuiItem_save() {
65 return KGuiItem( i18n("&Save to Disk..."), "document-save-as" );
66}
67
68static KGuiItem KGuiItem_copy() {
69 return KGuiItem( i18n("&Copy to Clipboard"), "edit-copy", i18n("Copy Audit Log to Clipboard") );
70}
71
72static KGuiItem KGuiItem_showAuditLog() {
73 return KGuiItem( i18n("&Show Audit Log") ); // "view_log"?
74}
75
76class AuditLogViewer : public KDialogBase {
77 // TQ_OBJECT
78
79public:
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() ),
83 m_log( /* sic */ ),
84 m_textEdit( new TQTextEdit( this, "m_textEdit" ) )
85 {
86 setWFlags( f );
87 setMainWidget( m_textEdit );
88 m_textEdit->setTextFormat( TQTextEdit::RichText );
89 m_textEdit->setReadOnly( true );
90 setAuditLog( log );
91 }
92 ~AuditLogViewer() {}
93
94 void setAuditLog( const TQString & log ) {
95 if ( log == m_log )
96 return;
97 m_log = 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() )
102 return;
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 ) );
107 }
108
109private:
110 void slotUser1() {
111 const TQString fileName = KFileDialog::getSaveFileName( TQString(), TQString(),
112 this, i18n("Choose File to Save GnuPG Audit Log to") );
113 if ( fileName.isEmpty() )
114 return;
115
116 KSaveFile file( fileName );
117
118 if ( TQTextStream * const s = file.textStream() ) {
119 *s << "<html><head>";
120 if ( !caption().isEmpty() )
121 *s << "\n<title>" << /*TQt*/TQStyleSheet::escape( caption() ) << "</title>\n";
122 *s << "</head><body>\n"
123 << m_log
124 << "\n</body></html>" << endl;
125 file.close();
126 }
127
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") );
132 }
133 void slotUser2() {
134 m_textEdit->selectAll();
135 m_textEdit->copy();
136 m_textEdit->selectAll( false );
137 }
138
139private:
140 TQString m_log;
141 TQTextEdit * m_textEdit;
142};
143
144} // anon namespace
145
146// static
147void MessageBox::auditLog( TQWidget * parent, const Job * job, const TQString & caption ) {
148
149 if ( !job )
150 return;
151
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") );
155 return;
156 }
157
158 const GpgME::Error err = job->auditLogError();
159
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") );
164 return;
165 }
166
167 const TQString log = job->auditLogAsHtml();
168
169 if ( log.isEmpty() ) {
170 KMessageBox::information( parent, i18n("No GnuPG Audit Log available for this operation."),
171 i18n("No GnuPG Audit Log") );
172 return;
173 }
174
175 auditLog( parent, log, caption );
176}
177
178// static
179void 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 );
182 alv->show();
183}
184
185// static
186void MessageBox::auditLog( TQWidget * parent, const Job * job ) {
187 auditLog( parent, job, i18n("GnuPG Audit Log Viewer") );
188}
189
190// static
191void MessageBox::auditLog( TQWidget * parent, const TQString & log ) {
192 auditLog( parent, log, i18n("GnuPG Audit Log Viewer") );
193}
194
195static 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") ;
199}
200
201static TQString to_error_string( const SigningResult & result ) {
202 return to_information_string( result );
203}
204
205static 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") ;
209}
210
211static TQString to_error_string( const EncryptionResult & result ) {
212 return to_information_string( result );
213}
214
215static TQString to_information_string( const SigningResult & sresult, const EncryptionResult & eresult ) {
216 return to_information_string( sresult ) + '\n' + to_information_string( eresult );
217}
218
219static TQString to_error_string( const SigningResult & sresult, const EncryptionResult & eresult ) {
220 return to_information_string( sresult, eresult );
221}
222
223// static
224void MessageBox::information( TQWidget * parent, const SigningResult & result, const Job * job, int options ) {
225 information( parent, result, job, i18n("Signing Result"), options );
226}
227
228// static
229void 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 );
231}
232
233// static
234void MessageBox::error( TQWidget * parent, const SigningResult & result, const Job * job, int options ) {
235 error( parent, result, job, i18n("Signing Error"), options );
236}
237
238// static
239void 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 );
241}
242
243// static
244void MessageBox::information( TQWidget * parent, const EncryptionResult & result, const Job * job, int options ) {
245 information( parent, result, job, i18n("Encryption Result"), options );
246}
247
248// static
249void 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 );
251}
252
253// static
254void MessageBox::error( TQWidget * parent, const EncryptionResult & result, const Job * job, int options ) {
255 error( parent, result, job, i18n("Encryption Error"), options );
256}
257
258// static
259void 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 );
261}
262
263// static
264void 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 );
266}
267
268// static
269void 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 );
271}
272
273// static
274void 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 );
276}
277
278// static
279void 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 );
281}
282
283// static
284bool MessageBox::showAuditLogButton( const Kleo::Job * job ) {
285 if ( !job ) {
286 kdDebug() << "not showing audit log button (no job instance)" << endl;
287 return false;
288 }
289 if ( !GpgME::hasFeature( GpgME::AuditLogFeature ) ) {
290 kdDebug() << "not showing audit log button (gpgme too old)" << endl;
291 return false;
292 }
293 if ( !job->isAuditLogSupported() ) {
294 kdDebug() << "not showing audit log button (not supported)" << endl;
295 return false;
296 }
297 if ( job->auditLogError().code() == GPG_ERR_NO_DATA ) {
298 kdDebug() << "not showing audit log button (GPG_ERR_NO_DATA)" << endl;
299 return false;
300 }
301 if ( !job->auditLogError() && job->auditLogAsHtml().isEmpty() ) {
302 kdDebug() << "not showing audit log button (success, but result empty)" << endl;
303 return false;
304 }
305 return true;
306}
307
308
309// static
310void 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 );
322
323 if ( KDialogBase::No == KMessageBox::createKMessageBox( dialog, icon, text, TQStringList(), TQString(), 0, options ) )
324 auditLog( 0, job );
325}
An abstract base class for asynchronous crypto operations.
Definition: job.h:64