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

0001 /*
0002     Copyright (c) 2009 Kevin Ottens <ervin@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to the
0016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301, USA.
0018 */
0019 
0020 #include "job.h"
0021 #include "job_p.h"
0022 #include "message_p.h"
0023 #include "session_p.h"
0024 
0025 #include "kimap_debug.h"
0026 
0027 using namespace KIMAP2;
0028 
0029 void JobPrivate::sendCommand(const QByteArray &command, const QByteArray &args)
0030 {
0031     tags << sessionInternal()->sendCommand(command, args);
0032     m_currentCommand = command + "" + args;
0033 }
0034 
0035 Job::Job(Session *session)
0036     : KJob(session), d_ptr(new JobPrivate(session, "Job"))
0037 {
0038 }
0039 
0040 Job::Job(JobPrivate &dd)
0041     : KJob(dd.m_session), d_ptr(&dd)
0042 {
0043 }
0044 
0045 Job::~Job()
0046 {
0047     delete d_ptr;
0048 }
0049 
0050 Session *Job::session() const
0051 {
0052     Q_D(const Job);
0053     return d->m_session;
0054 }
0055 
0056 void Job::start()
0057 {
0058     Q_D(Job);
0059     d->sessionInternal()->addJob(this);
0060 }
0061 
0062 void Job::handleResponse(const Message &response)
0063 {
0064     handleErrorReplies(response);
0065 }
0066 
0067 void Job::connectionLost()
0068 {
0069     Q_D(Job);
0070     setError(ConnectionLost);
0071     setErrorText("Connection to server lost: " + d->m_errorMessage);
0072     emitResult();
0073 }
0074 
0075 void Job::setSocketError(QAbstractSocket::SocketError error)
0076 {
0077     Q_D(Job);
0078     d->m_socketError = error;
0079 }
0080 
0081 void Job::setErrorMessage(const QString &msg)
0082 {
0083     Q_D(Job);
0084     d->m_errorMessage = msg;
0085 }
0086 
0087 Job::HandlerResponse Job::handleErrorReplies(const Message &response)
0088 {
0089     Q_D(Job);
0090     // qCDebug(KIMAP2_LOG) << response.toString();
0091 
0092     if (!response.content.isEmpty() &&
0093             d->tags.contains(response.content.first().toString())) {
0094         if (response.content.size() < 2) {
0095             setErrorText(QString("%1 failed, malformed reply from the server.").arg(d->m_name));
0096         } else if (response.content[1].toString() != "OK") {
0097             setError(CommandFailed);
0098             setErrorText(QString("%1 failed, server replied: %2.\n Sent command: %3").arg(d->m_name).arg(QLatin1String(response.toString().constData())).arg(QString(d->m_currentCommand)));
0099         }
0100         d->tags.removeAll(response.content.first().toString());
0101         if (d->tags.isEmpty()) {   // Only emit result when the last command returned
0102             emitResult();
0103         }
0104         return Handled;
0105     }
0106 
0107     return NotHandled;
0108 }