Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 2d098e39 authored by Laurent Montel's avatar Laurent Montel 😁

implement attach vcard from addressbook

parent 73359eed
......@@ -39,6 +39,7 @@ set( messagecomposer_job_src
job/attachmentfrompublickeyjob.cpp
job/distributionlistexpandjob.cpp
job/savecontactpreferencejob.cpp
job/attachmentvcardfromaddressbookjob.cpp
)
set( messagecomposer_composer_src
......
......@@ -17,13 +17,18 @@
#include "attachmentvcardfromaddressbookjob.h"
#include "messagecomposer/utils/util.h"
#include <KLocalizedString>
#include <KABC/Addressee>
#include <KABC/ContactGroup>
#include <KABC/VCardConverter>
#include <akonadi/contact/contactgroupexpandjob.h>
using namespace MessageComposer;
AttachmentVcardFromAddressBookJob::AttachmentVcardFromAddressBookJob(Akonadi::Item::Id id, QObject *parent)
AttachmentVcardFromAddressBookJob::AttachmentVcardFromAddressBookJob(const Akonadi::Item &item, QObject *parent)
: MessageCore::AttachmentLoadJob(parent),
mId(id)
mItem(item)
{
}
......@@ -33,16 +38,64 @@ AttachmentVcardFromAddressBookJob::~AttachmentVcardFromAddressBookJob()
}
void AttachmentVcardFromAddressBookJob::doStart()
void AttachmentVcardFromAddressBookJob::addAttachment(const QByteArray &data, const QString &attachmentName)
{
Akonadi::Item item(id);
if (item.isValid()) {
MessageCore::AttachmentPart::Ptr attachment = MessageCore::AttachmentPart::Ptr( new MessageCore::AttachmentPart() );
if( !data.isEmpty() ) {
attachment->setName( attachmentName );
attachment->setFileName( attachmentName );
attachment->setData( data );
attachment->setMimeType( "text/x-vcard" );
// TODO what about the other fields?
}
setAttachmentPart( attachment );
emitResult(); // Success.
}
void AttachmentVcardFromAddressBookJob::doStart()
{
if (mItem.isValid()) {
if ( mItem.hasPayload<KABC::Addressee>() ) {
const KABC::Addressee contact = mItem.payload<KABC::Addressee>();
QString attachmentName = contact.realName() + QLatin1String( ".vcf" );
//Workaround about broken kaddressbook fields.
QByteArray data = mItem.payloadData();
MessageComposer::Util::adaptVcard(data);
addAttachment( data, attachmentName );
} else if ( mItem.hasPayload<KABC::ContactGroup>() ) {
const KABC::ContactGroup group = mItem.payload<KABC::ContactGroup>();
QString attachmentName = group.name() + QLatin1String( ".vcf" );
Akonadi::ContactGroupExpandJob *expandJob = new Akonadi::ContactGroupExpandJob( group, this );
expandJob->setProperty("groupName", attachmentName);
connect( expandJob, SIGNAL(result(KJob*)), this, SLOT(slotExpandGroupResult(KJob*)) );
expandJob->start();
} else {
//TODO define error message
setError( KJob::UserDefinedError );
//q->setErrorText( msg );
emitResult();
}
} else {
//TODO define error message
q->setError( KJob::UserDefinedError );
setError( KJob::UserDefinedError );
//q->setErrorText( msg );
q->emitResult();
emitResult();
}
}
void AttachmentVcardFromAddressBookJob::slotExpandGroupResult(KJob* job)
{
Akonadi::ContactGroupExpandJob *expandJob = qobject_cast<Akonadi::ContactGroupExpandJob*>( job );
Q_ASSERT( expandJob );
const QString attachmentName = expandJob->property("groupName").toString();
KABC::VCardConverter converter;
const QByteArray groupData = converter.exportVCards(expandJob->contacts(), KABC::VCardConverter::v3_0);
if (!groupData.isEmpty()) {
addAttachment( groupData, attachmentName );
} else {
setError( KJob::UserDefinedError );
//q->setErrorText( msg );
emitResult();
}
}
......@@ -26,14 +26,18 @@ class AttachmentVcardFromAddressBookJob : public MessageCore::AttachmentLoadJob
{
Q_OBJECT
public:
explicit AttachmentVcardFromAddressBookJob(Akonadi::Item::Id id, QObject *parent = 0);
explicit AttachmentVcardFromAddressBookJob(const Akonadi::Item &item, QObject *parent = 0);
~AttachmentVcardFromAddressBookJob();
protected slots:
virtual void doStart();
private Q_SLOTS:
void slotExpandGroupResult(KJob *job);
private:
Akonadi::Item::Id mId;
void addAttachment(const QByteArray &data, const QString &attachmentName);
Akonadi::Item mItem;
};
}
#endif // ATTACHMENTVCARDFROMADDRESSBOOKJOB_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