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

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 "ewsdeleteitemrequest.h"
0008 #include "ewsclient_debug.h"
0009 
0010 static const QList<QString> deleteTypes = {
0011     QStringLiteral("HardDelete"),
0012     QStringLiteral("SoftDelete"),
0013     QStringLiteral("MoveToDeletedItems"),
0014 };
0015 
0016 EwsDeleteItemRequest::EwsDeleteItemRequest(EwsClient &client, QObject *parent)
0017     : EwsRequest(client, parent)
0018     , mType(SoftDelete)
0019 {
0020 }
0021 
0022 EwsDeleteItemRequest::~EwsDeleteItemRequest()
0023 {
0024 }
0025 
0026 void EwsDeleteItemRequest::start()
0027 {
0028     QString reqString;
0029     QXmlStreamWriter writer(&reqString);
0030 
0031     startSoapDocument(writer);
0032 
0033     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("DeleteItem"));
0034 
0035     writer.writeAttribute(QStringLiteral("DeleteType"), deleteTypes[mType]);
0036 
0037     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("ItemIds"));
0038     for (const EwsId &id : std::as_const(mIds)) {
0039         id.writeItemIds(writer);
0040     }
0041     writer.writeEndElement();
0042 
0043     writer.writeEndElement();
0044 
0045     endSoapDocument(writer);
0046 
0047     qCDebugNCS(EWSCLI_REQUEST_LOG) << QStringLiteral("Starting DeleteItem request (") << mIds << ")";
0048 
0049     qCDebug(EWSCLI_PROTO_LOG) << reqString;
0050 
0051     prepare(reqString);
0052 
0053     doSend();
0054 }
0055 
0056 bool EwsDeleteItemRequest::parseResult(QXmlStreamReader &reader)
0057 {
0058     return parseResponseMessage(reader, QStringLiteral("DeleteItem"), [this](QXmlStreamReader &reader) {
0059         return parseItemsResponse(reader);
0060     });
0061 }
0062 
0063 bool EwsDeleteItemRequest::parseItemsResponse(QXmlStreamReader &reader)
0064 {
0065     Response resp(reader);
0066     if (resp.responseClass() == EwsResponseUnknown) {
0067         return false;
0068     }
0069 
0070     if (EWSCLI_REQUEST_LOG().isDebugEnabled()) {
0071         if (resp.isSuccess()) {
0072             qCDebugNC(EWSCLI_REQUEST_LOG) << QStringLiteral("Got DeleteItem response - OK, deleted items") << mIds;
0073         } else {
0074             qCDebugNC(EWSCLI_REQUEST_LOG) << QStringLiteral("Got DeleteItem response - %1").arg(resp.responseMessage());
0075         }
0076     }
0077 
0078     mResponses.append(resp);
0079     return true;
0080 }
0081 
0082 EwsDeleteItemRequest::Response::Response(QXmlStreamReader &reader)
0083     : EwsRequest::Response::Response(reader)
0084 {
0085     if (mClass == EwsResponseParseError) {
0086         return;
0087     }
0088 
0089     while (reader.readNextStartElement()) {
0090         if (reader.namespaceUri() != ewsMsgNsUri && reader.namespaceUri() != ewsTypeNsUri) {
0091             setErrorMsg(QStringLiteral("Unexpected namespace in %1 element: %2").arg(QStringLiteral("ResponseMessage"), reader.namespaceUri().toString()));
0092             return;
0093         }
0094 
0095         if (!readResponseElement(reader)) {
0096             setErrorMsg(QStringLiteral("Failed to read EWS request - invalid response element."));
0097             return;
0098         }
0099     }
0100 }
0101 
0102 #include "moc_ewsdeleteitemrequest.cpp"