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 "ewsgetstreamingeventsrequest.h"
0008 
0009 #include <QTemporaryFile>
0010 
0011 #include "ewsclient_debug.h"
0012 
0013 static constexpr uint respChunkTimeout = 250; /* ms */
0014 
0015 EwsGetStreamingEventsRequest::EwsGetStreamingEventsRequest(EwsClient &client, QObject *parent)
0016     : EwsEventRequestBase(client, QStringLiteral("GetStreamingEvents"), parent)
0017     , mTimeout(30)
0018     , mRespTimer(this)
0019 {
0020     mRespTimer.setInterval(respChunkTimeout);
0021     connect(&mRespTimer, &QTimer::timeout, this, &EwsGetStreamingEventsRequest::requestDataTimeout);
0022 }
0023 
0024 EwsGetStreamingEventsRequest::~EwsGetStreamingEventsRequest() = default;
0025 
0026 void EwsGetStreamingEventsRequest::start()
0027 {
0028     QString reqString;
0029     QXmlStreamWriter writer(&reqString);
0030 
0031     if (!serverVersion().supports(EwsServerVersion::StreamingSubscription)) {
0032         setServerVersion(EwsServerVersion::minSupporting(EwsServerVersion::StreamingSubscription));
0033     }
0034 
0035     startSoapDocument(writer);
0036 
0037     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("GetStreamingEvents"));
0038 
0039     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("SubscriptionIds"));
0040     writer.writeTextElement(ewsTypeNsUri, QStringLiteral("SubscriptionId"), mSubscriptionId);
0041     writer.writeEndElement();
0042 
0043     writer.writeTextElement(ewsMsgNsUri, QStringLiteral("ConnectionTimeout"), QString::number(mTimeout));
0044 
0045     writer.writeEndElement();
0046 
0047     endSoapDocument(writer);
0048 
0049     qCDebugNC(EWSCLI_REQUEST_LOG) << QStringLiteral("Starting GetStreamingEvents request (subId: %1, timeout: %2)").arg(ewsHash(mSubscriptionId)).arg(mTimeout);
0050 
0051     qCDebug(EWSCLI_PROTO_LOG) << reqString;
0052 
0053     prepare(reqString);
0054 
0055     doSend();
0056 }
0057 
0058 void EwsGetStreamingEventsRequest::requestData(KIO::Job *job, const QByteArray &data)
0059 {
0060     Q_UNUSED(job)
0061 
0062     mRespTimer.stop();
0063     qCDebug(EWSCLI_PROTO_LOG) << "data" << job << data;
0064     mResponseData += QString::fromUtf8(data);
0065     mRespTimer.start();
0066 }
0067 
0068 void EwsGetStreamingEventsRequest::requestDataTimeout()
0069 {
0070     if (mResponseData.isEmpty()) {
0071         return;
0072     }
0073     if (EWSCLI_PROTO_LOG().isDebugEnabled()) {
0074         ewsLogDir.setAutoRemove(false);
0075         if (ewsLogDir.isValid()) {
0076             QTemporaryFile dumpFile(ewsLogDir.path() + QStringLiteral("/ews_xmldump_XXXXXXX.xml"));
0077             dumpFile.open();
0078             dumpFile.setAutoRemove(false);
0079             dumpFile.write(mResponseData.toUtf8());
0080             qCDebug(EWSCLI_PROTO_LOG) << "response dumped to" << dumpFile.fileName();
0081             dumpFile.close();
0082         }
0083     }
0084 
0085     QXmlStreamReader reader(mResponseData);
0086     if (!readResponse(reader)) {
0087         const auto jobs{subjobs()};
0088         for (KJob *job : jobs) {
0089             removeSubjob(job);
0090             job->kill();
0091         }
0092         emitResult();
0093     } else {
0094         Q_EMIT eventsReceived(this);
0095     }
0096 
0097     mResponseData.clear();
0098 }
0099 
0100 void EwsGetStreamingEventsRequest::eventsProcessed(const Response &resp)
0101 {
0102     mResponses.removeOne(resp);
0103 }
0104 
0105 #include "moc_ewsgetstreamingeventsrequest.cpp"