Messagelib

emailaddressresolvejob.cpp
1/*
2 * This file is part of KMail.
3 *
4 * SPDX-FileCopyrightText: 2010 KDAB
5 * SPDX-FileContributor: Tobias Koenig <tokoe@kde.org>
6 * SPDX-FileContributor: Leo Franchi <lfranchi@kde.org>
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11#include "job/emailaddressresolvejob.h"
12
13#include "aliasesexpandjob.h"
14#include "settings/messagecomposersettings.h"
15
16#include "MessageComposer/Composer"
17#include "MessageComposer/InfoPart"
18
19#include <KEmailAddress>
20#include <QVariant>
21
22using namespace MessageComposer;
23
24class MessageComposer::EmailAddressResolveJobPrivate
25{
26public:
27 EmailAddressResolveJobPrivate() = default;
28
29 QVariantMap mResultMap;
30 QString mFrom;
31 QStringList mTo;
32 QStringList mCc;
33 QStringList mBcc;
34 QStringList mReplyTo;
35 QString mDefaultDomainName;
36 int mJobCount = 0;
37};
38
40 : KJob(parent)
41 , d(new MessageComposer::EmailAddressResolveJobPrivate)
42{
43}
44
46
47static inline bool containsAliases(const QString &address)
48{
49 // an valid email is defined as foo@foo.extension
50 return !(address.contains(QLatin1Char('@')) && address.contains(QLatin1Char('.')));
51}
52
53static bool containsAliases(const QStringList &addresses)
54{
55 for (const QString &address : addresses) {
56 if (containsAliases(address)) {
57 return true;
58 }
59 }
60
61 return false;
62}
63
64void EmailAddressResolveJob::setDefaultDomainName(const QString &domainName)
65{
66 d->mDefaultDomainName = domainName;
67}
68
70{
72
73 if (containsAliases(d->mFrom)) {
74 auto job = new AliasesExpandJob(d->mFrom, d->mDefaultDomainName, this);
75 job->setProperty("id", QStringLiteral("infoPartFrom"));
76 connect(job, &AliasesExpandJob::result, this, &EmailAddressResolveJob::slotAliasExpansionDone);
77 jobs << job;
78 }
79 if (containsAliases(d->mTo)) {
80 auto job = new AliasesExpandJob(d->mTo.join(QLatin1StringView(", ")), d->mDefaultDomainName, this);
81 job->setProperty("id", QStringLiteral("infoPartTo"));
82 connect(job, &AliasesExpandJob::result, this, &EmailAddressResolveJob::slotAliasExpansionDone);
83 jobs << job;
84 }
85
86 if (containsAliases(d->mCc)) {
87 auto job = new AliasesExpandJob(d->mCc.join(QLatin1StringView(", ")), d->mDefaultDomainName, this);
88 job->setProperty("id", QStringLiteral("infoPartCc"));
89 connect(job, &AliasesExpandJob::result, this, &EmailAddressResolveJob::slotAliasExpansionDone);
90 jobs << job;
91 }
92
93 if (containsAliases(d->mBcc)) {
94 auto job = new AliasesExpandJob(d->mBcc.join(QLatin1StringView(", ")), d->mDefaultDomainName, this);
95 job->setProperty("id", QStringLiteral("infoPartBcc"));
96 connect(job, &AliasesExpandJob::result, this, &EmailAddressResolveJob::slotAliasExpansionDone);
97 jobs << job;
98 }
99 if (containsAliases(d->mReplyTo)) {
100 auto job = new AliasesExpandJob(d->mReplyTo.join(QLatin1StringView(", ")), d->mDefaultDomainName, this);
101 job->setProperty("id", QStringLiteral("infoPartReplyTo"));
102 connect(job, &AliasesExpandJob::result, this, &EmailAddressResolveJob::slotAliasExpansionDone);
103 jobs << job;
104 }
105
106 d->mJobCount = jobs.count();
107
108 if (d->mJobCount == 0) {
109 emitResult();
110 } else {
111 for (AliasesExpandJob *job : std::as_const(jobs)) {
112 job->start();
113 }
114 }
115}
116
117void EmailAddressResolveJob::slotAliasExpansionDone(KJob *job)
118{
119 if (job->error()) {
120 setError(job->error());
121 setErrorText(job->errorText());
122 emitResult();
123 return;
124 }
125
127 d->mResultMap.insert(expandJob->property("id").toString(), expandJob->addresses());
128
129 d->mJobCount--;
130 if (d->mJobCount == 0) {
131 emitResult();
132 }
133}
134
136{
137 d->mFrom = from;
138 d->mResultMap.insert(QStringLiteral("infoPartFrom"), from);
139}
140
142{
143 d->mTo = to;
144 d->mResultMap.insert(QStringLiteral("infoPartTo"), to.join(QLatin1StringView(", ")));
145}
146
148{
149 d->mCc = cc;
150 d->mResultMap.insert(QStringLiteral("infoPartCc"), cc.join(QLatin1StringView(", ")));
151}
152
154{
155 d->mBcc = bcc;
156 d->mResultMap.insert(QStringLiteral("infoPartBcc"), bcc.join(QLatin1StringView(", ")));
157}
158
160{
161 d->mReplyTo = replyTo;
162 d->mResultMap.insert(QStringLiteral("infoPartReplyTo"), replyTo.join(QLatin1StringView(", ")));
163}
164
166{
167 return d->mResultMap.value(QStringLiteral("infoPartFrom")).toString();
168}
169
171{
172 return KEmailAddress::splitAddressList(d->mResultMap.value(QStringLiteral("infoPartTo")).toString());
173}
174
176{
177 return KEmailAddress::splitAddressList(d->mResultMap.value(QStringLiteral("infoPartCc")).toString());
178}
179
181{
182 return KEmailAddress::splitAddressList(d->mResultMap.value(QStringLiteral("infoPartBcc")).toString());
183}
184
186{
187 return KEmailAddress::splitAddressList(d->mResultMap.value(QStringLiteral("infoPartReplyTo")).toString());
188}
189
190#include "moc_emailaddressresolvejob.cpp"
void setErrorText(const QString &errorText)
void emitResult()
int error() const
void result(KJob *job)
void setError(int errorCode)
QString errorText() const
A job to expand aliases to email addresses.
QStringList expandedReplyTo() const
Returns the expanded Reply-To field.
QString expandedFrom() const
Returns the expanded From field.
QStringList expandedCc() const
Returns the expanded CC field.
void setFrom(const QString &from)
Sets the from address to expand.
void setBcc(const QStringList &from)
Sets the from address to expand.
EmailAddressResolveJob(QObject *parent=nullptr)
Creates a new email address resolve job.
QStringList expandedBcc() const
Returns the expanded Bcc field.
void setCc(const QStringList &from)
Sets the from address to expand.
~EmailAddressResolveJob() override
Destroys the email address resolve job.
void setTo(const QStringList &from)
Sets the from address to expand.
QStringList expandedTo() const
Returns the expanded To field.
void setReplyTo(const QStringList &replyTo)
Sets the Reply-To address to expand.
KCODECS_EXPORT QStringList splitAddressList(const QString &aStr)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
T qobject_cast(QObject *object)
QString join(const QString &separator) const const
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.