Messagelib

attachmentvcardfromaddressbookjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2015-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "attachmentvcardfromaddressbookjob.h"
8 #include <KContacts/Addressee>
9 #include <KContacts/ContactGroup>
10 #include <KContacts/VCardConverter>
11 #include <KLocalizedString>
12 #include <akonadi/contact/contactgroupexpandjob.h>
13 
14 using namespace MessageComposer;
15 
16 class MessageComposer::AttachmentVcardFromAddressBookJobPrivate
17 {
18 public:
19  explicit AttachmentVcardFromAddressBookJobPrivate(const Akonadi::Item &item)
20  : mItem(item)
21  {
22  }
23 
24  Akonadi::Item mItem;
25 };
26 
27 AttachmentVcardFromAddressBookJob::AttachmentVcardFromAddressBookJob(const Akonadi::Item &item, QObject *parent)
28  : MessageCore::AttachmentLoadJob(parent)
29  , d(new MessageComposer::AttachmentVcardFromAddressBookJobPrivate(item))
30 {
31 }
32 
33 AttachmentVcardFromAddressBookJob::~AttachmentVcardFromAddressBookJob() = default;
34 
35 void 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 
49 void 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) + QLatin1String(".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) + QLatin1String(".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 
83 void AttachmentVcardFromAddressBookJob::invalidContact()
84 {
85  setError(KJob::UserDefinedError);
86  setErrorText(i18n("Invalid Contact"));
87  emitResult();
88 }
89 
90 void AttachmentVcardFromAddressBookJob::slotExpandGroupResult(KJob *job)
91 {
92  auto expandJob = qobject_cast<Akonadi::ContactGroupExpandJob *>(job);
93  Q_ASSERT(expandJob);
94 
95  const QString attachmentName = expandJob->property("groupName").toString();
96  KContacts::VCardConverter converter;
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 }
bool isEmpty() const const
A class that encapsulates an attachment.
QString i18n(const char *text, const TYPE &arg...)
QSharedPointer< AttachmentPart > Ptr
Defines a pointer to an attachment object.
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
QByteArray exportVCards(const Addressee::List &list, Version version) const
void result(KJob *job)
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-2021 The KDE developers.
Generated on Thu Dec 2 2021 23:06:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.