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

0001 /*
0002     Copyright (c) 2016 Daniel Vrátil <dvratil@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 "statusjob.h"
0021 #include "job_p.h"
0022 #include "message_p.h"
0023 #include "session_p.h"
0024 #include "rfccodecs.h"
0025 #include "kimap_debug.h"
0026 
0027 namespace KIMAP2
0028 {
0029 
0030 class StatusJobPrivate : public JobPrivate
0031 {
0032 public:
0033     explicit StatusJobPrivate(Session *session, const QString &name)
0034         : JobPrivate(session, name)
0035     {
0036     }
0037 
0038     ~StatusJobPrivate()
0039     {
0040     }
0041 
0042     QString mailBox;
0043     QList<QByteArray> dataItems;
0044     QList<QPair<QByteArray, qint64>> status;
0045 };
0046 
0047 }
0048 
0049 using namespace KIMAP2;
0050 
0051 StatusJob::StatusJob(Session *session)
0052     : Job(*new StatusJobPrivate(session, "Status"))
0053 {
0054 }
0055 
0056 StatusJob::~StatusJob()
0057 {
0058 }
0059 
0060 void StatusJob::setMailBox(const QString &mailBox)
0061 {
0062     Q_D(StatusJob);
0063     d->mailBox = mailBox;
0064 }
0065 
0066 QString StatusJob::mailBox() const
0067 {
0068     Q_D(const StatusJob);
0069     return d->mailBox;
0070 }
0071 
0072 void StatusJob::setDataItems(const QList<QByteArray> &dataItems)
0073 {
0074     Q_D(StatusJob);
0075     d->dataItems = dataItems;
0076 }
0077 
0078 QList<QByteArray> StatusJob::dataItems() const
0079 {
0080     Q_D(const StatusJob);
0081     return d->dataItems;
0082 }
0083 
0084 QList<QPair<QByteArray, qint64>> StatusJob::status() const
0085 {
0086     Q_D(const StatusJob);
0087     return d->status;
0088 }
0089 
0090 void StatusJob::doStart()
0091 {
0092     Q_D(StatusJob);
0093 
0094     const QByteArray params = '\"' + KIMAP2::encodeImapFolderName(d->mailBox.toUtf8()) + "\" ("
0095                             + d->dataItems.join(' ') + ')';
0096 
0097     d->sendCommand("STATUS", params);
0098 }
0099 
0100 void StatusJob::handleResponse(const Message &response)
0101 {
0102     Q_D(StatusJob);
0103 
0104     if (handleErrorReplies(response) == NotHandled) {
0105         if (response.content.size() >= 3) {
0106             const QByteArray code = response.content[1].toString();
0107             if (code == "STATUS") {
0108 
0109                 const QList<QByteArray> resp = response.content[3].toList();
0110                 for (int i = 0; i < resp.size(); i += 2) {
0111                     d->status << (qMakePair(resp[i], resp[i + 1].toLongLong()));
0112                 }
0113 
0114             } else if (code == "OK") {
0115                 return;
0116             } else {
0117                 qCDebug(KIMAP2_LOG) << response.toString();
0118             }
0119         }
0120     }
0121 }