7#include "addemaildisplayjob.h"
8#include "widgets/selectaddressbookdialog.h"
9#include <Akonadi/AgentFilterProxyModel>
10#include <Akonadi/AgentInstanceCreateJob>
11#include <Akonadi/AgentType>
12#include <Akonadi/AgentTypeDialog>
13#include <Akonadi/Collection>
14#include <Akonadi/CollectionDialog>
15#include <Akonadi/CollectionFetchJob>
16#include <Akonadi/CollectionFetchScope>
17#include <Akonadi/ContactEditorDialog>
18#include <Akonadi/ContactSearchJob>
19#include <Akonadi/ItemCreateJob>
20#include <Akonadi/ItemModifyJob>
22#include <KContacts/ContactGroup>
23#include <KLocalizedString>
29class Akonadi::AddEmailDisplayJobPrivate
32 AddEmailDisplayJobPrivate(AddEmailDisplayJob *qq,
const QString &emailString, QWidget *parentWidget)
34 , mCompleteAddress(emailString)
35 , mParentWidget(parentWidget)
40 void slotResourceCreationDone(KJob *job)
43 q->setError(job->
error());
54 auto searchJob =
new Akonadi::ContactSearchJob(q);
55 searchJob->setLimit(1);
64 Akonadi::Item item = contact;
67 address.insertCustom(QStringLiteral(
"KADDRESSBOOK"),
68 QStringLiteral(
"MailPreferedFormatting"),
69 mShowAsHTML ? QStringLiteral(
"HTML") : QStringLiteral(
"TEXT"));
70 address.insertCustom(QStringLiteral(
"KADDRESSBOOK"),
71 QStringLiteral(
"MailAllowToRemoteContent"),
72 mRemoteContent ? QStringLiteral(
"TRUE") : QStringLiteral(
"FALSE"));
74 auto itemModifyJob =
new Akonadi::ItemModifyJob(item);
76 auto modifiedJob =
static_cast<Akonadi::ItemModifyJob *
>(itemModifyJob);
77 if (!modifiedJob->error()) {
78 Q_EMIT q->contactUpdated(modifiedJob->item(), messageId, mShowAsHTML, mRemoteContent);
80 slotAddModifyContactDone(itemModifyJob);
87 void slotSearchDone(KJob *job)
90 q->setError(job->
error());
96 const Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob *>(job);
102 Akonadi::Item item = items.
at(0);
103 KContacts::Addressee contact = searchJob->
contacts().at(0);
104 contact.insertCustom(QStringLiteral(
"KADDRESSBOOK"),
105 QStringLiteral(
"MailPreferedFormatting"),
106 mShowAsHTML ? QStringLiteral(
"HTML") : QStringLiteral(
"TEXT"));
107 contact.insertCustom(QStringLiteral(
"KADDRESSBOOK"),
108 QStringLiteral(
"MailAllowToRemoteContent"),
109 mRemoteContent ? QStringLiteral(
"TRUE") : QStringLiteral(
"FALSE"));
110 item.
setPayload<KContacts::Addressee>(contact);
111 auto itemModifyJob =
new Akonadi::ItemModifyJob(item);
113 auto modifiedJob =
static_cast<Akonadi::ItemModifyJob *
>(itemModifyJob);
114 if (!modifiedJob->error()) {
115 Q_EMIT q->contactUpdated(modifiedJob->item(), messageId, mShowAsHTML, mRemoteContent);
117 slotAddModifyContactDone(itemModifyJob);
128 addressBookJob->fetchScope().setContentMimeTypes(mimeTypes);
130 slotCollectionsFetched(job);
134 void slotCollectionsFetched(KJob *job)
137 q->setError(job->
error());
143 const Akonadi::CollectionFetchJob *addressBookJob = qobject_cast<Akonadi::CollectionFetchJob *>(job);
147 for (
const Akonadi::Collection &collection : colsList) {
149 canCreateItemCollections.
append(collection);
153 Akonadi::Collection addressBook;
155 const int nbItemCollection(canCreateItemCollections.
size());
156 if (nbItemCollection == 0) {
159 i18nc(
"@info",
"You must create an address book before adding a contact. Do you want to create an address book?"),
160 i18nc(
"@title:window",
"No Address Book Available"),
161 KGuiItem(
i18nc(
"@action:button",
"Create Address Book"), QStringLiteral(
"address-book-new")),
163 == KMessageBox::ButtonCode::PrimaryAction) {
164 QPointer<Akonadi::AgentTypeDialog> dlg =
new Akonadi::AgentTypeDialog(mParentWidget);
165 dlg->setWindowTitle(
i18nc(
"@title:window",
"Add Address Book"));
168 dlg->agentFilterProxyModel()->addCapabilityFilter(QStringLiteral(
"Resource"));
171 const Akonadi::AgentType agentType = dlg->agentType();
174 auto job =
new Akonadi::AgentInstanceCreateJob(agentType, q);
176 slotResourceCreationDone(job);
178 job->configure(mParentWidget);
183 q->setError(KJob::UserDefinedError);
189 q->setError(KJob::UserDefinedError);
195 q->setError(KJob::UserDefinedError);
199 }
else if (nbItemCollection == 1) {
200 addressBook = canCreateItemCollections[0];
203 QPointer<Akonadi::SelectAddressBookDialog> dlg =
new Akonadi::SelectAddressBookDialog(mParentWidget);
207 addressBook = dlg->selectedCollection();
209 q->setError(KJob::UserDefinedError);
220 q->setError(KJob::UserDefinedError);
224 KContacts::Addressee contact;
225 contact.setNameFromString(mName);
226 KContacts::Email email(mEmail);
227 email.setPreferred(
true);
228 contact.addEmail(email);
229 contact.insertCustom(QStringLiteral(
"KADDRESSBOOK"),
230 QStringLiteral(
"MailPreferedFormatting"),
231 mShowAsHTML ? QStringLiteral(
"HTML") : QStringLiteral(
"TEXT"));
232 contact.insertCustom(QStringLiteral(
"KADDRESSBOOK"),
233 QStringLiteral(
"MailAllowToRemoteContent"),
234 mRemoteContent ? QStringLiteral(
"TRUE") : QStringLiteral(
"FALSE"));
239 item.
setPayload<KContacts::Addressee>(contact);
242 auto createJob =
new Akonadi::ItemCreateJob(item, addressBook, q);
244 auto modifiedJob =
static_cast<Akonadi::ItemCreateJob *
>(createJob);
245 if (!modifiedJob->error()) {
246 Q_EMIT q->contactUpdated(modifiedJob->item(), messageId, mShowAsHTML, mRemoteContent);
248 slotAddModifyContactDone(createJob);
252 void slotAddModifyContactDone(KJob *job)
255 q->setError(job->
error());
261 AddEmailDisplayJob *
const q;
262 Akonadi::Item contact;
264 const QString mCompleteAddress;
267 QWidget *
const mParentWidget;
268 bool mShowAsHTML =
false;
269 bool mRemoteContent =
false;
274 , d(new AddEmailDisplayJobPrivate(this, email, parentWidget))
278AddEmailDisplayJob::~AddEmailDisplayJob() =
default;
280void AddEmailDisplayJob::setShowAsHTML(
bool html)
282 d->mShowAsHTML = html;
285void AddEmailDisplayJob::setRemoteContent(
bool b)
287 d->mRemoteContent = b;
290void AddEmailDisplayJob::setContact(
const Akonadi::Item &contact)
292 d->contact = contact;
300void AddEmailDisplayJob::start()
302 if (d->contact.isValid()) {
309#include "moc_addemaildisplayjob.cpp"
Collection::List collections() const
void setPayload(const T &p)
void setMimeType(const QString &mimeType)
virtual Q_SCRIPTABLE void start()=0
QString errorText() const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
A widget for editing the display name of a contact.
PostalAddress address(const QVariant &location)
ButtonCode questionTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Notify)
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
bool isEmpty() const const
qsizetype size() const const