File indexing completed on 2024-11-24 04:43:04

0001 /*
0002     SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "mergecontactsjob.h"
0008 #include "mergecontacts.h"
0009 #include <Akonadi/ItemCreateJob>
0010 #include <Akonadi/ItemDeleteJob>
0011 #include <KContacts/Address>
0012 
0013 #include "kaddressbookmergelibprivate_debug.h"
0014 
0015 using namespace KABMergeContacts;
0016 
0017 MergeContactsJob::MergeContactsJob(QObject *parent)
0018     : QObject(parent)
0019 {
0020 }
0021 
0022 MergeContactsJob::~MergeContactsJob() = default;
0023 
0024 bool MergeContactsJob::canStart() const
0025 {
0026     if (!mCollection.isValid()) {
0027         qCDebug(KADDRESSBOOKMERGELIBPRIVATE_LOG) << " mCollection is not valid !";
0028         return false;
0029     }
0030     if (mNewContact.isEmpty()) {
0031         if (mListItem.isEmpty()) {
0032             qCDebug(KADDRESSBOOKMERGELIBPRIVATE_LOG) << " list item is empty !";
0033             return false;
0034         }
0035     }
0036     return true;
0037 }
0038 
0039 void MergeContactsJob::start()
0040 {
0041     if (!canStart()) {
0042         Q_EMIT finished(mCreatedContact);
0043         deleteLater();
0044         return;
0045     }
0046     generateMergedContact();
0047 }
0048 
0049 void MergeContactsJob::generateMergedContact()
0050 {
0051     if (mNewContact.isEmpty()) {
0052         MergeContacts mergeContact(mListItem);
0053         const KContacts::Addressee newContact = mergeContact.mergedContact();
0054         if (newContact.isEmpty()) {
0055             Q_EMIT finished(mCreatedContact);
0056             deleteLater();
0057             return;
0058         }
0059         createMergedContact(newContact);
0060     } else {
0061         createMergedContact(mNewContact);
0062     }
0063 }
0064 
0065 void MergeContactsJob::setNewContact(const KContacts::Addressee &addr)
0066 {
0067     mNewContact = addr;
0068 }
0069 
0070 void MergeContactsJob::setListItem(const Akonadi::Item::List &lstItem)
0071 {
0072     mListItem = lstItem;
0073 }
0074 
0075 void MergeContactsJob::setDestination(const Akonadi::Collection &collection)
0076 {
0077     mCollection = collection;
0078 }
0079 
0080 void MergeContactsJob::createMergedContact(const KContacts::Addressee &addressee)
0081 {
0082     Akonadi::Item item;
0083     item.setMimeType(KContacts::Addressee::mimeType());
0084     item.setPayload<KContacts::Addressee>(addressee);
0085 
0086     auto job = new Akonadi::ItemCreateJob(item, mCollection, this);
0087     connect(job, &Akonadi::ItemCreateJob::result, this, &MergeContactsJob::slotCreateMergedContactFinished);
0088 }
0089 
0090 void MergeContactsJob::slotCreateMergedContactFinished(KJob *job)
0091 {
0092     if (job->error()) {
0093         qCDebug(KADDRESSBOOKMERGELIBPRIVATE_LOG) << job->errorString();
0094         Q_EMIT finished(mCreatedContact);
0095         deleteLater();
0096         return;
0097     }
0098     auto createdJob = qobject_cast<Akonadi::ItemCreateJob *>(job);
0099     mCreatedContact = createdJob->item();
0100 
0101     if (!mListItem.isEmpty()) {
0102         auto deleteJob = new Akonadi::ItemDeleteJob(mListItem, this);
0103         connect(deleteJob, &Akonadi::ItemDeleteJob::result, this, &MergeContactsJob::slotDeleteContactsFinished);
0104     } else {
0105         qCDebug(KADDRESSBOOKMERGELIBPRIVATE_LOG) << " mListITem is empty. We can't delete it";
0106     }
0107 }
0108 
0109 void MergeContactsJob::slotDeleteContactsFinished(KJob *job)
0110 {
0111     if (job->error()) {
0112         qCDebug(KADDRESSBOOKMERGELIBPRIVATE_LOG) << job->errorString();
0113     }
0114     Q_EMIT finished(mCreatedContact);
0115     deleteLater();
0116 }
0117 
0118 #include "moc_mergecontactsjob.cpp"