<commit_before>/* -*- mode: c++; c-basic-offset:4 -*-
    uiserver/signencryptfilestask.cpp

    This file is part of Kleopatra, the KDE keymanager
    Copyright (c) 2007 Klarälvdalens Datakonsult AB

    Kleopatra is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Kleopatra 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

    In addition, as a special exception, the copyright holders give
    permission to link the code of this program with any edition of
    the Qt library by Trolltech AS, Norway (or with modified versions
    of Qt that use the same license as Qt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    Qt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.
*/

#include "signencryptfilestask.h"

#include "kleo-assuan.h"

#include "input.h"
#include "output.h"

#include <utils/stl_util.h>

#include <kleo/cryptobackendfactory.h>
#include <kleo/cryptobackend.h>
#include <kleo/signjob.h>
#include <kleo/signencryptjob.h>
#include <kleo/encryptjob.h>

#include <gpgme++/signingresult.h>
#include <gpgme++/encryptionresult.h>
#include <gpgme++/key.h>

#include <KLocale>

#include <QPointer>
#include <QTextDocument> // for Qt::escape

#include <boost/bind.hpp>

using namespace Kleo;
using namespace boost;
using namespace GpgME;

namespace {

    class SignEncryptFilesResult : public Task::Result {
        const SigningResult m_sresult;
        const EncryptionResult m_eresult;
    public:
        explicit SignEncryptFilesResult( const SigningResult & sr )
            : Task::Result(), m_sresult( sr ) {}
        explicit SignEncryptFilesResult( const EncryptionResult & er )
            : Task::Result(), m_eresult( er ) {}
        explicit SignEncryptFilesResult( const SigningResult & sr, const EncryptionResult & er )
            : Task::Result(), m_sresult( sr ), m_eresult( er ) {}

        /* reimp */ QString overview() const;
        /* reimp */ QString details() const;
    };


    static QString makeErrorString( const SigningResult & result ) {
        const Error err = result.error();

        assuan_assert( err || err.isCanceled() );

        if ( err.isCanceled() )
            return i18n("Signing canceled.");
        else // if ( err )
            return i18n("Signing failed: %1.", Qt::escape( QString::fromLocal8Bit( err.asString() ) ) );
    }

    static QString makeErrorString( const EncryptionResult & result ) {
        const Error err = result.error();

        assuan_assert( err || err.isCanceled() );

        if ( err.isCanceled() )
            return i18n("Encryption canceled.");
        else // if ( err )
            return i18n("Encryption failed: %1.", Qt::escape( QString::fromLocal8Bit( err.asString() ) ) );
    }

}

class SignEncryptFilesTask::Private {
    friend class ::Kleo::SignEncryptFilesTask;
    SignEncryptFilesTask * const q;
public:
    explicit Private( SignEncryptFilesTask * qq );

private:
    std::auto_ptr<Kleo::SignJob> createSignJob( GpgME::Protocol proto );
    std::auto_ptr<Kleo::SignEncryptJob> createSignEncryptJob( GpgME::Protocol proto );
    std::auto_ptr<Kleo::EncryptJob> createEncryptJob( GpgME::Protocol proto );

private:
    void slotResult( const SigningResult & );
    void slotResult( const SigningResult &, const EncryptionResult & );
    void slotResult( const EncryptionResult & );

private:
    shared_ptr<Input> input;
    shared_ptr<Output> output;
    std::vector<Key> signers;
    std::vector<Key> recipients;

    bool sign     : 1;
    bool encrypt  : 1;
    bool ascii    : 1;
    bool detached : 1;

    QPointer<Kleo::Job> job;
};

SignEncryptFilesTask::Private::Private( SignEncryptFilesTask * qq )
    : q( qq ),
      input(),
      output(),
      signers(),
      recipients(),
      sign( true ),
      encrypt( true ),
      ascii( true ),
      detached( false ),
      job( 0 )
{

}

SignEncryptFilesTask::SignEncryptFilesTask( QObject * p )
    : Task( p ), d( new Private( this ) )
{

}

SignEncryptFilesTask::~SignEncryptFilesTask() {}

