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
15#include <KEmailAddress>
16#include <QVariant>
17
18using namespace MessageComposer;
19
20class MessageComposer::EmailAddressResolveJobPrivate
21{
22public:
23 EmailAddressResolveJobPrivate() = default;
24
25 QVariantMap mResultMap;
26 QString mFrom;
27 QStringList mTo;
28 QStringList mCc;
29 QStringList mBcc;
30 QStringList mReplyTo;
31 QString mDefaultDomainName;
32 int mJobCount = 0;
33};
34
36 : KJob(parent)
37 , d(new MessageComposer::EmailAddressResolveJobPrivate)
38{
39}
40
42
43static inline bool containsAliases(const QString &address)
44{
45 // an valid email is defined as foo@foo.extension
46 return !(address.contains(QLatin1Char('@')) && address.contains(QLatin1Char('.')));
47}
48
49static bool containsAliases(const QStringList &addresses)
50{
51 for (const QString &address : addresses) {
52 if (containsAliases(address)) {
53 return true;
54 }
55 }
56
57 return false;
58}
59
60void EmailAddressResolveJob::setDefaultDomainName(const QString &domainName)
61{
62 d->mDefaultDomainName = domainName;
63}
64
66{
68
69 if (containsAliases(d->mFrom)) {
70 auto job = new AliasesExpandJob(d->mFrom, d->mDefaultDomainName, this);
71 job->setProperty("id", QStringLiteral("infoPartFrom"));
72 connect(job, &AliasesExpandJob::result, this, &EmailAddressResolveJob::slotAliasExpansionDone);
73 jobs << job;
74 }
75 if (containsAliases(d->mTo)) {
76 auto job = new AliasesExpandJob(d->mTo.join(QLatin1StringView(", ")), d->mDefaultDomainName, this);
77 job->setProperty("id", QStringLiteral("infoPartTo"));
78 connect(job, &AliasesExpandJob::result, this, &EmailAddressResolveJob::slotAliasExpansionDone);
79 jobs << job;
80 }
81
82 if (containsAliases(d->mCc)) {
83 auto job = new AliasesExpandJob(d->mCc.join(QLatin1StringView(", ")), d->mDefaultDomainName, this);
84 job->setProperty("id", QStringLiteral("infoPartCc"));
85 connect(job, &AliasesExpandJob::result, this, &EmailAddressResolveJob::slotAliasExpansionDone);
86 jobs << job;
87 }
88
89 if (containsAliases(d->mBcc)) {
90 auto job = new AliasesExpandJob(d->mBcc.join(QLatin1StringView(", ")), d->mDefaultDomainName, this);
91 job->setProperty("id", QStringLiteral("infoPartBcc"));
92 connect(job, &AliasesExpandJob::result, this, &EmailAddressResolveJob::slotAliasExpansionDone);
93 jobs << job;
94 }
95 if (containsAliases(d->mReplyTo)) {
96 auto job = new AliasesExpandJob(d->mReplyTo.join(QLatin1StringView(", ")), d->mDefaultDomainName, this);
97 job->setProperty("id", QStringLiteral("infoPartReplyTo"));
98 connect(job, &AliasesExpandJob::result, this, &EmailAddressResolveJob::slotAliasExpansionDone);
99 jobs << job;
100 }
101
102 d->mJobCount = jobs.count();
103
104 if (d->mJobCount == 0) {
105 emitResult();
106 } else {
107 for (AliasesExpandJob *job : std::as_const(jobs)) {
108 job->start();
109 }
110 }
111}
112
113void EmailAddressResolveJob::slotAliasExpansionDone(KJob *job)
114{
115 if (job->error()) {
116 setError(job->error());
117 setErrorText(job->errorText());
118 emitResult();
119 return;
120 }
121
123 d->mResultMap.insert(expandJob->property("id").toString(), expandJob->addresses());
124
125 d->mJobCount--;
126 if (d->mJobCount == 0) {
127 emitResult();
128 }
129}
130
132{
133 d->mFrom = from;
134 d->mResultMap.insert(QStringLiteral("infoPartFrom"), from);
135}
136
138{
139 d->mTo = to;
140 d->mResultMap.insert(QStringLiteral("infoPartTo"), to.join(QLatin1StringView(", ")));
141}
142
144{
145 d->mCc = cc;
146 d->mResultMap.insert(QStringLiteral("infoPartCc"), cc.join(QLatin1StringView(", ")));
147}
148
150{
151 d->mBcc = bcc;
152 d->mResultMap.insert(QStringLiteral("infoPartBcc"), bcc.join(QLatin1StringView(", ")));
153}
154
156{
157 d->mReplyTo = replyTo;
158 d->mResultMap.insert(QStringLiteral("infoPartReplyTo"), replyTo.join(QLatin1StringView(", ")));
159}
160
162{
163 return d->mResultMap.value(QStringLiteral("infoPartFrom")).toString();
164}
165
167{
168 return KEmailAddress::splitAddressList(d->mResultMap.value(QStringLiteral("infoPartTo")).toString());
169}
170
172{
173 return KEmailAddress::splitAddressList(d->mResultMap.value(QStringLiteral("infoPartCc")).toString());
174}
175
177{
178 return KEmailAddress::splitAddressList(d->mResultMap.value(QStringLiteral("infoPartBcc")).toString());
179}
180
182{
183 return KEmailAddress::splitAddressList(d->mResultMap.value(QStringLiteral("infoPartReplyTo")).toString());
184}
185
186#include "moc_emailaddressresolvejob.cpp"
void setErrorText(const QString &errorText)
void emitResult()
int error() const
void result(KJob *job)
void setError(int errorCode)
KJob(QObject *parent=nullptr)
QString errorText() const
A job to expand aliases to email addresses.
QString addresses() const
Returns the expanded 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...
qsizetype count() const const
iterator insert(const_iterator before, parameter_type value)
QObject(QObject *parent)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QVariant property(const char *name) const const
T qobject_cast(QObject *object)
QString & insert(qsizetype position, QChar ch)
QString join(QChar separator) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 21 2025 11:47:08 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.