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 foo[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 }
QStringList expandedBcc() const
Returns the expanded Bcc field.
~EmailAddressResolveJob() override
Destroys the email address resolve job.
QString expandedFrom() const
Returns the expanded From field.
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
void setErrorText(const QString &errorText)
void result(KJob *job)
QStringList expandedCc() const
Returns the expanded CC field.
void setBcc(const QStringList &from)
Sets the from address to expand.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KCODECS_EXPORT QStringList splitAddressList(const QString &aStr)
void setTo(const QStringList &from)
Sets the from address to expand.
QString errorText() const
A job to expand aliases to email addresses.
QStringList expandedReplyTo() const
Returns the expanded Reply-To field.
QString join(const QString &separator) const const
EmailAddressResolveJob(QObject *parent=nullptr)
Creates a new email address resolve job.
void setReplyTo(const QStringList &replyTo)
Sets the Reply-To address to expand.
QStringList expandedTo() const
Returns the expanded To field.
void setFrom(const QString &from)
Sets the from address to expand.
QString addresses() const
Returns the expanded email addresses.
int count(const T &value) const const
void emitResult()
int error() const
void setError(int errorCode)
void setCc(const QStringList &from)
Sets the from address to expand.
QString toString() const const
QVariant property(const char *name) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu May 26 2022 03:52:54 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.