certmanager/lib

messagebox.cpp

00001 /*
00002     messagebox.cpp
00003 
00004     This file is part of libkleopatra, the KDE keymanagement library
00005     Copyright (c) 2004 Klarälvdalens Datakonsult AB
00006 
00007     Libkleopatra is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU General Public License as
00009     published by the Free Software Foundation; either version 2 of the
00010     License, or (at your option) any later version.
00011 
00012     Libkleopatra is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021     In addition, as a special exception, the copyright holders give
00022     permission to link the code of this program with any edition of
00023     the Qt library by Trolltech AS, Norway (or with modified versions
00024     of Qt that use the same license as Qt), and distribute linked
00025     combinations including the two.  You must obey the GNU General
00026     Public License in all respects for all of the code used other than
00027     Qt.  If you modify this file, you may extend this exception to
00028     your version of the file, but you are not obligated to do so.  If
00029     you do not wish to do so, delete this exception statement from
00030     your version.
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 #include <qstylesheet.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") ); // "view_log"?
00074 }
00075 
00076 class AuditLogViewer : public KDialogBase {
00077     // Q_OBJECT
00078 public:
00079     explicit AuditLogViewer( const QString & log, QWidget * 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( /* sic */ ),
00083           m_textEdit( new QTextEdit( this, "m_textEdit" ) )
00084     {
00085         setWFlags( f );
00086         setMainWidget( m_textEdit );
00087         m_textEdit->setTextFormat( QTextEdit::RichText );
00088         m_textEdit->setReadOnly( true );
00089         setAuditLog( log );
00090     }
00091     ~AuditLogViewer() {}
00092 
00093     void setAuditLog( const QString & log ) {
00094         if ( log == m_log )
00095             return;
00096         m_log = log;
00097         m_textEdit->setText( "<qt>" + log + "</qt>" );
00098         const QRect rect = m_textEdit->paragraphRect( 0 );
00099         kdDebug() << "setAuditLog: rect = " << rect << endl;
00100         if ( !rect.isValid() )
00101             return;
00102         QSize maxSize = qApp->desktop()->screenGeometry( this ).size() * 2 / 3 ;
00103         if ( !maxSize.isValid() )
00104             maxSize = QSize( 640, 480 );
00105         m_textEdit->setMinimumSize( rect.size().boundedTo( maxSize ) );
00106     }
00107 
00108 private:
00109     void slotUser1() {
00110         const QString fileName = KFileDialog::getSaveFileName( QString(), QString(),
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 ( QTextStream * const s = file.textStream() ) {
00118             *s << "<html><head>";
00119             if ( !caption().isEmpty() )
00120                 *s << "\n<title>" << /*Qt*/QStyleSheet::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(), QString::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     QString m_log;
00140     QTextEdit * m_textEdit;
00141 };
00142 
00143 } // anon namespace
00144 
00145 // static
00146 void MessageBox::auditLog( QWidget * parent, const Job * job, const QString & 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( QString::fromLocal8Bit( err.asString() ) ),
00162                                   i18n("GnuPG Audit Log Error") );
00163         return;
00164     }
00165 
00166     const QString 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 // static
00178 void MessageBox::auditLog( QWidget * parent, const QString & log, const QString & caption ) {
00179     AuditLogViewer * const alv = new AuditLogViewer( log, parent, "alv", Qt::WDestructiveClose );
00180     alv->setCaption( caption );
00181     alv->show();
00182 }
00183 
00184 // static
00185 void MessageBox::auditLog( QWidget * parent, const Job * job ) {
00186     auditLog( parent, job, i18n("GnuPG Audit Log Viewer") );
00187 }
00188 
00189 // static
00190 void MessageBox::auditLog( QWidget * parent, const QString & log ) {
00191     auditLog( parent, log, i18n("GnuPG Audit Log Viewer") );
00192 }
00193 
00194 static QString to_information_string( const SigningResult & result ) {
00195     return result.error()
00196         ? i18n("Signing failed: %1").arg( QString::fromLocal8Bit( result.error().asString() ) )
00197         : i18n("Signing successful") ;
00198 }
00199 
00200 static QString to_error_string( const SigningResult & result ) {
00201     return to_information_string( result );
00202 }
00203 
00204 static QString to_information_string( const EncryptionResult & result ) {
00205     return result.error()
00206         ? i18n("Encryption failed: %1").arg( QString::fromLocal8Bit( result.error().asString() ) )
00207         : i18n("Encryption successful") ;
00208 }
00209 
00210 static QString to_error_string( const EncryptionResult & result ) {
00211     return to_information_string( result );
00212 }
00213 
00214 static QString to_information_string( const SigningResult & sresult, const EncryptionResult & eresult ) {
00215     return to_information_string( sresult ) + '\n' + to_information_string( eresult );
00216 }
00217 
00218 static QString to_error_string( const SigningResult & sresult, const EncryptionResult & eresult ) {
00219     return to_information_string( sresult, eresult );
00220 }
00221 
00222 // static
00223 void MessageBox::information( QWidget * parent, const SigningResult & result, const Job * job, int options ) {
00224     information( parent, result, job, i18n("Signing Result"), options );
00225 }
00226 
00227 // static
00228 void MessageBox::information( QWidget * parent, const SigningResult & result, const Job * job, const QString & caption, int options ) {
00229     make( parent, QMessageBox::Information, to_information_string( result ), job, caption, options );
00230 }
00231 
00232 // static
00233 void MessageBox::error( QWidget * parent, const SigningResult & result, const Job * job, int options ) {
00234     error( parent, result, job, i18n("Signing Error"), options );
00235 }
00236 
00237 // static
00238 void MessageBox::error( QWidget * parent, const SigningResult & result, const Job * job, const QString & caption, int options ) {
00239     make( parent, QMessageBox::Critical, to_error_string( result ), job, caption, options );
00240 }
00241 
00242 // static
00243 void MessageBox::information( QWidget * parent, const EncryptionResult & result, const Job * job, int options ) {
00244     information( parent, result, job, i18n("Encryption Result"), options );
00245 }
00246 
00247 // static
00248 void MessageBox::information( QWidget * parent, const EncryptionResult & result, const Job * job, const QString & caption, int options ) {
00249     make( parent, QMessageBox::Information, to_information_string( result ), job, caption, options );
00250 }
00251 
00252 // static
00253 void MessageBox::error( QWidget * parent, const EncryptionResult & result, const Job * job, int options ) {
00254     error( parent, result, job, i18n("Encryption Error"), options );
00255 }
00256 
00257 // static
00258 void MessageBox::error( QWidget * parent, const EncryptionResult & result, const Job * job, const QString & caption, int options ) {
00259     make( parent, QMessageBox::Critical, to_error_string( result ), job, caption, options );
00260 }
00261 
00262 // static
00263 void MessageBox::information( QWidget * 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 // static
00268 void MessageBox::information( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, const QString & caption, int options ) {
00269     make( parent, QMessageBox::Information, to_information_string( sresult, eresult ), job, caption, options );
00270 }
00271 
00272 // static
00273 void MessageBox::error( QWidget * 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 // static
00278 void MessageBox::error( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, const QString & caption, int options ) {
00279     make( parent, QMessageBox::Critical, to_error_string( sresult, eresult ), job, caption, options );
00280 }
00281 
00282 // static
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 // static
00309 void MessageBox::make( QWidget * parent, QMessageBox::Icon icon, const QString & text, const Job * job, const QString & 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, QStringList(), QString::null, 0, options ) )
00323         auditLog( 0, job );
00324 }
KDE Home | KDE Accessibility Home | Description of Access Keys