Commit dacf4f23 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Make async save contact crypto pref

parent 6a6e69a7
......@@ -33,7 +33,9 @@ set( messagecomposer_src
helper/messagehelper.cpp
helper/messagefactory.cpp
settings/messagecomposersettings.cpp
composer/composerviewbase.cpp
composer/savecontactpreferencejob.cpp
part/messagepart.cpp
part/globalpart.cpp
......
......@@ -35,6 +35,7 @@
*/
#include "keyresolver.h"
#include "savecontactpreferencejob.h"
#ifndef QT_NO_CURSOR
#include "messageviewer/utils/kcursorsaver.h"
......@@ -1815,66 +1816,11 @@ Kleo::KeyResolver::ContactPreferences Kleo::KeyResolver::lookupContactPreference
return pref;
}
void Kleo::KeyResolver::writeCustomContactProperties( KABC::Addressee &contact, const ContactPreferences& pref ) const
{
contact.insertCustom( QLatin1String("KADDRESSBOOK"), QLatin1String("CRYPTOENCRYPTPREF"), QLatin1String( Kleo::encryptionPreferenceToString( pref.encryptionPreference ) ) );
contact.insertCustom( QLatin1String("KADDRESSBOOK"), QLatin1String("CRYPTOSIGNPREF"), QLatin1String( Kleo::signingPreferenceToString( pref.signingPreference ) ) );
contact.insertCustom( QLatin1String("KADDRESSBOOK"), QLatin1String("CRYPTOPROTOPREF"), QLatin1String( cryptoMessageFormatToString( pref.cryptoMessageFormat ) ) );
contact.insertCustom( QLatin1String("KADDRESSBOOK"), QLatin1String("OPENPGPFP"), pref.pgpKeyFingerprints.join( QLatin1String(",") ) );
contact.insertCustom( QLatin1String("KADDRESSBOOK"), QLatin1String("SMIMEFP"), pref.smimeCertFingerprints.join( QLatin1String(",") ) );
}
void Kleo::KeyResolver::saveContactPreference( const QString& email, const ContactPreferences& pref ) const
{
d->mContactPreferencesMap.insert( std::make_pair( email, pref ) );
Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob();
job->setLimit( 1 );
job->setQuery( Akonadi::ContactSearchJob::Email, email );
job->exec();
const Akonadi::Item::List items = job->items();
if ( items.isEmpty() ) {
bool ok = true;
const QString fullName = KInputDialog::getText( i18n( "Name Selection" ), i18n( "Which name shall the contact '%1' have in your address book?", email ), QString(), &ok );
if ( !ok )
return;
QPointer<Akonadi::CollectionDialog> dlg =
new Akonadi::CollectionDialog( Akonadi::CollectionDialog::KeepTreeExpanded );
dlg->setMimeTypeFilter( QStringList() << KABC::Addressee::mimeType() );
dlg->setAccessRightsFilter( Akonadi::Collection::CanCreateItem );
dlg->setDescription( i18n( "Select the address book folder to store the new contact in:" ) );
if ( !dlg->exec() ) {
delete dlg;
return;
}
const Akonadi::Collection targetCollection = dlg->selectedCollection();
delete dlg;
KABC::Addressee contact;
contact.setNameFromString( fullName );
contact.insertEmail( email, true );
writeCustomContactProperties( contact, pref );
Akonadi::Item item( KABC::Addressee::mimeType() );
item.setPayload<KABC::Addressee>( contact );
new Akonadi::ItemCreateJob( item, targetCollection );
} else {
Akonadi::Item item = items.first();
KABC::Addressee contact = item.payload<KABC::Addressee>();
writeCustomContactProperties( contact, pref );
item.setPayload<KABC::Addressee>( contact );
new Akonadi::ItemModifyJob( item );
}
// Assumption: 'pref' comes from d->mContactPreferencesMap already, no need to update that
SaveContactPreferenceJob *saveContactPreferencesJob = new SaveContactPreferenceJob(email, pref);
saveContactPreferencesJob->start();
}
Kleo::KeyResolver::ContactPreferences::ContactPreferences()
......
......@@ -141,6 +141,15 @@ public:
~KeyResolver();
struct ContactPreferences {
ContactPreferences();
Kleo::EncryptionPreference encryptionPreference;
Kleo::SigningPreference signingPreference;
Kleo::CryptoMessageFormat cryptoMessageFormat;
QStringList pgpKeyFingerprints;
QStringList smimeCertFingerprints;
};
struct Item : public KeyApprovalDialog::Item {
Item()
: KeyApprovalDialog::Item(),
......@@ -284,17 +293,8 @@ private:
return mSigningChainCertNearExpiryWarningThreshold;
}
struct ContactPreferences {
ContactPreferences();
Kleo::EncryptionPreference encryptionPreference;
Kleo::SigningPreference signingPreference;
Kleo::CryptoMessageFormat cryptoMessageFormat;
QStringList pgpKeyFingerprints;
QStringList smimeCertFingerprints;
};
ContactPreferences lookupContactPreferences( const QString& address ) const;
void writeCustomContactProperties( KABC::Addressee &contact, const ContactPreferences& pref ) const;
void saveContactPreference( const QString& email, const ContactPreferences& pref ) const;
private:
......
/*
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 "savecontactpreferencejob.h"
#include <KInputDialog>
#include <KLocalizedString>
#include <Akonadi/Contact/ContactSearchJob>
#include <Akonadi/CollectionDialog>
#include <Akonadi/ItemCreateJob>
#include <Akonadi/ItemModifyJob>
#include <QPointer>
#include <QDebug>
using namespace Kleo;
SaveContactPreferenceJob::SaveContactPreferenceJob( const QString& email, const Kleo::KeyResolver::ContactPreferences& pref, QObject *parent)
: KJob(parent),
mEmail(email),
mPref(pref)
{
}
SaveContactPreferenceJob::~SaveContactPreferenceJob()
{
}
void SaveContactPreferenceJob::start()
{
Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob(this);
connect( job, SIGNAL(result(KJob*)), this, SLOT(slotSearchContact(KJob*)) );
job->setLimit( 1 );
job->setQuery( Akonadi::ContactSearchJob::Email, mEmail );
job->start();
}
void SaveContactPreferenceJob::slotSearchContact(KJob* job)
{
Akonadi::ContactSearchJob *contactSearchJob = qobject_cast<Akonadi::ContactSearchJob *>(job);
const Akonadi::Item::List items = contactSearchJob->items();
if ( items.isEmpty() ) {
bool ok = true;
const QString fullName = KInputDialog::getText( i18n( "Name Selection" ), i18n( "Which name shall the contact '%1' have in your address book?", mEmail ), QString(), &ok );
if ( !ok ) {
emitResult();
return;
}
QPointer<Akonadi::CollectionDialog> dlg =
new Akonadi::CollectionDialog( Akonadi::CollectionDialog::KeepTreeExpanded );
dlg->setMimeTypeFilter( QStringList() << KABC::Addressee::mimeType() );
dlg->setAccessRightsFilter( Akonadi::Collection::CanCreateItem );
dlg->setDescription( i18n( "Select the address book folder to store the new contact in:" ) );
if ( !dlg->exec() ) {
delete dlg;
emitResult();
return;
}
const Akonadi::Collection targetCollection = dlg->selectedCollection();
delete dlg;
KABC::Addressee contact;
contact.setNameFromString( fullName );
contact.insertEmail( mEmail, true );
writeCustomContactProperties( contact, mPref );
Akonadi::Item item( KABC::Addressee::mimeType() );
item.setPayload<KABC::Addressee>( contact );
Akonadi::ItemCreateJob *job = new Akonadi::ItemCreateJob( item, targetCollection );
connect(job, SIGNAL(result(KJob*)), SLOT(slotModifyCreateItem(KJob*)));
} else {
Akonadi::Item item = items.first();
KABC::Addressee contact = item.payload<KABC::Addressee>();
writeCustomContactProperties( contact, mPref );
item.setPayload<KABC::Addressee>( contact );
Akonadi::ItemModifyJob *job = new Akonadi::ItemModifyJob( item );
connect(job, SIGNAL(result(KJob*)), SLOT(slotModifyCreateItem(KJob*)));
job->start();
}
}
void SaveContactPreferenceJob::writeCustomContactProperties( KABC::Addressee &contact, const Kleo::KeyResolver::ContactPreferences& pref ) const
{
contact.insertCustom( QLatin1String("KADDRESSBOOK"), QLatin1String("CRYPTOENCRYPTPREF"), QLatin1String( Kleo::encryptionPreferenceToString( pref.encryptionPreference ) ) );
contact.insertCustom( QLatin1String("KADDRESSBOOK"), QLatin1String("CRYPTOSIGNPREF"), QLatin1String( Kleo::signingPreferenceToString( pref.signingPreference ) ) );
contact.insertCustom( QLatin1String("KADDRESSBOOK"), QLatin1String("CRYPTOPROTOPREF"), QLatin1String( cryptoMessageFormatToString( pref.cryptoMessageFormat ) ) );
contact.insertCustom( QLatin1String("KADDRESSBOOK"), QLatin1String("OPENPGPFP"), pref.pgpKeyFingerprints.join( QLatin1String(",") ) );
contact.insertCustom( QLatin1String("KADDRESSBOOK"), QLatin1String("SMIMEFP"), pref.smimeCertFingerprints.join( QLatin1String(",") ) );
}
void SaveContactPreferenceJob::slotModifyCreateItem(KJob *job)
{
if ( job->error() ) {
qDebug()<<"modify item failed "<<job->errorString();
}
emitResult();
}
/*
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 SAVECONTACTPREFERENCEJOB_H
#define SAVECONTACTPREFERENCEJOB_H
#include <KJob>
#include <KABC/Addressee>
#include "keyresolver.h"
namespace Kleo {
class SaveContactPreferenceJob : public KJob
{
Q_OBJECT
public:
explicit SaveContactPreferenceJob(const QString &email, const Kleo::KeyResolver::ContactPreferences &pref, QObject *parent=0);
~SaveContactPreferenceJob();
void start();
private slots:
void slotSearchContact(KJob *job);
void slotModifyCreateItem(KJob *job);
private:
void writeCustomContactProperties(KABC::Addressee &contact, const Kleo::KeyResolver::ContactPreferences &pref) const;
QString mEmail;
Kleo::KeyResolver::ContactPreferences mPref;
};
}
#endif // SAVECONTACTPREFERENCEJOB_H
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