File indexing completed on 2024-11-24 04:43:05
0001 /* 0002 This file is part of KAddressBook. 0003 0004 SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org> 0005 based on code from SPDX-FileCopyrightText: 2014 Clément Vannier <clement.vannier@free.fr> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 0010 #include "mailsenderjob.h" 0011 0012 #include "kaddressbook_sendmailplugin_debug.h" 0013 #include <KEmailAddress> 0014 #include <KJob> 0015 #include <KLocalizedString> 0016 0017 #include <Akonadi/ItemFetchJob> 0018 #include <Akonadi/ItemFetchScope> 0019 0020 #include <KContacts/Addressee> 0021 #include <KContacts/ContactGroup> 0022 0023 using namespace KABMailSender; 0024 0025 MailSenderJob::MailSenderJob(const Akonadi::Item::List &listItem, QObject *parent) 0026 : QObject(parent) 0027 , mListItem(listItem) 0028 { 0029 } 0030 0031 MailSenderJob::~MailSenderJob() = default; 0032 0033 void MailSenderJob::start() 0034 { 0035 for (const Akonadi::Item &item : std::as_const(mListItem)) { 0036 if (item.hasPayload<KContacts::Addressee>()) { 0037 const auto contact = item.payload<KContacts::Addressee>(); 0038 const QString preferredEmail = contact.preferredEmail(); 0039 if (!preferredEmail.isEmpty() && !mEmailAddresses.contains(preferredEmail)) { 0040 if (KEmailAddress::isValidSimpleAddress(contact.preferredEmail())) { 0041 mEmailAddresses << KEmailAddress::normalizedAddress(contact.formattedName(), preferredEmail); 0042 } 0043 } 0044 } else if (item.hasPayload<KContacts::ContactGroup>()) { 0045 const auto group = item.payload<KContacts::ContactGroup>(); 0046 unsigned int nbDataCount(group.dataCount()); 0047 for (unsigned int i = 0; i < nbDataCount; ++i) { 0048 const QString currentEmail(group.data(i).email()); 0049 if (KEmailAddress::isValidSimpleAddress(currentEmail)) { 0050 const QString email = KEmailAddress::normalizedAddress(group.data(i).name(), currentEmail); 0051 if (!email.isEmpty() && !mEmailAddresses.contains(email)) { 0052 mEmailAddresses << email; 0053 } 0054 } 0055 } 0056 const unsigned int nbContactReference(group.contactReferenceCount()); 0057 for (unsigned int i = 0; i < nbContactReference; ++i) { 0058 KContacts::ContactGroup::ContactReference reference = group.contactReference(i); 0059 0060 Akonadi::Item newItem; 0061 if (reference.gid().isEmpty()) { 0062 newItem.setId(reference.uid().toLongLong()); 0063 } else { 0064 newItem.setGid(reference.gid()); 0065 } 0066 mItemToFetch << newItem; 0067 } 0068 } 0069 } 0070 0071 if (mItemToFetch.isEmpty()) { 0072 finishJob(); 0073 } else { 0074 fetchNextItem(); 0075 } 0076 } 0077 0078 void MailSenderJob::fetchNextItem() 0079 { 0080 if (mFetchJobCount < mItemToFetch.count()) { 0081 fetchItem(mItemToFetch.at(mFetchJobCount)); 0082 ++mFetchJobCount; 0083 } else { 0084 finishJob(); 0085 } 0086 } 0087 0088 void MailSenderJob::fetchItem(const Akonadi::Item &item) 0089 { 0090 auto job = new Akonadi::ItemFetchJob(item, this); 0091 job->fetchScope().fetchFullPayload(); 0092 0093 connect(job, &Akonadi::ItemFetchJob::result, this, &MailSenderJob::slotFetchJobFinished); 0094 } 0095 0096 void MailSenderJob::slotFetchJobFinished(KJob *job) 0097 { 0098 if (job->error()) { 0099 qCDebug(KADDRESSBOOK_SENDMAIL_LOG) << " error during fetching " << job->errorString(); 0100 fetchNextItem(); 0101 return; 0102 } 0103 0104 auto fetchJob = qobject_cast<Akonadi::ItemFetchJob *>(job); 0105 0106 if (fetchJob->items().count() != 1) { 0107 fetchNextItem(); 0108 return; 0109 } 0110 0111 const Akonadi::Item item = fetchJob->items().at(0); 0112 const auto contact = item.payload<KContacts::Addressee>(); 0113 0114 if (!contact.preferredEmail().isEmpty()) { 0115 if (KEmailAddress::isValidSimpleAddress(contact.preferredEmail())) { 0116 mEmailAddresses << KEmailAddress::normalizedAddress(contact.formattedName(), contact.preferredEmail()); 0117 } 0118 } 0119 fetchNextItem(); 0120 } 0121 0122 void MailSenderJob::finishJob() 0123 { 0124 if (!mEmailAddresses.isEmpty()) { 0125 Q_EMIT sendMails(mEmailAddresses); 0126 } else { 0127 Q_EMIT sendMailsError(i18n("No emails found in selected contacts.")); 0128 } 0129 deleteLater(); 0130 } 0131 0132 #include "moc_mailsenderjob.cpp"