File indexing completed on 2024-05-12 05:17:14

0001 /*
0002     SPDX-FileCopyrightText: 2009 Kevin Ottens <ervin@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "job.h"
0008 #include "job_p.h"
0009 #include "response_p.h"
0010 #include "session_p.h"
0011 
0012 #include "kimap_debug.h"
0013 #include <KLocalizedString>
0014 
0015 using namespace KIMAP;
0016 
0017 Job::Job(Session *session)
0018     : KJob(session)
0019     , d_ptr(new JobPrivate(session, i18n("Job")))
0020 {
0021 }
0022 
0023 Job::Job(JobPrivate &dd)
0024     : KJob(dd.m_session)
0025     , d_ptr(&dd)
0026 {
0027 }
0028 
0029 Job::~Job()
0030 {
0031     delete d_ptr;
0032 }
0033 
0034 Session *Job::session() const
0035 {
0036     Q_D(const Job);
0037     return d->m_session;
0038 }
0039 
0040 void Job::start()
0041 {
0042     Q_D(Job);
0043     d->sessionInternal()->addJob(this);
0044 }
0045 
0046 void Job::handleResponse(const Response &response)
0047 {
0048     handleErrorReplies(response);
0049 }
0050 
0051 void Job::connectionLost()
0052 {
0053     setError(KJob::UserDefinedError);
0054     setErrorText(i18n("Connection to server lost."));
0055     emitResult();
0056 }
0057 
0058 Job::HandlerResponse Job::handleErrorReplies(const Response &response)
0059 {
0060     Q_D(Job);
0061     //   qCDebug(KIMAP_LOG) << response.toString();
0062 
0063     if (!response.content.isEmpty() && d->tags.contains(response.content.first().toString())) {
0064         if (response.content.size() < 2) {
0065             setErrorText(i18n("%1 failed, malformed reply from the server.", d->m_name));
0066         } else if (response.content[1].toString() != "OK") {
0067             setError(UserDefinedError);
0068             setErrorText(i18n("%1 failed, server replied: %2", d->m_name, QLatin1StringView(response.toString().constData())));
0069         }
0070         d->tags.removeAll(response.content.first().toString());
0071         if (d->tags.isEmpty()) { // Only emit result when the last command returned
0072             emitResult();
0073         }
0074         return Handled;
0075     }
0076 
0077     return NotHandled;
0078 }
0079 
0080 #include "moc_job.cpp"