void SignEncryptFilesTask::setInput( const shared_ptr<Input> & input ) {
    assuan_assert( !d->job );
    assuan_assert( input );
    d->input = input;
}

void SignEncryptFilesTask::setOutput( const shared_ptr<Output> & output ) {
    assuan_assert( !d->job );
    assuan_assert( output );
    d->output = output;
}

void SignEncryptFilesTask::setSigners( const std::vector<Key> & signers ) {
    assuan_assert( !d->job );
    d->signers = signers;
}

void SignEncryptFilesTask::setRecipients( const std::vector<Key> & recipients ) {
    assuan_assert( !d->job );
    d->recipients = recipients;
}

void SignEncryptFilesTask::setSign( bool sign ) {
    assuan_assert( !d->job );
    d->sign = sign;
}

void SignEncryptFilesTask::setEncrypt( bool encrypt ) {
    assuan_assert( !d->job );
    d->encrypt = encrypt;
}

void SignEncryptFilesTask::setAsciiArmor( bool ascii ) {
    assuan_assert( !d->ascii );
    d->ascii = ascii;
}

void SignEncryptFilesTask::setDetachedSignature( bool detached ) {
    assuan_assert( !d->job );
    d->detached = detached;
}

Protocol SignEncryptFilesTask::protocol() const {
    if ( d->sign && !d->signers.empty() )
        return d->signers.front().protocol();
    if ( d->encrypt )
        if ( !d->recipients.empty() )
            return d->recipients.front().protocol();
        else
            return GpgME::OpenPGP; // symmetric OpenPGP encryption
    throw assuan_exception( gpg_error( GPG_ERR_INTERNAL ),
                            i18n("Cannot determine protocol for task") );
}

QString SignEncryptFilesTask::label() const {
    return d->input ? d->input->label() : QString();
}

