File indexing completed on 2025-01-05 04:49:36

0001 /*
0002   This file is part of KOrganizer.
0003   SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org>
0004   SPDX-FileCopyrightText: 2007 Loïc Corbasson <loic.corbasson@gmail.com>
0005   SPDX-FileCopyrightText: 2021 Friedrich W. H. Kossebau <kossebau@kde.org>
0006 
0007   SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #pragma once
0011 
0012 #include <EventViews/CalendarDecoration>
0013 using namespace EventViews::CalendarDecoration;
0014 
0015 #include <KIO/SimpleJob>
0016 
0017 #include <QUrl>
0018 
0019 enum DataState {
0020     LoadingFailed = -1,
0021     NeedingPageData = 0,
0022     NeedingBasicImageInfo,
0023     NeedingFirstThumbImageInfo,
0024     NeedingFirstThumbImage,
0025     DataLoaded,
0026     NeedingNextThumbImageInfo,
0027     NeedingNextThumbImage,
0028 };
0029 
0030 struct ElementData {
0031     float mPictureHWRatio = 1;
0032     QString mPictureName;
0033     QUrl mAboutPageUrl;
0034     QSize mThumbSize;
0035     QSize mFetchedThumbSize;
0036     QPixmap mThumbnail;
0037     QString mTitle;
0038 
0039     DataState mState = NeedingPageData;
0040 
0041     void updateFetchedThumbSize();
0042 };
0043 
0044 class POTDElement : public Element
0045 {
0046     Q_OBJECT
0047 
0048 public:
0049     POTDElement(const QString &id, QDate date, ElementData *data);
0050     ~POTDElement() override;
0051 
0052 public: // Element API
0053     [[nodiscard]] QString shortText() const override;
0054     [[nodiscard]] QString longText() const override;
0055     [[nodiscard]] QUrl url() const override;
0056     [[nodiscard]] QPixmap newPixmap(const QSize &size) override;
0057 
0058 private:
0059     void queryImagesJson();
0060     void queryBasicImageInfoJson();
0061     void queryThumbImageInfoJson();
0062     void getThumbImage(const QUrl &thumbUrl);
0063 
0064     // POTD pages once decided about should get an edit-protected variant, but not all have that
0065     enum PageProtectionState { ProtectedPage, UnprotectedPage };
0066     KIO::SimpleJob *createImagesJsonQueryJob(PageProtectionState pageProtectionState);
0067 
0068     struct QueryItem {
0069         QString key;
0070         QString value;
0071     };
0072     KIO::SimpleJob *createJsonQueryJob(const QString &property, const QString &title, const QList<QueryItem> &otherQueryItems = {});
0073 
0074     void handleImagesJsonResponse(KJob *job, PageProtectionState pageProtectionState);
0075 
0076     void setLoadingFailed();
0077 
0078 private Q_SLOTS:
0079     void handleProtectedImagesJsonResponse(KJob *job);
0080     void handleUnprotectedImagesJsonResponse(KJob *job);
0081     void handleBasicImageInfoJsonResponse(KJob *job);
0082     void handleThumbImageInfoJsonResponse(KJob *job);
0083     void handleGetThumbImageResponse(KJob *job);
0084     void completeMissingData();
0085 
0086 private:
0087     const QDate mDate;
0088     QSize mRequestedThumbSize;
0089 
0090     ElementData *const mData;
0091 
0092     QTimer *const mThumbImageGetDelayTimer;
0093     KIO::SimpleJob *mQueryThumbImageInfoJob = nullptr;
0094     KIO::SimpleJob *mGetThumbImageJob = nullptr;
0095 };