1 /*
2   This file is part of KOrganizer.
3   SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org>
4   SPDX-FileCopyrightText: 2007 Loïc Corbasson <loic.corbasson@gmail.com>
5   SPDX-FileCopyrightText: 2021 Friedrich W. H. Kossebau <kossebau@kde.org>
6 
7   SPDX-License-Identifier: GPL-2.0-or-later
8 */
9 
10 #pragma once
11 
12 #include <EventViews/CalendarDecoration>
13 using namespace EventViews::CalendarDecoration;
14 
15 #include <KIO/SimpleJob>
16 
17 #include <QUrl>
18 
19 enum DataState {
20     LoadingFailed = -1,
21     NeedingPageData = 0,
22     NeedingBasicImageInfo,
23     NeedingFirstThumbImageInfo,
24     NeedingFirstThumbImage,
25     DataLoaded,
26     NeedingNextThumbImageInfo,
27     NeedingNextThumbImage,
28 };
29 
30 struct ElementData {
31     float mPictureHWRatio = 1;
32     QString mPictureName;
33     QUrl mAboutPageUrl;
34     QSize mThumbSize;
35     QSize mFetchedThumbSize;
36     QPixmap mThumbnail;
37     QString mTitle;
38 
39     DataState mState = NeedingPageData;
40 
41     void updateFetchedThumbSize();
42 };
43 
44 class POTDElement : public Element
45 {
46     Q_OBJECT
47 
48 public:
49     POTDElement(const QString &id, QDate date, ElementData *data);
50     ~POTDElement() override;
51 
52 public: // Element API
53     Q_REQUIRED_RESULT QString shortText() const override;
54     Q_REQUIRED_RESULT QString longText() const override;
55     Q_REQUIRED_RESULT QUrl url() const override;
56     Q_REQUIRED_RESULT QPixmap newPixmap(const QSize &size) override;
57 
58 private:
59     void queryImagesJson();
60     void queryBasicImageInfoJson();
61     void queryThumbImageInfoJson();
62     void getThumbImage(const QUrl &thumbUrl);
63 
64     // POTD pages once decided about should get an edit-protected variant, but not all have that
65     enum PageProtectionState { ProtectedPage, UnprotectedPage };
66     KIO::SimpleJob *createImagesJsonQueryJob(PageProtectionState pageProtectionState);
67 
68     struct QueryItem {
69         QString key;
70         QString value;
71     };
72     KIO::SimpleJob *createJsonQueryJob(const QString &property, const QString &title, const QList<QueryItem> &otherQueryItems = {});
73 
74     void handleImagesJsonResponse(KJob *job, PageProtectionState pageProtectionState);
75 
76     void setLoadingFailed();
77 
78 private Q_SLOTS:
79     void handleProtectedImagesJsonResponse(KJob *job);
80     void handleUnprotectedImagesJsonResponse(KJob *job);
81     void handleBasicImageInfoJsonResponse(KJob *job);
82     void handleThumbImageInfoJsonResponse(KJob *job);
83     void handleGetThumbImageResponse(KJob *job);
84     void completeMissingData();
85 
86 private:
87     const QDate mDate;
88     QSize mRequestedThumbSize;
89 
90     ElementData *const mData;
91 
92     QTimer *const mThumbImageGetDelayTimer;
93     KIO::SimpleJob *mQueryThumbImageInfoJob = nullptr;
94     KIO::SimpleJob *mGetThumbImageJob = nullptr;
95 };
96