File indexing completed on 2024-05-19 05:17:44

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 "selectjob.h"
0021 
0022 #include "kimap_debug.h"
0023 
0024 #include "job_p.h"
0025 #include "message_p.h"
0026 #include "session_p.h"
0027 #include "rfccodecs.h"
0028 
0029 namespace KIMAP2
0030 {
0031 class SelectJobPrivate : public JobPrivate
0032 {
0033 public:
0034     SelectJobPrivate(Session *session, const QString &name)
0035         : JobPrivate(session, name), readOnly(false), messageCount(-1), recentCount(-1),
0036           firstUnseenIndex(-1), uidValidity(-1), nextUid(-1), highestmodseq(0),
0037           condstoreEnabled(false) { }
0038     ~SelectJobPrivate() { }
0039 
0040     QString mailBox;
0041     bool readOnly;
0042 
0043     QList<QByteArray> flags;
0044     QList<QByteArray> permanentFlags;
0045     int messageCount;
0046     int recentCount;
0047     int firstUnseenIndex;
0048     qint64 uidValidity;
0049     qint64 nextUid;
0050     quint64 highestmodseq;
0051     bool condstoreEnabled;
0052 };
0053 }
0054 
0055 using namespace KIMAP2;
0056 
0057 SelectJob::SelectJob(Session *session)
0058     : Job(*new SelectJobPrivate(session, "Select"))
0059 {
0060 }
0061 
0062 SelectJob::~SelectJob()
0063 {
0064 }
0065 
0066 void SelectJob::setMailBox(const QString &mailBox)
0067 {
0068     Q_D(SelectJob);
0069     d->mailBox = mailBox;
0070 }
0071 
0072 QString SelectJob::mailBox() const
0073 {
0074     Q_D(const SelectJob);
0075     return d->mailBox;
0076 }
0077 
0078 void SelectJob::setOpenReadOnly(bool readOnly)
0079 {
0080     Q_D(SelectJob);
0081     d->readOnly = readOnly;
0082 }
0083 
0084 bool SelectJob::isOpenReadOnly() const
0085 {
0086     Q_D(const SelectJob);
0087     return d->readOnly;
0088 }
0089 
0090 QList<QByteArray> SelectJob::flags() const
0091 {
0092     Q_D(const SelectJob);
0093     return d->flags;
0094 }
0095 
0096 QList<QByteArray> SelectJob::permanentFlags() const
0097 {
0098     Q_D(const SelectJob);
0099     return d->permanentFlags;
0100 }
0101 
0102 int SelectJob::messageCount() const
0103 {
0104     Q_D(const SelectJob);
0105     return d->messageCount;
0106 }
0107 
0108 int SelectJob::recentCount() const
0109 {
0110     Q_D(const SelectJob);
0111     return d->recentCount;
0112 }
0113 
0114 int SelectJob::firstUnseenIndex() const
0115 {
0116     Q_D(const SelectJob);
0117     return d->firstUnseenIndex;
0118 }
0119 
0120 qint64 SelectJob::uidValidity() const
0121 {
0122     Q_D(const SelectJob);
0123     return d->uidValidity;
0124 }
0125 
0126 qint64 SelectJob::nextUid() const
0127 {
0128     Q_D(const SelectJob);
0129     return d->nextUid;
0130 }
0131 
0132 quint64 SelectJob::highestModSequence() const
0133 {
0134     Q_D(const SelectJob);
0135     return d->highestmodseq;
0136 }
0137 
0138 void SelectJob::setCondstoreEnabled(bool enable)
0139 {
0140     Q_D(SelectJob);
0141     d->condstoreEnabled = enable;
0142 }
0143 
0144 bool SelectJob::condstoreEnabled() const
0145 {
0146     Q_D(const SelectJob);
0147     return d->condstoreEnabled;
0148 }
0149 
0150 void SelectJob::doStart()
0151 {
0152     Q_D(SelectJob);
0153 
0154     QByteArray command = "SELECT";
0155     if (d->readOnly) {
0156         command = "EXAMINE";
0157     }
0158 
0159     QByteArray params = '\"' + KIMAP2::encodeImapFolderName(d->mailBox.toUtf8()) + '\"';
0160 
0161     if (d->condstoreEnabled) {
0162         params += " (CONDSTORE)";
0163     }
0164 
0165     d->sendCommand(command, params);
0166 }
0167 
0168 void SelectJob::handleResponse(const Message &response)
0169 {
0170     Q_D(SelectJob);
0171 
0172     if (handleErrorReplies(response) == NotHandled) {
0173         if (response.content.size() >= 2) {
0174             QByteArray code = response.content[1].toString();
0175 
0176             if (code == "OK") {
0177                 if (response.responseCode.size() < 2) {
0178                     return;
0179                 }
0180 
0181                 code = response.responseCode[0].toString();
0182 
0183                 if (code == "PERMANENTFLAGS") {
0184                     d->permanentFlags = response.responseCode[1].toList();
0185                 } else if (code == "HIGHESTMODSEQ") {
0186                     bool isInt;
0187                     quint64 value = response.responseCode[1].toString().toULongLong(&isInt);
0188                     if (!isInt) {
0189                         return;
0190                     }
0191                     d->highestmodseq = value;
0192                 } else {
0193                     bool isInt;
0194                     qint64 value = response.responseCode[1].toString().toLongLong(&isInt);
0195                     if (!isInt) {
0196                         return;
0197                     }
0198                     if (code == "UIDVALIDITY") {
0199                         d->uidValidity = value;
0200                     } else if (code == "UNSEEN") {
0201                         d->firstUnseenIndex = value;
0202                     } else if (code == "UIDNEXT") {
0203                         d->nextUid = value;
0204                     }
0205                 }
0206             } else if (code == "FLAGS") {
0207                 d->flags = response.content[2].toList();
0208             } else {
0209                 bool isInt;
0210                 int value = response.content[1].toString().toInt(&isInt);
0211                 if (!isInt || response.content.size() < 3) {
0212                     return;
0213                 }
0214 
0215                 code = response.content[2].toString();
0216                 if (code == "EXISTS") {
0217                     d->messageCount = value;
0218                 } else if (code == "RECENT") {
0219                     d->recentCount = value;
0220                 }
0221             }
0222         } else {
0223             qCDebug(KIMAP2_LOG) << response.toString();
0224         }
0225     } else {
0226         Q_ASSERT(error() || d->m_session->selectedMailBox() == d->mailBox);
0227     }
0228 }