File indexing completed on 2024-05-12 16:59:41

0001 /*
0002     SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QObject>
0010 #include <QQmlParserStatus>
0011 #include <QUrl>
0012 #include <QVariantList>
0013 
0014 #include "potdengine.h"
0015 
0016 /**
0017  * This class provides the backend of Pictures of The Day from various online
0018  * websites.
0019  */
0020 class PotdBackend : public QObject, public QQmlParserStatus
0021 {
0022     Q_OBJECT
0023     Q_INTERFACES(QQmlParserStatus)
0024 
0025     Q_PROPERTY(QString identifier READ identifier WRITE setIdentifier NOTIFY identifierChanged)
0026     Q_PROPERTY(QVariantList arguments READ arguments WRITE setArguments NOTIFY argumentsChanged)
0027 
0028     /**
0029      * Read-only properties that expose data from the provider.
0030      */
0031     Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged)
0032     Q_PROPERTY(QString localUrl READ localUrl NOTIFY localUrlChanged)
0033     /**
0034      * @return The website URL of the image
0035      */
0036     Q_PROPERTY(QUrl infoUrl READ infoUrl NOTIFY infoUrlChanged)
0037     /**
0038      * @return The remote image URL
0039      */
0040     Q_PROPERTY(QUrl remoteUrl READ remoteUrl NOTIFY remoteUrlChanged)
0041     Q_PROPERTY(QString title READ title NOTIFY titleChanged)
0042     Q_PROPERTY(QString author READ author NOTIFY authorChanged)
0043 
0044     /**
0045      * @return the result of the file operation.
0046      */
0047     Q_PROPERTY(FileOperationStatus saveStatus MEMBER m_saveStatus NOTIFY saveStatusChanged)
0048 
0049     /**
0050      * @return the status message after a save operation.
0051      */
0052     Q_PROPERTY(QString saveStatusMessage MEMBER m_saveStatusMessage CONSTANT)
0053 
0054     /**
0055      * @return the folder path of the saved image file.
0056      */
0057     Q_PROPERTY(QUrl savedFolder MEMBER m_savedFolder CONSTANT)
0058 
0059     /**
0060      * @return the path of the saved image file.
0061      */
0062     Q_PROPERTY(QUrl savedUrl MEMBER m_savedUrl CONSTANT)
0063 
0064     /**
0065      * @return NetworkMangerQt is available on the system
0066      */
0067     Q_PROPERTY(bool networkManagerQtAvailable MEMBER m_networkManagerQtAvailable CONSTANT)
0068 
0069     /**
0070      * @return Whether to update wallpapers over metered connection
0071      */
0072     Q_PROPERTY(
0073         int updateOverMeteredConnection READ doesUpdateOverMeteredConnection WRITE setUpdateOverMeteredConnection NOTIFY updateOverMeteredConnectionChanged)
0074 
0075 public:
0076     enum class FileOperationStatus {
0077         None,
0078         Successful,
0079         Failed,
0080     };
0081     Q_ENUM(FileOperationStatus)
0082 
0083     explicit PotdBackend(QObject *parent = nullptr);
0084     ~PotdBackend() override;
0085 
0086     void classBegin() override;
0087     void componentComplete() override;
0088 
0089     QString identifier() const;
0090     void setIdentifier(const QString &identifier);
0091 
0092     QVariantList arguments() const;
0093     void setArguments(const QVariantList &args);
0094 
0095     bool loading() const;
0096     QString localUrl() const;
0097     QUrl infoUrl() const;
0098     QUrl remoteUrl() const;
0099     QString title() const;
0100     QString author() const;
0101 
0102     int doesUpdateOverMeteredConnection() const;
0103     void setUpdateOverMeteredConnection(int value);
0104 
0105     /**
0106      * Opens a Save dialog to choose the save location, and copies the source file to the
0107      * selected destination.
0108      */
0109     Q_INVOKABLE void saveImage();
0110 
0111 Q_SIGNALS:
0112     void identifierChanged();
0113     void argumentsChanged();
0114 
0115     void imageChanged();
0116     void loadingChanged();
0117     void localUrlChanged();
0118     void infoUrlChanged();
0119     void remoteUrlChanged();
0120     void titleChanged();
0121     void authorChanged();
0122 
0123     void saveStatusChanged();
0124     void updateOverMeteredConnectionChanged();
0125 
0126 private:
0127     void registerClient();
0128 
0129     bool m_ready = false;
0130     bool m_networkManagerQtAvailable = false;
0131 
0132     QString m_identifier;
0133     QVariantList m_args;
0134 
0135     QUrl m_savedFolder;
0136     QUrl m_savedUrl;
0137     FileOperationStatus m_saveStatus = FileOperationStatus::None;
0138     QString m_saveStatusMessage;
0139 
0140     int m_doesUpdateOverMeteredConnection = 0;
0141 
0142     PotdClient *m_client = nullptr;
0143 };