void SignEncryptFilesTask::start() {
    assuan_assert( !d->job );
    assuan_assert( d->input );
    assuan_assert( d->output );<commit_msg>fix assertion<commit_after>/* -*- mode: c++; c-basic-offset:4 -*-
    uiserver/signencryptfilestask.cpp

    This file is part of Kleopatra, the KDE keymanager
    Copyright (c) 2007 Klarälvdalens Datakonsult AB

    Kleopatra is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Kleopatra 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

    In addition, as a special exception, the copyright holders give
    permission to link the code of this program with any edition of
    the Qt library by Trolltech AS, Norway (or with modified versions
    of Qt that use the same license as Qt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    Qt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.
*/

#include "signencryptfilestask.h"

#include "kleo-assuan.h"

#include "input.h"
#include "output.h"

#include <utils/stl_util.h>

#include <kleo/cryptobackendfactory.h>
#include <kleo/cryptobackend.h>
#include <kleo/signjob.h>
#include <kleo/signencryptjob.h>
#include <kleo/encryptjob.h>

#include <gpgme++/signingresult.h>
#include <gpgme++/encryptionresult.h>
#include <gpgme++/key.h>

#include <KLocale>

#include <QPointer>
#include <QTextDocument> // for Qt::escape

#include <boost/bind.hpp>

using namespace Kleo;
using namespace boost;
using namespace GpgME;

namespace {

    class SignEncryptFilesResult : public Task::Result {
        const SigningResult m_sresult;
        const EncryptionResult m_eresult;
    public:
        explicit SignEncryptFilesResult( const SigningResult & sr )
            : Task::Result(), m_sresult( sr ) {}
        explicit SignEncryptFilesResult( const EncryptionResult & er )
            : Task::Result(), m_eresult( er ) {}
        explicit SignEncryptFilesResult( const SigningResult & sr, const EncryptionResult & er )
            : Task::Result(), m_sresult( sr ), m_eresult( er ) {}

        /* reimp */ QString overview() const;
        /* reimp */ QString details() const;
    };


    static QString makeErrorString( const SigningResult & result ) {
        const Error err = result.error();

        assuan_assert( err || err.isCanceled() );

        if ( err.isCanceled() )
            return i18n("Signing canceled.");
        else // if ( err )
            return i18n("Signing failed: %1.", Qt::escape( QString::fromLocal8Bit( err.asString() ) ) );
    }

    static QString makeErrorString( const EncryptionResult & result ) {
        const Error err = result.error();

        assuan_assert( err || err.isCanceled() );

        if ( err.isCanceled() )
            return i18n("Encryption canceled.");
        else // if ( err )
            return i18n("Encryption failed: %1.", Qt::escape( QString::fromLocal8Bit( err.asString() ) ) );
    }

}

class SignEncryptFilesTask::Private {
    friend class ::Kleo::SignEncryptFilesTask;
    SignEncryptFilesTask * const q;
public:
    explicit Private( SignEncryptFilesTask * qq );

private:
    std::auto_ptr<Kleo::SignJob> createSignJob( GpgME::Protocol proto );
    std::auto_ptr<Kleo::SignEncryptJob> createSignEncryptJob( GpgME::Protocol proto );
    std::auto_ptr<Kleo::EncryptJob> createEncryptJob( GpgME::Protocol proto );

private:
    void slotResult( const SigningResult & );
    void slotResult( const SigningResult &, const EncryptionResult & );
    void slotResult( const EncryptionResult & );

private:
    shared_ptr<Input> input;
    shared_ptr<Output> output;
    std::vector<Key> signers;
    std::vector<Key> recipients;

    bool sign     : 1;
    bool encrypt  : 1;
    bool ascii    : 1;
    bool detached : 1;

    QPointer<Kleo::Job> job;
};

SignEncryptFilesTask::Private::Private( SignEncryptFilesTask * qq )
    : q( qq ),
      input(),
      output(),
      signers(),
      recipients(),
      sign( true ),
      encrypt( true ),
      ascii( true ),
      detached( false ),
      job( 0 )
{

}

SignEncryptFilesTask::SignEncryptFilesTask( QObject * p )
    : Task( p ), d( new Private( this ) )
{

}

SignEncryptFilesTask::~SignEncryptFilesTask() {}

void SignEncryptFilesTask::setInput( const shared_ptr<Input> & input ) {
    assuan_assert( !d->job );
    assuan_assert( input );
    d->input = input;
}

void SignEncryptFilesTask::setOutput( const shared_ptr<Output> & output ) {
    assuan_assert( !d->job );
    assuan_assert( output );
    d->output = output;
}

void SignEncryptFilesTask::setSigners( const std::vector<Key> & signers ) {
    assuan_assert( !d->job );
    d->signers = signers;
}

void SignEncryptFilesTask::setRecipients( const std::vector<Key> & recipients ) {
    assuan_assert( !d->job );
    d->recipients = recipients;
}

void SignEncryptFilesTask::setSign( bool sign ) {
    assuan_assert( !d->job );
    d->sign = sign;
}

void SignEncryptFilesTask::setEncrypt( bool encrypt ) {
    assuan_assert( !d->job );
    d->encrypt = encrypt;
}

void SignEncryptFilesTask::setAsciiArmor( bool ascii ) {
    assuan_assert( !d->job );
    d->ascii = ascii;
}

void SignEncryptFilesTask::setDetachedSignature( bool detached ) {
    assuan_assert( !d->job );
    d->detached = detached;
}

Protocol SignEncryptFilesTask::protocol() const {
    if ( d->sign && !d->signers.empty() )
        return d->signers.front().protocol();
    if ( d->encrypt )
        if ( !d->recipients.empty() )
            return d->recipients.front().protocol();
        else
            return GpgME::OpenPGP; // symmetric OpenPGP encryption
    throw assuan_exception( gpg_error( GPG_ERR_INTERNAL ),
                            i18n("Cannot determine protocol for task") );
}

QString SignEncryptFilesTask::label() const {
    return d->input ? d->input->label() : QString();
}

void SignEncryptFilesTask::start() {
    assuan_assert( !d->job );
    assuan_assert( d->input );
    assuan_assert( d->output );
{'fraction_non_alphanumeric': 0.1106743271847596, 'fraction_numerical': 0.005442999697611128, 'mean_word_length': 3.223818646232439, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1073691', 'n_tokens_mistral': 4144, 'n_tokens_neox': 4037, 'n_words': 1197}