2 Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
4 Based on KMail code by:
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of
10 the License or (at your option) version 3 or any later version
11 accepted by the membership of KDE e.V. (or its successor approved
12 by the membership of KDE e.V.), which shall act as a proxy
13 defined in Section 14 of version 3 of the license.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "attachmentfrompublickeyjob.h"
26 #include <boost/shared_ptr.hpp>
29 #include <kdialogjobuidelegate.h> // FIXME add CamelCase header in kdelibs/kdeui
30 #include <KLocalizedString>
32 #include <kleo/cryptobackendfactory.h>
33 #include <kleo/exportjob.h>
34 #include <libkleo/ui/progressdialog.h>
36 using namespace Message
;
39 class Message::AttachmentFromPublicKeyJob::Private
42 Private( AttachmentFromPublicKeyJob
*qq
);
44 void exportResult( const GpgME::Error
&error
, const QByteArray
&keyData
); // slot
45 void emitGpgError( const GpgME::Error
&error
);
47 AttachmentFromPublicKeyJob
*const q
;
52 AttachmentFromPublicKeyJob::Private::Private( AttachmentFromPublicKeyJob
*qq
)
57 void AttachmentFromPublicKeyJob::Private::exportResult( const GpgME::Error
&error
, const QByteArray
&keyData
)
60 emitGpgError( error
);
64 // Create the AttachmentPart.
65 AttachmentPart::Ptr part
= AttachmentPart::Ptr( new AttachmentPart
);
66 part
->setName( i18n( "OpenPGP key 0x%1", fingerprint
) );
67 part
->setFileName( QString::fromLatin1( "0x" + fingerprint
.toLatin1() + ".asc" ) );
68 part
->setMimeType( "application/pgp-keys" );
69 part
->setData( keyData
);
71 q
->setAttachmentPart( part
);
72 q
->emitResult(); // Success.
75 void AttachmentFromPublicKeyJob::Private::emitGpgError( const GpgME::Error
&error
)
78 const QString msg
= i18n( "<p>An error occurred while trying to export "
79 "the key from the backend:</p>"
81 QString::fromLocal8Bit( error
.asString() ) );
82 q
->setError( KJob::UserDefinedError
);
83 q
->setErrorText( msg
);
89 AttachmentFromPublicKeyJob::AttachmentFromPublicKeyJob( const QString
&fingerprint
, QObject
*parent
)
90 : AttachmentLoadJob( parent
)
91 , d( new Private( this ) )
93 d
->fingerprint
= fingerprint
;
96 AttachmentFromPublicKeyJob::~AttachmentFromPublicKeyJob()
101 QString
AttachmentFromPublicKeyJob::fingerprint() const
103 return d
->fingerprint
;
106 void AttachmentFromPublicKeyJob::setFingerprint( const QString
&fingerprint
)
108 d
->fingerprint
= fingerprint
;
111 void AttachmentFromPublicKeyJob::doStart()
113 Kleo::ExportJob
*job
= Kleo::CryptoBackendFactory::instance()->openpgp()->publicKeyExportJob( true );
115 QObject::connect( job
, SIGNAL(result(GpgME::Error
,QByteArray
)),
116 this, SLOT(exportResult(GpgME::Error
,QByteArray
)) );
118 const GpgME::Error error
= job
->start( QStringList( d
->fingerprint
) );
120 d
->emitGpgError( error
);
121 // TODO check autodeletion policy of Kleo::Jobs...
123 } else if( uiDelegate() ) {
124 Q_ASSERT( dynamic_cast<KDialogJobUiDelegate
*>( uiDelegate() ) );
125 KDialogJobUiDelegate
*delegate
= static_cast<KDialogJobUiDelegate
*>( uiDelegate() );
126 (void)new Kleo::ProgressDialog( job
, i18n( "Exporting key..." ),
127 delegate
->window() );
131 #include "attachmentfrompublickeyjob.moc"