File indexing completed on 2024-06-23 05:32:17

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 <QPalette>
0011 #include <QQmlParserStatus>
0012 #include <QSize>
0013 #include <QUrl>
0014 
0015 #include <KDirWatch>
0016 
0017 #include "../provider/providertype.h"
0018 #include "backgroundtype.h"
0019 
0020 namespace KPackage
0021 {
0022 class Package;
0023 }
0024 
0025 /**
0026  * A proxy class that converts a provider url to a real resource url.
0027  */
0028 class MediaProxy : public QObject, public QQmlParserStatus
0029 {
0030     Q_OBJECT
0031     Q_INTERFACES(QQmlParserStatus)
0032 
0033     /**
0034      * Package path from the saved configuration, can be an image file, a url with
0035      * "image://" scheme or a folder (KPackage).
0036      */
0037     Q_PROPERTY(QString source READ source WRITE setSource NOTIFY sourceChanged)
0038 
0039     /**
0040      * The real path of the image
0041      * e.g. /home/kde/Pictures/image.png
0042      *      image://package/get? (KPackage)
0043      */
0044     Q_PROPERTY(QUrl modelImage READ modelImage NOTIFY modelImageChanged)
0045 
0046     /**
0047      * Type of the current background provider
0048      */
0049     Q_PROPERTY(Provider::Type providerType MEMBER m_providerType NOTIFY providerTypeChanged)
0050 
0051     /**
0052      * Type of the current wallpaper
0053      */
0054     Q_PROPERTY(BackgroundType::Type backgroundType MEMBER m_backgroundType NOTIFY backgroundTypeChanged)
0055 
0056     Q_PROPERTY(QSize targetSize READ targetSize WRITE setTargetSize NOTIFY targetSizeChanged)
0057 
0058     Q_PROPERTY(QColor customColor MEMBER m_customColor NOTIFY customColorChanged)
0059 
0060 public:
0061     explicit MediaProxy(QObject *parent = nullptr);
0062 
0063     void classBegin() override;
0064     void componentComplete() override;
0065 
0066     QString source() const;
0067     void setSource(const QString &url);
0068 
0069     QUrl modelImage() const;
0070 
0071     QSize targetSize() const;
0072     void setTargetSize(const QSize &size);
0073 
0074     Provider::Type providerType() const;
0075 
0076     Q_INVOKABLE void openModelImage();
0077 
0078     Q_INVOKABLE void useSingleImageDefaults();
0079 
0080 Q_SIGNALS:
0081     void sourceChanged();
0082     void modelImageChanged();
0083 
0084     /**
0085      * Emitted when the type of the current wallpaper changes.
0086      */
0087     void backgroundTypeChanged();
0088 
0089     void targetSizeChanged(const QSize &size);
0090 
0091     /**
0092      * Emitted when the target size changes while the provider type is single image
0093      * or the current wallpaper is animated.
0094      * The frontend is required to force update the wallpaper.
0095      */
0096     void actualSizeChanged();
0097 
0098     /**
0099      * Emitted when system color scheme changes. The frontend is required to
0100      * reload the wallpaper even if the image path is not changed.
0101      */
0102     void colorSchemeChanged();
0103 
0104     /**
0105      * Emitted when the type of the background provider changes.
0106      * @see determineProviderType
0107      */
0108     void providerTypeChanged();
0109 
0110     /**
0111      * The wallpaper package has specified a custom accent color, or
0112      * @c transparent if no/invalid color is specified.
0113      */
0114     void customColorChanged();
0115 
0116     /**
0117      * Emitted when the current source file has changed.
0118      */
0119     void sourceFileUpdated();
0120 
0121 private Q_SLOTS:
0122     /**
0123      * Switches to dark-colored wallpaper if available when system color
0124      * scheme is dark.
0125      *
0126      * @since 5.26
0127      */
0128     void slotSystemPaletteChanged(const QPalette &palette);
0129 
0130     /**
0131      * Reloads the current wallpaper if the current source file has changed on disk.
0132      *
0133      * @since 6.0
0134      */
0135     void slotSourceFileUpdated(const QString &path);
0136 
0137 private:
0138     static inline bool isDarkColorScheme(const QPalette &palette = {});
0139 
0140     /**
0141      * Reads accent color from wallpaper metadata. The value is stored under key
0142      * @c X-KDE-PlasmaImageWallpaper-AccentColor in metadata.json.
0143      *
0144      * @param package wallpaper package
0145      * @return custom accent color from the wallpaper package
0146      * @since 5.27
0147      */
0148     static QColor getAccentColorFromMetaData(const KPackage::Package &package);
0149 
0150     void determineBackgroundType(KPackage::Package *package);
0151     void determineProviderType();
0152     void processSource(KPackage::Package *package = nullptr, bool doesBlockSignal = false);
0153 
0154     QUrl findPreferredImageInPackage(KPackage::Package &package);
0155     void updateModelImage(KPackage::Package *package, bool doesBlockSignal = false);
0156     void updateModelImageWithoutSignal(KPackage::Package *package);
0157 
0158     bool m_ready = false;
0159 
0160     QUrl m_source;
0161     bool m_isDefaultSource = false;
0162     QUrl m_modelImage;
0163     BackgroundType::Type m_backgroundType = BackgroundType::Type::Unknown;
0164     Provider::Type m_providerType = Provider::Type::Unknown;
0165 
0166     QSize m_targetSize;
0167     QColor m_customColor = Qt::transparent;
0168 
0169     bool m_isDarkColorScheme;
0170     KDirWatch m_dirWatch;
0171 
0172     friend class ImageFrontendTest;
0173 };