File indexing completed on 2025-01-05 04:50:03

0001 /*
0002     SPDX-FileCopyrightText: 2015-2016 Krzysztof Nowicki <krissn@op.pl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ewsmovefolderrequest.h"
0008 #include "ewsclient_debug.h"
0009 
0010 EwsMoveFolderRequest::EwsMoveFolderRequest(EwsClient &client, QObject *parent)
0011     : EwsRequest(client, parent)
0012 {
0013 }
0014 
0015 EwsMoveFolderRequest::~EwsMoveFolderRequest() = default;
0016 
0017 void EwsMoveFolderRequest::start()
0018 {
0019     QString reqString;
0020     QXmlStreamWriter writer(&reqString);
0021 
0022     startSoapDocument(writer);
0023 
0024     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("MoveFolder"));
0025 
0026     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("ToFolderId"));
0027     mDestFolderId.writeFolderIds(writer);
0028     writer.writeEndElement();
0029 
0030     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("FolderIds"));
0031     for (const EwsId &id : std::as_const(mIds)) {
0032         id.writeFolderIds(writer);
0033     }
0034     writer.writeEndElement();
0035 
0036     writer.writeEndElement();
0037 
0038     endSoapDocument(writer);
0039 
0040     qCDebugNC(EWSCLI_REQUEST_LOG) << QStringLiteral("Starting MoveFolder request (%1 folders, to %2)").arg(mIds.size()).arg(mDestFolderId.id());
0041 
0042     qCDebug(EWSCLI_PROTO_LOG) << reqString;
0043 
0044     prepare(reqString);
0045 
0046     doSend();
0047 }
0048 
0049 bool EwsMoveFolderRequest::parseResult(QXmlStreamReader &reader)
0050 {
0051     return parseResponseMessage(reader, QStringLiteral("MoveFolder"), [this](QXmlStreamReader &reader) {
0052         return parseItemsResponse(reader);
0053     });
0054 }
0055 
0056 bool EwsMoveFolderRequest::parseItemsResponse(QXmlStreamReader &reader)
0057 {
0058     Response resp(reader);
0059     if (resp.responseClass() == EwsResponseUnknown) {
0060         return false;
0061     }
0062 
0063     if (EWSCLI_REQUEST_LOG().isDebugEnabled()) {
0064         if (resp.isSuccess()) {
0065             qCDebug(EWSCLI_REQUEST_LOG) << QStringLiteral("Got MoveFolder response - OK");
0066         } else {
0067             qCDebug(EWSCLI_REQUEST_LOG) << QStringLiteral("Got MoveFolder response - %1").arg(resp.responseMessage());
0068         }
0069     }
0070     mResponses.append(resp);
0071     return true;
0072 }
0073 
0074 EwsMoveFolderRequest::Response::Response(QXmlStreamReader &reader)
0075     : EwsRequest::Response(reader)
0076 {
0077     if (mClass == EwsResponseParseError) {
0078         return;
0079     }
0080 
0081     while (reader.readNextStartElement()) {
0082         if (reader.namespaceUri() != ewsMsgNsUri && reader.namespaceUri() != ewsTypeNsUri) {
0083             setErrorMsg(QStringLiteral("Unexpected namespace in %1 element: %2").arg(QStringLiteral("ResponseMessage"), reader.namespaceUri().toString()));
0084             return;
0085         }
0086 
0087         if (reader.name() == QLatin1StringView("Folders")) {
0088             if (reader.readNextStartElement()) {
0089                 EwsFolder folder(reader);
0090                 if (!folder.isValid()) {
0091                     return;
0092                 }
0093                 mId = folder[EwsFolderFieldFolderId].value<EwsId>();
0094 
0095                 // Finish the Folders element.
0096                 reader.skipCurrentElement();
0097             }
0098         } else if (!readResponseElement(reader)) {
0099             setErrorMsg(QStringLiteral("Failed to read EWS request - invalid response element."));
0100             return;
0101         }
0102     }
0103 }
0104 
0105 #include "moc_ewsmovefolderrequest.cpp"