File indexing completed on 2024-05-05 05:34:05

0001 /*
0002     SPDX-FileCopyrightText: 2019 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "schemes.h"
0007 
0008 // local
0009 #include "../abstractwindowinterface.h"
0010 #include "../../lattecorona.h"
0011 #include "../../tools/commontools.h"
0012 
0013 // Qt
0014 #include <QDir>
0015 #include <QLatin1String>
0016 
0017 // KDE
0018 #include <KDirWatch>
0019 
0020 
0021 namespace Latte {
0022 namespace WindowSystem {
0023 namespace Tracker {
0024 
0025 Schemes::Schemes(AbstractWindowInterface *parent)
0026     : QObject(parent)
0027 {
0028     m_wm = parent;
0029     init();
0030 }
0031 
0032 Schemes::~Schemes()
0033 {
0034     m_windowScheme.clear();
0035     //! it is just a reference to a real scheme file
0036     m_schemes.take("kdeglobals");
0037     qDeleteAll(m_schemes.values());
0038     m_schemes.clear();
0039 }
0040 
0041 void Schemes::init()
0042 {
0043     updateDefaultScheme();
0044 
0045     connect(this, &Schemes::colorSchemeChanged, this, [&](WindowId wid) {
0046         if (wid == m_wm->activeWindow()) {
0047             emit m_wm->activeWindowChanged(wid);
0048         }
0049     });
0050 
0051     connect(m_wm, &AbstractWindowInterface::windowRemoved, this, [&](WindowId wid) {
0052         m_windowScheme.remove(wid);
0053     });
0054 
0055     //! track for changing default scheme
0056     QString kdeSettingsFile = Latte::configPath() + "/kdeglobals";
0057 
0058     KDirWatch::self()->addFile(kdeSettingsFile);
0059 
0060     connect(KDirWatch::self(), &KDirWatch::dirty, this, [ &, kdeSettingsFile](const QString & path) {
0061         if (path == kdeSettingsFile) {
0062             this->updateDefaultScheme();
0063         }
0064     });
0065 
0066     connect(KDirWatch::self(), &KDirWatch::created, this, [ &, kdeSettingsFile](const QString & path) {
0067         if (path == kdeSettingsFile) {
0068             this->updateDefaultScheme();
0069         }
0070     });
0071 }
0072 
0073 //! Scheme support for windows
0074 void Schemes::updateDefaultScheme()
0075 {
0076     QString defaultSchemePath = SchemeColors::possibleSchemeFile("kdeglobals");
0077 
0078     qDebug() << " Windows default color scheme :: " << defaultSchemePath;
0079 
0080     SchemeColors *dScheme;
0081 
0082     if (!m_schemes.contains(defaultSchemePath)) {
0083         dScheme = new SchemeColors(this, defaultSchemePath);
0084         m_schemes[defaultSchemePath] = dScheme;
0085     } else {
0086         dScheme = m_schemes[defaultSchemePath];
0087     }
0088 
0089     if (!m_schemes.contains("kdeglobals") || m_schemes["kdeglobals"]->schemeFile() != defaultSchemePath) {
0090         m_schemes["kdeglobals"] = dScheme;
0091     }
0092 
0093     emit defaultSchemeChanged();
0094 }
0095 
0096 SchemeColors *Schemes::schemeForFile(const QString &scheme)
0097 {
0098     QString schemeFile = SchemeColors::possibleSchemeFile(scheme);
0099 
0100     if (!schemeFile.isEmpty() && !m_schemes.contains(schemeFile)) {
0101         //! when this scheme file has not been loaded yet
0102         m_schemes[schemeFile] = new SchemeColors(this, schemeFile);
0103     }
0104 
0105     return m_schemes.contains(schemeFile) ? m_schemes[schemeFile] : nullptr;
0106 }
0107 
0108 SchemeColors *Schemes::schemeForWindow(WindowId wid)
0109 {
0110     if (!m_windowScheme.contains(wid)) {
0111         return m_schemes["kdeglobals"];
0112     } else {
0113         return m_schemes[m_windowScheme[wid]];
0114     }
0115 
0116     return nullptr;
0117 }
0118 
0119 void Schemes::setColorSchemeForWindow(WindowId wid, QString scheme)
0120 {
0121     if (scheme == QLatin1String("kdeglobals") && !m_windowScheme.contains(wid)) {
0122         //default scheme does not have to be set
0123         return;
0124     }
0125 
0126     if (scheme == QLatin1String("kdeglobals")) {
0127         //! a window that previously had an explicit set scheme now is set back to default scheme
0128         m_windowScheme.remove(wid);
0129     } else {
0130         QString schemeFile = SchemeColors::possibleSchemeFile(scheme);
0131 
0132         if (!m_schemes.contains(schemeFile)) {
0133             //! when this scheme file has not been loaded yet
0134             m_schemes[schemeFile] = new SchemeColors(this, schemeFile);
0135         }
0136 
0137         m_windowScheme[wid] = schemeFile;
0138     }
0139 
0140     emit colorSchemeChanged(wid);
0141 }
0142 
0143 }
0144 }
0145 }