Messagelib

emailaddressresolvejob.cpp
1 /*
2  * This file is part of KMail.
3  *
4  * SPDX-FileCopyrightText: 2010 KDAB
5  * SPDX-FileContributor: Tobias Koenig <[email protected]>
6  * SPDX-FileContributor: Leo Franchi <[email protected]>
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 
22 using namespace MessageComposer;
23 
24 class MessageComposer::EmailAddressResolveJobPrivate
25 {
26 public:
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 
47 static inline bool containsAliases(const QString &address)
48 {
49  // an valid email is defined as [email protected]
50  return !(address.contains(QLatin1Char('@')) && address.contains(QLatin1Char('.')));
51 }
52 
53 static 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 
64 void 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(QLatin1String(", ")), 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(QLatin1String(", ")), 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(QLatin1String(", ")), 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(QLatin1String(", ")), 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 
117 void EmailAddressResolveJob::slotAliasExpansionDone(KJob *job)
118 {
119  if (job->error()) {
120  setError(job->error());
121  setErrorText(job->errorText());
122  emitResult();
123  return;
124  }
125 
126  const AliasesExpandJob *expandJob = qobject_cast<AliasesExpandJob *>(job);
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(QLatin1String(", ")));
145 }
146 
148 {
149  d->mCc = cc;
150  d->mResultMap.insert(QStringLiteral("infoPartCc"), cc.join(QLatin1String(", ")));
151 }
152 
154 {
155  d->mBcc = bcc;
156  d->mResultMap.insert(QStringLiteral("infoPartBcc"), bcc.join(QLatin1String(", ")));
157 }
158 
160 {
161  d->mReplyTo = replyTo;
162  d->mResultMap.insert(QStringLiteral("infoPartReplyTo"), replyTo.join(QLatin1String(", ")));
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 }
void emitResult()
void setBcc(const QStringList &from)
Sets the from address to expand.
QString expandedFrom() const
Returns the expanded From field.
void setError(int errorCode)
void setCc(const QStringList &from)
Sets the from address to expand.
A job to expand aliases to email addresses.
QString join(const QString &separator) const const
QString addresses() const
Returns the expanded email addresses.
void setErrorText(const QString &errorText)
EmailAddressResolveJob(QObject *parent=nullptr)
Creates a new email address resolve job.
QVariant property(const char *name) const const
void setFrom(const QString &from)
Sets the from address to expand.
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
KCODECS_EXPORT QStringList splitAddressList(const QString &aStr)
void setTo(const QStringList &from)
Sets the from address to expand.
QStringList expandedReplyTo() const
Returns the expanded Reply-To field.
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
~EmailAddressResolveJob() override
Destroys the email address resolve job.
int count(const T &value) const const
QStringList expandedCc() const
Returns the expanded CC field.
bool setProperty(const char *name, const QVariant &value)
QStringList expandedBcc() const
Returns the expanded Bcc field.
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)
void setReplyTo(const QStringList &replyTo)
Sets the Reply-To address to expand.
QString toString() const const
QStringList expandedTo() const
Returns the expanded To field.
QString errorText() const
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 23:04:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.