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 192520f4 authored by Laurent Montel's avatar Laurent Montel 😁

Improve++

parent 7e7f8e35
......@@ -20,6 +20,7 @@
#include "sendvcardsjobtest.h"
#include "sendvcards/sendvcardsjob.h"
#include <Akonadi/Item>
#include <qtest_kde.h>
SendVcardsJobTest::SendVcardsJobTest(QObject *parent)
: QObject(parent)
......@@ -32,4 +33,11 @@ SendVcardsJobTest::~SendVcardsJobTest()
}
void SendVcardsJobTest::shouldNotStartWhenListAddressIsEmpty()
{
Akonadi::Item::List lst;
KABSendVCards::SendVcardsJob *job = new KABSendVCards::SendVcardsJob(lst);
QVERIFY(!job->start());
}
QTEST_KDEMAIN(SendVcardsJobTest, NoGUI)
......@@ -29,6 +29,8 @@ class SendVcardsJobTest : public QObject
public:
explicit SendVcardsJobTest(QObject *parent = 0);
~SendVcardsJobTest();
private Q_SLOTS:
void shouldNotStartWhenListAddressIsEmpty();
};
#endif // SENDVCARDSJOBTEST_H
......@@ -21,11 +21,14 @@
#include "sendvcardsjob.h"
#include <KABC/Addressee>
#include <KABC/ContactGroup>
#include <KABC/VCardConverter>
#include <Akonadi/Item>
#include <Akonadi/ItemFetchJob>
#include <Akonadi/ItemFetchScope>
#include <QDebug>
#include <util/vcardutil.h>
#include <Akonadi/Contact/ContactGroupExpandJob>
#include "pimcommon/temporaryfile/attachmenttemporaryfilesdirs.h"
using namespace KABSendVCards;
......@@ -34,20 +37,21 @@ SendVcardsJob::SendVcardsJob(const Akonadi::Item::List &listItem, QObject *paren
mListItem(listItem),
mFetchJobCount(0)
{
//Don't delete it.
mAttachmentTemporary = new PimCommon::AttachmentTemporaryFilesDirs();
}
SendVcardsJob::~SendVcardsJob()
{
mAttachmentTemporary = 0;
}
void SendVcardsJob::start()
bool SendVcardsJob::start()
{
if (mListItem.isEmpty()) {
qDebug()<<" No Item found";
deleteLater();
return;
return false;
}
Q_FOREACH (const Akonadi::Item &item, mListItem) {
......@@ -56,83 +60,44 @@ void SendVcardsJob::start()
//Workaround about broken kaddressbook fields.
PimCommon::VCardUtil vcardUtil;
vcardUtil.adaptVcard(data);
//TODO
} else if (item.hasPayload<KABC::ContactGroup>()) {
++mFetchJobCount;
const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
unsigned int nbDataCount(group.dataCount());
for(unsigned int i=0; i<nbDataCount; ++i) {
const QString currentEmail(group.data(i).email());
//TODO
}
const unsigned int nbContactReference(group.contactReferenceCount());
for(unsigned int i=0; i<nbContactReference; ++i){
KABC::ContactGroup::ContactReference reference = group.contactReference(i);
Akonadi::Item item;
if (reference.gid().isEmpty()) {
item.setId( reference.uid().toLongLong() );
} else {
item.setGid( reference.gid() );
}
mItemToFetch << item;
}
const QString groupName(group.name());
const QString attachmentName = ( groupName.isEmpty() ? QLatin1String("vcard") : groupName ) + 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();
}
}
if(mItemToFetch.isEmpty()) {
finishJob();
} else {
fetchNextItem();
if (mFetchJobCount == 0) {
jobFinished();
}
return true;
}
void SendVcardsJob::fetchNextItem()
void SendVcardsJob::jobFinished()
{
if (mFetchJobCount < mItemToFetch.count()) {
fetchItem(mItemToFetch.at(mFetchJobCount));
++mFetchJobCount;
} else {
finishJob();
if (!mPath.isEmpty()) {
//TODO
}
deleteLater();
}
void SendVcardsJob::fetchItem(const Akonadi::Item &item)
void SendVcardsJob::slotExpandGroupResult(KJob* job)
{
Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( item, this );
job->fetchScope().fetchFullPayload();
Akonadi::ContactGroupExpandJob *expandJob = qobject_cast<Akonadi::ContactGroupExpandJob*>( job );
Q_ASSERT( expandJob );
connect( job, SIGNAL(result(KJob*)), SLOT(fetchJobFinished(KJob*)) );
}
void SendVcardsJob::fetchJobFinished(KJob *job)
{
if ( job->error() ) {
qDebug()<<" error during fetching "<<job->errorString();
fetchNextItem();
return;
}
Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob*>( job );
if ( fetchJob->items().count() != 1 ) {
fetchNextItem();
return;
}
const Akonadi::Item item = fetchJob->items().first();
const KABC::Addressee contact = item.payload<KABC::Addressee>();
const QString attachmentName = expandJob->property("groupName").toString();
KABC::VCardConverter converter;
const QByteArray groupData = converter.exportVCards(expandJob->contacts(), KABC::VCardConverter::v3_0);
//TODO
fetchNextItem();
}
void SendVcardsJob::finishJob()
{
#if 0
if (!mEmailAddresses.isEmpty()) {
//emit sendMails(mEmailAddresses);
--mFetchJobCount;
if (mFetchJobCount == 0) {
jobFinished();
}
#endif
deleteLater();
}
......@@ -20,28 +20,34 @@
#ifndef SENDVCARDSJOB_H
#define SENDVCARDSJOB_H
#include "kaddressbook_export.h"
#include <QObject>
#include <Akonadi/Item>
#include <QStringList>
namespace PimCommon {
class AttachmentTemporaryFilesDirs;
}
namespace KABSendVCards {
class SendVcardsJob : public QObject
class KADDRESSBOOK_EXPORT SendVcardsJob : public QObject
{
Q_OBJECT
public:
explicit SendVcardsJob(const Akonadi::Item::List &listItem, QObject *parent = 0);
~SendVcardsJob();
void start();
bool start();
Q_SIGNALS:
void jobDone();
private slots:
void fetchJobFinished(KJob *job);
private Q_SLOTS:
void slotExpandGroupResult(KJob *job);
private:
void fetchNextItem();
void fetchItem(const Akonadi::Item &item);
void finishJob();
void jobFinished();
Akonadi::Item::List mListItem;
Akonadi::Item::List mItemToFetch;
QStringList mPath;
PimCommon::AttachmentTemporaryFilesDirs *mAttachmentTemporary;
int mFetchJobCount;
};
}
......
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