File indexing completed on 2024-05-12 05:21:35

0001 /*
0002   SPDX-FileCopyrightText: 2010 BetterInbox <contact@betterinbox.com>
0003   SPDX-FileContributor: Christophe Laveault <christophe@betterinbox.com>
0004   SPDX-FileContributor: Gregory Schlomoff <gregory.schlomoff@gmail.com>
0005 
0006   SPDX-License-Identifier: LGPL-2.1-or-later
0007 */
0008 
0009 #include "job.h"
0010 #include "job_p.h"
0011 #include "serverresponse_p.h"
0012 #include "session_p.h"
0013 
0014 #include <KLocalizedString>
0015 
0016 using namespace KSmtp;
0017 
0018 Job::Job(Session *session)
0019     : KJob(session)
0020     , d_ptr(new JobPrivate(session, QStringLiteral("Job")))
0021 {
0022 }
0023 
0024 Job::Job(JobPrivate &dd)
0025     : KJob(dd.m_session)
0026     , d_ptr(&dd)
0027 {
0028 }
0029 
0030 Job::~Job() = default;
0031 
0032 Session *Job::session() const
0033 {
0034     Q_D(const Job);
0035     return d->m_session;
0036 }
0037 
0038 void Job::start()
0039 {
0040     Q_D(Job);
0041     d->sessionInternal()->addJob(this);
0042 }
0043 
0044 void Job::sendCommand(const QByteArray &cmd)
0045 {
0046     Q_D(Job);
0047     d->sessionInternal()->sendData(cmd);
0048 }
0049 
0050 void Job::handleErrors(const ServerResponse &r)
0051 {
0052     if (r.isCode(4) || r.isCode(5)) {
0053         setError(KJob::UserDefinedError);
0054         // https://www.ietf.org/rfc/rfc2821.txt
0055         // We could just use r.text(), but that might not be in the user's language, so try and prepend a translated message.
0056         const QString serverText = QString::fromUtf8(r.text());
0057         if (r.code() == 421) {
0058             setErrorText(i18n("Service not available")); // e.g. the server is shutting down
0059         } else if (r.code() == 450 || r.code() == 550) {
0060             setErrorText(i18n("Mailbox unavailable. The server said: %1", serverText));
0061         } else if (r.code() == 452 || r.code() == 552) {
0062             setErrorText(i18n("Insufficient storage space on server. The server said: %1", serverText));
0063         } else {
0064             setErrorText(i18n("Server error: %1", serverText));
0065         }
0066         emitResult();
0067     }
0068 }
0069 
0070 void Job::connectionLost()
0071 {
0072     setError(KJob::UserDefinedError);
0073     setErrorText(i18n("Connection to server lost."));
0074     emitResult();
0075 }
0076 
0077 #include "moc_job.cpp"