verifydetachedbodypartmemento.cpp 5.02 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
/*
  Copyright (c) 2014 Montel Laurent <montel@kde.org>

  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License, version 2, as
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "verifydetachedbodypartmemento.h"

#include <kdebug.h>
#include <kleo/verifydetachedjob.h>
#include <kleo/keylistjob.h>

#include <gpgme++/keylistresult.h>

#include <qstringlist.h>

#include <cassert>

using namespace Kleo;
using namespace GpgME;
using namespace MessageViewer;



VerifyDetachedBodyPartMemento::VerifyDetachedBodyPartMemento( VerifyDetachedJob * job,
                                                              KeyListJob * klj,
                                                              const QByteArray & signature,
                                                              const QByteArray & plainText )
    : CryptoBodyPartMemento(),
      m_signature( signature ),
      m_plainText( plainText ),
      m_job( job ),
      m_keylistjob( klj )
{
    assert( m_job );
}

VerifyDetachedBodyPartMemento::~VerifyDetachedBodyPartMemento() {
    if ( m_job )
        m_job->slotCancel();
    if ( m_keylistjob )
        m_keylistjob->slotCancel();
}

bool VerifyDetachedBodyPartMemento::start() {
    assert( m_job );
#ifdef DEBUG_SIGNATURE
    kDebug() << "tokoe: VerifyDetachedBodyPartMemento started";
#endif
    connect( m_job, SIGNAL(result(GpgME::VerificationResult)),
             this, SLOT(slotResult(GpgME::VerificationResult)) );
    if ( const Error err = m_job->start( m_signature, m_plainText ) ) {
        m_vr = VerificationResult( err );
#ifdef DEBUG_SIGNATURE
        kDebug() << "tokoe: VerifyDetachedBodyPartMemento stopped with error";
#endif
        return false;
    }
    setRunning( true );
    return true;
}

void VerifyDetachedBodyPartMemento::exec() {
    assert( m_job );
    setRunning( true );
#ifdef DEBUG_SIGNATURE
    kDebug() << "tokoe: VerifyDetachedBodyPartMemento execed";
#endif
    saveResult( m_job->exec( m_signature, m_plainText ) );
    m_job->deleteLater(); // exec'ed jobs don't delete themselves
    m_job = 0;
#ifdef DEBUG_SIGNATURE
    kDebug() << "tokoe: VerifyDetachedBodyPartMemento after execed";
#endif
    if ( canStartKeyListJob() ) {
        std::vector<GpgME::Key> keys;
        m_keylistjob->exec( keyListPattern(), /*secretOnly=*/false, keys );
        if ( !keys.empty() )
            m_key = keys.back();
    }
    if ( m_keylistjob )
        m_keylistjob->deleteLater(); // exec'ed jobs don't delete themselves
    m_keylistjob = 0;
    setRunning( false );
}

bool VerifyDetachedBodyPartMemento::canStartKeyListJob() const
{
    if ( !m_keylistjob )
        return false;
    const char * const fpr = m_vr.signature( 0 ).fingerprint();
    return fpr && *fpr;
}

QStringList VerifyDetachedBodyPartMemento::keyListPattern() const
{
    assert( canStartKeyListJob() );
    return QStringList( QString::fromLatin1( m_vr.signature( 0 ).fingerprint() ) );
}

void VerifyDetachedBodyPartMemento::saveResult( const VerificationResult & vr )
{
    assert( m_job );
#ifdef DEBUG_SIGNATURE
    kDebug() << "tokoe: VerifyDetachedBodyPartMemento::saveResult called";
#endif
    m_vr = vr;
    setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
}

void VerifyDetachedBodyPartMemento::slotResult( const VerificationResult & vr )
{
#ifdef DEBUG_SIGNATURE
    kDebug() << "tokoe: VerifyDetachedBodyPartMemento::slotResult called";
#endif
    saveResult( vr );
    m_job = 0;
    if ( canStartKeyListJob() && startKeyListJob() ) {
#ifdef DEBUG_SIGNATURE
        kDebug() << "tokoe: VerifyDetachedBodyPartMemento: canStartKeyListJob && startKeyListJob";
#endif
        return;
    }
    if ( m_keylistjob )
        m_keylistjob->deleteLater();
    m_keylistjob = 0;
    setRunning( false );
    notify();
}

bool VerifyDetachedBodyPartMemento::startKeyListJob()
{
    assert( canStartKeyListJob() );
    if ( const GpgME::Error err = m_keylistjob->start( keyListPattern() ) )
        return false;
    connect( m_keylistjob, SIGNAL(done()), this, SLOT(slotKeyListJobDone()) );
    connect( m_keylistjob, SIGNAL(nextKey(GpgME::Key)),
             this, SLOT(slotNextKey(GpgME::Key)) );
    return true;
}

void VerifyDetachedBodyPartMemento::slotNextKey( const GpgME::Key & key )
{
#ifdef DEBUG_SIGNATURE
    kDebug() << "tokoe: VerifyDetachedBodyPartMemento::slotNextKey called";
#endif
    m_key = key;
}

void VerifyDetachedBodyPartMemento::slotKeyListJobDone()
{
#ifdef DEBUG_SIGNATURE
    kDebug() << "tokoe: VerifyDetachedBodyPartMemento::slotKeyListJobDone called";
#endif
    m_keylistjob = 0;
    setRunning( false );
    notify();
}