File indexing completed on 2024-04-28 09:25:32

0001 /*
0002     SPDX-FileCopyrightText: 2018 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef PLASMABACKGROUNDCACHE_H
0007 #define PLASMABACKGROUNDCACHE_H
0008 
0009 // local
0010 #include "screenpool.h"
0011 
0012 // Qt
0013 #include <QHash>
0014 #include <QObject>
0015 
0016 // Plasma
0017 #include <Plasma>
0018 
0019 // KDE
0020 #include <KConfigGroup>
0021 #include <KSharedConfig>
0022 
0023 struct imageHints {
0024     bool busy{false};
0025     float brightness{-1000};
0026 };
0027 
0028 typedef QHash<Plasma::Types::Location, imageHints> EdgesHash;
0029 
0030 namespace Latte {
0031 namespace PlasmaExtended {
0032 
0033 class BackgroundCache: public QObject
0034 {
0035     Q_OBJECT
0036 
0037 public:
0038     static BackgroundCache *self();
0039     ~BackgroundCache() override;
0040 
0041     bool busyFor(QString activity, QString screen, Plasma::Types::Location location);
0042     float brightnessFor(QString activity, QString screen, Plasma::Types::Location location);
0043 
0044     QString background(QString activity, QString screen) const;
0045 
0046     void setBackgroundFromBroadcast(QString activity, QString screen, QString filename);
0047     void setBroadcastedBackgroundsEnabled(QString activity, QString screen, bool enabled);
0048 
0049 signals:
0050     void backgroundChanged(const QString &activity, const QString &screenName);
0051 
0052 private slots:
0053     void reload();
0054     void settingsFileChanged(const QString &file);
0055 
0056 private:
0057     BackgroundCache(QObject *parent = nullptr);
0058 
0059     bool backgroundIsBroadcasted(QString activity, QString screenName) const;
0060     bool pluginExistsFor(QString activity, QString screenName) const;
0061     bool areaIsBusy(float bright1, float bright2) const;
0062     bool busyForFile(QString imageFile, Plasma::Types::Location location);
0063     bool isDesktopContainment(const KConfigGroup &containment) const;
0064 
0065     float brightnessForFile(QString imageFile, Plasma::Types::Location location);
0066     float brightnessFromArea(QImage &image, int firstRow, int firstColumn, int endRow, int endColumn);
0067     QString backgroundFromConfig(const KConfigGroup &config, QString wallpaperPlugin) const;
0068 
0069     void cleanupHashes();
0070     void updateImageCalculations(QString imageFile, Plasma::Types::Location location);
0071 
0072 private:
0073     bool m_initialized{false};
0074 
0075     QString m_defaultWallpaperPath;
0076 
0077     ScreenPool *m_pool{nullptr};
0078 
0079     //! screen aware backgrounds: activity id, screen name, backgroundfile
0080     QHash<QString, QHash<QString, QString>> m_backgrounds;
0081 
0082     //! plugin names tracked: activity id, screen name, pluginName
0083     QHash<QString, QHash<QString, QString>> m_plugins;
0084 
0085     //! backgrounds that are broadcasted (not found through plasma files):
0086     //! and have higher priority: activity id, screen names
0087     QHash<QString, QList<QString>> m_broadcasted;
0088 
0089     //! image file and brightness per edge
0090     QHash<QString, EdgesHash> m_hintsCache;
0091 
0092     KSharedConfig::Ptr m_plasmaConfig;
0093 };
0094 
0095 }
0096 }
0097 
0098 #endif