File indexing completed on 2024-12-22 04:57:02

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 "ewsmoveitemrequest.h"
0008 #include "ewsclient_debug.h"
0009 
0010 EwsMoveItemRequest::EwsMoveItemRequest(EwsClient &client, QObject *parent)
0011     : EwsRequest(client, parent)
0012 {
0013 }
0014 
0015 EwsMoveItemRequest::~EwsMoveItemRequest() = default;
0016 
0017 void EwsMoveItemRequest::start()
0018 {
0019     QString reqString;
0020     QXmlStreamWriter writer(&reqString);
0021 
0022     startSoapDocument(writer);
0023 
0024     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("MoveItem"));
0025 
0026     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("ToFolderId"));
0027     mDestFolderId.writeFolderIds(writer);
0028     writer.writeEndElement();
0029 
0030     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("ItemIds"));
0031     for (const EwsId &id : std::as_const(mIds)) {
0032         id.writeItemIds(writer);
0033     }
0034     writer.writeEndElement();
0035 
0036     writer.writeEndElement();
0037 
0038     endSoapDocument(writer);
0039 
0040     qCDebugNCS(EWSCLI_REQUEST_LOG) << QStringLiteral("Starting MoveItem request (") << mIds << "to" << mDestFolderId << ")";
0041 
0042     qCDebug(EWSCLI_PROTO_LOG) << reqString;
0043 
0044     prepare(reqString);
0045 
0046     doSend();
0047 }
0048 
0049 bool EwsMoveItemRequest::parseResult(QXmlStreamReader &reader)
0050 {
0051     return parseResponseMessage(reader, QStringLiteral("MoveItem"), [this](QXmlStreamReader &reader) {
0052         return parseItemsResponse(reader);
0053     });
0054 }
0055 
0056 bool EwsMoveItemRequest::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             qCDebugNC(EWSCLI_REQUEST_LOG) << QStringLiteral("Got MoveItem response - OK");
0066         } else {
0067             qCDebugNC(EWSCLI_REQUEST_LOG) << QStringLiteral("Got MoveItem response - %1").arg(resp.responseMessage());
0068         }
0069     }
0070     mResponses.append(resp);
0071     return true;
0072 }
0073 
0074 EwsMoveItemRequest::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("Items")) {
0088             if (reader.readNextStartElement()) {
0089                 EwsItem item(reader);
0090                 if (!item.isValid()) {
0091                     return;
0092                 }
0093                 mId = item[EwsItemFieldItemId].value<EwsId>();
0094 
0095                 // Finish the Items 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_ewsmoveitemrequest.cpp"