Messagelib

attachmentvcardfromaddressbookjob.cpp
1/*
2 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "attachmentvcardfromaddressbookjob.h"
8#include <Akonadi/ContactGroupExpandJob>
9#include <KContacts/Addressee>
10#include <KContacts/ContactGroup>
11#include <KContacts/VCardConverter>
12#include <KLocalizedString>
13
14using namespace MessageComposer;
15
16class MessageComposer::AttachmentVcardFromAddressBookJobPrivate
17{
18public:
19 explicit AttachmentVcardFromAddressBookJobPrivate(const Akonadi::Item &item)
20 : mItem(item)
21 {
22 }
23
24 Akonadi::Item mItem;
25};
26
27AttachmentVcardFromAddressBookJob::AttachmentVcardFromAddressBookJob(const Akonadi::Item &item, QObject *parent)
28 : MessageCore::AttachmentLoadJob(parent)
29 , d(new MessageComposer::AttachmentVcardFromAddressBookJobPrivate(item))
30{
31}
32
33AttachmentVcardFromAddressBookJob::~AttachmentVcardFromAddressBookJob() = default;
34
35void AttachmentVcardFromAddressBookJob::addAttachment(const QByteArray &data, const QString &attachmentName)
36{
38 if (!data.isEmpty()) {
39 attachment->setName(attachmentName);
40 attachment->setFileName(attachmentName);
41 attachment->setData(data);
42 attachment->setMimeType("text/x-vcard");
43 // TODO what about the other fields?
44 }
45 setAttachmentPart(attachment);
46 emitResult(); // Success.
47}
48
49void AttachmentVcardFromAddressBookJob::doStart()
50{
51 if (d->mItem.isValid()) {
52 if (d->mItem.hasPayload<KContacts::Addressee>()) {
53 const auto contact = d->mItem.payload<KContacts::Addressee>();
54 if (contact.isEmpty()) {
55 invalidContact();
56 } else {
57 const QString contactRealName(contact.realName());
58 const QString attachmentName = (contactRealName.isEmpty() ? QStringLiteral("vcard") : contactRealName) + QLatin1StringView(".vcf");
59
60 QByteArray data = d->mItem.payloadData();
61 // Workaround about broken kaddressbook fields.
62 KContacts::adaptIMAttributes(data);
63 addAttachment(data, attachmentName);
64 }
65 } else if (d->mItem.hasPayload<KContacts::ContactGroup>()) {
66 const auto group = d->mItem.payload<KContacts::ContactGroup>();
67 const QString groupName(group.name());
68 const QString attachmentName = (groupName.isEmpty() ? QStringLiteral("vcard") : groupName) + QLatin1StringView(".vcf");
69 auto expandJob = new Akonadi::ContactGroupExpandJob(group, this);
70 expandJob->setProperty("groupName", attachmentName);
71 connect(expandJob, &KJob::result, this, &AttachmentVcardFromAddressBookJob::slotExpandGroupResult);
72 expandJob->start();
73 } else {
74 setError(KJob::UserDefinedError);
75 setErrorText(i18n("Unknown Contact Type"));
76 emitResult();
77 }
78 } else {
79 invalidContact();
80 }
81}
82
83void AttachmentVcardFromAddressBookJob::invalidContact()
84{
85 setError(KJob::UserDefinedError);
86 setErrorText(i18n("Invalid Contact"));
87 emitResult();
88}
89
90void AttachmentVcardFromAddressBookJob::slotExpandGroupResult(KJob *job)
91{
94
95 const QString attachmentName = expandJob->property("groupName").toString();
97 const QByteArray groupData = converter.exportVCards(expandJob->contacts(), KContacts::VCardConverter::v3_0);
98 if (!groupData.isEmpty()) {
99 addAttachment(groupData, attachmentName);
100 } else {
101 setError(KJob::UserDefinedError);
102 setErrorText(i18n("Impossible to generate vCard."));
103 emitResult();
104 }
105}
106
107#include "moc_attachmentvcardfromaddressbookjob.cpp"
void setErrorText(const QString &errorText)
void emitResult()
void result(KJob *job)
void setError(int errorCode)
void setAttachmentPart(const AttachmentPart::Ptr &part)
Subclasses use this method to set the loaded part.
A class that encapsulates an attachment.
QSharedPointer< AttachmentPart > Ptr
Defines a pointer to an attachment object.
QString i18n(const char *text, const TYPE &arg...)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
T qobject_cast(QObject *object)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:37:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.