File indexing completed on 2025-01-19 04:46:51

0001 /*
0002    SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "vcardmemento.h"
0008 #include "vcard_debug.h"
0009 #include <Akonadi/ContactSearchJob>
0010 using namespace MessageViewer;
0011 
0012 VcardMemento::VcardMemento(const QStringList &emails)
0013     : QObject(nullptr)
0014 {
0015     mVCardList.reserve(emails.count());
0016     for (const QString &str : emails) {
0017         VCard vcard(str, false);
0018         mVCardList.append(vcard);
0019     }
0020     checkEmail();
0021 }
0022 
0023 VcardMemento::~VcardMemento() = default;
0024 
0025 void VcardMemento::checkEmail()
0026 {
0027     auto searchJob = new Akonadi::ContactSearchJob();
0028     searchJob->setQuery(Akonadi::ContactSearchJob::Email, mVCardList.at(mIndex).email.toLower());
0029     connect(searchJob, &Akonadi::ContactSearchJob::result, this, &VcardMemento::slotSearchJobFinished);
0030 }
0031 
0032 void VcardMemento::slotSearchJobFinished(KJob *job)
0033 {
0034     auto searchJob = static_cast<Akonadi::ContactSearchJob *>(job);
0035     if (searchJob->error()) {
0036         qCWarning(VCARD_LOG) << "Unable to fetch contact:" << searchJob->errorText();
0037         mIndex++;
0038         continueToCheckEmail();
0039         return;
0040     }
0041 
0042     const int contactSize(searchJob->contacts().size());
0043     if (contactSize >= 1) {
0044         VCard vcard = mVCardList.at(mIndex);
0045         vcard.found = true;
0046         vcard.address = searchJob->contacts().at(0);
0047         mVCardList[mIndex] = vcard;
0048         if (contactSize > 1) {
0049             qCDebug(VCARD_LOG) << " more than 1 contact was found";
0050         }
0051     }
0052 
0053     mIndex++;
0054     continueToCheckEmail();
0055 }
0056 
0057 void VcardMemento::continueToCheckEmail()
0058 {
0059     if (mIndex == mVCardList.count()) {
0060         mFinished = true;
0061         Q_EMIT update(MimeTreeParser::Delayed);
0062     } else {
0063         checkEmail();
0064     }
0065 }
0066 
0067 bool VcardMemento::finished() const
0068 {
0069     return mFinished;
0070 }
0071 
0072 void VcardMemento::detach()
0073 {
0074     disconnect(this, SIGNAL(update(MimeTreeParser::UpdateMode)), nullptr, nullptr);
0075 }
0076 
0077 bool VcardMemento::vcardExist(int index) const
0078 {
0079     // We can have more vcard as we have emails. For example a vcard without email will not created here => necessary to make this check see #405791
0080     if (index >= mVCardList.count()) {
0081         return false;
0082     }
0083     return mVCardList.at(index).found;
0084 }
0085 
0086 KContacts::Addressee VcardMemento::address(int index) const
0087 {
0088     // We can have more vcard as we have emails. For example a vcard without email will not created here => necessary to make this check see #405791
0089     if (index >= mVCardList.count()) {
0090         return {};
0091     }
0092     return mVCardList.at(index).address;
0093 }
0094 
0095 #include "moc_vcardmemento.cpp"