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 
00050 #include <qtextedit.h>
00051 #include <qtextstream.h>
00052 #include <qvbox.h>
00053 
00054 using namespace Kleo;
00055 using namespace GpgME;
00056 
00057 namespace {
00058 
00059 static KGuiItem KGuiItem_save() {
00060     return KGuiItem( i18n("&Save to Disk..."), "filesaveas" );
00061 }
00062 
00063 static KGuiItem KGuiItem_copy() {
00064     return KGuiItem( i18n("&Copy to Clipboard"), "editcopy", i18n("Copy Audit Log to Clipboard") );
00065 }
00066 
00067 static KGuiItem KGuiItem_showAuditLog() {
00068     return KGuiItem( i18n("&Show Audit Log") ); // "view_log"?
00069 }
00070 
00071 class AuditLogViewer : public KDialogBase {
00072     // Q_OBJECT
00073 public:
00074     explicit AuditLogViewer( const QString & log, QWidget * parent=0, const char * name=0, WFlags f=0 )
00075         : KDialogBase( parent, name, false, i18n("View GnuPG Audit Log"),
00076                        Close|User1|User2, Close, false, KGuiItem_save(), KGuiItem_copy() ),
00077           m_textEdit( new QTextEdit( this, "m_textEdit" ) )
00078     {
00079         setWFlags( f );
00080         setMainWidget( m_textEdit );
00081         m_textEdit->setTextFormat( QTextEdit::RichText );
00082         m_textEdit->setReadOnly( true );
00083         setAuditLog( log );
00084     }
00085     ~AuditLogViewer() {}
00086 
00087     void setAuditLog( const QString & log ) {
00088         m_textEdit->setText( log );
00089     }
00090 
00091 private:
00092     void slotUser1() {
00093         const QString fileName = KFileDialog::getSaveFileName( QString(), QString(),
00094                                                                this, i18n("Choose File to Save GnuPG Audit Log to") );
00095         if ( fileName.isEmpty() )
00096             return;
00097 
00098         KSaveFile file( fileName );
00099 
00100         if ( QTextStream * const s = file.textStream() ) {
00101             *s << m_textEdit->text() << endl;
00102             file.close();
00103         }
00104 
00105         if ( const int err = file.status() )
00106             KMessageBox::error( this, i18n("Couldn't save to file \"%1\": %2")
00107                                 .arg( file.name(), QString::fromLocal8Bit( strerror( err ) ) ),
00108                                 i18n("File Save Error") );
00109     }
00110     void slotUser2() {
00111         m_textEdit->selectAll();
00112         m_textEdit->copy();
00113         m_textEdit->selectAll( false );
00114     }
00115 
00116 private:
00117     QTextEdit * m_textEdit;
00118 };
00119 
00120 } // anon namespace
00121 
00122 // static
00123 void MessageBox::auditLog( QWidget * parent, const Job * job, const QString & caption ) {
00124 
00125     if ( !job )
00126         return;
00127 
00128     if ( !GpgME::hasFeature( AuditLogFeature ) ) {
00129         KMessageBox::information( parent, i18n("Your system does not have support for GnuPG Audit Logs"),
00130                                   i18n("System Error") );
00131         return;
00132     }
00133 
00134     const QString log = job->auditLogAsHtml();
00135     if ( log.isEmpty() ) {
00136         KMessageBox::information( parent, i18n("No GnuPG Audit Log available for this operation."),
00137                                   i18n("No GnuPG Audit Log") );
00138         return;
00139     }
00140 
00141     auditLog( parent, log, caption );
00142 }
00143 
00144 // static
00145 void MessageBox::auditLog( QWidget * parent, const QString & log, const QString & caption ) {
00146     AuditLogViewer * const alv = new AuditLogViewer( "<qt>" + log + "</qt>", parent, "alv", Qt::WDestructiveClose );
00147     alv->setCaption( caption );
00148     alv->show();
00149 }
00150 
00151 // static
00152 void MessageBox::auditLog( QWidget * parent, const Job * job ) {
00153     auditLog( parent, job, i18n("GnuPG Audit Log Viewer") );
00154 }
00155 
00156 // static
00157 void MessageBox::auditLog( QWidget * parent, const QString & log ) {
00158     auditLog( parent, log, i18n("GnuPG Audit Log Viewer") );
00159 }
00160 
00161 static QString to_information_string( const SigningResult & result ) {
00162     return result.error()
00163         ? i18n("Signing failed: %1").arg( QString::fromLocal8Bit( result.error().asString() ) )
00164         : i18n("Signing successful") ;
00165 }
00166 
00167 static QString to_error_string( const SigningResult & result ) {
00168     return to_information_string( result );
00169 }
00170 
00171 static QString to_information_string( const EncryptionResult & result ) {
00172     return result.error()
00173         ? i18n("Encryption failed: %1").arg( QString::fromLocal8Bit( result.error().asString() ) )
00174         : i18n("Encryption successful") ;
00175 }
00176 
00177 static QString to_error_string( const EncryptionResult & result ) {
00178     return to_information_string( result );
00179 }
00180 
00181 static QString to_information_string( const SigningResult & sresult, const EncryptionResult & eresult ) {
00182     return to_information_string( sresult ) + '\n' + to_information_string( eresult );
00183 }
00184 
00185 static QString to_error_string( const SigningResult & sresult, const EncryptionResult & eresult ) {
00186     return to_information_string( sresult, eresult );
00187 }
00188 
00189 // static
00190 void MessageBox::information( QWidget * parent, const SigningResult & result, const Job * job, int options ) {
00191     information( parent, result, job, i18n("Signing Result"), options );
00192 }
00193 
00194 // static
00195 void MessageBox::information( QWidget * parent, const SigningResult & result, const Job * job, const QString & caption, int options ) {
00196     make( parent, QMessageBox::Information, to_information_string( result ), job, caption, options );
00197 }
00198 
00199 // static
00200 void MessageBox::error( QWidget * parent, const SigningResult & result, const Job * job, int options ) {
00201     error( parent, result, job, i18n("Signing Error"), options );
00202 }
00203 
00204 // static
00205 void MessageBox::error( QWidget * parent, const SigningResult & result, const Job * job, const QString & caption, int options ) {
00206     make( parent, QMessageBox::Critical, to_error_string( result ), job, caption, options );
00207 }
00208 
00209 // static
00210 void MessageBox::information( QWidget * parent, const EncryptionResult & result, const Job * job, int options ) {
00211     information( parent, result, job, i18n("Encryption Result"), options );
00212 }
00213 
00214 // static
00215 void MessageBox::information( QWidget * parent, const EncryptionResult & result, const Job * job, const QString & caption, int options ) {
00216     make( parent, QMessageBox::Information, to_information_string( result ), job, caption, options );
00217 }
00218 
00219 // static
00220 void MessageBox::error( QWidget * parent, const EncryptionResult & result, const Job * job, int options ) {
00221     error( parent, result, job, i18n("Encryption Error"), options );
00222 }
00223 
00224 // static
00225 void MessageBox::error( QWidget * parent, const EncryptionResult & result, const Job * job, const QString & caption, int options ) {
00226     make( parent, QMessageBox::Critical, to_error_string( result ), job, caption, options );
00227 }
00228 
00229 // static
00230 void MessageBox::information( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, int options ) {
00231     information( parent, sresult, eresult, job, i18n("Encryption Result"), options );
00232 }
00233 
00234 // static
00235 void MessageBox::information( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, const QString & caption, int options ) {
00236     make( parent, QMessageBox::Information, to_information_string( sresult, eresult ), job, caption, options );
00237 }
00238 
00239 // static
00240 void MessageBox::error( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, int options ) {
00241     error( parent, sresult, eresult, job, i18n("Encryption Error"), options );
00242 }
00243 
00244 // static
00245 void MessageBox::error( QWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, const QString & caption, int options ) {
00246     make( parent, QMessageBox::Critical, to_error_string( sresult, eresult ), job, caption, options );
00247 }
00248 
00249 // static
00250 void MessageBox::make( QWidget * parent, QMessageBox::Icon icon, const QString & text, const Job * job, const QString & caption, int options ) {
00251     KDialogBase * dialog = GpgME::hasFeature( GpgME::AuditLogFeature )
00252         ? new KDialogBase( caption, KDialogBase::Yes | KDialogBase::No,
00253                            KDialogBase::Yes, KDialogBase::Yes,
00254                            parent, "error", true, true,
00255                            KStdGuiItem::ok(), KGuiItem_showAuditLog() )
00256         : new KDialogBase( caption, KDialogBase::Yes,
00257                            KDialogBase::Yes, KDialogBase::Yes,
00258                            parent, "error", true, true,
00259                            KStdGuiItem::ok() ) ;
00260     if ( options & KMessageBox::PlainCaption )
00261         dialog->setPlainCaption( caption );
00262 
00263     if ( KDialogBase::No == KMessageBox::createKMessageBox( dialog, icon, text, QStringList(), QString::null, 0, options ) )
00264         auditLog( 0, job );
00265 }
KDE Home | KDE Accessibility Home | Description of Access Keys