certmanager/lib

qgpgmesignjob.cpp

00001 /*
00002     qgpgmesignjob.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 TQt library by Trolltech AS, Norway (or with modified versions
00024     of TQt that use the same license as TQt), 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     TQt.  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 "qgpgmesignjob.h"
00038 
00039 #include "ui/messagebox.h"
00040 
00041 #include <qgpgme/eventloopinteractor.h>
00042 #include <qgpgme/dataprovider.h>
00043 
00044 #include <gpgmepp/context.h>
00045 #include <gpgmepp/signingresult.h>
00046 #include <gpgmepp/data.h>
00047 #include <gpgmepp/key.h>
00048 
00049 #include <tdelocale.h>
00050 
00051 #include <assert.h>
00052 #include <stdlib.h>
00053 
00054 Kleo::QGpgMESignJob::QGpgMESignJob( GpgME::Context * context )
00055   : SignJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMESignJob" ),
00056     QGpgMEJob( this, context )
00057 {
00058   assert( context );
00059 }
00060 
00061 Kleo::QGpgMESignJob::~QGpgMESignJob() {
00062 }
00063 
00064 GpgME::Error Kleo::QGpgMESignJob::setup( const std::vector<GpgME::Key> & signers,
00065                      const TQByteArray & plainText ) {
00066   assert( !mInData );
00067   assert( !mOutData );
00068 
00069   createInData( plainText );
00070   createOutData();
00071 
00072   return setSigningKeys( signers );
00073 }
00074 
00075 GpgME::Error Kleo::QGpgMESignJob::start( const std::vector<GpgME::Key> & signers,
00076                      const TQByteArray & plainText,
00077                      GpgME::Context::SignatureMode mode ) {
00078   if ( const GpgME::Error error = setup( signers, plainText ) ) {
00079     deleteLater();
00080     return error;
00081   }
00082 
00083   hookupContextToEventLoopInteractor();
00084 
00085   const GpgME::Error err = mCtx->startSigning( *mInData, *mOutData, mode );
00086                           
00087   if ( err )
00088     deleteLater();
00089   mResult = GpgME::SigningResult( err );
00090   return err;
00091 }
00092 
00093 GpgME::SigningResult Kleo::QGpgMESignJob::exec( const std::vector<GpgME::Key> & signers,
00094                         const TQByteArray & plainText,
00095                         GpgME::Context::SignatureMode mode,
00096                         TQByteArray & signature ) {
00097   if ( const GpgME::Error err = setup( signers, plainText ) )
00098     return mResult = GpgME::SigningResult( 0, err );
00099   mResult = mCtx->sign( *mInData, *mOutData, mode );
00100   signature = mOutDataDataProvider->data();
00101   getAuditLog();
00102   return mResult;
00103 }
00104 
00105 void Kleo::QGpgMESignJob::doOperationDoneEvent( const GpgME::Error & ) {
00106   mResult = mCtx->signingResult();
00107   const TQByteArray signature = mOutDataDataProvider->data();
00108   getAuditLog();
00109   emit result( mResult, signature );
00110 }
00111 
00112 void Kleo::QGpgMESignJob::showErrorDialog( TQWidget * parent, const TQString & caption ) const {
00113   if ( mResult.error() && !mResult.error().isCanceled() )
00114       Kleo::MessageBox::error( parent, mResult, this, caption );
00115 }
00116 
00117 #include "qgpgmesignjob.moc"