File indexing completed on 2024-04-28 16:49:31

0001 /*
0002 *  Copyright 2019  Michail Vourlakos <mvourlakos@gmail.com>
0003 *
0004 *  This file is part of Latte-Dock
0005 *
0006 *  Latte-Dock is free software; you can redistribute it and/or
0007 *  modify it under the terms of the GNU General Public License as
0008 *  published by the Free Software Foundation; either version 2 of
0009 *  the License, or (at your option) any later version.
0010 *
0011 *  Latte-Dock is distributed in the hope that it will be useful,
0012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 *  GNU General Public License for more details.
0015 *
0016 *  You should have received a copy of the GNU General Public License
0017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 
0020 #include "schemes.h"
0021 
0022 // local
0023 #include "../abstractwindowinterface.h"
0024 #include "../../lattecorona.h"
0025 
0026 // Qt
0027 #include <QDir>
0028 
0029 // KDE
0030 #include <KDirWatch>
0031 
0032 
0033 namespace Latte {
0034 namespace WindowSystem {
0035 namespace Tracker {
0036 
0037 Schemes::Schemes(AbstractWindowInterface *parent)
0038     : QObject(parent)
0039 {
0040     m_wm = parent;
0041     init();
0042 }
0043 
0044 Schemes::~Schemes()
0045 {
0046     m_windowScheme.clear();
0047     //! it is just a reference to a real scheme file
0048     m_schemes.take("kdeglobals");
0049     qDeleteAll(m_schemes);
0050     m_schemes.clear();
0051 }
0052 
0053 void Schemes::init()
0054 {
0055     updateDefaultScheme();
0056 
0057     connect(this, &Schemes::colorSchemeChanged, this, [&](WindowId wid) {
0058         if (wid == m_wm->activeWindow()) {
0059             emit m_wm->activeWindowChanged(wid);
0060         }
0061     });
0062 
0063     connect(m_wm, &AbstractWindowInterface::windowRemoved, this, [&](WindowId wid) {
0064         m_windowScheme.remove(wid);
0065     });
0066 
0067     //! track for changing default scheme
0068     QString kdeSettingsFile = QDir::homePath() + "/.config/kdeglobals";
0069 
0070     KDirWatch::self()->addFile(kdeSettingsFile);
0071 
0072     connect(KDirWatch::self(), &KDirWatch::dirty, this, [ &, kdeSettingsFile](const QString & path) {
0073         if (path == kdeSettingsFile) {
0074             this->updateDefaultScheme();
0075         }
0076     });
0077 
0078     connect(KDirWatch::self(), &KDirWatch::created, this, [ &, kdeSettingsFile](const QString & path) {
0079         if (path == kdeSettingsFile) {
0080             this->updateDefaultScheme();
0081         }
0082     });
0083 }
0084 
0085 //! Scheme support for windows
0086 void Schemes::updateDefaultScheme()
0087 {
0088     QString defaultSchemePath = SchemeColors::possibleSchemeFile("kdeglobals");
0089 
0090     qDebug() << " Windows default color scheme :: " << defaultSchemePath;
0091 
0092     SchemeColors *dScheme;
0093 
0094     if (!m_schemes.contains(defaultSchemePath)) {
0095         dScheme = new SchemeColors(this, defaultSchemePath);
0096         m_schemes[defaultSchemePath] = dScheme;
0097     } else {
0098         dScheme = m_schemes[defaultSchemePath];
0099     }
0100 
0101     if (!m_schemes.contains("kdeglobals") || m_schemes["kdeglobals"]->schemeFile() != defaultSchemePath) {
0102         m_schemes["kdeglobals"] = dScheme;
0103     }
0104 }
0105 
0106 SchemeColors *Schemes::schemeForWindow(WindowId wid)
0107 {
0108     if (!m_windowScheme.contains(wid)) {
0109         return m_schemes["kdeglobals"];
0110     } else {
0111         return m_schemes[m_windowScheme[wid]];
0112     }
0113 
0114     return nullptr;
0115 }
0116 
0117 void Schemes::setColorSchemeForWindow(WindowId wid, QString scheme)
0118 {
0119     if (scheme == "kdeglobals" && !m_windowScheme.contains(wid)) {
0120         //default scheme does not have to be set
0121         return;
0122     }
0123 
0124     if (scheme == "kdeglobals") {
0125         //! a window that previously had an explicit set scheme now is set back to default scheme
0126         m_windowScheme.remove(wid);
0127     } else {
0128         QString schemeFile = SchemeColors::possibleSchemeFile(scheme);
0129 
0130         if (!m_schemes.contains(schemeFile)) {
0131             //! when this scheme file has not been loaded yet
0132             m_schemes[schemeFile] = new SchemeColors(this, schemeFile);
0133         }
0134 
0135         m_windowScheme[wid] = schemeFile;
0136     }
0137 
0138     emit colorSchemeChanged(wid);
0139 }
0140 
0141 }
0142 }
0143 }