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

0001 /*
0002     SPDX-FileCopyrightText: 2015-2019 Krzysztof Nowicki <krissn@op.pl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <functional>
0010 
0011 #include <QSharedPointer>
0012 #include <QXmlStreamReader>
0013 #include <QXmlStreamWriter>
0014 
0015 #include <KIO/TransferJob>
0016 
0017 #include "ewsclient.h"
0018 #include "ewsjob.h"
0019 #include "ewsserverversion.h"
0020 #include "ewstypes.h"
0021 
0022 class QWidget;
0023 
0024 class EwsRequest : public EwsJob
0025 {
0026     Q_OBJECT
0027 public:
0028     class Response
0029     {
0030     public:
0031         EwsResponseClass responseClass() const
0032         {
0033             return mClass;
0034         }
0035 
0036         bool isSuccess() const
0037         {
0038             return mClass == EwsResponseSuccess;
0039         }
0040 
0041         QString responseCode() const
0042         {
0043             return mCode;
0044         }
0045 
0046         QString responseMessage() const
0047         {
0048             return mMessage;
0049         }
0050 
0051     protected:
0052         Response(QXmlStreamReader &reader);
0053         bool readResponseElement(QXmlStreamReader &reader);
0054         bool setErrorMsg(const QString &msg);
0055 
0056         EwsResponseClass mClass;
0057         QString mCode;
0058         QString mMessage;
0059     };
0060 
0061     EwsRequest(EwsClient &client, QObject *parent);
0062     ~EwsRequest() override;
0063 
0064     void setMetaData(const KIO::MetaData &md);
0065     void addMetaData(const QString &key, const QString &value);
0066 
0067     void setServerVersion(const EwsServerVersion &version);
0068     const EwsServerVersion &serverVersion() const
0069     {
0070         return mServerVersion;
0071     }
0072 
0073     void dump() const;
0074 
0075 protected:
0076     typedef std::function<bool(QXmlStreamReader &reader)> ContentReaderFn;
0077 
0078     void doSend();
0079     void prepare(const QString &body);
0080     virtual bool parseResult(QXmlStreamReader &reader) = 0;
0081     void startSoapDocument(QXmlStreamWriter &writer);
0082     void endSoapDocument(QXmlStreamWriter &writer);
0083     bool parseResponseMessage(QXmlStreamReader &reader, const QString &reqName, ContentReaderFn contentReader);
0084     bool readResponse(QXmlStreamReader &reader);
0085 
0086     KIO::MetaData mMd;
0087     QString mResponseData;
0088 protected Q_SLOTS:
0089     void requestResult(KJob *job);
0090     virtual void requestData(KIO::Job *job, const QByteArray &data);
0091 
0092 private:
0093     bool readSoapBody(QXmlStreamReader &reader);
0094     bool readSoapFault(QXmlStreamReader &reader);
0095     bool readHeader(QXmlStreamReader &reader);
0096     bool readResponseAttr(const QXmlStreamAttributes &attrs, EwsResponseClass &responseClass);
0097     QString getOAuthToken();
0098     QPair<QStringView, QString> parseNamespacedString(const QString &str, const QXmlStreamNamespaceDeclarations &namespaces);
0099     EwsResponseCode parseEwsResponseCode(const QPair<QStringView, QString> &code);
0100 
0101     QString mBody;
0102     EwsClient &mClient;
0103     EwsServerVersion mServerVersion;
0104     QWidget *mParentWindow;
0105 };