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

0001 /*
0002     SPDX-FileCopyrightText: 2015-2017 Krzysztof Nowicki <krissn@op.pl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ewsgetfolderrequest.h"
0008 
0009 #include <QXmlStreamWriter>
0010 
0011 #include "ewsclient_debug.h"
0012 
0013 EwsGetFolderRequest::EwsGetFolderRequest(EwsClient &client, QObject *parent)
0014     : EwsRequest(client, parent)
0015 {
0016 }
0017 
0018 EwsGetFolderRequest::~EwsGetFolderRequest() = default;
0019 
0020 void EwsGetFolderRequest::setFolderIds(const EwsId::List &ids)
0021 {
0022     mIds = ids;
0023 }
0024 
0025 void EwsGetFolderRequest::setFolderShape(const EwsFolderShape &shape)
0026 {
0027     mShape = shape;
0028 }
0029 
0030 void EwsGetFolderRequest::start()
0031 {
0032     QString reqString;
0033     QXmlStreamWriter writer(&reqString);
0034 
0035     startSoapDocument(writer);
0036 
0037     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("GetFolder"));
0038 
0039     mShape.write(writer);
0040 
0041     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("FolderIds"));
0042     for (const EwsId &id : std::as_const(mIds)) {
0043         id.writeFolderIds(writer);
0044     }
0045     writer.writeEndElement();
0046 
0047     writer.writeEndElement();
0048 
0049     endSoapDocument(writer);
0050 
0051     qCDebug(EWSCLI_PROTO_LOG) << reqString;
0052 
0053     qCDebugNCS(EWSCLI_REQUEST_LOG) << QStringLiteral("Starting GetFolder request (") << mIds << ")";
0054 
0055     prepare(reqString);
0056 
0057     doSend();
0058 }
0059 
0060 bool EwsGetFolderRequest::parseResult(QXmlStreamReader &reader)
0061 {
0062     return parseResponseMessage(reader, QStringLiteral("GetFolder"), [this](QXmlStreamReader &reader) {
0063         return parseFoldersResponse(reader);
0064     });
0065 }
0066 
0067 bool EwsGetFolderRequest::parseFoldersResponse(QXmlStreamReader &reader)
0068 {
0069     Response resp(reader);
0070     if (resp.responseClass() == EwsResponseUnknown) {
0071         return false;
0072     }
0073 
0074     mResponses.append(resp);
0075     if (EWSCLI_REQUEST_LOG().isDebugEnabled()) {
0076         if (resp.isSuccess()) {
0077             const EwsFolder &folder = resp.folder();
0078             const EwsId &id = folder[EwsFolderFieldFolderId].value<EwsId>();
0079             qCDebugNC(EWSCLI_REQUEST_LOG)
0080                 << QStringLiteral("Got GetFolder response (id: %1, name: %2)").arg(ewsHash(id.id()), folder[EwsFolderFieldDisplayName].toString());
0081         } else {
0082             qCDebugNC(EWSCLI_REQUEST_LOG) << QStringLiteral("Got GetFolder response - %1").arg(resp.responseMessage());
0083         }
0084     }
0085 
0086     QVariant dn = resp.folder()[EwsFolderFieldDisplayName];
0087     if (!dn.isNull()) {
0088         EwsClient::folderHash[resp.folder()[EwsFolderFieldFolderId].value<EwsId>().id()] = dn.toString();
0089     }
0090 
0091     return true;
0092 }
0093 
0094 EwsGetFolderRequest::Response::Response(QXmlStreamReader &reader)
0095     : EwsRequest::Response(reader)
0096 {
0097     while (reader.readNextStartElement()) {
0098         if (reader.namespaceUri() != ewsMsgNsUri && reader.namespaceUri() != ewsTypeNsUri) {
0099             setErrorMsg(QStringLiteral("Unexpected namespace in %1 element: %2").arg(QStringLiteral("ResponseMessage"), reader.namespaceUri().toString()));
0100             return;
0101         }
0102 
0103         if (reader.name() == QLatin1StringView("Folders")) {
0104             if (responseClass() == EwsResponseError) {
0105                 // Skip empty folders element
0106                 reader.skipCurrentElement();
0107             } else if (!parseFolders(reader)) {
0108                 return;
0109             }
0110         } else if (!readResponseElement(reader)) {
0111             setErrorMsg(QStringLiteral("Failed to read EWS request - invalid response element."));
0112             return;
0113         }
0114     }
0115 }
0116 
0117 bool EwsGetFolderRequest::Response::parseFolders(QXmlStreamReader &reader)
0118 {
0119     if (reader.namespaceUri() != ewsMsgNsUri || reader.name() != QLatin1StringView("Folders")) {
0120         return setErrorMsg(QStringLiteral("Failed to read EWS request - expected Folders element (got %1).").arg(reader.qualifiedName().toString()));
0121     }
0122 
0123     if (!reader.readNextStartElement()) {
0124         return setErrorMsg(QStringLiteral("Failed to read EWS request - expected a child element in Folders element."));
0125     }
0126 
0127     if (reader.namespaceUri() != ewsTypeNsUri) {
0128         return setErrorMsg(QStringLiteral("Failed to read EWS request - expected child element from types namespace."));
0129     }
0130 
0131     EwsFolder folder(reader);
0132     if (!folder.isValid()) {
0133         return setErrorMsg(QStringLiteral("Failed to read EWS request - invalid Folder element."));
0134     }
0135     mFolder = folder;
0136 
0137     // Finish the Folders element
0138     reader.skipCurrentElement();
0139 
0140     return true;
0141 }
0142 
0143 #include "moc_ewsgetfolderrequest.cpp"