1 /*
2     SPDX-FileCopyrightText: 2015-2017 Krzysztof Nowicki <krissn@op.pl>
3 
4     SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #pragma once
8 
9 #include <QDateTime>
10 #include <QList>
11 #include <QSharedPointer>
12 
13 #include "ewsid.h"
14 #include "ewsrequest.h"
15 #include "ewstypes.h"
16 
17 class QXmlStreamReader;
18 
19 class EwsEventRequestBase : public EwsRequest
20 {
21     Q_OBJECT
22 public:
23     class Notification;
24     class Response;
25 
26     class Event
27     {
28     public:
29         typedef QVector<Event> List;
30 
type()31         EwsEventType type() const
32         {
33             return mType;
34         }
35 
watermark()36         const QString &watermark() const
37         {
38             return mWatermark;
39         }
40 
timestamp()41         const QDateTime &timestamp() const
42         {
43             return mTimestamp;
44         }
45 
itemId()46         const EwsId &itemId() const
47         {
48             return mId;
49         }
50 
parentFolderId()51         const EwsId &parentFolderId() const
52         {
53             return mParentFolderId;
54         }
55 
unreadCount()56         uint unreadCount() const
57         {
58             return mUnreadCount;
59         }
60 
oldItemId()61         const EwsId &oldItemId() const
62         {
63             return mOldId;
64         }
65 
oldParentFolderId()66         const EwsId &oldParentFolderId() const
67         {
68             return mOldParentFolderId;
69         }
70 
itemIsFolder()71         bool itemIsFolder() const
72         {
73             return mIsFolder;
74         }
75 
76         bool operator==(const Event &other) const;
77 
78     protected:
79         Event(QXmlStreamReader &reader);
isValid()80         bool isValid() const
81         {
82             return mType != EwsUnknownEvent;
83         }
84 
85         EwsEventType mType;
86         QString mWatermark;
87         QDateTime mTimestamp;
88         EwsId mId;
89         EwsId mParentFolderId;
90         uint mUnreadCount = 0;
91         EwsId mOldId;
92         EwsId mOldParentFolderId;
93         bool mIsFolder;
94 
95         friend class EwsEventRequestBase::Notification;
96     };
97 
98     class Notification
99     {
100     public:
101         typedef QList<Notification> List;
102 
subscriptionId()103         const QString &subscriptionId() const
104         {
105             return mSubscriptionId;
106         }
107 
previousWatermark()108         const QString &previousWatermark() const
109         {
110             return mWatermark;
111         }
112 
hasMoreEvents()113         bool hasMoreEvents() const
114         {
115             return mMoreEvents;
116         }
117 
events()118         const Event::List &events() const
119         {
120             return mEvents;
121         }
122 
123         bool operator==(const Notification &other) const;
124 
125     protected:
126         Notification(QXmlStreamReader &reader);
127 
isValid()128         bool isValid() const
129         {
130             return !mSubscriptionId.isNull();
131         }
132 
133         static bool eventsReader(QXmlStreamReader &reader, QVariant &val);
134 
135         QString mSubscriptionId;
136         QString mWatermark;
137         bool mMoreEvents;
138         Event::List mEvents;
139 
140         friend class EwsEventRequestBase::Response;
141     };
142 
143     class Response : public EwsRequest::Response
144     {
145     public:
notifications()146         const Notification::List &notifications() const
147         {
148             return mNotifications;
149         }
150 
151         bool operator==(const Response &other) const;
152 
153     protected:
154         Response(QXmlStreamReader &reader);
155 
156         Notification::List mNotifications;
157 
158         friend class EwsEventRequestBase;
159     };
160 
161     ~EwsEventRequestBase() override;
162 
setSubscriptionId(const QString & id)163     void setSubscriptionId(const QString &id)
164     {
165         mSubscriptionId = id;
166     }
167 
responses()168     const QList<Response> &responses() const
169     {
170         return mResponses;
171     }
172 
173 protected:
174     EwsEventRequestBase(EwsClient &client, const QString &reqName, QObject *parent);
175     bool parseResult(QXmlStreamReader &reader) override;
176     bool parseNotificationsResponse(QXmlStreamReader &reader);
177 
178     QString mSubscriptionId;
179     QList<Response> mResponses;
180     const QString mReqName;
181 };
182 
183 Q_DECLARE_METATYPE(EwsEventRequestBase::Event::List)
184 
185