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

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 "ewsgetitemrequest.h"
0008 #include "ewsclient_debug.h"
0009 
0010 EwsGetItemRequest::EwsGetItemRequest(EwsClient &client, QObject *parent)
0011     : EwsRequest(client, parent)
0012 {
0013 }
0014 
0015 EwsGetItemRequest::~EwsGetItemRequest() = default;
0016 
0017 void EwsGetItemRequest::setItemIds(const EwsId::List &ids)
0018 {
0019     mIds = ids;
0020 }
0021 
0022 void EwsGetItemRequest::setItemShape(const EwsItemShape &shape)
0023 {
0024     mShape = shape;
0025 }
0026 
0027 void EwsGetItemRequest::start()
0028 {
0029     QString reqString;
0030     QXmlStreamWriter writer(&reqString);
0031 
0032     startSoapDocument(writer);
0033 
0034     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("GetItem"));
0035 
0036     mShape.write(writer);
0037 
0038     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("ItemIds"));
0039     for (const EwsId &id : std::as_const(mIds)) {
0040         id.writeItemIds(writer);
0041     }
0042     writer.writeEndElement();
0043 
0044     writer.writeEndElement();
0045 
0046     endSoapDocument(writer);
0047 
0048     qCDebug(EWSCLI_PROTO_LOG) << reqString;
0049 
0050     qCDebugNCS(EWSCLI_REQUEST_LOG) << QStringLiteral("Starting GetItem request (") << mIds << ")";
0051 
0052     prepare(reqString);
0053 
0054     doSend();
0055 }
0056 
0057 bool EwsGetItemRequest::parseResult(QXmlStreamReader &reader)
0058 {
0059     mResponses.reserve(mIds.size());
0060     return parseResponseMessage(reader, QStringLiteral("GetItem"), [this](QXmlStreamReader &reader) {
0061         return parseItemsResponse(reader);
0062     });
0063 }
0064 
0065 bool EwsGetItemRequest::parseItemsResponse(QXmlStreamReader &reader)
0066 {
0067     Response resp(reader);
0068     if (resp.responseClass() == EwsResponseUnknown) {
0069         return false;
0070     }
0071 
0072     if (EWSCLI_REQUEST_LOG().isDebugEnabled()) {
0073         if (resp.isSuccess()) {
0074             const EwsItem &item = resp.item();
0075             const EwsId &id = item[EwsItemFieldItemId].value<EwsId>();
0076             qCDebugNC(EWSCLI_REQUEST_LOG)
0077                 << QStringLiteral("Got GetItem response (id: %1, subject: %2)").arg(ewsHash(id.id()), item[EwsItemFieldSubject].toString());
0078         } else {
0079             qCDebugNC(EWSCLI_REQUEST_LOG) << QStringLiteral("Got GetItem response - %1").arg(resp.responseMessage());
0080         }
0081     }
0082 
0083     mResponses.append(resp);
0084 
0085     return true;
0086 }
0087 
0088 EwsGetItemRequest::Response::Response(QXmlStreamReader &reader)
0089     : EwsRequest::Response(reader)
0090 {
0091     if (mClass == EwsResponseParseError) {
0092         return;
0093     }
0094 
0095     while (reader.readNextStartElement()) {
0096         if (reader.namespaceUri() != ewsMsgNsUri && reader.namespaceUri() != ewsTypeNsUri) {
0097             setErrorMsg(QStringLiteral("Unexpected namespace in %1 element: %2").arg(QStringLiteral("ResponseMessage"), reader.namespaceUri().toString()));
0098             return;
0099         }
0100 
0101         if (reader.name() == QLatin1StringView("Items")) {
0102             if (!parseItems(reader)) {
0103                 return;
0104             }
0105         } else if (!readResponseElement(reader)) {
0106             setErrorMsg(QStringLiteral("Failed to read EWS request - invalid response element."));
0107             return;
0108         }
0109     }
0110 }
0111 
0112 bool EwsGetItemRequest::Response::parseItems(QXmlStreamReader &reader)
0113 {
0114     if (reader.namespaceUri() != ewsMsgNsUri || reader.name() != QLatin1StringView("Items")) {
0115         return setErrorMsg(QStringLiteral("Failed to read EWS request - expected Items element (got %1).").arg(reader.qualifiedName().toString()));
0116     }
0117 
0118     if (reader.readNextStartElement()) {
0119         if (reader.namespaceUri() != ewsTypeNsUri) {
0120             return setErrorMsg(QStringLiteral("Failed to read EWS request - expected child element from types namespace."));
0121         }
0122 
0123         EwsItem item(reader);
0124         if (!item.isValid()) {
0125             return setErrorMsg(QStringLiteral("Failed to read EWS request - invalid Item element."));
0126         }
0127         mItem = item;
0128 
0129         // Finish the Items element.
0130         reader.skipCurrentElement();
0131     }
0132     return true;
0133 }
0134 
0135 #include "moc_ewsgetitemrequest.cpp"