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

0001 /*
0002     SPDX-FileCopyrightText: 2009 Andras Mantia <amantia@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "createjob.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 #include "job_p.h"
0012 #include "response_p.h"
0013 #include "rfccodecs.h"
0014 #include "session_p.h"
0015 
0016 namespace KIMAP
0017 {
0018 class CreateJobPrivate : public JobPrivate
0019 {
0020 public:
0021     CreateJobPrivate(Session *session, const QString &name)
0022         : JobPrivate(session, name)
0023     {
0024     }
0025     ~CreateJobPrivate()
0026     {
0027     }
0028 
0029     QString mailBox;
0030 };
0031 }
0032 
0033 using namespace KIMAP;
0034 
0035 CreateJob::CreateJob(Session *session)
0036     : Job(*new CreateJobPrivate(session, i18n("Create")))
0037 {
0038 }
0039 
0040 CreateJob::~CreateJob()
0041 {
0042 }
0043 
0044 void CreateJob::doStart()
0045 {
0046     Q_D(CreateJob);
0047     d->tags << d->sessionInternal()->sendCommand("CREATE", '\"' + KIMAP::encodeImapFolderName(d->mailBox.toUtf8()) + '\"');
0048 }
0049 
0050 void CreateJob::handleResponse(const Response &response)
0051 {
0052     Q_D(CreateJob);
0053 
0054     if (!response.content.isEmpty() && d->tags.contains(response.content.first().toString())) {
0055         if (response.content.size() >= 2 && response.content[1].toString() == "NO") {
0056             for (auto it = response.responseCode.cbegin(), end = response.responseCode.cend(); it != end; ++it) {
0057                 // ALREADYEXISTS can be considered a success during CREATE
0058                 // cf. https://tools.ietf.org/html/rfc5530#section-3
0059                 if (it->toString() == "ALREADYEXISTS") {
0060                     // Code copied from handleErrorReplies:
0061                     d->tags.removeAll(response.content.first().toString());
0062                     if (d->tags.isEmpty()) { // Only emit result when the last command returned
0063                         emitResult();
0064                     }
0065                     return;
0066                 }
0067             }
0068         }
0069     }
0070 
0071     handleErrorReplies(response);
0072 }
0073 
0074 void CreateJob::setMailBox(const QString &mailBox)
0075 {
0076     Q_D(CreateJob);
0077     d->mailBox = mailBox;
0078 }
0079 
0080 QString CreateJob::mailBox() const
0081 {
0082     Q_D(const CreateJob);
0083     return d->mailBox;
0084 }
0085 
0086 #include "moc_createjob.cpp"