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

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 "ewssubscriberequest.h"
0008 
0009 #include <QXmlStreamWriter>
0010 
0011 #include "ewsclient_debug.h"
0012 
0013 static const QList<QString> subscribeTypeNames = {
0014     QStringLiteral("PullSubscriptionRequest"),
0015     QStringLiteral("PushSubscriptionRequest"),
0016     QStringLiteral("StreamingSubscriptionRequest"),
0017 };
0018 
0019 static const QList<QString> eventTypeNames = {
0020     QStringLiteral("CopiedEvent"),
0021     QStringLiteral("CreatedEvent"),
0022     QStringLiteral("DeletedEvent"),
0023     QStringLiteral("ModifiedEvent"),
0024     QStringLiteral("MovedEvent"),
0025     QStringLiteral("NewMailEvent"),
0026     QStringLiteral("FreeBusyChangedEvent"),
0027 };
0028 
0029 EwsSubscribeRequest::EwsSubscribeRequest(EwsClient &client, QObject *parent)
0030     : EwsRequest(client, parent)
0031     , mType(PullSubscription)
0032     , mAllFolders(false)
0033     , mTimeout(30)
0034 {
0035 }
0036 
0037 EwsSubscribeRequest::~EwsSubscribeRequest() = default;
0038 
0039 void EwsSubscribeRequest::start()
0040 {
0041     QString reqString;
0042     QXmlStreamWriter writer(&reqString);
0043 
0044     if (mType == StreamingSubscription && !serverVersion().supports(EwsServerVersion::StreamingSubscription)) {
0045         setServerVersion(EwsServerVersion::minSupporting(EwsServerVersion::StreamingSubscription));
0046     }
0047     if (mEventTypes.contains(EwsFreeBusyChangedEvent) && !serverVersion().supports(EwsServerVersion::FreeBusyChangedEvent)) {
0048         setServerVersion(EwsServerVersion::minSupporting(EwsServerVersion::FreeBusyChangedEvent));
0049     }
0050 
0051     startSoapDocument(writer);
0052 
0053     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("Subscribe"));
0054 
0055     writer.writeStartElement(ewsMsgNsUri, subscribeTypeNames[mType]);
0056 
0057     if (mAllFolders) {
0058         writer.writeAttribute(QStringLiteral("SubscribeToAllFolders"), QStringLiteral("true"));
0059     }
0060 
0061     writer.writeStartElement(ewsTypeNsUri, QStringLiteral("FolderIds"));
0062     for (const EwsId &id : std::as_const(mFolderIds)) {
0063         id.writeFolderIds(writer);
0064     }
0065     writer.writeEndElement();
0066 
0067     writer.writeStartElement(ewsTypeNsUri, QStringLiteral("EventTypes"));
0068     for (const EwsEventType type : std::as_const(mEventTypes)) {
0069         writer.writeTextElement(ewsTypeNsUri, QStringLiteral("EventType"), eventTypeNames[type]);
0070     }
0071     writer.writeEndElement(); // EventTypes
0072 
0073     if (mType == PullSubscription) {
0074         if (!mWatermark.isNull()) {
0075             writer.writeTextElement(ewsTypeNsUri, QStringLiteral("Watermark"), mWatermark);
0076         }
0077         writer.writeTextElement(ewsTypeNsUri, QStringLiteral("Timeout"), QString::number(mTimeout));
0078     }
0079 
0080     writer.writeEndElement(); // XxxSubscriptionRequest
0081 
0082     writer.writeEndElement(); // Subscribe
0083 
0084     endSoapDocument(writer);
0085 
0086     qCDebug(EWSCLI_PROTO_LOG) << reqString;
0087 
0088     prepare(reqString);
0089 
0090     doSend();
0091 }
0092 
0093 bool EwsSubscribeRequest::parseResult(QXmlStreamReader &reader)
0094 {
0095     return parseResponseMessage(reader, QStringLiteral("Subscribe"), [this](QXmlStreamReader &reader) {
0096         return parseSubscribeResponse(reader);
0097     });
0098 }
0099 
0100 bool EwsSubscribeRequest::parseSubscribeResponse(QXmlStreamReader &reader)
0101 {
0102     QSharedPointer<Response> resp(new Response(reader));
0103     if (resp->responseClass() == EwsResponseUnknown) {
0104         return false;
0105     }
0106 
0107     mResponse = resp;
0108     return true;
0109 }
0110 
0111 EwsSubscribeRequest::Response::Response(QXmlStreamReader &reader)
0112     : EwsRequest::Response(reader)
0113 {
0114     if (mClass == EwsResponseParseError) {
0115         return;
0116     }
0117 
0118     while (reader.readNextStartElement()) {
0119         if (reader.namespaceUri() != ewsMsgNsUri && reader.namespaceUri() != ewsTypeNsUri) {
0120             setErrorMsg(QStringLiteral("Unexpected namespace in %1 element: %2").arg(QStringLiteral("ResponseMessage"), reader.namespaceUri().toString()));
0121             return;
0122         }
0123 
0124         if (reader.name() == QLatin1StringView("SubscriptionId")) {
0125             mId = reader.readElementText();
0126 
0127             if (reader.error() != QXmlStreamReader::NoError) {
0128                 setErrorMsg(QStringLiteral("Failed to read EWS request - invalid %1 element.").arg(QStringLiteral("SubscriptionId")));
0129                 return;
0130             }
0131         } else if (reader.name() == QLatin1StringView("Watermark")) {
0132             mWatermark = reader.readElementText();
0133 
0134             if (reader.error() != QXmlStreamReader::NoError) {
0135                 setErrorMsg(QStringLiteral("Failed to read EWS request - invalid %1 element.").arg(QStringLiteral("Watermark")));
0136                 return;
0137             }
0138         } else if (!readResponseElement(reader)) {
0139             setErrorMsg(QStringLiteral("Failed to read EWS request - invalid response element."));
0140             return;
0141         }
0142     }
0143 }
0144 
0145 #include "moc_ewssubscriberequest.cpp"