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

0001 /*
0002     Copyright (c) 2009 Andras Mantia <amantia@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 "copyjob.h"
0021 
0022 #include "job_p.h"
0023 #include "message_p.h"
0024 #include "session_p.h"
0025 #include "rfccodecs.h"
0026 
0027 //TODO: when custom error codes are introduced, handle the NO [TRYCREATE] response
0028 
0029 namespace KIMAP2
0030 {
0031 class CopyJobPrivate : public JobPrivate
0032 {
0033 public:
0034     CopyJobPrivate(Session *session, const QString &name) : JobPrivate(session, name), uidBased(false) { }
0035     ~CopyJobPrivate() { }
0036 
0037     QString mailBox;
0038     ImapSet set;
0039     bool uidBased;
0040     ImapSet resultingUids;
0041 };
0042 }
0043 
0044 using namespace KIMAP2;
0045 
0046 CopyJob::CopyJob(Session *session)
0047     : Job(*new CopyJobPrivate(session, "Copy"))
0048 {
0049     Q_D(CopyJob);
0050     d->uidBased = false;
0051 }
0052 
0053 CopyJob::~CopyJob()
0054 {
0055 }
0056 
0057 void CopyJob::setMailBox(const QString &mailBox)
0058 {
0059     Q_D(CopyJob);
0060     d->mailBox = mailBox;
0061 }
0062 
0063 QString CopyJob::mailBox() const
0064 {
0065     Q_D(const CopyJob);
0066     return d->mailBox;
0067 }
0068 
0069 void CopyJob::setSequenceSet(const ImapSet &set)
0070 {
0071     Q_D(CopyJob);
0072     d->set = set;
0073 }
0074 
0075 ImapSet CopyJob::sequenceSet() const
0076 {
0077     Q_D(const CopyJob);
0078     return d->set;
0079 }
0080 
0081 void CopyJob::setUidBased(bool uidBased)
0082 {
0083     Q_D(CopyJob);
0084     d->uidBased = uidBased;
0085 }
0086 
0087 bool CopyJob::isUidBased() const
0088 {
0089     Q_D(const CopyJob);
0090     return d->uidBased;
0091 }
0092 
0093 ImapSet CopyJob::resultingUids() const
0094 {
0095     Q_D(const CopyJob);
0096     return d->resultingUids;
0097 }
0098 
0099 void CopyJob::doStart()
0100 {
0101     Q_D(CopyJob);
0102 
0103     d->set.optimize();
0104     QByteArray parameters = d->set.toImapSequenceSet() + ' ';
0105     parameters += '\"' + KIMAP2::encodeImapFolderName(d->mailBox.toUtf8()) + '\"';
0106 
0107     QByteArray command = "COPY";
0108     if (d->uidBased) {
0109         command = "UID " + command;
0110     }
0111 
0112     d->sendCommand(command, parameters);
0113 }
0114 
0115 void CopyJob::handleResponse(const Message &response)
0116 {
0117     Q_D(CopyJob);
0118 
0119     for (QList<Message::Part>::ConstIterator it = response.responseCode.begin();
0120             it != response.responseCode.end(); ++it) {
0121         if (it->toString() == "COPYUID") {
0122             it = it + 3;
0123             if (it < response.responseCode.end()) {
0124                 d->resultingUids = ImapSet::fromImapSequenceSet(it->toString());
0125             }
0126             break;
0127         }
0128     }
0129 
0130     handleErrorReplies(response);
0131 }