Astyle kdelibs
[kdepim.git] / kmail / job / addressvalidationjob.cpp
blob0b3543edfa17a91d1ea2bf18bd8e51364745b0eb
1 /*
2 * This file is part of KMail.
4 * Copyright (c) 2010 KDAB
6 * Author: Tobias Koenig <tokoe@kde.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "addressvalidationjob.h"
24 #include <messagecomposer/job/aliasesexpandjob.h>
25 using MessageComposer::AliasesExpandJob;
27 #include "settings/messagecomposersettings.h"
29 #include <KLocalizedString>
30 #include <kmessagebox.h>
32 #include <KPIMUtils/Email>
34 class AddressValidationJob::Private
36 public:
37 Private(AddressValidationJob *qq, const QString &emailAddresses, QWidget *parentWidget)
38 : q(qq),
39 mEmailAddresses(emailAddresses),
40 mIsValid(false),
41 mParentWidget(parentWidget)
45 void slotAliasExpansionDone(KJob *);
47 AddressValidationJob *q;
48 QString mEmailAddresses;
49 QString mDomainDefaultName;
50 bool mIsValid;
51 QWidget *mParentWidget;
54 void AddressValidationJob::Private::slotAliasExpansionDone(KJob *job)
56 mIsValid = true;
58 if (job->error()) {
59 q->setError(job->error());
60 q->setErrorText(job->errorText());
61 mIsValid = false;
62 q->emitResult();
63 return;
66 const AliasesExpandJob *expandJob = qobject_cast<AliasesExpandJob *>(job);
67 const QStringList emptyDistributionLists = expandJob->emptyDistributionLists();
69 QString brokenAddress;
71 const KPIMUtils::EmailParseResult errorCode = KPIMUtils::isValidAddressList(expandJob->addresses(), brokenAddress);
72 if (!emptyDistributionLists.isEmpty()) {
73 QString errorMsg;
74 const int numberOfDistributionList(emptyDistributionLists.count());
75 QString listOfDistributionList;
76 for (int i = 0; i < numberOfDistributionList; ++i) {
77 if (i != 0) {
78 listOfDistributionList.append(QLatin1String(", "));
80 listOfDistributionList.append(QString::fromLatin1("\"%1\"").arg(emptyDistributionLists.at(i)));
82 errorMsg = i18np("Distribution list %2 is empty, it cannot be used.",
83 "Distribution lists %2 are empty, they cannot be used.",
84 numberOfDistributionList, listOfDistributionList);
85 KMessageBox::sorry(mParentWidget, errorMsg, i18n("Invalid Email Address"));
86 mIsValid = false;
87 } else {
88 if (!(errorCode == KPIMUtils::AddressOk ||
89 errorCode == KPIMUtils::AddressEmpty)) {
90 const QString errorMsg(QLatin1String("<qt><p><b>") + brokenAddress +
91 QLatin1String("</b></p><p>") +
92 KPIMUtils::emailParseResultToString(errorCode) +
93 QLatin1String("</p></qt>"));
94 KMessageBox::sorry(mParentWidget, errorMsg, i18n("Invalid Email Address"));
95 mIsValid = false;
99 q->emitResult();
102 AddressValidationJob::AddressValidationJob(const QString &emailAddresses, QWidget *parentWidget, QObject *parent)
103 : KJob(parent), d(new Private(this, emailAddresses, parentWidget))
107 AddressValidationJob::~AddressValidationJob()
109 delete d;
112 void AddressValidationJob::setDefaultDomain(const QString &domainName)
114 d->mDomainDefaultName = domainName;
117 void AddressValidationJob::start()
119 AliasesExpandJob *job = new AliasesExpandJob(d->mEmailAddresses, d->mDomainDefaultName, this);
120 connect(job, SIGNAL(result(KJob*)), SLOT(slotAliasExpansionDone(KJob*)));
121 job->start();
124 bool AddressValidationJob::isValid() const
126 return d->mIsValid;
129 #include "moc_addressvalidationjob.cpp"