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

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "sendvcardsjob.h"
0008 
0009 #include "kaddressbook_sendvcardsplugin_debug.h"
0010 #include <Akonadi/ContactGroupExpandJob>
0011 #include <Akonadi/ItemFetchJob>
0012 #include <Akonadi/ItemFetchScope>
0013 #include <KEMailClientLauncherJob>
0014 #include <KLocalizedString>
0015 #include <MimeTreeParser/AttachmentTemporaryFilesDirs>
0016 #include <QFile>
0017 #include <QTemporaryDir>
0018 
0019 using namespace KABSendVCards;
0020 
0021 SendVcardsJob::SendVcardsJob(const Akonadi::Item::List &listItem, QObject *parent)
0022     : QObject(parent)
0023     , mListItem(listItem)
0024 {
0025     // Don't delete it.
0026     mAttachmentTemporary = new MimeTreeParser::AttachmentTemporaryFilesDirs();
0027 }
0028 
0029 SendVcardsJob::~SendVcardsJob()
0030 {
0031     delete mTempDir;
0032     mTempDir = nullptr;
0033     // Don't delete it.
0034     mAttachmentTemporary = nullptr;
0035 }
0036 
0037 bool SendVcardsJob::start()
0038 {
0039     if (mListItem.isEmpty()) {
0040         qCDebug(KADDRESSBOOK_SENDVCARDS_LOG) << " No Item found";
0041         mAttachmentTemporary->deleteLater();
0042         mAttachmentTemporary = nullptr;
0043         deleteLater();
0044         return false;
0045     }
0046 
0047     for (const Akonadi::Item &item : std::as_const(mListItem)) {
0048         if (item.hasPayload<KContacts::Addressee>()) {
0049             const auto contact = item.payload<KContacts::Addressee>();
0050             QByteArray data = item.payloadData();
0051             // Workaround about broken kaddressbook fields.
0052             KContacts::adaptIMAttributes(data);
0053             createTemporaryDir();
0054             const QString contactRealName(contact.realName());
0055             const QString attachmentName = (contactRealName.isEmpty() ? QStringLiteral("vcard") : contactRealName) + QStringLiteral(".vcf");
0056             createTemporaryFile(data, attachmentName);
0057         } else if (item.hasPayload<KContacts::ContactGroup>()) {
0058             ++mExpandGroupJobCount;
0059             const auto group = item.payload<KContacts::ContactGroup>();
0060             const QString groupName(group.name());
0061             const QString attachmentName = (groupName.isEmpty() ? QStringLiteral("vcard") : groupName) + QStringLiteral(".vcf");
0062             auto expandJob = new Akonadi::ContactGroupExpandJob(group, this);
0063             expandJob->setProperty("groupName", attachmentName);
0064             connect(expandJob, &KJob::result, this, &SendVcardsJob::slotExpandGroupResult);
0065             expandJob->start();
0066         }
0067     }
0068 
0069     if (mExpandGroupJobCount == 0) {
0070         jobFinished();
0071     }
0072     return true;
0073 }
0074 
0075 void SendVcardsJob::createTemporaryDir()
0076 {
0077     if (!mTempDir) {
0078         mTempDir = new QTemporaryDir(QDir::tempPath() + QLatin1Char('/') + QStringLiteral("sendvcards"));
0079         mTempDir->setAutoRemove(false);
0080         mAttachmentTemporary->addTempDir(mTempDir->path());
0081     }
0082 }
0083 
0084 void SendVcardsJob::jobFinished()
0085 {
0086     const QStringList tempFilePaths{mAttachmentTemporary->temporaryFiles()};
0087     QList<QUrl> lstAttachment;
0088     for (const QString &path : tempFilePaths) {
0089         lstAttachment.append(QUrl::fromLocalFile(path));
0090     }
0091     if (!lstAttachment.isEmpty()) {
0092         auto job = new KEMailClientLauncherJob(this);
0093         job->setAttachments(lstAttachment);
0094         job->start();
0095     } else {
0096         Q_EMIT sendVCardsError(i18n("No vCard created."));
0097     }
0098     mAttachmentTemporary->removeTempFiles();
0099     deleteLater();
0100 }
0101 
0102 KContacts::VCardConverter::Version SendVcardsJob::version() const
0103 {
0104     return mVersion;
0105 }
0106 
0107 void SendVcardsJob::setVersion(KContacts::VCardConverter::Version version)
0108 {
0109     mVersion = version;
0110 }
0111 
0112 void SendVcardsJob::slotExpandGroupResult(KJob *job)
0113 {
0114     auto expandJob = qobject_cast<Akonadi::ContactGroupExpandJob *>(job);
0115     Q_ASSERT(expandJob);
0116 
0117     const QString attachmentName = expandJob->property("groupName").toString();
0118     KContacts::VCardConverter converter;
0119     const QByteArray groupData = converter.exportVCards(expandJob->contacts(), mVersion);
0120     createTemporaryDir();
0121     createTemporaryFile(groupData, attachmentName);
0122 
0123     --mExpandGroupJobCount;
0124     if (mExpandGroupJobCount == 0) {
0125         jobFinished();
0126     }
0127 }
0128 
0129 void SendVcardsJob::createTemporaryFile(const QByteArray &data, const QString &filename)
0130 {
0131     QFile file(mTempDir->path() + QLatin1Char('/') + filename);
0132     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
0133         qCDebug(KADDRESSBOOK_SENDVCARDS_LOG) << "Cannot write vcard filename :" << filename;
0134         Q_EMIT sendVCardsError(i18n("Temporary file \'%1\' cannot be created", filename));
0135         return;
0136     }
0137 
0138     QTextStream out(&file);
0139     out << data;
0140     file.close();
0141     mAttachmentTemporary->addTempFile(file.fileName());
0142 }
0143 
0144 #include "moc_sendvcardsjob.cpp"