savecontactpreferencejob.cpp 4.6 KB
Newer Older
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
/*
  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*)));
    }
}

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();
}