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

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 "ewsupdatefolderrequest.h"
0008 #include "ewsclient_debug.h"
0009 
0010 static const QList<QString> updateTypeElementNames = {
0011     QStringLiteral("AppendToFolderField"),
0012     QStringLiteral("SetFolderField"),
0013     QStringLiteral("DeleteFolderField"),
0014 };
0015 
0016 static const QList<QString> folderTypeNames = {
0017     QStringLiteral("Folder"),
0018     QStringLiteral("CalendarFolder"),
0019     QStringLiteral("ContactsFolder"),
0020     QStringLiteral("SearchFolder"),
0021     QStringLiteral("TasksFolder"),
0022 };
0023 
0024 EwsUpdateFolderRequest::EwsUpdateFolderRequest(EwsClient &client, QObject *parent)
0025     : EwsRequest(client, parent)
0026 {
0027 }
0028 
0029 EwsUpdateFolderRequest::~EwsUpdateFolderRequest() = default;
0030 
0031 void EwsUpdateFolderRequest::start()
0032 {
0033     QString reqString;
0034     QXmlStreamWriter writer(&reqString);
0035 
0036     startSoapDocument(writer);
0037 
0038     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("UpdateFolder"));
0039 
0040     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("FolderChanges"));
0041     for (const FolderChange &ch : std::as_const(mChanges)) {
0042         ch.write(writer);
0043     }
0044     writer.writeEndElement();
0045 
0046     writer.writeEndElement();
0047 
0048     endSoapDocument(writer);
0049 
0050     qCDebugNC(EWSCLI_REQUEST_LOG) << QStringLiteral("Starting UpdateFolder request (%1 changes)").arg(mChanges.size());
0051 
0052     qCDebug(EWSCLI_PROTO_LOG) << reqString;
0053 
0054     prepare(reqString);
0055 
0056     doSend();
0057 }
0058 
0059 bool EwsUpdateFolderRequest::parseResult(QXmlStreamReader &reader)
0060 {
0061     return parseResponseMessage(reader, QStringLiteral("UpdateFolder"), [this](QXmlStreamReader &reader) {
0062         return parseItemsResponse(reader);
0063     });
0064 }
0065 
0066 bool EwsUpdateFolderRequest::parseItemsResponse(QXmlStreamReader &reader)
0067 {
0068     Response resp(reader);
0069     if (resp.responseClass() == EwsResponseUnknown) {
0070         return false;
0071     }
0072 
0073     if (EWSCLI_REQUEST_LOG().isDebugEnabled()) {
0074         if (resp.isSuccess()) {
0075             qCDebugNC(EWSCLI_REQUEST_LOG) << QStringLiteral("Got UpdateFolder response - OK");
0076         } else {
0077             qCDebugNC(EWSCLI_REQUEST_LOG) << QStringLiteral("Got UpdateFolder response - %1").arg(resp.responseMessage());
0078         }
0079     }
0080 
0081     mResponses.append(resp);
0082     return true;
0083 }
0084 
0085 EwsUpdateFolderRequest::Response::Response(QXmlStreamReader &reader)
0086     : EwsRequest::Response(reader)
0087 {
0088     if (mClass == EwsResponseParseError) {
0089         return;
0090     }
0091 
0092     while (reader.readNextStartElement()) {
0093         if (reader.namespaceUri() != ewsMsgNsUri && reader.namespaceUri() != ewsTypeNsUri) {
0094             setErrorMsg(QStringLiteral("Unexpected namespace in %1 element: %2").arg(QStringLiteral("ResponseMessage"), reader.namespaceUri().toString()));
0095             return;
0096         }
0097 
0098         if (reader.name() == QLatin1StringView("Folders")) {
0099             if (reader.readNextStartElement()) {
0100                 EwsFolder folder(reader);
0101                 if (!folder.isValid()) {
0102                     return;
0103                 }
0104                 mId = folder[EwsFolderFieldFolderId].value<EwsId>();
0105             }
0106 
0107             // Finish the Folders element.
0108             reader.skipCurrentElement();
0109         } else if (!readResponseElement(reader)) {
0110             setErrorMsg(QStringLiteral("Failed to read EWS request - invalid response element."));
0111             return;
0112         }
0113     }
0114 }
0115 
0116 bool EwsUpdateFolderRequest::Update::write(QXmlStreamWriter &writer, EwsFolderType folderType) const
0117 {
0118     bool retVal = true;
0119 
0120     writer.writeStartElement(ewsTypeNsUri, updateTypeElementNames[mType]);
0121 
0122     mField.write(writer);
0123 
0124     if (mType != Delete) {
0125         writer.writeStartElement(ewsTypeNsUri, folderTypeNames[folderType]);
0126         retVal = mField.writeWithValue(writer, mValue);
0127         writer.writeEndElement();
0128     }
0129 
0130     writer.writeEndElement();
0131 
0132     return retVal;
0133 }
0134 
0135 bool EwsUpdateFolderRequest::FolderChange::write(QXmlStreamWriter &writer) const
0136 {
0137     bool retVal = true;
0138 
0139     writer.writeStartElement(ewsTypeNsUri, QStringLiteral("FolderChange"));
0140 
0141     mId.writeFolderIds(writer);
0142 
0143     writer.writeStartElement(ewsTypeNsUri, QStringLiteral("Updates"));
0144 
0145     for (const QSharedPointer<const Update> &upd : std::as_const(mUpdates)) {
0146         if (!upd->write(writer, mType)) {
0147             retVal = false;
0148             break;
0149         }
0150     }
0151 
0152     writer.writeEndElement();
0153 
0154     writer.writeEndElement();
0155 
0156     return retVal;
0157 }
0158 
0159 #include "moc_ewsupdatefolderrequest.cpp"