Commit 9cd9579e authored by Laurent Montel's avatar Laurent Montel 😁

Extract attachment encrypt with chiasmus as a job

parent f306d7d4
......@@ -158,6 +158,7 @@ set(libmessageviewer_antispam_SRCS
set(libmessageviewer_job_SRCS
job/createtodojob.cpp
job/createeventjob.cpp
job/attachmentencryptwithchiasmusjob.cpp
)
set(libmessageviewer_SRCS
......
/*
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 "attachmentencryptwithchiasmusjob.h"
#include "viewer/chiasmuskeyselector.h"
#include "utils/util.h"
#include "settings/globalsettings.h"
#include "utils/autoqpointer.h"
#include <gpgme++/error.h>
#include <kio/jobuidelegate.h>
#include <KIO/Job>
#include <KMessageBox>
#include <KLocalizedString>
#include <KFileDialog>
#include <kleo/cryptobackendfactory.h>
#include <kleo/cryptobackend.h>
#include <kleo/specialjob.h>
using namespace MessageViewer;
static const QString chomp( const QString & base, const QString & suffix, bool cs )
{
return base.endsWith( suffix, cs ? (Qt::CaseSensitive) : (Qt::CaseInsensitive) ) ? base.left( base.length() - suffix.length() ) : base ;
}
AttachmentEncryptWithChiasmusJob::AttachmentEncryptWithChiasmusJob(QObject *parent)
: QObject(parent),
mJob(0),
mMainWindow(0),
mContent(0)
{
}
AttachmentEncryptWithChiasmusJob::~AttachmentEncryptWithChiasmusJob()
{
}
void AttachmentEncryptWithChiasmusJob::setContent(KMime::Content *content)
{
mContent = content;
}
void AttachmentEncryptWithChiasmusJob::setCurrentFileName(const QString &currentFileName)
{
mCurrentFileName = currentFileName;
}
void AttachmentEncryptWithChiasmusJob::setMainWindow(QWidget *mainWindow)
{
mMainWindow = mainWindow;
}
void AttachmentEncryptWithChiasmusJob::start()
{
Q_UNUSED( mContent );
// FIXME: better detection of mimetype??
if ( !mCurrentFileName.endsWith( QLatin1String(".xia"), Qt::CaseInsensitive ) ) {
deleteLater();
return;
}
const Kleo::CryptoBackend::Protocol * chiasmus =
Kleo::CryptoBackendFactory::instance()->protocol( "Chiasmus" );
Q_ASSERT( chiasmus );
if ( !chiasmus ) {
deleteLater();
return;
}
const std::auto_ptr<Kleo::SpecialJob> listjob( chiasmus->specialJob( "x-obtain-keys", QMap<QString,QVariant>() ) );
if ( !listjob.get() ) {
const QString msg = i18n( "Chiasmus backend does not offer the "
"\"x-obtain-keys\" function. Please report this bug." );
KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
deleteLater();
return;
}
if ( listjob->exec() ) {
listjob->showErrorDialog( mMainWindow, i18n( "Chiasmus Backend Error" ) );
deleteLater();
return;
}
const QVariant result = listjob->property( "result" );
if ( result.type() != QVariant::StringList ) {
const QString msg = i18n( "Unexpected return value from Chiasmus backend: "
"The \"x-obtain-keys\" function did not return a "
"string list. Please report this bug." );
KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
deleteLater();
return;
}
const QStringList keys = result.toStringList();
if ( keys.empty() ) {
const QString msg = i18n( "No keys have been found. Please check that a "
"valid key path has been set in the Chiasmus "
"configuration." );
KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
deleteLater();
return;
}
AutoQPointer<ChiasmusKeySelector> selectorDlg( new ChiasmusKeySelector( mMainWindow,
i18n( "Chiasmus Decryption Key Selection" ),
keys, GlobalSettings::chiasmusDecryptionKey(),
GlobalSettings::chiasmusDecryptionOptions() ) );
if ( selectorDlg->exec() != QDialog::Accepted || !selectorDlg ) {
deleteLater();
return;
}
GlobalSettings::setChiasmusDecryptionOptions( selectorDlg->options() );
GlobalSettings::setChiasmusDecryptionKey( selectorDlg->key() );
assert( !GlobalSettings::chiasmusDecryptionKey().isEmpty() );
Kleo::SpecialJob * job = chiasmus->specialJob( "x-decrypt", QMap<QString,QVariant>() );
if ( !job ) {
const QString msg = i18n( "Chiasmus backend does not offer the "
"\"x-decrypt\" function. Please report this bug." );
KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
deleteLater();
return;
}
//PORT IT
const QByteArray input;// = node->msgPart().bodyDecodedBinary();
if ( !job->setProperty( "key", GlobalSettings::chiasmusDecryptionKey() ) ||
!job->setProperty( "options", GlobalSettings::chiasmusDecryptionOptions() ) ||
!job->setProperty( "input", input ) ) {
const QString msg = i18n( "The \"x-decrypt\" function does not accept "
"the expected parameters. Please report this bug." );
KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
deleteLater();
return;
}
if ( job->start() ) {
job->showErrorDialog( mMainWindow, i18n( "Chiasmus Decryption Error" ) );
deleteLater();
return;
}
mJob = job;
connect( job, SIGNAL(result(GpgME::Error,QVariant)),
this, SLOT(slotAtmDecryptWithChiasmusResult(GpgME::Error,QVariant)) );
}
void AttachmentEncryptWithChiasmusJob::slotAtmDecryptWithChiasmusResult( const GpgME::Error & err, const QVariant & result )
{
if ( !mJob ) {
deleteLater();
return;
}
Q_ASSERT( mJob == sender() );
if ( mJob != sender() ) {
deleteLater();
return;
}
Kleo::Job * job = mJob;
mJob = 0;
if ( err.isCanceled() ) {
deleteLater();
return;
}
if ( err ) {
job->showErrorDialog( mMainWindow, i18n( "Chiasmus Decryption Error" ) );
deleteLater();
return;
}
if ( result.type() != QVariant::ByteArray ) {
const QString msg = i18n( "Unexpected return value from Chiasmus backend: "
"The \"x-decrypt\" function did not return a "
"byte array. Please report this bug." );
KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
deleteLater();
return;
}
const KUrl url = KFileDialog::getSaveUrl( chomp( mCurrentFileName, QLatin1String(".xia"), false ), QString(), mMainWindow );
if ( url.isEmpty() ) {
deleteLater();
return;
}
bool overwrite = MessageViewer::Util::checkOverwrite( url, mMainWindow );
if ( !overwrite ) {
deleteLater();
return;
}
KIO::Job * uploadJob = KIO::storedPut( result.toByteArray(), url, -1, KIO::Overwrite );
uploadJob->ui()->setWindow( mMainWindow );
connect( uploadJob, SIGNAL(result(KJob*)),
this, SLOT(slotAtmDecryptWithChiasmusUploadResult(KJob*)) );
}
void AttachmentEncryptWithChiasmusJob::slotAtmDecryptWithChiasmusUploadResult( KJob * job )
{
if ( job->error() )
static_cast<KIO::Job*>(job)->ui()->showErrorMessage();
deleteLater();
}
/*
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
*/
#ifndef ATTACHMENTENCRYPTWITHCHIASMUSJOB_H
#define ATTACHMENTENCRYPTWITHCHIASMUSJOB_H
#include <QObject>
namespace KMime {
class Content;
}
namespace Kleo {
class SpecialJob;
}
namespace GpgME {
class Error;
}
class KJob;
namespace MessageViewer {
class AttachmentEncryptWithChiasmusJob : public QObject
{
Q_OBJECT
public:
explicit AttachmentEncryptWithChiasmusJob(QObject *parent = 0);
~AttachmentEncryptWithChiasmusJob();
void start();
void setContent(KMime::Content *content);
void setCurrentFileName(const QString &currentFileName);
void setMainWindow(QWidget *mainWindow);
private slots:
void slotAtmDecryptWithChiasmusResult(const GpgME::Error &err, const QVariant &result);
void slotAtmDecryptWithChiasmusUploadResult(KJob *job);
private:
QString mCurrentFileName;
Kleo::SpecialJob *mJob;
QWidget *mMainWindow;
KMime::Content *mContent;
};
}
#endif // ATTACHMENTENCRYPTWITHCHIASMUSJOB_H
......@@ -37,6 +37,7 @@
#include "viewer/mimeparttreeview.h"
#include "widgets/openattachmentfolderwidget.h"
#include "pimcommon/widgets/slidecontainer.h"
#include "messageviewer/job/attachmentencryptwithchiasmusjob.h"
#ifdef MESSAGEVIEWER_READER_HTML_DEBUG
#include "htmlwriter/filehtmlwriter.h"
......@@ -113,9 +114,7 @@
#include <akonadi/kmime/specialmailcollections.h>
#include <akonadi/attributefactory.h>
#include <Akonadi/KMime/MessageParts>
#include <kleo/specialjob.h>
#include "chiasmuskeyselector.h"
#include "utils/autoqpointer.h"
......@@ -147,9 +146,6 @@
#include <messagecore/utils/stringutil.h>
#include <kio/jobuidelegate.h>
#include <gpgme++/error.h>
#include <messagecore/helpers/nodehelper.h>
#include "messagecore/settings/globalsettings.h"
#include <akonadi/agentinstance.h>
......@@ -214,7 +210,6 @@ ViewerPrivate::ViewerPrivate(Viewer *aParent, QWidget *mainWindow,
mRecursionCountForDisplayMessage( 0 ),
mCurrentContent( 0 ),
mMessagePartNode( 0 ),
mJob( 0 ),
q( aParent ),
mShowFullToAddressList( true ),
mShowFullCcAddressList( true ),
......@@ -2914,138 +2909,11 @@ void ViewerPrivate::setUseFixedFont( bool useFixedFont )
void ViewerPrivate::attachmentEncryptWithChiasmus( KMime::Content *content )
{
Q_UNUSED( content );
// FIXME: better detection of mimetype??
if ( !mCurrentFileName.endsWith( QLatin1String(".xia"), Qt::CaseInsensitive ) )
return;
const Kleo::CryptoBackend::Protocol * chiasmus =
Kleo::CryptoBackendFactory::instance()->protocol( "Chiasmus" );
Q_ASSERT( chiasmus );
if ( !chiasmus )
return;
const std::auto_ptr<Kleo::SpecialJob> listjob( chiasmus->specialJob( "x-obtain-keys", QMap<QString,QVariant>() ) );
if ( !listjob.get() ) {
const QString msg = i18n( "Chiasmus backend does not offer the "
"\"x-obtain-keys\" function. Please report this bug." );
KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
return;
}
if ( listjob->exec() ) {
listjob->showErrorDialog( mMainWindow, i18n( "Chiasmus Backend Error" ) );
return;
}
const QVariant result = listjob->property( "result" );
if ( result.type() != QVariant::StringList ) {
const QString msg = i18n( "Unexpected return value from Chiasmus backend: "
"The \"x-obtain-keys\" function did not return a "
"string list. Please report this bug." );
KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
return;
}
const QStringList keys = result.toStringList();
if ( keys.empty() ) {
const QString msg = i18n( "No keys have been found. Please check that a "
"valid key path has been set in the Chiasmus "
"configuration." );
KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
return;
}
AutoQPointer<ChiasmusKeySelector> selectorDlg( new ChiasmusKeySelector( mMainWindow,
i18n( "Chiasmus Decryption Key Selection" ),
keys, GlobalSettings::chiasmusDecryptionKey(),
GlobalSettings::chiasmusDecryptionOptions() ) );
if ( selectorDlg->exec() != QDialog::Accepted || !selectorDlg ) {
return;
}
GlobalSettings::setChiasmusDecryptionOptions( selectorDlg->options() );
GlobalSettings::setChiasmusDecryptionKey( selectorDlg->key() );
assert( !GlobalSettings::chiasmusDecryptionKey().isEmpty() );
Kleo::SpecialJob * job = chiasmus->specialJob( "x-decrypt", QMap<QString,QVariant>() );
if ( !job ) {
const QString msg = i18n( "Chiasmus backend does not offer the "
"\"x-decrypt\" function. Please report this bug." );
KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
return;
}
//PORT IT
const QByteArray input;// = node->msgPart().bodyDecodedBinary();
if ( !job->setProperty( "key", GlobalSettings::chiasmusDecryptionKey() ) ||
!job->setProperty( "options", GlobalSettings::chiasmusDecryptionOptions() ) ||
!job->setProperty( "input", input ) ) {
const QString msg = i18n( "The \"x-decrypt\" function does not accept "
"the expected parameters. Please report this bug." );
KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
return;
}
if ( job->start() ) {
job->showErrorDialog( mMainWindow, i18n( "Chiasmus Decryption Error" ) );
return;
}
mJob = job;
connect( job, SIGNAL(result(GpgME::Error,QVariant)),
this, SLOT(slotAtmDecryptWithChiasmusResult(GpgME::Error,QVariant)) );
}
static const QString chomp( const QString & base, const QString & suffix, bool cs )
{
return base.endsWith( suffix, cs ? (Qt::CaseSensitive) : (Qt::CaseInsensitive) ) ? base.left( base.length() - suffix.length() ) : base ;
}
void ViewerPrivate::slotAtmDecryptWithChiasmusResult( const GpgME::Error & err, const QVariant & result )
{
if ( !mJob )
return;
Q_ASSERT( mJob == sender() );
if ( mJob != sender() )
return;
Kleo::Job * job = mJob;
mJob = 0;
if ( err.isCanceled() )
return;
if ( err ) {
job->showErrorDialog( mMainWindow, i18n( "Chiasmus Decryption Error" ) );
return;
}
if ( result.type() != QVariant::ByteArray ) {
const QString msg = i18n( "Unexpected return value from Chiasmus backend: "
"The \"x-decrypt\" function did not return a "
"byte array. Please report this bug." );
KMessageBox::error( mMainWindow, msg, i18n( "Chiasmus Backend Error" ) );
return;
}
const KUrl url = KFileDialog::getSaveUrl( chomp( mCurrentFileName, QLatin1String(".xia"), false ), QString(), mMainWindow );
if ( url.isEmpty() )
return;
bool overwrite = Util::checkOverwrite( url, mMainWindow );
if ( !overwrite )
return;
KIO::Job * uploadJob = KIO::storedPut( result.toByteArray(), url, -1, KIO::Overwrite );
uploadJob->ui()->setWindow( mMainWindow );
connect( uploadJob, SIGNAL(result(KJob*)),
this, SLOT(slotAtmDecryptWithChiasmusUploadResult(KJob*)) );
}
void ViewerPrivate::slotAtmDecryptWithChiasmusUploadResult( KJob * job )
{
if ( job->error() )
static_cast<KIO::Job*>(job)->ui()->showErrorMessage();
MessageViewer::AttachmentEncryptWithChiasmusJob *job = new MessageViewer::AttachmentEncryptWithChiasmusJob(this);
job->setContent(content);
job->setCurrentFileName(mCurrentFileName);
job->setMainWindow(mMainWindow);
job->start();
}
bool ViewerPrivate::showFullToAddressList() const
......
......@@ -38,16 +38,10 @@
#include <QTimer>
#include <QWebElement>
namespace GpgME { class Error; }
namespace KIO {
class Job;
}
namespace Kleo
{
class SpecialJob;
}
class KAction;
class KActionCollection;
class KSelectAction;
......@@ -466,8 +460,6 @@ private slots:
void slotMessageIsNotAScam();
void slotAddToWhiteList();
void slotToggleCaretBrowsing(bool);
void slotAtmDecryptWithChiasmusResult( const GpgME::Error &, const QVariant & );
void slotAtmDecryptWithChiasmusUploadResult( KJob * );
/** Show hide all fields specified inside this function */
void toggleFullAddressList();
......@@ -716,7 +708,6 @@ public:
QString mCurrentFileName;
QString mMessagePath;
QMap<EditorWatcher*, KMime::Content*> mEditorWatchers;
Kleo::SpecialJob *mJob;
Viewer *const q;
bool mShowFullToAddressList;
bool mShowFullCcAddressList;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment